-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetsyspath.c
More file actions
43 lines (40 loc) · 1.03 KB
/
getsyspath.c
File metadata and controls
43 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright Isaac Dunham, in the year of our Lord 2015
* No rights reserved, see LICENSE for details.
*/
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <libsysdev/sysdev.h>
/* Given major/minor/type, return the directory in /sys
* Returns NULL on failure.
*/
char *sysdev_getsyspath(unsigned int major, unsigned int minor, int ischar)
{
char tpath[256], *syspath = NULL;
ssize_t len;
errno = 0;
len = snprintf(tpath, sizeof(tpath), "/sys/dev/%s/%u:%u",
ischar ? "char" : "block", major, minor);
if (len < sizeof(tpath)) {
syspath = calloc(PATH_MAX, 1);
if (syspath) {
len = readlink(tpath, syspath, PATH_MAX);
if ((len < 6) || (len > PATH_MAX - 8)) {
free(syspath);
syspath = NULL;
}
}
/* Overwrite the start of syspath (../..) with "/sys/" */
if (syspath) {
memcpy(syspath, "/sys/", 5);
strncat(syspath, "/device", PATH_MAX - 1);
}
}
return syspath;
}