Home | History | Annotate | Line # | Download | only in date
date.c revision 1.44
      1 /* $NetBSD: date.c,v 1.44 2006/08/17 10:03:53 jnemeth 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.44 2006/08/17 10:03:53 jnemeth 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 static time_t tval;
     66 static int aflag, rflag, nflag;
     67 int retval;
     68 
     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;
     78 	size_t bufsiz;
     79 	const char *format;
     80 	int ch;
     81 
     82 	setprogname(argv[0]);
     83 	(void)setlocale(LC_ALL, "");
     84 
     85 	while ((ch = getopt(argc, argv, "anr:u")) != -1) {
     86 		switch (ch) {
     87 		case 'a':		/* adjust time slowly */
     88 			aflag = 1;
     89 			nflag = 1;
     90 			break;
     91 		case 'n':		/* don't set network */
     92 			nflag = 1;
     93 			break;
     94 		case 'r':		/* user specified seconds */
     95 			rflag = 1;
     96 			tval = strtol(optarg, NULL, 0);
     97 			break;
     98 		case 'u':		/* do everything in UTC */
     99 			(void)putenv("TZ=UTC0");
    100 			break;
    101 		default:
    102 			usage();
    103 		}
    104 	}
    105 	argc -= optind;
    106 	argv += optind;
    107 
    108 	if (!rflag && time(&tval) == -1)
    109 		err(EXIT_FAILURE, "time");
    110 
    111 	format = "%a %b %e %H:%M:%S %Z %Y";
    112 
    113 	/* allow the operands in any order */
    114 	if (*argv && **argv == '+') {
    115 		format = *argv + 1;
    116 		++argv;
    117 	}
    118 
    119 	if (*argv) {
    120 		setthetime(*argv);
    121 		++argv;
    122 	}
    123 
    124 	if (*argv && **argv == '+')
    125 		format = *argv + 1;
    126 
    127 	if ((buf = malloc(bufsiz = 1024)) == NULL)
    128 		goto bad;
    129 	while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
    130 		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
    131 			goto bad;
    132 	(void)printf("%s\n", buf);
    133 	free(buf);
    134 	return 0;
    135 bad:
    136 	err(1, "Cannot allocate format buffer");
    137 }
    138 
    139 static void
    140 badformat(void)
    141 {
    142 	warnx("illegal time format");
    143 	usage();
    144 }
    145 
    146 static void
    147 badtime(void)
    148 {
    149 	errx(EXIT_FAILURE, "illegal time");
    150 	/* NOTREACHED */
    151 }
    152 
    153 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    154 
    155 static void
    156 setthetime(const char *p)
    157 {
    158 	struct timeval tv;
    159 	time_t new_time;
    160 	struct tm *lt;
    161 	const char *dot, *t;
    162 	int len, yearset;
    163 
    164 	for (t = p, dot = NULL; *t; ++t) {
    165 		if (isdigit((unsigned char)*t))
    166 			continue;
    167 		if (*t == '.' && dot == NULL) {
    168 			dot = t;
    169 			continue;
    170 		}
    171 		badformat();
    172 	}
    173 
    174 	lt = localtime(&tval);
    175 
    176 	lt->tm_isdst = -1;			/* Divine correct DST */
    177 
    178 	if (dot != NULL) {			/* .ss */
    179 		len = strlen(dot);
    180 		if (len != 3)
    181 			badformat();
    182 		++dot;
    183 		lt->tm_sec = ATOI2(dot);
    184 	} else {
    185 		len = 0;
    186 		lt->tm_sec = 0;
    187 	}
    188 
    189 	yearset = 0;
    190 	switch (strlen(p) - len) {
    191 	case 12:				/* cc */
    192 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
    193 		yearset = 1;
    194 		/* FALLTHROUGH */
    195 	case 10:				/* yy */
    196 		if (yearset) {
    197 			lt->tm_year += ATOI2(p);
    198 		} else {
    199 			yearset = ATOI2(p);
    200 			if (yearset < 69)
    201 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
    202 			else
    203 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
    204 		}
    205 		/* FALLTHROUGH */
    206 	case 8:					/* mm */
    207 		lt->tm_mon = ATOI2(p);
    208 		--lt->tm_mon;			/* time struct is 0 - 11 */
    209 		/* FALLTHROUGH */
    210 	case 6:					/* dd */
    211 		lt->tm_mday = ATOI2(p);
    212 		/* FALLTHROUGH */
    213 	case 4:					/* hh */
    214 		lt->tm_hour = ATOI2(p);
    215 		/* FALLTHROUGH */
    216 	case 2:					/* mm */
    217 		lt->tm_min = ATOI2(p);
    218 		break;
    219 	case 0:					/* was just .sss */
    220 		if (len != 0)
    221 			break;
    222 		/* FALLTHROUGH */
    223 	default:
    224 		badformat();
    225 	}
    226 
    227 	/* convert broken-down time to UTC clock time */
    228 	if ((new_time = mktime(lt)) == -1)
    229 		badtime();
    230 
    231 	/* set the time */
    232 	if (nflag || netsettime(new_time)) {
    233 		logwtmp("|", "date", "");
    234 		if (aflag) {
    235 			tv.tv_sec = new_time - tval;
    236 			tv.tv_usec = 0;
    237 			if (adjtime(&tv, NULL))
    238 				err(EXIT_FAILURE, "adjtime");
    239 		} else {
    240 			tval = new_time;
    241 			tv.tv_sec = tval;
    242 			tv.tv_usec = 0;
    243 			if (settimeofday(&tv, NULL))
    244 				err(EXIT_FAILURE, "settimeofday");
    245 		}
    246 		logwtmp("{", "date", "");
    247 	}
    248 
    249 	if ((p = getlogin()) == NULL)
    250 		p = "???";
    251 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
    252 }
    253 
    254 static void
    255 usage(void)
    256 {
    257 	(void)fprintf(stderr,
    258 	    "usage: %s [-u] [-r seconds] [+format]\n", getprogname());
    259 	(void)fprintf(stderr, "       %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n",
    260 	    getprogname());
    261 	exit(EXIT_FAILURE);
    262 	/* NOTREACHED */
    263 }
    264