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