Home | History | Annotate | Line # | Download | only in date
date.c revision 1.63
      1  1.63  christos /* $NetBSD: date.c,v 1.63 2022/10/22 20:11:43 christos Exp $ */
      2   1.9       cgd 
      3   1.1       cgd /*
      4   1.8   mycroft  * Copyright (c) 1985, 1987, 1988, 1993
      5   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.37       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.62  christos #if HAVE_NBTOOL_CONFIG_H
     33  1.62  christos #include "nbtool_config.h"
     34  1.62  christos #endif
     35  1.62  christos 
     36  1.15   thorpej #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.15   thorpej __COPYRIGHT(
     39  1.52     lukem "@(#) Copyright (c) 1985, 1987, 1988, 1993\
     40  1.52     lukem  The Regents of the University of California.  All rights reserved.");
     41   1.1       cgd #endif /* not lint */
     42   1.1       cgd 
     43   1.1       cgd #ifndef lint
     44  1.10       cgd #if 0
     45  1.11       jtc static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
     46  1.10       cgd #else
     47  1.63  christos __RCSID("$NetBSD: date.c,v 1.63 2022/10/22 20:11:43 christos Exp $");
     48  1.10       cgd #endif
     49   1.1       cgd #endif /* not lint */
     50   1.1       cgd 
     51   1.1       cgd #include <sys/param.h>
     52   1.1       cgd #include <sys/time.h>
     53   1.8   mycroft 
     54   1.8   mycroft #include <ctype.h>
     55   1.8   mycroft #include <err.h>
     56   1.8   mycroft #include <fcntl.h>
     57  1.59  christos #include <errno.h>
     58  1.34       wiz #include <locale.h>
     59   1.1       cgd #include <stdio.h>
     60   1.1       cgd #include <stdlib.h>
     61   1.1       cgd #include <string.h>
     62   1.8   mycroft #include <syslog.h>
     63  1.22    kleink #include <time.h>
     64  1.18   mycroft #include <tzfile.h>
     65   1.8   mycroft #include <unistd.h>
     66  1.14       cgd #include <util.h>
     67  1.63  christos #if !HAVE_NBTOOL_CONFIG_H
     68  1.63  christos #include <utmpx.h>
     69  1.63  christos #endif
     70   1.8   mycroft 
     71   1.8   mycroft #include "extern.h"
     72   1.1       cgd 
     73  1.38       dsl static time_t tval;
     74  1.46   jdarrow static int aflag, jflag, rflag, nflag;
     75  1.63  christos static char *fmt;
     76   1.1       cgd 
     77  1.61  dholland __dead static void badcanotime(const char *, const char *, size_t);
     78  1.34       wiz static void setthetime(const char *);
     79  1.60     joerg __dead static void usage(void);
     80   1.8   mycroft 
     81   1.8   mycroft int
     82  1.34       wiz main(int argc, char *argv[])
     83   1.1       cgd {
     84  1.43  christos 	char *buf;
     85  1.43  christos 	size_t bufsiz;
     86  1.40  christos 	const char *format;
     87  1.38       dsl 	int ch;
     88  1.59  christos 	long long val;
     89  1.59  christos 	struct tm *tm;
     90   1.1       cgd 
     91  1.35       wiz 	setprogname(argv[0]);
     92  1.16       cgd 	(void)setlocale(LC_ALL, "");
     93   1.7       jtc 
     94  1.63  christos 	while ((ch = getopt(argc, argv, "ad:f:jnr:u")) != -1) {
     95  1.42     peter 		switch (ch) {
     96  1.38       dsl 		case 'a':		/* adjust time slowly */
     97  1.38       dsl 			aflag = 1;
     98  1.38       dsl 			nflag = 1;
     99  1.38       dsl 			break;
    100  1.47  christos 		case 'd':
    101  1.62  christos #ifndef HAVE_NBTOOL_CONFIG_H
    102  1.47  christos 			rflag = 1;
    103  1.49  christos 			tval = parsedate(optarg, NULL, NULL);
    104  1.61  dholland 			if (tval == -1) {
    105  1.61  dholland 				errx(EXIT_FAILURE,
    106  1.61  dholland 				    "%s: Unrecognized date format", optarg);
    107  1.61  dholland 			}
    108  1.47  christos 			break;
    109  1.62  christos #else
    110  1.62  christos 			errx(EXIT_FAILURE,
    111  1.62  christos 			    "-d not supported in the tool version");
    112  1.62  christos #endif
    113  1.63  christos 		case 'f':
    114  1.63  christos 			fmt = optarg;
    115  1.63  christos 			break;
    116  1.46   jdarrow 		case 'j':		/* don't set time */
    117  1.46   jdarrow 			jflag = 1;
    118  1.46   jdarrow 			break;
    119   1.1       cgd 		case 'n':		/* don't set network */
    120   1.1       cgd 			nflag = 1;
    121   1.1       cgd 			break;
    122   1.1       cgd 		case 'r':		/* user specified seconds */
    123  1.61  dholland 			if (optarg[0] == '\0') {
    124  1.61  dholland 				errx(EXIT_FAILURE, "<empty>: Invalid number");
    125  1.61  dholland 			}
    126  1.59  christos 			errno = 0;
    127  1.59  christos 			val = strtoll(optarg, &buf, 0);
    128  1.61  dholland 			if (errno) {
    129  1.61  dholland 				err(EXIT_FAILURE, "%s", optarg);
    130  1.61  dholland 			}
    131  1.61  dholland 			if (optarg[0] == '\0' || *buf != '\0') {
    132  1.61  dholland 				errx(EXIT_FAILURE,
    133  1.61  dholland 				    "%s: Invalid number", optarg);
    134  1.61  dholland 			}
    135   1.1       cgd 			rflag = 1;
    136  1.59  christos 			tval = (time_t)val;
    137   1.1       cgd 			break;
    138  1.27   mycroft 		case 'u':		/* do everything in UTC */
    139  1.55      gson 			(void)setenv("TZ", "UTC0", 1);
    140   1.1       cgd 			break;
    141   1.1       cgd 		default:
    142   1.1       cgd 			usage();
    143   1.1       cgd 		}
    144  1.38       dsl 	}
    145   1.1       cgd 	argc -= optind;
    146   1.1       cgd 	argv += optind;
    147   1.1       cgd 
    148   1.8   mycroft 	if (!rflag && time(&tval) == -1)
    149  1.42     peter 		err(EXIT_FAILURE, "time");
    150   1.1       cgd 
    151   1.1       cgd 
    152   1.1       cgd 	/* allow the operands in any order */
    153   1.1       cgd 	if (*argv && **argv == '+') {
    154  1.53  dholland 		format = *argv;
    155   1.1       cgd 		++argv;
    156  1.59  christos 	} else
    157  1.59  christos 		format = "+%a %b %e %H:%M:%S %Z %Y";
    158   1.1       cgd 
    159   1.1       cgd 	if (*argv) {
    160   1.1       cgd 		setthetime(*argv);
    161   1.1       cgd 		++argv;
    162  1.63  christos 	} else if (fmt)
    163  1.63  christos 		usage();
    164   1.1       cgd 
    165   1.1       cgd 	if (*argv && **argv == '+')
    166  1.53  dholland 		format = *argv;
    167   1.1       cgd 
    168  1.43  christos 	if ((buf = malloc(bufsiz = 1024)) == NULL)
    169  1.43  christos 		goto bad;
    170  1.59  christos 
    171  1.59  christos 	if ((tm = localtime(&tval)) == NULL)
    172  1.61  dholland 		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
    173  1.59  christos 
    174  1.59  christos 	while (strftime(buf, bufsiz, format, tm) == 0)
    175  1.43  christos 		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
    176  1.43  christos 			goto bad;
    177  1.59  christos 
    178  1.59  christos 	(void)printf("%s\n", buf + 1);
    179  1.43  christos 	free(buf);
    180  1.43  christos 	return 0;
    181  1.43  christos bad:
    182  1.59  christos 	err(EXIT_FAILURE, "Cannot allocate format buffer");
    183   1.1       cgd }
    184   1.1       cgd 
    185  1.34       wiz static void
    186  1.61  dholland badcanotime(const char *msg, const char *val, size_t where)
    187  1.34       wiz {
    188  1.61  dholland 	warnx("%s in canonical time", msg);
    189  1.61  dholland 	warnx("%s", val);
    190  1.61  dholland 	warnx("%*s", (int)where + 1, "^");
    191  1.45      elad 	usage();
    192  1.45      elad }
    193  1.45      elad 
    194  1.34       wiz #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    195  1.21   mycroft 
    196  1.33  gmcgarry static void
    197  1.34       wiz setthetime(const char *p)
    198   1.1       cgd {
    199  1.34       wiz 	struct timeval tv;
    200  1.38       dsl 	time_t new_time;
    201  1.12       tls 	struct tm *lt;
    202  1.61  dholland 	const char *dot, *t, *op;
    203  1.50    cbiere 	size_t len;
    204  1.50    cbiere 	int yearset;
    205   1.1       cgd 
    206  1.63  christos 	if ((lt = localtime(&tval)) == NULL)
    207  1.63  christos 		err(EXIT_FAILURE, "%lld: localtime", (long long)tval);
    208  1.63  christos 
    209  1.63  christos 	lt->tm_isdst = -1;			/* Divine correct DST */
    210  1.63  christos 
    211  1.63  christos 	if (fmt) {
    212  1.63  christos 		t = strptime(p, fmt, lt);
    213  1.63  christos 		if (t == NULL) {
    214  1.63  christos 			warnx("Failed conversion of ``%s''"
    215  1.63  christos 			    " using format ``%s''\n", p, fmt);
    216  1.63  christos 		} else if (*t != '\0')
    217  1.63  christos 			warnx("Ignoring %zu extraneous"
    218  1.63  christos 				" characters in date string (%s)",
    219  1.63  christos 				strlen(t), t);
    220  1.63  christos 		goto setit;
    221  1.63  christos 	}
    222   1.8   mycroft 	for (t = p, dot = NULL; *t; ++t) {
    223  1.61  dholland 		if (*t == '.') {
    224  1.61  dholland 			if (dot == NULL) {
    225  1.61  dholland 				dot = t;
    226  1.61  dholland 			} else {
    227  1.61  dholland 				badcanotime("Unexpected dot", p, t - p);
    228  1.61  dholland 			}
    229  1.61  dholland 		} else if (!isdigit((unsigned char)*t)) {
    230  1.61  dholland 			badcanotime("Expected digit", p, t - p);
    231   1.8   mycroft 		}
    232   1.8   mycroft 	}
    233   1.1       cgd 
    234   1.1       cgd 
    235   1.8   mycroft 	if (dot != NULL) {			/* .ss */
    236  1.23   mycroft 		len = strlen(dot);
    237  1.61  dholland 		if (len > 3) {
    238  1.61  dholland 			badcanotime("Unexpected digit after seconds field",
    239  1.61  dholland 				    p, strlen(p) - 1);
    240  1.61  dholland 		} else if (len < 3) {
    241  1.61  dholland 			badcanotime("Expected digit in seconds field",
    242  1.61  dholland 				    p, strlen(p));
    243  1.61  dholland 		}
    244  1.23   mycroft 		++dot;
    245   1.8   mycroft 		lt->tm_sec = ATOI2(dot);
    246  1.45      elad 		if (lt->tm_sec > 61)
    247  1.61  dholland 			badcanotime("Seconds out of range", p, strlen(p) - 1);
    248  1.23   mycroft 	} else {
    249  1.23   mycroft 		len = 0;
    250   1.1       cgd 		lt->tm_sec = 0;
    251  1.23   mycroft 	}
    252   1.1       cgd 
    253  1.61  dholland 	op = p;
    254  1.18   mycroft 	yearset = 0;
    255  1.23   mycroft 	switch (strlen(p) - len) {
    256  1.21   mycroft 	case 12:				/* cc */
    257  1.21   mycroft 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    258  1.45      elad 		if (lt->tm_year < 0)
    259  1.61  dholland 			badcanotime("Year before 1900", op, p - op + 1);
    260  1.18   mycroft 		yearset = 1;
    261  1.18   mycroft 		/* FALLTHROUGH */
    262   1.1       cgd 	case 10:				/* yy */
    263  1.18   mycroft 		if (yearset) {
    264  1.21   mycroft 			lt->tm_year += ATOI2(p);
    265  1.18   mycroft 		} else {
    266  1.18   mycroft 			yearset = ATOI2(p);
    267  1.21   mycroft 			if (yearset < 69)
    268  1.21   mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    269  1.18   mycroft 			else
    270  1.21   mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    271  1.18   mycroft 		}
    272   1.1       cgd 		/* FALLTHROUGH */
    273   1.1       cgd 	case 8:					/* mm */
    274   1.1       cgd 		lt->tm_mon = ATOI2(p);
    275  1.45      elad 		if (lt->tm_mon > 12 || lt->tm_mon == 0)
    276  1.61  dholland 			badcanotime("Month out of range", op, p - op - 1);
    277   1.1       cgd 		--lt->tm_mon;			/* time struct is 0 - 11 */
    278   1.1       cgd 		/* FALLTHROUGH */
    279   1.1       cgd 	case 6:					/* dd */
    280   1.1       cgd 		lt->tm_mday = ATOI2(p);
    281  1.45      elad 		switch (lt->tm_mon) {
    282  1.45      elad 		case 0:
    283  1.45      elad 		case 2:
    284  1.45      elad 		case 4:
    285  1.45      elad 		case 6:
    286  1.45      elad 		case 7:
    287  1.45      elad 		case 9:
    288  1.45      elad 		case 11:
    289  1.45      elad 			if (lt->tm_mday > 31 || lt->tm_mday == 0)
    290  1.61  dholland 				badcanotime("Day out of range (max 31)",
    291  1.61  dholland 					    op, p - op - 1);
    292  1.45      elad 			break;
    293  1.45      elad 		case 3:
    294  1.45      elad 		case 5:
    295  1.45      elad 		case 8:
    296  1.45      elad 		case 10:
    297  1.45      elad 			if (lt->tm_mday > 30 || lt->tm_mday == 0)
    298  1.61  dholland 				badcanotime("Day out of range (max 30)",
    299  1.61  dholland 					    op, p - op - 1);
    300  1.45      elad 			break;
    301  1.45      elad 		case 1:
    302  1.61  dholland 			if (isleap(lt->tm_year + TM_YEAR_BASE)) {
    303  1.61  dholland 				if (lt->tm_mday > 29 || lt->tm_mday == 0) {
    304  1.61  dholland 					badcanotime("Day out of range "
    305  1.61  dholland 						    "(max 29)",
    306  1.61  dholland 						    op, p - op - 1);
    307  1.61  dholland 				}
    308  1.61  dholland 			} else {
    309  1.61  dholland 				if (lt->tm_mday > 28 || lt->tm_mday == 0) {
    310  1.61  dholland 					badcanotime("Day out of range "
    311  1.61  dholland 						    "(max 28)",
    312  1.61  dholland 						    op, p - op - 1);
    313  1.61  dholland 				}
    314  1.61  dholland 			}
    315  1.45      elad 			break;
    316  1.45      elad 		default:
    317  1.61  dholland 			/*
    318  1.61  dholland 			 * If the month was given, it's already been
    319  1.61  dholland 			 * checked.  If a bad value came back from
    320  1.61  dholland 			 * localtime, something's badly broken.
    321  1.61  dholland 			 * (make this an assertion?)
    322  1.61  dholland 			 */
    323  1.61  dholland 			errx(EXIT_FAILURE, "localtime gave invalid month %d",
    324  1.61  dholland 			    lt->tm_mon);
    325  1.45      elad 		}
    326   1.1       cgd 		/* FALLTHROUGH */
    327   1.1       cgd 	case 4:					/* hh */
    328   1.1       cgd 		lt->tm_hour = ATOI2(p);
    329  1.45      elad 		if (lt->tm_hour > 23)
    330  1.61  dholland 			badcanotime("Hour out of range", op, p - op - 1);
    331   1.1       cgd 		/* FALLTHROUGH */
    332   1.1       cgd 	case 2:					/* mm */
    333   1.1       cgd 		lt->tm_min = ATOI2(p);
    334  1.45      elad 		if (lt->tm_min > 59)
    335  1.61  dholland 			badcanotime("Minute out of range", op, p - op - 1);
    336   1.1       cgd 		break;
    337  1.38       dsl 	case 0:					/* was just .sss */
    338  1.38       dsl 		if (len != 0)
    339  1.38       dsl 			break;
    340  1.38       dsl 		/* FALLTHROUGH */
    341   1.1       cgd 	default:
    342  1.61  dholland 	    if (strlen(p) - len > 12) {
    343  1.61  dholland 		    badcanotime("Too many digits", p, 12);
    344  1.61  dholland 	    } else {
    345  1.61  dholland 		    badcanotime("Not enough digits", p, strlen(p) - len);
    346  1.61  dholland 	    }
    347   1.1       cgd 	}
    348  1.63  christos setit:
    349  1.27   mycroft 	/* convert broken-down time to UTC clock time */
    350  1.61  dholland 	if ((new_time = mktime(lt)) == -1) {
    351  1.61  dholland 		/* Can this actually happen? */
    352  1.63  christos 		err(EXIT_FAILURE, "mktime");
    353  1.61  dholland 	}
    354   1.1       cgd 
    355  1.46   jdarrow 	/* if jflag is set, don't actually change the time, just return */
    356  1.46   jdarrow 	if (jflag) {
    357  1.46   jdarrow 		tval = new_time;
    358  1.46   jdarrow 		return;
    359  1.46   jdarrow 	}
    360  1.46   jdarrow 
    361   1.1       cgd 	/* set the time */
    362  1.62  christos #ifndef HAVE_NBTOOL_CONFIG_H
    363  1.63  christos 	struct utmpx utx;
    364  1.63  christos 	memset(&utx, 0, sizeof(utx));
    365  1.63  christos 	utx.ut_type = OLD_TIME;
    366  1.63  christos 	(void)gettimeofday(&utx.ut_tv, NULL);
    367  1.63  christos 	pututxline(&utx);
    368  1.63  christos 
    369  1.38       dsl 	if (nflag || netsettime(new_time)) {
    370   1.1       cgd 		logwtmp("|", "date", "");
    371  1.38       dsl 		if (aflag) {
    372  1.38       dsl 			tv.tv_sec = new_time - tval;
    373  1.38       dsl 			tv.tv_usec = 0;
    374  1.41   hubertf 			if (adjtime(&tv, NULL))
    375  1.44   jnemeth 				err(EXIT_FAILURE, "adjtime");
    376  1.38       dsl 		} else {
    377  1.39       dsl 			tval = new_time;
    378  1.38       dsl 			tv.tv_sec = tval;
    379  1.38       dsl 			tv.tv_usec = 0;
    380  1.41   hubertf 			if (settimeofday(&tv, NULL))
    381  1.44   jnemeth 				err(EXIT_FAILURE, "settimeofday");
    382   1.1       cgd 		}
    383   1.1       cgd 		logwtmp("{", "date", "");
    384   1.1       cgd 	}
    385  1.63  christos 	utx.ut_type = NEW_TIME;
    386  1.63  christos 	(void)gettimeofday(&utx.ut_tv, NULL);
    387  1.63  christos 	pututxline(&utx);
    388   1.8   mycroft 
    389   1.8   mycroft 	if ((p = getlogin()) == NULL)
    390   1.8   mycroft 		p = "???";
    391   1.8   mycroft 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    392  1.63  christos #else
    393  1.63  christos 	errx(EXIT_FAILURE, "Can't set the time in the tools version");
    394  1.63  christos #endif
    395   1.1       cgd }
    396   1.1       cgd 
    397   1.8   mycroft static void
    398  1.34       wiz usage(void)
    399   1.1       cgd {
    400   1.1       cgd 	(void)fprintf(stderr,
    401  1.59  christos 	    "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
    402  1.59  christos 	    getprogname());
    403  1.46   jdarrow 	(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
    404  1.63  christos 	(void)fprintf(stderr,
    405  1.63  christos 	    "       %s [-ajnu] -f input_format new_date [+format]\n",
    406  1.63  christos 	    getprogname());
    407  1.41   hubertf 	exit(EXIT_FAILURE);
    408  1.24   mycroft 	/* NOTREACHED */
    409   1.1       cgd }
    410