temperature.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stddef.h>
  3. #include "../slstatus.h"
  4. #include "../util.h"
  5. #if defined(__linux__)
  6. #include <stdint.h>
  7. const char *
  8. temp(const char *file)
  9. {
  10. uintmax_t temp;
  11. if (pscanf(file, "%ju", &temp) != 1)
  12. return NULL;
  13. return bprintf("%ju", temp / 1000);
  14. }
  15. #elif defined(__OpenBSD__)
  16. #include <stdio.h>
  17. #include <sys/time.h> /* before <sys/sensors.h> for struct timeval */
  18. #include <sys/sensors.h>
  19. #include <sys/sysctl.h>
  20. const char *
  21. temp(const char *unused)
  22. {
  23. int mib[5];
  24. size_t size;
  25. struct sensor temp;
  26. mib[0] = CTL_HW;
  27. mib[1] = HW_SENSORS;
  28. mib[2] = 0; /* cpu0 */
  29. mib[3] = SENSOR_TEMP;
  30. mib[4] = 0; /* temp0 */
  31. size = sizeof(temp);
  32. if (sysctl(mib, 5, &temp, &size, NULL, 0) < 0) {
  33. warn("sysctl 'SENSOR_TEMP':");
  34. return NULL;
  35. }
  36. /* kelvin to celsius */
  37. return bprintf("%d", (int)((float)(temp.value-273150000) / 1E6));
  38. }
  39. #elif defined(__FreeBSD__)
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <sys/sysctl.h>
  43. #define ACPI_TEMP "hw.acpi.thermal.%s.temperature"
  44. const char *
  45. temp(const char *zone)
  46. {
  47. char buf[256];
  48. int temp;
  49. size_t len;
  50. len = sizeof(temp);
  51. snprintf(buf, sizeof(buf), ACPI_TEMP, zone);
  52. if (sysctlbyname(buf, &temp, &len, NULL, 0) < 0
  53. || !len)
  54. return NULL;
  55. /* kelvin to decimal celcius */
  56. return bprintf("%d.%d", (temp - 2731) / 10, abs((temp - 2731) % 10));
  57. }
  58. #endif