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/>.
32 #include "libudev-private.h"
34 static ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
36 char path[UTIL_PATH_SIZE];
40 util_strlcpy(path, syspath, sizeof(path));
41 util_strlcat(path, "/", sizeof(path));
42 util_strlcat(path, slink, sizeof(path));
43 len = readlink(path, path, sizeof(path));
44 if (len < 0 || len >= (ssize_t) sizeof(path))
47 pos = strrchr(path, '/');
51 info(udev, "resolved link to: '%s'\n", pos);
52 return util_strlcpy(value, pos, size);
55 ssize_t util_get_sys_subsystem(struct udev *udev, const char *syspath, char *subsystem, size_t size)
57 return get_sys_link(udev, "subsystem", syspath, subsystem, size);
60 ssize_t util_get_sys_driver(struct udev *udev, const char *syspath, char *driver, size_t size)
62 return get_sys_link(udev, "driver", syspath, driver, size);
65 int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size)
67 char link_target[UTIL_PATH_SIZE];
73 len = readlink(syspath, link_target, sizeof(link_target));
76 link_target[len] = '\0';
77 dbg(udev, "path link '%s' points to '%s'\n", syspath, link_target);
79 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
81 dbg(udev, "base '%s', tail '%s', back %i\n", syspath, &link_target[back * 3], back);
82 for (i = 0; i <= back; i++) {
83 char *pos = strrchr(syspath, '/');
89 dbg(udev, "after moving back '%s'\n", syspath);
90 util_strlcat(syspath, "/", size);
91 util_strlcat(syspath, &link_target[back * 3], size);
95 int util_log_priority(const char *priority)
100 prio = strtol(priority, &endptr, 10);
101 if (endptr[0] == '\0')
103 if (strncasecmp(priority, "err", 3) == 0)
105 if (strcasecmp(priority, "info") == 0)
107 if (strcasecmp(priority, "debug") == 0)
112 size_t util_path_encode(char *s, size_t len)
117 for (i = 0, j = 0; s[i] != '\0'; i++) {
119 memcpy(&t[j], "\\x2f", 4);
121 } else if (s[i] == '\\') {
122 memcpy(&t[j], "\\x5c", 4);
131 i = (j < len - 1) ? j : len - 1;
137 size_t util_path_decode(char *s)
141 for (i = 0, j = 0; s[i] != '\0'; j++) {
142 if (memcmp(&s[i], "\\x2f", 4) == 0) {
145 }else if (memcmp(&s[i], "\\x5c", 4) == 0) {
157 void util_remove_trailing_chars(char *path, char c)
164 while (len > 0 && path[len-1] == c)
168 size_t util_strlcpy(char *dst, const char *src, size_t size)
175 while ((ch = *p++)) {
181 /* If size == 0 there is no space for a final null... */
187 size_t util_strlcat(char *dst, const char *src, size_t size)
194 while (bytes < size && *q) {
199 return (bytes + strlen(src));
201 while ((ch = *p++)) {
211 /* count of characters used to encode one unicode char */
212 static int utf8_encoded_expected_len(const char *str)
214 unsigned char c = (unsigned char)str[0];
218 if ((c & 0xe0) == 0xc0)
220 if ((c & 0xf0) == 0xe0)
222 if ((c & 0xf8) == 0xf0)
224 if ((c & 0xfc) == 0xf8)
226 if ((c & 0xfe) == 0xfc)
231 /* decode one unicode char */
232 static int utf8_encoded_to_unichar(const char *str)
238 len = utf8_encoded_expected_len(str);
243 unichar = str[0] & 0x1f;
246 unichar = (int)str[0] & 0x0f;
249 unichar = (int)str[0] & 0x07;
252 unichar = (int)str[0] & 0x03;
255 unichar = (int)str[0] & 0x01;
261 for (i = 1; i < len; i++) {
262 if (((int)str[i] & 0xc0) != 0x80)
265 unichar |= (int)str[i] & 0x3f;
271 /* expected size used to encode one unicode char */
272 static int utf8_unichar_to_encoded_len(int unichar)
278 if (unichar < 0x10000)
280 if (unichar < 0x200000)
282 if (unichar < 0x4000000)
287 /* check if unicode char has a valid numeric range */
288 static int utf8_unichar_valid_range(int unichar)
290 if (unichar > 0x10ffff)
292 if ((unichar & 0xfffff800) == 0xd800)
294 if ((unichar > 0xfdcf) && (unichar < 0xfdf0))
296 if ((unichar & 0xffff) == 0xffff)
301 /* validate one encoded unicode char and return its length */
302 static int utf8_encoded_valid_unichar(const char *str)
308 len = utf8_encoded_expected_len(str);
316 /* check if expected encoded chars are available */
317 for (i = 0; i < len; i++)
318 if ((str[i] & 0x80) != 0x80)
321 unichar = utf8_encoded_to_unichar(str);
323 /* check if encoded length matches encoded value */
324 if (utf8_unichar_to_encoded_len(unichar) != len)
327 /* check if value has valid range */
328 if (!utf8_unichar_valid_range(unichar))
334 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
335 int util_replace_chars(char *str, const char *white)
340 while (str[i] != '\0') {
343 /* accept whitelist */
344 if (white != NULL && strchr(white, str[i]) != NULL) {
349 /* accept plain ascii char */
350 if ((str[i] >= '0' && str[i] <= '9') ||
351 (str[i] >= 'A' && str[i] <= 'Z') ||
352 (str[i] >= 'a' && str[i] <= 'z')) {
357 /* accept hex encoding */
358 if (str[i] == '\\' && str[i+1] == 'x') {
363 /* accept valid utf8 */
364 len = utf8_encoded_valid_unichar(&str[i]);
370 /* if space is allowed, replace whitespace with ordinary space */
371 if (isspace(str[i]) && strchr(white, ' ') != NULL) {
378 /* everything else is replaced with '_' */