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 int xopen_rtc(int flags)
41 rtc = open("/dev/rtc", flags);
43 rtc = open("/dev/misc/rtc", flags);
45 bb_perror_msg_and_die("cannot access RTC");
50 static time_t read_rtc(int utc)
55 int rtc = xopen_rtc(O_RDONLY);
57 memset(&tm, 0, sizeof(struct tm));
58 if (ioctl(rtc, RTC_RD_TIME, &tm) < 0 )
59 bb_perror_msg_and_die("cannot read time from RTC");
60 tm.tm_isdst = -1; /* not known */
66 setenv("TZ", "UTC 0", 1);
74 setenv("TZ", oldtz, 1);
82 static void write_rtc(time_t t, int utc)
85 int rtc = xopen_rtc(O_WRONLY);
87 tm = *(utc ? gmtime(&t) : localtime(&t));
90 if (ioctl(rtc, RTC_SET_TIME, &tm) < 0)
91 bb_perror_msg_and_die("cannot set the RTC time");
96 static int show_clock(int utc)
100 RESERVE_CONFIG_BUFFER(buffer, 64);
103 ptm = localtime(&t); /* Sets 'tzname[]' */
105 safe_strncpy(buffer, ctime(&t), 64);
107 buffer[strlen(buffer) - 1] = 0;
109 //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
110 printf( "%s %.6f seconds\n", buffer, 0.0);
111 RELEASE_CONFIG_BUFFER(buffer);
116 static int to_sys_clock(int utc)
118 struct timeval tv = { 0, 0 };
119 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
121 tv.tv_sec = read_rtc(utc);
123 if (settimeofday(&tv, &tz))
124 bb_perror_msg_and_die("settimeofday() failed");
129 static int from_sys_clock(int utc)
131 struct timeval tv = { 0, 0 };
132 struct timezone tz = { 0, 0 };
134 if (gettimeofday(&tv, &tz))
135 bb_perror_msg_and_die("gettimeofday() failed");
137 write_rtc(tv.tv_sec, utc);
141 #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS
142 # define ADJTIME_PATH "/var/lib/hwclock/adjtime"
144 # define ADJTIME_PATH "/etc/adjtime"
146 static int check_utc(void)
149 FILE *f = fopen(ADJTIME_PATH, "r");
152 RESERVE_CONFIG_BUFFER(buffer, 128);
154 while (fgets(buffer, sizeof(buffer), f)) {
155 int len = strlen(buffer);
157 while (len && isspace(buffer[len - 1]))
162 if (strncmp(buffer, "UTC", 3) == 0 ) {
168 RELEASE_CONFIG_BUFFER(buffer);
173 #define HWCLOCK_OPT_LOCALTIME 0x01
174 #define HWCLOCK_OPT_UTC 0x02
175 #define HWCLOCK_OPT_SHOW 0x04
176 #define HWCLOCK_OPT_HCTOSYS 0x08
177 #define HWCLOCK_OPT_SYSTOHC 0x10
179 int hwclock_main(int argc, char **argv )
184 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
185 static const struct option hwclock_long_options[] = {
186 { "localtime", 0, 0, 'l' },
187 { "utc", 0, 0, 'u' },
188 { "show", 0, 0, 'r' },
189 { "hctosys", 0, 0, 's' },
190 { "systohc", 0, 0, 'w' },
193 applet_long_options = hwclock_long_options;
195 opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l";
196 opt = getopt32(argc, argv, "lursw");
198 /* If -u or -l wasn't given check if we are using utc */
199 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
200 utc = opt & HWCLOCK_OPT_UTC;
204 if (opt & HWCLOCK_OPT_HCTOSYS) {
205 return to_sys_clock(utc);
207 else if (opt & HWCLOCK_OPT_SYSTOHC) {
208 return from_sys_clock(utc);
210 /* default HWCLOCK_OPT_SHOW */
211 return show_clock(utc);