Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 3 | * Mini hwclock implementation for busybox |
| 4 | * |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 5 | * Copyright (C) 2002 Robert Griebl <griebl@gmx.de> |
| 6 | * |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 8 | */ |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 9 | |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 10 | #include <sys/utsname.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 12 | #include "rtc_.h" |
Eric Andersen | 8882ea5 | 2002-12-11 03:41:28 +0000 | [diff] [blame] | 13 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 14 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 15 | # ifndef _GNU_SOURCE |
| 16 | # define _GNU_SOURCE |
| 17 | # endif |
| 18 | #endif |
| 19 | |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 20 | static const char *rtcname; |
| 21 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 22 | static time_t read_rtc(int utc) |
| 23 | { |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 24 | time_t ret; |
| 25 | int fd; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 26 | |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 27 | fd = rtc_xopen(&rtcname, O_RDONLY); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 28 | ret = rtc_read_time(fd, utc); |
| 29 | close(fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 31 | return ret; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 34 | static void write_rtc(time_t t, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 35 | { |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 36 | struct tm tm; |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 37 | int rtc = rtc_xopen(&rtcname, O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 39 | tm = *(utc ? gmtime(&t) : localtime(&t)); |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 40 | tm.tm_isdst = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 42 | xioctl(rtc, RTC_SET_TIME, &tm); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 44 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 47 | static void show_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 49 | //struct tm *ptm; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 50 | time_t t; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 51 | char *cp; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 53 | t = read_rtc(utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 54 | //ptm = localtime(&t); /* Sets 'tzname[]' */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 56 | cp = ctime(&t); |
| 57 | if (cp[0]) |
| 58 | cp[strlen(cp) - 1] = '\0'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 60 | //printf("%s %.6f seconds %s\n", cp, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0])); |
| 61 | printf("%s 0.000000 seconds\n", cp); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 64 | static void to_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 65 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 66 | struct timeval tv; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 67 | const struct timezone tz = { timezone/60 - 60*daylight, 0 }; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 69 | tv.tv_sec = read_rtc(utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 70 | tv.tv_usec = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 71 | if (settimeofday(&tv, &tz)) |
| 72 | bb_perror_msg_and_die("settimeofday() failed"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 75 | static void from_sys_clock(int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 76 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 77 | struct timeval tv; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 79 | gettimeofday(&tv, NULL); |
| 80 | //if (gettimeofday(&tv, NULL)) |
| 81 | // bb_perror_msg_and_die("gettimeofday() failed"); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 82 | write_rtc(tv.tv_sec, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 85 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 86 | #define HWCLOCK_OPT_UTC 0x02 |
| 87 | #define HWCLOCK_OPT_SHOW 0x04 |
| 88 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 89 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 90 | #define HWCLOCK_OPT_RTCFILE 0x20 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 92 | int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 93 | int hwclock_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 94 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 95 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 96 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 97 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 98 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 99 | static const char hwclock_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 100 | "localtime\0" No_argument "l" |
| 101 | "utc\0" No_argument "u" |
| 102 | "show\0" No_argument "r" |
| 103 | "hctosys\0" No_argument "s" |
| 104 | "systohc\0" No_argument "w" |
| 105 | "file\0" Required_argument "f" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 106 | ; |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 107 | applet_long_options = hwclock_longopts; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 108 | #endif |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 109 | opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 110 | opt = getopt32(argv, "lurswf:", &rtcname); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 111 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 112 | /* If -u or -l wasn't given check if we are using utc */ |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 113 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 114 | utc = (opt & HWCLOCK_OPT_UTC); |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 115 | else |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 116 | utc = rtc_adjtime_is_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 117 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 118 | if (opt & HWCLOCK_OPT_HCTOSYS) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 119 | to_sys_clock(utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 120 | else if (opt & HWCLOCK_OPT_SYSTOHC) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 121 | from_sys_clock(utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 122 | else |
| 123 | /* default HWCLOCK_OPT_SHOW */ |
| 124 | show_clock(utc); |
| 125 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 126 | return 0; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 127 | } |