#include <locale.h>
#include <fcntl.h>
#include <langinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <utmpx.h>
#include <tzfile.h>
#define year_size(A) ((isleap(A)) ? 366 : 365)
static char buf[BUFSIZ];
static time_t clock_val;
static short month_size[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static struct utmpx wtmpx[2] = {
{"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
{"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
};
static char *usage =
"usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n"
"\tdate [-Ru] [-r seconds | filename] [+format]\n"
"\tdate -a [-]sss[.fff]\n";
static int uflag = 0;
static int Rflag = 0;
static int rflag = 0;
static int get_adj(char *, struct timeval *);
static int setdate(struct tm *, char *);
static void fmt_extensions(char *, size_t,
const char *, const struct timespec *);
int
main(int argc, char **argv)
{
struct tm *tp, tm;
struct timeval tv;
char *fmt, *eptr;
char fmtbuf[BUFSIZ];
int c, aflag = 0, illflag = 0;
struct timespec ts;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "a:uRr:")) != EOF)
switch (c) {
case 'a':
aflag++;
if (get_adj(optarg, &tv) < 0) {
(void) fprintf(stderr,
gettext("date: invalid argument -- %s\n"),
optarg);
illflag++;
}
break;
case 'u':
uflag++;
break;
case 'R':
Rflag++;
break;
case 'r':
rflag++;
errno = 0;
ts.tv_sec = strtol(optarg, &eptr, 0);
if (errno == EINVAL || *eptr != '\0') {
struct stat st;
if (stat(optarg, &st) == 0) {
ts.tv_sec = st.st_mtime;
} else {
(void) fprintf(stderr,
gettext("date: failed to get stat "
"information about %s: %s\n"),
optarg, strerror(errno));
exit(1);
}
} else if (errno != 0) {
(void) fprintf(stderr,
gettext("date: failed to parse -r "
"argument: %s\n"), optarg);
exit(1);
}
break;
default:
illflag++;
}
argc -= optind;
argv = &argv[optind];
if (uflag && aflag)
illflag++;
if (Rflag && aflag)
illflag++;
if (rflag && aflag)
illflag++;
if (illflag) {
(void) fprintf(stderr, gettext(usage));
exit(1);
}
if (rflag == 0) {
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
perror(gettext("date: Failed to obtain system time"));
exit(1);
}
}
clock_val = ts.tv_sec;
if (aflag) {
if (adjtime(&tv, 0) < 0) {
perror(gettext("date: Failed to adjust date"));
exit(1);
}
exit(0);
}
if (argc > 0) {
if (*argv[0] == '+')
fmt = &argv[0][1];
else {
if (setdate(localtime(&clock_val), argv[0])) {
(void) fprintf(stderr, gettext(usage));
exit(1);
}
fmt = nl_langinfo(_DATE_FMT);
}
} else if (Rflag) {
fmt = "%a, %d %h %Y %H:%M:%S %z";
} else
fmt = nl_langinfo(_DATE_FMT);
fmt_extensions(fmtbuf, sizeof (fmtbuf), fmt, &ts);
if (uflag) {
(void) putenv("TZ=GMT0");
tzset();
tp = gmtime(&clock_val);
} else
tp = localtime(&clock_val);
(void) memcpy(&tm, tp, sizeof (struct tm));
(void) strftime(buf, BUFSIZ, fmtbuf, &tm);
(void) puts(buf);
return (0);
}
int
setdate(struct tm *current_date, char *date)
{
int i;
int mm;
int hh;
int min;
int sec = 0;
char *secptr;
int yy;
int dd = 0;
int minidx = 6;
int len;
int dd_check;
if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 &&
isdigit(secptr[1]) && isdigit(secptr[2]) &&
(sec = atoi(&secptr[1])) >= 0 && sec < 60)
secptr[0] = '\0';
len = strlen(date);
for (i = 0; i < len; i++) {
if (!isdigit(date[i])) {
(void) fprintf(stderr,
gettext("date: bad conversion\n"));
exit(1);
}
}
switch (strlen(date)) {
case 12:
yy = atoi(&date[8]);
date[8] = '\0';
break;
case 10:
if (atoi(&date[8]) <= 68) {
yy = 1900 + (atoi(&date[8]) + 100);
} else {
yy = 1900 + atoi(&date[8]);
}
date[8] = '\0';
break;
case 8:
yy = 1900 + current_date->tm_year;
break;
case 4:
yy = 1900 + current_date->tm_year;
mm = current_date->tm_mon + 1;
dd = current_date->tm_mday;
minidx = 2;
break;
default:
(void) fprintf(stderr, gettext("date: bad conversion\n"));
return (1);
}
min = atoi(&date[minidx]);
date[minidx] = '\0';
hh = atoi(&date[minidx-2]);
date[minidx-2] = '\0';
if (!dd) {
dd = atoi(&date[2]);
date[2] = '\0';
mm = atoi(&date[0]);
}
if (hh == 24)
hh = 0, dd++;
dd_check = 0;
if (mm >= 1 && mm <= 12) {
dd_check = month_size[mm - 1];
if (mm == 2 && isleap(yy))
dd_check++;
}
if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) &&
(hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) {
(void) fprintf(stderr, gettext("date: bad conversion\n"));
return (1);
}
for (clock_val = 0, i = 1970; i < yy; i++)
clock_val += year_size(i);
if (isleap(yy) && mm >= 3)
clock_val += 1;
while (--mm)
clock_val += (time_t)month_size[mm - 1];
clock_val += (time_t)(dd - 1);
clock_val *= 24;
clock_val += (time_t)hh;
clock_val *= 60;
clock_val += (time_t)min;
clock_val *= 60;
clock_val += sec;
if (!uflag) {
(void) localtime(&clock_val);
clock_val += (time_t)timezone;
if (localtime(&clock_val)->tm_isdst)
clock_val = clock_val - (time_t)(timezone - altzone);
}
(void) time(&wtmpx[0].ut_xtime);
if (stime(&clock_val) < 0) {
perror("date");
return (1);
}
#if defined(i386)
(void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
#endif
(void) time(&wtmpx[1].ut_xtime);
(void) pututxline(&wtmpx[0]);
(void) pututxline(&wtmpx[1]);
(void) updwtmpx(WTMPX_FILE, &wtmpx[0]);
(void) updwtmpx(WTMPX_FILE, &wtmpx[1]);
return (0);
}
int
get_adj(char *cp, struct timeval *tp)
{
register int mult;
int sign;
tp->tv_sec = tp->tv_usec = 0;
if (*cp == '-') {
sign = -1;
cp++;
} else {
sign = 1;
}
while (*cp >= '0' && *cp <= '9') {
tp->tv_sec *= 10;
tp->tv_sec += *cp++ - '0';
}
if (*cp == '.') {
cp++;
mult = 100000;
while (*cp >= '0' && *cp <= '9') {
tp->tv_usec += (*cp++ - '0') * mult;
mult /= 10;
}
}
if (*cp) {
return (-1);
} else {
tp->tv_sec *= sign;
tp->tv_usec *= sign;
return (0);
}
}
void
fmt_extensions(char *fmtbuf, size_t len,
const char *fmt, const struct timespec *tsp)
{
const char *p;
char *q;
for (p = fmt, q = fmtbuf; *p != '\0' && q < fmtbuf + len; ++p) {
if (*p == '%') {
switch (*(p + 1)) {
case 'N':
++p;
q += snprintf(q, len - (q - fmtbuf),
"%09lu", tsp->tv_nsec);
continue;
}
}
*q++ = *p;
}
if (q < fmtbuf + len)
*q = '\0';
else
fmtbuf[len - 1] = '\0';
}