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