-
Notifications
You must be signed in to change notification settings - Fork 0
/
bright.c
75 lines (61 loc) · 1.95 KB
/
bright.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
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
#include "bright.h"
int get_file_value(char *filepath) {
// Open the file.
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
fprintf(stderr, "Error: cannot open input file!");
return -1;
}
// Scan the file, fail if not the proper format.
int val;
if (fscanf(fp, "%u", &val) != 1) {
fprintf(stderr, "Error: file did not contain correct value!");
return -1;
}
fclose(fp);
return val;
}
int change_brightness(int perc) {
int curr = get_file_value(BRIGHTNESSFILE);
if (curr == -1) return -1;
int max = get_file_value(MAXBRIGHTNESS);
if (max == -1) return -1;
// Calculate new percentage for brightness.
int curr_perc = 100 * curr / max;
int new_perc = curr_perc + perc;
int new_brightness = max * new_perc / 100;
// Bounds check of new brightness.
new_brightness = new_brightness < 0 ? 0 : new_brightness;
new_brightness = new_brightness > max ? max : new_brightness;
FILE *fp = fopen(BRIGHTNESSFILE, "w");
if (fp == NULL) {
fprintf(stderr, "Error: could not open brigthness file for writing.");
return -1;
}
fprintf(fp, "%d\n", new_brightness);
fclose(fp);
// Change the permissions of the brightness file to 644.
// It is owned by root and is in the group root.
if (chmod(BRIGHTNESSFILE, PERMISSIONBITS)) {
fprintf(stderr, "Error: could not change file permissions.");
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
// Parse arguments.
if (argc > 2) {
fprintf(stderr, "Error: too many arguments supplied.");
return -1;
} else if (argc == 1) {
fprintf(stderr, "Error: one argument expected.");
return -1;
}
// Error if input is not a numerical value.
int inp;
if (sscanf(argv[1], "%i", &inp) != 1) {
fprintf(stderr, "Error: argument not an integer.");
return -1;
}
return change_brightness(inp);
}