diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-04-19 19:19:44 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-04-19 19:19:44 +0200 |
commit | 9dbfcaf229e6dbf2af1ec1e9d4ac769047db8db0 (patch) | |
tree | 14e04a563e4a8dd14f600070cd8b38fb90d07e45 /src/libsystemd/sd-device/sd-device.c | |
parent | string-util: introduce strspn_from_end() (diff) | |
download | systemd-9dbfcaf229e6dbf2af1ec1e9d4ac769047db8db0.tar.xz systemd-9dbfcaf229e6dbf2af1ec1e9d4ac769047db8db0.zip |
sd-device: use string_replace_char() and strspn_from_end()
Diffstat (limited to '')
-rw-r--r-- | src/libsystemd/sd-device/sd-device.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index bf9c753db5..c7f4c48d42 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -1173,7 +1173,7 @@ _public_ int sd_device_get_devname(sd_device *device, const char **devname) { static int device_set_sysname_and_sysnum(sd_device *device) { _cleanup_free_ char *sysname = NULL; - char *p; + size_t len, n; int r; assert(device); @@ -1185,14 +1185,15 @@ static int device_set_sysname_and_sysnum(sd_device *device) { return -EINVAL; /* some devices have '!' in their name, change that to '/' */ - for (p = strchrnul(sysname, '!'); *p != '\0'; p = strchrnul(p, '!')) - *p = '/'; + string_replace_char(sysname, '!', '/'); - /* trailing number (refuse number only sysname)*/ - for (; p > sysname && isdigit(p[-1]); p--) - ; + n = strspn_from_end(sysname, DIGITS); + len = strlen(sysname); + assert(n <= len); + if (n == len) + n = 0; /* Do not set sysnum for number only sysname. */ - device->sysnum = p > sysname && *p != '\0' ? p : NULL; + device->sysnum = n > 0 ? sysname + len - n : NULL; return free_and_replace(device->sysname, sysname); } |