1 // SPDX-License-Identifier: GPL-2.0+
2 /* getline.c -- Replacement for GNU C library function getline
4 * Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc.
7 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
12 /* Always add at least this many bytes when extending the buffer. */
15 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
16 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
17 malloc (or NULL), pointing to *N characters of space. It is realloc'd
18 as necessary. Return the number of characters read (not including the
19 null terminator), or -1 on error or EOF.
20 NOTE: There is another getstr() function declared in <curses.h>. */
21 static int getstr(char **lineptr
, size_t *n
, FILE *stream
,
22 char terminator
, size_t offset
)
24 int nchars_avail
; /* Allocated but unused chars in *LINEPTR. */
25 char *read_pos
; /* Where we're reading into *LINEPTR. */
28 if (!lineptr
|| !n
|| !stream
)
33 *lineptr
= malloc(*n
);
38 nchars_avail
= *n
- offset
;
39 read_pos
= *lineptr
+ offset
;
42 register int c
= getc(stream
);
44 /* We always want at least one char left in the buffer, since we
45 always (unless we get an error while reading the first char)
46 NUL-terminate the line buffer. */
48 assert(*n
- nchars_avail
== read_pos
- *lineptr
);
49 if (nchars_avail
< 2) {
55 nchars_avail
= *n
+ *lineptr
- read_pos
;
56 *lineptr
= realloc(*lineptr
, *n
);
59 read_pos
= *n
- nchars_avail
+ *lineptr
;
60 assert(*n
- nchars_avail
== read_pos
- *lineptr
);
63 if (c
== EOF
|| ferror (stream
)) {
64 /* Return partial line, if any. */
65 if (read_pos
== *lineptr
)
75 /* Return the line. */
79 /* Done - NUL terminate and return the number of chars read. */
82 ret
= read_pos
- (*lineptr
+ offset
);
86 int getline (char **lineptr
, size_t *n
, FILE *stream
)
88 return getstr(lineptr
, n
, stream
, '\n', 0);