]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "device-nodes.h"
9 #include "libudev-util.h"
10 #include "string-util.h"
15 * SECTION:libudev-util
16 * @short_description: utils
18 * Utilities useful when dealing with devices and device node names.
21 /* handle "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
22 int util_resolve_subsys_kernel(const char *string
, char *result
, size_t maxsize
, bool read_value
) {
23 char temp
[UTIL_PATH_SIZE
], *subsys
, *sysname
, *attr
;
24 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
31 strscpy(temp
, sizeof(temp
), string
);
35 sysname
= strchr(subsys
, '/');
39 sysname
= &sysname
[1];
41 attr
= strchr(sysname
, ']');
51 if (read_value
&& !attr
)
54 r
= sd_device_new_from_subsystem_sysname(&dev
, subsys
, sysname
);
59 r
= sd_device_get_sysattr_value(dev
, attr
, &val
);
60 if (r
< 0 && r
!= -ENOENT
)
65 strscpy(result
, maxsize
, val
);
66 log_debug("value '[%s/%s]%s' is '%s'", subsys
, sysname
, attr
, result
);
68 r
= sd_device_get_syspath(dev
, &val
);
72 strscpyl(result
, maxsize
, val
, attr
? "/" : NULL
, attr
?: NULL
, NULL
);
73 log_debug("path '[%s/%s]%s' is '%s'", subsys
, sysname
, strempty(attr
), result
);
78 size_t util_path_encode(const char *src
, char *dest
, size_t size
) {
84 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
90 memcpy(&dest
[j
], "\\x2f", 4);
92 } else if (src
[i
] == '\\') {
97 memcpy(&dest
[j
], "\\x5c", 4);
113 * Copy from 'str' to 'to', while removing all leading and trailing whitespace,
114 * and replacing each run of consecutive whitespace with a single underscore.
115 * The chars from 'str' are copied up to the \0 at the end of the string, or
116 * at most 'len' chars. This appends \0 to 'to', at the end of the copied
119 * If 'len' chars are copied into 'to', the final \0 is placed at len+1
120 * (i.e. 'to[len] = \0'), so the 'to' buffer must have at least len+1
123 * Note this may be called with 'str' == 'to', i.e. to replace whitespace
124 * in-place in a buffer. This function can handle that situation.
126 * Note that only 'len' characters are read from 'str'.
128 size_t util_replace_whitespace(const char *str
, char *to
, size_t len
) {
129 bool is_space
= false;
135 i
= strspn(str
, WHITESPACE
);
137 for (j
= 0; j
< len
&& i
< len
&& str
[i
] != '\0'; i
++) {
138 if (isspace(str
[i
])) {
157 /* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
158 size_t util_replace_chars(char *str
, const char *white
) {
159 size_t i
= 0, replaced
= 0;
163 while (str
[i
] != '\0') {
166 if (whitelisted_char_for_devnode(str
[i
], white
)) {
171 /* accept hex encoding */
172 if (str
[i
] == '\\' && str
[i
+1] == 'x') {
177 /* accept valid utf8 */
178 len
= utf8_encoded_valid_unichar(&str
[i
]);
184 /* if space is allowed, replace whitespace with ordinary space */
185 if (isspace(str
[i
]) && white
&& strchr(white
, ' ')) {
192 /* everything else is replaced with '_' */
201 * udev_util_encode_string:
202 * @str: input string to be encoded
203 * @str_enc: output string to store the encoded input string
204 * @len: maximum size of the output string, which may be
205 * four times as long as the input string
207 * Encode all potentially unsafe characters of a string to the
208 * corresponding 2 char hex value prefixed by '\x'.
210 * Returns: 0 if the entire string was copied, non-zero otherwise.
212 _public_
int udev_util_encode_string(const char *str
, char *str_enc
, size_t len
) {
213 return encode_devnode_name(str
, str_enc
, len
);