1 /* vi: set sw=4 ts=4: */
3 * really dumb hwclock implementation for busybox
4 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
9 #include <sys/utsname.h>
18 #include <linux/rtc.h>
19 #include <sys/ioctl.h>
22 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
38 time_t read_rtc ( int utc )
45 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
46 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
47 perror_msg_and_die ( "Could not access RTC" );
49 memset ( &tm, 0, sizeof( struct tm ));
50 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
51 perror_msg_and_die ( "Could not read time from RTC" );
52 tm. tm_isdst = -1; // not known
57 oldtz = getenv ( "TZ" );
58 setenv ( "TZ", "UTC 0", 1 );
66 setenv ( "TZ", oldtz, 1 );
74 void write_rtc ( time_t t, int utc )
79 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
80 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
81 perror_msg_and_die ( "Could not access RTC" );
86 tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
91 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
92 perror_msg_and_die ( "Could not set the RTC time" );
97 int show_clock ( int utc )
103 t = read_rtc ( utc );
104 ptm = localtime ( &t ); /* Sets 'tzname[]' */
106 safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer ));
108 buffer [xstrlen ( buffer ) - 1] = 0;
110 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] ));
111 printf ( "%s %.6f seconds\n", buffer, 0.0 );
116 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 perror_msg_and_die ( "settimeofday() failed" );
129 int from_sys_clock ( int utc )
131 struct timeval tv = { 0, 0 };
132 struct timezone tz = { 0, 0 };
134 if ( gettimeofday ( &tv, &tz ))
135 perror_msg_and_die ( "gettimeofday() failed" );
137 write_rtc ( tv. tv_sec, utc );
142 int check_utc ( void )
145 FILE *f = fopen ( "/etc/adjtime", "r" );
150 while ( fgets ( buffer, sizeof( buffer ), f )) {
151 int len = xstrlen ( buffer );
153 while ( len && isspace ( buffer [len - 1] ))
158 if ( strncmp ( buffer, "UTC", 3 ) == 0 ) {
168 extern int hwclock_main ( int argc, char **argv )
171 enum OpMode mode = SHOW;
175 #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS
176 struct option long_options[] = {
177 { "show", 0, 0, 'r' },
178 { "utc", 0, 0, 'u' },
179 { "localtime", 0, 0, 'l' },
180 { "hctosys", 0, 0, 's' },
181 { "systohc", 0, 0, 'w' },
185 while (( opt = getopt_long ( argc, argv, "rwsul", long_options, 0 )) != EOF ) {
187 while (( opt = getopt ( argc, argv, "rwsul" )) != EOF ) {
203 case 'l': // -l is not supported by the normal hwclock (only --localtime)
218 return from_sys_clock ( utc );
221 return to_sys_clock ( utc );
225 return show_clock ( utc );