1 /* Taken from uClibc and adapted to GLiv */
3 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
5 * GNU Library General Public License (LGPL) version 2 or later.
7 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
14 /* Note: There is a defect in this function. (size_t vs long). */
17 * Return -1 if error or EOF prior to any chars read.
18 * Return number of chars read (including possible delimiter but not
19 * the terminating nul) otherwise.
21 * NOTE: If we need to allocate a buffer, we do so prior to attempting
22 * a reading. So space may be allocated even if initially at EOF.
24 #define GETDELIM_GROWBY 64
25 long getdelim(char ** lineptr
, size_t * n
,
26 int delimiter
, FILE * stream
)
32 if (lineptr
&& n
&& stream
) {
33 if (!(buf
= *lineptr
)) { /* If passed NULL for buffer, */
34 *n
= 0; /* ignore value passed and treat size as 0. */
37 /* Within the loop, pos is actually the current buffer index + 2,
38 * because we want to make sure we have enough space to store
39 * an additional char plus a nul terminator.
45 if (!(buf
= realloc(buf
, *n
+ GETDELIM_GROWBY
))) {
49 *n
+= GETDELIM_GROWBY
;
53 if ((c
= fgetc(stream
)) != EOF
) {
60 /* We're done, so correct pos back to being the current index. */
61 if ((pos
-= 2) >= 0) {