rtc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Common RTC functions
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  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. if (strncmp(buffer, "UTC", 3) == 0) {
  21. utc = 1;
  22. break;
  23. }
  24. }
  25. fclose(f);
  26. }
  27. return utc;
  28. }
  29. int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
  30. {
  31. int rtc;
  32. if (!*default_rtc) {
  33. *default_rtc = "/dev/rtc";
  34. rtc = open(*default_rtc, flags);
  35. if (rtc >= 0)
  36. return rtc;
  37. *default_rtc = "/dev/rtc0";
  38. rtc = open(*default_rtc, flags);
  39. if (rtc >= 0)
  40. return rtc;
  41. *default_rtc = "/dev/misc/rtc";
  42. }
  43. return xopen(*default_rtc, flags);
  44. }
  45. void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
  46. {
  47. memset(ptm, 0, sizeof(*ptm));
  48. xioctl(fd, RTC_RD_TIME, ptm);
  49. ptm->tm_isdst = -1; /* "not known" */
  50. }
  51. time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
  52. {
  53. char *oldtz = oldtz; /* for compiler */
  54. time_t t;
  55. if (utc) {
  56. oldtz = getenv("TZ");
  57. putenv((char*)"TZ=UTC0");
  58. tzset();
  59. }
  60. t = mktime(ptm);
  61. if (utc) {
  62. unsetenv("TZ");
  63. if (oldtz)
  64. putenv(oldtz - 3);
  65. tzset();
  66. }
  67. return t;
  68. }