(jm_FUNC_NANOSLEEP): New file/macro.
[gnulib.git] / lib / getline.c
1 /* getline.c -- Replacement for GNU C library function getline
2
3 Copyright (C) 1993, 1996, 1997, 1998 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 #ifndef _GNU_SOURCE
28 # define _GNU_SOURCE 1
29 #endif
30
31 #include <stdio.h>
32 #include <sys/types.h>
33
34 #if defined __GNU_LIBRARY__ && HAVE_GETDELIM
35
36 int
37 getline (char **lineptr, size_t *n, FILE *stream)
38 {
39   return getdelim (lineptr, n, '\n', stream);
40 }
41
42
43 #else /* ! have getdelim */
44
45 # define NDEBUG
46 # include <assert.h>
47
48 # if STDC_HEADERS
49 #  include <stdlib.h>
50 # else
51 char *malloc (), *realloc ();
52 # endif
53
54 /* Always add at least this many bytes when extending the buffer.  */
55 # define MIN_CHUNK 64
56
57 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
58    + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
59    malloc (or NULL), pointing to *N characters of space.  It is realloc'd
60    as necessary.  Return the number of characters read (not including the
61    null terminator), or -1 on error or EOF.  */
62
63 int
64 getstr (char **lineptr, size_t *n, FILE *stream, char terminator, size_t offset)
65 {
66   int nchars_avail;             /* Allocated but unused chars in *LINEPTR.  */
67   char *read_pos;               /* Where we're reading into *LINEPTR. */
68   int ret;
69
70   if (!lineptr || !n || !stream)
71     return -1;
72
73   if (!*lineptr)
74     {
75       *n = MIN_CHUNK;
76       *lineptr = malloc (*n);
77       if (!*lineptr)
78         return -1;
79     }
80
81   nchars_avail = *n - offset;
82   read_pos = *lineptr + offset;
83
84   for (;;)
85     {
86       register int c = getc (stream);
87
88       /* We always want at least one char left in the buffer, since we
89          always (unless we get an error while reading the first char)
90          NUL-terminate the line buffer.  */
91
92       assert(*n - nchars_avail == read_pos - *lineptr);
93       if (nchars_avail < 2)
94         {
95           if (*n > MIN_CHUNK)
96             *n *= 2;
97           else
98             *n += MIN_CHUNK;
99
100           nchars_avail = *n + *lineptr - read_pos;
101           *lineptr = realloc (*lineptr, *n);
102           if (!*lineptr)
103             return -1;
104           read_pos = *n - nchars_avail + *lineptr;
105           assert(*n - nchars_avail == read_pos - *lineptr);
106         }
107
108       if (c == EOF || ferror (stream))
109         {
110           /* Return partial line, if any.  */
111           if (read_pos == *lineptr)
112             return -1;
113           else
114             break;
115         }
116
117       *read_pos++ = c;
118       nchars_avail--;
119
120       if (c == terminator)
121         /* Return the line.  */
122         break;
123     }
124
125   /* Done - NUL terminate and return the number of chars read.  */
126   *read_pos = '\0';
127
128   ret = read_pos - (*lineptr + offset);
129   return ret;
130 }
131
132 int
133 getline (char **lineptr, size_t *n, FILE *stream)
134 {
135   return getstr (lineptr, n, stream, '\n', 0);
136 }
137
138 int
139 getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
140 {
141   return getstr (lineptr, n, stream, delimiter, 0);
142 }
143 #endif