Xinqi Bao's Git

ram: Use POSIX types
[slstatus.git] / components / temperature.c
1 /* See LICENSE file for copyright and license details. */
2 #include <stddef.h>
3
4 #include "../util.h"
5
6 #if defined(__linux__)
7 const char *
8 temp(const char *file)
9 {
10 int temp;
11
12 if(pscanf(file, "%d", &temp) != 1) {
13 return NULL;
14 }
15
16 return bprintf("%d", temp / 1000);
17 }
18 #elif defined(__OpenBSD__)
19 #include <stdio.h>
20 #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */
21 #include <sys/sensors.h>
22 #include <sys/sysctl.h>
23
24 const char *
25 temp(const char *unused)
26 {
27 int mib[5];
28 size_t size;
29 struct sensor temp;
30
31 mib[0] = CTL_HW;
32 mib[1] = HW_SENSORS;
33 mib[2] = 0; /* cpu0 */
34 mib[3] = SENSOR_TEMP;
35 mib[4] = 0; /* temp0 */
36
37 size = sizeof(temp);
38
39 if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
40 warn("sysctl 'SENSOR_TEMP':");
41 return NULL;
42 }
43
44 /* kelvin to celsius */
45 return bprintf("%d", (temp.value - 273150000) / 1E6);
46 }
47 #endif