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(
57 WriteStringFileFlags flags,
58 struct timespec *ts) {
64 if (!(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"))
68 struct timespec twice[2] = {*ts, *ts};
70 if (futimens(fileno(f), twice) < 0)
74 if (flags & WRITE_STRING_FILE_SYNC)
75 return fflush_sync_and_check(f);
77 return fflush_and_check(f);
80 static int write_string_file_atomic(
83 WriteStringFileFlags flags,
84 struct timespec *ts) {
86 _cleanup_fclose_ FILE *f = NULL;
87 _cleanup_free_ char *p = NULL;
93 r = fopen_temporary(fn, &f, &p);
97 (void) fchmod_umask(fileno(f), 0644);
99 r = write_string_stream_ts(f, line, flags, ts);
103 if (rename(p, fn) < 0) {
115 int write_string_file_ts(
118 WriteStringFileFlags flags,
119 struct timespec *ts) {
121 _cleanup_fclose_ FILE *f = NULL;
127 /* We don't know how to verify whether the file contents was already on-disk. */
128 assert(!((flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE) && (flags & WRITE_STRING_FILE_SYNC)));
130 if (flags & WRITE_STRING_FILE_ATOMIC) {
131 assert(flags & WRITE_STRING_FILE_CREATE);
133 r = write_string_file_atomic(fn, line, flags, ts);
141 if (flags & WRITE_STRING_FILE_CREATE) {
150 /* We manually build our own version of fopen(..., "we") that
151 * works without O_CREAT */
152 fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
158 f = fdopen(fd, "we");
166 r = write_string_stream_ts(f, line, flags, ts);
173 if (!(flags & WRITE_STRING_FILE_VERIFY_ON_FAILURE))
178 /* OK, the operation failed, but let's see if the right
179 * contents in place already. If so, eat up the error. */
181 q = verify_file(fn, line, !(flags & WRITE_STRING_FILE_AVOID_NEWLINE));
188 int read_one_line_file(const char *fn, char **line) {
189 _cleanup_fclose_ FILE *f = NULL;
198 return read_line(f, LONG_LINE_MAX, line);
201 int verify_file(const char *fn, const char *blob, bool accept_extra_nl) {
202 _cleanup_fclose_ FILE *f = NULL;
203 _cleanup_free_ char *buf = NULL;
211 if (accept_extra_nl && endswith(blob, "\n"))
212 accept_extra_nl = false;
214 buf = malloc(l + accept_extra_nl + 1);
222 /* We try to read one byte more than we need, so that we know whether we hit eof */
224 k = fread(buf, 1, l + accept_extra_nl + 1, f);
226 return errno > 0 ? -errno : -EIO;
228 if (k != l && k != l + accept_extra_nl)
230 if (memcmp(buf, blob, l) != 0)
232 if (k > l && buf[l] != '\n')
238 int read_full_stream(FILE *f, char **contents, size_t *size) {
240 _cleanup_free_ char *buf = NULL;
246 if (fstat(fileno(f), &st) < 0)
251 if (S_ISREG(st.st_mode)) {
254 if (st.st_size > READ_FULL_BYTES_MAX)
257 /* Start with the right file size, but be prepared for files from /proc which generally report a file
258 * size of 0. Note that we increase the size to read here by one, so that the first read attempt
259 * already makes us notice the EOF. */
269 t = realloc(buf, n + 1);
274 k = fread(buf + l, 1, n - l, f);
284 /* We aren't expecting fread() to return a short read outside
285 * of (error && eof), assert buffer is full and enlarge buffer.
290 if (n >= READ_FULL_BYTES_MAX)
293 n = MIN(n * 2, READ_FULL_BYTES_MAX);
298 buf = NULL; /* do not free */
306 int read_full_file(const char *fn, char **contents, size_t *size) {
307 _cleanup_fclose_ FILE *f = NULL;
316 return read_full_stream(f, contents, size);
319 static int parse_env_file_internal(
323 int (*push) (const char *filename, unsigned line,
324 const char *key, char *value, void *userdata, int *n_pushed),
328 _cleanup_free_ char *contents = NULL, *key = NULL;
329 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;
330 char *p, *value = NULL;
341 SINGLE_QUOTE_VALUE_ESCAPE,
343 DOUBLE_QUOTE_VALUE_ESCAPE,
351 r = read_full_stream(f, &contents, NULL);
353 r = read_full_file(fname, &contents, NULL);
357 for (p = contents; *p; p++) {
363 if (strchr(COMMENTS, c))
365 else if (!strchr(WHITESPACE, c)) {
367 last_key_whitespace = (size_t) -1;
369 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
379 if (strchr(newline, c)) {
383 } else if (c == '=') {
385 last_value_whitespace = (size_t) -1;
387 if (!strchr(WHITESPACE, c))
388 last_key_whitespace = (size_t) -1;
389 else if (last_key_whitespace == (size_t) -1)
390 last_key_whitespace = n_key;
392 if (!GREEDY_REALLOC(key, key_alloc, n_key+2)) {
403 if (strchr(newline, c)) {
411 /* strip trailing whitespace from key */
412 if (last_key_whitespace != (size_t) -1)
413 key[last_key_whitespace] = 0;
415 r = push(fname, line, key, value, userdata, n_pushed);
421 value_alloc = n_value = 0;
423 } else if (c == '\'')
424 state = SINGLE_QUOTE_VALUE;
426 state = DOUBLE_QUOTE_VALUE;
428 state = VALUE_ESCAPE;
429 else if (!strchr(WHITESPACE, c)) {
432 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
437 value[n_value++] = c;
443 if (strchr(newline, c)) {
452 /* Chomp off trailing whitespace from value */
453 if (last_value_whitespace != (size_t) -1)
454 value[last_value_whitespace] = 0;
456 /* strip trailing whitespace from key */
457 if (last_key_whitespace != (size_t) -1)
458 key[last_key_whitespace] = 0;
460 r = push(fname, line, key, value, userdata, n_pushed);
466 value_alloc = n_value = 0;
468 } else if (c == '\\') {
469 state = VALUE_ESCAPE;
470 last_value_whitespace = (size_t) -1;
472 if (!strchr(WHITESPACE, c))
473 last_value_whitespace = (size_t) -1;
474 else if (last_value_whitespace == (size_t) -1)
475 last_value_whitespace = n_value;
477 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
482 value[n_value++] = c;
490 if (!strchr(newline, c)) {
491 /* Escaped newlines we eat up entirely */
492 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
497 value[n_value++] = c;
501 case SINGLE_QUOTE_VALUE:
505 state = SINGLE_QUOTE_VALUE_ESCAPE;
507 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
512 value[n_value++] = c;
517 case SINGLE_QUOTE_VALUE_ESCAPE:
518 state = SINGLE_QUOTE_VALUE;
520 if (!strchr(newline, c)) {
521 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
526 value[n_value++] = c;
530 case DOUBLE_QUOTE_VALUE:
534 state = DOUBLE_QUOTE_VALUE_ESCAPE;
536 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
541 value[n_value++] = c;
546 case DOUBLE_QUOTE_VALUE_ESCAPE:
547 state = DOUBLE_QUOTE_VALUE;
549 if (!strchr(newline, c)) {
550 if (!GREEDY_REALLOC(value, value_alloc, n_value+2)) {
555 value[n_value++] = c;
561 state = COMMENT_ESCAPE;
562 else if (strchr(newline, c)) {
579 SINGLE_QUOTE_VALUE_ESCAPE,
581 DOUBLE_QUOTE_VALUE_ESCAPE)) {
589 if (last_value_whitespace != (size_t) -1)
590 value[last_value_whitespace] = 0;
592 /* strip trailing whitespace from key */
593 if (last_key_whitespace != (size_t) -1)
594 key[last_key_whitespace] = 0;
596 r = push(fname, line, key, value, userdata, n_pushed);
608 static int check_utf8ness_and_warn(
609 const char *filename, unsigned line,
610 const char *key, char *value) {
612 if (!utf8_is_valid(key)) {
613 _cleanup_free_ char *p = NULL;
615 p = utf8_escape_invalid(key);
616 log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p);
620 if (value && !utf8_is_valid(value)) {
621 _cleanup_free_ char *p = NULL;
623 p = utf8_escape_invalid(value);
624 log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p);
631 static int parse_env_file_push(
632 const char *filename, unsigned line,
633 const char *key, char *value,
638 va_list aq, *ap = userdata;
641 r = check_utf8ness_and_warn(filename, line, key, value);
647 while ((k = va_arg(aq, const char *))) {
650 v = va_arg(aq, char **);
672 const char *newline, ...) {
680 va_start(ap, newline);
681 r = parse_env_file_internal(NULL, fname, newline, parse_env_file_push, &ap, &n_pushed);
684 return r < 0 ? r : n_pushed;
687 #if 0 /// UNNEEDED by elogind
688 static int load_env_file_push(
689 const char *filename, unsigned line,
690 const char *key, char *value,
693 char ***m = userdata;
697 r = check_utf8ness_and_warn(filename, line, key, value);
701 p = strjoin(key, "=", value);
705 r = strv_env_replace(m, p);
718 int load_env_file(FILE *f, const char *fname, const char *newline, char ***rl) {
725 r = parse_env_file_internal(f, fname, newline, load_env_file_push, &m, NULL);
736 static int load_env_file_push_pairs(
737 const char *filename, unsigned line,
738 const char *key, char *value,
741 char ***m = userdata;
744 r = check_utf8ness_and_warn(filename, line, key, value);
748 r = strv_extend(m, key);
753 r = strv_extend(m, "");
757 r = strv_push(m, value);
768 int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ***rl) {
775 r = parse_env_file_internal(f, fname, newline, load_env_file_push_pairs, &m, NULL);
784 #if 0 /// UNNEEDED by elogind
786 static int merge_env_file_push(
787 const char *filename, unsigned line,
788 const char *key, char *value,
792 char ***env = userdata;
793 char *expanded_value;
798 log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
802 if (!env_name_is_valid(key)) {
803 log_error("%s:%u: invalid variable name \"%s\", ignoring.", strna(filename), line, key);
808 expanded_value = replace_env(value, *env,
809 REPLACE_ENV_USE_ENVIRONMENT|
810 REPLACE_ENV_ALLOW_BRACELESS|
811 REPLACE_ENV_ALLOW_EXTENDED);
815 free_and_replace(value, expanded_value);
817 return load_env_file_push(filename, line, key, value, env, n_pushed);
825 /* NOTE: this function supports braceful and braceless variable expansions,
826 * plus "extended" substitutions, unlike other exported parsing functions.
829 return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);
832 static void write_env_var(FILE *f, const char *v) {
838 fputs_unlocked(v, f);
839 fputc_unlocked('\n', f);
844 fwrite_unlocked(v, 1, p-v, f);
846 if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
847 fputc_unlocked('\"', f);
850 if (strchr(SHELL_NEED_ESCAPE, *p))
851 fputc_unlocked('\\', f);
853 fputc_unlocked(*p, f);
856 fputc_unlocked('\"', f);
858 fputs_unlocked(p, f);
860 fputc_unlocked('\n', f);
863 int write_env_file(const char *fname, char **l) {
864 _cleanup_fclose_ FILE *f = NULL;
865 _cleanup_free_ char *p = NULL;
871 r = fopen_temporary(fname, &f, &p);
875 fchmod_umask(fileno(f), 0644);
878 write_env_var(f, *i);
880 r = fflush_and_check(f);
882 if (rename(p, fname) >= 0)
892 int executable_is_script(const char *path, char **interpreter) {
894 _cleanup_free_ char *line = NULL;
900 r = read_one_line_file(path, &line);
904 if (!startswith(line, "#!"))
907 ans = strstrip(line + 2);
908 len = strcspn(ans, " \t");
913 ans = strndup(ans, len);
923 * Retrieve one field from a file like /proc/self/status. pattern
924 * should not include whitespace or the delimiter (':'). pattern matches only
925 * the beginning of a line. Whitespace before ':' is skipped. Whitespace and
926 * zeros after the ':' will be skipped. field must be freed afterwards.
927 * terminator specifies the terminating characters of the field value (not
928 * included in the value).
930 int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) {
931 _cleanup_free_ char *status = NULL;
941 r = read_full_file(filename, &status, NULL);
951 t = strstr(t, pattern);
955 /* Check that pattern occurs in beginning of line. */
956 pattern_ok = (t == status || t[-1] == '\n');
958 t += strlen(pattern);
960 } while (!pattern_ok);
962 t += strspn(t, " \t");
971 t += strspn(t, " \t");
973 /* Also skip zeros, because when this is used for
974 * capabilities, we don't want the zeros. This way the
975 * same capability set always maps to the same string,
976 * irrespective of the total capability set size. For
977 * other numbers it shouldn't matter. */
979 /* Back off one char if there's nothing but whitespace
981 if (!*t || isspace(*t))
985 len = strcspn(t, terminator);
995 DIR *xopendirat(int fd, const char *name, int flags) {
999 assert(!(flags & O_CREAT));
1001 nfd = openat(fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|flags, 0);
1014 #if 0 /// UNNEEDED by elogind
1015 static int search_and_fopen_internal(const char *path, const char *mode, const char *root, char **search, FILE **_f) {
1022 if (!path_strv_resolve_uniq(search, root))
1025 STRV_FOREACH(i, search) {
1026 _cleanup_free_ char *p = NULL;
1030 p = strjoin(root, *i, "/", path);
1032 p = strjoin(*i, "/", path);
1042 if (errno != ENOENT)
1049 int search_and_fopen(const char *path, const char *mode, const char *root, const char **search, FILE **_f) {
1050 _cleanup_strv_free_ char **copy = NULL;
1056 if (path_is_absolute(path)) {
1059 f = fopen(path, mode);
1068 copy = strv_copy((char**) search);
1072 return search_and_fopen_internal(path, mode, root, copy, _f);
1075 int search_and_fopen_nulstr(const char *path, const char *mode, const char *root, const char *search, FILE **_f) {
1076 _cleanup_strv_free_ char **s = NULL;
1078 if (path_is_absolute(path)) {
1081 f = fopen(path, mode);
1090 s = strv_split_nulstr(search);
1094 return search_and_fopen_internal(path, mode, root, s, _f);
1098 int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
1107 r = tempfn_xxxxxx(path, NULL, &t);
1111 fd = mkostemp_safe(t);
1117 f = fdopen(fd, "we");
1131 int fflush_and_check(FILE *f) {
1138 return errno > 0 ? -errno : -EIO;
1143 int fflush_sync_and_check(FILE *f) {
1148 r = fflush_and_check(f);
1152 if (fsync(fileno(f)) < 0)
1158 /* This is much like mkostemp() but is subject to umask(). */
1159 int mkostemp_safe(char *pattern) {
1160 _cleanup_umask_ mode_t u = 0;
1167 fd = mkostemp(pattern, O_CLOEXEC);
1174 int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
1186 * /foo/bar/.#<extra>waldoXXXXXX
1190 if (!filename_is_valid(fn))
1196 t = new(char, strlen(p) + 2 + strlen(extra) + 6 + 1);
1200 strcpy(stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn), "XXXXXX");
1202 *ret = path_kill_slashes(t);
1206 int tempfn_random(const char *p, const char *extra, char **ret) {
1220 * /foo/bar/.#<extra>waldobaa2a261115984a9
1224 if (!filename_is_valid(fn))
1230 t = new(char, strlen(p) + 2 + strlen(extra) + 16 + 1);
1234 x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
1237 for (i = 0; i < 16; i++) {
1238 *(x++) = hexchar(u & 0xF);
1244 *ret = path_kill_slashes(t);
1248 #if 0 /// UNNEEDED by elogind
1249 int tempfn_random_child(const char *p, const char *extra, char **ret) {
1260 * /foo/bar/waldo/.#<extra>3c2b6219aa75d7d0
1272 t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
1276 x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
1279 for (i = 0; i < 16; i++) {
1280 *(x++) = hexchar(u & 0xF);
1286 *ret = path_kill_slashes(t);
1290 int write_timestamp_file_atomic(const char *fn, usec_t n) {
1291 char ln[DECIMAL_STR_MAX(n)+2];
1293 /* Creates a "timestamp" file, that contains nothing but a
1294 * usec_t timestamp, formatted in ASCII. */
1296 if (n <= 0 || n >= USEC_INFINITY)
1299 xsprintf(ln, USEC_FMT "\n", n);
1301 return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
1304 int read_timestamp_file(const char *fn, usec_t *ret) {
1305 _cleanup_free_ char *ln = NULL;
1309 r = read_one_line_file(fn, &ln);
1313 r = safe_atou64(ln, &t);
1317 if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
1324 int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
1329 /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
1330 * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
1331 * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
1332 * element, but not before the first one. */
1342 r = fputs(separator, f);
1354 int open_tmpfile_unlinkable(const char *directory, int flags) {
1359 r = tmp_dir(&directory);
1364 /* Returns an unlinked temporary file that cannot be linked into the file system anymore */
1366 /* Try O_TMPFILE first, if it is supported */
1367 fd = open(directory, flags|O_TMPFILE|O_EXCL, S_IRUSR|S_IWUSR);
1371 /* Fall back to unguessable name + unlinking */
1372 p = strjoina(directory, "/systemd-tmp-XXXXXX");
1374 fd = mkostemp_safe(p);
1383 #if 0 /// UNNEEDED by elogind
1384 int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
1385 _cleanup_free_ char *tmp = NULL;
1391 /* Don't allow O_EXCL, as that has a special meaning for O_TMPFILE */
1392 assert((flags & O_EXCL) == 0);
1394 /* Creates a temporary file, that shall be renamed to "target" later. If possible, this uses O_TMPFILE – in
1395 * which case "ret_path" will be returned as NULL. If not possible a the tempoary path name used is returned in
1396 * "ret_path". Use link_tmpfile() below to rename the result after writing the file in full. */
1399 _cleanup_free_ char *dn = NULL;
1401 dn = dirname_malloc(target);
1405 fd = open(dn, O_TMPFILE|flags, 0640);
1411 log_debug_errno(errno, "Failed to use O_TMPFILE on %s: %m", dn);
1414 r = tempfn_random(target, NULL, &tmp);
1418 fd = open(tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, 0640);
1429 int open_serialization_fd(const char *ident) {
1432 fd = memfd_create(ident, MFD_CLOEXEC);
1436 path = getpid_cached() == 1 ? "/run/systemd" : "/tmp";
1437 fd = open_tmpfile_unlinkable(path, O_RDWR|O_CLOEXEC);
1441 log_debug("Serializing %s to %s.", ident, path);
1443 log_debug("Serializing %s to memfd.", ident);
1448 #if 0 /// UNNEEDED by elogind
1449 int link_tmpfile(int fd, const char *path, const char *target) {
1454 /* Moves a temporary file created with open_tmpfile() above into its final place. if "path" is NULL an fd
1455 * created with O_TMPFILE is assumed, and linkat() is used. Otherwise it is assumed O_TMPFILE is not supported
1456 * on the directory, and renameat2() is used instead.
1458 * Note that in both cases we will not replace existing files. This is because linkat() does not support this
1459 * operation currently (renameat2() does), and there is no nice way to emulate this. */
1462 if (rename_noreplace(AT_FDCWD, path, AT_FDCWD, target) < 0)
1465 char proc_fd_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1467 xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
1469 if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
1476 int read_nul_string(FILE *f, char **ret) {
1477 _cleanup_free_ char *x = NULL;
1478 size_t allocated = 0, n = 0;
1483 /* Reads a NUL-terminated string from the specified file. */
1488 if (!GREEDY_REALLOC(x, allocated, n+2))
1492 if (c == 0) /* Terminate at NUL byte */
1497 break; /* Terminate at EOF */
1517 int mkdtemp_malloc(const char *template, char **ret) {
1523 p = strdup(template);
1537 int read_line(FILE *f, size_t limit, char **ret) {
1538 _cleanup_free_ char *buffer = NULL;
1539 size_t n = 0, allocated = 0, count = 0;
1544 /* Something like a bounded version of getline().
1546 * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
1549 * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
1550 * the number of characters in the returned string). When EOF is hit, 0 is returned.
1552 * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
1553 * delimiters. If the limit is hit we fail and return -ENOBUFS.
1555 * If a line shall be skipped ret may be initialized as NULL. */
1558 if (!GREEDY_REALLOC(buffer, allocated, 1))
1573 c = fgetc_unlocked(f);
1575 /* if we read an error, and have no data to return, then propagate the error */
1576 if (ferror_unlocked(f) && n == 0) {
1577 r = errno > 0 ? -errno : -EIO;
1587 if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
1591 if (!GREEDY_REALLOC(buffer, allocated, n + 2)) {
1596 buffer[n] = (char) c;