Xinqi Bao's Git

7e5cbe7c063c33fe0237c2da2d023d47d8c1f0e7
[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°C", temp / 1000);
17 }
18 #elif defined(__OpenBSD__)
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.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°C", (temp.value - 273150000) / 1000000);
48 }
49 #endif