]> git.sur5r.net Git - i3/i3status/blob - src/get_cpu_temperature.c
Support for (Debian) GNU/kFreeBSD
[i3/i3status] / src / get_cpu_temperature.c
1 // vim:ts=8:expandtab
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5
6 #include "i3status.h"
7
8 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9 #include <err.h>
10 #include <sys/types.h>
11 #include <sys/sysctl.h>
12 #define TZ_ZEROC 2732
13 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
14 #endif
15
16
17 /*
18  * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
19  * returns the temperature in degree celcius.
20  *
21  */
22 const char *get_cpu_temperature_info() {
23         static char buf[16];
24
25 #if defined(LINUX)
26         long int temp;
27         if (!slurp(thermal_zone, buf, sizeof(buf)))
28                 die("Could not open \"%s\"\n", thermal_zone);
29         temp = strtol(buf, NULL, 10);
30         if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
31                 (void)snprintf(buf, sizeof(buf), "T: ? C");
32         else
33                 (void)snprintf(buf, sizeof(buf), "T: %ld C", (temp/1000));
34 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
35         int sysctl_rslt;
36         size_t sysctl_size = sizeof (sysctl_rslt);
37         if (sysctlbyname(thermal_zone,&sysctl_rslt,&sysctl_size,NULL,0))
38             return "No Thermal";
39
40         snprintf(buf,sizeof(buf),"T: %d.%d C",TZ_KELVTOC(sysctl_rslt));
41 #endif
42
43         return buf;
44 }