X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Ffileio.c;h=df432eaee4283f9c182fa01301e48cffc869fb13;hp=42de50dcf628b0c3473ab940f90f33a786b96dea;hb=c60d32aa37fc0b10451b0d392d90b3b96eb88ebc;hpb=33863e9c1572a093818e4559e69ca02452b60d5c diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 42de50dcf..df432eaee 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -1361,3 +1361,44 @@ int link_tmpfile(int fd, const char *path, const char *target) { return 0; } #endif // 0 + +int read_nul_string(FILE *f, char **ret) { + _cleanup_free_ char *x = NULL; + size_t allocated = 0, n = 0; + + assert(f); + assert(ret); + + /* Reads a NUL-terminated string from the specified file. */ + + for (;;) { + int c; + + if (!GREEDY_REALLOC(x, allocated, n+2)) + return -ENOMEM; + + c = fgetc(f); + if (c == 0) /* Terminate at NUL byte */ + break; + if (c == EOF) { + if (ferror(f)) + return -errno; + break; /* Terminate at EOF */ + } + + x[n++] = (char) c; + } + + if (x) + x[n] = 0; + else { + x = new0(char, 1); + if (!x) + return -ENOMEM; + } + + *ret = x; + x = NULL; + + return 0; +}