-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsysfs.c
More file actions
123 lines (99 loc) · 2.23 KB
/
Copy pathsysfs.c
File metadata and controls
123 lines (99 loc) · 2.23 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Automatic Framerate Daemon for AMLogic S905/S912-based boxes.
* Copyright (C) 2017-2019 Andrey Zabolotnyi <zapparello@ya.ru>
*
* For copying conditions, see file COPYING.txt.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "afrd.h"
char *sysfs_read (const char *device_attr)
{
int h, n;
char tmp [4096];
h = open (device_attr, O_RDONLY);
if (h < 0)
goto error;
n = read (h, tmp, sizeof (tmp) - 1);
tmp [n] = 0;
close (h);
return strdup (tmp);
error:
trace (1, "failed to read sysfs attr from %s\n", device_attr);
if (h >= 0)
close (h);
return NULL;
}
char *sysfs_get_str (const char *device, const char *attr)
{
char *ret;
if (attr) {
char tmp [200];
snprintf (tmp, sizeof (tmp), "%s/%s", device, attr);
ret = sysfs_read (tmp);
} else
ret = sysfs_read (device);
if (!ret)
return NULL;
// remove trailing spaces
char *eol = strchr (ret, 0);
while ((eol > ret) && strchr ("\r\n\t ", eol [-1]))
eol--;
*eol = 0;
return ret;
}
int sysfs_get_int (const char *device, const char *attr)
{
int val;
char *vals = sysfs_get_str (device, attr);
if (!vals)
return -1;
/* may be something like HDMI=1 */
char *eq = strchr (vals, '=');
if (eq != NULL)
vals = eq + 1;
val = strtol (vals, NULL, 0);
free (vals);
return val;
}
int sysfs_write (const char *device_attr, const char *value)
{
int h, n;
h = open (device_attr, O_TRUNC | O_WRONLY);
if (h < 0)
goto error;
n = strlen (value);
if (write (h, value, n) != n)
goto error;
close (h);
return 0;
error:
trace (1, "failed to write [%s] into %s\n", value, device_attr);
if (h >= 0)
close (h);
return -1;
}
int sysfs_set_str (const char *device, const char *attr, const char *value)
{
if (attr) {
char tmp [200];
snprintf (tmp, sizeof (tmp), "%s/%s", device, attr);
return sysfs_write (tmp, value);
} else
return sysfs_write (device, value);
}
int sysfs_set_int (const char *device, const char *attr, int value)
{
char tmp [11];
snprintf (tmp, sizeof (tmp), "%d", value);
return sysfs_set_str (device, attr, tmp);
}
int sysfs_exists (const char *device_attr)
{
return access (device_attr, F_OK);
}