2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
31 #include "alloc-util.h"
39 #include "hexdecoct.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
46 #include "random-util.h"
47 #include "stdio-util.h"
48 #include "string-util.h"
50 //#include "time-util.h"
51 #include "umask-util.h"
54 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
56 int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
62 if (enforce_newline && !endswith(line, "\n"))
66 struct timespec twice[2] = {*ts, *ts};
68 if (futimens(fileno(f), twice) < 0)
72 return fflush_and_check(f);
75 static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
76 _cleanup_fclose_ FILE *f = NULL;
77 _cleanup_free_ char *p = NULL;
83 r = fopen_temporary(fn, &f, &p);
87 (void) fchmod_umask(fileno(f), 0644);
89 r = write_string_stream(f, line, enforce_newline);
90 if (r >= 0 && do_fsync)
91 r = fflush_sync_and_check(f);
94 if (rename(p, fn) < 0)
104 int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
105 _cleanup_fclose_ FILE *f = NULL;
111 /* We don't know how to verify whether the file contents was already on-disk. */
112 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
114 if (flags & WRITE_STRING_FILE_ATOMIC) {
115 assert(flags & WRITE_STRING_FILE_CREATE);
117 r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
118 flags & WRITE_STRING_FILE_SYNC);
126 if (flags & WRITE_STRING_FILE_CREATE) {
135 /* We manually build our own version of fopen(..., "we") that
136 * works without O_CREAT */
137 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
143 f = fdopen(fd, "we");
151 r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
155 if (flags & WRITE_STRING_FILE_SYNC) {
156 r = fflush_sync_and_check(f);
164 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
169 /* OK, the operation failed, but let's see if the right
170 * contents in place already. If so, eat up the error. */
172 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
179 int read_one_line_file(const char *fn, char **line) {
180 _cleanup_fclose_ FILE *f = NULL;
190 r = read_line(f, LONG_LINE_MAX, line);
191 return r < 0 ? r : 0;
194 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
195 _cleanup_fclose_ FILE *f = NULL;
196 _cleanup_free_ char *buf = NULL;
204 if (accept_extra_nl && endswith(blob, "\n"))
205 accept_extra_nl = false;
207 buf = malloc(l + accept_extra_nl + 1);
215 /* We try to read one byte more than we need, so that we know whether we hit eof */
217 k = fread(buf, 1, l + accept_extra_nl + 1, f);
219 return errno > 0 ? -errno : -EIO;
221 if (k != l && k != l + accept_extra_nl)
223 if (memcmp(buf, blob, l) != 0)
225 if (k > l && buf[l] != '\n')
231 int read_full_stream(FILE *f, char **contents, size_t *size) {
233 _cleanup_free_ char *buf = NULL;
239 if (fstat(fileno(f), &st) < 0)
244 if (S_ISREG(st.st_mode)) {
247 if (st.st_size > READ_FULL_BYTES_MAX)
250 /* Start with the right file size, but be prepared for
251 * files from /proc which generally report a file size
262 t = realloc(buf, n + 1);
267 k = fread(buf + l, 1, n - l, f);
277 /* We aren't expecting fread() to return a short read outside
278 * of (error && eof), assert buffer is full and enlarge buffer.
283 if (n >= READ_FULL_BYTES_MAX)
286 n = MIN(n * 2, READ_FULL_BYTES_MAX);
291 buf = NULL; /* do not free */
299 int read_full_file(const char *fn, char **contents, size_t *size) {
300 _cleanup_fclose_ FILE *f = NULL;
309 return read_full_stream(f, contents, size);
312 static int parse_env_file_internal(
316 int (*push) (const char *filename, unsigned line,
317 const char *key, char *value, void *userdata, int *n_pushed),
321 _cleanup_free_ char *contents = NULL, *key = NULL;
322 size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1;
323 char *p, *value = NULL;
334 SINGLE_QUOTE_VALUE_ESCAPE,
336 DOUBLE_QUOTE_VALUE_ESCAPE,
344 r = read_full_stream(f, &contents, NULL);
346 r = read_full_file(fname, &contents, NULL);
350 for (p = contents; *p; p++) {
356 if (strchr(COMMENTS, c))
358 else if (!strchr(WHITESPACE, c)) {
360 last_key_whitespace = (size_t) -1;
362 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
372 if (strchr(newline, c)) {
376 } else if (c == '=') {
378 last_value_whitespace = (size_t) -1;
380 if (!strchr(WHITESPACE, c))
381 last_key_whitespace = (size_t) -1;
382 else if (last_key_whitespace == (size_t) -1)
383 last_key_whitespace = n_key;
385 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
396 if (strchr(newline, c)) {
404 /* strip trailing whitespace from key */
405 if (last_key_whitespace != (size_t) -1)
406 key[last_key_whitespace] = 0;
408 r = push(fname, line, key, value, userdata, n_pushed);
414 value_alloc = n_value = 0;
416 } else if (c == '\'')
417 state = SINGLE_QUOTE_VALUE;
419 state = DOUBLE_QUOTE_VALUE;
421 state = VALUE_ESCAPE;
422 else if (!strchr(WHITESPACE, c)) {
425 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
430 value[n_value++] = c;
436 if (strchr(newline, c)) {
445 /* Chomp off trailing whitespace from value */
446 if (last_value_whitespace != (size_t) -1)
447 value[last_value_whitespace] = 0;
449 /* strip trailing whitespace from key */
450 if (last_key_whitespace != (size_t) -1)
451 key[last_key_whitespace] = 0;
453 r = push(fname, line, key, value, userdata, n_pushed);
459 value_alloc = n_value = 0;
461 } else if (c == '\\') {
462 state = VALUE_ESCAPE;
463 last_value_whitespace = (size_t) -1;
465 if (!strchr(WHITESPACE, c))
466 last_value_whitespace = (size_t) -1;
467 else if (last_value_whitespace == (size_t) -1)
468 last_value_whitespace = n_value;
470 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
475 value[n_value++] = c;
483 if (!strchr(newline, c)) {
484 /* Escaped newlines we eat up entirely */
485 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
490 value[n_value++] = c;
494 case SINGLE_QUOTE_VALUE:
498 state = SINGLE_QUOTE_VALUE_ESCAPE;
500 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
505 value[n_value++] = c;
510 case SINGLE_QUOTE_VALUE_ESCAPE:
511 state = SINGLE_QUOTE_VALUE;
513 if (!strchr(newline, c)) {
514 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
519 value[n_value++] = c;
523 case DOUBLE_QUOTE_VALUE:
527 state = DOUBLE_QUOTE_VALUE_ESCAPE;
529 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
534 value[n_value++] = c;
539 case DOUBLE_QUOTE_VALUE_ESCAPE:
540 state = DOUBLE_QUOTE_VALUE;
542 if (!strchr(newline, c)) {
543 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
548 value[n_value++] = c;
554 state = COMMENT_ESCAPE;
555 else if (strchr(newline, c)) {
572 SINGLE_QUOTE_VALUE_ESCAPE,
574 DOUBLE_QUOTE_VALUE_ESCAPE)) {
582 if (last_value_whitespace != (size_t) -1)
583 value[last_value_whitespace] = 0;
585 /* strip trailing whitespace from key */
586 if (last_key_whitespace != (size_t) -1)
587 key[last_key_whitespace] = 0;
589 r = push(fname, line, key, value, userdata, n_pushed);
601 static int check_utf8ness_and_warn(
602 const char *filename, unsigned line,
603 const char *key, char *value) {
605 if (!utf8_is_valid(key)) {
606 _cleanup_free_ char *p = NULL;
608 p = utf8_escape_invalid(key);
609 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
613 if (value && !utf8_is_valid(value)) {
614 _cleanup_free_ char *p = NULL;
616 p = utf8_escape_invalid(value);
617 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
624 static int parse_env_file_push(
625 const char *filename, unsigned line,
626 const char *key, char *value,
631 va_list aq, *ap = userdata;
634 r = check_utf8ness_and_warn(filename, line, key, value);
640 while ((k = va_arg(aq, const char *))) {
643 v = va_arg(aq, char **);
665 const char *newline, ...) {
673 va_start(ap, newline);
674 r = parse_env_file_internal(NULL, fname, newline, parse_env_file_push, &ap, &n_pushed);
677 return r < 0 ? r : n_pushed;
680 #if 0 /// UNNEEDED by elogind
681 static int load_env_file_push(
682 const char *filename, unsigned line,
683 const char *key, char *value,
686 char ***m = userdata;
690 r = check_utf8ness_and_warn(filename, line, key, value);
694 p = strjoin(key, "=", value);
698 r = strv_env_replace(m, p);
711 int load_env_file(FILE *f, const char *fname, const char *newline, char ***rl) {
718 r = parse_env_file_internal(f, fname, newline, load_env_file_push, &m, NULL);
729 static int load_env_file_push_pairs(
730 const char *filename, unsigned line,
731 const char *key, char *value,
734 char ***m = userdata;
737 r = check_utf8ness_and_warn(filename, line, key, value);
741 r = strv_extend(m, key);
746 r = strv_extend(m, "");
750 r = strv_push(m, value);
761 int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ***rl) {
768 r = parse_env_file_internal(f, fname, newline, load_env_file_push_pairs, &m, NULL);
777 #if 0 /// UNNEEDED by elogind
779 static int merge_env_file_push(
780 const char *filename, unsigned line,
781 const char *key, char *value,
785 char ***env = userdata;
786 char *expanded_value;
791 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
795 if (!env_name_is_valid(key)) {
796 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
801 expanded_value = replace_env(value, *env,
802 REPLACE_ENV_USE_ENVIRONMENT|
803 REPLACE_ENV_ALLOW_BRACELESS|
804 REPLACE_ENV_ALLOW_EXTENDED);
808 free_and_replace(value, expanded_value);
810 return load_env_file_push(filename, line, key, value, env, n_pushed);
818 /* NOTE: this function supports braceful and braceless variable expansions,
819 * plus "extended" substitutions, unlike other exported parsing functions.
822 return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);
825 static void write_env_var(FILE *f, const char *v) {
831 fputs_unlocked(v, f);
832 fputc_unlocked('\n', f);
837 fwrite_unlocked(v, 1, p-v, f);
839 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
840 fputc_unlocked('\"', f);
843 if (strchr(SHELL_NEED_ESCAPE, *p))
844 fputc_unlocked('\\', f);
846 fputc_unlocked(*p, f);
849 fputc_unlocked('\"', f);
851 fputs_unlocked(p, f);
853 fputc_unlocked('\n', f);
856 int write_env_file(const char *fname, char **l) {
857 _cleanup_fclose_ FILE *f = NULL;
858 _cleanup_free_ char *p = NULL;
864 r = fopen_temporary(fname, &f, &p);
868 fchmod_umask(fileno(f), 0644);
871 write_env_var(f, *i);
873 r = fflush_and_check(f);
875 if (rename(p, fname) >= 0)
885 int executable_is_script(const char *path, char **interpreter) {
887 _cleanup_free_ char *line = NULL;
893 r = read_one_line_file(path, &line);
897 if (!startswith(line, "#!"))
900 ans = strstrip(line + 2);
901 len = strcspn(ans, " \t");
906 ans = strndup(ans, len);
916 * Retrieve one field from a file like /proc/self/status. pattern
917 * should not include whitespace or the delimiter (':'). pattern matches only
918 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
919 * zeros after the ':' will be skipped. field must be freed afterwards.
920 * terminator specifies the terminating characters of the field value (not
921 * included in the value).
923 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
924 _cleanup_free_ char *status = NULL;
934 r = read_full_file(filename, &status, NULL);
944 t = strstr(t, pattern);
948 /* Check that pattern occurs in beginning of line. */
949 pattern_ok = (t == status || t[-1] == '\n');
951 t += strlen(pattern);
953 } while (!pattern_ok);
955 t += strspn(t, " \t");
964 t += strspn(t, " \t");
966 /* Also skip zeros, because when this is used for
967 * capabilities, we don't want the zeros. This way the
968 * same capability set always maps to the same string,
969 * irrespective of the total capability set size. For
970 * other numbers it shouldn't matter. */
972 /* Back off one char if there's nothing but whitespace
974 if (!*t || isspace(*t))
978 len = strcspn(t, terminator);
988 DIR *xopendirat(int fd, const char *name, int flags) {
992 assert(!(flags & O_CREAT));
994 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
1007 #if 0 /// UNNEEDED by elogind
1008 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
1015 if (!path_strv_resolve_uniq(search, root))
1018 STRV_FOREACH(i, search) {
1019 _cleanup_free_ char *p = NULL;
1023 p = strjoin(root, *i, "/", path);
1025 p = strjoin(*i, "/", path);
1035 if (errno != ENOENT)
1042 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
1043 _cleanup_strv_free_ char **copy = NULL;
1049 if (path_is_absolute(path)) {
1052 f = fopen(path, mode);
1061 copy = strv_copy((char**) search);
1065 return search_and_fopen_internal(path, mode, root, copy, _f);
1068 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1069 _cleanup_strv_free_ char **s = NULL;
1071 if (path_is_absolute(path)) {
1074 f = fopen(path, mode);
1083 s = strv_split_nulstr(search);
1087 return search_and_fopen_internal(path, mode, root, s, _f);
1091 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
1100 r = tempfn_xxxxxx(path, NULL, &t);
1104 fd = mkostemp_safe(t);
1110 f = fdopen(fd, "we");
1124 int fflush_and_check(FILE *f) {
1131 return errno > 0 ? -errno : -EIO;
1136 int fflush_sync_and_check(FILE *f) {
1141 r = fflush_and_check(f);
1145 if (fsync(fileno(f)) < 0)
1151 /* This is much like mkostemp() but is subject to umask(). */
1152 int mkostemp_safe(char *pattern) {
1153 _cleanup_umask_ mode_t u = 0;
1160 fd = mkostemp(pattern, O_CLOEXEC);
1167 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
1179 * /foo/bar/.#<extra>waldoXXXXXX
1183 if (!filename_is_valid(fn))
1189 t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
1193 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
1195 *ret = path_kill_slashes(t);
1199 int tempfn_random(const char *p, const char *extra, char **ret) {
1213 * /foo/bar/.#<extra>waldobaa2a261115984a9
1217 if (!filename_is_valid(fn))
1223 t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
1227 x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
1230 for (i = 0; i < 16; i++) {
1231 *(x++) = hexchar(u & 0xF);
1237 *ret = path_kill_slashes(t);
1241 #if 0 /// UNNEEDED by elogind
1242 int tempfn_random_child(const char *p, const char *extra, char **ret) {
1253 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1265 t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
1269 x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
1272 for (i = 0; i < 16; i++) {
1273 *(x++) = hexchar(u & 0xF);
1279 *ret = path_kill_slashes(t);
1283 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1284 char ln[DECIMAL_STR_MAX(n)+2];
1286 /* Creates a "timestamp" file, that contains nothing but a
1287 * usec_t timestamp, formatted in ASCII. */
1289 if (n <= 0 || n >= USEC_INFINITY)
1292 xsprintf(ln, USEC_FMT "\n", n);
1294 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1297 int read_timestamp_file(const char *fn, usec_t *ret) {
1298 _cleanup_free_ char *ln = NULL;
1302 r = read_one_line_file(fn, &ln);
1306 r = safe_atou64(ln, &t);
1310 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
1317 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
1322 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1323 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1324 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1325 * element, but not before the first one. */
1335 r = fputs(separator, f);
1347 int open_tmpfile_unlinkable(const char *directory, int flags) {
1352 r = tmp_dir(&directory);
1357 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1359 /* Try O_TMPFILE first, if it is supported */
1360 fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
1364 /* Fall back to unguessable name + unlinking */
1365 p = strjoina(directory, "/systemd-tmp-XXXXXX");
1367 fd = mkostemp_safe(p);
1376 #if 0 /// UNNEEDED by elogind
1377 int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
1378 _cleanup_free_ char *tmp = NULL;
1384 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1385 assert((flags & O_EXCL) == 0);
1387 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1388 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1389 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1392 _cleanup_free_ char *dn = NULL;
1394 dn = dirname_malloc(target);
1398 fd = open(dn, O_TMPFILE|flags, 0640);
1404 log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
1407 r = tempfn_random(target, NULL, &tmp);
1411 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
1422 int open_serialization_fd(const char *ident) {
1425 fd = memfd_create(ident, MFD_CLOEXEC);
1429 path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1430 fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1434 log_debug("Serializing %s to %s.", ident, path);
1436 log_debug("Serializing %s to memfd.", ident);
1441 #if 0 /// UNNEEDED by elogind
1442 int link_tmpfile(int fd, const char *path, const char *target) {
1447 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1448 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1449 * on the directory, and renameat2() is used instead.
1451 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1452 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1455 if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
1458 char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1460 xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
1462 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
1469 int read_nul_string(FILE *f, char **ret) {
1470 _cleanup_free_ char *x = NULL;
1471 size_t allocated = 0, n = 0;
1476 /* Reads a NUL-terminated string from the specified file. */
1481 if (!GREEDY_REALLOC(x, allocated, n+2))
1485 if (c == 0) /* Terminate at NUL byte */
1490 break; /* Terminate at EOF */
1510 int mkdtemp_malloc(const char *template, char **ret) {
1516 p = strdup(template);
1530 static inline void funlockfilep(FILE **f) {
1534 int read_line(FILE *f, size_t limit, char **ret) {
1535 _cleanup_free_ char *buffer = NULL;
1536 size_t n = 0, allocated = 0, count = 0;
1540 /* Something like a bounded version of getline().
1542 * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
1545 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1546 * the number of characters in the returned string). When EOF is hit, 0 is returned.
1548 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1549 * delimiters. If the limit is hit we fail and return -ENOBUFS.
1551 * If a line shall be skipped ret may be initialized as NULL. */
1554 if (!GREEDY_REALLOC(buffer, allocated, 1))
1559 _cleanup_(funlockfilep) FILE *flocked = f;
1569 c = fgetc_unlocked(f);
1571 /* if we read an error, and have no data to return, then propagate the error */
1572 if (ferror_unlocked(f) && n == 0)
1573 return errno > 0 ? -errno : -EIO;
1580 if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
1584 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
1587 buffer[n] = (char) c;