blob: a03d61bbf0d307038b71b0906cc2af91b0b6451e [file] [log] [blame]
Robert Griebl1cd04452002-07-21 16:50:49 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Griebl6859d762002-08-05 02:57:12 +00003 * Mini hwclock implementation for busybox
4 *
Robert Griebl1cd04452002-07-21 16:50:49 +00005 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6 *
Bernhard Reutner-Fischer5cf905a2006-03-31 22:36:15 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Robert Griebl6859d762002-08-05 02:57:12 +00008*/
Robert Griebl1cd04452002-07-21 16:50:49 +00009
Robert Griebl1cd04452002-07-21 16:50:49 +000010#include <sys/utsname.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Mike Frysinger6b160e42008-02-15 02:27:19 +000012#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000013
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000014#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Robert Griebl1cd04452002-07-21 16:50:49 +000015# ifndef _GNU_SOURCE
16# define _GNU_SOURCE
17# endif
18#endif
19
Denis Vlasenko673d4bb2007-03-07 23:02:50 +000020static const char *rtcname;
21
Denis Vlasenko92258542006-11-01 10:25:35 +000022static time_t read_rtc(int utc)
23{
Mike Frysinger6b160e42008-02-15 02:27:19 +000024 time_t ret;
25 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000026
Mike Frysinger977bc6a2008-02-15 07:19:03 +000027 fd = rtc_xopen(&rtcname, O_RDONLY);
Mike Frysinger6b160e42008-02-15 02:27:19 +000028 ret = rtc_read_time(fd, utc);
29 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000030
Mike Frysinger6b160e42008-02-15 02:27:19 +000031 return ret;
Robert Griebl1cd04452002-07-21 16:50:49 +000032}
33
Glenn L McGrath689e4b92004-02-22 09:11:33 +000034static void write_rtc(time_t t, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000035{
Robert Griebl1cd04452002-07-21 16:50:49 +000036 struct tm tm;
Mike Frysinger977bc6a2008-02-15 07:19:03 +000037 int rtc = rtc_xopen(&rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +000038
Denis Vlasenko92258542006-11-01 10:25:35 +000039 tm = *(utc ? gmtime(&t) : localtime(&t));
Bernhard Reutner-Fischer5cf905a2006-03-31 22:36:15 +000040 tm.tm_isdst = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000042 xioctl(rtc, RTC_SET_TIME, &tm);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043
Denis Vlasenko92258542006-11-01 10:25:35 +000044 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +000045}
46
Denis Vlasenko459be352007-06-17 19:09:05 +000047static void show_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000048{
Denis Vlasenko459be352007-06-17 19:09:05 +000049 //struct tm *ptm;
Robert Griebl1cd04452002-07-21 16:50:49 +000050 time_t t;
Denis Vlasenko459be352007-06-17 19:09:05 +000051 char *cp;
Robert Griebl1cd04452002-07-21 16:50:49 +000052
Denis Vlasenko92258542006-11-01 10:25:35 +000053 t = read_rtc(utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000054 //ptm = localtime(&t); /* Sets 'tzname[]' */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000055
Denis Vlasenko459be352007-06-17 19:09:05 +000056 cp = ctime(&t);
57 if (cp[0])
58 cp[strlen(cp) - 1] = '\0';
Eric Andersenc7bda1c2004-03-15 08:29:22 +000059
Denis Vlasenko459be352007-06-17 19:09:05 +000060 //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 Griebl1cd04452002-07-21 16:50:49 +000062}
63
Denis Vlasenko459be352007-06-17 19:09:05 +000064static void to_sys_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000065{
Denis Vlasenko459be352007-06-17 19:09:05 +000066 struct timeval tv;
Robert Griebl1cd04452002-07-21 16:50:49 +000067 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
Eric Andersenc7bda1c2004-03-15 08:29:22 +000068
Denis Vlasenko92258542006-11-01 10:25:35 +000069 tv.tv_sec = read_rtc(utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000070 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +000071 if (settimeofday(&tv, &tz))
72 bb_perror_msg_and_die("settimeofday() failed");
Robert Griebl1cd04452002-07-21 16:50:49 +000073}
74
Denis Vlasenko459be352007-06-17 19:09:05 +000075static void from_sys_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000076{
Denis Vlasenko459be352007-06-17 19:09:05 +000077 struct timeval tv;
Robert Griebl1cd04452002-07-21 16:50:49 +000078
Denis Vlasenko459be352007-06-17 19:09:05 +000079 gettimeofday(&tv, NULL);
80 //if (gettimeofday(&tv, NULL))
81 // bb_perror_msg_and_die("gettimeofday() failed");
Denis Vlasenko92258542006-11-01 10:25:35 +000082 write_rtc(tv.tv_sec, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000083}
84
Denis Vlasenko92258542006-11-01 10:25:35 +000085#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 Vlasenko673d4bb2007-03-07 23:02:50 +000090#define HWCLOCK_OPT_RTCFILE 0x20
Glenn L McGrath689e4b92004-02-22 09:11:33 +000091
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000092int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000093int hwclock_main(int argc ATTRIBUTE_UNUSED, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +000094{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000095 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +000096 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +000097
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000098#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000099 static const char hwclock_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000100 "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 Vlasenko990d0f62007-07-24 15:54:42 +0000106 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000107 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000108#endif
Denis Vlasenko09196572007-07-21 13:27:44 +0000109 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000110 opt = getopt32(argv, "lurswf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000111
Robert Griebl6bb80872004-03-22 21:27:39 +0000112 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000113 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000114 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000115 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000116 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000117
Mike Frysinger6b160e42008-02-15 02:27:19 +0000118 if (opt & HWCLOCK_OPT_HCTOSYS)
Denis Vlasenko459be352007-06-17 19:09:05 +0000119 to_sys_clock(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000120 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denis Vlasenko459be352007-06-17 19:09:05 +0000121 from_sys_clock(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000122 else
123 /* default HWCLOCK_OPT_SHOW */
124 show_clock(utc);
125
Denis Vlasenko459be352007-06-17 19:09:05 +0000126 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000127}