3
0

rtc.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Common RTC functions
  3. */
  4. #include "libbb.h"
  5. #include "rtc_.h"
  6. #if ENABLE_FEATURE_HWCLOCK_ADJTIME_FHS
  7. # define ADJTIME_PATH "/var/lib/hwclock/adjtime"
  8. #else
  9. # define ADJTIME_PATH "/etc/adjtime"
  10. #endif
  11. int rtc_adjtime_is_utc(void)
  12. {
  13. int utc = 0;
  14. FILE *f = fopen(ADJTIME_PATH, "r");
  15. if (f) {
  16. RESERVE_CONFIG_BUFFER(buffer, 128);
  17. while (fgets(buffer, sizeof(buffer), f)) {
  18. int len = strlen(buffer);
  19. while (len && isspace(buffer[len - 1]))
  20. len--;
  21. buffer[len] = 0;
  22. if (strncmp(buffer, "UTC", 3) == 0) {
  23. utc = 1;
  24. break;
  25. }
  26. }
  27. fclose(f);
  28. RELEASE_CONFIG_BUFFER(buffer);
  29. }
  30. return utc;
  31. }
  32. int rtc_xopen(const char **default_rtc, int flags)
  33. {
  34. int rtc;
  35. if (!*default_rtc) {
  36. *default_rtc = "/dev/rtc";
  37. rtc = open(*default_rtc, flags);
  38. if (rtc >= 0)
  39. return rtc;
  40. *default_rtc = "/dev/rtc0";
  41. rtc = open(*default_rtc, flags);
  42. if (rtc >= 0)
  43. return rtc;
  44. *default_rtc = "/dev/misc/rtc";
  45. }
  46. return xopen(*default_rtc, flags);
  47. }
  48. time_t rtc_read_time(int fd, int utc)
  49. {
  50. struct tm tm;
  51. char *oldtz = 0;
  52. time_t t = 0;
  53. memset(&tm, 0, sizeof(struct tm));
  54. xioctl(fd, RTC_RD_TIME, &tm);
  55. tm.tm_isdst = -1; /* not known */
  56. if (utc) {
  57. oldtz = getenv("TZ");
  58. putenv((char*)"TZ=UTC0");
  59. tzset();
  60. }
  61. t = mktime(&tm);
  62. if (utc) {
  63. unsetenv("TZ");
  64. if (oldtz)
  65. putenv(oldtz - 3);
  66. tzset();
  67. }
  68. return t;
  69. }