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