]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fileio.c
tree-wide: port some code over to safe_fgetc()
[thirdparty/systemd.git] / src / basic / fileio.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a5c32cff 2
8bdc9a90 3#include <ctype.h>
11c3a366
TA
4#include <errno.h>
5#include <fcntl.h>
6#include <limits.h>
7#include <stdarg.h>
8#include <stdint.h>
35bbbf85 9#include <stdio_ext.h>
11c3a366
TA
10#include <stdlib.h>
11#include <string.h>
12#include <sys/stat.h>
13#include <sys/types.h>
a5c32cff 14#include <unistd.h>
cda134ab 15
b5efdb8a 16#include "alloc-util.h"
3ffd4af2
LP
17#include "fd-util.h"
18#include "fileio.h"
f4f15635 19#include "fs-util.h"
93cc7779
TA
20#include "log.h"
21#include "macro.h"
1d9ed171 22#include "missing.h"
33d52ab9 23#include "parse-util.h"
0d39fa9c 24#include "path-util.h"
33d52ab9 25#include "stdio-util.h"
07630cea 26#include "string-util.h"
e4de7287 27#include "tmpfile-util.h"
a5c32cff 28
c2d11a63
VC
29#define READ_FULL_BYTES_MAX (4U*1024U*1024U)
30
b1837133
LP
31int write_string_stream_ts(
32 FILE *f,
33 const char *line,
34 WriteStringFileFlags flags,
35 struct timespec *ts) {
dacd6cee 36
91dc2bf7 37 bool needs_nl;
be83711c 38 int r;
91dc2bf7 39
717603e3
LP
40 assert(f);
41 assert(line);
42
ba8b8c9e
MG
43 if (ferror(f))
44 return -EIO;
45
91dc2bf7
LP
46 needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
47
48 if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
49 /* If STDIO buffering was disabled, then let's append the newline character to the string itself, so
50 * that the write goes out in one go, instead of two */
51
52 line = strjoina(line, "\n");
53 needs_nl = false;
54 }
55
94d3b60f
MG
56 if (fputs(line, f) == EOF)
57 return -errno;
58
91dc2bf7 59 if (needs_nl)
94d3b60f
MG
60 if (fputc('\n', f) == EOF)
61 return -errno;
a5c32cff 62
be83711c
CL
63 if (flags & WRITE_STRING_FILE_SYNC)
64 r = fflush_sync_and_check(f);
65 else
66 r = fflush_and_check(f);
67 if (r < 0)
68 return r;
69
39c38d77
ZJS
70 if (ts) {
71 struct timespec twice[2] = {*ts, *ts};
72
73 if (futimens(fileno(f), twice) < 0)
74 return -errno;
75 }
76
be83711c 77 return 0;
a5c32cff
HH
78}
79
2eabcc77
LP
80static int write_string_file_atomic(
81 const char *fn,
82 const char *line,
b1837133 83 WriteStringFileFlags flags,
2eabcc77
LP
84 struct timespec *ts) {
85
a5c32cff
HH
86 _cleanup_fclose_ FILE *f = NULL;
87 _cleanup_free_ char *p = NULL;
88 int r;
89
90 assert(fn);
91 assert(line);
92
93 r = fopen_temporary(fn, &f, &p);
94 if (r < 0)
95 return r;
96
35bbbf85 97 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
0d39fa9c 98 (void) fchmod_umask(fileno(f), 0644);
a5c32cff 99
b1837133 100 r = write_string_stream_ts(f, line, flags, ts);
9dd1b1e8
LP
101 if (r < 0)
102 goto fail;
103
104 if (rename(p, fn) < 0) {
105 r = -errno;
106 goto fail;
a5c32cff
HH
107 }
108
9dd1b1e8 109 return 0;
a5c32cff 110
9dd1b1e8
LP
111fail:
112 (void) unlink(p);
a5c32cff
HH
113 return r;
114}
115
b1837133
LP
116int write_string_file_ts(
117 const char *fn,
118 const char *line,
119 WriteStringFileFlags flags,
120 struct timespec *ts) {
121
4c1fc3e4 122 _cleanup_fclose_ FILE *f = NULL;
eb3da901 123 int q, r;
4c1fc3e4
DM
124
125 assert(fn);
126 assert(line);
127
265710c2
AJ
128 /* We don't know how to verify whether the file contents was already on-disk. */
129 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
0675e94a 130
4c1fc3e4
DM
131 if (flags & WRITE_STRING_FILE_ATOMIC) {
132 assert(flags & WRITE_STRING_FILE_CREATE);
133
b1837133 134 r = write_string_file_atomic(fn, line, flags, ts);
eb3da901
LP
135 if (r < 0)
136 goto fail;
137
138 return r;
39c38d77 139 } else
234519ae 140 assert(!ts);
4c1fc3e4
DM
141
142 if (flags & WRITE_STRING_FILE_CREATE) {
143 f = fopen(fn, "we");
eb3da901
LP
144 if (!f) {
145 r = -errno;
146 goto fail;
147 }
4c1fc3e4
DM
148 } else {
149 int fd;
150
151 /* We manually build our own version of fopen(..., "we") that
152 * works without O_CREAT */
835d18ba 153 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY | ((flags & WRITE_STRING_FILE_NOFOLLOW) ? O_NOFOLLOW : 0));
eb3da901
LP
154 if (fd < 0) {
155 r = -errno;
156 goto fail;
157 }
4c1fc3e4 158
e92aaed3 159 f = fdopen(fd, "w");
4c1fc3e4 160 if (!f) {
eb3da901 161 r = -errno;
4c1fc3e4 162 safe_close(fd);
eb3da901 163 goto fail;
4c1fc3e4
DM
164 }
165 }
166
35bbbf85
LP
167 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
168
12ec9c30
TSH
169 if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
170 setvbuf(f, NULL, _IONBF, 0);
171
b1837133 172 r = write_string_stream_ts(f, line, flags, ts);
eb3da901
LP
173 if (r < 0)
174 goto fail;
175
176 return 0;
177
178fail:
179 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
180 return r;
181
182 f = safe_fclose(f);
183
184 /* OK, the operation failed, but let's see if the right
185 * contents in place already. If so, eat up the error. */
186
187 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
188 if (q <= 0)
189 return r;
190
191 return 0;
4c1fc3e4
DM
192}
193
3130fca5
LP
194int write_string_filef(
195 const char *fn,
196 WriteStringFileFlags flags,
197 const char *format, ...) {
198
199 _cleanup_free_ char *p = NULL;
200 va_list ap;
201 int r;
202
203 va_start(ap, format);
204 r = vasprintf(&p, format, ap);
205 va_end(ap);
206
207 if (r < 0)
208 return -ENOMEM;
209
210 return write_string_file(fn, p, flags);
211}
212
a5c32cff
HH
213int read_one_line_file(const char *fn, char **line) {
214 _cleanup_fclose_ FILE *f = NULL;
2e33df93 215 int r;
a5c32cff
HH
216
217 assert(fn);
218 assert(line);
219
220 f = fopen(fn, "re");
221 if (!f)
222 return -errno;
223
35bbbf85
LP
224 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
225
2e33df93
ZJS
226 r = read_line(f, LONG_LINE_MAX, line);
227 return r < 0 ? r : 0;
a5c32cff
HH
228}
229
eb3da901
LP
230int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
231 _cleanup_fclose_ FILE *f = NULL;
232 _cleanup_free_ char *buf = NULL;
233 size_t l, k;
15dee3f0 234
eb3da901
LP
235 assert(fn);
236 assert(blob);
237
238 l = strlen(blob);
239
240 if (accept_extra_nl && endswith(blob, "\n"))
241 accept_extra_nl = false;
242
243 buf = malloc(l + accept_extra_nl + 1);
244 if (!buf)
245 return -ENOMEM;
246
247 f = fopen(fn, "re");
248 if (!f)
249 return -errno;
250
35bbbf85
LP
251 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
252
eb3da901
LP
253 /* We try to read one byte more than we need, so that we know whether we hit eof */
254 errno = 0;
255 k = fread(buf, 1, l + accept_extra_nl + 1, f);
256 if (ferror(f))
257 return errno > 0 ? -errno : -EIO;
258
259 if (k != l && k != l + accept_extra_nl)
260 return 0;
261 if (memcmp(buf, blob, l) != 0)
262 return 0;
263 if (k > l && buf[l] != '\n')
264 return 0;
15dee3f0 265
eb3da901 266 return 1;
15dee3f0
LP
267}
268
2d78717b
LP
269int read_full_stream(
270 FILE *f,
271 char **ret_contents,
272 size_t *ret_size) {
273
a5c32cff
HH
274 _cleanup_free_ char *buf = NULL;
275 struct stat st;
c4054ddf
LP
276 size_t n, l;
277 int fd;
a5c32cff 278
717603e3 279 assert(f);
2d78717b 280 assert(ret_contents);
a5c32cff 281
2d78717b 282 n = LINE_MAX; /* Start size */
a5c32cff 283
c4054ddf
LP
284 fd = fileno(f);
285 if (fd >= 0) { /* If the FILE* object is backed by an fd (as opposed to memory or such, see fmemopen(), let's
286 * optimize our buffering) */
717603e3 287
c4054ddf
LP
288 if (fstat(fileno(f), &st) < 0)
289 return -errno;
290
291 if (S_ISREG(st.st_mode)) {
717603e3 292
c4054ddf
LP
293 /* Safety check */
294 if (st.st_size > READ_FULL_BYTES_MAX)
295 return -E2BIG;
296
297 /* Start with the right file size, but be prepared for files from /proc which generally report a file
298 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
299 * already makes us notice the EOF. */
300 if (st.st_size > 0)
301 n = st.st_size + 1;
302 }
717603e3 303 }
a5c32cff 304
717603e3 305 l = 0;
a5c32cff
HH
306 for (;;) {
307 char *t;
308 size_t k;
309
c2d11a63 310 t = realloc(buf, n + 1);
a5c32cff
HH
311 if (!t)
312 return -ENOMEM;
313
314 buf = t;
5a89faf0 315 errno = 0;
a5c32cff 316 k = fread(buf + l, 1, n - l, f);
c2d11a63
VC
317 if (k > 0)
318 l += k;
a5c32cff 319
c2d11a63 320 if (ferror(f))
5a89faf0 321 return errno > 0 ? -errno : -EIO;
a5c32cff 322
c2d11a63 323 if (feof(f))
a5c32cff 324 break;
a5c32cff 325
c2d11a63
VC
326 /* We aren't expecting fread() to return a short read outside
327 * of (error && eof), assert buffer is full and enlarge buffer.
328 */
329 assert(l == n);
a5c32cff
HH
330
331 /* Safety check */
c2d11a63 332 if (n >= READ_FULL_BYTES_MAX)
a5c32cff 333 return -E2BIG;
c2d11a63 334
82b103a7 335 n = MIN(n * 2, READ_FULL_BYTES_MAX);
a5c32cff
HH
336 }
337
2d78717b
LP
338 if (!ret_size) {
339 /* Safety check: if the caller doesn't want to know the size of what we just read it will rely on the
340 * trailing NUL byte. But if there's an embedded NUL byte, then we should refuse operation as otherwise
341 * there'd be ambiguity about what we just read. */
342
343 if (memchr(buf, 0, l))
344 return -EBADMSG;
345 }
346
a5c32cff 347 buf[l] = 0;
2d78717b 348 *ret_contents = TAKE_PTR(buf);
a5c32cff 349
2d78717b
LP
350 if (ret_size)
351 *ret_size = l;
a5c32cff
HH
352
353 return 0;
354}
355
717603e3
LP
356int read_full_file(const char *fn, char **contents, size_t *size) {
357 _cleanup_fclose_ FILE *f = NULL;
358
359 assert(fn);
360 assert(contents);
361
362 f = fopen(fn, "re");
363 if (!f)
364 return -errno;
365
35bbbf85
LP
366 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
367
717603e3
LP
368 return read_full_stream(f, contents, size);
369}
370
68fee104 371int executable_is_script(const char *path, char **interpreter) {
c8b32e11 372 _cleanup_free_ char *line = NULL;
99c61f6b 373 size_t len;
68fee104 374 char *ans;
99c61f6b 375 int r;
68fee104
ZJS
376
377 assert(path);
378
379 r = read_one_line_file(path, &line);
99c61f6b
LP
380 if (r == -ENOBUFS) /* First line overly long? if so, then it's not a script */
381 return 0;
68fee104
ZJS
382 if (r < 0)
383 return r;
384
385 if (!startswith(line, "#!"))
386 return 0;
387
388 ans = strstrip(line + 2);
389 len = strcspn(ans, " \t");
390
391 if (len == 0)
392 return 0;
393
394 ans = strndup(ans, len);
395 if (!ans)
396 return -ENOMEM;
397
398 *interpreter = ans;
399 return 1;
400}
69ab8088
ZJS
401
402/**
0a7b53bd 403 * Retrieve one field from a file like /proc/self/status. pattern
c4cd1d4d
AK
404 * should not include whitespace or the delimiter (':'). pattern matches only
405 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
406 * zeros after the ':' will be skipped. field must be freed afterwards.
407 * terminator specifies the terminating characters of the field value (not
408 * included in the value).
69ab8088 409 */
c4cd1d4d 410int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
69ab8088 411 _cleanup_free_ char *status = NULL;
90110825 412 char *t, *f;
69ab8088
ZJS
413 size_t len;
414 int r;
415
c4cd1d4d 416 assert(terminator);
69ab8088 417 assert(filename);
7ff7394d 418 assert(pattern);
69ab8088
ZJS
419 assert(field);
420
421 r = read_full_file(filename, &status, NULL);
422 if (r < 0)
423 return r;
424
c4cd1d4d
AK
425 t = status;
426
427 do {
428 bool pattern_ok;
429
430 do {
431 t = strstr(t, pattern);
432 if (!t)
433 return -ENOENT;
434
435 /* Check that pattern occurs in beginning of line. */
436 pattern_ok = (t == status || t[-1] == '\n');
437
438 t += strlen(pattern);
439
440 } while (!pattern_ok);
441
442 t += strspn(t, " \t");
443 if (!*t)
444 return -ENOENT;
445
446 } while (*t != ':');
447
448 t++;
69ab8088 449
4ec29144 450 if (*t) {
1e5413f7
ZJS
451 t += strspn(t, " \t");
452
453 /* Also skip zeros, because when this is used for
454 * capabilities, we don't want the zeros. This way the
455 * same capability set always maps to the same string,
456 * irrespective of the total capability set size. For
457 * other numbers it shouldn't matter. */
458 t += strspn(t, "0");
4ec29144
ZJS
459 /* Back off one char if there's nothing but whitespace
460 and zeros */
1e5413f7 461 if (!*t || isspace(*t))
313cefa1 462 t--;
4ec29144 463 }
69ab8088 464
c4cd1d4d 465 len = strcspn(t, terminator);
69ab8088 466
90110825
LP
467 f = strndup(t, len);
468 if (!f)
69ab8088
ZJS
469 return -ENOMEM;
470
90110825 471 *field = f;
69ab8088
ZJS
472 return 0;
473}
0d39fa9c
LP
474
475DIR *xopendirat(int fd, const char *name, int flags) {
476 int nfd;
477 DIR *d;
478
479 assert(!(flags & O_CREAT));
480
481 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
482 if (nfd < 0)
483 return NULL;
484
485 d = fdopendir(nfd);
486 if (!d) {
487 safe_close(nfd);
488 return NULL;
489 }
490
491 return d;
492}
493
494static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
495 char **i;
496
497 assert(path);
498 assert(mode);
499 assert(_f);
500
501 if (!path_strv_resolve_uniq(search, root))
502 return -ENOMEM;
503
504 STRV_FOREACH(i, search) {
505 _cleanup_free_ char *p = NULL;
506 FILE *f;
507
508 if (root)
605405c6 509 p = strjoin(root, *i, "/", path);
0d39fa9c 510 else
605405c6 511 p = strjoin(*i, "/", path);
0d39fa9c
LP
512 if (!p)
513 return -ENOMEM;
514
515 f = fopen(p, mode);
516 if (f) {
517 *_f = f;
518 return 0;
519 }
520
521 if (errno != ENOENT)
522 return -errno;
523 }
524
525 return -ENOENT;
526}
527
528int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
529 _cleanup_strv_free_ char **copy = NULL;
530
531 assert(path);
532 assert(mode);
533 assert(_f);
534
535 if (path_is_absolute(path)) {
536 FILE *f;
537
538 f = fopen(path, mode);
539 if (f) {
540 *_f = f;
541 return 0;
542 }
543
544 return -errno;
545 }
546
547 copy = strv_copy((char**) search);
548 if (!copy)
549 return -ENOMEM;
550
551 return search_and_fopen_internal(path, mode, root, copy, _f);
552}
553
554int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
555 _cleanup_strv_free_ char **s = NULL;
556
557 if (path_is_absolute(path)) {
558 FILE *f;
559
560 f = fopen(path, mode);
561 if (f) {
562 *_f = f;
563 return 0;
564 }
565
566 return -errno;
567 }
568
569 s = strv_split_nulstr(search);
570 if (!s)
571 return -ENOMEM;
572
573 return search_and_fopen_internal(path, mode, root, s, _f);
574}
575
0d39fa9c
LP
576int fflush_and_check(FILE *f) {
577 assert(f);
578
579 errno = 0;
580 fflush(f);
581
582 if (ferror(f))
f5e5c28f 583 return errno > 0 ? -errno : -EIO;
0d39fa9c
LP
584
585 return 0;
586}
587
0675e94a
AJ
588int fflush_sync_and_check(FILE *f) {
589 int r;
590
591 assert(f);
592
593 r = fflush_and_check(f);
594 if (r < 0)
595 return r;
596
597 if (fsync(fileno(f)) < 0)
598 return -errno;
599
8ac2f74f
LP
600 r = fsync_directory_of_file(fileno(f));
601 if (r < 0)
602 return r;
603
0675e94a
AJ
604 return 0;
605}
606
33d52ab9
LP
607int write_timestamp_file_atomic(const char *fn, usec_t n) {
608 char ln[DECIMAL_STR_MAX(n)+2];
609
610 /* Creates a "timestamp" file, that contains nothing but a
611 * usec_t timestamp, formatted in ASCII. */
612
613 if (n <= 0 || n >= USEC_INFINITY)
614 return -ERANGE;
615
616 xsprintf(ln, USEC_FMT "\n", n);
617
618 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
619}
620
621int read_timestamp_file(const char *fn, usec_t *ret) {
622 _cleanup_free_ char *ln = NULL;
623 uint64_t t;
624 int r;
625
626 r = read_one_line_file(fn, &ln);
627 if (r < 0)
628 return r;
629
630 r = safe_atou64(ln, &t);
631 if (r < 0)
632 return r;
633
634 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
635 return -ERANGE;
636
637 *ret = (usec_t) t;
638 return 0;
639}
d390f8ef
LP
640
641int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
642 int r;
643
644 assert(s);
645
646 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
647 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
648 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
649 * element, but not before the first one. */
650
651 if (!f)
652 f = stdout;
653
654 if (space) {
655 if (!separator)
656 separator = " ";
657
658 if (*space) {
659 r = fputs(separator, f);
660 if (r < 0)
661 return r;
662 }
663
664 *space = true;
665 }
666
667 return fputs(s, f);
668}
03532f0a 669
03c2b288
LP
670int read_nul_string(FILE *f, char **ret) {
671 _cleanup_free_ char *x = NULL;
672 size_t allocated = 0, n = 0;
673
674 assert(f);
675 assert(ret);
676
677 /* Reads a NUL-terminated string from the specified file. */
678
679 for (;;) {
680 int c;
681
682 if (!GREEDY_REALLOC(x, allocated, n+2))
683 return -ENOMEM;
684
685 c = fgetc(f);
686 if (c == 0) /* Terminate at NUL byte */
687 break;
688 if (c == EOF) {
689 if (ferror(f))
690 return -errno;
691 break; /* Terminate at EOF */
692 }
693
694 x[n++] = (char) c;
695 }
696
697 if (x)
698 x[n] = 0;
699 else {
700 x = new0(char, 1);
701 if (!x)
702 return -ENOMEM;
703 }
704
ae2a15bc 705 *ret = TAKE_PTR(x);
03c2b288
LP
706
707 return 0;
708}
676bcb0f 709
838894b0
LP
710/* A bitmask of the EOL markers we know */
711typedef enum EndOfLineMarker {
712 EOL_NONE = 0,
713 EOL_ZERO = 1 << 0, /* \0 (aka NUL) */
714 EOL_TEN = 1 << 1, /* \n (aka NL, aka LF) */
715 EOL_THIRTEEN = 1 << 2, /* \r (aka CR) */
716} EndOfLineMarker;
717
718static EndOfLineMarker categorize_eol(char c) {
719 if (c == '\n')
720 return EOL_TEN;
721 if (c == '\r')
722 return EOL_THIRTEEN;
723 if (c == '\0')
724 return EOL_ZERO;
725
726 return EOL_NONE;
727}
728
57d6f700 729DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
f858e514 730
4f9a66a3 731int read_line(FILE *f, size_t limit, char **ret) {
4f9a66a3 732 size_t n = 0, allocated = 0, count = 0;
838894b0 733 _cleanup_free_ char *buffer = NULL;
03a7dbea 734 int r;
4f9a66a3
LP
735
736 assert(f);
737
738 /* Something like a bounded version of getline().
739 *
838894b0
LP
740 * Considers EOF, \n, \r and \0 end of line delimiters (or combinations of these), and does not include these
741 * delimiters in the string returned. Specifically, recognizes the following combinations of markers as line
742 * endings:
743 *
744 * • \n (UNIX)
745 * • \r (old MacOS)
746 * • \0 (C strings)
747 * • \n\0
748 * • \r\0
749 * • \r\n (Windows)
750 * • \n\r
751 * • \r\n\0
752 * • \n\r\0
4f9a66a3
LP
753 *
754 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
755 * the number of characters in the returned string). When EOF is hit, 0 is returned.
756 *
757 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
758 * delimiters. If the limit is hit we fail and return -ENOBUFS.
759 *
760 * If a line shall be skipped ret may be initialized as NULL. */
761
762 if (ret) {
763 if (!GREEDY_REALLOC(buffer, allocated, 1))
764 return -ENOMEM;
765 }
766
f858e514 767 {
3f691417 768 _unused_ _cleanup_(funlockfilep) FILE *flocked = f;
838894b0 769 EndOfLineMarker previous_eol = EOL_NONE;
f858e514 770 flockfile(f);
4f9a66a3 771
f858e514 772 for (;;) {
838894b0 773 EndOfLineMarker eol;
03a7dbea 774 char c;
4f9a66a3 775
f858e514
ZJS
776 if (n >= limit)
777 return -ENOBUFS;
4f9a66a3 778
31fd02f0
LP
779 if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
780 return -ENOBUFS;
781
03a7dbea
LP
782 r = safe_fgetc(f, &c);
783 if (r < 0)
784 return r;
785 if (r == 0)
f858e514 786 break;
4f9a66a3 787
f858e514 788 count++;
4f9a66a3 789
838894b0
LP
790 eol = categorize_eol(c);
791
792 if (FLAGS_SET(previous_eol, EOL_ZERO) ||
793 (eol == EOL_NONE && previous_eol != EOL_NONE) ||
794 (eol != EOL_NONE && (previous_eol & eol) != 0)) {
795 /* Previous char was a NUL? This is not an EOL, but the previous char was? This type of
796 * EOL marker has been seen right before? In either of these three cases we are
797 * done. But first, let's put this character back in the queue. */
798 assert_se(ungetc(c, f) != EOF);
799 count--;
f858e514 800 break;
838894b0
LP
801 }
802
803 if (eol != EOL_NONE) {
804 previous_eol |= eol;
805 continue;
806 }
4f9a66a3 807
f858e514
ZJS
808 if (ret) {
809 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
810 return -ENOMEM;
4f9a66a3 811
03a7dbea 812 buffer[n] = c;
4f9a66a3
LP
813 }
814
f858e514 815 n++;
4f9a66a3 816 }
4f9a66a3
LP
817 }
818
4f9a66a3
LP
819 if (ret) {
820 buffer[n] = 0;
821
1cc6c93a 822 *ret = TAKE_PTR(buffer);
4f9a66a3
LP
823 }
824
825 return (int) count;
826}
285a9b27
LP
827
828int safe_fgetc(FILE *f, char *ret) {
829 int k;
830
831 assert(f);
832
833 /* A safer version of plain fgetc(): let's propagate the error that happened while reading as such, and
834 * separate the EOF condition from the byte read, to avoid those confusion signed/unsigned issues fgetc()
835 * has. */
836
837 errno = 0;
838 k = fgetc(f);
839 if (k == EOF) {
840 if (ferror(f))
841 return errno > 0 ? -errno : -EIO;
842
843 if (ret)
844 *ret = 0;
845
846 return 0;
847 }
848
849 if (ret)
850 *ret = k;
851
852 return 1;
853}