Home | History | Annotate | Line # | Download | only in date
date.c revision 1.41
      1  1.41   hubertf /* $NetBSD: date.c,v 1.41 2005/07/22 11:06:24 hubertf 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.8   mycroft "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
     36  1.15   thorpej 	The Regents of the University of California.  All rights reserved.\n");
     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.41   hubertf __RCSID("$NetBSD: date.c,v 1.41 2005/07/22 11:06:24 hubertf 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.34       wiz #include <locale.h>
     54   1.1       cgd #include <stdio.h>
     55   1.1       cgd #include <stdlib.h>
     56   1.1       cgd #include <string.h>
     57   1.8   mycroft #include <syslog.h>
     58  1.22    kleink #include <time.h>
     59  1.18   mycroft #include <tzfile.h>
     60   1.8   mycroft #include <unistd.h>
     61  1.14       cgd #include <util.h>
     62   1.8   mycroft 
     63   1.8   mycroft #include "extern.h"
     64   1.1       cgd 
     65  1.38       dsl static time_t tval;
     66  1.38       dsl static int aflag, rflag, nflag;
     67  1.38       dsl int retval;
     68   1.1       cgd 
     69  1.34       wiz int main(int, char *[]);
     70  1.34       wiz static void badformat(void);
     71  1.34       wiz static void badtime(void);
     72  1.34       wiz static void setthetime(const char *);
     73  1.34       wiz 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.40  christos 	char buf[1024];
     79  1.40  christos 	const char *format;
     80  1.38       dsl 	int ch;
     81   1.1       cgd 
     82  1.35       wiz 	setprogname(argv[0]);
     83  1.16       cgd 	(void)setlocale(LC_ALL, "");
     84   1.7       jtc 
     85  1.38       dsl 	while ((ch = getopt(argc, argv, "anr:u")) != -1) {
     86  1.41   hubertf 		switch(ch) {
     87  1.38       dsl 		case 'a':		/* adjust time slowly */
     88  1.38       dsl 			aflag = 1;
     89  1.38       dsl 			nflag = 1;
     90  1.38       dsl 			break;
     91   1.1       cgd 		case 'n':		/* don't set network */
     92   1.1       cgd 			nflag = 1;
     93   1.1       cgd 			break;
     94   1.1       cgd 		case 'r':		/* user specified seconds */
     95   1.1       cgd 			rflag = 1;
     96  1.32    simonb 			tval = strtol(optarg, NULL, 0);
     97   1.1       cgd 			break;
     98  1.27   mycroft 		case 'u':		/* do everything in UTC */
     99  1.29    kleink 			(void)putenv("TZ=UTC0");
    100   1.1       cgd 			break;
    101   1.1       cgd 		default:
    102   1.1       cgd 			usage();
    103   1.1       cgd 		}
    104  1.38       dsl 	}
    105   1.1       cgd 	argc -= optind;
    106   1.1       cgd 	argv += optind;
    107   1.1       cgd 
    108   1.8   mycroft 	if (!rflag && time(&tval) == -1)
    109   1.8   mycroft 		err(1, "time");
    110   1.1       cgd 
    111   1.6       jtc 	format = "%a %b %e %H:%M:%S %Z %Y";
    112   1.1       cgd 
    113   1.1       cgd 	/* allow the operands in any order */
    114   1.1       cgd 	if (*argv && **argv == '+') {
    115   1.1       cgd 		format = *argv + 1;
    116   1.1       cgd 		++argv;
    117   1.1       cgd 	}
    118   1.1       cgd 
    119   1.1       cgd 	if (*argv) {
    120   1.1       cgd 		setthetime(*argv);
    121   1.1       cgd 		++argv;
    122   1.1       cgd 	}
    123   1.1       cgd 
    124   1.1       cgd 	if (*argv && **argv == '+')
    125   1.1       cgd 		format = *argv + 1;
    126   1.1       cgd 
    127   1.1       cgd 	(void)strftime(buf, sizeof(buf), format, localtime(&tval));
    128   1.6       jtc 	(void)printf("%s\n", buf);
    129   1.1       cgd 	exit(retval);
    130  1.16       cgd 	/* NOTREACHED */
    131   1.1       cgd }
    132   1.1       cgd 
    133  1.34       wiz static void
    134  1.34       wiz badformat(void)
    135  1.34       wiz {
    136  1.34       wiz 	warnx("illegal time format");
    137  1.34       wiz 	usage();
    138  1.34       wiz }
    139  1.34       wiz 
    140  1.34       wiz static void
    141  1.34       wiz badtime(void)
    142  1.34       wiz {
    143  1.36  jschauma 	errx(EXIT_FAILURE, "illegal time");
    144  1.36  jschauma 	/* NOTREACHED */
    145  1.34       wiz }
    146  1.34       wiz 
    147  1.34       wiz #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    148  1.21   mycroft 
    149  1.33  gmcgarry static void
    150  1.34       wiz setthetime(const char *p)
    151   1.1       cgd {
    152  1.34       wiz 	struct timeval tv;
    153  1.38       dsl 	time_t new_time;
    154  1.12       tls 	struct tm *lt;
    155  1.23   mycroft 	const char *dot, *t;
    156  1.34       wiz 	int len, yearset;
    157   1.1       cgd 
    158   1.8   mycroft 	for (t = p, dot = NULL; *t; ++t) {
    159  1.28  christos 		if (isdigit((unsigned char)*t))
    160   1.8   mycroft 			continue;
    161   1.8   mycroft 		if (*t == '.' && dot == NULL) {
    162   1.8   mycroft 			dot = t;
    163   1.8   mycroft 			continue;
    164   1.8   mycroft 		}
    165   1.8   mycroft 		badformat();
    166   1.8   mycroft 	}
    167   1.1       cgd 
    168   1.1       cgd 	lt = localtime(&tval);
    169  1.31     bjh21 
    170  1.31     bjh21 	lt->tm_isdst = -1;			/* Divine correct DST */
    171   1.1       cgd 
    172   1.8   mycroft 	if (dot != NULL) {			/* .ss */
    173  1.23   mycroft 		len = strlen(dot);
    174  1.23   mycroft 		if (len != 3)
    175   1.8   mycroft 			badformat();
    176  1.23   mycroft 		++dot;
    177   1.8   mycroft 		lt->tm_sec = ATOI2(dot);
    178  1.23   mycroft 	} else {
    179  1.23   mycroft 		len = 0;
    180   1.1       cgd 		lt->tm_sec = 0;
    181  1.23   mycroft 	}
    182   1.1       cgd 
    183  1.18   mycroft 	yearset = 0;
    184  1.23   mycroft 	switch (strlen(p) - len) {
    185  1.21   mycroft 	case 12:				/* cc */
    186  1.21   mycroft 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    187  1.18   mycroft 		yearset = 1;
    188  1.18   mycroft 		/* FALLTHROUGH */
    189   1.1       cgd 	case 10:				/* yy */
    190  1.18   mycroft 		if (yearset) {
    191  1.21   mycroft 			lt->tm_year += ATOI2(p);
    192  1.18   mycroft 		} else {
    193  1.18   mycroft 			yearset = ATOI2(p);
    194  1.21   mycroft 			if (yearset < 69)
    195  1.21   mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    196  1.18   mycroft 			else
    197  1.21   mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    198  1.18   mycroft 		}
    199   1.1       cgd 		/* FALLTHROUGH */
    200   1.1       cgd 	case 8:					/* mm */
    201   1.1       cgd 		lt->tm_mon = ATOI2(p);
    202   1.1       cgd 		--lt->tm_mon;			/* time struct is 0 - 11 */
    203   1.1       cgd 		/* FALLTHROUGH */
    204   1.1       cgd 	case 6:					/* dd */
    205   1.1       cgd 		lt->tm_mday = ATOI2(p);
    206   1.1       cgd 		/* FALLTHROUGH */
    207   1.1       cgd 	case 4:					/* hh */
    208   1.1       cgd 		lt->tm_hour = ATOI2(p);
    209   1.1       cgd 		/* FALLTHROUGH */
    210   1.1       cgd 	case 2:					/* mm */
    211   1.1       cgd 		lt->tm_min = ATOI2(p);
    212   1.1       cgd 		break;
    213  1.38       dsl 	case 0:					/* was just .sss */
    214  1.38       dsl 		if (len != 0)
    215  1.38       dsl 			break;
    216  1.38       dsl 		/* FALLTHROUGH */
    217   1.1       cgd 	default:
    218   1.1       cgd 		badformat();
    219   1.1       cgd 	}
    220   1.1       cgd 
    221  1.27   mycroft 	/* convert broken-down time to UTC clock time */
    222  1.38       dsl 	if ((new_time = mktime(lt)) == -1)
    223  1.26  wsanchez 		badtime();
    224   1.1       cgd 
    225   1.1       cgd 	/* set the time */
    226  1.38       dsl 	if (nflag || netsettime(new_time)) {
    227   1.1       cgd 		logwtmp("|", "date", "");
    228  1.38       dsl 		if (aflag) {
    229  1.38       dsl 			tv.tv_sec = new_time - tval;
    230  1.38       dsl 			tv.tv_usec = 0;
    231  1.41   hubertf 			if (adjtime(&tv, NULL))
    232  1.41   hubertf 				err(1, "date: adjtime");
    233  1.38       dsl 		} else {
    234  1.39       dsl 			tval = new_time;
    235  1.38       dsl 			tv.tv_sec = tval;
    236  1.38       dsl 			tv.tv_usec = 0;
    237  1.41   hubertf 			if (settimeofday(&tv, NULL))
    238  1.41   hubertf 				err(1, "date: settimeofday");
    239   1.1       cgd 		}
    240   1.1       cgd 		logwtmp("{", "date", "");
    241   1.1       cgd 	}
    242   1.8   mycroft 
    243   1.8   mycroft 	if ((p = getlogin()) == NULL)
    244   1.8   mycroft 		p = "???";
    245   1.8   mycroft 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.8   mycroft static void
    249  1.34       wiz usage(void)
    250   1.1       cgd {
    251   1.1       cgd 	(void)fprintf(stderr,
    252  1.38       dsl 	    "usage: %s [-u] [-r seconds] [+format]\n", getprogname());
    253  1.38       dsl 	(void)fprintf(stderr, "       %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname());
    254  1.41   hubertf 	exit(EXIT_FAILURE);
    255  1.24   mycroft 	/* NOTREACHED */
    256   1.1       cgd }
    257