fileio: add read_stripped_line() as trivial read_line() + strstrip() combo

This commit is contained in:
Lennart Poettering
2023-10-17 14:36:54 +02:00
parent 74b2c22fd7
commit c56cb33f09
2 changed files with 32 additions and 0 deletions
+30
View File
@@ -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;
+2
View File
@@ -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);