1 /* getline.c -- Replacement for GNU C library function getline
2
3 Copyright (C) 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <stdio.h>
27 #define NDEBUG
28 #include <assert.h>
29
30 #include <stdlib.h>
31
32 /* Always add at least this many bytes when extending the buffer. */
33 #define MIN_CHUNK 64
34
35 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
36 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
37 malloc (or NULL), pointing to *N characters of space. It is realloc'd
38 as necessary. Return the number of characters read (not including the
39 null terminator), or -1 on error or EOF. */
40
41 int
getstr(lineptr,n,stream,terminator,offset)42 getstr (lineptr, n, stream, terminator, offset)
43 char **lineptr;
44 size_t *n;
45 FILE *stream;
46 char terminator;
47 int offset;
48 {
49 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
50 char *read_pos; /* Where we're reading into *LINEPTR. */
51 int ret;
52
53 if (!lineptr || !n || !stream)
54 return -1;
55
56 if (!*lineptr)
57 {
58 *n = MIN_CHUNK;
59 *lineptr = malloc (*n);
60 if (!*lineptr)
61 return -1;
62 }
63
64 nchars_avail = *n - offset;
65 read_pos = *lineptr + offset;
66
67 for (;;)
68 {
69 register int c = getc (stream);
70
71 /* We always want at least one char left in the buffer, since we
72 always (unless we get an error while reading the first char)
73 NUL-terminate the line buffer. */
74
75 assert(*n - nchars_avail == read_pos - *lineptr);
76 if (nchars_avail < 1)
77 {
78 if (*n > MIN_CHUNK)
79 *n *= 2;
80 else
81 *n += MIN_CHUNK;
82
83 nchars_avail = *n + *lineptr - read_pos;
84 *lineptr = realloc (*lineptr, *n);
85 if (!*lineptr)
86 return -1;
87 read_pos = *n - nchars_avail + *lineptr;
88 assert(*n - nchars_avail == read_pos - *lineptr);
89 }
90
91 if (c == EOF || ferror (stream))
92 {
93 /* Return partial line, if any. */
94 if (read_pos == *lineptr)
95 return -1;
96 else
97 break;
98 }
99
100 *read_pos++ = c;
101 nchars_avail--;
102
103 if (c == terminator)
104 /* Return the line. */
105 break;
106 }
107
108 /* Done - NUL terminate and return the number of chars read. */
109 *read_pos = '\0';
110
111 ret = read_pos - (*lineptr + offset);
112 return ret;
113 }
114
115 int
getline(lineptr,n,stream)116 getline (lineptr, n, stream)
117 char **lineptr;
118 size_t *n;
119 FILE *stream;
120 {
121 return getstr (lineptr, n, stream, '\n', 0);
122 }
123