Add and change modules to make it easier for coreutils to use
[gnulib.git] / lib / getndelim2.c
1 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
2    with bounded memory allocation.
3
4    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004, 2006 Free
5    Software Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include "getndelim2.h"
28
29 #include <stdlib.h>
30 #include <stddef.h>
31
32 #if USE_UNLOCKED_IO
33 # include "unlocked-io.h"
34 #endif
35
36 #include <limits.h>
37 #include <stdint.h>
38
39 /* The maximum value that getndelim2 can return without suffering from
40    overflow problems, either internally (because of pointer
41    subtraction overflow) or due to the API (because of ssize_t).  */
42 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
43
44 /* Try to add at least this many bytes when extending the buffer.
45    MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM.  */
46 #define MIN_CHUNK 64
47
48 ssize_t
49 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
50             int delim1, int delim2, FILE *stream)
51 {
52   size_t nbytes_avail;          /* Allocated but unused bytes in *LINEPTR.  */
53   char *read_pos;               /* Where we're reading into *LINEPTR. */
54   ssize_t bytes_stored = -1;
55   char *ptr = *lineptr;
56   size_t size = *linesize;
57
58   if (!ptr)
59     {
60       size = nmax < MIN_CHUNK ? nmax : MIN_CHUNK;
61       ptr = malloc (size);
62       if (!ptr)
63         return -1;
64     }
65
66   if (size < offset)
67     goto done;
68
69   nbytes_avail = size - offset;
70   read_pos = ptr + offset;
71
72   if (nbytes_avail == 0 && nmax <= size)
73     goto done;
74
75   for (;;)
76     {
77       /* Here always ptr + size == read_pos + nbytes_avail.  */
78
79       int c;
80
81       /* We always want at least one byte left in the buffer, since we
82          always (unless we get an error while reading the first byte)
83          NUL-terminate the line buffer.  */
84
85       if (nbytes_avail < 2 && size < nmax)
86         {
87           size_t newsize = size < MIN_CHUNK ? size + MIN_CHUNK : 2 * size;
88           char *newptr;
89
90           if (! (size < newsize && newsize <= nmax))
91             newsize = nmax;
92
93           if (GETNDELIM2_MAXIMUM < newsize - offset)
94             {
95               size_t newsizemax = offset + GETNDELIM2_MAXIMUM + 1;
96               if (size == newsizemax)
97                 goto done;
98               newsize = newsizemax;
99             }
100
101           nbytes_avail = newsize - (read_pos - ptr);
102           newptr = realloc (ptr, newsize);
103           if (!newptr)
104             goto done;
105           ptr = newptr;
106           size = newsize;
107           read_pos = size - nbytes_avail + ptr;
108         }
109
110       c = getc (stream);
111       if (c == EOF)
112         {
113           /* Return partial line, if any.  */
114           if (read_pos == ptr)
115             goto done;
116           else
117             break;
118         }
119
120       if (nbytes_avail >= 2)
121         {
122           *read_pos++ = c;
123           nbytes_avail--;
124         }
125
126       if (c == delim1 || c == delim2)
127         /* Return the line.  */
128         break;
129     }
130
131   /* Done - NUL terminate and return the number of bytes read.
132      At this point we know that nbytes_avail >= 1.  */
133   *read_pos = '\0';
134
135   bytes_stored = read_pos - (ptr + offset);
136
137  done:
138   *lineptr = ptr;
139   *linesize = size;
140   return bytes_stored;
141 }