avoid a warning from gcc
[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, 2008 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 3 of the License, or
10    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
19
20 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
21
22 #include <config.h>
23
24 #include "getndelim2.h"
25
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #if USE_UNLOCKED_IO
32 # include "unlocked-io.h"
33 #endif
34 #if !HAVE_FLOCKFILE
35 # undef flockfile
36 # define flockfile(x) ((void) 0)
37 #endif
38 #if !HAVE_FUNLOCKFILE
39 # undef funlockfile
40 # define funlockfile(x) ((void) 0)
41 #endif
42
43 #include <limits.h>
44 #include <stdint.h>
45
46 #include "freadptr.h"
47 #include "freadseek.h"
48 #include "memchr2.h"
49
50 #ifndef SSIZE_MAX
51 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
52 #endif
53
54 /* Use this to suppress gcc's `...may be used before initialized' warnings. */
55 #ifdef lint
56 # define IF_LINT(Code) Code
57 #else
58 # define IF_LINT(Code) /* empty */
59 #endif
60
61 /* The maximum value that getndelim2 can return without suffering from
62    overflow problems, either internally (because of pointer
63    subtraction overflow) or due to the API (because of ssize_t).  */
64 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
65
66 /* Try to add at least this many bytes when extending the buffer.
67    MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM.  */
68 #define MIN_CHUNK 64
69
70 ssize_t
71 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
72             int delim1, int delim2, FILE *stream)
73 {
74   size_t nbytes_avail;          /* Allocated but unused bytes in *LINEPTR.  */
75   char *read_pos;               /* Where we're reading into *LINEPTR. */
76   ssize_t bytes_stored = -1;
77   char *ptr = *lineptr;
78   size_t size = *linesize;
79   bool done = false;
80
81   if (!ptr)
82     {
83       size = nmax < MIN_CHUNK ? nmax : MIN_CHUNK;
84       ptr = malloc (size);
85       if (!ptr)
86         return -1;
87     }
88
89   if (size < offset)
90     goto done;
91
92   nbytes_avail = size - offset;
93   read_pos = ptr + offset;
94
95   if (nbytes_avail == 0 && nmax <= size)
96     goto done;
97
98   /* Normalize delimiters, since memchr2 doesn't handle EOF.  */
99   if (delim1 == EOF)
100     delim1 = delim2;
101   else if (delim2 == EOF)
102     delim2 = delim1;
103
104   flockfile (stream);
105
106   while (!done)
107     {
108       /* Here always ptr + size == read_pos + nbytes_avail.  */
109
110       int c IF_LINT (= 0);
111       const char *buffer;
112       size_t buffer_len;
113
114       buffer = freadptr (stream, &buffer_len);
115       if (buffer)
116         {
117           if (delim1 != EOF)
118             {
119               const char *end = memchr2 (buffer, delim1, delim2, buffer_len);
120               if (end)
121                 {
122                   buffer_len = end - buffer + 1;
123                   done = true;
124                 }
125             }
126         }
127       else
128         {
129           c = getc (stream);
130           if (c == EOF)
131             {
132               /* Return partial line, if any.  */
133               if (read_pos == ptr)
134                 goto unlock_done;
135               else
136                 break;
137             }
138           if (c == delim1 || c == delim2)
139             done = true;
140           buffer_len = 1;
141         }
142
143       /* We always want at least one byte left in the buffer, since we
144          always (unless we get an error while reading the first byte)
145          NUL-terminate the line buffer.  */
146
147       if (nbytes_avail < 1 + buffer_len && size < nmax)
148         {
149           size_t newsize = size < MIN_CHUNK ? size + MIN_CHUNK : 2 * size;
150           char *newptr;
151
152           if (newsize < buffer_len)
153             newsize = buffer_len + size;
154           if (! (size < newsize && newsize <= nmax))
155             newsize = nmax;
156
157           if (GETNDELIM2_MAXIMUM < newsize - offset)
158             {
159               size_t newsizemax = offset + GETNDELIM2_MAXIMUM + 1;
160               if (size == newsizemax)
161                 goto unlock_done;
162               newsize = newsizemax;
163             }
164
165           nbytes_avail = newsize - (read_pos - ptr);
166           newptr = realloc (ptr, newsize);
167           if (!newptr)
168             goto unlock_done;
169           ptr = newptr;
170           size = newsize;
171           read_pos = size - nbytes_avail + ptr;
172         }
173
174       if (1 < nbytes_avail)
175         {
176           size_t copy_len = nbytes_avail - 1;
177           if (buffer_len < copy_len)
178             copy_len = buffer_len;
179           if (buffer)
180             memcpy (read_pos, buffer, copy_len);
181           else
182             *read_pos = c;
183           read_pos += copy_len;
184           nbytes_avail -= copy_len;
185         }
186       if (buffer && freadseek (stream, buffer_len))
187         goto unlock_done;
188     }
189
190   /* Done - NUL terminate and return the number of bytes read.
191      At this point we know that nbytes_avail >= 1.  */
192   *read_pos = '\0';
193
194   bytes_stored = read_pos - (ptr + offset);
195
196  unlock_done:
197   funlockfile (stream);
198
199  done:
200   *lineptr = ptr;
201   *linesize = size;
202   return bytes_stored ? bytes_stored : -1;
203 }