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"
38 #include "hexdecoct.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #include "random-util.h"
46 #include "stdio-util.h"
47 #include "string-util.h"
49 //#include "time-util.h"
50 #include "umask-util.h"
53 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
55 int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
61 if (enforce_newline && !endswith(line, "\n"))
65 struct timespec twice[2] = {*ts, *ts};
67 if (futimens(fileno(f), twice) < 0)
71 return fflush_and_check(f);
74 static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
75 _cleanup_fclose_ FILE *f = NULL;
76 _cleanup_free_ char *p = NULL;
82 r = fopen_temporary(fn, &f, &p);
86 (void) fchmod_umask(fileno(f), 0644);
88 r = write_string_stream(f, line, enforce_newline);
89 if (r >= 0 && do_fsync)
90 r = fflush_sync_and_check(f);
93 if (rename(p, fn) < 0)
103 int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
104 _cleanup_fclose_ FILE *f = NULL;
110 /* We don't know how to verify whether the file contents was already on-disk. */
111 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
113 if (flags & WRITE_STRING_FILE_ATOMIC) {
114 assert(flags & WRITE_STRING_FILE_CREATE);
116 r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
117 flags & WRITE_STRING_FILE_SYNC);
125 if (flags & WRITE_STRING_FILE_CREATE) {
134 /* We manually build our own version of fopen(..., "we") that
135 * works without O_CREAT */
136 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
142 f = fdopen(fd, "we");
150 r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
154 if (flags & WRITE_STRING_FILE_SYNC) {
155 r = fflush_sync_and_check(f);
163 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
168 /* OK, the operation failed, but let's see if the right
169 * contents in place already. If so, eat up the error. */
171 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
178 int read_one_line_file(const char *fn, char **line) {
179 _cleanup_fclose_ FILE *f = NULL;
189 r = read_line(f, LONG_LINE_MAX, line);
190 return r < 0 ? r : 0;
193 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
194 _cleanup_fclose_ FILE *f = NULL;
195 _cleanup_free_ char *buf = NULL;
203 if (accept_extra_nl && endswith(blob, "\n"))
204 accept_extra_nl = false;
206 buf = malloc(l + accept_extra_nl + 1);
214 /* We try to read one byte more than we need, so that we know whether we hit eof */
216 k = fread(buf, 1, l + accept_extra_nl + 1, f);
218 return errno > 0 ? -errno : -EIO;
220 if (k != l && k != l + accept_extra_nl)
222 if (memcmp(buf, blob, l) != 0)
224 if (k > l && buf[l] != '\n')
230 int read_full_stream(FILE *f, char **contents, size_t *size) {
232 _cleanup_free_ char *buf = NULL;
238 if (fstat(fileno(f), &st) < 0)
243 if (S_ISREG(st.st_mode)) {
246 if (st.st_size > READ_FULL_BYTES_MAX)
249 /* Start with the right file size, but be prepared for
250 * files from /proc which generally report a file size
261 t = realloc(buf, n + 1);
266 k = fread(buf + l, 1, n - l, f);
276 /* We aren't expecting fread() to return a short read outside
277 * of (error && eof), assert buffer is full and enlarge buffer.
282 if (n >= READ_FULL_BYTES_MAX)
285 n = MIN(n * 2, READ_FULL_BYTES_MAX);
290 buf = NULL; /* do not free */
298 int read_full_file(const char *fn, char **contents, size_t *size) {
299 _cleanup_fclose_ FILE *f = NULL;
308 return read_full_stream(f, contents, size);
311 static int parse_env_file_internal(
315 int (*push) (const char *filename, unsigned line,
316 const char *key, char *value, void *userdata, int *n_pushed),
320 _cleanup_free_ char *contents = NULL, *key = NULL;
321 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;
322 char *p, *value = NULL;
333 SINGLE_QUOTE_VALUE_ESCAPE,
335 DOUBLE_QUOTE_VALUE_ESCAPE,
343 r = read_full_stream(f, &contents, NULL);
345 r = read_full_file(fname, &contents, NULL);
349 for (p = contents; *p; p++) {
355 if (strchr(COMMENTS, c))
357 else if (!strchr(WHITESPACE, c)) {
359 last_key_whitespace = (size_t) -1;
361 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
371 if (strchr(newline, c)) {
375 } else if (c == '=') {
377 last_value_whitespace = (size_t) -1;
379 if (!strchr(WHITESPACE, c))
380 last_key_whitespace = (size_t) -1;
381 else if (last_key_whitespace == (size_t) -1)
382 last_key_whitespace = n_key;
384 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
395 if (strchr(newline, c)) {
403 /* strip trailing whitespace from key */
404 if (last_key_whitespace != (size_t) -1)
405 key[last_key_whitespace] = 0;
407 r = push(fname, line, key, value, userdata, n_pushed);
413 value_alloc = n_value = 0;
415 } else if (c == '\'')
416 state = SINGLE_QUOTE_VALUE;
418 state = DOUBLE_QUOTE_VALUE;
420 state = VALUE_ESCAPE;
421 else if (!strchr(WHITESPACE, c)) {
424 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
429 value[n_value++] = c;
435 if (strchr(newline, c)) {
444 /* Chomp off trailing whitespace from value */
445 if (last_value_whitespace != (size_t) -1)
446 value[last_value_whitespace] = 0;
448 /* strip trailing whitespace from key */
449 if (last_key_whitespace != (size_t) -1)
450 key[last_key_whitespace] = 0;
452 r = push(fname, line, key, value, userdata, n_pushed);
458 value_alloc = n_value = 0;
460 } else if (c == '\\') {
461 state = VALUE_ESCAPE;
462 last_value_whitespace = (size_t) -1;
464 if (!strchr(WHITESPACE, c))
465 last_value_whitespace = (size_t) -1;
466 else if (last_value_whitespace == (size_t) -1)
467 last_value_whitespace = n_value;
469 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
474 value[n_value++] = c;
482 if (!strchr(newline, c)) {
483 /* Escaped newlines we eat up entirely */
484 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
489 value[n_value++] = c;
493 case SINGLE_QUOTE_VALUE:
497 state = SINGLE_QUOTE_VALUE_ESCAPE;
499 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
504 value[n_value++] = c;
509 case SINGLE_QUOTE_VALUE_ESCAPE:
510 state = SINGLE_QUOTE_VALUE;
512 if (!strchr(newline, c)) {
513 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
518 value[n_value++] = c;
522 case DOUBLE_QUOTE_VALUE:
526 state = DOUBLE_QUOTE_VALUE_ESCAPE;
528 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
533 value[n_value++] = c;
538 case DOUBLE_QUOTE_VALUE_ESCAPE:
539 state = DOUBLE_QUOTE_VALUE;
541 if (!strchr(newline, c)) {
542 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
547 value[n_value++] = c;
553 state = COMMENT_ESCAPE;
554 else if (strchr(newline, c)) {
571 SINGLE_QUOTE_VALUE_ESCAPE,
573 DOUBLE_QUOTE_VALUE_ESCAPE)) {
581 if (last_value_whitespace != (size_t) -1)
582 value[last_value_whitespace] = 0;
584 /* strip trailing whitespace from key */
585 if (last_key_whitespace != (size_t) -1)
586 key[last_key_whitespace] = 0;
588 r = push(fname, line, key, value, userdata, n_pushed);
600 static int check_utf8ness_and_warn(
601 const char *filename, unsigned line,
602 const char *key, char *value) {
604 if (!utf8_is_valid(key)) {
605 _cleanup_free_ char *p = NULL;
607 p = utf8_escape_invalid(key);
608 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
612 if (value && !utf8_is_valid(value)) {
613 _cleanup_free_ char *p = NULL;
615 p = utf8_escape_invalid(value);
616 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
623 static int parse_env_file_push(
624 const char *filename, unsigned line,
625 const char *key, char *value,
630 va_list aq, *ap = userdata;
633 r = check_utf8ness_and_warn(filename, line, key, value);
639 while ((k = va_arg(aq, const char *))) {
642 v = va_arg(aq, char **);
664 const char *newline, ...) {
672 va_start(ap, newline);
673 r = parse_env_file_internal(NULL, fname, newline, parse_env_file_push, &ap, &n_pushed);
676 return r < 0 ? r : n_pushed;
679 #if 0 /// UNNEEDED by elogind
680 static int load_env_file_push(
681 const char *filename, unsigned line,
682 const char *key, char *value,
685 char ***m = userdata;
689 r = check_utf8ness_and_warn(filename, line, key, value);
693 p = strjoin(key, "=", value);
697 r = strv_env_replace(m, p);
710 int load_env_file(FILE *f, const char *fname, const char *newline, char ***rl) {
717 r = parse_env_file_internal(f, fname, newline, load_env_file_push, &m, NULL);
728 static int load_env_file_push_pairs(
729 const char *filename, unsigned line,
730 const char *key, char *value,
733 char ***m = userdata;
736 r = check_utf8ness_and_warn(filename, line, key, value);
740 r = strv_extend(m, key);
745 r = strv_extend(m, "");
749 r = strv_push(m, value);
760 int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ***rl) {
767 r = parse_env_file_internal(f, fname, newline, load_env_file_push_pairs, &m, NULL);
776 #if 0 /// UNNEEDED by elogind
778 static int merge_env_file_push(
779 const char *filename, unsigned line,
780 const char *key, char *value,
784 char ***env = userdata;
785 char *expanded_value;
790 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
794 if (!env_name_is_valid(key)) {
795 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
800 expanded_value = replace_env(value, *env,
801 REPLACE_ENV_USE_ENVIRONMENT|
802 REPLACE_ENV_ALLOW_BRACELESS|
803 REPLACE_ENV_ALLOW_EXTENDED);
807 free_and_replace(value, expanded_value);
809 return load_env_file_push(filename, line, key, value, env, n_pushed);
817 /* NOTE: this function supports braceful and braceless variable expansions,
818 * plus "extended" substitutions, unlike other exported parsing functions.
821 return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);
824 static void write_env_var(FILE *f, const char *v) {
830 fputs_unlocked(v, f);
831 fputc_unlocked('\n', f);
836 fwrite_unlocked(v, 1, p-v, f);
838 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
839 fputc_unlocked('\"', f);
842 if (strchr(SHELL_NEED_ESCAPE, *p))
843 fputc_unlocked('\\', f);
845 fputc_unlocked(*p, f);
848 fputc_unlocked('\"', f);
850 fputs_unlocked(p, f);
852 fputc_unlocked('\n', f);
855 int write_env_file(const char *fname, char **l) {
856 _cleanup_fclose_ FILE *f = NULL;
857 _cleanup_free_ char *p = NULL;
863 r = fopen_temporary(fname, &f, &p);
867 fchmod_umask(fileno(f), 0644);
870 write_env_var(f, *i);
872 r = fflush_and_check(f);
874 if (rename(p, fname) >= 0)
884 int executable_is_script(const char *path, char **interpreter) {
886 _cleanup_free_ char *line = NULL;
892 r = read_one_line_file(path, &line);
896 if (!startswith(line, "#!"))
899 ans = strstrip(line + 2);
900 len = strcspn(ans, " \t");
905 ans = strndup(ans, len);
915 * Retrieve one field from a file like /proc/self/status. pattern
916 * should not include whitespace or the delimiter (':'). pattern matches only
917 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
918 * zeros after the ':' will be skipped. field must be freed afterwards.
919 * terminator specifies the terminating characters of the field value (not
920 * included in the value).
922 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
923 _cleanup_free_ char *status = NULL;
933 r = read_full_file(filename, &status, NULL);
943 t = strstr(t, pattern);
947 /* Check that pattern occurs in beginning of line. */
948 pattern_ok = (t == status || t[-1] == '\n');
950 t += strlen(pattern);
952 } while (!pattern_ok);
954 t += strspn(t, " \t");
963 t += strspn(t, " \t");
965 /* Also skip zeros, because when this is used for
966 * capabilities, we don't want the zeros. This way the
967 * same capability set always maps to the same string,
968 * irrespective of the total capability set size. For
969 * other numbers it shouldn't matter. */
971 /* Back off one char if there's nothing but whitespace
973 if (!*t || isspace(*t))
977 len = strcspn(t, terminator);
987 DIR *xopendirat(int fd, const char *name, int flags) {
991 assert(!(flags & O_CREAT));
993 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
1006 #if 0 /// UNNEEDED by elogind
1007 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
1014 if (!path_strv_resolve_uniq(search, root))
1017 STRV_FOREACH(i, search) {
1018 _cleanup_free_ char *p = NULL;
1022 p = strjoin(root, *i, "/", path);
1024 p = strjoin(*i, "/", path);
1034 if (errno != ENOENT)
1041 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
1042 _cleanup_strv_free_ char **copy = NULL;
1048 if (path_is_absolute(path)) {
1051 f = fopen(path, mode);
1060 copy = strv_copy((char**) search);
1064 return search_and_fopen_internal(path, mode, root, copy, _f);
1067 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1068 _cleanup_strv_free_ char **s = NULL;
1070 if (path_is_absolute(path)) {
1073 f = fopen(path, mode);
1082 s = strv_split_nulstr(search);
1086 return search_and_fopen_internal(path, mode, root, s, _f);
1090 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
1099 r = tempfn_xxxxxx(path, NULL, &t);
1103 fd = mkostemp_safe(t);
1109 f = fdopen(fd, "we");
1123 int fflush_and_check(FILE *f) {
1130 return errno > 0 ? -errno : -EIO;
1135 int fflush_sync_and_check(FILE *f) {
1140 r = fflush_and_check(f);
1144 if (fsync(fileno(f)) < 0)
1150 /* This is much like mkostemp() but is subject to umask(). */
1151 int mkostemp_safe(char *pattern) {
1152 _cleanup_umask_ mode_t u = 0;
1159 fd = mkostemp(pattern, O_CLOEXEC);
1166 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
1178 * /foo/bar/.#<extra>waldoXXXXXX
1182 if (!filename_is_valid(fn))
1188 t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
1192 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
1194 *ret = path_kill_slashes(t);
1198 int tempfn_random(const char *p, const char *extra, char **ret) {
1212 * /foo/bar/.#<extra>waldobaa2a261115984a9
1216 if (!filename_is_valid(fn))
1222 t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
1226 x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
1229 for (i = 0; i < 16; i++) {
1230 *(x++) = hexchar(u & 0xF);
1236 *ret = path_kill_slashes(t);
1240 #if 0 /// UNNEEDED by elogind
1241 int tempfn_random_child(const char *p, const char *extra, char **ret) {
1252 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1264 t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
1268 x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
1271 for (i = 0; i < 16; i++) {
1272 *(x++) = hexchar(u & 0xF);
1278 *ret = path_kill_slashes(t);
1282 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1283 char ln[DECIMAL_STR_MAX(n)+2];
1285 /* Creates a "timestamp" file, that contains nothing but a
1286 * usec_t timestamp, formatted in ASCII. */
1288 if (n <= 0 || n >= USEC_INFINITY)
1291 xsprintf(ln, USEC_FMT "\n", n);
1293 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1296 int read_timestamp_file(const char *fn, usec_t *ret) {
1297 _cleanup_free_ char *ln = NULL;
1301 r = read_one_line_file(fn, &ln);
1305 r = safe_atou64(ln, &t);
1309 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
1316 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
1321 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1322 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1323 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1324 * element, but not before the first one. */
1334 r = fputs(separator, f);
1346 int open_tmpfile_unlinkable(const char *directory, int flags) {
1351 r = tmp_dir(&directory);
1356 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1358 /* Try O_TMPFILE first, if it is supported */
1359 fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
1363 /* Fall back to unguessable name + unlinking */
1364 p = strjoina(directory, "/systemd-tmp-XXXXXX");
1366 fd = mkostemp_safe(p);
1375 #if 0 /// UNNEEDED by elogind
1376 int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
1377 _cleanup_free_ char *tmp = NULL;
1383 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1384 assert((flags & O_EXCL) == 0);
1386 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1387 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1388 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1391 _cleanup_free_ char *dn = NULL;
1393 dn = dirname_malloc(target);
1397 fd = open(dn, O_TMPFILE|flags, 0640);
1403 log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
1406 r = tempfn_random(target, NULL, &tmp);
1410 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
1421 int open_serialization_fd(const char *ident) {
1424 fd = memfd_create(ident, MFD_CLOEXEC);
1428 path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1429 fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1433 log_debug("Serializing %s to %s.", ident, path);
1435 log_debug("Serializing %s to memfd.", ident);
1440 #if 0 /// UNNEEDED by elogind
1441 int link_tmpfile(int fd, const char *path, const char *target) {
1446 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1447 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1448 * on the directory, and renameat2() is used instead.
1450 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1451 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1454 if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
1457 char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1459 xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
1461 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
1468 int read_nul_string(FILE *f, char **ret) {
1469 _cleanup_free_ char *x = NULL;
1470 size_t allocated = 0, n = 0;
1475 /* Reads a NUL-terminated string from the specified file. */
1480 if (!GREEDY_REALLOC(x, allocated, n+2))
1484 if (c == 0) /* Terminate at NUL byte */
1489 break; /* Terminate at EOF */
1509 int mkdtemp_malloc(const char *template, char **ret) {
1515 p = strdup(template);
1529 static inline void funlockfilep(FILE **f) {
1533 int read_line(FILE *f, size_t limit, char **ret) {
1534 _cleanup_free_ char *buffer = NULL;
1535 size_t n = 0, allocated = 0, count = 0;
1539 /* Something like a bounded version of getline().
1541 * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
1544 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1545 * the number of characters in the returned string). When EOF is hit, 0 is returned.
1547 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1548 * delimiters. If the limit is hit we fail and return -ENOBUFS.
1550 * If a line shall be skipped ret may be initialized as NULL. */
1553 if (!GREEDY_REALLOC(buffer, allocated, 1))
1558 _cleanup_(funlockfilep) FILE *flocked = f;
1568 c = fgetc_unlocked(f);
1570 /* if we read an error, and have no data to return, then propagate the error */
1571 if (ferror_unlocked(f) && n == 0)
1572 return errno > 0 ? -errno : -EIO;
1579 if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
1583 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
1586 buffer[n] = (char) c;