1 /* zgetline - read a line of input from a specified file descriptor and return
2 a pointer to a newly-allocated buffer containing the data. */
4 /* Copyright (C) 2008-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bush is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bush. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/types.h>
26 #if defined (HAVE_UNISTD_H)
37 extern ssize_t zread
PARAMS((int, char *, size_t));
38 extern ssize_t zreadc
PARAMS((int, char *));
39 extern ssize_t zreadintr
PARAMS((int, char *, size_t));
40 extern ssize_t zreadcintr
PARAMS((int, char *));
42 typedef ssize_t breadfunc_t
PARAMS((int, char *, size_t));
43 typedef ssize_t creadfunc_t
PARAMS((int, char *));
45 /* Initial memory allocation for automatic growing buffer in zreadlinec */
46 #define GET_LINE_INITIAL_ALLOCATION 16
48 /* Derived from GNU libc's getline.
49 The behavior is almost the same as getline. See man getline.
51 (1) using file descriptor instead of FILE *;
52 (2) the order of arguments: the file descriptor comes first;
53 (3) the addition of a fourth argument, DELIM; sets the delimiter to
54 be something other than newline if desired. If setting DELIM,
55 the next argument should be 1; and
56 (4) the addition of a fifth argument, UNBUFFERED_READ; this argument
57 controls whether get_line uses buffering or not to get a byte data
58 from FD. get_line uses zreadc if UNBUFFERED_READ is zero; and
59 uses zread if UNBUFFERED_READ is non-zero.
61 Returns number of bytes read or -1 on error. */
64 zgetline (fd
, lineptr
, n
, delim
, unbuffered_read
)
74 if (lineptr
== 0 || n
== 0 || (*lineptr
== 0 && *n
!= 0))
82 retval
= unbuffered_read
? zread (fd
, &c
, 1) : zreadc(fd
, &c
);
95 new_size
= (*n
== 0) ? GET_LINE_INITIAL_ALLOCATION
: *n
* 2;
96 line
= (*n
>= new_size
) ? NULL
: xrealloc (*lineptr
, new_size
);
107 (*lineptr
)[*n
- 1] = '\0';