Home | History | Annotate | Line # | Download | only in date
date.c revision 1.27
      1 /*	$NetBSD: date.c,v 1.27 1998/10/24 03:45:51 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1987, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT(
     39 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
     46 #else
     47 __RCSID("$NetBSD: date.c,v 1.27 1998/10/24 03:45:51 mycroft Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/time.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <fcntl.h>
     57 #include <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <locale.h>
     61 #include <syslog.h>
     62 #include <time.h>
     63 #include <tzfile.h>
     64 #include <unistd.h>
     65 #include <util.h>
     66 
     67 #include "extern.h"
     68 
     69 time_t tval;
     70 int retval, nflag;
     71 
     72 int main __P((int, char *[]));
     73 static void setthetime __P((const char *));
     74 static void badformat __P((void));
     75 static void badtime __P((void));
     76 static void usage __P((void));
     77 
     78 int
     79 main(argc, argv)
     80 	int argc;
     81 	char *argv[];
     82 {
     83 	extern int optind;
     84 	extern char *optarg;
     85 	int ch, rflag;
     86 	char *format, buf[1024];
     87 
     88 	(void)setlocale(LC_ALL, "");
     89 
     90 	rflag = 0;
     91 	while ((ch = getopt(argc, argv, "nr:u")) != -1)
     92 		switch((char)ch) {
     93 		case 'n':		/* don't set network */
     94 			nflag = 1;
     95 			break;
     96 		case 'r':		/* user specified seconds */
     97 			rflag = 1;
     98 			tval = atol(optarg);
     99 			break;
    100 		case 'u':		/* do everything in UTC */
    101 			(void)setenv("TZ", "UTC", 1);
    102 			break;
    103 		default:
    104 			usage();
    105 		}
    106 	argc -= optind;
    107 	argv += optind;
    108 
    109 	if (!rflag && time(&tval) == -1)
    110 		err(1, "time");
    111 
    112 	format = "%a %b %e %H:%M:%S %Z %Y";
    113 
    114 	/* allow the operands in any order */
    115 	if (*argv && **argv == '+') {
    116 		format = *argv + 1;
    117 		++argv;
    118 	}
    119 
    120 	if (*argv) {
    121 		setthetime(*argv);
    122 		++argv;
    123 	}
    124 
    125 	if (*argv && **argv == '+')
    126 		format = *argv + 1;
    127 
    128 	(void)strftime(buf, sizeof(buf), format, localtime(&tval));
    129 	(void)printf("%s\n", buf);
    130 	exit(retval);
    131 	/* NOTREACHED */
    132 }
    133 
    134 #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    135 
    136 void
    137 setthetime(p)
    138 	const char *p;
    139 {
    140 	struct tm *lt;
    141 	struct timeval tv;
    142 	const char *dot, *t;
    143 	int yearset, len;
    144 
    145 	for (t = p, dot = NULL; *t; ++t) {
    146 		if (isdigit(*t))
    147 			continue;
    148 		if (*t == '.' && dot == NULL) {
    149 			dot = t;
    150 			continue;
    151 		}
    152 		badformat();
    153 	}
    154 
    155 	lt = localtime(&tval);
    156 
    157 	if (dot != NULL) {			/* .ss */
    158 		len = strlen(dot);
    159 		if (len != 3)
    160 			badformat();
    161 		++dot;
    162 		lt->tm_sec = ATOI2(dot);
    163 	} else {
    164 		len = 0;
    165 		lt->tm_sec = 0;
    166 	}
    167 
    168 	yearset = 0;
    169 	switch (strlen(p) - len) {
    170 	case 12:				/* cc */
    171 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    172 		yearset = 1;
    173 		/* FALLTHROUGH */
    174 	case 10:				/* yy */
    175 		if (yearset) {
    176 			lt->tm_year += ATOI2(p);
    177 		} else {
    178 			yearset = ATOI2(p);
    179 			if (yearset < 69)
    180 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    181 			else
    182 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    183 		}
    184 		/* FALLTHROUGH */
    185 	case 8:					/* mm */
    186 		lt->tm_mon = ATOI2(p);
    187 		--lt->tm_mon;			/* time struct is 0 - 11 */
    188 		/* FALLTHROUGH */
    189 	case 6:					/* dd */
    190 		lt->tm_mday = ATOI2(p);
    191 		/* FALLTHROUGH */
    192 	case 4:					/* hh */
    193 		lt->tm_hour = ATOI2(p);
    194 		/* FALLTHROUGH */
    195 	case 2:					/* mm */
    196 		lt->tm_min = ATOI2(p);
    197 		break;
    198 	default:
    199 		badformat();
    200 	}
    201 
    202 	/* convert broken-down time to UTC clock time */
    203 	if ((tval = mktime(lt)) == -1)
    204 		badtime();
    205 
    206 	/* set the time */
    207 	if (nflag || netsettime(tval)) {
    208 		logwtmp("|", "date", "");
    209 		tv.tv_sec = tval;
    210 		tv.tv_usec = 0;
    211 		if (settimeofday(&tv, NULL)) {
    212 			perror("date: settimeofday");
    213 			exit(1);
    214 		}
    215 		logwtmp("{", "date", "");
    216 	}
    217 
    218 	if ((p = getlogin()) == NULL)
    219 		p = "???";
    220 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    221 }
    222 
    223 static void
    224 badformat()
    225 {
    226 	warnx("illegal time format");
    227 	usage();
    228 }
    229 
    230 static void
    231 badtime()
    232 {
    233 	errx(1, "illegal time");
    234 }
    235 
    236 static void
    237 usage()
    238 {
    239 	(void)fprintf(stderr,
    240 	    "usage: date [-nu] [-r seconds] [+format]\n");
    241 	(void)fprintf(stderr, "       date [[[[[cc]yy]mm]dd]hh]mm[.ss]\n");
    242 	exit(1);
    243 	/* NOTREACHED */
    244 }
    245