2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "libudev-private.h"
35 static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *devpath, char *subsystem, size_t size)
37 char path[UTIL_PATH_SIZE];
41 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
42 util_strlcat(path, devpath, sizeof(path));
43 util_strlcat(path, "/", sizeof(path));
44 util_strlcat(path, slink, sizeof(path));
45 len = readlink(path, path, sizeof(path));
46 if (len < 0 || len >= (ssize_t) sizeof(path))
49 pos = strrchr(path, '/');
53 return util_strlcpy(subsystem, pos, size);
56 ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size)
58 return get_sys_link(udev, "subsystem", devpath, subsystem, size);
61 ssize_t util_get_sys_driver(struct udev *udev, const char *devpath, char *driver, size_t size)
63 return get_sys_link(udev, "driver", devpath, driver, size);
66 int util_resolve_sys_link(struct udev *udev, char *devpath, size_t size)
68 char link_path[UTIL_PATH_SIZE];
69 char link_target[UTIL_PATH_SIZE];
75 util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
76 util_strlcat(link_path, devpath, sizeof(link_path));
77 len = readlink(link_path, link_target, sizeof(link_target));
80 link_target[len] = '\0';
81 dbg(udev, "path link '%s' points to '%s'\n", devpath, link_target);
83 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
85 dbg(udev, "base '%s', tail '%s', back %i\n", devpath, &link_target[back * 3], back);
86 for (i = 0; i <= back; i++) {
87 char *pos = strrchr(devpath, '/');
93 dbg(udev, "after moving back '%s'\n", devpath);
94 util_strlcat(devpath, "/", size);
95 util_strlcat(devpath, &link_target[back * 3], size);
99 struct util_name_entry *util_name_list_add(struct udev *udev, struct list_head *name_list,
100 const char *name, int sort)
102 struct util_name_entry *name_loop;
103 struct util_name_entry *name_new;
105 /* avoid duplicate entries */
106 list_for_each_entry(name_loop, name_list, node) {
107 if (strcmp(name_loop->name, name) == 0) {
108 dbg(udev, "'%s' is already in the list\n", name);
114 list_for_each_entry(name_loop, name_list, node) {
115 if (strcmp(name_loop->name, name) > 0)
120 name_new = malloc(sizeof(struct util_name_entry));
121 if (name_new == NULL)
123 memset(name_new, 0x00, sizeof(struct util_name_entry));
124 name_new->name = strdup(name);
125 if (name_new->name == NULL) {
129 dbg(udev, "adding '%s'\n", name_new->name);
130 list_add_tail(&name_new->node, &name_loop->node);
134 void util_name_list_cleanup(struct udev *udev, struct list_head *name_list)
136 struct util_name_entry *name_loop;
137 struct util_name_entry *name_tmp;
139 list_for_each_entry_safe(name_loop, name_tmp, name_list, node) {
140 list_del(&name_loop->node);
141 free(name_loop->name);
146 int util_log_priority(const char *priority)
151 prio = strtol(priority, &endptr, 10);
152 if (endptr[0] == '\0')
154 if (strncasecmp(priority, "err", 3) == 0)
156 if (strcasecmp(priority, "info") == 0)
158 if (strcasecmp(priority, "debug") == 0)
163 size_t util_path_encode(char *s, size_t len)
169 for (i = 0, j = 0; s[i] != '\0'; i++) {
171 memcpy(&t[j], "\\x2f", 4);
173 } else if (s[i] == '\\') {
174 memcpy(&t[j], "\\x5c", 4);
186 size_t util_path_decode(char *s)
190 for (i = 0, j = 0; s[i] != '\0'; j++) {
191 if (memcmp(&s[i], "\\x2f", 4) == 0) {
194 }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
206 void util_remove_trailing_chars(char *path, char c)
213 while (len > 0 && path[len-1] == c)
217 size_t util_strlcpy(char *dst, const char *src, size_t size)
224 while ((ch = *p++)) {
230 /* If size == 0 there is no space for a final null... */
236 size_t util_strlcat(char *dst, const char *src, size_t size)
243 while (bytes < size && *q) {
248 return (bytes + strlen(src));
250 while ((ch = *p++)) {
260 /* count of characters used to encode one unicode char */
261 static int utf8_encoded_expected_len(const char *str)
263 unsigned char c = (unsigned char)str[0];
267 if ((c & 0xe0) == 0xc0)
269 if ((c & 0xf0) == 0xe0)
271 if ((c & 0xf8) == 0xf0)
273 if ((c & 0xfc) == 0xf8)
275 if ((c & 0xfe) == 0xfc)
280 /* decode one unicode char */
281 static int utf8_encoded_to_unichar(const char *str)
287 len = utf8_encoded_expected_len(str);
292 unichar = str[0] & 0x1f;
295 unichar = (int)str[0] & 0x0f;
298 unichar = (int)str[0] & 0x07;
301 unichar = (int)str[0] & 0x03;
304 unichar = (int)str[0] & 0x01;
310 for (i = 1; i < len; i++) {
311 if (((int)str[i] & 0xc0) != 0x80)
314 unichar |= (int)str[i] & 0x3f;
320 /* expected size used to encode one unicode char */
321 static int utf8_unichar_to_encoded_len(int unichar)
327 if (unichar < 0x10000)
329 if (unichar < 0x200000)
331 if (unichar < 0x4000000)
336 /* check if unicode char has a valid numeric range */
337 static int utf8_unichar_valid_range(int unichar)
339 if (unichar > 0x10ffff)
341 if ((unichar & 0xfffff800) == 0xd800)
343 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
345 if ((unichar & 0xffff) == 0xffff)
350 /* validate one encoded unicode char and return its length */
351 static int utf8_encoded_valid_unichar(const char *str)
357 len = utf8_encoded_expected_len(str);
365 /* check if expected encoded chars are available */
366 for (i = 0; i < len; i++)
367 if ((str[i] & 0x80) != 0x80)
370 unichar = utf8_encoded_to_unichar(str);
372 /* check if encoded length matches encoded value */
373 if (utf8_unichar_to_encoded_len(unichar) != len)
376 /* check if value has valid range */
377 if (!utf8_unichar_valid_range(unichar))
383 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
384 int util_replace_chars(char *str, const char *white)
389 while (str[i] != '\0') {
392 /* accept whitelist */
393 if (white != NULL && strchr(white, str[i]) != NULL) {
398 /* accept plain ascii char */
399 if ((str[i] >= '0' && str[i] <= '9') ||
400 (str[i] >= 'A' && str[i] <= 'Z') ||
401 (str[i] >= 'a' && str[i] <= 'z')) {
406 /* accept hex encoding */
407 if (str[i] == '\\' && str[i+1] == 'x') {
412 /* accept valid utf8 */
413 len = utf8_encoded_valid_unichar(&str[i]);
419 /* if space is allowed, replace whitespace with ordinary space */
420 if (isspace(str[i]) && strchr(white, ' ') != NULL) {
427 /* everything else is replaced with '_' */