[systemd-devel] [PATCH 2/3] use strneq instead of strncmp

Thomas Hindoe Paaboel Andersen phomes at gmail.com
Tue Feb 12 12:47:36 PST 2013


Signed-off-by: Thomas Hindoe Paaboel Andersen <phomes at gmail.com>
---
 src/core/load-fragment.c        |  8 ++++----
 src/delta/delta.c               | 12 ++++++------
 src/libudev/libudev-enumerate.c |  2 +-
 src/locale/localed.c            |  2 +-
 src/login/logind-inhibit.c      | 14 +++++++-------
 src/login/sd-login.c            |  2 +-
 src/shared/cgroup-util.c        |  2 +-
 src/shared/env-util.c           | 10 +++++-----
 src/shared/socket-util.c        |  2 +-
 src/shared/util.c               |  2 +-
 src/stdio-bridge/stdio-bridge.c |  6 +++---
 src/udev/scsi_id/scsi_id.c      |  8 ++++----
 src/udev/scsi_id/scsi_serial.c  |  2 +-
 src/udev/udev-builtin-net_id.c  |  2 +-
 src/udev/udev-rules.c           |  2 +-
 15 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 8436d4f..32d544b 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -458,7 +458,7 @@ int config_parse_exec(
 
                 k = 0;
                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
-                        if (strncmp(w, ";", MAX(l, 1U)) == 0)
+                        if (strneq(w, ";", MAX(l, 1U)))
                                 break;
 
                         k++;
@@ -470,9 +470,9 @@ int config_parse_exec(
 
                 k = 0;
                 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
-                        if (strncmp(w, ";", MAX(l, 1U)) == 0)
+                        if (strneq(w, ";", MAX(l, 1U)))
                                 break;
-                        else if (strncmp(w, "\\;", MAX(l, 1U)) == 0)
+                        else if (strneq(w, "\\;", MAX(l, 1U)))
                                 w ++;
 
                         if (honour_argv0 && w == rvalue) {
@@ -2619,7 +2619,7 @@ void unit_dump_config_items(FILE *f) {
                 prefix_len = dot-i;
 
                 if (dot)
-                        if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
+                        if (!prev || !strneq(prev, i, prefix_len+1)) {
                                 if (prev)
                                         fputc('\n', f);
 
diff --git a/src/delta/delta.c b/src/delta/delta.c
index 9f20938..16b1002 100644
--- a/src/delta/delta.c
+++ b/src/delta/delta.c
@@ -312,17 +312,17 @@ static int parse_flags(const char *flag_str, int flags) {
         size_t l;
 
         FOREACH_WORD(w, l, flag_str, state) {
-                if (strncmp("masked", w, l) == 0)
+                if (strneq("masked", w, l))
                         flags |= SHOW_MASKED;
-                else if (strncmp ("equivalent", w, l) == 0)
+                else if (strneq ("equivalent", w, l))
                         flags |= SHOW_EQUIVALENT;
-                else if (strncmp("redirected", w, l) == 0)
+                else if (strneq("redirected", w, l))
                         flags |= SHOW_REDIRECTED;
-                else if (strncmp("overridden", w, l) == 0)
+                else if (strneq("overridden", w, l))
                         flags |= SHOW_OVERRIDDEN;
-                else if (strncmp("unchanged", w, l) == 0)
+                else if (strneq("unchanged", w, l))
                         flags |= SHOW_UNCHANGED;
-                else if (strncmp("default", w, l) == 0)
+                else if (strneq("default", w, l))
                         flags |= SHOW_DEFAULTS;
                 else
                         return -EINVAL;
diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c
index 2c1f7ab..5ccaabd 100644
--- a/src/libudev/libudev-enumerate.c
+++ b/src/libudev/libudev-enumerate.c
@@ -309,7 +309,7 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
                         }
 
                         if (move_later &&
-                            strncmp(entry->syspath, move_later->syspath, move_later_prefix) != 0) {
+                             !strneq(entry->syspath, move_later->syspath, move_later_prefix)) {
 
                                 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
                                 move_later = NULL;
diff --git a/src/locale/localed.c b/src/locale/localed.c
index 6b1a793..fedcdfb 100644
--- a/src/locale/localed.c
+++ b/src/locale/localed.c
@@ -854,7 +854,7 @@ static int convert_x11_to_vconsole(DBusConnection *connection) {
                                  * layout stripped off. */
                                 if (x > 0 &&
                                     strlen(a[1]) == x &&
-                                    strncmp(state.x11_layout, a[1], x) == 0)
+                                    strneq(state.x11_layout, a[1], x))
                                         matching = 5;
                                 else  {
                                         size_t w;
diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c
index f1b9cca..2c1a412 100644
--- a/src/login/logind-inhibit.c
+++ b/src/login/logind-inhibit.c
@@ -439,19 +439,19 @@ InhibitWhat inhibit_what_from_string(const char *s) {
         size_t l;
 
         FOREACH_WORD_SEPARATOR(w, l, s, ":", state) {
-                if (l == 8 && strncmp(w, "shutdown", l) == 0)
+                if (l == 8 && strneq(w, "shutdown", l))
                         what |= INHIBIT_SHUTDOWN;
-                else if (l == 5 && strncmp(w, "sleep", l) == 0)
+                else if (l == 5 && strneq(w, "sleep", l))
                         what |= INHIBIT_SLEEP;
-                else if (l == 4 && strncmp(w, "idle", l) == 0)
+                else if (l == 4 && strneq(w, "idle", l))
                         what |= INHIBIT_IDLE;
-                else if (l == 16 && strncmp(w, "handle-power-key", l) == 0)
+                else if (l == 16 && strneq(w, "handle-power-key", l))
                         what |= INHIBIT_HANDLE_POWER_KEY;
-                else if (l == 18 && strncmp(w, "handle-suspend-key", l) == 0)
+                else if (l == 18 && strneq(w, "handle-suspend-key", l))
                         what |= INHIBIT_HANDLE_SUSPEND_KEY;
-                else if (l == 20 && strncmp(w, "handle-hibernate-key", l) == 0)
+                else if (l == 20 && strneq(w, "handle-hibernate-key", l))
                         what |= INHIBIT_HANDLE_HIBERNATE_KEY;
-                else if (l == 17 && strncmp(w, "handle-lid-switch", l) == 0)
+                else if (l == 17 && strneq(w, "handle-lid-switch", l))
                         what |= INHIBIT_HANDLE_LID_SWITCH;
                 else
                         return _INHIBIT_WHAT_INVALID;
diff --git a/src/login/sd-login.c b/src/login/sd-login.c
index 4bc51e7..b81dddf 100644
--- a/src/login/sd-login.c
+++ b/src/login/sd-login.c
@@ -196,7 +196,7 @@ _public_ int sd_uid_is_on_seat(uid_t uid, int require_active, const char *seat)
         }
 
         FOREACH_WORD(w, l, s, state) {
-                if (strncmp(t, w, l) == 0) {
+                if (strneq(t, w, l)) {
                         free(s);
                         free(t);
 
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index acace52..7efbc2e 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -811,7 +811,7 @@ int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
                         continue;
 
                 l++;
-                if (strncmp(l, controller, cs) != 0)
+                if (!strneq(l, controller, cs))
                         continue;
 
                 if (l[cs] != ':')
diff --git a/src/shared/env-util.c b/src/shared/env-util.c
index 7a213a7..8bb25c2 100644
--- a/src/shared/env-util.c
+++ b/src/shared/env-util.c
@@ -128,7 +128,7 @@ bool strv_env_is_valid(char **e) {
                 /* Check if there are duplicate assginments */
                 k = strcspn(*p, "=");
                 STRV_FOREACH(q, p + 1)
-                        if (strncmp(*p, *q, k) == 0 && (*q)[k] == '=')
+                        if (strneq(*p, *q, k) && (*q)[k] == '=')
                                 return false;
         }
 
@@ -156,7 +156,7 @@ static int env_append(char **r, char ***k, char **a) {
                         n++;
 
                 for (j = r; j < *k; j++)
-                        if (strncmp(*j, *a, n) == 0)
+                        if (strneq(*j, *a, n))
                                 break;
 
                 if (j >= *k)
@@ -232,7 +232,7 @@ static bool env_match(const char *t, const char *pattern) {
         if (!strchr(pattern, '=')) {
                 size_t l = strlen(pattern);
 
-                return strncmp(t, pattern, l) == 0 && t[l] == '=';
+                return strneq(t, pattern, l) && t[l] == '=';
         }
 
         return false;
@@ -348,7 +348,7 @@ char *strv_env_get_n(char **l, const char *name, size_t k) {
                 return NULL;
 
         STRV_FOREACH(i, l)
-                if (strncmp(*i, name, k) == 0 &&
+                if (strneq(*i, name, k) &&
                     (*i)[k] == '=')
                         return *i + k + 1;
 
@@ -376,7 +376,7 @@ char **strv_env_clean(char **e) {
 
                 n = strcspn(*p, "=");
                 STRV_FOREACH(q, p + 1)
-                        if (strncmp(*p, *q, n) == 0 && (*q)[n] == '=') {
+                        if (strneq(*p, *q, n) && (*q)[n] == '=') {
                                 duplicate = true;
                                 break;
                         }
diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c
index 56ec99f..39b6142 100644
--- a/src/shared/socket-util.c
+++ b/src/shared/socket-util.c
@@ -432,7 +432,7 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
                         return false;
 
                 if (a->sockaddr.un.sun_path[0]) {
-                        if (strncmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)) != 0)
+                        if (!strneq(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, sizeof(a->sockaddr.un.sun_path)))
                                 return false;
                 } else {
                         if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
diff --git a/src/shared/util.c b/src/shared/util.c
index 29cb9f1..1b5538c 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -720,7 +720,7 @@ int parse_env_file(
                                 value = va_arg(ap, char **);
 
                                 n = strlen(key);
-                                if (strncmp(p, key, n) != 0 ||
+                                if (!strneq(p, key, n) ||
                                     p[n] != '=')
                                         continue;
 
diff --git a/src/stdio-bridge/stdio-bridge.c b/src/stdio-bridge/stdio-bridge.c
index f926fe5..adc692f 100644
--- a/src/stdio-bridge/stdio-bridge.c
+++ b/src/stdio-bridge/stdio-bridge.c
@@ -69,16 +69,16 @@ static size_t patch_in_line(char *line, size_t l, size_t left) {
         } else
                 r = 0;
 
-        if (l == 5 && strncmp(line, "BEGIN", 5) == 0) {
+        if (l == 5 && strneq(line, "BEGIN", 5)) {
                 r += l;
                 auth_over = true;
 
-        } else if (l == 17 && strncmp(line, "NEGOTIATE_UNIX_FD", 17) == 0) {
+        } else if (l == 17 && strneq(line, "NEGOTIATE_UNIX_FD", 17)) {
                 memmove(line + 13, line + 17, left);
                 memcpy(line, "NEGOTIATE_NOP", 13);
                 r += 13;
 
-        } else if (l >= 14 && strncmp(line, "AUTH EXTERNAL ", 14) == 0) {
+        } else if (l >= 14 && strneq(line, "AUTH EXTERNAL ", 14)) {
                 char uid[20*2 + 1];
                 size_t len;
 
diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c
index 20bf492..7765d22 100644
--- a/src/udev/scsi_id/scsi_id.c
+++ b/src/udev/scsi_id/scsi_id.c
@@ -267,10 +267,10 @@ static int get_file_options(struct udev *udev,
                 if (vendor == NULL) {
                         if (vendor_in == NULL)
                                 break;
-                } else if ((vendor_in && strncmp(vendor, vendor_in,
-                                                 strlen(vendor_in)) == 0) &&
-                           (!model_in || (strncmp(model, model_in,
-                                                  strlen(model_in)) == 0))) {
+                } else if ((vendor_in && strneq(vendor, vendor_in,
+                                                 strlen(vendor_in))) &&
+                           (!model_in || (strneq(model, model_in,
+                                                  strlen(model_in))))) {
                                 /*
                                  * Matched vendor and optionally model.
                                  *
diff --git a/src/udev/scsi_id/scsi_serial.c b/src/udev/scsi_id/scsi_serial.c
index 950b8ad..d522a23 100644
--- a/src/udev/scsi_id/scsi_serial.c
+++ b/src/udev/scsi_id/scsi_serial.c
@@ -436,7 +436,7 @@ static int do_scsi_page0_inquiry(struct udev *udev,
                  * If the vendor id appears in the page assume the page is
                  * invalid.
                  */
-                if (!strncmp((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
+                if (strneq((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
                         log_debug("%s: invalid page0 data\n", dev_scsi->kernel);
                         return 1;
                 }
diff --git a/src/udev/udev-builtin-net_id.c b/src/udev/udev-builtin-net_id.c
index 85540ac..57674bf 100644
--- a/src/udev/udev-builtin-net_id.c
+++ b/src/udev/udev-builtin-net_id.c
@@ -223,7 +223,7 @@ static int dev_pci_slot(struct udev_device *dev, struct netnames *names) {
                 snprintf(str, sizeof(str), "%s/%s/address", slots, dent->d_name);
                 if (read_one_line_file(str, &address) >= 0) {
                         /* match slot address with device by stripping the function */
-                        if (strncmp(address, udev_device_get_sysname(names->pcidev), strlen(address)) == 0)
+                        if (strneq(address, udev_device_get_sysname(names->pcidev), strlen(address)))
                                 hotplug_slot = i;
                         free(address);
                 }
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index a86f8c3..d2810a0 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -1737,7 +1737,7 @@ static int match_key(struct udev_rules *rules, struct token *token, const char *
                                 if (next != NULL) {
                                         size_t matchlen = (size_t)(next - s);
 
-                                        match = (matchlen == len && strncmp(s, val, matchlen) == 0);
+                                        match = (matchlen == len && strneq(s, val, matchlen));
                                         if (match)
                                                 break;
                                 } else {
-- 
1.8.1.2



More information about the systemd-devel mailing list