cleanup and fix savefile creation

master
Alessandro Mauri 4 years ago
parent 3299a68059
commit 599065ea07
  1. 67
      sbl.c

67
sbl.c

@ -20,7 +20,7 @@
const char *savefile_paths[] = { const char *savefile_paths[] = {
NULL, /* reserved for external savefile path */ NULL, /* reserved for external savefile path */
"/etc/sbl/savefile" "/etc/sblsave"
}; };
enum action { enum action {
@ -28,7 +28,10 @@ enum action {
}; };
static uid_t ruid; static uid_t ruid;
int device_index = 0; static int device_index = 0;
static const char *savefile;
static int max_brightness;
static const char *device_path;
static void die (const char *, ...); static void die (const char *, ...);
static void usage (void); static void usage (void);
@ -40,7 +43,7 @@ static int save_value (float);
static int get_max_brightness (void); static int get_max_brightness (void);
static float get_saved_value (void); static float get_saved_value (void);
static float get_current_brightness (void); static float get_current_brightness (void);
static const char * backlight_path (int); static const char * get_device_path (int);
static const char * get_savefile (void); static const char * get_savefile (void);
int main (int argc, char *argv[]) int main (int argc, char *argv[])
@ -93,6 +96,10 @@ int main (int argc, char *argv[])
} }
ruid = getuid (); ruid = getuid ();
if (!(device_path = get_device_path(device_index)))
die("invalid device index");
max_brightness = get_max_brightness();
savefile = get_savefile();
switch (act) { switch (act) {
case DISPLAY: case DISPLAY:
@ -127,11 +134,13 @@ int main (int argc, char *argv[])
int save_value (float value) int save_value (float value)
{ {
FILE *fd; FILE *fd;
fd = fopen(get_savefile(), "w"); change_permissions(savefile);
fd = fopen(savefile, "w");
if (!fd) if (!fd)
die("could not open savefile"); die("could not open savefile");
fprintf(fd, "%f\n", value); fprintf(fd, "%f\n", value);
fclose(fd); fclose(fd);
restore_permissions();
return 1; return 1;
} }
@ -139,7 +148,7 @@ float get_saved_value (void)
{ {
FILE *fd; FILE *fd;
float rtf = -1; float rtf = -1;
fd = fopen(get_savefile(), "r"); fd = fopen(savefile, "r");
if (!fd) if (!fd)
die("could not open savefile"); die("could not open savefile");
fscanf(fd, "%f", &rtf); fscanf(fd, "%f", &rtf);
@ -152,10 +161,7 @@ float get_current_brightness (void)
FILE *fd; FILE *fd;
char buf[PATH_MAX] = {0}; char buf[PATH_MAX] = {0};
int cb = -1; int cb = -1;
const char *path = backlight_path(device_index); strcpy(buf, device_path);
if (!path)
die("invalid device index");
strcpy(buf, path);
strcat(buf, "/brightness"); strcat(buf, "/brightness");
fd = fopen(buf, "r"); fd = fopen(buf, "r");
if (!fd) if (!fd)
@ -163,7 +169,7 @@ float get_current_brightness (void)
fscanf(fd, "%d", &cb); fscanf(fd, "%d", &cb);
fclose(fd); fclose(fd);
return ((float)cb / (float)get_max_brightness()) * 100.0;; return ((float)cb / (float)max_brightness) * 100.0;;
} }
int get_max_brightness (void) int get_max_brightness (void)
@ -171,10 +177,7 @@ int get_max_brightness (void)
FILE *fd; FILE *fd;
char buf[PATH_MAX] = {0}; char buf[PATH_MAX] = {0};
int rti = -1; int rti = -1;
const char *path = backlight_path(device_index); strcpy(buf, device_path);
if (!path)
die("invalid device index");
strcpy(buf, path);
strcat(buf, "/max_brightness"); strcat(buf, "/max_brightness");
fd = fopen(buf, "r"); fd = fopen(buf, "r");
if (!fd) if (!fd)
@ -226,10 +229,12 @@ const char * get_savefile (void)
} }
strcpy(buf, result.we_wordv[0]); strcpy(buf, result.we_wordv[0]);
if ((fd = fopen(result.we_wordv[0], "w"))) change_permissions(buf);
break; if ((fd = fopen(result.we_wordv[0], "w")))
break;
} }
} }
restore_permissions();
wordfree(&result); wordfree(&result);
if (!fd) if (!fd)
die("could not open or create a savefile"); die("could not open or create a savefile");
@ -237,7 +242,7 @@ const char * get_savefile (void)
return buf; return buf;
} }
const char * backlight_path (int index) const char * get_device_path (int index)
{ {
DIR *dp; DIR *dp;
struct dirent *de; struct dirent *de;
@ -276,7 +281,7 @@ void list_cards (void)
{ {
int i = 0; int i = 0;
const char *s; const char *s;
for (; (s = backlight_path(i)); i++) for (; (s = get_device_path(i)); i++)
printf("%d: %s\n", i, s); printf("%d: %s\n", i, s);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -285,18 +290,15 @@ void set_brightness (float value)
{ {
int set; int set;
char buf[PATH_MAX] = {0}; char buf[PATH_MAX] = {0};
const char *path = backlight_path(device_index);
FILE *fd; FILE *fd;
if (value > 100) if (value > 100)
value = 100; value = 100;
if (value < BL_MIN) if (value < BL_MIN)
value = BL_MIN; value = BL_MIN;
set = (value/100.0) * get_max_brightness(); set = (value/100.0) * max_brightness;
if (set < 0) if (set < 0)
return; return;
if (!path) strcpy(buf, device_path);
die("invalid device index");
strcpy(buf, path);
strcat(buf, "/brightness"); strcat(buf, "/brightness");
change_permissions(buf); change_permissions(buf);
fd = fopen(buf, "w"); fd = fopen(buf, "w");
@ -312,8 +314,23 @@ void change_permissions (const char *path)
{ {
int status; int status;
struct stat st; struct stat st;
static char buf[PATH_MAX];
stat(path, &st); char *p;
if (stat(path, &st) == -1) {
if (errno != ENOENT)
die("stat error");
strcpy(buf, path);
for (p = &buf[strlen(buf) - 1]; *p && p != buf; p--) {
if (*p == '/') {
*p = '\0';
if (stat(buf, &st) != -1)
break;
}
}
if (p == buf)
die("stat error, invalid path");
}
status = seteuid(st.st_uid); status = seteuid(st.st_uid);
if (status < 0) if (status < 0)
die("could not setuid"); die("could not setuid");

Loading…
Cancel
Save