2 * Copyright © 2024 Nick Bowler
4 * getline-like function which removes trailing newline (if any), and
5 * returns nonzero if and only if a line was successfully read.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
28 * Use the minimum initial alloc size in the test programs to ensure we at
29 * least are exercising the realloc path on a reasonably regular basis.
31 # define CDECL99_GETLINE_INITIAL_ALLOC 1
33 # define CDECL99_GETLINE_INITIAL_ALLOC 75
36 static inline int do_getline(char **linebuf, size_t *n)
38 extern void print_error(const char *fmt, ...);
44 if ((rc = getline(linebuf, n, stdin)) < 0) {
50 if (rc-- && (*linebuf)[rc] == '\n')
51 (*linebuf)[rc] = '\0';
54 char *work = *linebuf;
59 sz = CDECL99_GETLINE_INITIAL_ALLOC;
64 if (!fgets(&work[pos], sz - pos, stdin)) {
71 pos += strlen(&work[pos]);
72 if (work[pos-1] == '\n') {
77 if (sz > INT_MAX/2 || sz > ((size_t)-1)/4)
80 sz = ((sz*4) + 2) / 3;
82 work = realloc(work, sz);
89 errmsg = _("failed to allocate memory");
91 if (0) input_error: errmsg = strerror(errno);
92 print_error("%s", errmsg);