1 /* getline.c -- Replacement for GNU C library function getline
2
3 Copyright (C) 1993, 1996 Free Software Foundation, Inc.
4
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.
9
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.
14
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. */
18
19 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 /* The `getdelim' function is only declared if the following symbol
26 is defined. */
27 #define _GNU_SOURCE 1
28 #include <stdio.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31
32 #if defined __GNU_LIBRARY__ && HAVE_GETDELIM
33
34 int
getline(lineptr,n,stream)35 getline (lineptr, n, stream)
36 char **lineptr;
37 size_t *n;
38 FILE *stream;
39 {
40 return getdelim (lineptr, n, '\n', stream);
41 }
42
43
44 #else /* ! have getdelim */
45
46 # define NDEBUG
47 # include <assert.h>
48
49 # if STDC_HEADERS
50 # include <stdlib.h>
51 # else
52 char *malloc (), *realloc ();
53 # endif
54
55 /* Always add at least this many bytes when extending the buffer. */
56 # define MIN_CHUNK 64
57
58 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
59 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
60 malloc (or NULL), pointing to *N characters of space. It is realloc'd
61 as necessary. Return the number of characters read (not including the
62 null terminator), or -1 on error or EOF. */
63
64 int
getstr(lineptr,n,stream,terminator,offset)65 getstr (lineptr, n, stream, terminator, offset)
66 char **lineptr;
67 size_t *n;
68 FILE *stream;
69 char terminator;
70 size_t offset;
71 {
72 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
73 char *read_pos; /* Where we're reading into *LINEPTR. */
74 int ret;
75
76 if (!lineptr || !n || !stream)
77 return -1;
78
79 if (!*lineptr)
80 {
81 *n = MIN_CHUNK;
82 *lineptr = malloc (*n);
83 if (!*lineptr)
84 return -1;
85 }
86
87 nchars_avail = *n - offset;
88 read_pos = *lineptr + offset;
89
90 for (;;)
91 {
92 register int c = getc (stream);
93
94 /* We always want at least one char left in the buffer, since we
95 always (unless we get an error while reading the first char)
96 NUL-terminate the line buffer. */
97
98 assert(*n - nchars_avail == read_pos - *lineptr);
99 if (nchars_avail < 2)
100 {
101 if (*n > MIN_CHUNK)
102 *n *= 2;
103 else
104 *n += MIN_CHUNK;
105
106 nchars_avail = *n + *lineptr - read_pos;
107 *lineptr = realloc (*lineptr, *n);
108 if (!*lineptr)
109 return -1;
110 read_pos = *n - nchars_avail + *lineptr;
111 assert(*n - nchars_avail == read_pos - *lineptr);
112 }
113
114 if (c == EOF || ferror (stream))
115 {
116 /* Return partial line, if any. */
117 if (read_pos == *lineptr)
118 return -1;
119 else
120 break;
121 }
122
123 *read_pos++ = c;
124 nchars_avail--;
125
126 if (c == terminator)
127 /* Return the line. */
128 break;
129 }
130
131 /* Done - NUL terminate and return the number of chars read. */
132 *read_pos = '\0';
133
134 ret = read_pos - (*lineptr + offset);
135 return ret;
136 }
137
138 #if defined(__FreeBSD__) && __FreeBSD_version < 800067
139 int
getline(lineptr,n,stream)140 getline (lineptr, n, stream)
141 char **lineptr;
142 size_t *n;
143 FILE *stream;
144 {
145 return getstr (lineptr, n, stream, '\n', 0);
146 }
147
148 int
getdelim(lineptr,n,delimiter,stream)149 getdelim (lineptr, n, delimiter, stream)
150 char **lineptr;
151 size_t *n;
152 int delimiter;
153 FILE *stream;
154 {
155 return getstr (lineptr, n, stream, delimiter, 0);
156 }
157 #endif /* __FreeBSD_version < 800067 */
158 #endif
159