Xinqi Bao's Git

d56cc0e73f4023510980b91bd371ba4b0412c9f5
[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 #include <stdint.h>
8
9 const char *
10 temp(const char *file)
11 {
12 uintmax_t temp;
13
14 if (pscanf(file, "%ju", &temp) != 1) {
15 return NULL;
16 }
17
18 return bprintf("%ju", temp / 1000);
19 }
20 #elif defined(__OpenBSD__)
21 #include <stdio.h>
22 #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */
23 #include <sys/sensors.h>
24 #include <sys/sysctl.h>
25
26 const char *
27 temp(const char *unused)
28 {
29 int mib[5];
30 size_t size;
31 struct sensor temp;
32
33 mib[0] = CTL_HW;
34 mib[1] = HW_SENSORS;
35 mib[2] = 0; /* cpu0 */
36 mib[3] = SENSOR_TEMP;
37 mib[4] = 0; /* temp0 */
38
39 size = sizeof(temp);
40
41 if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
42 warn("sysctl 'SENSOR_TEMP':");
43 return NULL;
44 }
45
46 /* kelvin to celsius */
47 return bprintf("%d", (temp.value - 273150000) / 1E6);
48 }
49 #endif