Xinqi Bao's Git

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