From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Mon, 8 Jan 2024 21:02:39 +0000 (-0500) Subject: tree-wide: replace string functions with fundamental functions X-Git-Tag: v256-rc1~1197 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aa9ff6c28d682887ad6f19bda4aab3485897db78;p=thirdparty%2Fsystemd.git tree-wide: replace string functions with fundamental functions --- diff --git a/src/basic/env-util.c b/src/basic/env-util.c index 22eb3308e48..38387cb2ff3 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -244,9 +244,9 @@ static bool env_match(const char *t, const char *pattern) { return true; if (!strchr(pattern, '=')) { - size_t l = strlen(pattern); + t = startswith(t, pattern); - return strneq(t, pattern, l) && t[l] == '='; + return t && *t == '='; } return false; diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 0f6cace426d..d75d25af997 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1024,7 +1024,7 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) { _cleanup_fclose_ FILE *f = NULL; char *value = NULL; const char *path; - size_t l, sum = 0; + size_t sum = 0; int r; assert(pid >= 0); @@ -1059,9 +1059,9 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) { if (r < 0) return r; - l = strlen(field); for (;;) { _cleanup_free_ char *line = NULL; + const char *match; if (sum > ENVIRONMENT_BLOCK_MAX) /* Give up searching eventually */ return -ENOBUFS; @@ -1074,8 +1074,9 @@ int getenv_for_pid(pid_t pid, const char *field, char **ret) { sum += r; - if (strneq(line, field, l) && line[l] == '=') { - value = strdup(line + l + 1); + match = startswith(line, field); + if (match && *match == '=') { + value = strdup(match + 1); if (!value) return -ENOMEM; diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 38ca04b8597..2aac588118c 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -1509,24 +1509,20 @@ ssize_t strlevenshtein(const char *x, const char *y) { } char *strrstr(const char *haystack, const char *needle) { - const char *f = NULL; - size_t l; - /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */ if (!haystack || !needle) return NULL; - l = strlen(needle); - /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the * last char, not before. */ - if (l == 0) + if (*needle == 0) return strchr(haystack, 0); - for (const char *p = haystack; *p; p++) - if (strneq(p, needle, l)) - f = p; - - return (char*) f; + for (const char *p = strstr(haystack, needle), *q; p; p = q) { + q = strstr(p + 1, needle); + if (!q) + return (char *) p; + } + return NULL; } diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 86b626595e7..46a367cbd7a 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -640,7 +640,7 @@ int bus_start_running(sd_bus *bus) { static int parse_address_key(const char **p, const char *key, char **value) { _cleanup_free_ char *r = NULL; - size_t l, n = 0; + size_t n = 0; const char *a; assert(p); @@ -648,17 +648,14 @@ static int parse_address_key(const char **p, const char *key, char **value) { assert(value); if (key) { - l = strlen(key); - if (strncmp(*p, key, l) != 0) - return 0; - - if ((*p)[l] != '=') + a = startswith(*p, key); + if (!a || *a != '=') return 0; if (*value) return -EINVAL; - a = *p + l + 1; + a++; } else a = *p; @@ -4121,13 +4118,13 @@ _public_ int sd_bus_path_decode_many(const char *path, const char *path_template for (template_pos = path_template; *template_pos; ) { const char *sep; - size_t length; + size_t length, path_length; char *label; /* verify everything until the next '%' matches verbatim */ sep = strchrnul(template_pos, '%'); length = sep - template_pos; - if (strncmp(path_pos, template_pos, length)) + if (!strneq(path_pos, template_pos, length)) return 0; path_pos += length; @@ -4148,8 +4145,8 @@ _public_ int sd_bus_path_decode_many(const char *path, const char *path_template /* verify the suffixes match */ sep = strchrnul(path_pos, '/'); - if (sep - path_pos < (ssize_t)length || - strncmp(sep - length, template_pos, length)) + path_length = sep - path_pos; + if (length > path_length || !strneq(sep - length, template_pos, length)) return 0; template_pos += length; /* skip over matched label */ diff --git a/src/shared/efi-api.c b/src/shared/efi-api.c index 4cd1091e9ac..eb84e16dae4 100644 --- a/src/shared/efi-api.c +++ b/src/shared/efi-api.c @@ -453,13 +453,13 @@ int efi_get_boot_options(uint16_t **ret_options) { FOREACH_DIRENT(de, dir, return -errno) { int id; - if (strncmp(de->d_name, "Boot", 4) != 0) + if (!startswith(de->d_name, "Boot")) continue; if (strlen(de->d_name) != 45) continue; - if (strcmp(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR("")) != 0) /* generate variable suffix using macro */ + if (!streq(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR(""))) /* generate variable suffix using macro */ continue; id = boot_id_hex(de->d_name + 4); diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index ef3dae5b624..bfb0f05c4fe 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -232,23 +232,21 @@ int fstab_filter_options( } NULSTR_FOREACH(name, names) { - if (end < word + strlen(name)) - continue; - if (!strneq(word, name, strlen(name))) + x = startswith(word, name); + if (!x || x > end) continue; /* We know that the string is NUL terminated, so *x is valid */ - x = word + strlen(name); if (IN_SET(*x, '\0', '=', ',')) { namefound = name; break; } } - if (*end) - word = end + 1; - else + if (*end == '\0') break; + + word = end + 1; } answer: diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index c4057d63c35..dc0630596b9 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -186,7 +186,7 @@ static sd_device *handle_scsi_sas(sd_device *parent, char **path) { return NULL; /* Check if we are simple disk */ - if (strncmp(phy_count, "1", 2) != 0) + if (!streq(phy_count, "1")) return handle_scsi_sas_wide_port(parent, path); /* Get connected phy */