2008-02-15 07:57:19 +05:30
|
|
|
/*
|
|
|
|
* Common RTC functions
|
2008-12-07 06:22:58 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-02-15 07:57:19 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "rtc_.h"
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_HWCLOCK_ADJTIME_FHS
|
|
|
|
# define ADJTIME_PATH "/var/lib/hwclock/adjtime"
|
|
|
|
#else
|
|
|
|
# define ADJTIME_PATH "/etc/adjtime"
|
|
|
|
#endif
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC rtc_adjtime_is_utc(void)
|
2008-02-15 07:57:19 +05:30
|
|
|
{
|
|
|
|
int utc = 0;
|
2008-07-22 04:35:26 +05:30
|
|
|
FILE *f = fopen_for_read(ADJTIME_PATH);
|
2008-02-15 07:57:19 +05:30
|
|
|
|
|
|
|
if (f) {
|
2010-01-06 22:46:39 +05:30
|
|
|
char buffer[128];
|
2008-02-15 07:57:19 +05:30
|
|
|
|
|
|
|
while (fgets(buffer, sizeof(buffer), f)) {
|
|
|
|
if (strncmp(buffer, "UTC", 3) == 0) {
|
|
|
|
utc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return utc;
|
|
|
|
}
|
|
|
|
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
|
2008-02-15 07:57:19 +05:30
|
|
|
{
|
|
|
|
int rtc;
|
|
|
|
|
2008-02-15 12:49:03 +05:30
|
|
|
if (!*default_rtc) {
|
|
|
|
*default_rtc = "/dev/rtc";
|
|
|
|
rtc = open(*default_rtc, flags);
|
2008-02-15 07:57:19 +05:30
|
|
|
if (rtc >= 0)
|
|
|
|
return rtc;
|
2008-02-15 12:49:03 +05:30
|
|
|
*default_rtc = "/dev/rtc0";
|
|
|
|
rtc = open(*default_rtc, flags);
|
2008-02-15 07:57:19 +05:30
|
|
|
if (rtc >= 0)
|
|
|
|
return rtc;
|
2008-02-15 12:49:03 +05:30
|
|
|
*default_rtc = "/dev/misc/rtc";
|
2008-02-15 07:57:19 +05:30
|
|
|
}
|
|
|
|
|
2008-02-15 12:49:03 +05:30
|
|
|
return xopen(*default_rtc, flags);
|
2008-02-15 07:57:19 +05:30
|
|
|
}
|
|
|
|
|
2010-01-09 23:40:49 +05:30
|
|
|
void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
|
2008-02-15 07:57:19 +05:30
|
|
|
{
|
2010-01-09 23:40:49 +05:30
|
|
|
memset(ptm, 0, sizeof(*ptm));
|
|
|
|
xioctl(fd, RTC_RD_TIME, ptm);
|
|
|
|
ptm->tm_isdst = -1; /* "not known" */
|
2010-01-07 03:13:39 +05:30
|
|
|
}
|
2008-02-15 07:57:19 +05:30
|
|
|
|
2010-01-09 23:40:49 +05:30
|
|
|
time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
|
2010-01-07 03:13:39 +05:30
|
|
|
{
|
|
|
|
char *oldtz = oldtz; /* for compiler */
|
|
|
|
time_t t;
|
2008-02-15 07:57:19 +05:30
|
|
|
|
|
|
|
if (utc) {
|
|
|
|
oldtz = getenv("TZ");
|
|
|
|
putenv((char*)"TZ=UTC0");
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
|
2010-01-09 23:40:49 +05:30
|
|
|
t = mktime(ptm);
|
2008-02-15 07:57:19 +05:30
|
|
|
|
|
|
|
if (utc) {
|
|
|
|
unsetenv("TZ");
|
|
|
|
if (oldtz)
|
|
|
|
putenv(oldtz - 3);
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|