ubase/hwclock.c

129 lines
2.1 KiB
C
Raw Normal View History

2014-06-03 13:46:27 +00:00
/* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "rtc.h"
#include "util.h"
static void echotime(char *);
static void readrtctm(struct tm *, int);
static void writertctm(struct tm *, int);
static void writetime(char *);
static void
usage(void)
{
eprintf("usage: %s [-rw] [-u]\n", argv0);
}
int
main(int argc, char *argv[])
{
char *dev = "/dev/rtc";
int rflag = 0;
int wflag = 0;
ARGBEGIN {
case 'r':
rflag = 1;
break;
case 'w':
wflag = 1;
break;
case 'u':
break;
default:
usage();
} ARGEND;
if ((rflag ^ wflag) == 0)
eprintf("missing or incompatible function\n");
/* Only UTC support at the moment */
setenv("TZ", "UTC0", 1);
tzset();
if (rflag == 1)
echotime(dev);
else if (wflag == 1)
writetime(dev);
return EXIT_SUCCESS;
}
static void
echotime(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
readrtctm(struct tm *tm, int fd)
{
2014-06-03 14:04:53 +00:00
struct rtc_time rt;
memset(&rt, 0, sizeof(rt));
ioctl(fd, RTC_RD_TIME, &rt);
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;
ioctl(fd, RTC_SET_TIME, &rt);
2014-06-03 13:46:27 +00:00
}
static void
writetime(char *dev)
{
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);
writertctm(tm, fd);
close(fd);
}