fileio: add read_stripped_line() as trivial read_line() + strstrip() combo
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user