From f79d48d95423842e9f28efb0614aaf5da9474071 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 11 Jul 2025 08:02:10 +0000 Subject: usr: date: Allow time-setting by user If the user believes the system time is off, they may now invoke 'date' with a time string "hh:mm:ss" to write it to the clock. Signed-off-by: Ian Moffett --- usr.bin/date/date.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'usr.bin/date/date.c') diff --git a/usr.bin/date/date.c b/usr.bin/date/date.c index a47e3eb..ab26c4c 100644 --- a/usr.bin/date/date.c +++ b/usr.bin/date/date.c @@ -32,6 +32,7 @@ #include #include #include +#include #define MONTHS_PER_YEAR 12 #define DAYS_PER_WEEK 7 @@ -51,19 +52,63 @@ static const char *daytab[] = { "Fri" }; +static int +set_time(int clock_fd, struct date *dp, char *timestr) +{ + uint32_t hour, min, sec; + char *p; + + /* Hour */ + p = strtok(timestr, ":"); + if (p == NULL) + return -1; + hour = atoi(p); + + /* Minute */ + p = strtok(NULL, ":"); + if (p == NULL) + return -1; + min = atoi(p); + + /* Second */ + p = strtok(NULL, ":"); + if (p == NULL) + return -1; + sec = atoi(p); + + /* Set the time */ + dp->hour = hour; + dp->min = min; + dp->sec = sec; + write(clock_fd, dp, sizeof(*dp)); + return 0; +} + int -main(void) +main(int argc, char **argv) { const char *day, *month; char date_str[32]; struct date d; - int rtc_fd; + int rtc_fd, error = 0; - if ((rtc_fd = open("/dev/rtc", O_RDONLY)) < 0) { + if ((rtc_fd = open("/dev/rtc", O_RDWR)) < 0) { return rtc_fd; } + read(rtc_fd, &d, sizeof(d)); + + /* + * If a time was specified to be set in the + * 'hh:mm:ss' format, attempt to write it. + */ + if (argc > 1) { + error = set_time(rtc_fd, &d, argv[1]); + if (error < 0) + printf("bad time specified, not set\n"); + } + close(rtc_fd); /* This should not happen */ -- cgit v1.2.3