2014-06-03 09:46:27 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-03 09:46:27 -04:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-06-30 14:03:41 -04:00
|
|
|
|
2014-06-03 09:46:27 -04:00
|
|
|
#include "rtc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void readrtctm(struct tm *, int);
|
|
|
|
static void writertctm(struct tm *, int);
|
2014-06-03 10:52:45 -04:00
|
|
|
static void show(char *);
|
|
|
|
static void hctosys(char *);
|
|
|
|
static void systohc(char *);
|
2014-06-03 09:46:27 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-06-03 11:04:53 -04:00
|
|
|
eprintf("usage: %s [-rsw] [-u] [dev]\n", argv0);
|
2014-06-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *dev = "/dev/rtc";
|
|
|
|
int rflag = 0;
|
2014-06-03 10:52:45 -04:00
|
|
|
int sflag = 0;
|
2014-06-03 09:46:27 -04:00
|
|
|
int wflag = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2014-06-03 10:52:45 -04:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
2014-06-03 09:46:27 -04:00
|
|
|
case 'w':
|
|
|
|
wflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-06-03 11:04:53 -04:00
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
else if (argc == 1)
|
|
|
|
dev = argv[0];
|
|
|
|
|
2014-06-03 10:52:45 -04:00
|
|
|
if ((rflag ^ sflag ^ wflag) == 0)
|
2014-06-03 09:46:27 -04:00
|
|
|
eprintf("missing or incompatible function\n");
|
|
|
|
|
|
|
|
/* Only UTC support at the moment */
|
|
|
|
setenv("TZ", "UTC0", 1);
|
|
|
|
tzset();
|
|
|
|
|
|
|
|
if (rflag == 1)
|
2014-06-03 10:52:45 -04:00
|
|
|
show(dev);
|
|
|
|
else if (sflag == 1)
|
|
|
|
hctosys(dev);
|
2014-06-03 09:46:27 -04:00
|
|
|
else if (wflag == 1)
|
2014-06-03 10:52:45 -04:00
|
|
|
systohc(dev);
|
2014-06-03 09:46:27 -04:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
readrtctm(struct tm *tm, int fd)
|
|
|
|
{
|
2014-06-03 10:04:53 -04:00
|
|
|
struct rtc_time rt;
|
|
|
|
|
|
|
|
memset(&rt, 0, sizeof(rt));
|
2014-06-28 06:28:19 -04:00
|
|
|
if (ioctl(fd, RTC_RD_TIME, &rt) < 0)
|
|
|
|
eprintf("RTC_RD_TIME:");
|
2014-06-03 10:04:53 -04:00
|
|
|
tm->tm_sec = rt.tm_sec;
|
|
|
|
tm->tm_min = rt.tm_min;
|
|
|
|
tm->tm_hour = rt.tm_hour;
|
|
|
|
tm->tm_mday = rt.tm_mday;
|
|
|
|
tm->tm_mon = rt.tm_mon;
|
|
|
|
tm->tm_year = rt.tm_year;
|
|
|
|
tm->tm_wday = rt.tm_wday;
|
|
|
|
tm->tm_yday = rt.tm_yday;
|
|
|
|
tm->tm_isdst = rt.tm_isdst;
|
2014-06-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
writertctm(struct tm *tm, int fd)
|
|
|
|
{
|
2014-06-03 10:04:53 -04:00
|
|
|
struct rtc_time rt;
|
|
|
|
|
|
|
|
rt.tm_sec = tm->tm_sec;
|
|
|
|
rt.tm_min = tm->tm_min;
|
|
|
|
rt.tm_hour = tm->tm_hour;
|
|
|
|
rt.tm_mday = tm->tm_mday;
|
|
|
|
rt.tm_mon = tm->tm_mon;
|
|
|
|
rt.tm_year = tm->tm_year;
|
|
|
|
rt.tm_wday = tm->tm_wday;
|
|
|
|
rt.tm_yday = tm->tm_yday;
|
|
|
|
rt.tm_isdst = tm->tm_isdst;
|
2014-06-28 06:28:19 -04:00
|
|
|
if (ioctl(fd, RTC_SET_TIME, &rt) < 0)
|
|
|
|
eprintf("RTC_SET_TIME:");
|
2014-06-03 09:46:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-06-03 10:52:45 -04:00
|
|
|
show(char *dev)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
time_t t;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(dev, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", dev);
|
|
|
|
readrtctm(&tm, fd);
|
|
|
|
t = mktime(&tm);
|
|
|
|
printf("%s", asctime(localtime(&t)));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hctosys(char *dev)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct tm tm;
|
|
|
|
int r;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(dev, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", dev);
|
|
|
|
readrtctm(&tm, fd);
|
|
|
|
tv.tv_sec = mktime(&tm);
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
r = settimeofday(&tv, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
eprintf("settimeofday:");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
systohc(char *dev)
|
2014-06-03 09:46:27 -04:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct tm *tm;
|
|
|
|
time_t t;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(dev, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
eprintf("open %s:", dev);
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
t = tv.tv_sec;
|
|
|
|
tm = gmtime(&t);
|
2014-06-03 11:24:53 -04:00
|
|
|
weprintf("warning: assuming UTC for systohc\n");
|
2014-06-03 09:46:27 -04:00
|
|
|
writertctm(tm, fd);
|
|
|
|
close(fd);
|
|
|
|
}
|