temperature.c 658 B

1234567891011121314151617181920212223242526272829303132
  1. /* See LICENSE file for copyright and license details. */
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/sysctl.h>
  6. #include <sys/time.h>
  7. #include <sys/sensors.h>
  8. #include "../../util.h"
  9. const char *
  10. temp(const char *null)
  11. {
  12. int mib[5];
  13. size_t size;
  14. struct sensor temp;
  15. mib[0] = CTL_HW;
  16. mib[1] = HW_SENSORS;
  17. mib[2] = 0; /* cpu0 */
  18. mib[3] = SENSOR_TEMP;
  19. mib[4] = 0; /* temp0 */
  20. size = sizeof(temp);
  21. if (sysctl(mib, 5, &temp, &size, NULL, 0) == -1) {
  22. fprintf(stderr, "sysctl 'SENSOR_TEMP': %s\n", strerror(errno));
  23. return NULL;
  24. }
  25. return bprintf("%d", (temp.value - 273150000) / 1000000); /* kelvin to celsius */
  26. }