I am writing a C program to change the screen brightness as xbacklight does not work in my circumstance. The solution should be native C (no system() function), because the program should be executable as a normal user via setuid. Calling external shell commands or scripts causes the kernel to ignore this bit.
Reading the proc file that controls the brightness works fine, but writing to it using C produces no result, even if I run the program as root. The fprintf call returns -130, indicating an error. As a sanity check, I included a working solution using system() as a comment.
[...]
const char* brightness = "/sys/class/backlight/intel_backlight/brightness";
f = fopen(brightness, (!strncmp(argv[1], "get", 3)) ? "r" : "rw");
[...]
int get_brightness() {
int buff;
fscanf(f, "%d", &buff);
return buff;
}
int set(int i) {
i = MAX(0, MIN(255, i));
fprintf(f, "%d", i);
printf("%d", i);
//char *cmd = (char*) malloc(59 *sizeof(char));
//snprintf(cmd, 59, "echo %d > %s", i, brightness);
//system(cmd);
//free(cmd);
}