date.c revision 1.45
1/* $NetBSD: date.c,v 1.45 2006/10/07 09:34:46 elad 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
41static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
42#else
43__RCSID("$NetBSD: date.c,v 1.45 2006/10/07 09:34:46 elad 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
65static time_t tval;
66static int aflag, rflag, nflag;
67int retval;
68
69static void badformat(void);
70static void badtime(void);
71static void badvalue(const char *);
72static void setthetime(const char *);
73static void usage(void);
74
75int
76main(int argc, char *argv[])
77{
78	char *buf;
79	size_t bufsiz;
80	const char *format;
81	int ch;
82
83	setprogname(argv[0]);
84	(void)setlocale(LC_ALL, "");
85
86	while ((ch = getopt(argc, argv, "anr:u")) != -1) {
87		switch (ch) {
88		case 'a':		/* adjust time slowly */
89			aflag = 1;
90			nflag = 1;
91			break;
92		case 'n':		/* don't set network */
93			nflag = 1;
94			break;
95		case 'r':		/* user specified seconds */
96			rflag = 1;
97			tval = strtol(optarg, NULL, 0);
98			break;
99		case 'u':		/* do everything in UTC */
100			(void)putenv("TZ=UTC0");
101			break;
102		default:
103			usage();
104		}
105	}
106	argc -= optind;
107	argv += optind;
108
109	if (!rflag && time(&tval) == -1)
110		err(EXIT_FAILURE, "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	if ((buf = malloc(bufsiz = 1024)) == NULL)
129		goto bad;
130	while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
131		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
132			goto bad;
133	(void)printf("%s\n", buf);
134	free(buf);
135	return 0;
136bad:
137	err(1, "Cannot allocate format buffer");
138}
139
140static void
141badformat(void)
142{
143	warnx("illegal time format");
144	usage();
145}
146
147static void
148badtime(void)
149{
150	errx(EXIT_FAILURE, "illegal time");
151	/* NOTREACHED */
152}
153
154static void
155badvalue(const char *param)
156{
157	warnx("invalid %s supplied", param);
158	usage();
159}
160
161#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
162
163static void
164setthetime(const char *p)
165{
166	struct timeval tv;
167	time_t new_time;
168	struct tm *lt;
169	const char *dot, *t;
170	int len, yearset;
171
172	for (t = p, dot = NULL; *t; ++t) {
173		if (isdigit((unsigned char)*t))
174			continue;
175		if (*t == '.' && dot == NULL) {
176			dot = t;
177			continue;
178		}
179		badformat();
180	}
181
182	lt = localtime(&tval);
183
184	lt->tm_isdst = -1;			/* Divine correct DST */
185
186	if (dot != NULL) {			/* .ss */
187		len = strlen(dot);
188		if (len != 3)
189			badformat();
190		++dot;
191		lt->tm_sec = ATOI2(dot);
192		if (lt->tm_sec > 61)
193			badvalue("seconds");
194	} else {
195		len = 0;
196		lt->tm_sec = 0;
197	}
198
199	yearset = 0;
200	switch (strlen(p) - len) {
201	case 12:				/* cc */
202		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
203		if (lt->tm_year < 0)
204			badtime();
205		yearset = 1;
206		/* FALLTHROUGH */
207	case 10:				/* yy */
208		if (yearset) {
209			lt->tm_year += ATOI2(p);
210		} else {
211			yearset = ATOI2(p);
212			if (yearset < 69)
213				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
214			else
215				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
216		}
217		/* FALLTHROUGH */
218	case 8:					/* mm */
219		lt->tm_mon = ATOI2(p);
220		if (lt->tm_mon > 12 || lt->tm_mon == 0)
221			badvalue("month");
222		--lt->tm_mon;			/* time struct is 0 - 11 */
223		/* FALLTHROUGH */
224	case 6:					/* dd */
225		lt->tm_mday = ATOI2(p);
226		switch (lt->tm_mon) {
227		case 0:
228		case 2:
229		case 4:
230		case 6:
231		case 7:
232		case 9:
233		case 11:
234			if (lt->tm_mday > 31 || lt->tm_mday == 0)
235				badvalue("day of month");
236			break;
237		case 3:
238		case 5:
239		case 8:
240		case 10:
241			if (lt->tm_mday > 30 || lt->tm_mday == 0)
242				badvalue("day of month");
243			break;
244		case 1:
245			if (lt->tm_mday > 29 || lt->tm_mday == 0 ||
246			    (lt->tm_mday == 29 &&
247			     !isleap(lt->tm_year + TM_YEAR_BASE)))
248				badvalue("day of month");
249			break;
250		default:
251			badvalue("month");
252			break;
253		}
254		/* FALLTHROUGH */
255	case 4:					/* hh */
256		lt->tm_hour = ATOI2(p);
257		if (lt->tm_hour > 23)
258			badvalue("hour");
259		/* FALLTHROUGH */
260	case 2:					/* mm */
261		lt->tm_min = ATOI2(p);
262		if (lt->tm_min > 59)
263			badvalue("minute");
264		break;
265	case 0:					/* was just .sss */
266		if (len != 0)
267			break;
268		/* FALLTHROUGH */
269	default:
270		badformat();
271	}
272
273	/* convert broken-down time to UTC clock time */
274	if ((new_time = mktime(lt)) == -1)
275		badtime();
276
277	/* set the time */
278	if (nflag || netsettime(new_time)) {
279		logwtmp("|", "date", "");
280		if (aflag) {
281			tv.tv_sec = new_time - tval;
282			tv.tv_usec = 0;
283			if (adjtime(&tv, NULL))
284				err(EXIT_FAILURE, "adjtime");
285		} else {
286			tval = new_time;
287			tv.tv_sec = tval;
288			tv.tv_usec = 0;
289			if (settimeofday(&tv, NULL))
290				err(EXIT_FAILURE, "settimeofday");
291		}
292		logwtmp("{", "date", "");
293	}
294
295	if ((p = getlogin()) == NULL)
296		p = "???";
297	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
298}
299
300static void
301usage(void)
302{
303	(void)fprintf(stderr,
304	    "usage: %s [-u] [-r seconds] [+format]\n", getprogname());
305	(void)fprintf(stderr, "       %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n",
306	    getprogname());
307	exit(EXIT_FAILURE);
308	/* NOTREACHED */
309}
310