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