3
0

hwclock.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini hwclock implementation for busybox
  4. *
  5. * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include <sys/utsname.h>
  10. #include "libbb.h"
  11. #include "rtc_.h"
  12. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  13. # ifndef _GNU_SOURCE
  14. # define _GNU_SOURCE
  15. # endif
  16. #endif
  17. static const char *rtcname;
  18. static time_t read_rtc(int utc)
  19. {
  20. time_t ret;
  21. int fd;
  22. fd = rtc_xopen(&rtcname, O_RDONLY);
  23. ret = rtc_read_time(fd, utc);
  24. close(fd);
  25. return ret;
  26. }
  27. static void write_rtc(time_t t, int utc)
  28. {
  29. struct tm tm;
  30. int rtc = rtc_xopen(&rtcname, O_WRONLY);
  31. tm = *(utc ? gmtime(&t) : localtime(&t));
  32. tm.tm_isdst = 0;
  33. xioctl(rtc, RTC_SET_TIME, &tm);
  34. close(rtc);
  35. }
  36. static void show_clock(int utc)
  37. {
  38. //struct tm *ptm;
  39. time_t t;
  40. char *cp;
  41. t = read_rtc(utc);
  42. //ptm = localtime(&t); /* Sets 'tzname[]' */
  43. cp = ctime(&t);
  44. if (cp[0])
  45. cp[strlen(cp) - 1] = '\0';
  46. //printf("%s %.6f seconds %s\n", cp, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
  47. printf("%s 0.000000 seconds\n", cp);
  48. }
  49. static void to_sys_clock(int utc)
  50. {
  51. struct timeval tv;
  52. const struct timezone tz = { timezone/60 - 60*daylight, 0 };
  53. tv.tv_sec = read_rtc(utc);
  54. tv.tv_usec = 0;
  55. if (settimeofday(&tv, &tz))
  56. bb_perror_msg_and_die("settimeofday() failed");
  57. }
  58. static void from_sys_clock(int utc)
  59. {
  60. struct timeval tv;
  61. gettimeofday(&tv, NULL);
  62. //if (gettimeofday(&tv, NULL))
  63. // bb_perror_msg_and_die("gettimeofday() failed");
  64. write_rtc(tv.tv_sec, utc);
  65. }
  66. #define HWCLOCK_OPT_LOCALTIME 0x01
  67. #define HWCLOCK_OPT_UTC 0x02
  68. #define HWCLOCK_OPT_SHOW 0x04
  69. #define HWCLOCK_OPT_HCTOSYS 0x08
  70. #define HWCLOCK_OPT_SYSTOHC 0x10
  71. #define HWCLOCK_OPT_RTCFILE 0x20
  72. int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  73. int hwclock_main(int argc ATTRIBUTE_UNUSED, char **argv)
  74. {
  75. unsigned opt;
  76. int utc;
  77. #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
  78. static const char hwclock_longopts[] ALIGN1 =
  79. "localtime\0" No_argument "l"
  80. "utc\0" No_argument "u"
  81. "show\0" No_argument "r"
  82. "hctosys\0" No_argument "s"
  83. "systohc\0" No_argument "w"
  84. "file\0" Required_argument "f"
  85. ;
  86. applet_long_options = hwclock_longopts;
  87. #endif
  88. opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
  89. opt = getopt32(argv, "lurswf:", &rtcname);
  90. /* If -u or -l wasn't given check if we are using utc */
  91. if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
  92. utc = (opt & HWCLOCK_OPT_UTC);
  93. else
  94. utc = rtc_adjtime_is_utc();
  95. if (opt & HWCLOCK_OPT_HCTOSYS)
  96. to_sys_clock(utc);
  97. else if (opt & HWCLOCK_OPT_SYSTOHC)
  98. from_sys_clock(utc);
  99. else
  100. /* default HWCLOCK_OPT_SHOW */
  101. show_clock(utc);
  102. return 0;
  103. }