added curfreq check and started laying down the main loop

This commit is contained in:
celso 2022-11-18 18:13:41 -03:00
parent 2cd6dd4b71
commit 58c0d9f503
1 changed files with 55 additions and 4 deletions

View File

@ -88,6 +88,17 @@ int countblanks(char line[]){
return j;
}
void newlinetonullbyte(char string[]){
int i = 0;
while (string[i] != '\0'){
if (string[i] == '\n'){
string[i] = '\0';
break;
}
i++;
}
}
int main(int argc, char** argv){
/* check for no arguments or help */
if (argc == 1 || comparestrings(argv[1], "-h") == 0
@ -153,13 +164,53 @@ int main(int argc, char** argv){
/* free memory and close file */
free(freqsfilec);
fclose(freqsfile);
/* try to open scaling_maximum_frequency, exit on fail */
FILE* curmaxfile = fopen(MAXSPDPATH, "r");
if (curmaxfile == NULL){
fprintf(stderr, "error: couldn't open scaling_maximum_frequency\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 5;
}
/* assign content of curmaxfile to a temporary char array */
char *curmaxfreq = malloc(sizeof(char) * 20);
getfilec(curmaxfile, curmaxfreq);
newlinetonullbyte(curmaxfreq);
/* figure out what step we're on */
int curstep = -1;
for(i = 0; i < stepc; i++){
if (comparestrings(steps[i], curmaxfreq) == 0){
curstep = i;
break;
}
}
/* free memory and close file */
free(curmaxfreq);
fclose(curmaxfile);
/* check if we didn't find our current step, exit on fail */
if (curstep == -1){
fprintf(stderr, "error: couldn't figure out the current frequency\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 6;
}
FILE* curtempfile;
/* from here onwards an infinite while loop should start */
while((curtempfile = fopen(CURTEMPPATH, "r")) != NULL){
char* temperature = malloc(sizeof(char) * 8);
getfilec(curtempfile, temperature);
newlinetonullbyte(temperature);
if (stringtoint(temperature, stringlength(temperature)) >= maxtemp
&& curstep <= stepc){
}
free(temperature);
fclose(curtempfile);
}
/* cleanup for graceful exit */
free(steps);
FILE* curmaxfile = fopen(MAXSPDPATH, "r");
FILE* setspeedfile = fopen(SETSPDPATH, "r");
FILE* curtempfile = fopen(CURTEMPPATH, "r");
fclose(curmaxfile);
FILE* setspeedfile = fopen(SETSPDPATH, "w");
fclose(setspeedfile);
fclose(curtempfile);
return 0;