date.c revision 30073
1235368Sgnn/*
2235368Sgnn * Copyright (c) 1985, 1987, 1988, 1993
3235368Sgnn *	The Regents of the University of California.  All rights reserved.
4235368Sgnn *
5235368Sgnn * Redistribution and use in source and binary forms, with or without
6235368Sgnn * modification, are permitted provided that the following conditions
7235368Sgnn * are met:
8235368Sgnn * 1. Redistributions of source code must retain the above copyright
9235368Sgnn *    notice, this list of conditions and the following disclaimer.
10235368Sgnn * 2. Redistributions in binary form must reproduce the above copyright
11235368Sgnn *    notice, this list of conditions and the following disclaimer in the
12235368Sgnn *    documentation and/or other materials provided with the distribution.
13235368Sgnn * 3. All advertising materials mentioning features or use of this software
14235368Sgnn *    must display the following acknowledgement:
15235368Sgnn *	This product includes software developed by the University of
16235368Sgnn *	California, Berkeley and its contributors.
17235368Sgnn * 4. Neither the name of the University nor the names of its contributors
18235368Sgnn *    may be used to endorse or promote products derived from this software
19235368Sgnn *    without specific prior written permission.
20235368Sgnn *
21235368Sgnn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22235368Sgnn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23235368Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24235368Sgnn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25235368Sgnn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26235368Sgnn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27235368Sgnn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28235368Sgnn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29235368Sgnn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30235368Sgnn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31235368Sgnn * SUCH DAMAGE.
32235368Sgnn *
33235368Sgnn *	$Id: date.c,v 1.7.2.5 1997/10/01 06:12:58 danny Exp $
34235368Sgnn */
35235368Sgnn
36235368Sgnn#ifndef lint
37235368Sgnnstatic char const copyright[] =
38235368Sgnn"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
39235368Sgnn	The Regents of the University of California.  All rights reserved.\n";
40235368Sgnn#endif /* not lint */
41235368Sgnn
42235368Sgnn#ifndef lint
43235368Sgnnstatic char const sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
44235368Sgnn#endif /* not lint */
45235368Sgnn
46235368Sgnn#include <sys/param.h>
47235368Sgnn#include <sys/time.h>
48235368Sgnn
49235368Sgnn#include <ctype.h>
50235368Sgnn#include <err.h>
51235368Sgnn#include <fcntl.h>
52235368Sgnn#include <stdio.h>
53235368Sgnn#include <stdlib.h>
54235368Sgnn#include <string.h>
55235368Sgnn#include <syslog.h>
56235368Sgnn#include <unistd.h>
57235368Sgnn#include <locale.h>
58235368Sgnn
59235368Sgnn#include "extern.h"
60235368Sgnn#include "vary.h"
61235368Sgnn
62235368Sgnntime_t tval;
63235368Sgnnint retval, nflag;
64235368Sgnn
65235368Sgnnstatic void setthetime __P((const char *, const char *));
66235368Sgnnstatic void badformat __P((void));
67235368Sgnnstatic void usage __P((void));
68235368Sgnn
69235368Sgnnint logwtmp __P((char *, char *, char *));
70235368Sgnn
71235368Sgnnint
72235368Sgnnmain(argc, argv)
73235368Sgnn	int argc;
74235368Sgnn	char **argv;
75235368Sgnn{
76235368Sgnn	extern int optind;
77235368Sgnn	extern char *optarg;
78235368Sgnn	struct timezone tz;
79235368Sgnn	int ch, rflag;
80235368Sgnn	char *format, buf[1024];
81235368Sgnn	char *endptr, *fmt;
82235368Sgnn	int set_timezone;
83235368Sgnn	struct vary *v;
84235368Sgnn	const struct vary *badv;
85235368Sgnn	struct tm lt;
86235368Sgnn
87235368Sgnn	v = NULL;
88235368Sgnn	fmt = NULL;
89235368Sgnn	(void) setlocale(LC_TIME, "");
90235368Sgnn	tz.tz_dsttime = tz.tz_minuteswest = 0;
91235368Sgnn	rflag = 0;
92235368Sgnn	set_timezone = 0;
93235368Sgnn	while ((ch = getopt(argc, argv, "d:f:nr:t:uv:")) != -1)
94235368Sgnn		switch((char)ch) {
95235368Sgnn		case 'd':		/* daylight savings time */
96235368Sgnn			tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
97235368Sgnn			if (endptr == optarg || *endptr != '\0')
98235368Sgnn				usage();
99235368Sgnn			set_timezone = 1;
100235368Sgnn			break;
101235368Sgnn		case 'f':
102235368Sgnn			fmt = optarg;
103235368Sgnn			break;
104235368Sgnn		case 'n':		/* don't set network */
105235368Sgnn			nflag = 1;
106235368Sgnn			break;
107235368Sgnn		case 'r':		/* user specified seconds */
108235368Sgnn			rflag = 1;
109235368Sgnn			tval = atol(optarg);
110235368Sgnn			break;
111235368Sgnn		case 't':		/* minutes west of GMT */
112235368Sgnn					/* error check; don't allow "PST" */
113235368Sgnn			tz.tz_minuteswest = strtol(optarg, &endptr, 10);
114235368Sgnn			if (endptr == optarg || *endptr != '\0')
115235368Sgnn				usage();
116235368Sgnn			set_timezone = 1;
117235368Sgnn			break;
118235368Sgnn		case 'u':		/* do everything in GMT */
119235368Sgnn			(void)setenv("TZ", "GMT0", 1);
120235368Sgnn			break;
121235368Sgnn		case 'v':
122235368Sgnn			v = vary_append(v, optarg);
123235368Sgnn			break;
124235368Sgnn		default:
125235368Sgnn			usage();
126235368Sgnn		}
127235368Sgnn	argc -= optind;
128235368Sgnn	argv += optind;
129235368Sgnn
130235368Sgnn	/*
131235368Sgnn	 * If -d or -t, set the timezone or daylight savings time; this
132235368Sgnn	 * doesn't belong here; the kernel should not know about either.
133235368Sgnn	 */
134235368Sgnn	if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
135235368Sgnn		err(1, "settimeofday (timezone)");
136235368Sgnn
137235368Sgnn	if (!rflag && time(&tval) == -1)
138235368Sgnn		err(1, "time");
139235368Sgnn
140235368Sgnn	format = "%+";
141235368Sgnn
142235368Sgnn	/* allow the operands in any order */
143235368Sgnn	if (*argv && **argv == '+') {
144235368Sgnn		format = *argv + 1;
145235368Sgnn		++argv;
146235368Sgnn	}
147235368Sgnn
148235368Sgnn	if (*argv) {
149235368Sgnn		setthetime(fmt, *argv);
150235368Sgnn		++argv;
151235368Sgnn	} else if (fmt != NULL)
152235368Sgnn		usage();
153235368Sgnn
154235368Sgnn	if (*argv && **argv == '+')
155235368Sgnn		format = *argv + 1;
156
157	lt = *localtime(&tval);
158	badv = vary_apply(v, &lt);
159	if (badv) {
160		fprintf(stderr, "%s: Cannot apply date adjustment\n",
161			badv->arg);
162		vary_destroy(v);
163		usage();
164	}
165	vary_destroy(v);
166	(void)strftime(buf, sizeof(buf), format, &lt);
167	(void)printf("%s\n", buf);
168	exit(retval);
169}
170
171#define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
172void
173setthetime(fmt, p)
174	const char *fmt;
175	register const char *p;
176{
177	register struct tm *lt;
178	struct timeval tv;
179	const char *dot, *t;
180
181	if (fmt != NULL) {
182		lt = localtime(&tval);
183		t = strptime(p, fmt, lt);
184		if (t == NULL) {
185			fprintf(stderr, "Failed conversion of ``%s''"
186				" using format ``%s''\n", p, fmt);
187			lt = localtime(&tval);
188			return;
189		} else if (*t != '\0')
190			fprintf(stderr, "Warning: Ignoring %d extraneous"
191				" characters in date string (%s)\n",
192				strlen(t), t);
193	} else {
194		for (t = p, dot = NULL; *t; ++t) {
195			if (isdigit(*t))
196				continue;
197			if (*t == '.' && dot == NULL) {
198				dot = t;
199				continue;
200			}
201			badformat();
202		}
203
204		lt = localtime(&tval);
205
206		if (dot != NULL) {			/* .ss */
207			dot++; /* *dot++ = '\0'; */
208			if (strlen(dot) != 2)
209				badformat();
210			lt->tm_sec = ATOI2(dot);
211			if (lt->tm_sec > 61)
212				badformat();
213		} else
214			lt->tm_sec = 0;
215
216		/* if p has a ".ss" field then let's pretend it's not there */
217		switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
218		case 10:				/* yy */
219			lt->tm_year = ATOI2(p);
220			if (lt->tm_year < 69)		/* hack for 2000 ;-} */
221				lt->tm_year += 100;
222			/* FALLTHROUGH */
223		case 8:					/* mm */
224			lt->tm_mon = ATOI2(p);
225			if (lt->tm_mon > 12)
226				badformat();
227			--lt->tm_mon;		/* time struct is 0 - 11 */
228			/* FALLTHROUGH */
229		case 6:					/* dd */
230			lt->tm_mday = ATOI2(p);
231			if (lt->tm_mday > 31)
232				badformat();
233			/* FALLTHROUGH */
234		case 4:					/* HH */
235			lt->tm_hour = ATOI2(p);
236			if (lt->tm_hour > 23)
237				badformat();
238			/* FALLTHROUGH */
239		case 2:					/* MM */
240			lt->tm_min = ATOI2(p);
241			if (lt->tm_min > 59)
242				badformat();
243			break;
244		default:
245			badformat();
246		}
247	}
248
249	/* convert broken-down time to GMT clock time */
250	if ((tval = mktime(lt)) == -1)
251		errx(1, "nonexistent time");
252
253	/* set the time */
254	if (nflag || netsettime(tval)) {
255		logwtmp("|", "date", "");
256		tv.tv_sec = tval;
257		tv.tv_usec = 0;
258		if (settimeofday(&tv, (struct timezone *)NULL))
259			err(1, "settimeofday (timeval)");
260		logwtmp("{", "date", "");
261	}
262
263	if ((p = getlogin()) == NULL)
264		p = "???";
265	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
266}
267
268static void
269badformat()
270{
271	warnx("illegal time format");
272	usage();
273}
274
275static void
276usage()
277{
278	(void)fprintf(stderr, "%s\n%s\n",
279	    "usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]",
280	    "            [-v [+|-]val[ymwdHM]] ... [-f fmt date | [[[[yy]mm]dd]HH]MM[.ss]]");
281	exit(1);
282}
283