funcs.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. Function definitions for the cputhrottle program
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "funcs.h"
  8. #include "specs.h"
  9. /**
  10. Opens a CPU frequency file and returns its contents as a long int.
  11. Returns -1 on error.
  12. */
  13. long
  14. query_file(char* filename)
  15. {
  16. FILE* fp = fopen(filename, "r");
  17. if (!fp) {
  18. return -1;
  19. }
  20. long cpufreq;
  21. fscanf(fp, "%ld", &cpufreq);
  22. fclose(fp);
  23. return cpufreq;
  24. }
  25. /**
  26. Sets a (frequency) value into the CPU frequency file.
  27. Care must be taken to make sure the frequency value is within the
  28. acceptable limits of your machine. Check the limit files specified in
  29. `specs.h` for more information.
  30. Parameters:
  31. 1. filename: a cpufreq file to which write the new freq value.
  32. 2. value: a long int value in kHz of the freq to be set.
  33. Returns 0 on success, -1 on error (most likely permission).
  34. */
  35. int
  36. set_value(char* filename, long value)
  37. {
  38. FILE* fp = fopen(filename, "w");
  39. if (!fp)
  40. return -1;
  41. fprintf(fp, "%ld\n", value);
  42. fclose(fp);
  43. return 0;
  44. }