]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: replace string functions with fundamental functions
authorRose <83477269+AtariDreams@users.noreply.github.com>
Mon, 8 Jan 2024 21:02:39 +0000 (16:02 -0500)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 11 Jan 2024 04:36:25 +0000 (13:36 +0900)
src/basic/env-util.c
src/basic/process-util.c
src/basic/string-util.c
src/libsystemd/sd-bus/sd-bus.c
src/shared/efi-api.c
src/shared/fstab-util.c
src/udev/udev-builtin-path_id.c

index 22eb3308e48c9cd438f531e45acd1b4b7f3c00ee..38387cb2ff35fb2b5880a9ad1a1eeb86b301ae7f 100644 (file)
@@ -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;
index 0f6cace426d4626ce579cf7ed8f3b8bfff28fec1..d75d25af9975416896c0048dd0c1b7df14b5f8f0 100644 (file)
@@ -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;
 
index 38ca04b8597b873fd4456215027db17ad998b739..2aac588118c1902d19aa68d69bf79c2917655e16 100644 (file)
@@ -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;
 }
index 86b626595e71f386ad219256580e5dfedc6b2e9f..46a367cbd7a1b3e97cccda6f74438eaa7856463b 100644 (file)
@@ -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 */
index 4cd1091e9ac37a0c816760a47e6d0be10daeb7bd..eb84e16dae4db1566578bfc09636bd5c81d72e56 100644 (file)
@@ -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);
index ef3dae5b6242fe1b01957846c0823d0f9f6ebea5..bfb0f05c4fe4424db2c1de97424e4d32f9c78d42 100644 (file)
@@ -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:
index c4057d63c35dfedf14044147b2c34756ad8a1367..dc0630596b9eed98e9e98b965634eaa0e7601ba5 100644 (file)
@@ -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 */