diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-10 17:08:24 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-10 17:08:24 +0200 |
commit | 7a01f11afbcc0a639cc3676572a41445f7edb65d (patch) | |
tree | 2081d9de1db9fdc163a712dc24bd633989408208 /udev | |
parent | b21b95d7222f32940c8e9462c361e45041c9f7d6 (diff) |
libudev: get rid of udev_utils.c
Diffstat (limited to 'udev')
-rw-r--r-- | udev/lib/Makefile.am | 2 | ||||
-rw-r--r-- | udev/lib/libudev-device.c | 10 | ||||
-rw-r--r-- | udev/lib/libudev-enumerate.c | 2 | ||||
-rw-r--r-- | udev/lib/libudev-private.h | 7 | ||||
-rw-r--r-- | udev/lib/libudev-utils.c | 118 | ||||
-rw-r--r-- | udev/lib/libudev.c | 16 |
6 files changed, 139 insertions, 16 deletions
diff --git a/udev/lib/Makefile.am b/udev/lib/Makefile.am index a768daa817..b33addfbf7 100644 --- a/udev/lib/Makefile.am +++ b/udev/lib/Makefile.am @@ -30,8 +30,6 @@ libudev_la_SOURCES =\ libudev-monitor.c \ ../list.h \ ../udev.h \ - ../udev_utils.c \ - ../udev_utils_string.c \ ../udev_sysdeps.c libudev_la_LDFLAGS = \ diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index 416cae450d..78b644b01d 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -61,7 +61,7 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f strlcpy(filename, udev_get_dev_path(udev), len); start = strlcat(filename, "/.udev/db/", len); strlcat(filename, devpath, len); - return path_encode(&filename[start], len - start); + return util_path_encode(&filename[start], len - start); } static int device_read_db(struct udev_device *udev_device) @@ -264,8 +264,8 @@ void udev_device_unref(struct udev_device *udev_device) free(udev_device->syspath); free(udev_device->devname); free(udev_device->subsystem); - name_list_cleanup(udev_device->udev, &udev_device->link_list); - name_list_cleanup(udev_device->udev, &udev_device->env_list); + util_name_list_cleanup(udev_device->udev, &udev_device->link_list); + util_name_list_cleanup(udev_device->udev, &udev_device->env_list); free(udev_device->action); free(udev_device->driver); free(udev_device->devpath_old); @@ -477,14 +477,14 @@ int device_set_devname(struct udev_device *udev_device, const char *devname) int device_add_devlink(struct udev_device *udev_device, const char *devlink) { - if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL) + if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL) return -ENOMEM; return 0; } int device_add_property(struct udev_device *udev_device, const char *property) { - if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL) + if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL) return -ENOMEM; return 0; } diff --git a/udev/lib/libudev-enumerate.c b/udev/lib/libudev-enumerate.c index f36eafaff6..72b97cebd7 100644 --- a/udev/lib/libudev-enumerate.c +++ b/udev/lib/libudev-enumerate.c @@ -59,7 +59,7 @@ static int devices_scan_subsystem(struct udev *udev, strlcat(devpath, "/", sizeof(devpath)); strlcat(devpath, dent->d_name, sizeof(devpath)); util_resolve_sys_link(udev, devpath, sizeof(devpath)); - name_list_add(udev, device_list, devpath, 1); + util_name_list_add(udev, device_list, devpath, 1); } closedir(dir); return 0; diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index 1579df6d6e..a715067ebb 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -107,4 +107,11 @@ extern int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg); extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size); extern ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver, size_t size); extern int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size); +extern struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list, + const char *name, int sort); +extern void util_name_list_cleanup(struct udev *udev, struct list_head *name_list); +extern int util_log_priority(const char *priority); +extern size_t util_path_encode(char *s, size_t len); +extern size_t util_path_decode(char *s); +extern void util_remove_trailing_chars(char *path, char c); #endif diff --git a/udev/lib/libudev-utils.c b/udev/lib/libudev-utils.c index e5f1ed7c38..dfd5f3c3fb 100644 --- a/udev/lib/libudev-utils.c +++ b/udev/lib/libudev-utils.c @@ -94,3 +94,121 @@ int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size) strlcat(devpath, &link_target[back * 3], size); return 0; } + +struct util_name_entry { + struct list_head node; + char *name; + int *i; +}; + +struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list, + const char *name, int sort) +{ + struct util_name_entry *name_loop; + struct util_name_entry *name_new; + + /* avoid duplicate entries */ + list_for_each_entry(name_loop, name_list, node) { + if (strcmp(name_loop->name, name) == 0) { + dbg(udev, "'%s' is already in the list\n", name); + return name_loop; + } + } + + if (sort) { + list_for_each_entry(name_loop, name_list, node) { + if (strcmp(name_loop->name, name) > 0) + break; + } + } + + name_new->name = strdup(name); + if (name_new->name == NULL) + return NULL; + dbg(udev, "adding '%s'\n", name_new->name); + list_add_tail(&name_new->node, &name_loop->node); + return name_new; +} + +void util_name_list_cleanup(struct udev *udev, struct list_head *name_list) +{ + struct util_name_entry *name_loop; + struct util_name_entry *name_tmp; + + list_for_each_entry_safe(name_loop, name_tmp, name_list, node) { + list_del(&name_loop->node); + free(name_loop->name); + free(name_loop); + } +} + +int util_log_priority(const char *priority) +{ + char *endptr; + int prio; + + prio = strtol(priority, &endptr, 10); + if (endptr[0] == '\0') + return prio; + if (strncasecmp(priority, "err", 3) == 0) + return LOG_ERR; + if (strcasecmp(priority, "info") == 0) + return LOG_INFO; + if (strcasecmp(priority, "debug") == 0) + return LOG_DEBUG; + return 0; +} + +size_t util_path_encode(char *s, size_t len) +{ + char t[(len * 3)+1]; + size_t i, j; + + t[0] = '\0'; + for (i = 0, j = 0; s[i] != '\0'; i++) { + if (s[i] == '/') { + memcpy(&t[j], "\\x2f", 4); + j += 4; + } else if (s[i] == '\\') { + memcpy(&t[j], "\\x5c", 4); + j += 4; + } else { + t[j] = s[i]; + j++; + } + } + t[j] = '\0'; + strncpy(s, t, len); + return j; +} + +size_t util_path_decode(char *s) +{ + size_t i, j; + + for (i = 0, j = 0; s[i] != '\0'; j++) { + if (memcmp(&s[i], "\\x2f", 4) == 0) { + s[j] = '/'; + i += 4; + }else if (memcmp(&s[i], "\\x5c", 4) == 0) { + s[j] = '\\'; + i += 4; + } else { + s[j] = s[i]; + i++; + } + } + s[j] = '\0'; + return j; +} + +void util_remove_trailing_chars(char *path, char c) +{ + size_t len; + + if (path == NULL) + return; + len = strlen(path); + while (len > 0 && path[len-1] == c) + path[--len] = '\0'; +} diff --git a/udev/lib/libudev.c b/udev/lib/libudev.c index 3bcafcd9a6..d1dd9e4838 100644 --- a/udev/lib/libudev.c +++ b/udev/lib/libudev.c @@ -184,18 +184,18 @@ struct udev *udev_new(void) if (env != NULL) { free(udev->sys_path); udev->sys_path = strdup(env); - remove_trailing_chars(udev->sys_path, '/'); + util_remove_trailing_chars(udev->sys_path, '/'); } env = getenv("UDEV_RUN"); - if (env != NULL && !string_is_true(env)) + if (env != NULL && strcmp(env, "0") == 0) udev->run = 0; env = getenv("UDEV_CONFIG_FILE"); if (env != NULL) { free(config_file); config_file = strdup(env); - remove_trailing_chars(config_file, '/'); + util_remove_trailing_chars(config_file, '/'); } if (config_file == NULL) goto err; @@ -263,19 +263,19 @@ struct udev *udev_new(void) } if (strcasecmp(key, "udev_log") == 0) { - udev->log_priority = log_priority(val); + udev->log_priority = util_log_priority(val); continue; } if (strcasecmp(key, "udev_root") == 0) { free(udev->dev_path); udev->dev_path = strdup(val); - remove_trailing_chars(udev->dev_path, '/'); + util_remove_trailing_chars(udev->dev_path, '/'); continue; } if (strcasecmp(key, "udev_rules") == 0) { free(udev->rules_path); udev->rules_path = strdup(val); - remove_trailing_chars(udev->rules_path, '/'); + util_remove_trailing_chars(udev->rules_path, '/'); continue; } } @@ -286,12 +286,12 @@ struct udev *udev_new(void) if (env != NULL) { free(udev->dev_path); udev->dev_path = strdup(env); - remove_trailing_chars(udev->dev_path, '/'); + util_remove_trailing_chars(udev->dev_path, '/'); } env = getenv("UDEV_LOG"); if (env != NULL) - udev->log_priority = log_priority(env); + udev->log_priority = util_log_priority(env); if (udev->dev_path == NULL || udev->sys_path == NULL) goto err; |