Include "getline.h", to check interface.
[gnulib.git] / lib / getline.c
1 /* getline.c -- Replacement for GNU C library function getline
2
3    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "getline.h"
27
28 /* The `getdelim' function is only declared if the following symbol
29    is defined.  */
30 #ifndef _GNU_SOURCE
31 # define _GNU_SOURCE 1
32 #endif
33
34 #include <stdio.h>
35 #include <sys/types.h>
36
37 #if defined __GNU_LIBRARY__ && HAVE_GETDELIM
38
39 int
40 getline (char **lineptr, size_t *n, FILE *stream)
41 {
42   return getdelim (lineptr, n, '\n', stream);
43 }
44
45 #else /* ! have getdelim */
46
47 # if STDC_HEADERS
48 #  include <stdlib.h>
49 # else
50 char *malloc (), *realloc ();
51 # endif
52
53 #include "unlocked-io.h"
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 delimiter DELIM1 from STREAM into *LINEPTR
59    + OFFSET (and NUL-terminate it).  If DELIM2 is non-zero, then read up
60    and including the first occurrence of DELIM1 or DELIM2.  *LINEPTR is
61    a pointer returned from malloc (or NULL), pointing to *N characters of
62    space.  It is realloc'd as necessary.  Return the number of characters
63    read (not including the NUL terminator), or -1 on error or EOF.  */
64
65 static int
66 getdelim2 (char **lineptr, size_t *n, FILE *stream, int delim1, int delim2,
67            size_t offset)
68 {
69   size_t nchars_avail;          /* Allocated but unused chars in *LINEPTR.  */
70   char *read_pos;               /* Where we're reading into *LINEPTR. */
71   int ret;
72
73   if (!lineptr || !n || !stream)
74     return -1;
75
76   if (!*lineptr)
77     {
78       *n = MIN_CHUNK;
79       *lineptr = malloc (*n);
80       if (!*lineptr)
81         return -1;
82     }
83
84   if (*n < offset)
85     return -1;
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       if (nchars_avail < 2)
99         {
100           if (*n > MIN_CHUNK)
101             *n *= 2;
102           else
103             *n += MIN_CHUNK;
104
105           nchars_avail = *n + *lineptr - read_pos;
106           *lineptr = realloc (*lineptr, *n);
107           if (!*lineptr)
108             return -1;
109           read_pos = *n - nchars_avail + *lineptr;
110         }
111
112       if (c == EOF || ferror (stream))
113         {
114           /* Return partial line, if any.  */
115           if (read_pos == *lineptr)
116             return -1;
117           else
118             break;
119         }
120
121       *read_pos++ = c;
122       nchars_avail--;
123
124       if (c == delim1 || (delim2 && c == delim2))
125         /* Return the line.  */
126         break;
127     }
128
129   /* Done - NUL terminate and return the number of chars read.  */
130   *read_pos = '\0';
131
132   ret = read_pos - (*lineptr + offset);
133   return ret;
134 }
135
136
137 int
138 getline (char **lineptr, size_t *n, FILE *stream)
139 {
140   return getdelim2 (lineptr, n, stream, '\n', 0, 0);
141 }
142
143 int
144 getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
145 {
146   return getdelim2 (lineptr, n, stream, delimiter, 0, 0);
147 }
148 #endif