]> git.draconx.ca Git - cdecl99.git/blob - src/getline.h
Release 1.3.
[cdecl99.git] / src / getline.h
1 /*
2  * Copyright © 2024 Nick Bowler
3  *
4  * getline-like function which removes trailing newline (if any), and
5  * returns nonzero if and only if a line was successfully read.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <errno.h>
25
26 #ifdef CDECL_TEST_H_
27 /*
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.
30  */
31 #  define CDECL99_GETLINE_INITIAL_ALLOC 1
32 #else
33 #  define CDECL99_GETLINE_INITIAL_ALLOC 75
34 #endif
35
36 static inline int do_getline(char **linebuf, size_t *n)
37 {
38         extern void print_error(const char *fmt, ...);
39         const char *errmsg;
40
41 #if HAVE_GETLINE
42         ssize_t rc;
43
44         if ((rc = getline(linebuf, n, stdin)) < 0) {
45                 if (ferror(stdin))
46                         goto input_error;
47                 return 0;
48         }
49
50         if (rc-- && (*linebuf)[rc] == '\n')
51                 (*linebuf)[rc] = '\0';
52         return 1;
53 #else
54         char *work = *linebuf;
55         size_t pos = 0;
56         size_t sz;
57
58         if (!work) {
59                 sz = CDECL99_GETLINE_INITIAL_ALLOC;
60                 goto initial_alloc;
61         }
62
63         for (sz = *n;;) {
64                 if (!fgets(&work[pos], sz - pos, stdin)) {
65                         if (ferror(stdin))
66                                 goto input_error;
67
68                         return !!pos;
69                 }
70
71                 pos += strlen(&work[pos]);
72                 if (work[pos-1] == '\n') {
73                         work[pos-1] = '\0';
74                         return 1;
75                 }
76
77                 if (sz > INT_MAX/2 || sz > ((size_t)-1)/4)
78                         break;
79
80                 sz = ((sz*4) + 2) / 3;
81 initial_alloc:
82                 work = realloc(work, sz);
83                 if (!work)
84                         break;
85                 *linebuf = work;
86                 *n = sz;
87         }
88
89         errmsg = _("failed to allocate memory");
90 #endif
91         if (0) input_error: errmsg = strerror(errno);
92         print_error("%s", errmsg);
93         return 0;
94 }
95