ubase/hwclock.c

166 lines
2.7 KiB
C
Raw Normal View History

2014-06-03 13:46:27 +00: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 18:03:41 +00:00
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-03 13:46:27 +00:00
#include <time.h>
#include <unistd.h>
2014-06-30 18:03:41 +00:00
2014-06-03 13:46:27 +00:00
#include "rtc.h"
#include "util.h"
static void readrtctm(struct tm *, int);
static void writertctm(struct tm *, int);
static void show(char *);
static void hctosys(char *);
static void systohc(char *);
2014-06-03 13:46:27 +00:00
static void
usage(void)
{
eprintf("usage: %s [-rsw] [-u] [dev]\n", argv0);
2014-06-03 13:46:27 +00:00
}
int
main(int argc, char *argv[])
{
char *dev = "/dev/rtc";
int rflag = 0;
int sflag = 0;
2014-06-03 13:46:27 +00:00
int wflag = 0;
ARGBEGIN {
case 'r':
rflag = 1;
break;
case 's':
sflag = 1;
break;
2014-06-03 13:46:27 +00:00
case 'w':
wflag = 1;
break;
case 'u':
break;
default:
usage();
} ARGEND;
if (argc > 1)
usage();
else if (argc == 1)
dev = argv[0];
if ((rflag ^ sflag ^ wflag) == 0)
2014-06-03 13:46:27 +00:00
eprintf("missing or incompatible function\n");
/* Only UTC support at the moment */
setenv("TZ", "UTC0", 1);
tzset();
if (rflag == 1)
show(dev);
else if (sflag == 1)
hctosys(dev);
2014-06-03 13:46:27 +00:00
else if (wflag == 1)
systohc(dev);
2014-06-03 13:46:27 +00:00
2014-10-02 22:45:25 +00:00
return 0;
2014-06-03 13:46:27 +00:00
}
static void
readrtctm(struct tm *tm, int fd)
{
2014-06-03 14:04:53 +00:00
struct rtc_time rt;
memset(&rt, 0, sizeof(rt));
if (ioctl(fd, RTC_RD_TIME, &rt) < 0)
eprintf("RTC_RD_TIME:");
2014-06-03 14:04:53 +00: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 13:46:27 +00:00
}
static void
writertctm(struct tm *tm, int fd)
{
2014-06-03 14:04:53 +00: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;
if (ioctl(fd, RTC_SET_TIME, &rt) < 0)
eprintf("RTC_SET_TIME:");
2014-06-03 13:46:27 +00:00
}
static void
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 13:46:27 +00: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);
weprintf("warning: assuming UTC for systohc\n");
2014-06-03 13:46:27 +00:00
writertctm(tm, fd);
close(fd);
}