Test getndelim2.
[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 <stdlib.h>
27 #include <stddef.h>
28
29 #if USE_UNLOCKED_IO
30 # include "unlocked-io.h"
31 #endif
32 #if !HAVE_FLOCKFILE
33 # undef flockfile
34 # define flockfile(x) ((void) 0)
35 #endif
36 #if !HAVE_FUNLOCKFILE
37 # undef funlockfile
38 # define funlockfile(x) ((void) 0)
39 #endif
40
41 #include <limits.h>
42 #include <stdint.h>
43
44 #ifndef SSIZE_MAX
45 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
46 #endif
47
48 /* The maximum value that getndelim2 can return without suffering from
49    overflow problems, either internally (because of pointer
50    subtraction overflow) or due to the API (because of ssize_t).  */
51 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
52
53 /* Try to add at least this many bytes when extending the buffer.
54    MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM.  */
55 #define MIN_CHUNK 64
56
57 ssize_t
58 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
59             int delim1, int delim2, FILE *stream)
60 {
61   size_t nbytes_avail;          /* Allocated but unused bytes in *LINEPTR.  */
62   char *read_pos;               /* Where we're reading into *LINEPTR. */
63   ssize_t bytes_stored = -1;
64   char *ptr = *lineptr;
65   size_t size = *linesize;
66
67   if (!ptr)
68     {
69       size = nmax < MIN_CHUNK ? nmax : MIN_CHUNK;
70       ptr = malloc (size);
71       if (!ptr)
72         return -1;
73     }
74
75   if (size < offset)
76     goto done;
77
78   nbytes_avail = size - offset;
79   read_pos = ptr + offset;
80
81   if (nbytes_avail == 0 && nmax <= size)
82     goto done;
83
84   flockfile (stream);
85
86   for (;;)
87     {
88       /* Here always ptr + size == read_pos + nbytes_avail.  */
89
90       int c;
91
92       /* We always want at least one byte left in the buffer, since we
93          always (unless we get an error while reading the first byte)
94          NUL-terminate the line buffer.  */
95
96       if (nbytes_avail < 2 && size < nmax)
97         {
98           size_t newsize = size < MIN_CHUNK ? size + MIN_CHUNK : 2 * size;
99           char *newptr;
100
101           if (! (size < newsize && newsize <= nmax))
102             newsize = nmax;
103
104           if (GETNDELIM2_MAXIMUM < newsize - offset)
105             {
106               size_t newsizemax = offset + GETNDELIM2_MAXIMUM + 1;
107               if (size == newsizemax)
108                 goto unlock_done;
109               newsize = newsizemax;
110             }
111
112           nbytes_avail = newsize - (read_pos - ptr);
113           newptr = realloc (ptr, newsize);
114           if (!newptr)
115             goto unlock_done;
116           ptr = newptr;
117           size = newsize;
118           read_pos = size - nbytes_avail + ptr;
119         }
120
121       c = getc (stream);
122       if (c == EOF)
123         {
124           /* Return partial line, if any.  */
125           if (read_pos == ptr)
126             goto unlock_done;
127           else
128             break;
129         }
130
131       if (nbytes_avail >= 2)
132         {
133           *read_pos++ = c;
134           nbytes_avail--;
135         }
136
137       if (c == delim1 || c == delim2)
138         /* Return the line.  */
139         break;
140     }
141
142   /* Done - NUL terminate and return the number of bytes read.
143      At this point we know that nbytes_avail >= 1.  */
144   *read_pos = '\0';
145
146   bytes_stored = read_pos - (ptr + offset);
147
148  unlock_done:
149   funlockfile (stream);
150
151  done:
152   *lineptr = ptr;
153   *linesize = size;
154   return bytes_stored ? bytes_stored : -1;
155 }