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