]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-util.c
Merge pull request #9832 from yuwata/fix-9831
[thirdparty/systemd.git] / src / udev / udevadm-util.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include "path-util.h"
4 #include "string-util.h"
5 #include "udevadm-util.h"
6
7 struct udev_device *find_device(struct udev *udev,
8 const char *id,
9 const char *prefix) {
10
11 assert(id);
12
13 if (prefix && !startswith(id, prefix))
14 id = strjoina(prefix, id);
15
16 if (path_startswith(id, "/dev/")) {
17 struct stat statbuf;
18 char type;
19
20 if (stat(id, &statbuf) < 0)
21 return NULL;
22
23 if (S_ISBLK(statbuf.st_mode))
24 type = 'b';
25 else if (S_ISCHR(statbuf.st_mode))
26 type = 'c';
27 else
28 return NULL;
29
30 return udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
31 } else if (path_startswith(id, "/sys/"))
32 return udev_device_new_from_syspath(udev, id);
33 else
34 return NULL;
35 }