]>
diplodocus.org Git - nmh/blob - sbr/getline.c
2 * getline.c -- replacement getline() implementation
4 * This code is Copyright (c) 2016, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
16 /* Largest possible size of buffer that allows SSIZE_MAX to be returned
17 * to indicate SSIZE_MAX - 1 characters read before the '\n'. The
18 * additional 1 is for the terminating NUL. */
19 #define MAX_AVAIL ((size_t)SSIZE_MAX + 1)
21 /* Ideal increase in size of line buffer. */
24 ssize_t
getline(char **lineptr
, size_t *n
, FILE *stream
)
39 avail
= *n
<= MAX_AVAIL
? *n
: MAX_AVAIL
;
41 avail
= *n
= 0; /* POSIX allows *lineptr = NULL, *n = 42. */
47 if (ferror(stream
) || /* errno set by stdlib. */
48 !used
) /* EOF with nothing read. */
50 /* Line will be returned without a \n terminator. */
60 if (avail
== MAX_AVAIL
) {
64 want
= avail
+ GROWTH
;
67 new = realloc(l
, want
);
69 return -1; /* errno set by stdlib. */
76 return used
- 1; /* Don't include NUL. */
79 goto append_nul
; /* Final half loop. */