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