aboutsummaryrefslogtreecommitdiff
path: root/util-linux/hwclock.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-11-01 10:25:35 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-11-01 10:25:35 +0000
commit92258541449581302e180d05e827e27d35030a18 (patch)
tree99c5ad443f69860833c8ef37e142fddfedb90872 /util-linux/hwclock.c
parent048c93cc5593d53d6243c3e15dc8a5b0072a6083 (diff)
downloadbusybox-w32-92258541449581302e180d05e827e27d35030a18.tar.gz
busybox-w32-92258541449581302e180d05e827e27d35030a18.tar.bz2
busybox-w32-92258541449581302e180d05e827e27d35030a18.zip
mostly style fixes
Diffstat (limited to 'util-linux/hwclock.c')
-rw-r--r--util-linux/hwclock.c131
1 files changed, 63 insertions, 68 deletions
diff --git a/util-linux/hwclock.c b/util-linux/hwclock.c
index 1aff87c11..3a087725e 100644
--- a/util-linux/hwclock.c
+++ b/util-linux/hwclock.c
@@ -10,14 +10,7 @@
10 10
11#include <sys/ioctl.h> 11#include <sys/ioctl.h>
12#include <sys/utsname.h> 12#include <sys/utsname.h>
13#include <ctype.h>
14#include <fcntl.h>
15#include <getopt.h> 13#include <getopt.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
21#include "busybox.h" 14#include "busybox.h"
22 15
23/* Copied from linux/rtc.h to eliminate the kernel dependency */ 16/* Copied from linux/rtc.h to eliminate the kernel dependency */
@@ -42,59 +35,62 @@ struct linux_rtc_time {
42# endif 35# endif
43#endif 36#endif
44 37
45static time_t read_rtc(int utc) 38static int xopen_rtc(int flags)
46{ 39{
47 int rtc; 40 int rtc;
41 rtc = open("/dev/rtc", flags);
42 if (rtc < 0) {
43 rtc = open("/dev/misc/rtc", flags);
44 if (rtc < 0)
45 bb_perror_msg_and_die("cannot access RTC");
46 }
47 return rtc;
48}
49
50static time_t read_rtc(int utc)
51{
48 struct tm tm; 52 struct tm tm;
49 char *oldtz = 0; 53 char *oldtz = 0;
50 time_t t = 0; 54 time_t t = 0;
55 int rtc = xopen_rtc(O_RDONLY);
51 56
52 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) { 57 memset(&tm, 0, sizeof(struct tm));
53 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 ) 58 if (ioctl(rtc, RTC_RD_TIME, &tm) < 0 )
54 bb_perror_msg_and_die ( "cannot access RTC" ); 59 bb_perror_msg_and_die("cannot read time from RTC");
55 }
56 memset ( &tm, 0, sizeof( struct tm ));
57 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
58 bb_perror_msg_and_die ( "cannot read time from RTC" );
59 tm.tm_isdst = -1; /* not known */ 60 tm.tm_isdst = -1; /* not known */
60 61
61 close ( rtc ); 62 close(rtc);
62 63
63 if ( utc ) { 64 if (utc) {
64 oldtz = getenv ( "TZ" ); 65 oldtz = getenv("TZ");
65 setenv ( "TZ", "UTC 0", 1 ); 66 setenv("TZ", "UTC 0", 1);
66 tzset ( ); 67 tzset();
67 } 68 }
68 69
69 t = mktime ( &tm ); 70 t = mktime(&tm);
70 71
71 if ( utc ) { 72 if (utc) {
72 if ( oldtz ) 73 if (oldtz)
73 setenv ( "TZ", oldtz, 1 ); 74 setenv("TZ", oldtz, 1);
74 else 75 else
75 unsetenv ( "TZ" ); 76 unsetenv("TZ");
76 tzset ( ); 77 tzset();
77 } 78 }
78 return t; 79 return t;
79} 80}
80 81
81static void write_rtc(time_t t, int utc) 82static void write_rtc(time_t t, int utc)
82{ 83{
83 int rtc;
84 struct tm tm; 84 struct tm tm;
85 int rtc = xopen_rtc(O_WRONLY);
85 86
86 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) { 87 tm = *(utc ? gmtime(&t) : localtime(&t));
87 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
88 bb_perror_msg_and_die ( "cannot access RTC" );
89 }
90
91 tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
92 tm.tm_isdst = 0; 88 tm.tm_isdst = 0;
93 89
94 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 ) 90 if (ioctl(rtc, RTC_SET_TIME, &tm) < 0)
95 bb_perror_msg_and_die ( "cannot set the RTC time" ); 91 bb_perror_msg_and_die("cannot set the RTC time");
96 92
97 close ( rtc ); 93 close(rtc);
98} 94}
99 95
100static int show_clock(int utc) 96static int show_clock(int utc)
@@ -103,15 +99,15 @@ static int show_clock(int utc)
103 time_t t; 99 time_t t;
104 RESERVE_CONFIG_BUFFER(buffer, 64); 100 RESERVE_CONFIG_BUFFER(buffer, 64);
105 101
106 t = read_rtc ( utc ); 102 t = read_rtc(utc);
107 ptm = localtime ( &t ); /* Sets 'tzname[]' */ 103 ptm = localtime(&t); /* Sets 'tzname[]' */
108 104
109 safe_strncpy ( buffer, ctime ( &t ), 64); 105 safe_strncpy(buffer, ctime(&t), 64);
110 if ( buffer [0] ) 106 if (buffer[0])
111 buffer [strlen ( buffer ) - 1] = 0; 107 buffer[strlen(buffer) - 1] = 0;
112 108
113 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] )); 109 //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname [1] : tzname [0]));
114 printf ( "%s %.6f seconds\n", buffer, 0.0 ); 110 printf( "%s %.6f seconds\n", buffer, 0.0);
115 RELEASE_CONFIG_BUFFER(buffer); 111 RELEASE_CONFIG_BUFFER(buffer);
116 112
117 return 0; 113 return 0;
@@ -122,10 +118,10 @@ static int to_sys_clock(int utc)
122 struct timeval tv = { 0, 0 }; 118 struct timeval tv = { 0, 0 };
123 const struct timezone tz = { timezone/60 - 60*daylight, 0 }; 119 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
124 120
125 tv.tv_sec = read_rtc ( utc ); 121 tv.tv_sec = read_rtc(utc);
126 122
127 if ( settimeofday ( &tv, &tz )) 123 if (settimeofday(&tv, &tz))
128 bb_perror_msg_and_die ( "settimeofday() failed" ); 124 bb_perror_msg_and_die("settimeofday() failed");
129 125
130 return 0; 126 return 0;
131} 127}
@@ -135,10 +131,10 @@ static int from_sys_clock(int utc)
135 struct timeval tv = { 0, 0 }; 131 struct timeval tv = { 0, 0 };
136 struct timezone tz = { 0, 0 }; 132 struct timezone tz = { 0, 0 };
137 133
138 if ( gettimeofday ( &tv, &tz )) 134 if (gettimeofday(&tv, &tz))
139 bb_perror_msg_and_die ( "gettimeofday() failed" ); 135 bb_perror_msg_and_die("gettimeofday() failed");
140 136
141 write_rtc ( tv.tv_sec, utc ); 137 write_rtc(tv.tv_sec, utc);
142 return 0; 138 return 0;
143} 139}
144 140
@@ -150,43 +146,43 @@ static int from_sys_clock(int utc)
150static int check_utc(void) 146static int check_utc(void)
151{ 147{
152 int utc = 0; 148 int utc = 0;
153 FILE *f = fopen ( ADJTIME_PATH, "r" ); 149 FILE *f = fopen(ADJTIME_PATH, "r");
154 150
155 if ( f ) { 151 if (f) {
156 RESERVE_CONFIG_BUFFER(buffer, 128); 152 RESERVE_CONFIG_BUFFER(buffer, 128);
157 153
158 while ( fgets ( buffer, sizeof( buffer ), f )) { 154 while (fgets(buffer, sizeof(buffer), f)) {
159 int len = strlen ( buffer ); 155 int len = strlen(buffer);
160 156
161 while ( len && isspace ( buffer [len - 1] )) 157 while (len && isspace(buffer[len - 1]))
162 len--; 158 len--;
163 159
164 buffer [len] = 0; 160 buffer[len] = 0;
165 161
166 if ( strncmp ( buffer, "UTC", 3 ) == 0 ) { 162 if (strncmp(buffer, "UTC", 3) == 0 ) {
167 utc = 1; 163 utc = 1;
168 break; 164 break;
169 } 165 }
170 } 166 }
171 fclose ( f ); 167 fclose(f);
172 RELEASE_CONFIG_BUFFER(buffer); 168 RELEASE_CONFIG_BUFFER(buffer);
173 } 169 }
174 return utc; 170 return utc;
175} 171}
176 172
177#define HWCLOCK_OPT_LOCALTIME 0x01 173#define HWCLOCK_OPT_LOCALTIME 0x01
178#define HWCLOCK_OPT_UTC 0x02 174#define HWCLOCK_OPT_UTC 0x02
179#define HWCLOCK_OPT_SHOW 0x04 175#define HWCLOCK_OPT_SHOW 0x04
180#define HWCLOCK_OPT_HCTOSYS 0x08 176#define HWCLOCK_OPT_HCTOSYS 0x08
181#define HWCLOCK_OPT_SYSTOHC 0x10 177#define HWCLOCK_OPT_SYSTOHC 0x10
182 178
183int hwclock_main ( int argc, char **argv ) 179int hwclock_main(int argc, char **argv )
184{ 180{
185 unsigned opt; 181 unsigned opt;
186 int utc; 182 int utc;
187 183
188#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS 184#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
189static const struct option hwclock_long_options[] = { 185 static const struct option hwclock_long_options[] = {
190 { "localtime", 0, 0, 'l' }, 186 { "localtime", 0, 0, 'l' },
191 { "utc", 0, 0, 'u' }, 187 { "utc", 0, 0, 'u' },
192 { "show", 0, 0, 'r' }, 188 { "show", 0, 0, 'r' },
@@ -196,7 +192,6 @@ static const struct option hwclock_long_options[] = {
196 }; 192 };
197 applet_long_options = hwclock_long_options; 193 applet_long_options = hwclock_long_options;
198#endif 194#endif
199
200 opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l"; 195 opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l";
201 opt = getopt32(argc, argv, "lursw"); 196 opt = getopt32(argc, argv, "lursw");
202 197
@@ -207,12 +202,12 @@ static const struct option hwclock_long_options[] = {
207 utc = check_utc(); 202 utc = check_utc();
208 203
209 if (opt & HWCLOCK_OPT_HCTOSYS) { 204 if (opt & HWCLOCK_OPT_HCTOSYS) {
210 return to_sys_clock ( utc ); 205 return to_sys_clock(utc);
211 } 206 }
212 else if (opt & HWCLOCK_OPT_SYSTOHC) { 207 else if (opt & HWCLOCK_OPT_SYSTOHC) {
213 return from_sys_clock ( utc ); 208 return from_sys_clock(utc);
214 } else { 209 } else {
215 /* default HWCLOCK_OPT_SHOW */ 210 /* default HWCLOCK_OPT_SHOW */
216 return show_clock ( utc ); 211 return show_clock(utc);
217 } 212 }
218} 213}