added available frequencies split into strings and some fopen checks
This commit is contained in:
parent
d61c7f8dcd
commit
2cd6dd4b71
|
@ -1,4 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define CURGOV "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
|
#define CURGOV "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
|
||||||
#define FREQS "/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies"
|
#define FREQS "/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies"
|
||||||
#define MAXSPDPATH "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"
|
#define MAXSPDPATH "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"
|
||||||
|
@ -7,7 +9,7 @@
|
||||||
|
|
||||||
int comparestrings(char *a, char *b){
|
int comparestrings(char *a, char *b){
|
||||||
int c = 0;
|
int c = 0;
|
||||||
while(a[c]!='\0' && b[c]!='\0'){
|
while(a[c] != '\0' && b[c] != '\0'){
|
||||||
if(a[c] != b[c]){
|
if(a[c] != b[c]){
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -18,33 +20,33 @@ int comparestrings(char *a, char *b){
|
||||||
|
|
||||||
int getfilec(FILE *file, char arr[]){
|
int getfilec(FILE *file, char arr[]){
|
||||||
int c, i;
|
int c, i;
|
||||||
i=0;
|
i = 0;
|
||||||
while((c=fgetc(file)) != EOF){
|
while((c = fgetc(file)) != EOF){
|
||||||
arr[i]=c;
|
arr[i] = c;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
arr[i]='\0';
|
arr[i] = '\0';
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int powerof(int base, int exponent){
|
int powerof(int base, int exponent){
|
||||||
int i, result;
|
int i, result;
|
||||||
if (exponent==0){
|
if (exponent == 0){
|
||||||
return result=1;
|
return result = 1;
|
||||||
}
|
}
|
||||||
result=base;
|
result = base;
|
||||||
for(i=0; i<exponent-1; i++){
|
for(i = 0; i < exponent - 1; i++){
|
||||||
result*=base;
|
result *= base;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int stringtoint(char number[], int length){
|
int stringtoint(char number[], int length){
|
||||||
int j, result;
|
int j, result;
|
||||||
result=0;
|
result = 0;
|
||||||
j=0;
|
j = 0;
|
||||||
while(length-1>=0){
|
while(length - 1 >= 0){
|
||||||
result+=((int)number[length-1]-'0')*(powerof(10,j));
|
result += ((int)number[length - 1] - '0') * (powerof(10, j));
|
||||||
j++;
|
j++;
|
||||||
length--;
|
length--;
|
||||||
}
|
}
|
||||||
|
@ -53,41 +55,110 @@ int stringtoint(char number[], int length){
|
||||||
|
|
||||||
int stringlength(char string[]){
|
int stringlength(char string[]){
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(string[i]!='\0'){
|
while(string[i] != '\0'){
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return 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){
|
int main(int argc, char** argv){
|
||||||
|
/* check for no arguments or help */
|
||||||
if (argc == 1 || comparestrings(argv[1], "-h") == 0
|
if (argc == 1 || comparestrings(argv[1], "-h") == 0
|
||||||
|| comparestrings(argv[1], "--help") == 0){
|
|| comparestrings(argv[1], "--help") == 0){
|
||||||
printf("usage: homemade_speedstep <max temp> <min temp>\n");
|
fprintf(stderr, "usage: homemade_speedstep <max temp> <min temp>\n");
|
||||||
printf(" homemade_speedstep [-h | --help]\n");
|
fprintf(stderr, " homemade_speedstep [-h | --help]\n");
|
||||||
printf(" -h | --help prints out this help message\n");
|
fprintf(stderr, " -h | --help prints out this help message\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (argc!=3){
|
/* check for correct ammount of arguments */
|
||||||
printf("error: wrong ammount of arguments given\n");
|
if (argc != 3){
|
||||||
printf(" see homemade_speedstep --help\n");
|
fprintf(stderr, "error: wrong ammount of arguments given\n");
|
||||||
|
fprintf(stderr, " see homemade_speedstep --help\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int maxtemp = stringtoint(argv[1], stringlength(argv[1]));
|
/* set maxtemp and mintemp TODO: check for integer and difference */
|
||||||
int mintemp = stringtoint(argv[2], stringlength(argv[2]));
|
const int maxtemp = stringtoint(argv[1], stringlength(argv[1]));
|
||||||
char gov[20];
|
const int mintemp = stringtoint(argv[2], stringlength(argv[2]));
|
||||||
FILE *govfile = fopen(CURGOV, "r");
|
/* try to open scaling_current_governor, exit on fail */
|
||||||
getfilec(govfile, gov);
|
FILE* govfile = fopen(CURGOV, "r");
|
||||||
if (comparestrings(gov, "userspace\n") != 0){
|
if (govfile == NULL){
|
||||||
printf("error: userspace governor not set\n");
|
fprintf(stderr, "error: couldn't open scaling_current_governor\n");
|
||||||
fclose(govfile);
|
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;
|
return 2;
|
||||||
}
|
}
|
||||||
FILE *freqsfile = fopen(FREQS, "r");
|
/* assign content of govfile to a temporary char array */
|
||||||
FILE *curmaxfile = fopen(MAXSPDPATH, "r");
|
char* gov = malloc(sizeof(char) * 20);
|
||||||
FILE *setspeedfile = fopen(SETSPDPATH, "r");
|
getfilec(govfile, gov);
|
||||||
FILE *curtempfile = fopen(CURTEMPPATH, "r");
|
/* 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);
|
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);
|
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(curmaxfile);
|
||||||
fclose(setspeedfile);
|
fclose(setspeedfile);
|
||||||
fclose(curtempfile);
|
fclose(curtempfile);
|
||||||
|
|
Loading…
Reference in New Issue