delete tons of extra #includes
[platform/upstream/busybox.git] / util-linux / hwclock.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini hwclock implementation for busybox
4  *
5  * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10 //#include <sys/ioctl.h>
11 #include <sys/utsname.h>
12 #include <getopt.h>
13 #include "libbb.h"
14
15 /* Copied from linux/rtc.h to eliminate the kernel dependency */
16 struct linux_rtc_time {
17         int tm_sec;
18         int tm_min;
19         int tm_hour;
20         int tm_mday;
21         int tm_mon;
22         int tm_year;
23         int tm_wday;
24         int tm_yday;
25         int tm_isdst;
26 };
27
28 #define RTC_SET_TIME   _IOW('p', 0x0a, struct linux_rtc_time) /* Set RTC time    */
29 #define RTC_RD_TIME    _IOR('p', 0x09, struct linux_rtc_time) /* Read RTC time   */
30
31 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
32 # ifndef _GNU_SOURCE
33 #  define _GNU_SOURCE
34 # endif
35 #endif
36
37 static const char *rtcname;
38
39 static int xopen_rtc(int flags)
40 {
41         int rtc;
42
43         if (!rtcname) {
44                 rtc = open("/dev/rtc", flags);
45                 if (rtc >= 0)
46                         return rtc;
47                 rtc = open("/dev/rtc0", flags);
48                 if (rtc >= 0)
49                         return rtc;
50                 rtcname = "/dev/misc/rtc";
51         }
52         return xopen(rtcname, flags);
53 }
54
55 static time_t read_rtc(int utc)
56 {
57         struct tm tm;
58         char *oldtz = 0;
59         time_t t = 0;
60         int rtc = xopen_rtc(O_RDONLY);
61
62         memset(&tm, 0, sizeof(struct tm));
63         if (ioctl(rtc, RTC_RD_TIME, &tm) < 0)
64                 bb_perror_msg_and_die("cannot read time from RTC");
65         tm.tm_isdst = -1; /* not known */
66
67         close(rtc);
68
69         if (utc) {
70                 oldtz = getenv("TZ");
71                 setenv("TZ", "UTC 0", 1);
72                 tzset();
73         }
74
75         t = mktime(&tm);
76
77         if (utc) {
78                 if (oldtz)
79                         setenv("TZ", oldtz, 1);
80                 else
81                         unsetenv("TZ");
82                 tzset();
83         }
84         return t;
85 }
86
87 static void write_rtc(time_t t, int utc)
88 {
89         struct tm tm;
90         int rtc = xopen_rtc(O_WRONLY);
91
92         tm = *(utc ? gmtime(&t) : localtime(&t));
93         tm.tm_isdst = 0;
94
95         if (ioctl(rtc, RTC_SET_TIME, &tm) < 0)
96                 bb_perror_msg_and_die("cannot set the RTC time");
97
98         close(rtc);
99 }
100
101 static int show_clock(int utc)
102 {
103         struct tm *ptm;
104         time_t t;
105         RESERVE_CONFIG_BUFFER(buffer, 64);
106
107         t = read_rtc(utc);
108         ptm = localtime(&t);  /* Sets 'tzname[]' */
109
110         safe_strncpy(buffer, ctime(&t), 64);
111         if (buffer[0])
112                 buffer[strlen(buffer) - 1] = 0;
113
114         //printf("%s  %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0]));
115         printf( "%s  %.6f seconds\n", buffer, 0.0);
116         RELEASE_CONFIG_BUFFER(buffer);
117
118         return 0;
119 }
120
121 static int to_sys_clock(int utc)
122 {
123         struct timeval tv = { 0, 0 };
124         const struct timezone tz = { timezone/60 - 60*daylight, 0 };
125
126         tv.tv_sec = read_rtc(utc);
127
128         if (settimeofday(&tv, &tz))
129                 bb_perror_msg_and_die("settimeofday() failed");
130
131         return 0;
132 }
133
134 static int from_sys_clock(int utc)
135 {
136         struct timeval tv = { 0, 0 };
137         struct timezone tz = { 0, 0 };
138
139         if (gettimeofday(&tv, &tz))
140                 bb_perror_msg_and_die("gettimeofday() failed");
141
142         write_rtc(tv.tv_sec, utc);
143         return 0;
144 }
145
146 #ifdef CONFIG_FEATURE_HWCLOCK_ADJTIME_FHS
147 # define ADJTIME_PATH "/var/lib/hwclock/adjtime"
148 #else
149 # define ADJTIME_PATH "/etc/adjtime"
150 #endif
151 static int check_utc(void)
152 {
153         int utc = 0;
154         FILE *f = fopen(ADJTIME_PATH, "r");
155
156         if (f) {
157                 RESERVE_CONFIG_BUFFER(buffer, 128);
158
159                 while (fgets(buffer, sizeof(buffer), f)) {
160                         int len = strlen(buffer);
161
162                         while (len && isspace(buffer[len - 1]))
163                                 len--;
164
165                         buffer[len] = 0;
166
167                         if (strncmp(buffer, "UTC", 3) == 0) {
168                                 utc = 1;
169                                 break;
170                         }
171                 }
172                 fclose(f);
173                 RELEASE_CONFIG_BUFFER(buffer);
174         }
175         return utc;
176 }
177
178 #define HWCLOCK_OPT_LOCALTIME   0x01
179 #define HWCLOCK_OPT_UTC         0x02
180 #define HWCLOCK_OPT_SHOW        0x04
181 #define HWCLOCK_OPT_HCTOSYS     0x08
182 #define HWCLOCK_OPT_SYSTOHC     0x10
183 #define HWCLOCK_OPT_RTCFILE     0x20
184
185 int hwclock_main(int argc, char **argv );
186 int hwclock_main(int argc, char **argv )
187 {
188         unsigned opt;
189         int utc;
190
191 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
192         static const struct option hwclock_long_options[] = {
193                 { "localtime", 0, 0, 'l' },
194                 { "utc",       0, 0, 'u' },
195                 { "show",      0, 0, 'r' },
196                 { "hctosys",   0, 0, 's' },
197                 { "systohc",   0, 0, 'w' },
198                 { "file",      1, 0, 'f' },
199                 { 0,           0, 0, 0 }
200         };
201         applet_long_options = hwclock_long_options;
202 #endif
203         opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l";
204         opt = getopt32(argc, argv, "lurswf:", &rtcname);
205
206         /* If -u or -l wasn't given check if we are using utc */
207         if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
208                 utc = opt & HWCLOCK_OPT_UTC;
209         else
210                 utc = check_utc();
211
212         if (opt & HWCLOCK_OPT_HCTOSYS) {
213                 return to_sys_clock(utc);
214         }
215         else if (opt & HWCLOCK_OPT_SYSTOHC) {
216                 return from_sys_clock(utc);
217         } else {
218                 /* default HWCLOCK_OPT_SHOW */
219                 return show_clock(utc);
220         }
221 }