temperature.c (1436B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stddef.h> 3 4 #include "../slstatus.h" 5 #include "../util.h" 6 7 8 #if defined(__linux__) 9 #include <stdint.h> 10 11 const char * 12 temp(const char *file) 13 { 14 uintmax_t temp; 15 16 if (pscanf(file, "%ju", &temp) != 1) 17 return NULL; 18 19 return bprintf("%ju", temp / 1000); 20 } 21 #elif defined(__OpenBSD__) 22 #include <stdio.h> 23 #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */ 24 #include <sys/sensors.h> 25 #include <sys/sysctl.h> 26 27 const char * 28 temp(const char *unused) 29 { 30 int mib[5]; 31 size_t size; 32 struct sensor temp; 33 34 mib[0] = CTL_HW; 35 mib[1] = HW_SENSORS; 36 mib[2] = 0; /* cpu0 */ 37 mib[3] = SENSOR_TEMP; 38 mib[4] = 0; /* temp0 */ 39 40 size = sizeof(temp); 41 42 if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) { 43 warn("sysctl 'SENSOR_TEMP':"); 44 return NULL; 45 } 46 47 /* kelvin to celsius */ 48 return bprintf("%d", (int)((float)(temp.value-273150000) / 1E6)); 49 } 50 #elif defined(__FreeBSD__) 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <sys/sysctl.h> 54 55 #define ACPI_TEMP "hw.acpi.thermal.%s.temperature" 56 57 const char * 58 temp(const char *zone) 59 { 60 char buf[256]; 61 int temp; 62 size_t len; 63 64 len = sizeof(temp); 65 snprintf(buf, sizeof(buf), ACPI_TEMP, zone); 66 if (sysctlbyname(buf, &temp, &len, NULL, 0) < 0 67 || !len) 68 return NULL; 69 70 /* kelvin to decimal celcius */ 71 return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10)); 72 } 73 #endif