1 /* vi: set sw=4 ts=4: */
3 * Mini hwclock implementation for busybox
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <sys/ioctl.h>
12 #include <sys/utsname.h>
16 /* Copied from linux/rtc.h to eliminate the kernel dependency */
17 struct linux_rtc_time {
29 #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */
30 #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */
32 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
38 static const char *rtcname;
40 static int xopen_rtc(int flags)
45 rtc = open("/dev/rtc", flags);
48 rtc = open("/dev/rtc0", flags);
51 rtcname = "/dev/misc/rtc";
53 return xopen(rtcname, flags);
56 static time_t read_rtc(int utc)
61 int rtc = xopen_rtc(O_RDONLY);
63 memset(&tm, 0, sizeof(struct tm));
64 if (ioctl(rtc, RTC_RD_TIME, &tm) < 0)
65 bb_perror_msg_and_die("cannot read time from RTC");
66 tm.tm_isdst = -1; /* not known */
72 setenv("TZ", "UTC 0", 1);
80 setenv("TZ", oldtz, 1);
88 static void write_rtc(time_t t, int utc)
91 int rtc = xopen_rtc(O_WRONLY);
93 tm = *(utc ? gmtime(&t) : localtime(&t));
96 if (ioctl(rtc, RTC_SET_TIME, &tm) < 0)
97 bb_perror_msg_and_die("cannot set the RTC time");
102 static int show_clock(int utc)
106 RESERVE_CONFIG_BUFFER(buffer, 64);
109 ptm = localtime(&t); /* Sets 'tzname[]' */
111 safe_strncpy(buffer, ctime(&t), 64);
113 buffer[strlen(buffer) - 1] = 0;
115 //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
116 printf( "%s %.6f seconds\n", buffer, 0.0);
117 RELEASE_CONFIG_BUFFER(buffer);
122 static int to_sys_clock(int utc)
124 struct timeval tv = { 0, 0 };
125 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
127 tv.tv_sec = read_rtc(utc);
129 if (settimeofday(&tv, &tz))
130 bb_perror_msg_and_die("settimeofday() failed");
135 static int from_sys_clock(int utc)
137 struct timeval tv = { 0, 0 };
138 struct timezone tz = { 0, 0 };
140 if (gettimeofday(&tv, &tz))
141 bb_perror_msg_and_die("gettimeofday() failed");
143 write_rtc(tv.tv_sec, utc);
147 #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS
148 # define ADJTIME_PATH "/var/lib/hwclock/adjtime"
150 # define ADJTIME_PATH "/etc/adjtime"
152 static int check_utc(void)
155 FILE *f = fopen(ADJTIME_PATH, "r");
158 RESERVE_CONFIG_BUFFER(buffer, 128);
160 while (fgets(buffer, sizeof(buffer), f)) {
161 int len = strlen(buffer);
163 while (len && isspace(buffer[len - 1]))
168 if (strncmp(buffer, "UTC", 3) == 0) {
174 RELEASE_CONFIG_BUFFER(buffer);
179 #define HWCLOCK_OPT_LOCALTIME 0x01
180 #define HWCLOCK_OPT_UTC 0x02
181 #define HWCLOCK_OPT_SHOW 0x04
182 #define HWCLOCK_OPT_HCTOSYS 0x08
183 #define HWCLOCK_OPT_SYSTOHC 0x10
184 #define HWCLOCK_OPT_RTCFILE 0x20
186 int hwclock_main(int argc, char **argv );
187 int hwclock_main(int argc, char **argv )
192 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
193 static const struct option hwclock_long_options[] = {
194 { "localtime", 0, 0, 'l' },
195 { "utc", 0, 0, 'u' },
196 { "show", 0, 0, 'r' },
197 { "hctosys", 0, 0, 's' },
198 { "systohc", 0, 0, 'w' },
199 { "file", 1, 0, 'f' },
202 applet_long_options = hwclock_long_options;
204 opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l";
205 opt = getopt32(argc, argv, "lurswf:", &rtcname);
207 /* If -u or -l wasn't given check if we are using utc */
208 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
209 utc = opt & HWCLOCK_OPT_UTC;
213 if (opt & HWCLOCK_OPT_HCTOSYS) {
214 return to_sys_clock(utc);
216 else if (opt & HWCLOCK_OPT_SYSTOHC) {
217 return from_sys_clock(utc);
219 /* default HWCLOCK_OPT_SHOW */
220 return show_clock(utc);