added available frequencies split into strings and some fopen checks

This commit is contained in:
celso 2022-11-18 16:13:29 -03:00
parent d61c7f8dcd
commit 2cd6dd4b71
1 changed files with 104 additions and 33 deletions

View File

@ -1,4 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#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"
@ -59,35 +61,104 @@ int stringlength(char string[]){
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 <max temp> <min temp>\n");
printf(" homemade_speedstep [-h | --help]\n");
printf(" -h | --help prints out this help message\n");
fprintf(stderr, "usage: homemade_speedstep <max temp> <min temp>\n");
fprintf(stderr, " homemade_speedstep [-h | --help]\n");
fprintf(stderr, " -h | --help prints out this help message\n");
return 0;
}
/* check for correct ammount of arguments */
if (argc != 3){
printf("error: wrong ammount of arguments given\n");
printf(" see homemade_speedstep --help\n");
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];
/* 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");
getfilec(govfile, gov);
if (comparestrings(gov, "userspace\n") != 0){
printf("error: userspace governor not set\n");
fclose(govfile);
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;
}
/* 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(govfile);
fclose(freqsfile);
fclose(curmaxfile);
fclose(setspeedfile);
fclose(curtempfile);