diff options
author | Mike Frysinger <vapier@gentoo.org> | 2008-02-15 02:27:19 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2008-02-15 02:27:19 +0000 |
commit | 6b160e490d4d77596c1603d34d0a1ca0579a82da (patch) | |
tree | 653ab55714a0373751b619144e56b5ea163938f2 /libbb | |
parent | be7d2a8ded621a6d62f8caa76f7c51185b166ab1 (diff) | |
download | busybox-w32-6b160e490d4d77596c1603d34d0a1ca0579a82da.tar.gz busybox-w32-6b160e490d4d77596c1603d34d0a1ca0579a82da.tar.bz2 busybox-w32-6b160e490d4d77596c1603d34d0a1ca0579a82da.zip |
split some rtc funcs out of hwclock and into an rtc header/lib so that the new rtcwake applet as well as hwclock can utilize the same code
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild | 2 | ||||
-rw-r--r-- | libbb/rtc.c | 86 |
2 files changed, 88 insertions, 0 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild index c4aac95bb..2fb1b2420 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
@@ -115,6 +115,8 @@ lib-$(CONFIG_LOGIN) += correct_password.o | |||
115 | lib-$(CONFIG_DF) += find_mount_point.o | 115 | lib-$(CONFIG_DF) += find_mount_point.o |
116 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o | 116 | lib-$(CONFIG_MKFS_MINIX) += find_mount_point.o |
117 | lib-$(CONFIG_SELINUX) += selinux_common.o | 117 | lib-$(CONFIG_SELINUX) += selinux_common.o |
118 | lib-$(CONFIG_HWCLOCK) += rtc.o | ||
119 | lib-$(CONFIG_RTCWAKE) += rtc.o | ||
118 | 120 | ||
119 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't | 121 | # We shouldn't build xregcomp.c if we don't need it - this ensures we don't |
120 | # require regex.h to be in the include dir even if we don't need it thereby | 122 | # require regex.h to be in the include dir even if we don't need it thereby |
diff --git a/libbb/rtc.c b/libbb/rtc.c new file mode 100644 index 000000000..4cbf32206 --- /dev/null +++ b/libbb/rtc.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * Common RTC functions | ||
3 | */ | ||
4 | |||
5 | #include "libbb.h" | ||
6 | #include "rtc_.h" | ||
7 | |||
8 | #if ENABLE_FEATURE_HWCLOCK_ADJTIME_FHS | ||
9 | # define ADJTIME_PATH "/var/lib/hwclock/adjtime" | ||
10 | #else | ||
11 | # define ADJTIME_PATH "/etc/adjtime" | ||
12 | #endif | ||
13 | |||
14 | int rtc_adjtime_is_utc(void) | ||
15 | { | ||
16 | int utc = 0; | ||
17 | FILE *f = fopen(ADJTIME_PATH, "r"); | ||
18 | |||
19 | if (f) { | ||
20 | RESERVE_CONFIG_BUFFER(buffer, 128); | ||
21 | |||
22 | while (fgets(buffer, sizeof(buffer), f)) { | ||
23 | int len = strlen(buffer); | ||
24 | |||
25 | while (len && isspace(buffer[len - 1])) | ||
26 | len--; | ||
27 | |||
28 | buffer[len] = 0; | ||
29 | |||
30 | if (strncmp(buffer, "UTC", 3) == 0) { | ||
31 | utc = 1; | ||
32 | break; | ||
33 | } | ||
34 | } | ||
35 | fclose(f); | ||
36 | |||
37 | RELEASE_CONFIG_BUFFER(buffer); | ||
38 | } | ||
39 | |||
40 | return utc; | ||
41 | } | ||
42 | |||
43 | int rtc_xopen(const char *default_rtc, int flags) | ||
44 | { | ||
45 | int rtc; | ||
46 | |||
47 | if (!default_rtc) { | ||
48 | rtc = open("/dev/rtc", flags); | ||
49 | if (rtc >= 0) | ||
50 | return rtc; | ||
51 | rtc = open("/dev/rtc0", flags); | ||
52 | if (rtc >= 0) | ||
53 | return rtc; | ||
54 | default_rtc = "/dev/misc/rtc"; | ||
55 | } | ||
56 | |||
57 | return xopen(default_rtc, flags); | ||
58 | } | ||
59 | |||
60 | time_t rtc_read_time(int fd, int utc) | ||
61 | { | ||
62 | struct tm tm; | ||
63 | char *oldtz = 0; | ||
64 | time_t t = 0; | ||
65 | |||
66 | memset(&tm, 0, sizeof(struct tm)); | ||
67 | xioctl(fd, RTC_RD_TIME, &tm); | ||
68 | tm.tm_isdst = -1; /* not known */ | ||
69 | |||
70 | if (utc) { | ||
71 | oldtz = getenv("TZ"); | ||
72 | putenv((char*)"TZ=UTC0"); | ||
73 | tzset(); | ||
74 | } | ||
75 | |||
76 | t = mktime(&tm); | ||
77 | |||
78 | if (utc) { | ||
79 | unsetenv("TZ"); | ||
80 | if (oldtz) | ||
81 | putenv(oldtz - 3); | ||
82 | tzset(); | ||
83 | } | ||
84 | |||
85 | return t; | ||
86 | } | ||