date.c revision 1.54
1/* $NetBSD: date.c,v 1.54 2010/10/02 08:26:09 gson 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\
36 The Regents of the University of California.  All rights reserved.");
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.54 2010/10/02 08:26:09 gson 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, jflag, 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	static char tz_utc0[] = "TZ=UTC0";
83
84	setprogname(argv[0]);
85	(void)setlocale(LC_ALL, "");
86
87	while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) {
88		switch (ch) {
89		case 'a':		/* adjust time slowly */
90			aflag = 1;
91			nflag = 1;
92			break;
93		case 'd':
94			rflag = 1;
95			tval = parsedate(optarg, NULL, NULL);
96			if (tval == -1)
97				errx(1, "Cannot parse `%s'", optarg);
98			break;
99		case 'j':		/* don't set time */
100			jflag = 1;
101			break;
102		case 'n':		/* don't set network */
103			nflag = 1;
104			break;
105		case 'r':		/* user specified seconds */
106			rflag = 1;
107			tval = strtoll(optarg, NULL, 0);
108			break;
109		case 'u':		/* do everything in UTC */
110			(void)putenv(tz_utc0);
111			break;
112		default:
113			usage();
114		}
115	}
116	argc -= optind;
117	argv += optind;
118
119	if (!rflag && time(&tval) == -1)
120		err(EXIT_FAILURE, "time");
121
122	format = "+%a %b %e %H:%M:%S %Z %Y";
123
124	/* allow the operands in any order */
125	if (*argv && **argv == '+') {
126		format = *argv;
127		++argv;
128	}
129
130	if (*argv) {
131		setthetime(*argv);
132		++argv;
133	}
134
135	if (*argv && **argv == '+')
136		format = *argv;
137
138	if ((buf = malloc(bufsiz = 1024)) == NULL)
139		goto bad;
140	while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
141		if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
142			goto bad;
143	(void)printf("%s\n", buf+1);
144	free(buf);
145	return 0;
146bad:
147	err(1, "Cannot allocate format buffer");
148}
149
150static void
151badformat(void)
152{
153	warnx("illegal time format");
154	usage();
155}
156
157static void
158badtime(void)
159{
160	errx(EXIT_FAILURE, "illegal time");
161	/* NOTREACHED */
162}
163
164static void
165badvalue(const char *param)
166{
167	warnx("invalid %s supplied", param);
168	usage();
169}
170
171#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
172
173static void
174setthetime(const char *p)
175{
176	struct timeval tv;
177	time_t new_time;
178	struct tm *lt;
179	const char *dot, *t;
180	size_t len;
181	int yearset;
182
183	for (t = p, dot = NULL; *t; ++t) {
184		if (isdigit((unsigned char)*t))
185			continue;
186		if (*t == '.' && dot == NULL) {
187			dot = t;
188			continue;
189		}
190		badformat();
191	}
192
193	lt = localtime(&tval);
194
195	lt->tm_isdst = -1;			/* Divine correct DST */
196
197	if (dot != NULL) {			/* .ss */
198		len = strlen(dot);
199		if (len != 3)
200			badformat();
201		++dot;
202		lt->tm_sec = ATOI2(dot);
203		if (lt->tm_sec > 61)
204			badvalue("seconds");
205	} else {
206		len = 0;
207		lt->tm_sec = 0;
208	}
209
210	yearset = 0;
211	switch (strlen(p) - len) {
212	case 12:				/* cc */
213		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
214		if (lt->tm_year < 0)
215			badtime();
216		yearset = 1;
217		/* FALLTHROUGH */
218	case 10:				/* yy */
219		if (yearset) {
220			lt->tm_year += ATOI2(p);
221		} else {
222			yearset = ATOI2(p);
223			if (yearset < 69)
224				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
225			else
226				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
227		}
228		/* FALLTHROUGH */
229	case 8:					/* mm */
230		lt->tm_mon = ATOI2(p);
231		if (lt->tm_mon > 12 || lt->tm_mon == 0)
232			badvalue("month");
233		--lt->tm_mon;			/* time struct is 0 - 11 */
234		/* FALLTHROUGH */
235	case 6:					/* dd */
236		lt->tm_mday = ATOI2(p);
237		switch (lt->tm_mon) {
238		case 0:
239		case 2:
240		case 4:
241		case 6:
242		case 7:
243		case 9:
244		case 11:
245			if (lt->tm_mday > 31 || lt->tm_mday == 0)
246				badvalue("day of month");
247			break;
248		case 3:
249		case 5:
250		case 8:
251		case 10:
252			if (lt->tm_mday > 30 || lt->tm_mday == 0)
253				badvalue("day of month");
254			break;
255		case 1:
256			if (lt->tm_mday > 29 || lt->tm_mday == 0 ||
257			    (lt->tm_mday == 29 &&
258			     !isleap(lt->tm_year + TM_YEAR_BASE)))
259				badvalue("day of month");
260			break;
261		default:
262			badvalue("month");
263			break;
264		}
265		/* FALLTHROUGH */
266	case 4:					/* hh */
267		lt->tm_hour = ATOI2(p);
268		if (lt->tm_hour > 23)
269			badvalue("hour");
270		/* FALLTHROUGH */
271	case 2:					/* mm */
272		lt->tm_min = ATOI2(p);
273		if (lt->tm_min > 59)
274			badvalue("minute");
275		break;
276	case 0:					/* was just .sss */
277		if (len != 0)
278			break;
279		/* FALLTHROUGH */
280	default:
281		badformat();
282	}
283
284	/* convert broken-down time to UTC clock time */
285	if ((new_time = mktime(lt)) == -1)
286		badtime();
287
288	/* if jflag is set, don't actually change the time, just return */
289	if (jflag) {
290		tval = new_time;
291		return;
292	}
293
294	/* set the time */
295	if (nflag || netsettime(new_time)) {
296		logwtmp("|", "date", "");
297		if (aflag) {
298			tv.tv_sec = new_time - tval;
299			tv.tv_usec = 0;
300			if (adjtime(&tv, NULL))
301				err(EXIT_FAILURE, "adjtime");
302		} else {
303			tval = new_time;
304			tv.tv_sec = tval;
305			tv.tv_usec = 0;
306			if (settimeofday(&tv, NULL))
307				err(EXIT_FAILURE, "settimeofday");
308		}
309		logwtmp("{", "date", "");
310	}
311
312	if ((p = getlogin()) == NULL)
313		p = "???";
314	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
315}
316
317static void
318usage(void)
319{
320	(void)fprintf(stderr,
321	    "usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
322	(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
323	exit(EXIT_FAILURE);
324	/* NOTREACHED */
325}
326