1 /* Copyright 2002, Red Hat Inc. - all rights reserved */
4 <<getdelim>>---read a line up to a specified line delimiter
11 int getdelim(char **<[bufptr]>, size_t *<[n]>,
12 int <[delim]>, FILE *<[fp]>);
15 <<getdelim>> reads a file <[fp]> up to and possibly including a specified
16 delimiter <[delim]>. The line is read into a buffer pointed to
17 by <[bufptr]> and designated with size *<[n]>. If the buffer is
18 not large enough, it will be dynamically grown by <<getdelim>>.
19 As the buffer is grown, the pointer to the size <[n]> will be
23 <<getdelim>> returns <<-1>> if no characters were successfully read;
24 otherwise, it returns the number of bytes successfully read.
25 At end of file, the result is nonzero.
28 <<getdelim>> is a glibc extension.
30 No supporting OS subroutines are directly required.
39 #define MIN_LINE_SIZE 4
40 #define DEFAULT_LINE_SIZE 128
43 __getdelim (char **bufptr
,
50 size_t newsize
, numbytes
;
55 if (fp
== NULL
|| bufptr
== NULL
|| n
== NULL
)
62 if (buf
== NULL
|| *n
< MIN_LINE_SIZE
)
64 buf
= (char *)realloc (*bufptr
, DEFAULT_LINE_SIZE
);
70 *n
= DEFAULT_LINE_SIZE
;
73 CHECK_INIT (_REENT
, fp
);
75 _newlib_flockfile_start (fp
);
84 /* fill buffer - leaving room for nul-terminator */
85 while (--numbytes
> 0)
87 if ((ch
= getc_unlocked (fp
)) == EOF
)
105 /* Buffer is too small so reallocate a larger buffer. */
108 buf
= realloc (buf
, newsize
);
115 /* After reallocating, continue in new buffer */
119 numbytes
= newsize
- pos
;
123 _newlib_flockfile_end (fp
);
125 /* if no input data, return failure */
129 /* otherwise, nul-terminate and return number of bytes read */
131 return (ssize_t
)(ptr
- buf
);