diff options
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/hwclock.c | 109 |
1 files changed, 41 insertions, 68 deletions
diff --git a/util-linux/hwclock.c b/util-linux/hwclock.c index 6f3411a38..e62a64553 100644 --- a/util-linux/hwclock.c +++ b/util-linux/hwclock.c | |||
| @@ -21,19 +21,19 @@ | |||
| 21 | */ | 21 | */ |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | #include <sys/ioctl.h> | ||
| 25 | #include <sys/time.h> | ||
| 24 | #include <sys/utsname.h> | 26 | #include <sys/utsname.h> |
| 25 | #include <stdlib.h> | ||
| 26 | #include <unistd.h> | ||
| 27 | #include <syslog.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <ctype.h> | 27 | #include <ctype.h> |
| 30 | #include <fcntl.h> | 28 | #include <fcntl.h> |
| 31 | #include <sys/time.h> | 29 | #include <getopt.h> |
| 30 | #include <stdlib.h> | ||
| 31 | #include <string.h> | ||
| 32 | #include <syslog.h> | ||
| 32 | #include <time.h> | 33 | #include <time.h> |
| 33 | #include <sys/ioctl.h> | 34 | #include <unistd.h> |
| 34 | #include "busybox.h" | 35 | #include "busybox.h" |
| 35 | 36 | ||
| 36 | |||
| 37 | /* Copied from linux/rtc.h to eliminate the kernel dependancy */ | 37 | /* Copied from linux/rtc.h to eliminate the kernel dependancy */ |
| 38 | struct linux_rtc_time { | 38 | struct linux_rtc_time { |
| 39 | int tm_sec; | 39 | int tm_sec; |
| @@ -47,28 +47,16 @@ struct linux_rtc_time { | |||
| 47 | int tm_isdst; | 47 | int tm_isdst; |
| 48 | }; | 48 | }; |
| 49 | 49 | ||
| 50 | |||
| 51 | #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */ | 50 | #define RTC_SET_TIME _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time */ |
| 52 | #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */ | 51 | #define RTC_RD_TIME _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time */ |
| 53 | 52 | ||
| 54 | |||
| 55 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS | 53 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS |
| 56 | # ifndef _GNU_SOURCE | 54 | # ifndef _GNU_SOURCE |
| 57 | # define _GNU_SOURCE | 55 | # define _GNU_SOURCE |
| 58 | # endif | 56 | # endif |
| 59 | #endif | 57 | #endif |
| 60 | 58 | ||
| 61 | #include <getopt.h> | 59 | static time_t read_rtc(int utc) |
| 62 | |||
| 63 | |||
| 64 | enum OpMode { | ||
| 65 | SHOW, | ||
| 66 | SYSTOHC, | ||
| 67 | HCTOSYS | ||
| 68 | }; | ||
| 69 | |||
| 70 | |||
| 71 | time_t read_rtc ( int utc ) | ||
| 72 | { | 60 | { |
| 73 | int rtc; | 61 | int rtc; |
| 74 | struct tm tm; | 62 | struct tm tm; |
| @@ -104,7 +92,7 @@ time_t read_rtc ( int utc ) | |||
| 104 | return t; | 92 | return t; |
| 105 | } | 93 | } |
| 106 | 94 | ||
| 107 | void write_rtc ( time_t t, int utc ) | 95 | static void write_rtc(time_t t, int utc) |
| 108 | { | 96 | { |
| 109 | int rtc; | 97 | int rtc; |
| 110 | struct tm tm; | 98 | struct tm tm; |
| @@ -123,7 +111,7 @@ void write_rtc ( time_t t, int utc ) | |||
| 123 | close ( rtc ); | 111 | close ( rtc ); |
| 124 | } | 112 | } |
| 125 | 113 | ||
| 126 | int show_clock ( int utc ) | 114 | static int show_clock(int utc) |
| 127 | { | 115 | { |
| 128 | struct tm *ptm; | 116 | struct tm *ptm; |
| 129 | time_t t; | 117 | time_t t; |
| @@ -142,7 +130,7 @@ int show_clock ( int utc ) | |||
| 142 | return 0; | 130 | return 0; |
| 143 | } | 131 | } |
| 144 | 132 | ||
| 145 | int to_sys_clock ( int utc ) | 133 | static int to_sys_clock(int utc) |
| 146 | { | 134 | { |
| 147 | struct timeval tv = { 0, 0 }; | 135 | struct timeval tv = { 0, 0 }; |
| 148 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; | 136 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; |
| @@ -155,7 +143,7 @@ int to_sys_clock ( int utc ) | |||
| 155 | return 0; | 143 | return 0; |
| 156 | } | 144 | } |
| 157 | 145 | ||
| 158 | int from_sys_clock ( int utc ) | 146 | static int from_sys_clock(int utc) |
| 159 | { | 147 | { |
| 160 | struct timeval tv = { 0, 0 }; | 148 | struct timeval tv = { 0, 0 }; |
| 161 | struct timezone tz = { 0, 0 }; | 149 | struct timezone tz = { 0, 0 }; |
| @@ -168,7 +156,7 @@ int from_sys_clock ( int utc ) | |||
| 168 | } | 156 | } |
| 169 | 157 | ||
| 170 | 158 | ||
| 171 | int check_utc ( void ) | 159 | static int check_utc(void) |
| 172 | { | 160 | { |
| 173 | int utc = 0; | 161 | int utc = 0; |
| 174 | FILE *f = fopen ( "/var/lib/hwclock/adjtime", "r" ); | 162 | FILE *f = fopen ( "/var/lib/hwclock/adjtime", "r" ); |
| @@ -194,63 +182,48 @@ int check_utc ( void ) | |||
| 194 | return utc; | 182 | return utc; |
| 195 | } | 183 | } |
| 196 | 184 | ||
| 185 | #define HWCLOCK_OPT_LOCALTIME 1 | ||
| 186 | #define HWCLOCK_OPT_UTC 8 | ||
| 187 | #define HWCLOCK_OPT_SHOW 2 | ||
| 188 | #define HWCLOCK_OPT_HCTOSYS 4 | ||
| 189 | #define HWCLOCK_OPT_SYSTOHC 16 | ||
| 190 | |||
| 197 | extern int hwclock_main ( int argc, char **argv ) | 191 | extern int hwclock_main ( int argc, char **argv ) |
| 198 | { | 192 | { |
| 199 | int opt; | 193 | unsigned long opt; |
| 200 | enum OpMode mode = SHOW; | ||
| 201 | int utc = 0; | 194 | int utc = 0; |
| 202 | int utc_arg = 0; | ||
| 203 | 195 | ||
| 204 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS | 196 | #ifdef CONFIG_FEATURE_HWCLOCK_LONGOPTIONS |
| 205 | struct option long_options[] = { | 197 | static const struct option hwclock_long_options[] = { |
| 206 | { "show", 0, 0, 'r' }, | ||
| 207 | { "utc", 0, 0, 'u' }, | ||
| 208 | { "localtime", 0, 0, 'l' }, | 198 | { "localtime", 0, 0, 'l' }, |
| 199 | { "utc", 0, 0, 'u' }, | ||
| 200 | { "show", 0, 0, 'r' }, | ||
| 209 | { "hctosys", 0, 0, 's' }, | 201 | { "hctosys", 0, 0, 's' }, |
| 210 | { "systohc", 0, 0, 'w' }, | 202 | { "systohc", 0, 0, 'w' }, |
| 211 | { 0, 0, 0, 0 } | 203 | { 0, 0, 0, 0 } |
| 212 | }; | 204 | }; |
| 213 | 205 | bb_applet_long_options = hwclock_long_options; | |
| 214 | while (( opt = getopt_long ( argc, argv, "rwsul", long_options, 0 )) != EOF ) { | ||
| 215 | #else | ||
| 216 | while (( opt = getopt ( argc, argv, "rwsul" )) != EOF ) { | ||
| 217 | #endif | 206 | #endif |
| 218 | switch ( opt ) { | 207 | |
| 219 | case 'r': | 208 | bb_opt_complementaly = "r~ws:w~rs:s~wr"; |
| 220 | mode = SHOW; | 209 | opt = bb_getopt_ulflags(argc, argv, "lursw"); |
| 221 | break; | 210 | /* Check only one mode was given */ |
| 222 | case 'w': | 211 | if(opt & 0x80000000UL) { |
| 223 | mode = SYSTOHC; | 212 | bb_show_usage(); |
| 224 | break; | ||
| 225 | case 's': | ||
| 226 | mode = HCTOSYS; | ||
| 227 | break; | ||
| 228 | case 'u': | ||
| 229 | utc = 1; | ||
| 230 | utc_arg = 1; | ||
| 231 | break; | ||
| 232 | case 'l': // -l is not supported by the normal hwclock (only --localtime) | ||
| 233 | utc = 0; | ||
| 234 | utc_arg = 1; | ||
| 235 | break; | ||
| 236 | default: | ||
| 237 | bb_show_usage(); | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | } | 213 | } |
| 241 | 214 | ||
| 242 | if ( !utc_arg ) | 215 | /* If -u or -l wasnt give check if we are using utc */ |
| 243 | utc = check_utc ( ); | 216 | if ((opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) == 0) { |
| 244 | 217 | utc = check_utc(); | |
| 245 | switch ( mode ) { | 218 | } |
| 246 | case SYSTOHC: | ||
| 247 | return from_sys_clock ( utc ); | ||
| 248 | 219 | ||
| 249 | case HCTOSYS: | 220 | if (opt & HWCLOCK_OPT_HCTOSYS) { |
| 250 | return to_sys_clock ( utc ); | 221 | return to_sys_clock ( utc ); |
| 251 | 222 | } | |
| 252 | case SHOW: | 223 | else if (opt & HWCLOCK_OPT_SYSTOHC) { |
| 253 | default: | 224 | return from_sys_clock ( utc ); |
| 225 | } else { | ||
| 226 | /* default HWCLOCK_OPT_SHOW */ | ||
| 254 | return show_clock ( utc ); | 227 | return show_clock ( utc ); |
| 255 | } | 228 | } |
| 256 | } | 229 | } |
