From 2cd6dd4b713ad894c6fb40de0915745837dc3609 Mon Sep 17 00:00:00 2001 From: celso Date: Fri, 18 Nov 2022 16:13:29 -0300 Subject: [PATCH] added available frequencies split into strings and some fopen checks --- src/homemade_speedstep.c | 137 +++++++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 33 deletions(-) diff --git a/src/homemade_speedstep.c b/src/homemade_speedstep.c index 28d5529..af90dce 100644 --- a/src/homemade_speedstep.c +++ b/src/homemade_speedstep.c @@ -1,4 +1,6 @@ #include +#include + #define CURGOV "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor" #define FREQS "/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies" #define MAXSPDPATH "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq" @@ -7,7 +9,7 @@ int comparestrings(char *a, char *b){ int c = 0; - while(a[c]!='\0' && b[c]!='\0'){ + while(a[c] != '\0' && b[c] != '\0'){ if(a[c] != b[c]){ return 1; } @@ -18,33 +20,33 @@ int comparestrings(char *a, char *b){ int getfilec(FILE *file, char arr[]){ int c, i; - i=0; - while((c=fgetc(file)) != EOF){ - arr[i]=c; + i = 0; + while((c = fgetc(file)) != EOF){ + arr[i] = c; i++; } - arr[i]='\0'; + arr[i] = '\0'; return i; } int powerof(int base, int exponent){ int i, result; - if (exponent==0){ - return result=1; + if (exponent == 0){ + return result = 1; } - result=base; - for(i=0; i=0){ - result+=((int)number[length-1]-'0')*(powerof(10,j)); + result = 0; + j = 0; + while(length - 1 >= 0){ + result += ((int)number[length - 1] - '0') * (powerof(10, j)); j++; length--; } @@ -53,41 +55,110 @@ int stringtoint(char number[], int length){ int stringlength(char string[]){ int i = 0; - while(string[i]!='\0'){ + while(string[i] != '\0'){ i++; } return i; } +int splitstr(char** dest, char line[], int index1, int index2){ + int offset = index1; + while (line[index1] != ' ' && index1 < stringlength(line)){ + index1++; + } + char* p = malloc(sizeof (char)*index1); + int i; + for(i = 0; i < index1 - offset; i++){ + p[i] = line[i + offset]; + } + p[i] = '\0'; + dest[index2] = p; + return index1 + 1; +} + +int countblanks(char line[]){ + int i = 0; + int j = 0; + while(line[i] != '\0'){ + if (line[i] == ' '){ + j++; + } + i++; + } + return j; +} + int main(int argc, char** argv){ + /* check for no arguments or help */ if (argc == 1 || comparestrings(argv[1], "-h") == 0 || comparestrings(argv[1], "--help") == 0){ - printf("usage: homemade_speedstep \n"); - printf(" homemade_speedstep [-h | --help]\n"); - printf(" -h | --help prints out this help message\n"); + fprintf(stderr, "usage: homemade_speedstep \n"); + fprintf(stderr, " homemade_speedstep [-h | --help]\n"); + fprintf(stderr, " -h | --help prints out this help message\n"); return 0; } - if (argc!=3){ - printf("error: wrong ammount of arguments given\n"); - printf(" see homemade_speedstep --help\n"); + /* check for correct ammount of arguments */ + if (argc != 3){ + fprintf(stderr, "error: wrong ammount of arguments given\n"); + fprintf(stderr, " see homemade_speedstep --help\n"); return 1; } - int maxtemp = stringtoint(argv[1], stringlength(argv[1])); - int mintemp = stringtoint(argv[2], stringlength(argv[2])); - char gov[20]; - FILE *govfile = fopen(CURGOV, "r"); - getfilec(govfile, gov); - if (comparestrings(gov, "userspace\n") != 0){ - printf("error: userspace governor not set\n"); - fclose(govfile); + /* set maxtemp and mintemp TODO: check for integer and difference */ + const int maxtemp = stringtoint(argv[1], stringlength(argv[1])); + const int mintemp = stringtoint(argv[2], stringlength(argv[2])); + /* try to open scaling_current_governor, exit on fail */ + FILE* govfile = fopen(CURGOV, "r"); + if (govfile == NULL){ + fprintf(stderr, "error: couldn't open scaling_current_governor\n"); + fprintf(stderr, " check that you have support for cpufreq\n"); + fprintf(stderr, " and the userspace governor in your kernel\n"); + fprintf(stderr, " also check that you're running this program\n"); + fprintf(stderr, " as root\n"); return 2; } - FILE *freqsfile = fopen(FREQS, "r"); - FILE *curmaxfile = fopen(MAXSPDPATH, "r"); - FILE *setspeedfile = fopen(SETSPDPATH, "r"); - FILE *curtempfile = fopen(CURTEMPPATH, "r"); + /* assign content of govfile to a temporary char array */ + char* gov = malloc(sizeof(char) * 20); + getfilec(govfile, gov); + /* check content of scaling_current_governor and exit on failure */ + if (comparestrings(gov, "userspace\n") != 0){ + fprintf(stderr, "error: userspace governor not set\n"); + /* free memory and close file */ + free(gov); + fclose(govfile); + return 3; + } + /* free memory and close file */ + free(gov); fclose(govfile); + /* try to open scaling_available_frequencies, exit on fail */ + FILE *freqsfile = fopen(FREQS, "r"); + if (freqsfile == NULL){ + fprintf(stderr, "error: couldn't open scaling_available_frequencies\n"); + fprintf(stderr, " check that you have support for cpufreq\n"); + fprintf(stderr, " in your kernel and that the program is\n"); + fprintf(stderr, " running as root\n"); + return 4; + } + /* assign content of freqsfile to a temporary char array */ + char* freqsfilec = malloc(sizeof(char) * 200); + getfilec(freqsfile, freqsfilec); + int stepc = countblanks(freqsfilec); + /* create a char ** array with pointers to freq steps */ + char ** steps = malloc(sizeof(char*) * stepc); + int index = 0; + int i; + for(i = 0; i < stepc; i++){ + index = splitstr(steps, freqsfilec, index, i); + } + /* free memory and close file */ + free(freqsfilec); fclose(freqsfile); + /* from here onwards an infinite while loop should start */ + /* cleanup for graceful exit */ + free(steps); + FILE* curmaxfile = fopen(MAXSPDPATH, "r"); + FILE* setspeedfile = fopen(SETSPDPATH, "r"); + FILE* curtempfile = fopen(CURTEMPPATH, "r"); fclose(curmaxfile); fclose(setspeedfile); fclose(curtempfile);