New version from gettext-0.10.12.
[gnulib.git] / lib / getline.c
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <stdio.h>
26 #include <sys/types.h>
27
28 #if defined __GNU_LIBRARY__ && defined HAVE_GETDELIM
29
30 int
31 getline (lineptr, n, stream)
32      char **lineptr;
33      size_t *n;
34      FILE *stream;
35 {
36   return getdelim (lineptr, n, '\n', stream);
37 }
38
39
40 #else /* ! have getdelim */
41
42 #define NDEBUG
43 #include <assert.h>
44
45 #if STDC_HEADERS
46 # include <stdlib.h>
47 #else
48 char *malloc (), *realloc ();
49 #endif
50
51 /* Always add at least this many bytes when extending the buffer.  */
52 #define MIN_CHUNK 64
53
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.  */
59
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;
67 {
68   int nchars_avail;             /* Allocated but unused chars in *LINEPTR.  */
69   char *read_pos;               /* Where we're reading into *LINEPTR. */
70   int ret;
71
72   if (!lineptr || !n || !stream)
73     return -1;
74
75   if (!*lineptr)
76     {
77       *n = MIN_CHUNK;
78       *lineptr = malloc (*n);
79       if (!*lineptr)
80         return -1;
81     }
82
83   nchars_avail = *n - offset;
84   read_pos = *lineptr + offset;
85
86   for (;;)
87     {
88       register int c = getc (stream);
89
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.  */
93
94       assert(*n - nchars_avail == read_pos - *lineptr);
95       if (nchars_avail < 2)
96         {
97           if (*n > MIN_CHUNK)
98             *n *= 2;
99           else
100             *n += MIN_CHUNK;
101
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);
108         }
109
110       if (c == EOF || ferror (stream))
111         {
112           /* Return partial line, if any.  */
113           if (read_pos == *lineptr)
114             return -1;
115           else
116             break;
117         }
118
119       *read_pos++ = c;
120       nchars_avail--;
121
122       if (c == terminator)
123         /* Return the line.  */
124         break;
125     }
126
127   /* Done - NUL terminate and return the number of chars read.  */
128   *read_pos = '\0';
129
130   ret = read_pos - (*lineptr + offset);
131   return ret;
132 }
133
134 int
135 getline (lineptr, n, stream)
136      char **lineptr;
137      size_t *n;
138      FILE *stream;
139 {
140   return getstr (lineptr, n, stream, '\n', 0);
141 }
142
143 int
144 getdelim (lineptr, n, delimiter, stream)
145      char **lineptr;
146      size_t *n;
147      int delimiter;
148      FILE *stream;
149 {
150   return getstr (lineptr, n, stream, delimiter, 0);
151 }
152 #endif