1 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
2 with bounded memory allocation.
4 Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004 Free Software
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)
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.
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. */
21 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu. */
27 #include "getndelim2.h"
33 # include "unlocked-io.h"
38 # include <inttypes.h>
44 # define PTRDIFF_MAX ((ptrdiff_t) (SIZE_MAX / 2))
47 # define SIZE_MAX ((size_t) -1)
50 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
53 /* The maximum value that getndelim2 can return without suffering from
54 overflow problems, either internally (because of pointer
55 subtraction overflow) or due to the API (because of ssize_t). */
56 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
58 /* Try to add at least this many bytes when extending the buffer.
59 MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM. */
63 getndelim2 (char **lineptr
, size_t *linesize
, size_t offset
, size_t nmax
,
64 int delim1
, int delim2
, FILE *stream
)
66 size_t nbytes_avail
; /* Allocated but unused bytes in *LINEPTR. */
67 char *read_pos
; /* Where we're reading into *LINEPTR. */
68 ssize_t bytes_stored
= -1;
70 size_t size
= *linesize
;
74 size
= nmax
< MIN_CHUNK
? nmax
: MIN_CHUNK
;
83 nbytes_avail
= size
- offset
;
84 read_pos
= ptr
+ offset
;
86 if (nbytes_avail
== 0 && nmax
<= size
)
91 /* Here always ptr + size == read_pos + nbytes_avail. */
95 /* We always want at least one byte left in the buffer, since we
96 always (unless we get an error while reading the first byte)
97 NUL-terminate the line buffer. */
99 if (nbytes_avail
< 2 && size
< nmax
)
101 size_t newsize
= size
< MIN_CHUNK
? size
+ MIN_CHUNK
: 2 * size
;
104 if (! (size
< newsize
&& newsize
<= nmax
))
107 if (GETNDELIM2_MAXIMUM
< newsize
- offset
)
109 size_t newsizemax
= offset
+ GETNDELIM2_MAXIMUM
+ 1;
110 if (size
== newsizemax
)
112 newsize
= newsizemax
;
115 nbytes_avail
= newsize
- (read_pos
- ptr
);
116 newptr
= realloc (ptr
, newsize
);
121 read_pos
= size
- nbytes_avail
+ ptr
;
127 /* Return partial line, if any. */
134 if (nbytes_avail
>= 2)
140 if (c
== delim1
|| c
== delim2
)
141 /* Return the line. */
145 /* Done - NUL terminate and return the number of bytes read.
146 At this point we know that nbytes_avail >= 1. */
149 bytes_stored
= read_pos
- (ptr
+ offset
);