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