Add comma after e.g. in comment.
[coreutils.git] / lib / getline.c
blobe95aace34367dd118cdea2f9abf65a372f9a87fd
1 /* getline.c -- Replacement for GNU C library function getline
3 Copyright (C) 1993 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <sys/types.h>
28 #if defined __GNU_LIBRARY__ && defined HAVE_GETDELIM
30 int
31 getline (lineptr, n, stream)
32 char **lineptr;
33 size_t *n;
34 FILE *stream;
36 return getdelim (lineptr, n, '\n', stream);
40 #else /* ! have getdelim */
42 #define NDEBUG
43 #include <assert.h>
45 #if STDC_HEADERS
46 # include <stdlib.h>
47 #else
48 char *malloc (), *realloc ();
49 #endif
51 /* Always add at least this many bytes when extending the buffer. */
52 #define MIN_CHUNK 64
54 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
55 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
56 malloc (or NULL), pointing to *N characters of space. It is realloc'd
57 as necessary. Return the number of characters read (not including the
58 null terminator), or -1 on error or EOF. */
60 int
61 getstr (lineptr, n, stream, terminator, offset)
62 char **lineptr;
63 size_t *n;
64 FILE *stream;
65 char terminator;
66 size_t offset;
68 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
69 char *read_pos; /* Where we're reading into *LINEPTR. */
70 int ret;
72 if (!lineptr || !n || !stream)
73 return -1;
75 if (!*lineptr)
77 *n = MIN_CHUNK;
78 *lineptr = malloc (*n);
79 if (!*lineptr)
80 return -1;
83 nchars_avail = *n - offset;
84 read_pos = *lineptr + offset;
86 for (;;)
88 register int c = getc (stream);
90 /* We always want at least one char left in the buffer, since we
91 always (unless we get an error while reading the first char)
92 NUL-terminate the line buffer. */
94 assert(*n - nchars_avail == read_pos - *lineptr);
95 if (nchars_avail < 2)
97 if (*n > MIN_CHUNK)
98 *n *= 2;
99 else
100 *n += MIN_CHUNK;
102 nchars_avail = *n + *lineptr - read_pos;
103 *lineptr = realloc (*lineptr, *n);
104 if (!*lineptr)
105 return -1;
106 read_pos = *n - nchars_avail + *lineptr;
107 assert(*n - nchars_avail == read_pos - *lineptr);
110 if (c == EOF || ferror (stream))
112 /* Return partial line, if any. */
113 if (read_pos == *lineptr)
114 return -1;
115 else
116 break;
119 *read_pos++ = c;
120 nchars_avail--;
122 if (c == terminator)
123 /* Return the line. */
124 break;
127 /* Done - NUL terminate and return the number of chars read. */
128 *read_pos = '\0';
130 ret = read_pos - (*lineptr + offset);
131 return ret;
135 getline (lineptr, n, stream)
136 char **lineptr;
137 size_t *n;
138 FILE *stream;
140 return getstr (lineptr, n, stream, '\n', 0);
144 getdelim (lineptr, n, delimiter, stream)
145 char **lineptr;
146 size_t *n;
147 int delimiter;
148 FILE *stream;
150 return getstr (lineptr, n, stream, delimiter, 0);
152 #endif