Add tests with filenames containing newline and backslash characters.
[coreutils.git] / lib / getline.c
blobc028c6581b6e857bac92dc6dad16095171b3bbb4
1 /* getline.c -- Replacement for GNU C library function getline
3 Copyright (C) 1993, 1996, 1997 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 #if defined __GNU_LIBRARY__ && HAVE_GETDELIM
36 int
37 getline (lineptr, n, stream)
38 char **lineptr;
39 size_t *n;
40 FILE *stream;
42 return getdelim (lineptr, n, '\n', stream);
46 #else /* ! have getdelim */
48 # define NDEBUG
49 # include <assert.h>
51 # if STDC_HEADERS
52 # include <stdlib.h>
53 # else
54 char *malloc (), *realloc ();
55 # endif
57 /* Always add at least this many bytes when extending the buffer. */
58 # define MIN_CHUNK 64
60 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
61 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
62 malloc (or NULL), pointing to *N characters of space. It is realloc'd
63 as necessary. Return the number of characters read (not including the
64 null terminator), or -1 on error or EOF. */
66 int
67 getstr (lineptr, n, stream, terminator, offset)
68 char **lineptr;
69 size_t *n;
70 FILE *stream;
71 char terminator;
72 size_t offset;
74 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */
75 char *read_pos; /* Where we're reading into *LINEPTR. */
76 int ret;
78 if (!lineptr || !n || !stream)
79 return -1;
81 if (!*lineptr)
83 *n = MIN_CHUNK;
84 *lineptr = malloc (*n);
85 if (!*lineptr)
86 return -1;
89 nchars_avail = *n - offset;
90 read_pos = *lineptr + offset;
92 for (;;)
94 register int c = getc (stream);
96 /* We always want at least one char left in the buffer, since we
97 always (unless we get an error while reading the first char)
98 NUL-terminate the line buffer. */
100 assert(*n - nchars_avail == read_pos - *lineptr);
101 if (nchars_avail < 2)
103 if (*n > MIN_CHUNK)
104 *n *= 2;
105 else
106 *n += MIN_CHUNK;
108 nchars_avail = *n + *lineptr - read_pos;
109 *lineptr = realloc (*lineptr, *n);
110 if (!*lineptr)
111 return -1;
112 read_pos = *n - nchars_avail + *lineptr;
113 assert(*n - nchars_avail == read_pos - *lineptr);
116 if (c == EOF || ferror (stream))
118 /* Return partial line, if any. */
119 if (read_pos == *lineptr)
120 return -1;
121 else
122 break;
125 *read_pos++ = c;
126 nchars_avail--;
128 if (c == terminator)
129 /* Return the line. */
130 break;
133 /* Done - NUL terminate and return the number of chars read. */
134 *read_pos = '\0';
136 ret = read_pos - (*lineptr + offset);
137 return ret;
141 getline (lineptr, n, stream)
142 char **lineptr;
143 size_t *n;
144 FILE *stream;
146 return getstr (lineptr, n, stream, '\n', 0);
150 getdelim (lineptr, n, delimiter, stream)
151 char **lineptr;
152 size_t *n;
153 int delimiter;
154 FILE *stream;
156 return getstr (lineptr, n, stream, delimiter, 0);
158 #endif