From 33502ffe2eb7b56cdd018a4fb6830d7828519fad Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 16 Apr 2012 20:27:44 +0200 Subject: [PATCH 1/1] udev: use startswith() and streq() --- Makefile.am | 8 ++ src/libudev/libudev-device-private.c | 2 +- src/libudev/libudev-device.c | 80 +++++++++--------- src/libudev/libudev-enumerate.c | 7 +- src/libudev/libudev-private.h | 1 + src/libudev/libudev-util.c | 8 +- src/test/test-libudev.c | 3 +- src/udev/keymap/keymap.c | 3 +- src/udev/udev-builtin-blkid.c | 20 ++--- src/udev/udev-builtin-hwdb.c | 2 +- src/udev/udev-builtin-path_id.c | 8 +- src/udev/udev-event.c | 2 +- src/udev/udev-rules.c | 122 +++++++++++++-------------- src/udev/udev.h | 1 + src/udev/udevadm-info.c | 4 +- src/udev/udevadm-test-builtin.c | 2 +- src/udev/udevadm-test.c | 2 +- src/udev/udevadm-trigger.c | 2 +- 18 files changed, 143 insertions(+), 134 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7df3915b7..421d27c88 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1302,6 +1302,9 @@ libudev_la_LDFLAGS = \ $(AM_LDFLAGS) \ -version-info $(LIBUDEV_CURRENT):$(LIBUDEV_REVISION):$(LIBUDEV_AGE) +libudev_la_LIBADD = \ + libsystemd-shared.la + pkgconfiglib_DATA += \ src/libudev/libudev.pc @@ -1348,6 +1351,7 @@ libudev_private_la_CFLAGS = \ -fvisibility=default libudev_private_la_LIBADD = \ + libsystemd-shared.la \ $(SELINUX_LIBS) # ------------------------------------------------------------------------------ @@ -1498,6 +1502,7 @@ test_libudev_SOURCES = \ src/test/test-libudev.c test_libudev_LDADD = \ + libsystemd-shared.la \ libudev.la test_udev_SOURCES = \ @@ -1796,6 +1801,9 @@ keymap_SOURCES = \ keymap_CPPFLAGS = \ $(AM_CPPFLAGS) -I src/udev/keymap +keymap_LDADD = \ + libsystemd-shared.la + nodist_keymap_SOURCES = \ src/udev/keymap/keys-from-name.h \ src/udev/keymap/keys-to-name.h diff --git a/src/libudev/libudev-device-private.c b/src/libudev/libudev-device-private.c index c58cde2ff..a4dfa9bd3 100644 --- a/src/libudev/libudev-device-private.c +++ b/src/libudev/libudev-device-private.c @@ -60,7 +60,7 @@ int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) { const char *tag = udev_list_entry_get_name(list_entry_current); - if (strcmp(tag, tag_old) == 0) { + if (streq(tag, tag_old)) { found = true; break; } diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index 212163bb9..6a2b1d039 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -276,7 +276,7 @@ _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device) return udev_device->subsystem; } /* implicit names */ - if (strncmp(udev_device->devpath, "/module/", 8) == 0) { + if (startswith(udev_device->devpath, "/module/")) { udev_device_set_subsystem(udev_device, "module"); return udev_device->subsystem; } @@ -284,9 +284,9 @@ _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device) udev_device_set_subsystem(udev_device, "drivers"); return udev_device->subsystem; } - if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 || - strncmp(udev_device->devpath, "/class/", 7) == 0 || - strncmp(udev_device->devpath, "/bus/", 5) == 0) { + if (startswith(udev_device->devpath, "/subsystem/") || + startswith(udev_device->devpath, "/class/") || + startswith(udev_device->devpath, "/bus/")) { udev_device_set_subsystem(udev_device, "subsystem"); return udev_device->subsystem; } @@ -353,18 +353,18 @@ static struct udev_list_entry *udev_device_add_property_from_string(struct udev_ */ void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property) { - if (strncmp(property, "DEVPATH=", 8) == 0) { + if (startswith(property, "DEVPATH=")) { char path[UTIL_PATH_SIZE]; util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys", &property[8], NULL); udev_device_set_syspath(udev_device, path); - } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) { + } else if (startswith(property, "SUBSYSTEM=")) { udev_device_set_subsystem(udev_device, &property[10]); - } else if (strncmp(property, "DEVTYPE=", 8) == 0) { + } else if (startswith(property, "DEVTYPE=")) { udev_device_set_devtype(udev_device, &property[8]); - } else if (strncmp(property, "DEVNAME=", 8) == 0) { + } else if (startswith(property, "DEVNAME=")) { udev_device_set_devnode(udev_device, &property[8]); - } else if (strncmp(property, "DEVLINKS=", 9) == 0) { + } else if (startswith(property, "DEVLINKS=")) { char devlinks[UTIL_PATH_SIZE]; char *slink; char *next; @@ -380,7 +380,7 @@ void udev_device_add_property_from_string_parse(struct udev_device *udev_device, } if (slink[0] != '\0') udev_device_add_devlink(udev_device, slink, 0); - } else if (strncmp(property, "TAGS=", 5) == 0) { + } else if (startswith(property, "TAGS=")) { char tags[UTIL_PATH_SIZE]; char *next; @@ -400,23 +400,23 @@ void udev_device_add_property_from_string_parse(struct udev_device *udev_device, udev_device_add_tag(udev_device, tag); } } - } else if (strncmp(property, "USEC_INITIALIZED=", 19) == 0) { + } else if (startswith(property, "USEC_INITIALIZED=")) { udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10)); - } else if (strncmp(property, "DRIVER=", 7) == 0) { + } else if (startswith(property, "DRIVER=")) { udev_device_set_driver(udev_device, &property[7]); - } else if (strncmp(property, "ACTION=", 7) == 0) { + } else if (startswith(property, "ACTION=")) { udev_device_set_action(udev_device, &property[7]); - } else if (strncmp(property, "MAJOR=", 6) == 0) { + } else if (startswith(property, "MAJOR=")) { udev_device->maj = strtoull(&property[6], NULL, 10); - } else if (strncmp(property, "MINOR=", 6) == 0) { + } else if (startswith(property, "MINOR=")) { udev_device->min = strtoull(&property[6], NULL, 10); - } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) { + } else if (startswith(property, "DEVPATH_OLD=")) { udev_device_set_devpath_old(udev_device, &property[12]); - } else if (strncmp(property, "SEQNUM=", 7) == 0) { + } else if (startswith(property, "SEQNUM=")) { udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10)); - } else if (strncmp(property, "IFINDEX=", 8) == 0) { + } else if (startswith(property, "IFINDEX=")) { udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10)); - } else if (strncmp(property, "DEVMODE=", 8) == 0) { + } else if (startswith(property, "DEVMODE=")) { udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8)); } else { udev_device_add_property_from_string(udev_device, property); @@ -548,24 +548,24 @@ int udev_device_read_uevent_file(struct udev_device *udev_device) continue; pos[0] = '\0'; - if (strncmp(line, "DEVTYPE=", 8) == 0) { + if (startswith(line, "DEVTYPE=")) { udev_device_set_devtype(udev_device, &line[8]); continue; } - if (strncmp(line, "IFINDEX=", 8) == 0) { + if (startswith(line, "IFINDEX=")) { udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10)); continue; } - if (strncmp(line, "DEVNAME=", 8) == 0) { + if (startswith(line, "DEVNAME=")) { udev_device_set_devnode(udev_device, &line[8]); continue; } - if (strncmp(line, "MAJOR=", 6) == 0) + if (startswith(line, "MAJOR=")) maj = strtoull(&line[6], NULL, 10); - else if (strncmp(line, "MINOR=", 6) == 0) + else if (startswith(line, "MINOR=")) min = strtoull(&line[6], NULL, 10); - else if (strncmp(line, "DEVMODE=", 8) == 0) + else if (startswith(line, "DEVMODE=")) udev_device->devnode_mode = strtoul(&line[8], NULL, 8); udev_device_add_property_from_string(udev_device, line); @@ -636,7 +636,7 @@ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, con return NULL; /* path starts in sys */ - if (strncmp(syspath, TEST_PREFIX "/sys", strlen(TEST_PREFIX "/sys")) != 0) { + if (!startswith(syspath, TEST_PREFIX "/sys")) { dbg(udev, "not in sys :%s\n", syspath); return NULL; } @@ -651,7 +651,7 @@ _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, con util_strscpy(path, sizeof(path), syspath); util_resolve_sys_link(udev, path, sizeof(path)); - if (strncmp(path + strlen(TEST_PREFIX "/sys"), "/devices/", 9) == 0) { + if (startswith(path + strlen(TEST_PREFIX "/sys"), "/devices/")) { char file[UTIL_PATH_SIZE]; /* all "devices" require a "uevent" file */ @@ -783,7 +783,7 @@ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev char path[UTIL_PATH_SIZE]; struct stat statbuf; - if (strcmp(subsystem, "subsystem") == 0) { + if (streq(subsystem, "subsystem")) { util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/subsystem/", sysname, NULL); if (stat(path, &statbuf) == 0) goto found; @@ -798,14 +798,14 @@ _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev goto out; } - if (strcmp(subsystem, "module") == 0) { + if (streq(subsystem, "module")) { util_strscpyl(path, sizeof(path), TEST_PREFIX "/sys/module/", sysname, NULL); if (stat(path, &statbuf) == 0) goto found; goto out; } - if (strcmp(subsystem, "drivers") == 0) { + if (streq(subsystem, "drivers")) { char subsys[UTIL_NAME_SIZE]; char *driver; @@ -966,11 +966,11 @@ _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struc const char *parent_devtype; parent_subsystem = udev_device_get_subsystem(parent); - if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) { + if (parent_subsystem != NULL && streq(parent_subsystem, subsystem)) { if (devtype == NULL) break; parent_devtype = udev_device_get_devtype(parent); - if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0) + if (parent_devtype != NULL && streq(parent_devtype, devtype)) break; } parent = udev_device_get_parent(parent); @@ -1237,7 +1237,7 @@ _public_ const char *udev_device_get_action(struct udev_device *udev_device) **/ _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device) { - unsigned long long now; + unsigned long long now_ts; if (udev_device == NULL) return 0; @@ -1245,10 +1245,10 @@ _public_ unsigned long long int udev_device_get_usec_since_initialized(struct ud udev_device_read_db(udev_device, NULL); if (udev_device->usec_initialized == 0) return 0; - now = now_usec(); - if (now == 0) + now_ts = now_usec(); + if (now_ts == 0) return 0; - return now - udev_device->usec_initialized; + return now_ts - udev_device->usec_initialized; } unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device) @@ -1309,9 +1309,9 @@ _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_devi * Some core links return only the last element of the target path, * these are just values, the paths should not be exposed. */ - if (strcmp(sysattr, "driver") == 0 || - strcmp(sysattr, "subsystem") == 0 || - strcmp(sysattr, "module") == 0) { + if (streq(sysattr, "driver") || + streq(sysattr, "subsystem") || + streq(sysattr, "module")) { if (util_get_sys_core_link_value(udev_device->udev, sysattr, udev_device->syspath, value, sizeof(value)) < 0) return NULL; @@ -1497,7 +1497,7 @@ const char *udev_device_get_id_filename(struct udev_device *udev_device) if (major(udev_device_get_devnum(udev_device)) > 0) { /* use dev_t -- b259:131072, c254:0 */ if (asprintf(&udev_device->id_filename, "%c%u:%u", - strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c', + streq(udev_device_get_subsystem(udev_device), "block") ? 'b' : 'c', major(udev_device_get_devnum(udev_device)), minor(udev_device_get_devnum(udev_device))) < 0) udev_device->id_filename = NULL; diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c index ed4f62c66..4725912c3 100644 --- a/src/libudev/libudev-enumerate.c +++ b/src/libudev/libudev-enumerate.c @@ -232,7 +232,7 @@ static size_t devices_delay_later(struct udev *udev, const char *syspath) c += 11; c += strcspn(c, "/"); - if (strncmp(c, "/controlC", 9) == 0) + if (startswith(c, "/controlC")) return c - syspath + 1; } @@ -595,13 +595,10 @@ static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device static bool match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *dev) { - const char *parent; - if (udev_enumerate->parent_match == NULL) return true; - parent = udev_device_get_devpath(udev_enumerate->parent_match); - return strncmp(parent, udev_device_get_devpath(dev), strlen(parent)) == 0; + return startswith(udev_device_get_devpath(dev), udev_device_get_devpath(udev_enumerate->parent_match)); } static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname) diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h index 986c40c91..60bffa469 100644 --- a/src/libudev/libudev-private.h +++ b/src/libudev/libudev-private.h @@ -17,6 +17,7 @@ #include #include #include "macro.h" +#include "util.h" #include "libudev.h" #define READ_END 0 diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c index cb17c3f48..24d402cd2 100644 --- a/src/libudev/libudev-util.c +++ b/src/libudev/libudev-util.c @@ -62,7 +62,7 @@ int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size) return -1; link_target[len] = '\0'; - for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++) + for (back = 0; startswith(&link_target[back * 3], "../"); back++) ; for (i = 0; i <= back; i++) { base = strrchr(syspath, '/'); @@ -84,11 +84,11 @@ int util_log_priority(const char *priority) prio = strtol(priority, &endptr, 10); if (endptr[0] == '\0' || isspace(endptr[0])) return prio; - if (strncmp(priority, "err", 3) == 0) + if (startswith(priority, "err")) return LOG_ERR; - if (strncmp(priority, "info", 4) == 0) + if (startswith(priority, "info")) return LOG_INFO; - if (strncmp(priority, "debug", 5) == 0) + if (startswith(priority, "debug")) return LOG_DEBUG; return 0; } diff --git a/src/test/test-libudev.c b/src/test/test-libudev.c index f5c7ae2c3..481ce65db 100644 --- a/src/test/test-libudev.c +++ b/src/test/test-libudev.c @@ -21,6 +21,7 @@ #include #include "libudev.h" +#include "util.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) @@ -474,7 +475,7 @@ int main(int argc, char *argv[]) } /* add sys path if needed */ - if (strncmp(syspath, "/sys", strlen("/sys")) != 0) { + if (!startswith(syspath, "/sys")) { snprintf(path, sizeof(path), "/sys/%s", syspath); syspath = path; } diff --git a/src/udev/keymap/keymap.c b/src/udev/keymap/keymap.c index cc37a9b8c..a2e43f92c 100644 --- a/src/udev/keymap/keymap.c +++ b/src/udev/keymap/keymap.c @@ -40,6 +40,7 @@ const struct key* lookup_key (const char *str, unsigned int len); #include "keys-from-name.h" #include "keys-to-name.h" +#include "util.h" #define MAX_SCANCODES 1024 @@ -48,7 +49,7 @@ static int evdev_open(const char *dev) int fd; char fn[PATH_MAX]; - if (strncmp(dev, "/dev", 4) != 0) { + if (!startswith(dev, "/dev")) { snprintf(fn, sizeof(fn), "/dev/%s", dev); dev = fn; } diff --git a/src/udev/udev-builtin-blkid.c b/src/udev/udev-builtin-blkid.c index fdc68d00a..39f286a74 100644 --- a/src/udev/udev-builtin-blkid.c +++ b/src/udev/udev-builtin-blkid.c @@ -37,45 +37,45 @@ static void print_property(struct udev_device *dev, bool test, const char *name, s[0] = '\0'; - if (!strcmp(name, "TYPE")) { + if (streq(name, "TYPE")) { udev_builtin_add_property(dev, test, "ID_FS_TYPE", value); - } else if (!strcmp(name, "USAGE")) { + } else if (streq(name, "USAGE")) { udev_builtin_add_property(dev, test, "ID_FS_USAGE", value); - } else if (!strcmp(name, "VERSION")) { + } else if (streq(name, "VERSION")) { udev_builtin_add_property(dev, test, "ID_FS_VERSION", value); - } else if (!strcmp(name, "UUID")) { + } else if (streq(name, "UUID")) { blkid_safe_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_UUID", s); blkid_encode_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s); - } else if (!strcmp(name, "UUID_SUB")) { + } else if (streq(name, "UUID_SUB")) { blkid_safe_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s); blkid_encode_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s); - } else if (!strcmp(name, "LABEL")) { + } else if (streq(name, "LABEL")) { blkid_safe_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_LABEL", s); blkid_encode_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s); - } else if (!strcmp(name, "PTTYPE")) { + } else if (streq(name, "PTTYPE")) { udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value); - } else if (!strcmp(name, "PART_ENTRY_NAME")) { + } else if (streq(name, "PART_ENTRY_NAME")) { blkid_encode_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_PART_ENTRY_NAME", s); - } else if (!strcmp(name, "PART_ENTRY_TYPE")) { + } else if (streq(name, "PART_ENTRY_TYPE")) { blkid_encode_string(value, s, sizeof(s)); udev_builtin_add_property(dev, test, "ID_PART_ENTRY_TYPE", s); - } else if (!strncmp(name, "PART_ENTRY_", 11)) { + } else if (startswith(name, "PART_ENTRY_")) { util_strscpyl(s, sizeof(s), "ID_", name, NULL); udev_builtin_add_property(dev, test, s, value); } diff --git a/src/udev/udev-builtin-hwdb.c b/src/udev/udev-builtin-hwdb.c index aa996f375..0db21641b 100644 --- a/src/udev/udev-builtin-hwdb.c +++ b/src/udev/udev-builtin-hwdb.c @@ -40,7 +40,7 @@ static int get_id_attr( return -1; } - if (!strncmp(t, "0x", 2)) + if (startswith(t, "0x")) t += 2; if (sscanf(t, "%04x", &u) != 1 || u > 0xFFFFU) { diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index a8559d2dd..09abefc03 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -176,7 +176,7 @@ static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char ** transportdev = udev_device_get_parent(transportdev); if (transportdev == NULL) return NULL; - if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0) + if (startswith(udev_device_get_sysname(transportdev), "session")) break; } @@ -260,7 +260,7 @@ static struct udev_device *handle_scsi_default(struct udev_device *parent, char continue; if (dent->d_type != DT_DIR && dent->d_type != DT_LNK) continue; - if (strncmp(dent->d_name, "host", 4) != 0) + if (!startswith(dent->d_name, "host")) continue; i = strtoul(&dent->d_name[4], &rest, 10); if (rest[0] != '\0') @@ -349,9 +349,9 @@ static void handle_scsi_tape(struct udev_device *dev, char **path) return; name = udev_device_get_sysname(dev); - if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL) + if (startswith(name, "nst") && strchr("lma", name[3]) != NULL) path_prepend(path, "nst%c", name[3]); - else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL) + else if (startswith(name, "st") && strchr("lma", name[2]) != NULL) path_prepend(path, "st%c", name[2]); } diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index d330062eb..e6f405b74 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -133,7 +133,7 @@ size_t udev_event_apply_format(struct udev_event *event, const char *src, char * } for (i = 0; i < ELEMENTSOF(map); i++) { - if (strncmp(&from[1], map[i].name, strlen(map[i].name)) == 0) { + if (startswith(&from[1], map[i].name)) { type = map[i].type; from += strlen(map[i].name)+1; goto subst; diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 1ec817872..a800ccdc9 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -597,7 +597,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) /* lookup, if we know it already */ for (i = 0; i < rules->uids_cur; i++) { off = rules->uids[i].name_off; - if (strcmp(&rules->buf[off], owner) == 0) { + if (streq(&rules->buf[off], owner)) { uid = rules->uids[i].uid; return uid; } @@ -638,7 +638,7 @@ static gid_t add_gid(struct udev_rules *rules, const char *group) /* lookup, if we know it already */ for (i = 0; i < rules->gids_cur; i++) { off = rules->gids[i].name_off; - if (strcmp(&rules->buf[off], group) == 0) { + if (streq(&rules->buf[off], group)) { gid = rules->gids[i].gid; return gid; } @@ -726,7 +726,7 @@ static int import_property_from_string(struct udev_device *dev, char *line) } /* handle device, renamed by external tool, returning new path */ - if (strcmp(key, "DEVPATH") == 0) { + if (streq(key, "DEVPATH")) { char syspath[UTIL_PATH_SIZE]; log_debug("updating devpath from '%s' to '%s'\n", @@ -1103,7 +1103,7 @@ static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, } else if (has_split) { glob = GL_SPLIT; } else if (has_glob) { - if (strcmp(value, "?*") == 0) + if (streq(value, "?*")) glob = GL_SOMETHING; else glob = GL_GLOB; @@ -1200,7 +1200,7 @@ static int add_rule(struct udev_rules *rules, char *line, if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) break; - if (strcmp(key, "ACTION") == 0) { + if (streq(key, "ACTION")) { if (op > OP_MATCH_MAX) { log_error("invalid ACTION operation\n"); goto invalid; @@ -1209,7 +1209,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "DEVPATH") == 0) { + if (streq(key, "DEVPATH")) { if (op > OP_MATCH_MAX) { log_error("invalid DEVPATH operation\n"); goto invalid; @@ -1218,7 +1218,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "KERNEL") == 0) { + if (streq(key, "KERNEL")) { if (op > OP_MATCH_MAX) { log_error("invalid KERNEL operation\n"); goto invalid; @@ -1227,16 +1227,16 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "SUBSYSTEM") == 0) { + if (streq(key, "SUBSYSTEM")) { if (op > OP_MATCH_MAX) { log_error("invalid SUBSYSTEM operation\n"); goto invalid; } /* bus, class, subsystem events should all be the same */ - if (strcmp(value, "subsystem") == 0 || - strcmp(value, "bus") == 0 || - strcmp(value, "class") == 0) { - if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0) + if (streq(value, "subsystem") || + streq(value, "bus") || + streq(value, "class")) { + if (streq(value, "bus") || streq(value, "class")) log_error("'%s' must be specified as 'subsystem' \n" "please fix it in %s:%u", value, filename, lineno); rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL); @@ -1245,7 +1245,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "DRIVER") == 0) { + if (streq(key, "DRIVER")) { if (op > OP_MATCH_MAX) { log_error("invalid DRIVER operation\n"); goto invalid; @@ -1254,7 +1254,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) { + if (startswith(key, "ATTR{")) { attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1); if (attr == NULL) { log_error("error parsing ATTR attribute\n"); @@ -1268,7 +1268,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "KERNELS") == 0) { + if (streq(key, "KERNELS")) { if (op > OP_MATCH_MAX) { log_error("invalid KERNELS operation\n"); goto invalid; @@ -1277,7 +1277,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "SUBSYSTEMS") == 0) { + if (streq(key, "SUBSYSTEMS")) { if (op > OP_MATCH_MAX) { log_error("invalid SUBSYSTEMS operation\n"); goto invalid; @@ -1286,7 +1286,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "DRIVERS") == 0) { + if (streq(key, "DRIVERS")) { if (op > OP_MATCH_MAX) { log_error("invalid DRIVERS operation\n"); goto invalid; @@ -1295,7 +1295,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0) { + if (startswith(key, "ATTRS{")) { if (op > OP_MATCH_MAX) { log_error("invalid ATTRS operation\n"); goto invalid; @@ -1305,7 +1305,7 @@ static int add_rule(struct udev_rules *rules, char *line, log_error("error parsing ATTRS attribute\n"); goto invalid; } - if (strncmp(attr, "device/", 7) == 0) + if (startswith(attr, "device/")) log_error("the 'device' link may not be available in a future kernel, " "please fix it in %s:%u", filename, lineno); else if (strstr(attr, "../") != NULL) @@ -1315,7 +1315,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "TAGS") == 0) { + if (streq(key, "TAGS")) { if (op > OP_MATCH_MAX) { log_error("invalid TAGS operation\n"); goto invalid; @@ -1324,7 +1324,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) { + if (startswith(key, "ENV{")) { attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1); if (attr == NULL) { log_error("error parsing ENV attribute\n"); @@ -1350,7 +1350,7 @@ static int add_rule(struct udev_rules *rules, char *line, unsigned int i; for (i = 0; i < ELEMENTSOF(blacklist); i++) - if (strcmp(attr, blacklist[i]) == 0) { + if (streq(attr, blacklist[i])) { log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno); continue; } @@ -1360,7 +1360,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "TAG") == 0) { + if (streq(key, "TAG")) { if (op < OP_MATCH_MAX) rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL); else @@ -1368,12 +1368,12 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "PROGRAM") == 0) { + if (streq(key, "PROGRAM")) { rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL); continue; } - if (strcmp(key, "RESULT") == 0) { + if (streq(key, "RESULT")) { if (op > OP_MATCH_MAX) { log_error("invalid RESULT operation\n"); goto invalid; @@ -1382,13 +1382,13 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) { + if (startswith(key, "IMPORT")) { attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1); if (attr == NULL) { log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno); continue; } - if (strcmp(attr, "program") == 0) { + if (streq(attr, "program")) { /* find known built-in command */ if (value[0] != '/') { enum udev_builtin_cmd cmd; @@ -1402,27 +1402,27 @@ static int add_rule(struct udev_rules *rules, char *line, } } rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL); - } else if (strcmp(attr, "builtin") == 0) { + } else if (streq(attr, "builtin")) { enum udev_builtin_cmd cmd = udev_builtin_lookup(value); if (cmd < UDEV_BUILTIN_MAX) rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd); else log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno); - } else if (strcmp(attr, "file") == 0) { + } else if (streq(attr, "file")) { rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL); - } else if (strcmp(attr, "db") == 0) { + } else if (streq(attr, "db")) { rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL); - } else if (strcmp(attr, "cmdline") == 0) { + } else if (streq(attr, "cmdline")) { rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL); - } else if (strcmp(attr, "parent") == 0) { + } else if (streq(attr, "parent")) { rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL); } else log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename, lineno); continue; } - if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) { + if (startswith(key, "TEST")) { mode_t mode = 0; if (op > OP_MATCH_MAX) { @@ -1439,19 +1439,19 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "RUN", sizeof("RUN")-1) == 0) { + if (startswith(key, "RUN")) { attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1); if (attr == NULL) attr = "program"; - if (strcmp(attr, "builtin") == 0) { + if (streq(attr, "builtin")) { enum udev_builtin_cmd cmd = udev_builtin_lookup(value); if (cmd < UDEV_BUILTIN_MAX) rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd); else log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value, filename, lineno); - } else if (strcmp(attr, "program") == 0) { + } else if (streq(attr, "program")) { enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX; rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd); @@ -1462,26 +1462,26 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) { + if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) { rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL); continue; } - if (strcmp(key, "LABEL") == 0) { + if (streq(key, "LABEL")) { rule_tmp.rule.rule.label_off = add_string(rules, value); continue; } - if (strcmp(key, "GOTO") == 0) { + if (streq(key, "GOTO")) { rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL); continue; } - if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) { + if (startswith(key, "NAME")) { if (op < OP_MATCH_MAX) { rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL); } else { - if (strcmp(value, "%k") == 0) { + if (streq(value, "%k")) { log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, " "please remove it from %s:%u\n", filename, lineno); continue; @@ -1497,7 +1497,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strncmp(key, "SYMLINK", sizeof("SYMLINK")-1) == 0) { + if (startswith(key, "SYMLINK")) { if (op < OP_MATCH_MAX) { rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL); } else { @@ -1512,7 +1512,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "OWNER") == 0) { + if (streq(key, "OWNER")) { uid_t uid; char *endptr; @@ -1529,7 +1529,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "GROUP") == 0) { + if (streq(key, "GROUP")) { gid_t gid; char *endptr; @@ -1546,7 +1546,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "MODE") == 0) { + if (streq(key, "MODE")) { mode_t mode; char *endptr; @@ -1559,7 +1559,7 @@ static int add_rule(struct udev_rules *rules, char *line, continue; } - if (strcmp(key, "OPTIONS") == 0) { + if (streq(key, "OPTIONS")) { const char *pos; pos = strstr(value, "link_priority="); @@ -1579,9 +1579,9 @@ static int add_rule(struct udev_rules *rules, char *line, pos = strstr(value, "string_escape="); if (pos != NULL) { pos = &pos[strlen("string_escape=")]; - if (strncmp(pos, "none", strlen("none")) == 0) + if (startswith(pos, "none")) rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL); - else if (strncmp(pos, "replace", strlen("replace")) == 0) + else if (startswith(pos, "replace")) rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL); } @@ -1694,7 +1694,7 @@ static int parse_file(struct udev_rules *rules, const char *filename, unsigned s continue; if (rules->tokens[j].rule.label_off == 0) continue; - if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0) + if (!streq(label, &rules->buf[rules->tokens[j].rule.label_off])) continue; rules->tokens[i].key.rule_goto = j; break; @@ -1729,7 +1729,7 @@ static int add_matching_files(struct udev *udev, struct udev_list *file_list, co ext = strrchr(dent->d_name, '.'); if (ext == NULL) continue; - if (strcmp(ext, suffix) != 0) + if (!streq(ext, suffix)) continue; } util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL); @@ -1927,33 +1927,33 @@ static int match_key(struct udev_rules *rules, struct token *token, const char * switch (token->key.glob) { case GL_PLAIN: - match = (strcmp(key_value, val) == 0); + match = (streq(key_value, val)); break; case GL_GLOB: match = (fnmatch(key_value, val, 0) == 0); break; case GL_SPLIT: { - const char *split; + const char *s; size_t len; - split = &rules->buf[token->key.value_off]; + s = &rules->buf[token->key.value_off]; len = strlen(val); for (;;) { const char *next; - next = strchr(split, '|'); + next = strchr(s, '|'); if (next != NULL) { - size_t matchlen = (size_t)(next - split); + size_t matchlen = (size_t)(next - s); - match = (matchlen == len && strncmp(split, val, matchlen) == 0); + match = (matchlen == len && strncmp(s, val, matchlen) == 0); if (match) break; } else { - match = (strcmp(split, val) == 0); + match = (streq(s, val)); break; } - split = &next[1]; + s = &next[1]; } break; } @@ -2055,7 +2055,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event if (rules->tokens == NULL) return -1; - can_set_name = ((strcmp(udev_device_get_action(event->dev), "remove") != 0) && + can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) && (major(udev_device_get_devnum(event->dev)) > 0 || udev_device_get_ifindex(event->dev) > 0)); @@ -2122,7 +2122,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event bool match = false; udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) { - if (strcmp(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry)) == 0) { + if (streq(&rules->buf[cur->key.value_off], udev_list_entry_get_name(list_entry))) { match = true; break; } @@ -2566,7 +2566,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event log_debug("%i character(s) replaced\n", count); } if (major(udev_device_get_devnum(event->dev)) && - (strcmp(name_str, udev_device_get_devnode(event->dev) + strlen(TEST_PREFIX "/dev/")) != 0)) { + (!streq(name_str, udev_device_get_devnode(event->dev) + strlen(TEST_PREFIX "/dev/")))) { log_error("NAME=\"%s\" ignored, kernel device nodes " "can not be renamed; please fix it in %s:%u\n", name, &rules->buf[rule->rule.filename_off], rule->rule.filename_line); diff --git a/src/udev/udev.h b/src/udev/udev.h index ed5f768ea..e229faaa4 100644 --- a/src/udev/udev.h +++ b/src/udev/udev.h @@ -25,6 +25,7 @@ #include "libudev.h" #include "libudev-private.h" +#include "util.h" struct udev_event { struct udev *udev; diff --git a/src/udev/udevadm-info.c b/src/udev/udevadm-info.c index 3b1413945..229c0a284 100644 --- a/src/udev/udevadm-info.c +++ b/src/udev/udevadm-info.c @@ -325,7 +325,7 @@ static int uinfo(struct udev *udev, int argc, char *argv[]) goto exit; } /* add /dev if not given */ - if (strncmp(optarg, "/dev", strlen("/dev")) != 0) + if (!startswith(optarg, "/dev")) util_strscpyl(name, sizeof(name), "/dev/", optarg, NULL); else util_strscpy(name, sizeof(name), optarg); @@ -361,7 +361,7 @@ static int uinfo(struct udev *udev, int argc, char *argv[]) goto exit; } /* add sys dir if needed */ - if (strncmp(optarg, "/sys", strlen("/sys")) != 0) + if (!startswith(optarg, "/sys")) util_strscpyl(path, sizeof(path), "/sys", optarg, NULL); else util_strscpy(path, sizeof(path), optarg); diff --git a/src/udev/udevadm-test-builtin.c b/src/udev/udevadm-test-builtin.c index e63b1f4eb..ef788b0e0 100644 --- a/src/udev/udevadm-test-builtin.c +++ b/src/udev/udevadm-test-builtin.c @@ -95,7 +95,7 @@ static int adm_builtin(struct udev *udev, int argc, char *argv[]) } /* add /sys if needed */ - if (strncmp(syspath, "/sys", strlen("/sys")) != 0) + if (!startswith(syspath, "/sys")) util_strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); else util_strscpy(filename, sizeof(filename), syspath); diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 0c8a76254..65e9f3ecc 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -112,7 +112,7 @@ static int adm_test(struct udev *udev, int argc, char *argv[]) } /* add /sys if needed */ - if (strncmp(syspath, "/sys", strlen("/sys")) != 0) + if (!startswith(syspath, "/sys")) util_strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); else util_strscpy(filename, sizeof(filename), syspath); diff --git a/src/udev/udevadm-trigger.c b/src/udev/udevadm-trigger.c index a910121ec..7735e4b67 100644 --- a/src/udev/udevadm-trigger.c +++ b/src/udev/udevadm-trigger.c @@ -164,7 +164,7 @@ static int adm_trigger(struct udev *udev, int argc, char *argv[]) struct udev_device *dev; /* add sys dir if needed */ - if (strncmp(optarg, "/sys", strlen("/sys")) != 0) + if (!startswith(optarg, "/sys")) util_strscpyl(path, sizeof(path), "/sys", optarg, NULL); else util_strscpy(path, sizeof(path), optarg); -- 2.30.2