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