From 32bff14871bf8f534fcdcdba1d8409430d6c464b Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 1 Sep 2015 21:19:01 +1200 Subject: Conforming to style, switching paramano-set to getopt (finally) --- paramano_set.c | 149 +++++++++++++++++++++++++-------------------------------- 1 file changed, 64 insertions(+), 85 deletions(-) (limited to 'paramano_set.c') diff --git a/paramano_set.c b/paramano_set.c index 9bcbbff..b902a84 100644 --- a/paramano_set.c +++ b/paramano_set.c @@ -19,107 +19,86 @@ #include "paramano.h" -#define FILE_PATH_STRING_SIZE 100 - -#define ARG_CORE 0x1 -#define ARG_GOV 0x2 -#define ARG_FREQ 0x4 -typedef struct { - char present; - char *core; - char *governor; - char *frequency; -} argument_summary; - -char write_str_to_file(const char *file, const char *data, const char *core) +#define set_freq_max(freq, core) write_str_to_file("scaling_max_freq", freq, core) +#define set_freq_min(freq, core) write_str_to_file("scaling_min_freq", freq, core) +#define set_speed(freq, core) write_str_to_file("scaling_setspeed", freq, core) +#define set_gov(gov, core) write_str_to_file("scaling_governor", gov, core) + +void die_syntax(const char *prefix); +char write_str_to_file(const char *file, const char *data, int core); + +int main(int argc, char *argv[]) { - FILE *fd; - char file_path[FILE_PATH_STRING_SIZE]; + char c = 0; + int core = -1; + char *frequency = NULL; + char *governor = NULL; - // Prepare file path - memset(file_path, '\0', sizeof(file_path)); - sprintf(file_path, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", atoi(core), file ); + setlocale(LC_ALL, ""); + bindtextdomain("paramano", LOCALEDIR); + textdomain("paramano"); - // Try to open file and write data to it - if ( (fd = fopen(file_path, "w")) != NULL ) + if (getuid() != 0) + fprintf(stderr, "Warning: running as UID %d, not 0\n", getuid()); + + while ((c = getopt(argc, argv, "c:f:g:")) != -1) { - fputs(data, fd); - fclose(fd); - return 1; + switch (c) + { + case 'c': + core = atoi(optarg); + break; + case 'f': + frequency = optarg; + break; + case 'g': + governor = optarg; + break; + default: + die_syntax(argv[0]); + break; + } } - // Fallthrough: File couldn't be opened for writing - fprintf(stderr, _("FAILED: Couldn't open %s for writing\n") , file_path); - return 0; -} + if (core < 0 || + (governor == NULL && frequency == NULL) || + (governor != NULL && frequency != NULL)) + die_syntax(argv[0]); + if (governor != NULL) + return set_gov(governor, core); -#define set_freq_max(freq,core) write_str_to_file("scaling_max_freq",freq,core) -#define set_freq_min(freq,core) write_str_to_file("scaling_min_freq",freq,core) -#define set_speed(freq,core) write_str_to_file("scaling_setspeed",freq,core) -#define set_gov(gov,core) write_str_to_file("scaling_governor",gov,core) + if (frequency != NULL) + return set_gov("userspace", core) | set_speed(frequency, core); -void get_argument_summary(int argc, char **argv, argument_summary *argsum) -{ - int arg; + return 1; +} - // Check for -{c,g,f} xyz - for ( arg = 1; arg < argc-1; arg+=2 ) - { - // Can't have empty argument - if ( strlen(argv[arg+1]) != 0 ) - { - if ( strcmp(argv[arg], "-c") == 0 ) - { - // Found -c with an arg - argsum->present |= ARG_CORE; - argsum->core = (char*)(argv[arg+1]); - continue; - } - - if ( strcmp(argv[arg], "-f") == 0 ) - { - // Found -f with an arg - argsum->present |= ARG_FREQ; - argsum->frequency = (char*)(argv[arg+1]); - continue; - } - - if ( strcmp(argv[arg], "-g") == 0 ) - { - // Found -g with an arg - argsum->present |= ARG_GOV; - argsum->governor = (char*)(argv[arg+1]); - //continue; - } - } - } +void die_syntax(const char *prefix) +{ + fprintf(stderr, _("%s {-f frequency|-g governor} -c core\n"), prefix); + exit(1); } -int main(int argc, char *argv[]) +char write_str_to_file(const char *file, const char *data, int core) { - setlocale(LC_ALL,""); - bindtextdomain("paramano",LOCALEDIR); - textdomain("paramano"); + FILE *fd = NULL; + char file_path[1024]; - argument_summary args; - memset(&args, 0, sizeof(args)); + /* Prepare file path */ + snprintf(file_path, sizeof(file_path), "/sys/devices/system/cpu/cpu%d/cpufreq/%s", core, file); - // If unusual number of args, give up now - if (argc == 5) + /* Try to open file and write data to it */ + if ( (fd = fopen(file_path, "w")) != NULL ) { - if (getuid() != 0) - fprintf(stderr,"Warning: running as UID %d, not 0\n",getuid() ); + fputs(data, fd); + fclose(fd); + return 1; + } - get_argument_summary(argc, argv, &args); + /* Fallthrough: File couldn't be opened for writing */ + fprintf(stderr, _("FAILED: Couldn't open %s for writing\n") , file_path); + return 0; +} - if ( args.present == ( ARG_CORE | ARG_GOV ) ) - return set_gov(args.governor , args.core); - if ( args.present == ( ARG_CORE | ARG_FREQ ) ) - return set_gov("userspace", args.core) | set_speed(args.frequency, args.core); - } - // Fall through to here if no valid argument combination - fprintf(stderr, _("%s {-f frequency|-g governor} -c core\n"), argv[0] ); - return 1; -} -- cgit v1.1