Home | History | Annotate | Line # | Download | only in date
date.c revision 1.60
      1  1.60     joerg /* $NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg 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.15   thorpej #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.15   thorpej __COPYRIGHT(
     35  1.52     lukem "@(#) Copyright (c) 1985, 1987, 1988, 1993\
     36  1.52     lukem  The Regents of the University of California.  All rights reserved.");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.10       cgd #if 0
     41  1.11       jtc static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
     42  1.10       cgd #else
     43  1.60     joerg __RCSID("$NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg Exp $");
     44  1.10       cgd #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/time.h>
     49   1.8   mycroft 
     50   1.8   mycroft #include <ctype.h>
     51   1.8   mycroft #include <err.h>
     52   1.8   mycroft #include <fcntl.h>
     53  1.59  christos #include <errno.h>
     54  1.34       wiz #include <locale.h>
     55   1.1       cgd #include <stdio.h>
     56   1.1       cgd #include <stdlib.h>
     57   1.1       cgd #include <string.h>
     58   1.8   mycroft #include <syslog.h>
     59  1.22    kleink #include <time.h>
     60  1.18   mycroft #include <tzfile.h>
     61   1.8   mycroft #include <unistd.h>
     62  1.14       cgd #include <util.h>
     63   1.8   mycroft 
     64   1.8   mycroft #include "extern.h"
     65   1.1       cgd 
     66  1.38       dsl static time_t tval;
     67  1.46   jdarrow static int aflag, jflag, rflag, nflag;
     68   1.1       cgd 
     69  1.60     joerg __dead static void badformat(void);
     70  1.60     joerg __dead static void badtime(void);
     71  1.60     joerg __dead static void badvalue(const char *);
     72  1.34       wiz static void setthetime(const char *);
     73  1.60     joerg __dead static void usage(void);
     74   1.8   mycroft 
     75   1.8   mycroft int
     76  1.34       wiz main(int argc, char *argv[])
     77   1.1       cgd {
     78  1.43  christos 	char *buf;
     79  1.43  christos 	size_t bufsiz;
     80  1.40  christos 	const char *format;
     81  1.38       dsl 	int ch;
     82  1.59  christos 	long long val;
     83  1.59  christos 	struct tm *tm;
     84   1.1       cgd 
     85  1.35       wiz 	setprogname(argv[0]);
     86  1.16       cgd 	(void)setlocale(LC_ALL, "");
     87   1.7       jtc 
     88  1.47  christos 	while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) {
     89  1.42     peter 		switch (ch) {
     90  1.38       dsl 		case 'a':		/* adjust time slowly */
     91  1.38       dsl 			aflag = 1;
     92  1.38       dsl 			nflag = 1;
     93  1.38       dsl 			break;
     94  1.47  christos 		case 'd':
     95  1.47  christos 			rflag = 1;
     96  1.49  christos 			tval = parsedate(optarg, NULL, NULL);
     97  1.59  christos 			if (tval == -1)
     98  1.59  christos badarg:				 errx(EXIT_FAILURE,
     99  1.59  christos 				    "Cannot parse `%s'", optarg);
    100  1.47  christos 			break;
    101  1.46   jdarrow 		case 'j':		/* don't set time */
    102  1.46   jdarrow 			jflag = 1;
    103  1.46   jdarrow 			break;
    104   1.1       cgd 		case 'n':		/* don't set network */
    105   1.1       cgd 			nflag = 1;
    106   1.1       cgd 			break;
    107   1.1       cgd 		case 'r':		/* user specified seconds */
    108  1.59  christos 			errno = 0;
    109  1.59  christos 			val = strtoll(optarg, &buf, 0);
    110  1.59  christos 			if (optarg[0] == '\0' || *buf != '\0')
    111  1.59  christos 				goto badarg;
    112  1.59  christos 			if (errno == ERANGE && (val == LLONG_MAX ||
    113  1.59  christos 			    val == LLONG_MIN))
    114  1.59  christos 				err(EXIT_FAILURE, "Bad number `%s'", optarg);
    115   1.1       cgd 			rflag = 1;
    116  1.59  christos 			tval = (time_t)val;
    117   1.1       cgd 			break;
    118  1.27   mycroft 		case 'u':		/* do everything in UTC */
    119  1.55      gson 			(void)setenv("TZ", "UTC0", 1);
    120   1.1       cgd 			break;
    121   1.1       cgd 		default:
    122   1.1       cgd 			usage();
    123   1.1       cgd 		}
    124  1.38       dsl 	}
    125   1.1       cgd 	argc -= optind;
    126   1.1       cgd 	argv += optind;
    127   1.1       cgd 
    128   1.8   mycroft 	if (!rflag && time(&tval) == -1)
    129  1.42     peter 		err(EXIT_FAILURE, "time");
    130   1.1       cgd 
    131   1.1       cgd 
    132   1.1       cgd 	/* allow the operands in any order */
    133   1.1       cgd 	if (*argv && **argv == '+') {
    134  1.53  dholland 		format = *argv;
    135   1.1       cgd 		++argv;
    136  1.59  christos 	} else
    137  1.59  christos 		format = "+%a %b %e %H:%M:%S %Z %Y";
    138   1.1       cgd 
    139   1.1       cgd 	if (*argv) {
    140   1.1       cgd 		setthetime(*argv);
    141   1.1       cgd 		++argv;
    142   1.1       cgd 	}
    143   1.1       cgd 
    144   1.1       cgd 	if (*argv && **argv == '+')
    145  1.53  dholland 		format = *argv;
    146   1.1       cgd 
    147  1.43  christos 	if ((buf = malloc(bufsiz = 1024)) == NULL)
    148  1.43  christos 		goto bad;
    149  1.59  christos 
    150  1.59  christos 	if ((tm = localtime(&tval)) == NULL)
    151  1.59  christos 		err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
    152  1.59  christos 
    153  1.59  christos 	while (strftime(buf, bufsiz, format, tm) == 0)
    154  1.43  christos 		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
    155  1.43  christos 			goto bad;
    156  1.59  christos 
    157  1.59  christos 	(void)printf("%s\n", buf + 1);
    158  1.43  christos 	free(buf);
    159  1.43  christos 	return 0;
    160  1.43  christos bad:
    161  1.59  christos 	err(EXIT_FAILURE, "Cannot allocate format buffer");
    162   1.1       cgd }
    163   1.1       cgd 
    164  1.34       wiz static void
    165  1.34       wiz badformat(void)
    166  1.34       wiz {
    167  1.34       wiz 	warnx("illegal time format");
    168  1.34       wiz 	usage();
    169  1.34       wiz }
    170  1.34       wiz 
    171  1.34       wiz static void
    172  1.34       wiz badtime(void)
    173  1.34       wiz {
    174  1.36  jschauma 	errx(EXIT_FAILURE, "illegal time");
    175  1.36  jschauma 	/* NOTREACHED */
    176  1.34       wiz }
    177  1.34       wiz 
    178  1.45      elad static void
    179  1.45      elad badvalue(const char *param)
    180  1.45      elad {
    181  1.45      elad 	warnx("invalid %s supplied", param);
    182  1.45      elad 	usage();
    183  1.45      elad }
    184  1.45      elad 
    185  1.34       wiz #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    186  1.21   mycroft 
    187  1.33  gmcgarry static void
    188  1.34       wiz setthetime(const char *p)
    189   1.1       cgd {
    190  1.34       wiz 	struct timeval tv;
    191  1.38       dsl 	time_t new_time;
    192  1.12       tls 	struct tm *lt;
    193  1.23   mycroft 	const char *dot, *t;
    194  1.50    cbiere 	size_t len;
    195  1.50    cbiere 	int yearset;
    196   1.1       cgd 
    197   1.8   mycroft 	for (t = p, dot = NULL; *t; ++t) {
    198  1.28  christos 		if (isdigit((unsigned char)*t))
    199   1.8   mycroft 			continue;
    200   1.8   mycroft 		if (*t == '.' && dot == NULL) {
    201   1.8   mycroft 			dot = t;
    202   1.8   mycroft 			continue;
    203   1.8   mycroft 		}
    204   1.8   mycroft 		badformat();
    205   1.8   mycroft 	}
    206   1.1       cgd 
    207  1.59  christos 	if ((lt = localtime(&tval)) == NULL)
    208  1.59  christos 		err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
    209  1.31     bjh21 
    210  1.31     bjh21 	lt->tm_isdst = -1;			/* Divine correct DST */
    211   1.1       cgd 
    212   1.8   mycroft 	if (dot != NULL) {			/* .ss */
    213  1.23   mycroft 		len = strlen(dot);
    214  1.23   mycroft 		if (len != 3)
    215   1.8   mycroft 			badformat();
    216  1.23   mycroft 		++dot;
    217   1.8   mycroft 		lt->tm_sec = ATOI2(dot);
    218  1.45      elad 		if (lt->tm_sec > 61)
    219  1.45      elad 			badvalue("seconds");
    220  1.23   mycroft 	} else {
    221  1.23   mycroft 		len = 0;
    222   1.1       cgd 		lt->tm_sec = 0;
    223  1.23   mycroft 	}
    224   1.1       cgd 
    225  1.18   mycroft 	yearset = 0;
    226  1.23   mycroft 	switch (strlen(p) - len) {
    227  1.21   mycroft 	case 12:				/* cc */
    228  1.21   mycroft 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    229  1.45      elad 		if (lt->tm_year < 0)
    230  1.45      elad 			badtime();
    231  1.18   mycroft 		yearset = 1;
    232  1.18   mycroft 		/* FALLTHROUGH */
    233   1.1       cgd 	case 10:				/* yy */
    234  1.18   mycroft 		if (yearset) {
    235  1.21   mycroft 			lt->tm_year += ATOI2(p);
    236  1.18   mycroft 		} else {
    237  1.18   mycroft 			yearset = ATOI2(p);
    238  1.21   mycroft 			if (yearset < 69)
    239  1.21   mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    240  1.18   mycroft 			else
    241  1.21   mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    242  1.18   mycroft 		}
    243   1.1       cgd 		/* FALLTHROUGH */
    244   1.1       cgd 	case 8:					/* mm */
    245   1.1       cgd 		lt->tm_mon = ATOI2(p);
    246  1.45      elad 		if (lt->tm_mon > 12 || lt->tm_mon == 0)
    247  1.45      elad 			badvalue("month");
    248   1.1       cgd 		--lt->tm_mon;			/* time struct is 0 - 11 */
    249   1.1       cgd 		/* FALLTHROUGH */
    250   1.1       cgd 	case 6:					/* dd */
    251   1.1       cgd 		lt->tm_mday = ATOI2(p);
    252  1.45      elad 		switch (lt->tm_mon) {
    253  1.45      elad 		case 0:
    254  1.45      elad 		case 2:
    255  1.45      elad 		case 4:
    256  1.45      elad 		case 6:
    257  1.45      elad 		case 7:
    258  1.45      elad 		case 9:
    259  1.45      elad 		case 11:
    260  1.45      elad 			if (lt->tm_mday > 31 || lt->tm_mday == 0)
    261  1.45      elad 				badvalue("day of month");
    262  1.45      elad 			break;
    263  1.45      elad 		case 3:
    264  1.45      elad 		case 5:
    265  1.45      elad 		case 8:
    266  1.45      elad 		case 10:
    267  1.45      elad 			if (lt->tm_mday > 30 || lt->tm_mday == 0)
    268  1.45      elad 				badvalue("day of month");
    269  1.45      elad 			break;
    270  1.45      elad 		case 1:
    271  1.45      elad 			if (lt->tm_mday > 29 || lt->tm_mday == 0 ||
    272  1.45      elad 			    (lt->tm_mday == 29 &&
    273  1.45      elad 			     !isleap(lt->tm_year + TM_YEAR_BASE)))
    274  1.45      elad 				badvalue("day of month");
    275  1.45      elad 			break;
    276  1.45      elad 		default:
    277  1.45      elad 			badvalue("month");
    278  1.45      elad 			break;
    279  1.45      elad 		}
    280   1.1       cgd 		/* FALLTHROUGH */
    281   1.1       cgd 	case 4:					/* hh */
    282   1.1       cgd 		lt->tm_hour = ATOI2(p);
    283  1.45      elad 		if (lt->tm_hour > 23)
    284  1.45      elad 			badvalue("hour");
    285   1.1       cgd 		/* FALLTHROUGH */
    286   1.1       cgd 	case 2:					/* mm */
    287   1.1       cgd 		lt->tm_min = ATOI2(p);
    288  1.45      elad 		if (lt->tm_min > 59)
    289  1.45      elad 			badvalue("minute");
    290   1.1       cgd 		break;
    291  1.38       dsl 	case 0:					/* was just .sss */
    292  1.38       dsl 		if (len != 0)
    293  1.38       dsl 			break;
    294  1.38       dsl 		/* FALLTHROUGH */
    295   1.1       cgd 	default:
    296   1.1       cgd 		badformat();
    297   1.1       cgd 	}
    298   1.1       cgd 
    299  1.27   mycroft 	/* convert broken-down time to UTC clock time */
    300  1.38       dsl 	if ((new_time = mktime(lt)) == -1)
    301  1.26  wsanchez 		badtime();
    302   1.1       cgd 
    303  1.46   jdarrow 	/* if jflag is set, don't actually change the time, just return */
    304  1.46   jdarrow 	if (jflag) {
    305  1.46   jdarrow 		tval = new_time;
    306  1.46   jdarrow 		return;
    307  1.46   jdarrow 	}
    308  1.46   jdarrow 
    309   1.1       cgd 	/* set the time */
    310  1.38       dsl 	if (nflag || netsettime(new_time)) {
    311   1.1       cgd 		logwtmp("|", "date", "");
    312  1.38       dsl 		if (aflag) {
    313  1.38       dsl 			tv.tv_sec = new_time - tval;
    314  1.38       dsl 			tv.tv_usec = 0;
    315  1.41   hubertf 			if (adjtime(&tv, NULL))
    316  1.44   jnemeth 				err(EXIT_FAILURE, "adjtime");
    317  1.38       dsl 		} else {
    318  1.39       dsl 			tval = new_time;
    319  1.38       dsl 			tv.tv_sec = tval;
    320  1.38       dsl 			tv.tv_usec = 0;
    321  1.41   hubertf 			if (settimeofday(&tv, NULL))
    322  1.44   jnemeth 				err(EXIT_FAILURE, "settimeofday");
    323   1.1       cgd 		}
    324   1.1       cgd 		logwtmp("{", "date", "");
    325   1.1       cgd 	}
    326   1.8   mycroft 
    327   1.8   mycroft 	if ((p = getlogin()) == NULL)
    328   1.8   mycroft 		p = "???";
    329   1.8   mycroft 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.8   mycroft static void
    333  1.34       wiz usage(void)
    334   1.1       cgd {
    335   1.1       cgd 	(void)fprintf(stderr,
    336  1.59  christos 	    "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
    337  1.59  christos 	    getprogname());
    338  1.46   jdarrow 	(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
    339  1.41   hubertf 	exit(EXIT_FAILURE);
    340  1.24   mycroft 	/* NOTREACHED */
    341   1.1       cgd }
    342