2008-02-15 02:27:19 +00:00
|
|
|
/*
|
|
|
|
* Common RTC functions
|
2008-12-07 00:52:58 +00:00
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
2008-02-15 02:27:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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 02:52:20 +00:00
|
|
|
int FAST_FUNC rtc_adjtime_is_utc(void)
|
2008-02-15 02:27:19 +00:00
|
|
|
{
|
|
|
|
int utc = 0;
|
2008-07-21 23:05:26 +00:00
|
|
|
FILE *f = fopen_for_read(ADJTIME_PATH);
|
2008-02-15 02:27:19 +00:00
|
|
|
|
|
|
|
if (f) {
|
2010-01-06 17:16:39 +00:00
|
|
|
char buffer[128];
|
2008-02-15 02:27:19 +00:00
|
|
|
|
|
|
|
while (fgets(buffer, sizeof(buffer), f)) {
|
|
|
|
int len = strlen(buffer);
|
|
|
|
|
|
|
|
while (len && isspace(buffer[len - 1]))
|
|
|
|
len--;
|
|
|
|
|
|
|
|
buffer[len] = 0;
|
|
|
|
|
|
|
|
if (strncmp(buffer, "UTC", 3) == 0) {
|
|
|
|
utc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return utc;
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
|
2008-02-15 02:27:19 +00:00
|
|
|
{
|
|
|
|
int rtc;
|
|
|
|
|
2008-02-15 07:19:03 +00:00
|
|
|
if (!*default_rtc) {
|
|
|
|
*default_rtc = "/dev/rtc";
|
|
|
|
rtc = open(*default_rtc, flags);
|
2008-02-15 02:27:19 +00:00
|
|
|
if (rtc >= 0)
|
|
|
|
return rtc;
|
2008-02-15 07:19:03 +00:00
|
|
|
*default_rtc = "/dev/rtc0";
|
|
|
|
rtc = open(*default_rtc, flags);
|
2008-02-15 02:27:19 +00:00
|
|
|
if (rtc >= 0)
|
|
|
|
return rtc;
|
2008-02-15 07:19:03 +00:00
|
|
|
*default_rtc = "/dev/misc/rtc";
|
2008-02-15 02:27:19 +00:00
|
|
|
}
|
|
|
|
|
2008-02-15 07:19:03 +00:00
|
|
|
return xopen(*default_rtc, flags);
|
2008-02-15 02:27:19 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 18:10:49 +00:00
|
|
|
void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
|
2008-02-15 02:27:19 +00:00
|
|
|
{
|
2010-01-09 18:10:49 +00:00
|
|
|
memset(ptm, 0, sizeof(*ptm));
|
|
|
|
xioctl(fd, RTC_RD_TIME, ptm);
|
|
|
|
ptm->tm_isdst = -1; /* "not known" */
|
2010-01-06 21:43:39 +00:00
|
|
|
}
|
2008-02-15 02:27:19 +00:00
|
|
|
|
2010-01-09 18:10:49 +00:00
|
|
|
time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
|
2010-01-06 21:43:39 +00:00
|
|
|
{
|
|
|
|
char *oldtz = oldtz; /* for compiler */
|
|
|
|
time_t t;
|
2008-02-15 02:27:19 +00:00
|
|
|
|
|
|
|
if (utc) {
|
|
|
|
oldtz = getenv("TZ");
|
|
|
|
putenv((char*)"TZ=UTC0");
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
|
2010-01-09 18:10:49 +00:00
|
|
|
t = mktime(ptm);
|
2008-02-15 02:27:19 +00:00
|
|
|
|
|
|
|
if (utc) {
|
|
|
|
unsetenv("TZ");
|
|
|
|
if (oldtz)
|
|
|
|
putenv(oldtz - 3);
|
|
|
|
tzset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|