Xinqi Bao's Git

wifi: Fix order and add missing header
[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 return (pscanf(file, "%d", &temp) == 1) ?
13 bprintf("%d", temp / 1000) : NULL;
14 }
15 #elif defined(__OpenBSD__)
16 #include <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */
20 #include <sys/sensors.h>
21 #include <sys/sysctl.h>
22
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) < 0) {
39 fprintf(stderr, "sysctl 'SENSOR_TEMP': %s\n",
40 strerror(errno));
41 return NULL;
42 }
43
44 /* kelvin to celsius */
45 return bprintf("%d", (temp.value - 273150000) / 1000000);
46 }
47 #endif