*** empty log message ***
[findutils.git] / lib / getline.c
blob855743137ea7776a794d7a178df43db5d34e79ad
1 /* getline.c -- Replacement for GNU C library function getline
3 Copyright (C) 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
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.
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.
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. */
19 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
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
31 #include <stdio.h>
32 #include <sys/types.h>
34 /* define getstr even if the other functions are unneeded */
35 # define NDEBUG
36 # include <assert.h>
38 # if STDC_HEADERS
39 # include <stdlib.h>
40 # else
41 char *malloc (), *realloc ();
42 # endif
44 /* Always add at least this many bytes when extending the buffer. */
45 # define MIN_CHUNK 64
47 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
48 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
49 malloc (or NULL), pointing to *N characters of space. It is realloc'd
50 as necessary. Return the number of characters read (not including the
51 null terminator), or -1 on error or EOF. */
53 ssize_t
54 getstr (char **lineptr, size_t *n, FILE *stream, char terminator, size_t offset)
56 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
57 char *read_pos; /* Where we're reading into *LINEPTR. */
58 ssize_t ret;
60 if (!lineptr || !n || !stream)
61 return -1;
63 if (!*lineptr)
65 *n = MIN_CHUNK;
66 *lineptr = malloc (*n);
67 if (!*lineptr)
68 return -1;
71 nchars_avail = *n - offset;
72 read_pos = *lineptr + offset;
74 for (;;)
76 register int c = getc (stream);
78 /* We always want at least one char left in the buffer, since we
79 always (unless we get an error while reading the first char)
80 NUL-terminate the line buffer. */
82 assert(*n - nchars_avail == read_pos - *lineptr);
83 if (nchars_avail < 2)
85 if (*n > MIN_CHUNK)
86 *n *= 2;
87 else
88 *n += MIN_CHUNK;
90 nchars_avail = *n + *lineptr - read_pos;
91 *lineptr = realloc (*lineptr, *n);
92 if (!*lineptr)
93 return -1;
94 read_pos = *n - nchars_avail + *lineptr;
95 assert(*n - nchars_avail == read_pos - *lineptr);
98 if (c == EOF || ferror (stream))
100 /* Return partial line, if any. */
101 if (read_pos == *lineptr)
102 return -1;
103 else
104 break;
107 *read_pos++ = c;
108 nchars_avail--;
110 if (c == terminator)
111 /* Return the line. */
112 break;
115 /* Done - NUL terminate and return the number of chars read. */
116 *read_pos = '\0';
118 ret = read_pos - (*lineptr + offset);
119 return ret;
122 #ifndef HAVE_GETLINE
123 #if defined __GNU_LIBRARY__ && HAVE_GETDELIM
124 ssize_t
125 getline (char **lineptr, size_t *n, FILE *stream)
127 return getdelim (lineptr, n, '\n', stream);
130 #else /* ! have getdelim */
132 ssize_t
133 getline (char **lineptr, size_t *n, FILE *stream)
135 return getstr (lineptr, n, stream, '\n', 0);
138 #endif
139 #endif
142 #if ! (defined __GNU_LIBRARY__ && HAVE_GETDELIM)
143 ssize_t
144 getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream)
146 return getstr (lineptr, n, stream, delimiter, 0);
148 #endif