diff options
author | David Phillips <dbphillipsnz@gmail.com> | 2015-09-01 21:19:01 +1200 |
---|---|---|
committer | David Phillips <dbphillipsnz@gmail.com> | 2015-09-01 21:39:20 +1200 |
commit | 32bff14871bf8f534fcdcdba1d8409430d6c464b (patch) | |
tree | 6de6cd2cda5cc91d76ae2fac8764270a01cb260c /paramano_set.c | |
parent | 841445fb7a482b9c1ed9eca896d308afab4316d8 (diff) | |
download | paramano-32bff14871bf8f534fcdcdba1d8409430d6c464b.tar.xz |
Conforming to style, switching paramano-set to getopt (finally)
Diffstat (limited to 'paramano_set.c')
-rw-r--r-- | paramano_set.c | 149 |
1 files changed, 64 insertions, 85 deletions
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; -} |