diff --git a/src/main.c b/src/main.c index f3eae11..7c01c7f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -14,6 +15,8 @@ enum opt { OPT_INVERT = 8, // 'num' now represents the amount of cores to disable }; +char const* const cpu_path = "/sys/devices/system/cpu/cpu%i/online"; + noreturn void fatal(char const* fmt, ...) { char buf[128]; va_list args; @@ -61,6 +64,37 @@ uint8_t getoptions(int32_t argc, char* const* argv, int32_t* ncpus) { return opts; } +bool getcore(uint32_t id) { + // get the file path + char path[64]; // contains the file path (max length is 64 due to the path and a bunch of extra wiggle room) + snprintf(path, 64, cpu_path, id); // writes the path using the id + + // if the file doesn't exist; return true + if (access(path, R_OK) != 0) + return true; + + // read a character from the file, store in state + char state = '\0'; + FILE* f = fopen(path, "r"); + fread(&state, 1, 1, f); + fclose(f); + + // return whether state is truthy + return !!state; +} + +void setcore(uint32_t id, bool state) { + char path[64]; + snprintf(path, 64, cpu_path, id); + + char s = 0x30 + (char)state; // convert the state to a character + + // write the state to the file (creates file if it doesn't exist) + FILE* f = fopen(path, "w"); + fwrite(&s, 1, 1, f); + fclose(f); +} + int32_t main(int32_t argc, char** argv) { if (geteuid() != 0) fatal("must be executed as the root user!");