#include <minix/rs.h>
#include <sys/ipc.h>
-int nflag = 0; /* Tell what, but don't do it. */
-int wflag = 0; /* Set the CMOS clock. */
-int Wflag = 0; /* Also set the CMOS clock register bits. */
-int y2kflag = 0; /* Interpret 1980 as 2000 for clock with Y2K bug. */
-
void errmsg(char *s);
-void get_time(struct tm *t);
-void set_time(struct tm *t);
+static void readclock(int type, struct tm *t, int flags);
void usage(void);
+int quiet = 0;
+
int
main(int argc, char **argv)
{
+ int flags = RTCDEV_NOFLAGS;
+ int nflag = 0; /* Tell what, but don't do it. */
+ int wflag = 0; /* Set the CMOS clock. */
struct tm time1;
struct tm time2;
struct tm tmnow;
wflag = 1;
break;
case 'W':
- Wflag = 1;
+ flags |= RTCDEV_CMOSREG;
+ wflag = 1; /* -W implies -w */
break;
case '2':
- y2kflag = 1;
+ flags |= RTCDEV_Y2KBUG;
+ break;
+ case 'q':
+ quiet = 1;
break;
default:
usage();
}
argc--;
}
- if (Wflag)
- wflag = 1; /* -W implies -w */
/* Read the CMOS real time clock. */
for (i = 0; i < 10; i++) {
- get_time(&time1);
+ readclock(RTCDEV_GET_TIME, &time1, flags);
now = time(NULL);
time1.tm_isdst = -1; /* Do timezone calculations. */
if (rtc != -1)
break;
- printf
+ if (!quiet) printf
("readclock: Invalid time read from CMOS RTC: %d-%02d-%02d %02d:%02d:%02d\n",
time2.tm_year + 1900, time2.tm_mon + 1, time2.tm_mday,
time2.tm_hour, time2.tm_min, time2.tm_sec);
if (!wflag) {
/* Set system time. */
if (nflag) {
- printf("stime(%lu)\n", (unsigned long) rtc);
+ if (!quiet)
+ printf("stime(%lu)\n", (unsigned long) rtc);
} else {
if (stime(&rtc) < 0) {
- errmsg("Not allowed to set time.");
+ if (!quiet)
+ errmsg("Not allowed to set time.");
exit(1);
}
}
"%a %b %d %H:%M:%S %Z %Y", &tmnow) != 0) {
if (date[8] == '0')
date[8] = ' ';
- printf("%s\n", date);
+ if (!quiet) printf("%s\n", date);
}
} else {
/* Set the CMOS clock to the system time. */
tmnow = *localtime(&now);
if (nflag) {
- printf("%04d-%02d-%02d %02d:%02d:%02d\n",
- tmnow.tm_year + 1900,
- tmnow.tm_mon + 1,
- tmnow.tm_mday,
- tmnow.tm_hour, tmnow.tm_min, tmnow.tm_sec);
+ if (!quiet)
+ printf("%04d-%02d-%02d %02d:%02d:%02d\n",
+ tmnow.tm_year + 1900,
+ tmnow.tm_mon + 1,
+ tmnow.tm_mday,
+ tmnow.tm_hour, tmnow.tm_min, tmnow.tm_sec);
} else {
- set_time(&tmnow);
+ readclock(RTCDEV_SET_TIME, &tmnow, flags);
}
}
exit(0);
{
static char *prompt = "readclock: ";
- printf("%s%s\n", prompt, s);
+ if (!quiet) printf("%s%s\n", prompt, s);
prompt = "";
}
-void
-get_time(struct tm *t)
+static void
+readclock(int type, struct tm *t, int flags)
{
int r;
message m;
r = minix_rs_lookup("readclock.drv", &ep);
if (r != 0) {
- errmsg("Couldn't locate readclock.drv\n");
+ if (!quiet) errmsg("Couldn't locate readclock.drv\n");
exit(1);
}
- m.RTCDEV_TM = t;
- m.RTCDEV_FLAGS = (y2kflag) ? RTCDEV_Y2KBUG : RTCDEV_NOFLAGS;
-
- r = _syscall(ep, RTCDEV_GET_TIME, &m);
- if (r != RTCDEV_REPLY || m.RTCDEV_STATUS != 0) {
- errmsg("Call to readclock.drv failed\n");
- exit(1);
- }
-}
-
-void
-set_time(struct tm *t)
-{
- int r;
- message m;
- endpoint_t ep;
-
- r = minix_rs_lookup("readclock.drv", &ep);
- if (r != 0) {
- errmsg("Couldn't locate readclock.drv\n");
- exit(1);
- }
-
- m.RTCDEV_TM = t;
- m.RTCDEV_FLAGS = RTCDEV_NOFLAGS;
-
- if (y2kflag) {
- m.RTCDEV_FLAGS |= RTCDEV_Y2KBUG;
- }
-
- if (Wflag) {
- m.RTCDEV_FLAGS |= RTCDEV_CMOSREG;
- }
+ m.RTCDEV_TM = (char *) t;
+ m.RTCDEV_FLAGS = flags;
- r = _syscall(ep, RTCDEV_SET_TIME, &m);
+ r = _syscall(ep, type, &m);
if (r != RTCDEV_REPLY || m.RTCDEV_STATUS != 0) {
- errmsg("Call to readclock.drv failed\n");
+ if (!quiet) errmsg("Call to readclock.drv failed\n");
exit(1);
}
}
void
usage(void)
{
- printf("Usage: readclock [-nwW2]\n");
+ if (!quiet) printf("Usage: readclock [-nqwW2]\n");
exit(1);
}