date.c revision 1.41
1/* $NetBSD: date.c,v 1.41 2005/07/22 11:06:24 hubertf 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.41 2005/07/22 11:06:24 hubertf 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
69int main(int, char *[]);
70static void badformat(void);
71static void badtime(void);
72static void setthetime(const char *);
73static void usage(void);
74
75int
76main(int argc, char *argv[])
77{
78	char buf[1024];
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(1, "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	(void)strftime(buf, sizeof(buf), format, localtime(&tval));
128	(void)printf("%s\n", buf);
129	exit(retval);
130	/* NOTREACHED */
131}
132
133static void
134badformat(void)
135{
136	warnx("illegal time format");
137	usage();
138}
139
140static void
141badtime(void)
142{
143	errx(EXIT_FAILURE, "illegal time");
144	/* NOTREACHED */
145}
146
147#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
148
149static void
150setthetime(const char *p)
151{
152	struct timeval tv;
153	time_t new_time;
154	struct tm *lt;
155	const char *dot, *t;
156	int len, yearset;
157
158	for (t = p, dot = NULL; *t; ++t) {
159		if (isdigit((unsigned char)*t))
160			continue;
161		if (*t == '.' && dot == NULL) {
162			dot = t;
163			continue;
164		}
165		badformat();
166	}
167
168	lt = localtime(&tval);
169
170	lt->tm_isdst = -1;			/* Divine correct DST */
171
172	if (dot != NULL) {			/* .ss */
173		len = strlen(dot);
174		if (len != 3)
175			badformat();
176		++dot;
177		lt->tm_sec = ATOI2(dot);
178	} else {
179		len = 0;
180		lt->tm_sec = 0;
181	}
182
183	yearset = 0;
184	switch (strlen(p) - len) {
185	case 12:				/* cc */
186		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
187		yearset = 1;
188		/* FALLTHROUGH */
189	case 10:				/* yy */
190		if (yearset) {
191			lt->tm_year += ATOI2(p);
192		} else {
193			yearset = ATOI2(p);
194			if (yearset < 69)
195				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
196			else
197				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
198		}
199		/* FALLTHROUGH */
200	case 8:					/* mm */
201		lt->tm_mon = ATOI2(p);
202		--lt->tm_mon;			/* time struct is 0 - 11 */
203		/* FALLTHROUGH */
204	case 6:					/* dd */
205		lt->tm_mday = ATOI2(p);
206		/* FALLTHROUGH */
207	case 4:					/* hh */
208		lt->tm_hour = ATOI2(p);
209		/* FALLTHROUGH */
210	case 2:					/* mm */
211		lt->tm_min = ATOI2(p);
212		break;
213	case 0:					/* was just .sss */
214		if (len != 0)
215			break;
216		/* FALLTHROUGH */
217	default:
218		badformat();
219	}
220
221	/* convert broken-down time to UTC clock time */
222	if ((new_time = mktime(lt)) == -1)
223		badtime();
224
225	/* set the time */
226	if (nflag || netsettime(new_time)) {
227		logwtmp("|", "date", "");
228		if (aflag) {
229			tv.tv_sec = new_time - tval;
230			tv.tv_usec = 0;
231			if (adjtime(&tv, NULL))
232				err(1, "date: adjtime");
233		} else {
234			tval = new_time;
235			tv.tv_sec = tval;
236			tv.tv_usec = 0;
237			if (settimeofday(&tv, NULL))
238				err(1, "date: settimeofday");
239		}
240		logwtmp("{", "date", "");
241	}
242
243	if ((p = getlogin()) == NULL)
244		p = "???";
245	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
246}
247
248static void
249usage(void)
250{
251	(void)fprintf(stderr,
252	    "usage: %s [-u] [-r seconds] [+format]\n", getprogname());
253	(void)fprintf(stderr, "       %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname());
254	exit(EXIT_FAILURE);
255	/* NOTREACHED */
256}
257