Home | History | Annotate | Line # | Download | only in date
date.c revision 1.23
      1  1.23  mycroft /*	$NetBSD: date.c,v 1.23 1998/07/27 16:43:25 mycroft 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.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36  1.15  thorpej #include <sys/cdefs.h>
     37   1.1      cgd #ifndef lint
     38  1.15  thorpej __COPYRIGHT(
     39   1.8  mycroft "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
     40  1.15  thorpej 	The Regents of the University of California.  All rights reserved.\n");
     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.23  mycroft __RCSID("$NetBSD: date.c,v 1.23 1998/07/27 16:43:25 mycroft 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.1      cgd #include <stdio.h>
     58   1.1      cgd #include <stdlib.h>
     59   1.1      cgd #include <string.h>
     60   1.7      jtc #include <locale.h>
     61   1.8  mycroft #include <syslog.h>
     62  1.22   kleink #include <time.h>
     63  1.18  mycroft #include <tzfile.h>
     64   1.8  mycroft #include <unistd.h>
     65  1.14      cgd #include <util.h>
     66   1.8  mycroft 
     67   1.8  mycroft #include "extern.h"
     68   1.1      cgd 
     69   1.1      cgd time_t tval;
     70   1.1      cgd int retval, nflag;
     71   1.1      cgd 
     72  1.15  thorpej int main __P((int, char *[]));
     73  1.23  mycroft static void setthetime __P((const char *));
     74   1.8  mycroft static void badformat __P((void));
     75   1.8  mycroft static void usage __P((void));
     76   1.8  mycroft 
     77   1.8  mycroft int
     78   1.1      cgd main(argc, argv)
     79   1.1      cgd 	int argc;
     80  1.15  thorpej 	char *argv[];
     81   1.1      cgd {
     82   1.1      cgd 	extern int optind;
     83   1.1      cgd 	extern char *optarg;
     84   1.1      cgd 	int ch, rflag;
     85   1.1      cgd 	char *format, buf[1024];
     86   1.1      cgd 
     87  1.16      cgd 	(void)setlocale(LC_ALL, "");
     88   1.7      jtc 
     89   1.1      cgd 	rflag = 0;
     90  1.13    perry 	while ((ch = getopt(argc, argv, "nr:u")) != -1)
     91   1.1      cgd 		switch((char)ch) {
     92   1.1      cgd 		case 'n':		/* don't set network */
     93   1.1      cgd 			nflag = 1;
     94   1.1      cgd 			break;
     95   1.1      cgd 		case 'r':		/* user specified seconds */
     96   1.1      cgd 			rflag = 1;
     97   1.1      cgd 			tval = atol(optarg);
     98   1.1      cgd 			break;
     99   1.1      cgd 		case 'u':		/* do everything in GMT */
    100   1.1      cgd 			(void)setenv("TZ", "GMT0", 1);
    101   1.1      cgd 			break;
    102   1.1      cgd 		default:
    103   1.1      cgd 			usage();
    104   1.1      cgd 		}
    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.21  mycroft #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    134  1.21  mycroft 
    135   1.8  mycroft void
    136   1.1      cgd setthetime(p)
    137  1.23  mycroft 	const char *p;
    138   1.1      cgd {
    139  1.12      tls 	struct tm *lt;
    140   1.1      cgd 	struct timeval tv;
    141  1.23  mycroft 	const char *dot, *t;
    142  1.23  mycroft 	int yearset, len;
    143   1.1      cgd 
    144   1.8  mycroft 	for (t = p, dot = NULL; *t; ++t) {
    145   1.8  mycroft 		if (isdigit(*t))
    146   1.8  mycroft 			continue;
    147   1.8  mycroft 		if (*t == '.' && dot == NULL) {
    148   1.8  mycroft 			dot = t;
    149   1.8  mycroft 			continue;
    150   1.8  mycroft 		}
    151   1.8  mycroft 		badformat();
    152   1.8  mycroft 	}
    153   1.1      cgd 
    154   1.1      cgd 	lt = localtime(&tval);
    155   1.1      cgd 
    156   1.8  mycroft 	if (dot != NULL) {			/* .ss */
    157  1.23  mycroft 		len = strlen(dot);
    158  1.23  mycroft 		if (len != 3)
    159   1.8  mycroft 			badformat();
    160  1.23  mycroft 		++dot;
    161   1.8  mycroft 		lt->tm_sec = ATOI2(dot);
    162  1.23  mycroft 	} else {
    163  1.23  mycroft 		len = 0;
    164   1.1      cgd 		lt->tm_sec = 0;
    165  1.23  mycroft 	}
    166   1.1      cgd 
    167  1.18  mycroft 	yearset = 0;
    168  1.23  mycroft 	switch (strlen(p) - len) {
    169  1.21  mycroft 	case 12:				/* cc */
    170  1.21  mycroft 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    171  1.18  mycroft 		yearset = 1;
    172  1.18  mycroft 		/* FALLTHROUGH */
    173   1.1      cgd 	case 10:				/* yy */
    174  1.18  mycroft 		if (yearset) {
    175  1.21  mycroft 			lt->tm_year += ATOI2(p);
    176  1.18  mycroft 		} else {
    177  1.18  mycroft 			yearset = ATOI2(p);
    178  1.21  mycroft 			if (yearset < 69)
    179  1.21  mycroft 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    180  1.18  mycroft 			else
    181  1.21  mycroft 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    182  1.18  mycroft 		}
    183   1.1      cgd 		/* FALLTHROUGH */
    184   1.1      cgd 	case 8:					/* mm */
    185   1.1      cgd 		lt->tm_mon = ATOI2(p);
    186   1.1      cgd 		--lt->tm_mon;			/* time struct is 0 - 11 */
    187   1.1      cgd 		/* FALLTHROUGH */
    188   1.1      cgd 	case 6:					/* dd */
    189   1.1      cgd 		lt->tm_mday = ATOI2(p);
    190   1.1      cgd 		/* FALLTHROUGH */
    191   1.1      cgd 	case 4:					/* hh */
    192   1.1      cgd 		lt->tm_hour = ATOI2(p);
    193   1.1      cgd 		/* FALLTHROUGH */
    194   1.1      cgd 	case 2:					/* mm */
    195   1.1      cgd 		lt->tm_min = ATOI2(p);
    196   1.1      cgd 		break;
    197   1.1      cgd 	default:
    198   1.1      cgd 		badformat();
    199   1.1      cgd 	}
    200   1.1      cgd 
    201   1.1      cgd 	/* convert broken-down time to GMT clock time */
    202   1.1      cgd 	if ((tval = mktime(lt)) == -1)
    203   1.1      cgd 		badformat();
    204   1.1      cgd 
    205   1.1      cgd 	/* set the time */
    206   1.1      cgd 	if (nflag || netsettime(tval)) {
    207   1.1      cgd 		logwtmp("|", "date", "");
    208   1.1      cgd 		tv.tv_sec = tval;
    209   1.1      cgd 		tv.tv_usec = 0;
    210   1.8  mycroft 		if (settimeofday(&tv, NULL)) {
    211   1.1      cgd 			perror("date: settimeofday");
    212   1.1      cgd 			exit(1);
    213   1.1      cgd 		}
    214   1.1      cgd 		logwtmp("{", "date", "");
    215   1.1      cgd 	}
    216   1.8  mycroft 
    217   1.8  mycroft 	if ((p = getlogin()) == NULL)
    218   1.8  mycroft 		p = "???";
    219   1.8  mycroft 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    220   1.1      cgd }
    221   1.1      cgd 
    222   1.8  mycroft static void
    223   1.1      cgd badformat()
    224   1.1      cgd {
    225   1.8  mycroft 	warnx("illegal time format");
    226   1.1      cgd 	usage();
    227   1.1      cgd }
    228   1.1      cgd 
    229   1.8  mycroft static void
    230   1.1      cgd usage()
    231   1.1      cgd {
    232   1.1      cgd 	(void)fprintf(stderr,
    233  1.13    perry 	    "usage: date [-nu] [-r seconds] [+format]\n");
    234  1.19  mycroft 	(void)fprintf(stderr, "       date [[[[[cc]yy]mm]dd]hh]mm[.ss]\n");
    235   1.1      cgd 	exit(1);
    236   1.1      cgd }
    237