Add lib/fchown-stub.c.
[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
31 #include "unlocked-io.h"
32
33 #include <limits.h>
34 #if HAVE_INTTYPES_H
35 # include <inttypes.h>
36 #endif
37 #if HAVE_STDINT_H
38 # include <stdint.h>
39 #endif
40 #ifndef PTRDIFF_MAX
41 # define PTRDIFF_MAX ((ptrdiff_t) (SIZE_MAX / 2))
42 #endif
43 #ifndef SIZE_MAX
44 # define SIZE_MAX ((size_t) -1)
45 #endif
46 #ifndef SSIZE_MAX
47 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
48 #endif
49
50 /* The maximum value that getndelim2 can return without suffering from
51    overflow problems, either internally (because of pointer
52    subtraction overflow) or due to the API (because of ssize_t).  */
53 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
54
55 /* Try to add at least this many bytes when extending the buffer.
56    MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM.  */
57 #define MIN_CHUNK 64
58
59 ssize_t
60 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
61             int delim1, int delim2, FILE *stream)
62 {
63   size_t nbytes_avail;          /* Allocated but unused bytes in *LINEPTR.  */
64   char *read_pos;               /* Where we're reading into *LINEPTR. */
65   ssize_t bytes_stored = -1;
66   char *ptr = *lineptr;
67   size_t size = *linesize;
68
69   if (!ptr)
70     {
71       size = nmax < MIN_CHUNK ? nmax : MIN_CHUNK;
72       ptr = malloc (size);
73       if (!ptr)
74         return -1;
75     }
76
77   if (size < offset)
78     goto done;
79
80   nbytes_avail = size - offset;
81   read_pos = ptr + offset;
82
83   if (nbytes_avail == 0 && nmax <= size)
84     goto done;
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 done;
109               newsize = newsizemax;
110             }
111
112           nbytes_avail = newsize - (read_pos - ptr);
113           newptr = realloc (ptr, newsize);
114           if (!newptr)
115             goto 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 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  done:
149   *lineptr = ptr;
150   *linesize = size;
151   return bytes_stored;
152 }