-
Notifications
You must be signed in to change notification settings - Fork 2
/
set_micmute_led.c
36 lines (29 loc) · 971 Bytes
/
set_micmute_led.c
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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
const char *BRIGHTNESS_FILE_PATH = "/sys/class/leds/platform::micmute/brightness";
int main(int argc, char *argv[]) {
long newval;
FILE *fp;
if(argc != 2 || (strlen(argv[1]) == 0) ||(strspn(argv[1],"0123456789") != strlen(argv[1]))) {
fprintf(stderr, "Value not supplied!\n");
exit(22);
}
errno = 0;
newval = strtol(argv[1], NULL, 10);
/* Check for various possible errors */
if ((errno == ERANGE && (newval == LONG_MAX || newval == LONG_MIN))
|| (errno != 0 && newval == 0) || (newval != 0 && newval != 1)) {
fprintf(stderr, "Error parsing input!\n");
exit(EXIT_FAILURE);
}
fp = fopen(BRIGHTNESS_FILE_PATH, "w");
if(fp != NULL) {
fprintf(fp, "%ld", newval);
} else {
fprintf(stderr, "Error when opening file: %s\n", strerror(errno));
}
fclose(fp);
}