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
|
/*
* Program to set sysfs value - similar to sysctl commmand
*/
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define SYS "/sys"
static void get(const char *name)
{
char path[PATH_MAX];
char buf[BUFSIZ];
FILE *f;
snprintf(path, PATH_MAX, SYS "/%s", name);
f = fopen(path, "r");
if (f == NULL) {
fprintf(stderr, "%s : %s\n", path, strerror(errno));
exit(1);
}
while (fgets(buf, BUFSIZ, f) != NULL)
fputs(buf, stdout);
if (ferror(f)) {
fprintf(stderr, "%s : read %s\n", path, strerror(errno));
exit(1);
}
fclose(f);
}
static void set(const char *name, const char *val)
{
FILE *f;
char path[PATH_MAX];
snprintf(path, PATH_MAX, SYS "/%s", name);
f = fopen(path, "w");
if (f == NULL) {
fprintf(stderr, "%s : %s\n", path, strerror(errno));
exit(1);
}
fprintf(f, "%s\n", val);
fflush(f);
if (ferror(f)) {
fprintf(stderr, "%s : read %s\n", path, strerror(errno));
exit(1);
}
fclose(f);
}
int main(int argc, char **argv)
{
if (argc == 1) {
fprintf(stderr, "Usage: %s variable\n", argv[0]);
fprintf(stderr, " %s variable=value\n", argv[0]);
return 1;
}
while (--argc) {
char *ep, *arg = *++argv;
ep = strchr(arg, '=');
if (!ep)
get(arg);
else {
*ep++ = '\0';
set(arg, ep);
}
}
return 0;
}
|