3
0

rtc.c 1.5 KB

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