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/utsname.h>
33 #include <sys/ioctl.h>
37 /* Copied from linux/rtc.h to eliminate the kernel dependancy */
38 #define RTC_SET_TIME _IOW('p', 0x0a, struct tm) /* Set RTC time */
39 #define RTC_RD_TIME _IOR('p', 0x09, struct tm) /* Read RTC time */
42 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
58 time_t read_rtc ( int utc )
65 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
66 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
67 perror_msg_and_die ( "Could not access RTC" );
69 memset ( &tm, 0, sizeof( struct tm ));
70 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
71 perror_msg_and_die ( "Could not read time from RTC" );
72 tm. tm_isdst = -1; // not known
77 oldtz = getenv ( "TZ" );
78 setenv ( "TZ", "UTC 0", 1 );
86 setenv ( "TZ", oldtz, 1 );
94 void write_rtc ( time_t t, int utc )
99 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
100 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
101 perror_msg_and_die ( "Could not access RTC" );
106 tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
111 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
112 perror_msg_and_die ( "Could not set the RTC time" );
117 int show_clock ( int utc )
123 t = read_rtc ( utc );
124 ptm = localtime ( &t ); /* Sets 'tzname[]' */
126 safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer ));
128 buffer [xstrlen ( buffer ) - 1] = 0;
130 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] ));
131 printf ( "%s %.6f seconds\n", buffer, 0.0 );
136 int to_sys_clock ( int utc )
138 struct timeval tv = { 0, 0 };
139 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
141 tv. tv_sec = read_rtc ( utc );
143 if ( settimeofday ( &tv, &tz ))
144 perror_msg_and_die ( "settimeofday() failed" );
149 int from_sys_clock ( int utc )
151 struct timeval tv = { 0, 0 };
152 struct timezone tz = { 0, 0 };
154 if ( gettimeofday ( &tv, &tz ))
155 perror_msg_and_die ( "gettimeofday() failed" );
157 write_rtc ( tv. tv_sec, utc );
162 int check_utc ( void )
165 FILE *f = fopen ( "/etc/adjtime", "r" );
170 while ( fgets ( buffer, sizeof( buffer ), f )) {
171 int len = xstrlen ( buffer );
173 while ( len && isspace ( buffer [len - 1] ))
178 if ( strncmp ( buffer, "UTC", 3 ) == 0 ) {
188 extern int hwclock_main ( int argc, char **argv )
191 enum OpMode mode = SHOW;
195 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
196 struct option long_options[] = {
197 { "show", 0, 0, 'r' },
198 { "utc", 0, 0, 'u' },
199 { "localtime", 0, 0, 'l' },
200 { "hctosys", 0, 0, 's' },
201 { "systohc", 0, 0, 'w' },
205 while (( opt = getopt_long ( argc, argv, "rwsul", long_options, 0 )) != EOF ) {
207 while (( opt = getopt ( argc, argv, "rwsul" )) != EOF ) {
223 case 'l': // -l is not supported by the normal hwclock (only --localtime)
238 return from_sys_clock ( utc );
241 return to_sys_clock ( utc );
245 return show_clock ( utc );