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"
37 #include "hexdecoct.h"
41 #include "parse-util.h"
42 #include "path-util.h"
43 #include "process-util.h"
44 #include "random-util.h"
45 #include "stdio-util.h"
46 #include "string-util.h"
48 //#include "time-util.h"
49 #include "umask-util.h"
52 #define READ_FULL_BYTES_MAX (4U*1024U*1024U)
54 int write_string_stream_ts(FILE *f, const char *line, bool enforce_newline, struct timespec *ts) {
60 if (enforce_newline && !endswith(line, "\n"))
64 struct timespec twice[2] = {*ts, *ts};
66 if (futimens(fileno(f), twice) < 0)
70 return fflush_and_check(f);
73 static int write_string_file_atomic(const char *fn, const char *line, bool enforce_newline, bool do_fsync) {
74 _cleanup_fclose_ FILE *f = NULL;
75 _cleanup_free_ char *p = NULL;
81 r = fopen_temporary(fn, &f, &p);
85 (void) fchmod_umask(fileno(f), 0644);
87 r = write_string_stream(f, line, enforce_newline);
88 if (r >= 0 && do_fsync)
89 r = fflush_sync_and_check(f);
92 if (rename(p, fn) < 0)
102 int write_string_file_ts(const char *fn, const char *line, WriteStringFileFlags flags, struct timespec *ts) {
103 _cleanup_fclose_ FILE *f = NULL;
109 /* We don't know how to verify whether the file contents was already on-disk. */
110 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
112 if (flags & WRITE_STRING_FILE_ATOMIC) {
113 assert(flags & WRITE_STRING_FILE_CREATE);
115 r = write_string_file_atomic(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE),
116 flags & WRITE_STRING_FILE_SYNC);
124 if (flags & WRITE_STRING_FILE_CREATE) {
133 /* We manually build our own version of fopen(..., "we") that
134 * works without O_CREAT */
135 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
141 f = fdopen(fd, "we");
149 r = write_string_stream_ts(f, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE), ts);
153 if (flags & WRITE_STRING_FILE_SYNC) {
154 r = fflush_sync_and_check(f);
162 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
167 /* OK, the operation failed, but let's see if the right
168 * contents in place already. If so, eat up the error. */
170 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
177 int read_one_line_file(const char *fn, char **line) {
178 _cleanup_fclose_ FILE *f = NULL;
187 return read_line(f, LONG_LINE_MAX, line);
190 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
191 _cleanup_fclose_ FILE *f = NULL;
192 _cleanup_free_ char *buf = NULL;
200 if (accept_extra_nl && endswith(blob, "\n"))
201 accept_extra_nl = false;
203 buf = malloc(l + accept_extra_nl + 1);
211 /* We try to read one byte more than we need, so that we know whether we hit eof */
213 k = fread(buf, 1, l + accept_extra_nl + 1, f);
215 return errno > 0 ? -errno : -EIO;
217 if (k != l && k != l + accept_extra_nl)
219 if (memcmp(buf, blob, l) != 0)
221 if (k > l && buf[l] != '\n')
227 int read_full_stream(FILE *f, char **contents, size_t *size) {
229 _cleanup_free_ char *buf = NULL;
235 if (fstat(fileno(f), &st) < 0)
240 if (S_ISREG(st.st_mode)) {
243 if (st.st_size > READ_FULL_BYTES_MAX)
246 /* Start with the right file size, but be prepared for
247 * files from /proc which generally report a file size
258 t = realloc(buf, n + 1);
263 k = fread(buf + l, 1, n - l, f);
273 /* We aren't expecting fread() to return a short read outside
274 * of (error && eof), assert buffer is full and enlarge buffer.
279 if (n >= READ_FULL_BYTES_MAX)
282 n = MIN(n * 2, READ_FULL_BYTES_MAX);
287 buf = NULL; /* do not free */
295 int read_full_file(const char *fn, char **contents, size_t *size) {
296 _cleanup_fclose_ FILE *f = NULL;
305 return read_full_stream(f, contents, size);
308 static int parse_env_file_internal(
312 int (*push) (const char *filename, unsigned line,
313 const char *key, char *value, void *userdata, int *n_pushed),
317 _cleanup_free_ char *contents = NULL, *key = NULL;
318 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;
319 char *p, *value = NULL;
330 SINGLE_QUOTE_VALUE_ESCAPE,
332 DOUBLE_QUOTE_VALUE_ESCAPE,
340 r = read_full_stream(f, &contents, NULL);
342 r = read_full_file(fname, &contents, NULL);
346 for (p = contents; *p; p++) {
352 if (strchr(COMMENTS, c))
354 else if (!strchr(WHITESPACE, c)) {
356 last_key_whitespace = (size_t) -1;
358 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
368 if (strchr(newline, c)) {
372 } else if (c == '=') {
374 last_value_whitespace = (size_t) -1;
376 if (!strchr(WHITESPACE, c))
377 last_key_whitespace = (size_t) -1;
378 else if (last_key_whitespace == (size_t) -1)
379 last_key_whitespace = n_key;
381 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
392 if (strchr(newline, c)) {
400 /* strip trailing whitespace from key */
401 if (last_key_whitespace != (size_t) -1)
402 key[last_key_whitespace] = 0;
404 r = push(fname, line, key, value, userdata, n_pushed);
410 value_alloc = n_value = 0;
412 } else if (c == '\'')
413 state = SINGLE_QUOTE_VALUE;
415 state = DOUBLE_QUOTE_VALUE;
417 state = VALUE_ESCAPE;
418 else if (!strchr(WHITESPACE, c)) {
421 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
426 value[n_value++] = c;
432 if (strchr(newline, c)) {
441 /* Chomp off trailing whitespace from value */
442 if (last_value_whitespace != (size_t) -1)
443 value[last_value_whitespace] = 0;
445 /* strip trailing whitespace from key */
446 if (last_key_whitespace != (size_t) -1)
447 key[last_key_whitespace] = 0;
449 r = push(fname, line, key, value, userdata, n_pushed);
455 value_alloc = n_value = 0;
457 } else if (c == '\\') {
458 state = VALUE_ESCAPE;
459 last_value_whitespace = (size_t) -1;
461 if (!strchr(WHITESPACE, c))
462 last_value_whitespace = (size_t) -1;
463 else if (last_value_whitespace == (size_t) -1)
464 last_value_whitespace = n_value;
466 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
471 value[n_value++] = c;
479 if (!strchr(newline, c)) {
480 /* Escaped newlines we eat up entirely */
481 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
486 value[n_value++] = c;
490 case SINGLE_QUOTE_VALUE:
494 state = SINGLE_QUOTE_VALUE_ESCAPE;
496 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
501 value[n_value++] = c;
506 case SINGLE_QUOTE_VALUE_ESCAPE:
507 state = SINGLE_QUOTE_VALUE;
509 if (!strchr(newline, c)) {
510 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
515 value[n_value++] = c;
519 case DOUBLE_QUOTE_VALUE:
523 state = DOUBLE_QUOTE_VALUE_ESCAPE;
525 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
530 value[n_value++] = c;
535 case DOUBLE_QUOTE_VALUE_ESCAPE:
536 state = DOUBLE_QUOTE_VALUE;
538 if (!strchr(newline, c)) {
539 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
544 value[n_value++] = c;
550 state = COMMENT_ESCAPE;
551 else if (strchr(newline, c)) {
568 SINGLE_QUOTE_VALUE_ESCAPE,
570 DOUBLE_QUOTE_VALUE_ESCAPE)) {
578 if (last_value_whitespace != (size_t) -1)
579 value[last_value_whitespace] = 0;
581 /* strip trailing whitespace from key */
582 if (last_key_whitespace != (size_t) -1)
583 key[last_key_whitespace] = 0;
585 r = push(fname, line, key, value, userdata, n_pushed);
597 static int check_utf8ness_and_warn(
598 const char *filename, unsigned line,
599 const char *key, char *value) {
601 if (!utf8_is_valid(key)) {
602 _cleanup_free_ char *p = NULL;
604 p = utf8_escape_invalid(key);
605 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
609 if (value && !utf8_is_valid(value)) {
610 _cleanup_free_ char *p = NULL;
612 p = utf8_escape_invalid(value);
613 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
620 static int parse_env_file_push(
621 const char *filename, unsigned line,
622 const char *key, char *value,
627 va_list aq, *ap = userdata;
630 r = check_utf8ness_and_warn(filename, line, key, value);
636 while ((k = va_arg(aq, const char *))) {
639 v = va_arg(aq, char **);
661 const char *newline, ...) {
669 va_start(ap, newline);
670 r = parse_env_file_internal(NULL, fname, newline, parse_env_file_push, &ap, &n_pushed);
673 return r < 0 ? r : n_pushed;
676 #if 0 /// UNNEEDED by elogind
677 static int load_env_file_push(
678 const char *filename, unsigned line,
679 const char *key, char *value,
682 char ***m = userdata;
686 r = check_utf8ness_and_warn(filename, line, key, value);
690 p = strjoin(key, "=", value);
694 r = strv_env_replace(m, p);
707 int load_env_file(FILE *f, const char *fname, const char *newline, char ***rl) {
714 r = parse_env_file_internal(f, fname, newline, load_env_file_push, &m, NULL);
725 static int load_env_file_push_pairs(
726 const char *filename, unsigned line,
727 const char *key, char *value,
730 char ***m = userdata;
733 r = check_utf8ness_and_warn(filename, line, key, value);
737 r = strv_extend(m, key);
742 r = strv_extend(m, "");
746 r = strv_push(m, value);
757 int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ***rl) {
764 r = parse_env_file_internal(f, fname, newline, load_env_file_push_pairs, &m, NULL);
773 #if 0 /// UNNEEDED by elogind
775 static int merge_env_file_push(
776 const char *filename, unsigned line,
777 const char *key, char *value,
781 char ***env = userdata;
782 char *expanded_value;
787 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
791 if (!env_name_is_valid(key)) {
792 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
797 expanded_value = replace_env(value, *env,
798 REPLACE_ENV_USE_ENVIRONMENT|
799 REPLACE_ENV_ALLOW_BRACELESS|
800 REPLACE_ENV_ALLOW_EXTENDED);
804 free_and_replace(value, expanded_value);
806 return load_env_file_push(filename, line, key, value, env, n_pushed);
814 /* NOTE: this function supports braceful and braceless variable expansions,
815 * plus "extended" substitutions, unlike other exported parsing functions.
818 return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);
821 static void write_env_var(FILE *f, const char *v) {
827 fputs_unlocked(v, f);
828 fputc_unlocked('\n', f);
833 fwrite_unlocked(v, 1, p-v, f);
835 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
836 fputc_unlocked('\"', f);
839 if (strchr(SHELL_NEED_ESCAPE, *p))
840 fputc_unlocked('\\', f);
842 fputc_unlocked(*p, f);
845 fputc_unlocked('\"', f);
847 fputs_unlocked(p, f);
849 fputc_unlocked('\n', f);
852 int write_env_file(const char *fname, char **l) {
853 _cleanup_fclose_ FILE *f = NULL;
854 _cleanup_free_ char *p = NULL;
860 r = fopen_temporary(fname, &f, &p);
864 fchmod_umask(fileno(f), 0644);
867 write_env_var(f, *i);
869 r = fflush_and_check(f);
871 if (rename(p, fname) >= 0)
881 int executable_is_script(const char *path, char **interpreter) {
883 _cleanup_free_ char *line = NULL;
889 r = read_one_line_file(path, &line);
893 if (!startswith(line, "#!"))
896 ans = strstrip(line + 2);
897 len = strcspn(ans, " \t");
902 ans = strndup(ans, len);
912 * Retrieve one field from a file like /proc/self/status. pattern
913 * should not include whitespace or the delimiter (':'). pattern matches only
914 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
915 * zeros after the ':' will be skipped. field must be freed afterwards.
916 * terminator specifies the terminating characters of the field value (not
917 * included in the value).
919 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
920 _cleanup_free_ char *status = NULL;
930 r = read_full_file(filename, &status, NULL);
940 t = strstr(t, pattern);
944 /* Check that pattern occurs in beginning of line. */
945 pattern_ok = (t == status || t[-1] == '\n');
947 t += strlen(pattern);
949 } while (!pattern_ok);
951 t += strspn(t, " \t");
960 t += strspn(t, " \t");
962 /* Also skip zeros, because when this is used for
963 * capabilities, we don't want the zeros. This way the
964 * same capability set always maps to the same string,
965 * irrespective of the total capability set size. For
966 * other numbers it shouldn't matter. */
968 /* Back off one char if there's nothing but whitespace
970 if (!*t || isspace(*t))
974 len = strcspn(t, terminator);
984 DIR *xopendirat(int fd, const char *name, int flags) {
988 assert(!(flags & O_CREAT));
990 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
1003 #if 0 /// UNNEEDED by elogind
1004 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
1011 if (!path_strv_resolve_uniq(search, root))
1014 STRV_FOREACH(i, search) {
1015 _cleanup_free_ char *p = NULL;
1019 p = strjoin(root, *i, "/", path);
1021 p = strjoin(*i, "/", path);
1031 if (errno != ENOENT)
1038 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
1039 _cleanup_strv_free_ char **copy = NULL;
1045 if (path_is_absolute(path)) {
1048 f = fopen(path, mode);
1057 copy = strv_copy((char**) search);
1061 return search_and_fopen_internal(path, mode, root, copy, _f);
1064 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1065 _cleanup_strv_free_ char **s = NULL;
1067 if (path_is_absolute(path)) {
1070 f = fopen(path, mode);
1079 s = strv_split_nulstr(search);
1083 return search_and_fopen_internal(path, mode, root, s, _f);
1087 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
1096 r = tempfn_xxxxxx(path, NULL, &t);
1100 fd = mkostemp_safe(t);
1106 f = fdopen(fd, "we");
1120 int fflush_and_check(FILE *f) {
1127 return errno > 0 ? -errno : -EIO;
1132 int fflush_sync_and_check(FILE *f) {
1137 r = fflush_and_check(f);
1141 if (fsync(fileno(f)) < 0)
1147 /* This is much like mkostemp() but is subject to umask(). */
1148 int mkostemp_safe(char *pattern) {
1149 _cleanup_umask_ mode_t u = 0;
1156 fd = mkostemp(pattern, O_CLOEXEC);
1163 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
1175 * /foo/bar/.#<extra>waldoXXXXXX
1179 if (!filename_is_valid(fn))
1185 t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
1189 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
1191 *ret = path_kill_slashes(t);
1195 int tempfn_random(const char *p, const char *extra, char **ret) {
1209 * /foo/bar/.#<extra>waldobaa2a261115984a9
1213 if (!filename_is_valid(fn))
1219 t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
1223 x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
1226 for (i = 0; i < 16; i++) {
1227 *(x++) = hexchar(u & 0xF);
1233 *ret = path_kill_slashes(t);
1237 #if 0 /// UNNEEDED by elogind
1238 int tempfn_random_child(const char *p, const char *extra, char **ret) {
1249 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1261 t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
1265 x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
1268 for (i = 0; i < 16; i++) {
1269 *(x++) = hexchar(u & 0xF);
1275 *ret = path_kill_slashes(t);
1279 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1280 char ln[DECIMAL_STR_MAX(n)+2];
1282 /* Creates a "timestamp" file, that contains nothing but a
1283 * usec_t timestamp, formatted in ASCII. */
1285 if (n <= 0 || n >= USEC_INFINITY)
1288 xsprintf(ln, USEC_FMT "\n", n);
1290 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1293 int read_timestamp_file(const char *fn, usec_t *ret) {
1294 _cleanup_free_ char *ln = NULL;
1298 r = read_one_line_file(fn, &ln);
1302 r = safe_atou64(ln, &t);
1306 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
1313 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
1318 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1319 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1320 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1321 * element, but not before the first one. */
1331 r = fputs(separator, f);
1343 int open_tmpfile_unlinkable(const char *directory, int flags) {
1348 r = tmp_dir(&directory);
1353 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1355 /* Try O_TMPFILE first, if it is supported */
1356 fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
1360 /* Fall back to unguessable name + unlinking */
1361 p = strjoina(directory, "/systemd-tmp-XXXXXX");
1363 fd = mkostemp_safe(p);
1372 #if 0 /// UNNEEDED by elogind
1373 int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
1374 _cleanup_free_ char *tmp = NULL;
1380 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1381 assert((flags & O_EXCL) == 0);
1383 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1384 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1385 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1388 _cleanup_free_ char *dn = NULL;
1390 dn = dirname_malloc(target);
1394 fd = open(dn, O_TMPFILE|flags, 0640);
1400 log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
1403 r = tempfn_random(target, NULL, &tmp);
1407 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
1418 int open_serialization_fd(const char *ident) {
1421 fd = memfd_create(ident, MFD_CLOEXEC);
1425 path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1426 fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1430 log_debug("Serializing %s to %s.", ident, path);
1432 log_debug("Serializing %s to memfd.", ident);
1437 #if 0 /// UNNEEDED by elogind
1438 int link_tmpfile(int fd, const char *path, const char *target) {
1443 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1444 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1445 * on the directory, and renameat2() is used instead.
1447 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1448 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1451 if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
1454 char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1456 xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
1458 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
1465 int read_nul_string(FILE *f, char **ret) {
1466 _cleanup_free_ char *x = NULL;
1467 size_t allocated = 0, n = 0;
1472 /* Reads a NUL-terminated string from the specified file. */
1477 if (!GREEDY_REALLOC(x, allocated, n+2))
1481 if (c == 0) /* Terminate at NUL byte */
1486 break; /* Terminate at EOF */
1506 int mkdtemp_malloc(const char *template, char **ret) {
1512 p = strdup(template);
1526 int read_line(FILE *f, size_t limit, char **ret) {
1527 _cleanup_free_ char *buffer = NULL;
1528 size_t n = 0, allocated = 0, count = 0;
1533 /* Something like a bounded version of getline().
1535 * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
1538 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1539 * the number of characters in the returned string). When EOF is hit, 0 is returned.
1541 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1542 * delimiters. If the limit is hit we fail and return -ENOBUFS.
1544 * If a line shall be skipped ret may be initialized as NULL. */
1547 if (!GREEDY_REALLOC(buffer, allocated, 1))
1562 c = fgetc_unlocked(f);
1564 /* if we read an error, and have no data to return, then propagate the error */
1565 if (ferror_unlocked(f) && n == 0) {
1566 r = errno > 0 ? -errno : -EIO;
1576 if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
1580 if (!GREEDY_REALLOC(buffer, allocated, n + 2)) {
1585 buffer[n] = (char) c;