diff --git a/src/rexbacklight.c b/src/rexbacklight.c index 4bde55f..ba4d6a9 100644 --- a/src/rexbacklight.c +++ b/src/rexbacklight.c @@ -24,8 +24,10 @@ #include #include +//This is where backlight devices can be found in sysfs static const char* backlight_dir = "/sys/class/backlight/"; +//Print out usage message and GPL message if not caused by an error void usage(int exit_val){ fprintf(stderr, "Usage: rexbacklight [option]\n\n"); @@ -60,15 +62,28 @@ struct string_array get_backlight_sources(void){ struct dirent* dir; 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); return (struct string_array){.list = NULL, .size = 0}; } - char** list = malloc(sizeof(const char*) * 8); - int i; + int i, list_size = 8; + char** list = malloc(sizeof(const char*) * list_size); for(i = 0;(dir = readdir(fd));){ + + //Resize list if more than 8 backlight devices exist (not likely) + if(i > list_size){ + char** tmp = malloc(sizeof(const char*) * (list_size += 8)); + for(int j = 0;j < i;j++){ + tmp[j] = list[j]; + } + free(list); + list = tmp; + } + + //Don't include "." or ".." in directory list if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")){ continue; } @@ -82,16 +97,18 @@ struct string_array get_backlight_sources(void){ return (struct string_array){.list = list, .size = i}; } - +//Delete the list of backlight devices void free_string_array(struct string_array* s){ for(int i = 0;i < s->size;i++) free(s->list[i]); free(s->list); } -int get_brightness(const char* file){ - FILE* bright = fopen(file, "r"); +//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); return 0; @@ -171,7 +188,7 @@ int main(int argc, char** argv){ return -1; } - //save our starting directory so we can change to each backlight directory + //save our starting directory so we can change to each backlight directory (makes logic easier) const char* starting_dir = getcwd(NULL, 0); if(chdir(backlight_dir)){ @@ -186,7 +203,7 @@ int main(int argc, char** argv){ continue; } - //Open brightness file for writing + //Write value to backlight files int out = process_arg(argv[1], 0, get_brightness(backlight_file), get_brightness(max_backlight_file)); FILE* bright = fopen(backlight_file, "w+"); if(!bright){