xref: /openbsd/bin/date/date.c (revision 264ca280)
1 /*	$OpenBSD: date.c,v 1.49 2015/10/09 01:37:06 deraadt Exp $	*/
2 /*	$NetBSD: date.c,v 1.11 1995/09/07 06:21:05 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1985, 1987, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/time.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <locale.h>
43 #include <syslog.h>
44 #include <time.h>
45 #include <unistd.h>
46 #include <util.h>
47 
48 extern	char *__progname;
49 
50 time_t tval;
51 int jflag;
52 int slidetime;
53 
54 static void setthetime(char *);
55 static void badformat(void);
56 static void usage(void);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	struct timezone tz;
62 	const char *errstr;
63 	struct tm *tp;
64 	int ch, rflag;
65 	char *format, buf[1024], *outzone = NULL;
66 
67 	setlocale(LC_ALL, "");
68 
69 	tz.tz_dsttime = tz.tz_minuteswest = 0;
70 	rflag = 0;
71 	while ((ch = getopt(argc, argv, "ad:jr:ut:z:")) != -1)
72 		switch(ch) {
73 		case 'd':		/* daylight saving time */
74 			tz.tz_dsttime = atoi(optarg) ? 1 : 0;
75 			break;
76 		case 'a':
77 			slidetime = 1;
78 			break;
79 		case 'j':		/* don't set */
80 			jflag = 1;
81 			break;
82 		case 'r':		/* user specified seconds */
83 			rflag = 1;
84 			tval = atoll(optarg);
85 			break;
86 		case 'u':		/* do everything in UTC */
87 			if (setenv("TZ", "UTC", 1) == -1)
88 				err(1, "cannot unsetenv TZ");
89 			break;
90 		case 't':		/* minutes west of GMT */
91 			tz.tz_minuteswest = strtonum(optarg, 0, 24*60-1, &errstr);
92 			if (errstr)
93 				errx(1, "-t %s: %s", optarg, errstr);
94 			break;
95 		case 'z':
96 			outzone = optarg;
97 			break;
98 		default:
99 			usage();
100 		}
101 	argc -= optind;
102 	argv += optind;
103 
104 	/*
105 	 * If -d or -t, set the timezone or daylight saving time; this
106 	 * doesn't belong here, the kernel should not know about either.
107 	 */
108 	if ((tz.tz_minuteswest || tz.tz_dsttime) &&
109 	    settimeofday(NULL, &tz))
110 		err(1, "settimeofday");
111 
112 	if (!rflag && time(&tval) == -1)
113 		err(1, "time");
114 
115 	format = "%a %b %e %H:%M:%S %Z %Y";
116 
117 	/* allow the operands in any order */
118 	if (*argv && **argv == '+') {
119 		format = *argv + 1;
120 		argv++;
121 		argc--;
122 	}
123 
124 	if (*argv) {
125 		setthetime(*argv);
126 		argv++;
127 		argc--;
128 	}
129 
130 	if (pledge("stdio rpath wpath", NULL) == -1)
131 		err(1, "pledge");
132 
133 	if (*argv && **argv == '+') {
134 		format = *argv + 1;
135 		argc--;
136 	}
137 
138 	if (argc > 0)
139 		errx(1, "too many arguments");
140 
141 	if (outzone)
142 		setenv("TZ", outzone, 1);
143 
144 	tp = localtime(&tval);
145 	if (tp == NULL)
146 		errx(1, "conversion error");
147 	(void)strftime(buf, sizeof(buf), format, tp);
148 	(void)printf("%s\n", buf);
149 	exit(0);
150 }
151 
152 #define	ATOI2(ar)	((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0'))
153 void
154 setthetime(char *p)
155 {
156 	struct tm *lt;
157 	struct timeval tv;
158 	char *dot, *t;
159 	int yearset = 0;
160 
161 	for (t = p, dot = NULL; *t; ++t) {
162 		if (isdigit((unsigned char)*t))
163 			continue;
164 		if (*t == '.' && dot == NULL) {
165 			dot = t;
166 			continue;
167 		}
168 		badformat();
169 	}
170 
171 	lt = localtime(&tval);
172 
173 	lt->tm_isdst = -1;			/* correct for DST */
174 
175 	if (dot != NULL) {			/* .SS */
176 		*dot++ = '\0';
177 		if (strlen(dot) != 2)
178 			badformat();
179 		lt->tm_sec = ATOI2(dot);
180 		if (lt->tm_sec > 61)
181 			badformat();
182 	} else
183 		lt->tm_sec = 0;
184 
185 	switch (strlen(p)) {
186 	case 12:				/* cc */
187 		lt->tm_year = (ATOI2(p) * 100) - 1900;
188 		yearset = 1;
189 		/* FALLTHROUGH */
190 	case 10:				/* yy */
191 		if (!yearset) {
192 			/* mask out current year, leaving only century */
193 			lt->tm_year = ((lt->tm_year / 100) * 100);
194 		}
195 		lt->tm_year += ATOI2(p);
196 		/* FALLTHROUGH */
197 	case 8:					/* mm */
198 		lt->tm_mon = ATOI2(p);
199 		if ((lt->tm_mon > 12) || !lt->tm_mon)
200 			badformat();
201 		--lt->tm_mon;			/* time struct is 0 - 11 */
202 		/* FALLTHROUGH */
203 	case 6:					/* dd */
204 		lt->tm_mday = ATOI2(p);
205 		if ((lt->tm_mday > 31) || !lt->tm_mday)
206 			badformat();
207 		/* FALLTHROUGH */
208 	case 4:					/* HH */
209 		lt->tm_hour = ATOI2(p);
210 		if (lt->tm_hour > 23)
211 			badformat();
212 		/* FALLTHROUGH */
213 	case 2:					/* MM */
214 		lt->tm_min = ATOI2(p);
215 		if (lt->tm_min > 59)
216 			badformat();
217 		break;
218 	default:
219 		badformat();
220 	}
221 
222 	/* convert broken-down time to UTC clock time */
223 	if ((tval = mktime(lt)) < 0)
224 		errx(1, "specified date is outside allowed range");
225 
226 	if (jflag)
227 		return;
228 
229 	/* set the time */
230 	if (slidetime) {
231 		struct timeval tv_current;
232 
233 		if (gettimeofday(&tv_current, NULL) == -1)
234 			err(1, "Could not get local time of day");
235 
236 		tv.tv_sec = tval - tv_current.tv_sec;
237 		tv.tv_usec = 0;
238 		if (adjtime(&tv, NULL) == -1)
239 			errx(1, "adjtime");
240 	} else {
241 #ifndef SMALL
242 		logwtmp("|", "date", "");
243 #endif
244 		tv.tv_sec = tval;
245 		tv.tv_usec = 0;
246 		if (settimeofday(&tv, NULL))
247 			err(1, "settimeofday");
248 #ifndef SMALL
249 		logwtmp("{", "date", "");
250 #endif
251 	}
252 
253 	if ((p = getlogin()) == NULL)
254 		p = "???";
255 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
256 }
257 
258 static void
259 badformat(void)
260 {
261 	warnx("illegal time format");
262 	usage();
263 }
264 
265 static void
266 usage(void)
267 {
268 	(void)fprintf(stderr,
269 	    "usage: %s [-aju] [-d dst] [-r seconds] [-t minutes_west] [-z output_zone]\n",
270 	     __progname);
271 	(void)fprintf(stderr,
272 	    "%-*s[+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]]\n", (int)strlen(__progname) + 8, "");
273 	exit(1);
274 }
275