summaryrefslogtreecommitdiff
path: root/bin/date
diff options
context:
space:
mode:
Diffstat (limited to 'bin/date')
-rw-r--r--bin/date/date.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/bin/date/date.c b/bin/date/date.c
index 8b76d832c3e..ce21c5cfc0f 100644
--- a/bin/date/date.c
+++ b/bin/date/date.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: date.c,v 1.10 1997/09/17 23:23:03 bri Exp $ */
+/* $OpenBSD: date.c,v 1.11 1998/09/01 04:57:27 pjanzen Exp $ */
/* $NetBSD: date.c,v 1.11 1995/09/07 06:21:05 jtc Exp $ */
/*
@@ -44,7 +44,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
#else
-static char rcsid[] = "$OpenBSD: date.c,v 1.10 1997/09/17 23:23:03 bri Exp $";
+static char rcsid[] = "$OpenBSD: date.c,v 1.11 1998/09/01 04:57:27 pjanzen Exp $";
#endif
#endif /* not lint */
@@ -59,6 +59,8 @@ static char rcsid[] = "$OpenBSD: date.c,v 1.10 1997/09/17 23:23:03 bri Exp $";
#include <string.h>
#include <locale.h>
#include <syslog.h>
+#include <time.h>
+#include <tzfile.h>
#include <unistd.h>
#include <util.h>
@@ -154,7 +156,8 @@ setthetime(p)
register struct tm *lt;
struct timeval tv;
char *dot, *t;
- int bigyear = 0;
+ int bigyear;
+ int yearset = 0;
for (t = p, dot = NULL; *t; ++t) {
if (isdigit(*t))
@@ -181,27 +184,27 @@ setthetime(p)
switch (strlen(p)) {
case 12: /* yyyy */
bigyear = ATOI2(p);
- bigyear = bigyear * 100 - 1900;
+ lt->tm_year = bigyear * 100 - TM_YEAR_BASE;
+ yearset = 1;
/* FALLTHROUGH */
case 10: /* yy */
- lt->tm_year = bigyear;
- lt->tm_year += ATOI2(p);
- if (lt->tm_year < 69) /* hack for 2000 ;-} */
- lt->tm_year += 100;
- if (lt->tm_year > (2037-1900)) {
- warnx("year too large (overflows 32 bit value)");
- exit(1);
+ if (yearset) {
+ lt->tm_year += ATOI2(p);
+ } else {
+ lt->tm_year = ATOI2(p) + 1900 - TM_YEAR_BASE;
+ if (lt->tm_year < 69) /* hack for 2000 ;-} */
+ lt->tm_year += 100;
}
/* FALLTHROUGH */
case 8: /* mm */
lt->tm_mon = ATOI2(p);
- if (lt->tm_mon > 12)
+ if ((lt->tm_mon > 12) || !lt->tm_mon)
badformat();
--lt->tm_mon; /* time struct is 0 - 11 */
/* FALLTHROUGH */
case 6: /* dd */
lt->tm_mday = ATOI2(p);
- if (lt->tm_mday > 31)
+ if ((lt->tm_mday > 31) || !lt->tm_mday)
badformat();
/* FALLTHROUGH */
case 4: /* HH */
@@ -219,18 +222,16 @@ setthetime(p)
}
/* convert broken-down time to GMT clock time */
- if ((tval = mktime(lt)) == -1)
- badformat();
+ if ((tval = mktime(lt)) < 0)
+ errx(1, "specified date is outside allowed range");
/* set the time */
if (nflag || netsettime(tval)) {
logwtmp("|", "date", "");
tv.tv_sec = tval;
tv.tv_usec = 0;
- if (settimeofday(&tv, NULL)) {
- perror("date: settimeofday");
- exit(1);
- }
+ if (settimeofday(&tv, NULL))
+ errx(1, "settimeofday");
logwtmp("{", "date", "");
}