blob: f226535fa4e89bc17317cfe0948e425cdcce5716 [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>
Glenn L McGrath689e4b92004-02-22 09:11:33 +000011#include <getopt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Mike Frysinger6b160e42008-02-15 02:27:19 +000013#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000014
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000015#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Robert Griebl1cd04452002-07-21 16:50:49 +000016# ifndef _GNU_SOURCE
17# define _GNU_SOURCE
18# endif
19#endif
20
Denis Vlasenko673d4bb2007-03-07 23:02:50 +000021static const char *rtcname;
22
Denis Vlasenko92258542006-11-01 10:25:35 +000023static time_t read_rtc(int utc)
24{
Mike Frysinger6b160e42008-02-15 02:27:19 +000025 time_t ret;
26 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000027
Mike Frysinger977bc6a2008-02-15 07:19:03 +000028 fd = rtc_xopen(&rtcname, O_RDONLY);
Mike Frysinger6b160e42008-02-15 02:27:19 +000029 ret = rtc_read_time(fd, utc);
30 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000031
Mike Frysinger6b160e42008-02-15 02:27:19 +000032 return ret;
Robert Griebl1cd04452002-07-21 16:50:49 +000033}
34
Glenn L McGrath689e4b92004-02-22 09:11:33 +000035static void write_rtc(time_t t, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000036{
Robert Griebl1cd04452002-07-21 16:50:49 +000037 struct tm tm;
Mike Frysinger977bc6a2008-02-15 07:19:03 +000038 int rtc = rtc_xopen(&rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +000039
Denis Vlasenko92258542006-11-01 10:25:35 +000040 tm = *(utc ? gmtime(&t) : localtime(&t));
Bernhard Reutner-Fischer5cf905a2006-03-31 22:36:15 +000041 tm.tm_isdst = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000042
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000043 xioctl(rtc, RTC_SET_TIME, &tm);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000044
Denis Vlasenko92258542006-11-01 10:25:35 +000045 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +000046}
47
Denis Vlasenko459be352007-06-17 19:09:05 +000048static void show_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000049{
Denis Vlasenko459be352007-06-17 19:09:05 +000050 //struct tm *ptm;
Robert Griebl1cd04452002-07-21 16:50:49 +000051 time_t t;
Denis Vlasenko459be352007-06-17 19:09:05 +000052 char *cp;
Robert Griebl1cd04452002-07-21 16:50:49 +000053
Denis Vlasenko92258542006-11-01 10:25:35 +000054 t = read_rtc(utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000055 //ptm = localtime(&t); /* Sets 'tzname[]' */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056
Denis Vlasenko459be352007-06-17 19:09:05 +000057 cp = ctime(&t);
58 if (cp[0])
59 cp[strlen(cp) - 1] = '\0';
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060
Denis Vlasenko459be352007-06-17 19:09:05 +000061 //printf("%s %.6f seconds %s\n", cp, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
62 printf("%s 0.000000 seconds\n", cp);
Robert Griebl1cd04452002-07-21 16:50:49 +000063}
64
Denis Vlasenko459be352007-06-17 19:09:05 +000065static void to_sys_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000066{
Denis Vlasenko459be352007-06-17 19:09:05 +000067 struct timeval tv;
Robert Griebl1cd04452002-07-21 16:50:49 +000068 const struct timezone tz = { timezone/60 - 60*daylight, 0 };
Eric Andersenc7bda1c2004-03-15 08:29:22 +000069
Denis Vlasenko92258542006-11-01 10:25:35 +000070 tv.tv_sec = read_rtc(utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000071 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +000072 if (settimeofday(&tv, &tz))
73 bb_perror_msg_and_die("settimeofday() failed");
Robert Griebl1cd04452002-07-21 16:50:49 +000074}
75
Denis Vlasenko459be352007-06-17 19:09:05 +000076static void from_sys_clock(int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000077{
Denis Vlasenko459be352007-06-17 19:09:05 +000078 struct timeval tv;
Robert Griebl1cd04452002-07-21 16:50:49 +000079
Denis Vlasenko459be352007-06-17 19:09:05 +000080 gettimeofday(&tv, NULL);
81 //if (gettimeofday(&tv, NULL))
82 // bb_perror_msg_and_die("gettimeofday() failed");
Denis Vlasenko92258542006-11-01 10:25:35 +000083 write_rtc(tv.tv_sec, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000084}
85
Denis Vlasenko92258542006-11-01 10:25:35 +000086#define HWCLOCK_OPT_LOCALTIME 0x01
87#define HWCLOCK_OPT_UTC 0x02
88#define HWCLOCK_OPT_SHOW 0x04
89#define HWCLOCK_OPT_HCTOSYS 0x08
90#define HWCLOCK_OPT_SYSTOHC 0x10
Denis Vlasenko673d4bb2007-03-07 23:02:50 +000091#define HWCLOCK_OPT_RTCFILE 0x20
Glenn L McGrath689e4b92004-02-22 09:11:33 +000092
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000093int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko459be352007-06-17 19:09:05 +000094int hwclock_main(int argc, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +000095{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000096 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +000097 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +000098
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000099#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000100 static const char hwclock_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000101 "localtime\0" No_argument "l"
102 "utc\0" No_argument "u"
103 "show\0" No_argument "r"
104 "hctosys\0" No_argument "s"
105 "systohc\0" No_argument "w"
106 "file\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000107 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000108 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000109#endif
Denis Vlasenko09196572007-07-21 13:27:44 +0000110 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000111 opt = getopt32(argv, "lurswf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000112
Robert Griebl6bb80872004-03-22 21:27:39 +0000113 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000114 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000115 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000116 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000117 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000118
Mike Frysinger6b160e42008-02-15 02:27:19 +0000119 if (opt & HWCLOCK_OPT_HCTOSYS)
Denis Vlasenko459be352007-06-17 19:09:05 +0000120 to_sys_clock(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000121 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denis Vlasenko459be352007-06-17 19:09:05 +0000122 from_sys_clock(utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000123 else
124 /* default HWCLOCK_OPT_SHOW */
125 show_clock(utc);
126
Denis Vlasenko459be352007-06-17 19:09:05 +0000127 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000128}