Generalized io_error
This commit is contained in:
parent
ed3edbdf56
commit
8c17dcf7e5
@ -46,6 +46,7 @@ struct cmd_arg rexbacklight_args[] = {
|
||||
};
|
||||
int rexbacklight_args_length = sizeof(rexbacklight_args) / sizeof(rexbacklight_args[0]);
|
||||
|
||||
//Clean up a cmd_arg struct
|
||||
void free_cmd_args(struct arg_values* a){
|
||||
if(!a->next)
|
||||
return;
|
||||
|
||||
@ -29,12 +29,26 @@ static const char* backlight_dir = "/sys/class/backlight/";
|
||||
static const char* backlight_file = "brightness";
|
||||
static const char* max_backlight_file = "max_brightness";
|
||||
|
||||
//Used to store a dynamic array of dynamic strings and size
|
||||
struct string_array{
|
||||
char** list;
|
||||
int size;
|
||||
};
|
||||
|
||||
//Delete the list of backlight devices
|
||||
#define IO_ERROR_DIR "directory"
|
||||
#define IO_ERROR_FILE "file"
|
||||
#define IO_ERROR_OPEN "open"
|
||||
#define IO_ERROR_READ "read"
|
||||
#define IO_ERROR_WRITE "write to"
|
||||
|
||||
void io_error(const char* type, const char* error, const char* name){
|
||||
fprintf(stderr, "Unable to %s %s '%s'!\n", error, type, name);
|
||||
}
|
||||
void io_error_2(const char* type, const char* error, const char* name, const char* name2){
|
||||
fprintf(stderr, "Unable to %s %s '%s%s'!\n", error, type, name, name2);
|
||||
}
|
||||
|
||||
//Clean up a string array
|
||||
void free_string_array(struct string_array* s){
|
||||
for(int i = 0;i < s->size;i++)
|
||||
free(s->list[i]);
|
||||
@ -50,7 +64,7 @@ struct string_array get_backlight_sources(void){
|
||||
fd = opendir(backlight_dir);
|
||||
//If the dir can't be opened, return no backlight devices
|
||||
if(!fd){
|
||||
fprintf(stderr, "Unable to open '%s' for reading!\n", backlight_dir);
|
||||
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir);
|
||||
return (struct string_array){.list = NULL, .size = 0};
|
||||
}
|
||||
|
||||
@ -87,15 +101,14 @@ struct string_array get_backlight_sources(void){
|
||||
|
||||
//Read a float from a file
|
||||
float get_brightness(const char* file){
|
||||
//Static buffer because it shouldn't be able to exceed 127 chars long
|
||||
char buff[127];
|
||||
FILE* bright = fopen(file, "r");
|
||||
if(!bright){
|
||||
fprintf(stderr, "Unable to open brightness file \"%s\"!\n", file);
|
||||
io_error(IO_ERROR_OPEN, IO_ERROR_FILE, file);
|
||||
return 0;
|
||||
}
|
||||
if(!fgets(buff, sizeof(buff), bright)){
|
||||
fprintf(stderr, "Unable to read file \"%s\"!\n", file);
|
||||
io_error(IO_ERROR_READ, IO_ERROR_FILE, file);
|
||||
fclose(bright);
|
||||
return 0;
|
||||
}
|
||||
@ -108,24 +121,29 @@ void do_assignment(struct arg_values* arg){
|
||||
int out = process_op(arg, 0, get_brightness(backlight_file), get_brightness(max_backlight_file));
|
||||
FILE* bright = fopen(backlight_file, "w+");
|
||||
if(!bright){
|
||||
fprintf(stderr, "Unable to open brightness file \"%s%s\" for writing!\n", backlight_dir, backlight_file);
|
||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_FILE, backlight_dir, backlight_file);
|
||||
return;
|
||||
}
|
||||
fprintf(bright, "%d", out);
|
||||
fclose(bright);
|
||||
}
|
||||
|
||||
//Run get operation
|
||||
void do_get(const char* device){
|
||||
fprintf(stdout, "%s: %f\n", device, get_brightness(backlight_file));
|
||||
fprintf(stdout, "%s: %.2f\n", device, get_brightness(backlight_file) / get_brightness(max_backlight_file) * 100);
|
||||
}
|
||||
|
||||
//Run list operation
|
||||
void do_list(struct string_array* names){
|
||||
for(int i = 0;i < names->size;i++)
|
||||
fprintf(stdout, "%s\n", names->list[i]);
|
||||
}
|
||||
|
||||
//If devices were specified, this function will run
|
||||
void individual_device_loop(struct arg_values* a){
|
||||
for(struct arg_values* curr = a->next;curr;curr = curr->next){
|
||||
if(chdir(curr->device)){
|
||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device);
|
||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, curr->device);
|
||||
continue;
|
||||
}
|
||||
switch(curr->operation){
|
||||
@ -140,6 +158,8 @@ void individual_device_loop(struct arg_values* a){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If no devices were specified, this function will run
|
||||
void all_device_loop(struct string_array* backlight_names, struct arg_values* args){
|
||||
switch(args->operation){
|
||||
case OP_SET:
|
||||
@ -147,7 +167,7 @@ void all_device_loop(struct string_array* backlight_names, struct arg_values* ar
|
||||
case OP_DEC:
|
||||
for(int i = 0;i < backlight_names->size;i++){
|
||||
if(chdir(backlight_names->list[i])){
|
||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]);
|
||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, backlight_names->list[i]);
|
||||
continue;
|
||||
}
|
||||
do_assignment(args);
|
||||
@ -159,10 +179,10 @@ void all_device_loop(struct string_array* backlight_names, struct arg_values* ar
|
||||
case OP_GET:
|
||||
for(int i = 0;i < backlight_names->size;i++){
|
||||
if(chdir(backlight_names->list[i])){
|
||||
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]);
|
||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, backlight_dir, backlight_names->list[i]);
|
||||
continue;
|
||||
}
|
||||
fprintf(stdout, "%s: %f\n", backlight_names->list[i], get_brightness(backlight_file));
|
||||
do_get(backlight_names->list[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -189,7 +209,6 @@ int main(int argc, char** argv){
|
||||
return RETVAL_INVALID_DEVICE;
|
||||
}
|
||||
|
||||
|
||||
//Make sure all explicit devices actually exist
|
||||
for(struct arg_values* curr = args.next;curr;curr = curr->next){
|
||||
for(int i = 0;i < backlight_names.size;i++){
|
||||
@ -203,24 +222,19 @@ int main(int argc, char** argv){
|
||||
continue_outer:;
|
||||
}
|
||||
|
||||
|
||||
//save our starting directory so we can change to each backlight directory (makes logic easier)
|
||||
char* starting_dir = getcwd(NULL, 0);
|
||||
|
||||
|
||||
//Redefine CLEANUP to include freeing of the starting_dir string
|
||||
#undef CLEANUP
|
||||
#define CLEANUP() do{free_string_array(&backlight_names);free_cmd_args(&args);free(starting_dir);}while(0)
|
||||
|
||||
|
||||
//Change to the base directory for all sysfs backlights
|
||||
if(chdir(backlight_dir)){
|
||||
fprintf(stderr, "Unable to read backlight sysfs directory!\n");
|
||||
io_error(IO_ERROR_READ, IO_ERROR_DIR, backlight_dir);
|
||||
CLEANUP();
|
||||
return RETVAL_INVALID_DIR;
|
||||
}
|
||||
|
||||
|
||||
//Run selected operation
|
||||
if(args.next){
|
||||
individual_device_loop(&args);
|
||||
@ -228,7 +242,6 @@ int main(int argc, char** argv){
|
||||
all_device_loop(&backlight_names, &args);
|
||||
}
|
||||
|
||||
|
||||
//Return to start directory
|
||||
if(chdir(starting_dir)){
|
||||
CLEANUP();
|
||||
@ -239,6 +252,7 @@ int main(int argc, char** argv){
|
||||
return RETVAL_INVALID_DIR;
|
||||
}
|
||||
}
|
||||
|
||||
CLEANUP();
|
||||
return RETVAL_SUCCESS;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user