temp.c (851B)
1 #include <err.h> 2 #include <stdio.h> 3 4 #ifdef __OpenBSD__ 5 #include <sys/sysctl.h> 6 #include <sys/time.h> 7 #include <sys/sensors.h> 8 9 int 10 tempread(void *arg, char *buf, size_t len) 11 { 12 int mib[5]; 13 struct sensor temp; 14 size_t sz; 15 16 mib[0] = CTL_HW; 17 mib[1] = HW_SENSORS; 18 mib[2] = 0; /* cpu0 */ 19 mib[3] = SENSOR_TEMP; 20 mib[4] = 0; /* temp0 */ 21 sz = sizeof(temp); 22 if (sysctl(mib, 5, &temp, &sz, NULL, 0) == -1) 23 return -1; 24 /* temp.value is in kelvin so convert to celsius for display */ 25 snprintf(buf, len, "%lld", (temp.value - 273150000) / 1000000); 26 return 0; 27 } 28 #elif __linux__ 29 int 30 tempread(void *arg, char *buf, size_t len) 31 { 32 char *path = arg; 33 FILE *fp; 34 int temp; 35 36 fp = fopen(path, "r"); 37 if (fp == NULL) { 38 warn("fopen %s", path); 39 return -1; 40 } 41 fscanf(fp, "%d", &temp); 42 fclose(fp); 43 snprintf(buf, len, "%d", temp / 1000); 44 return 0; 45 } 46 #endif