From 0ff6ff2b29dfb02a803515fb0160d9963d2389d5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 16 Oct 2023 18:01:00 +0200 Subject: [PATCH] tree-wide: port various parsers over to read_stripped_line() --- src/basic/process-util.c | 8 ++--- src/binfmt/binfmt.c | 10 +++---- src/cgtop/cgtop.c | 7 ++--- src/core/manager-serialize.c | 8 ++--- src/core/unit-serialize.c | 8 ++--- src/cryptsetup/cryptsetup-generator.c | 9 +++--- src/getty-generator/getty-generator.c | 8 ++--- src/integritysetup/integritysetup-generator.c | 13 +++----- src/libsystemd/sd-path/sd-path.c | 16 ++++------ src/locale/localectl.c | 23 +++++++------- src/locale/localed-util.c | 30 ++++++++----------- src/modules-load/modules-load.c | 10 +++---- src/network/networkd-dhcp-server.c | 8 ++--- src/resolve/resolvconf-compat.c | 15 +++++----- src/resolve/resolved-dns-trust-anchor.c | 10 +++---- src/resolve/resolved-resolv-conf.c | 14 ++++----- src/shared/bootspec.c | 14 ++++----- src/shared/hostname-setup.c | 24 +++++---------- src/shared/install.c | 14 ++++----- src/shared/serialize.c | 8 ++--- src/sysctl/sysctl.c | 11 ++++--- src/sysusers/sysusers.c | 8 ++--- src/sysv-generator/sysv-generator.c | 18 +++++------ src/timedate/timedated.c | 10 +++---- src/tmpfiles/tmpfiles.c | 8 ++--- src/veritysetup/veritysetup-generator.c | 9 +++--- 26 files changed, 127 insertions(+), 194 deletions(-) diff --git a/src/basic/process-util.c b/src/basic/process-util.c index ed096900ac5..6e515280654 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -473,16 +473,14 @@ static int get_process_id(pid_t pid, const char *field, uid_t *ret) { _cleanup_free_ char *line = NULL; char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; - l = strstrip(line); - - if (startswith(l, field)) { - l += strlen(field); + l = startswith(line, field); + if (l) { l += strspn(l, WHITESPACE); l[strcspn(l, WHITESPACE)] = 0; diff --git a/src/binfmt/binfmt.c b/src/binfmt/binfmt.c index 675ae185e24..817d593c239 100644 --- a/src/binfmt/binfmt.c +++ b/src/binfmt/binfmt.c @@ -85,22 +85,20 @@ static int apply_file(const char *filename, bool ignore_enoent) { log_debug("Applying %s%s", pp, special_glyph(SPECIAL_GLYPH_ELLIPSIS)); for (unsigned line = 1;; line++) { _cleanup_free_ char *text = NULL; - char *p; int k; - k = read_line(f, LONG_LINE_MAX, &text); + k = read_stripped_line(f, LONG_LINE_MAX, &text); if (k < 0) return log_error_errno(k, "Failed to read file '%s': %m", pp); if (k == 0) break; - p = strstrip(text); - if (isempty(p)) + if (isempty(text)) continue; - if (strchr(COMMENTS, p[0])) + if (strchr(COMMENTS, text[0])) continue; - RET_GATHER(r, apply_rule(filename, line, p)); + RET_GATHER(r, apply_rule(filename, line, text)); } return r; diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 2e517a16362..a6adb8ce314 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -298,15 +298,14 @@ static int process( uint64_t k, *q; char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; - /* Trim and skip the device */ - l = strstrip(line); - l += strcspn(l, WHITESPACE); + /* Skip the device */ + l = line + strcspn(line, WHITESPACE); l += strspn(l, WHITESPACE); if (all_unified) { diff --git a/src/core/manager-serialize.c b/src/core/manager-serialize.c index 04393cf11da..09c49b6d6ca 100644 --- a/src/core/manager-serialize.c +++ b/src/core/manager-serialize.c @@ -221,21 +221,19 @@ static int manager_deserialize_one_unit(Manager *m, const char *name, FILE *f, F } static int manager_deserialize_units(Manager *m, FILE *f, FDSet *fds) { - const char *unit_name; int r; for (;;) { _cleanup_free_ char *line = NULL; + /* Start marker */ - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read serialization line: %m"); if (r == 0) break; - unit_name = strstrip(line); - - r = manager_deserialize_one_unit(m, unit_name, f, fds); + r = manager_deserialize_one_unit(m, line, f, fds); if (r == -ENOMEM) return r; if (r < 0) { diff --git a/src/core/unit-serialize.c b/src/core/unit-serialize.c index cd7f2ee86b6..be041010312 100644 --- a/src/core/unit-serialize.c +++ b/src/core/unit-serialize.c @@ -554,24 +554,22 @@ int unit_deserialize_state(Unit *u, FILE *f, FDSet *fds) { int unit_deserialize_state_skip(FILE *f) { int r; + assert(f); /* Skip serialized data for this unit. We don't know what it is. */ for (;;) { _cleanup_free_ char *line = NULL; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read serialization line: %m"); if (r == 0) return 0; - l = strstrip(line); - /* End marker */ - if (isempty(l)) + if (isempty(line)) return 1; } } diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 2caf15fa497..904e4cd3faf 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -809,10 +809,10 @@ static int add_crypttab_devices(void) { _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL, *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL; crypto_device *d = NULL; - char *l, *uuid; + char *uuid; int k; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read %s: %m", arg_crypttab); if (r == 0) @@ -820,11 +820,10 @@ static int add_crypttab_devices(void) { crypttab_line++; - l = strstrip(line); - if (IN_SET(l[0], 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); + k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options); if (k < 2 || k > 4) { log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line); continue; diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index 4379b4b6480..7486118365f 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -168,9 +168,8 @@ static int add_credential_gettys(void) { for (;;) { _cleanup_free_ char *tty = NULL; - char *s; - r = read_line(f, PATH_MAX, &tty); + r = read_stripped_line(f, PATH_MAX, &tty); if (r == 0) break; if (r < 0) { @@ -178,11 +177,10 @@ static int add_credential_gettys(void) { break; } - s = strstrip(tty); - if (startswith(s, "#")) + if (startswith(tty, "#")) continue; - r = t->func(s); + r = t->func(tty); if (r < 0) return r; } diff --git a/src/integritysetup/integritysetup-generator.c b/src/integritysetup/integritysetup-generator.c index 05154908aea..72b890575ce 100644 --- a/src/integritysetup/integritysetup-generator.c +++ b/src/integritysetup/integritysetup-generator.c @@ -132,9 +132,8 @@ static int add_integritytab_devices(void) { for (;;) { _cleanup_free_ char *line = NULL, *name = NULL, *device_id = NULL, *device_path = NULL, *key_file = NULL, *options = NULL; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read %s: %m", arg_integritytab); if (r == 0) @@ -142,17 +141,13 @@ static int add_integritytab_devices(void) { integritytab_line++; - l = strstrip(line); - if (!l) - continue; - - if (IN_SET(l[0], 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; /* The key file and the options are optional */ - r = sscanf(l, "%ms %ms %ms %ms", &name, &device_id, &key_file, &options); + r = sscanf(line, "%ms %ms %ms %ms", &name, &device_id, &key_file, &options); if (!IN_SET(r, 2, 3, 4)) { - log_error("Failed to parse %s:%u, ignoring.", l, integritytab_line); + log_error("Failed to parse %s:%u, ignoring.", arg_integritytab, integritytab_line); continue; } diff --git a/src/libsystemd/sd-path/sd-path.c b/src/libsystemd/sd-path/sd-path.c index 33adc314d6c..7290d1c40a0 100644 --- a/src/libsystemd/sd-path/sd-path.c +++ b/src/libsystemd/sd-path/sd-path.c @@ -70,7 +70,6 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) { _cleanup_free_ char *b = NULL; _cleanup_free_ const char *fn = NULL; const char *c = NULL; - size_t n; int r; assert(field); @@ -93,26 +92,21 @@ static int from_user_dir(const char *field, char **buffer, const char **ret) { return -errno; } - /* This is an awful parse, but it follows closely what - * xdg-user-dirs does upstream */ - - n = strlen(field); + /* This is an awful parse, but it follows closely what xdg-user-dirs does upstream */ for (;;) { _cleanup_free_ char *line = NULL; - char *l, *p, *e; + char *p, *e; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; - l = strstrip(line); - - if (!strneq(l, field, n)) + p = startswith(line, field); + if (!p) continue; - p = l + n; p += strspn(p, WHITESPACE); if (*p != '=') diff --git a/src/locale/localectl.c b/src/locale/localectl.c index d8db9d9d22a..32354027f17 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -314,27 +314,25 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) { for (;;) { _cleanup_free_ char *line = NULL; - char *l, *w; + char *w; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read keyboard mapping list: %m"); if (r == 0) break; - l = strstrip(line); - - if (isempty(l)) + if (isempty(line)) continue; - if (l[0] == '!') { - if (startswith(l, "! model")) + if (line[0] == '!') { + if (startswith(line, "! model")) state = MODELS; - else if (startswith(l, "! layout")) + else if (startswith(line, "! layout")) state = LAYOUTS; - else if (startswith(l, "! variant")) + else if (startswith(line, "! variant")) state = VARIANTS; - else if (startswith(l, "! option")) + else if (startswith(line, "! option")) state = OPTIONS; else state = NONE; @@ -345,7 +343,7 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) { if (state != look_for) continue; - w = l + strcspn(l, WHITESPACE); + w = line + strcspn(line, WHITESPACE); if (argc > 1) { char *e; @@ -368,8 +366,7 @@ static int list_x11_keymaps(int argc, char **argv, void *userdata) { } else *w = 0; - r = strv_extend(&list, l); - if (r < 0) + if (strv_consume(&list, TAKE_PTR(line)) < 0) return log_oom(); } diff --git a/src/locale/localed-util.c b/src/locale/localed-util.c index 9b6949e14dc..d78b878cf95 100644 --- a/src/locale/localed-util.c +++ b/src/locale/localed-util.c @@ -434,22 +434,20 @@ int x11_read_data(Context *c, sd_bus_message *m) { for (;;) { _cleanup_free_ char *line = NULL; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; - l = strstrip(line); - if (IN_SET(l[0], 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - if (in_section && first_word(l, "Option")) { + if (in_section && first_word(line, "Option")) { _cleanup_strv_free_ char **a = NULL; - r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); + r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE); if (r < 0) return r; @@ -469,17 +467,17 @@ int x11_read_data(Context *c, sd_bus_message *m) { free_and_replace(*p, a[2]); } - } else if (!in_section && first_word(l, "Section")) { + } else if (!in_section && first_word(line, "Section")) { _cleanup_strv_free_ char **a = NULL; - r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE); + r = strv_split_full(&a, line, WHITESPACE, EXTRACT_UNQUOTE); if (r < 0) return -ENOMEM; if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; - } else if (in_section && first_word(l, "EndSection")) + } else if (in_section && first_word(line, "EndSection")) in_section = false; } @@ -618,10 +616,9 @@ static int read_next_mapping( _cleanup_strv_free_ char **b = NULL; _cleanup_free_ char *line = NULL; size_t length; - const char *l; int r; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) @@ -629,11 +626,10 @@ static int read_next_mapping( (*n)++; - l = strstrip(line); - if (IN_SET(l[0], 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE); + r = strv_split_full(&b, line, WHITESPACE, EXTRACT_UNQUOTE); if (r < 0) return r; @@ -1008,16 +1004,14 @@ static int locale_gen_locale_supported(const char *locale_entry) { for (;;) { _cleanup_free_ char *line = NULL; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_debug_errno(r, "Failed to read /usr/share/i18n/SUPPORTED: %m"); if (r == 0) return 0; - l = strstrip(line); - if (strcaseeq_ptr(l, locale_entry)) + if (strcaseeq_ptr(line, locale_entry)) return 1; } } diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index 4adeef3afbd..efca2379eac 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -79,22 +79,20 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent log_debug("apply: %s", pp); for (;;) { _cleanup_free_ char *line = NULL; - char *l; int k; - k = read_line(f, LONG_LINE_MAX, &line); + k = read_stripped_line(f, LONG_LINE_MAX, &line); if (k < 0) return log_error_errno(k, "Failed to read file '%s': %m", pp); if (k == 0) break; - l = strstrip(line); - if (isempty(l)) + if (isempty(line)) continue; - if (strchr(COMMENTS, *l)) + if (strchr(COMMENTS, *line)) continue; - k = module_load_and_warn(ctx, l, true); + k = module_load_and_warn(ctx, line, true); if (k == -ENOENT) continue; RET_GATHER(r, k); diff --git a/src/network/networkd-dhcp-server.c b/src/network/networkd-dhcp-server.c index 569398571c0..17939ce54ac 100644 --- a/src/network/networkd-dhcp-server.c +++ b/src/network/networkd-dhcp-server.c @@ -325,19 +325,17 @@ static int dhcp4_server_set_dns_from_resolve_conf(Link *link) { for (;;) { _cleanup_free_ char *line = NULL; const char *a; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read " PRIVATE_UPLINK_RESOLV_CONF ": %m"); if (r == 0) break; - l = strstrip(line); - if (IN_SET(*l, '#', ';', 0)) + if (IN_SET(*line, '#', ';', 0)) continue; - a = first_word(l, "nameserver"); + a = first_word(line, "nameserver"); if (!a) continue; diff --git a/src/resolve/resolvconf-compat.c b/src/resolve/resolvconf-compat.c index 3804d3dde3b..bef95c0bb64 100644 --- a/src/resolve/resolvconf-compat.c +++ b/src/resolve/resolvconf-compat.c @@ -226,9 +226,9 @@ int resolvconf_parse_argv(int argc, char *argv[]) { for (;;) { _cleanup_free_ char *line = NULL; - const char *a, *l; + const char *a; - r = read_line(stdin, LONG_LINE_MAX, &line); + r = read_stripped_line(stdin, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read from stdin: %m"); if (r == 0) @@ -236,25 +236,24 @@ int resolvconf_parse_argv(int argc, char *argv[]) { n++; - l = strstrip(line); - if (IN_SET(*l, '#', ';', 0)) + if (IN_SET(*line, '#', ';', 0)) continue; - a = first_word(l, "nameserver"); + a = first_word(line, "nameserver"); if (a) { (void) parse_nameserver(a); continue; } - a = first_word(l, "domain"); + a = first_word(line, "domain"); if (!a) - a = first_word(l, "search"); + a = first_word(line, "search"); if (a) { (void) parse_search_domain(a); continue; } - log_syntax(NULL, LOG_DEBUG, "stdin", n, 0, "Ignoring resolv.conf line: %s", l); + log_syntax(NULL, LOG_DEBUG, "stdin", n, 0, "Ignoring resolv.conf line: %s", line); } if (type == TYPE_EXCLUSIVE) { diff --git a/src/resolve/resolved-dns-trust-anchor.c b/src/resolve/resolved-dns-trust-anchor.c index d626638c010..1703c43d4b0 100644 --- a/src/resolve/resolved-dns-trust-anchor.c +++ b/src/resolve/resolved-dns-trust-anchor.c @@ -441,9 +441,8 @@ static int dns_trust_anchor_load_files( for (;;) { _cleanup_free_ char *line = NULL; - char *l; - r = read_line(g, LONG_LINE_MAX, &line); + r = read_stripped_line(g, LONG_LINE_MAX, &line); if (r < 0) { log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f); break; @@ -453,14 +452,13 @@ static int dns_trust_anchor_load_files( n++; - l = strstrip(line); - if (isempty(l)) + if (isempty(line)) continue; - if (*l == ';') + if (*line == ';') continue; - (void) loader(d, *f, n, l); + (void) loader(d, *f, n, line); } } diff --git a/src/resolve/resolved-resolv-conf.c b/src/resolve/resolved-resolv-conf.c index 1da0ac89955..2071e0810cc 100644 --- a/src/resolve/resolved-resolv-conf.c +++ b/src/resolve/resolved-resolv-conf.c @@ -122,9 +122,8 @@ int manager_read_resolv_conf(Manager *m) { for (;;) { _cleanup_free_ char *line = NULL; const char *a; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) { log_error_errno(r, "Failed to read /etc/resolv.conf: %m"); goto clear; @@ -134,11 +133,10 @@ int manager_read_resolv_conf(Manager *m) { n++; - l = strstrip(line); - if (IN_SET(*l, '#', ';', 0)) + if (IN_SET(*line, '#', ';', 0)) continue; - a = first_word(l, "nameserver"); + a = first_word(line, "nameserver"); if (a) { r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_SYSTEM, a); if (r < 0) @@ -147,9 +145,9 @@ int manager_read_resolv_conf(Manager *m) { continue; } - a = first_word(l, "domain"); + a = first_word(line, "domain"); if (!a) /* We treat "domain" lines, and "search" lines as equivalent, and add both to our list. */ - a = first_word(l, "search"); + a = first_word(line, "search"); if (a) { r = manager_parse_search_domains_and_warn(m, a); if (r < 0) @@ -158,7 +156,7 @@ int manager_read_resolv_conf(Manager *m) { continue; } - log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", l); + log_syntax(NULL, LOG_DEBUG, "/etc/resolv.conf", n, 0, "Ignoring resolv.conf line: %s", line); } m->resolv_conf_stat = st; diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index f347ccdd8a8..746ae743cd2 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -325,9 +325,8 @@ static int boot_entry_load_type1( for (;;) { _cleanup_free_ char *buf = NULL, *field = NULL; - const char *p; - r = read_line(f, LONG_LINE_MAX, &buf); + r = read_stripped_line(f, LONG_LINE_MAX, &buf); if (r == 0) break; if (r == -ENOBUFS) @@ -337,10 +336,10 @@ static int boot_entry_load_type1( line++; - p = strstrip(buf); - if (IN_SET(p[0], '#', '\0')) + if (IN_SET(buf[0], '#', '\0')) continue; + const char *p = buf; r = extract_first_word(&p, &field, NULL, 0); if (r < 0) { log_syntax(NULL, LOG_WARNING, tmp.path, line, r, "Failed to parse, ignoring line: %m"); @@ -450,9 +449,8 @@ int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) { for (;;) { _cleanup_free_ char *buf = NULL, *field = NULL; - const char *p; - r = read_line(file, LONG_LINE_MAX, &buf); + r = read_stripped_line(file, LONG_LINE_MAX, &buf); if (r == 0) break; if (r == -ENOBUFS) @@ -462,10 +460,10 @@ int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) { line++; - p = strstrip(buf); - if (IN_SET(p[0], '#', '\0')) + if (IN_SET(buf[0], '#', '\0')) continue; + const char *p = buf; r = extract_first_word(&p, &field, NULL, 0); if (r < 0) { log_syntax(NULL, LOG_WARNING, path, line, r, "Failed to parse, ignoring line: %m"); diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index a3b2a913b1e..137c29aef5c 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -79,32 +79,24 @@ int read_etc_hostname_stream(FILE *f, char **ret) { for (;;) { _cleanup_free_ char *line = NULL; - char *p; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) /* EOF without any hostname? the file is empty, let's treat that exactly like no file at all: ENOENT */ return -ENOENT; - p = strstrip(line); - /* File may have empty lines or comments, ignore them */ - if (!IN_SET(*p, '\0', '#')) { - char *copy; + if (IN_SET(line[0], '\0', '#')) + continue; - hostname_cleanup(p); /* normalize the hostname */ + hostname_cleanup(line); /* normalize the hostname */ - if (!hostname_is_valid(p, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */ - return -EBADMSG; + if (!hostname_is_valid(line, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */ + return -EBADMSG; - copy = strdup(p); - if (!copy) - return -ENOMEM; - - *ret = copy; - return 0; - } + *ret = TAKE_PTR(line); + return 0; } } diff --git a/src/shared/install.c b/src/shared/install.c index 8b0705eb9ef..0f4dab4aa27 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -3246,23 +3246,21 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset _cleanup_free_ char *line = NULL; _cleanup_(unit_file_preset_rule_done) UnitFilePresetRule rule = {}; const char *parameter; - char *l; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return r; if (r == 0) break; - l = strstrip(line); n++; - if (isempty(l)) + if (isempty(line)) continue; - if (strchr(COMMENTS, *l)) + if (strchr(COMMENTS, line[0])) continue; - parameter = first_word(l, "enable"); + parameter = first_word(line, "enable"); if (parameter) { char *unit_name; char **instances = NULL; @@ -3281,7 +3279,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset }; } - parameter = first_word(l, "disable"); + parameter = first_word(line, "disable"); if (parameter) { char *pattern; @@ -3295,7 +3293,7 @@ static int read_presets(RuntimeScope scope, const char *root_dir, UnitFilePreset }; } - parameter = first_word(l, "ignore"); + parameter = first_word(line, "ignore"); if (parameter) { char *pattern; diff --git a/src/shared/serialize.c b/src/shared/serialize.c index edb232e2eef..ee35055aeae 100644 --- a/src/shared/serialize.c +++ b/src/shared/serialize.c @@ -268,7 +268,7 @@ int deserialize_read_line(FILE *f, char **ret) { assert(f); assert(ret); - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read serialization line: %m"); if (r == 0) { /* eof */ @@ -276,15 +276,11 @@ int deserialize_read_line(FILE *f, char **ret) { return 0; } - const char *l = strstrip(line); - if (isempty(l)) { /* End marker */ + if (isempty(line)) { /* End marker */ *ret = NULL; return 0; } - if (free_and_strdup(&line, l) < 0) - return log_oom(); - *ret = TAKE_PTR(line); return 1; } diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index ca41f02fdd5..3e1f79887db 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -238,10 +238,10 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig _cleanup_free_ char *l = NULL; bool ignore_failure = false; Option *existing; - char *p, *value; + char *value; int k; - k = read_line(f, LONG_LINE_MAX, &l); + k = read_stripped_line(f, LONG_LINE_MAX, &l); if (k == 0) break; if (k < 0) @@ -249,13 +249,12 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig c++; - p = strstrip(l); - - if (isempty(p)) + if (isempty(l)) continue; - if (strchr(COMMENTS "\n", *p)) + if (strchr(COMMENTS, l[0])) continue; + char *p = l; value = strchr(p, '='); if (value) { if (p[0] == '-') { diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 0fcbf261c11..c0deae11655 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -2005,10 +2005,9 @@ static int read_config_file(Context *c, const char *fn, bool ignore_enoent) { for (;;) { _cleanup_free_ char *line = NULL; - char *l; int k; - k = read_line(f, LONG_LINE_MAX, &line); + k = read_stripped_line(f, LONG_LINE_MAX, &line); if (k < 0) return log_error_errno(k, "Failed to read '%s': %m", fn); if (k == 0) @@ -2016,11 +2015,10 @@ static int read_config_file(Context *c, const char *fn, bool ignore_enoent) { v++; - l = strstrip(line); - if (IN_SET(*l, 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - k = parse_line(c, fn, v, l); + k = parse_line(c, fn, v, line); if (k < 0 && r == 0) r = k; } diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 30f82d57369..4485e2e3680 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -450,9 +450,8 @@ static int load_sysv(SysvStub *s) { for (;;) { _cleanup_free_ char *l = NULL; - char *t; - r = read_line(f, LONG_LINE_MAX, &l); + r = read_stripped_line(f, LONG_LINE_MAX, &l); if (r < 0) return log_error_errno(r, "Failed to read configuration file '%s': %m", s->path); if (r == 0) @@ -460,17 +459,16 @@ static int load_sysv(SysvStub *s) { line++; - t = strstrip(l); - if (*t != '#') { + if (l[0] != '#') { /* Try to figure out whether this init script supports * the reload operation. This heuristic looks for * "Usage" lines which include the reload option. */ if (state == USAGE_CONTINUATION || - (state == NORMAL && strcasestr(t, "usage"))) { - if (usage_contains_reload(t)) { + (state == NORMAL && strcasestr(l, "usage"))) { + if (usage_contains_reload(l)) { supports_reload = true; state = NORMAL; - } else if (t[strlen(t)-1] == '\\') + } else if (endswith(l, "\\")) state = USAGE_CONTINUATION; else state = NORMAL; @@ -479,18 +477,18 @@ static int load_sysv(SysvStub *s) { continue; } - if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { + if (state == NORMAL && streq(l, "### BEGIN INIT INFO")) { state = LSB; s->has_lsb = true; continue; } - if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(t, "### END INIT INFO")) { + if (IN_SET(state, LSB_DESCRIPTION, LSB) && streq(l, "### END INIT INFO")) { state = NORMAL; continue; } - t++; + char *t = l + 1; t += strspn(t, WHITESPACE); if (state == NORMAL) { diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 6406d6fefd5..c7be30f563f 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -211,9 +211,8 @@ static int context_parse_ntp_services_from_disk(Context *c) { for (;;) { _cleanup_free_ char *line = NULL; - const char *word; - r = read_line(file, LINE_MAX, &line); + r = read_stripped_line(file, LINE_MAX, &line); if (r < 0) { log_error_errno(r, "Failed to read %s, ignoring: %m", *f); continue; @@ -221,13 +220,12 @@ static int context_parse_ntp_services_from_disk(Context *c) { if (r == 0) break; - word = strstrip(line); - if (isempty(word) || startswith(word, "#")) + if (isempty(line) || startswith(line, "#")) continue; - r = context_add_ntp_service(c, word, *f); + r = context_add_ntp_service(c, line, *f); if (r < 0) - log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", word); + log_warning_errno(r, "Failed to add NTP service \"%s\", ignoring: %m", line); } } diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index aab65f49d12..93ac53792f5 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -4203,10 +4203,9 @@ static int read_config_file( for (;;) { _cleanup_free_ char *line = NULL; bool invalid_line = false; - char *l; int k; - k = read_line(f, LONG_LINE_MAX, &line); + k = read_stripped_line(f, LONG_LINE_MAX, &line); if (k < 0) return log_error_errno(k, "Failed to read '%s': %m", fn); if (k == 0) @@ -4214,11 +4213,10 @@ static int read_config_file( v++; - l = strstrip(line); - if (IN_SET(*l, 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - k = parse_line(c, fn, v, l, &invalid_line, &uid_cache, &gid_cache); + k = parse_line(c, fn, v, line, &invalid_line, &uid_cache, &gid_cache); if (k < 0) { if (invalid_line) /* Allow reporting with a special code if the caller requested this */ diff --git a/src/veritysetup/veritysetup-generator.c b/src/veritysetup/veritysetup-generator.c index 59ff4cd7a52..d55d4aab0b5 100644 --- a/src/veritysetup/veritysetup-generator.c +++ b/src/veritysetup/veritysetup-generator.c @@ -453,9 +453,9 @@ static int add_veritytab_devices(void) { for (;;) { _cleanup_free_ char *line = NULL, *name = NULL, *data_device = NULL, *hash_device = NULL, *roothash = NULL, *options = NULL; - char *l, *data_uuid, *hash_uuid; + char *data_uuid, *hash_uuid; - r = read_line(f, LONG_LINE_MAX, &line); + r = read_stripped_line(f, LONG_LINE_MAX, &line); if (r < 0) return log_error_errno(r, "Failed to read %s: %m", arg_veritytab); if (r == 0) @@ -463,11 +463,10 @@ static int add_veritytab_devices(void) { veritytab_line++; - l = strstrip(line); - if (IN_SET(l[0], 0, '#')) + if (IN_SET(line[0], 0, '#')) continue; - r = sscanf(l, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options); + r = sscanf(line, "%ms %ms %ms %ms %ms", &name, &data_device, &hash_device, &roothash, &options); if (!IN_SET(r, 4, 5)) { log_error("Failed to parse %s:%u, ignoring.", arg_veritytab, veritytab_line); continue;