blob: 97455e86ae187a359e7dbc4e94f846406bb19cfd [file] [log] [blame]
Mike Frysinger6b160e42008-02-15 02:27:19 +00001/*
2 * Common RTC functions
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Mike Frysinger6b160e42008-02-15 02:27:19 +00005 */
6
7#include "libbb.h"
8#include "rtc_.h"
9
10#if ENABLE_FEATURE_HWCLOCK_ADJTIME_FHS
11# define ADJTIME_PATH "/var/lib/hwclock/adjtime"
12#else
13# define ADJTIME_PATH "/etc/adjtime"
14#endif
15
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000016int FAST_FUNC rtc_adjtime_is_utc(void)
Mike Frysinger6b160e42008-02-15 02:27:19 +000017{
18 int utc = 0;
Denis Vlasenko5415c852008-07-21 23:05:26 +000019 FILE *f = fopen_for_read(ADJTIME_PATH);
Mike Frysinger6b160e42008-02-15 02:27:19 +000020
21 if (f) {
Denys Vlasenko695fa512010-01-06 18:16:39 +010022 char buffer[128];
Mike Frysinger6b160e42008-02-15 02:27:19 +000023
24 while (fgets(buffer, sizeof(buffer), f)) {
Mike Frysinger6b160e42008-02-15 02:27:19 +000025 if (strncmp(buffer, "UTC", 3) == 0) {
26 utc = 1;
27 break;
28 }
29 }
30 fclose(f);
Mike Frysinger6b160e42008-02-15 02:27:19 +000031 }
32
33 return utc;
34}
35
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000036int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
Mike Frysinger6b160e42008-02-15 02:27:19 +000037{
38 int rtc;
39
Mike Frysinger977bc6a2008-02-15 07:19:03 +000040 if (!*default_rtc) {
41 *default_rtc = "/dev/rtc";
42 rtc = open(*default_rtc, flags);
Mike Frysinger6b160e42008-02-15 02:27:19 +000043 if (rtc >= 0)
44 return rtc;
Mike Frysinger977bc6a2008-02-15 07:19:03 +000045 *default_rtc = "/dev/rtc0";
46 rtc = open(*default_rtc, flags);
Mike Frysinger6b160e42008-02-15 02:27:19 +000047 if (rtc >= 0)
48 return rtc;
Mike Frysinger977bc6a2008-02-15 07:19:03 +000049 *default_rtc = "/dev/misc/rtc";
Mike Frysinger6b160e42008-02-15 02:27:19 +000050 }
51
Mike Frysinger977bc6a2008-02-15 07:19:03 +000052 return xopen(*default_rtc, flags);
Mike Frysinger6b160e42008-02-15 02:27:19 +000053}
54
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010055void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
Mike Frysinger6b160e42008-02-15 02:27:19 +000056{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010057 memset(ptm, 0, sizeof(*ptm));
58 xioctl(fd, RTC_RD_TIME, ptm);
59 ptm->tm_isdst = -1; /* "not known" */
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010060}
Mike Frysinger6b160e42008-02-15 02:27:19 +000061
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010062time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010063{
64 char *oldtz = oldtz; /* for compiler */
65 time_t t;
Mike Frysinger6b160e42008-02-15 02:27:19 +000066
67 if (utc) {
68 oldtz = getenv("TZ");
69 putenv((char*)"TZ=UTC0");
70 tzset();
71 }
72
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010073 t = mktime(ptm);
Mike Frysinger6b160e42008-02-15 02:27:19 +000074
75 if (utc) {
76 unsetenv("TZ");
77 if (oldtz)
78 putenv(oldtz - 3);
79 tzset();
80 }
81
82 return t;
83}