1 /* vi: set sw=4 ts=4: */
3 * Mini hwclock implementation for busybox
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
37 /* Copied from linux/rtc.h to eliminate the kernel dependancy */
38 struct linux_rtc_time {
50 #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */
51 #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */
53 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
59 static time_t read_rtc(int utc)
66 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
67 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
68 bb_perror_msg_and_die ( "Could not access RTC" );
70 memset ( &tm, 0, sizeof( struct tm ));
71 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
72 bb_perror_msg_and_die ( "Could not read time from RTC" );
73 tm. tm_isdst = -1; // not known
78 oldtz = getenv ( "TZ" );
79 setenv ( "TZ", "UTC 0", 1 );
87 setenv ( "TZ", oldtz, 1 );
95 static void write_rtc(time_t t, int utc)
100 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
101 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
102 bb_perror_msg_and_die ( "Could not access RTC" );
105 tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
108 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
109 bb_perror_msg_and_die ( "Could not set the RTC time" );
114 static int show_clock(int utc)
120 t = read_rtc ( utc );
121 ptm = localtime ( &t ); /* Sets 'tzname[]' */
123 safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer ));
125 buffer [bb_strlen ( buffer ) - 1] = 0;
127 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] ));
128 printf ( "%s %.6f seconds\n", buffer, 0.0 );
133 static int to_sys_clock(int utc)
135 struct timeval tv = { 0, 0 };
136 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
138 tv. tv_sec = read_rtc ( utc );
140 if ( settimeofday ( &tv, &tz ))
141 bb_perror_msg_and_die ( "settimeofday() failed" );
146 static int from_sys_clock(int utc)
148 struct timeval tv = { 0, 0 };
149 struct timezone tz = { 0, 0 };
151 if ( gettimeofday ( &tv, &tz ))
152 bb_perror_msg_and_die ( "gettimeofday() failed" );
154 write_rtc ( tv. tv_sec, utc );
159 static int check_utc(void)
162 FILE *f = fopen ( "/var/lib/hwclock/adjtime", "r" );
167 while ( fgets ( buffer, sizeof( buffer ), f )) {
168 int len = bb_strlen ( buffer );
170 while ( len && isspace ( buffer [len - 1] ))
175 if ( strncmp ( buffer, "UTC", 3 ) == 0 ) {
185 #define HWCLOCK_OPT_LOCALTIME 1
186 #define HWCLOCK_OPT_UTC 2
187 #define HWCLOCK_OPT_SHOW 4
188 #define HWCLOCK_OPT_HCTOSYS 8
189 #define HWCLOCK_OPT_SYSTOHC 16
191 extern int hwclock_main ( int argc, char **argv )
196 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
197 static const struct option hwclock_long_options[] = {
198 { "localtime", 0, 0, 'l' },
199 { "utc", 0, 0, 'u' },
200 { "show", 0, 0, 'r' },
201 { "hctosys", 0, 0, 's' },
202 { "systohc", 0, 0, 'w' },
205 bb_applet_long_options = hwclock_long_options;
208 bb_opt_complementaly = "r~ws:w~rs:s~wr";
209 opt = bb_getopt_ulflags(argc, argv, "lursw");
210 /* Check only one mode was given */
211 if(opt & 0x80000000UL) {
215 /* If -u or -l wasnt give check if we are using utc */
216 if ((opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) == 0) {
220 if (opt & HWCLOCK_OPT_HCTOSYS) {
221 return to_sys_clock ( utc );
223 else if (opt & HWCLOCK_OPT_SYSTOHC) {
224 return from_sys_clock ( utc );
226 /* default HWCLOCK_OPT_SHOW */
227 return show_clock ( utc );