cpu.c (649B)
1 #include <err.h> 2 #include <stdio.h> 3 4 #ifdef __OpenBSD__ 5 #include <sys/sysctl.h> 6 7 int 8 cpuread(void *arg, char *buf, size_t len) 9 { 10 int mib[2], cpuspeed; 11 size_t sz; 12 13 mib[0] = CTL_HW; 14 mib[1] = HW_CPUSPEED; 15 sz = sizeof(cpuspeed); 16 if (sysctl(mib, 2, &cpuspeed, &sz, NULL, 0) < 0) 17 return -1; 18 snprintf(buf, len, "%4dMHz", cpuspeed); 19 return 0; 20 } 21 #elif __linux__ 22 int 23 cpuread(void *arg, char *buf, size_t len) 24 { 25 char *path = arg; 26 FILE *fp; 27 int freq; 28 29 fp = fopen(path, "r"); 30 if (fp == NULL) { 31 warn("fopen %s", path); 32 return -1; 33 } 34 fscanf(fp, "%d", &freq); 35 fclose(fp); 36 freq /= 1000; 37 snprintf(buf, len, "%4dMHz", freq); 38 return 0; 39 } 40 #endif