static int device_set_sysname_and_sysnum(sd_device *device) {
_cleanup_free_ char *sysname = NULL;
const char *sysnum = NULL;
- const char *pos;
size_t len = 0;
- if (!device->devpath)
- return -EINVAL;
-
- pos = strrchr(device->devpath, '/');
- if (!pos)
- return -EINVAL;
- pos++;
-
- /* devpath is not a root directory */
- if (*pos == '\0' || pos <= device->devpath)
- return -EINVAL;
+ assert(device);
- sysname = strdup(pos);
- if (!sysname)
- return -ENOMEM;
+ r = path_extract_filename(device->devpath, &sysname);
+ if (r < 0)
+ return r;
/* some devices have '!' in their name, change that to '/' */
while (sysname[len] != '\0') {