From: Lennart Poettering Date: Mon, 16 Oct 2023 15:55:07 +0000 (+0200) Subject: fileio: add read_stripped_line() as trivial read_line() + strstrip() combo X-Git-Tag: v255-rc1~218^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c56cb33f099b59c9f1cea06d499b6a06dcd7dc49;p=thirdparty%2Fsystemd.git fileio: add read_stripped_line() as trivial read_line() + strstrip() combo --- diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 527f53daee8..6129461e660 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -1488,6 +1488,36 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) { return (int) count; } +int read_stripped_line(FILE *f, size_t limit, char **ret) { + _cleanup_free_ char *s = NULL; + int r; + + assert(f); + + r = read_line(f, limit, ret ? &s : NULL); + if (r < 0) + return r; + + if (ret) { + const char *p; + + p = strstrip(s); + if (p == s) + *ret = TAKE_PTR(s); + else { + char *copy; + + copy = strdup(p); + if (!copy) + return -ENOMEM; + + *ret = copy; + } + } + + return r; +} + int safe_fgetc(FILE *f, char *ret) { int k; diff --git a/src/basic/fileio.h b/src/basic/fileio.h index cd18bcc1177..e0e0a45b2b5 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -166,6 +166,8 @@ static inline int read_nul_string(FILE *f, size_t limit, char **ret) { return read_line_full(f, limit, READ_LINE_ONLY_NUL, ret); } +int read_stripped_line(FILE *f, size_t limit, char **ret); + int safe_fgetc(FILE *f, char *ret); int warn_file_is_world_accessible(const char *filename, struct stat *st, const char *unit, unsigned line);