xref: /netbsd/bin/date/date.c (revision bf9ec67e)
1 /* $NetBSD: date.c,v 1.35 2001/09/16 13:35:52 wiz 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n");
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
46 #else
47 __RCSID("$NetBSD: date.c,v 1.35 2001/09/16 13:35:52 wiz Exp $");
48 #endif
49 #endif /* not lint */
50 
51 #include <sys/param.h>
52 #include <sys/time.h>
53 
54 #include <ctype.h>
55 #include <err.h>
56 #include <fcntl.h>
57 #include <locale.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <time.h>
63 #include <tzfile.h>
64 #include <unistd.h>
65 #include <util.h>
66 
67 #include "extern.h"
68 
69 time_t tval;
70 int retval, nflag;
71 
72 int main(int, char *[]);
73 static void badformat(void);
74 static void badtime(void);
75 static void setthetime(const char *);
76 static void usage(void);
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	char buf[1024], *format;
82 	int ch, rflag;
83 
84 	setprogname(argv[0]);
85 	(void)setlocale(LC_ALL, "");
86 
87 	rflag = 0;
88 	while ((ch = getopt(argc, argv, "nr:u")) != -1)
89 		switch((char)ch) {
90 		case 'n':		/* don't set network */
91 			nflag = 1;
92 			break;
93 		case 'r':		/* user specified seconds */
94 			rflag = 1;
95 			tval = strtol(optarg, NULL, 0);
96 			break;
97 		case 'u':		/* do everything in UTC */
98 			(void)putenv("TZ=UTC0");
99 			break;
100 		default:
101 			usage();
102 		}
103 	argc -= optind;
104 	argv += optind;
105 
106 	if (!rflag && time(&tval) == -1)
107 		err(1, "time");
108 
109 	format = "%a %b %e %H:%M:%S %Z %Y";
110 
111 	/* allow the operands in any order */
112 	if (*argv && **argv == '+') {
113 		format = *argv + 1;
114 		++argv;
115 	}
116 
117 	if (*argv) {
118 		setthetime(*argv);
119 		++argv;
120 	}
121 
122 	if (*argv && **argv == '+')
123 		format = *argv + 1;
124 
125 	(void)strftime(buf, sizeof(buf), format, localtime(&tval));
126 	(void)printf("%s\n", buf);
127 	exit(retval);
128 	/* NOTREACHED */
129 }
130 
131 static void
132 badformat(void)
133 {
134 	warnx("illegal time format");
135 	usage();
136 }
137 
138 static void
139 badtime(void)
140 {
141 	errx(1, "illegal time");
142 }
143 
144 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
145 
146 static void
147 setthetime(const char *p)
148 {
149 	struct timeval tv;
150 	struct tm *lt;
151 	const char *dot, *t;
152 	int len, yearset;
153 
154 	for (t = p, dot = NULL; *t; ++t) {
155 		if (isdigit((unsigned char)*t))
156 			continue;
157 		if (*t == '.' && dot == NULL) {
158 			dot = t;
159 			continue;
160 		}
161 		badformat();
162 	}
163 
164 	lt = localtime(&tval);
165 
166 	lt->tm_isdst = -1;			/* Divine correct DST */
167 
168 	if (dot != NULL) {			/* .ss */
169 		len = strlen(dot);
170 		if (len != 3)
171 			badformat();
172 		++dot;
173 		lt->tm_sec = ATOI2(dot);
174 	} else {
175 		len = 0;
176 		lt->tm_sec = 0;
177 	}
178 
179 	yearset = 0;
180 	switch (strlen(p) - len) {
181 	case 12:				/* cc */
182 		lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
183 		yearset = 1;
184 		/* FALLTHROUGH */
185 	case 10:				/* yy */
186 		if (yearset) {
187 			lt->tm_year += ATOI2(p);
188 		} else {
189 			yearset = ATOI2(p);
190 			if (yearset < 69)
191 				lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
192 			else
193 				lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
194 		}
195 		/* FALLTHROUGH */
196 	case 8:					/* mm */
197 		lt->tm_mon = ATOI2(p);
198 		--lt->tm_mon;			/* time struct is 0 - 11 */
199 		/* FALLTHROUGH */
200 	case 6:					/* dd */
201 		lt->tm_mday = ATOI2(p);
202 		/* FALLTHROUGH */
203 	case 4:					/* hh */
204 		lt->tm_hour = ATOI2(p);
205 		/* FALLTHROUGH */
206 	case 2:					/* mm */
207 		lt->tm_min = ATOI2(p);
208 		break;
209 	default:
210 		badformat();
211 	}
212 
213 	/* convert broken-down time to UTC clock time */
214 	if ((tval = mktime(lt)) == -1)
215 		badtime();
216 
217 	/* set the time */
218 	if (nflag || netsettime(tval)) {
219 		logwtmp("|", "date", "");
220 		tv.tv_sec = tval;
221 		tv.tv_usec = 0;
222 		if (settimeofday(&tv, NULL)) {
223 			perror("date: settimeofday");
224 			exit(1);
225 		}
226 		logwtmp("{", "date", "");
227 	}
228 
229 	if ((p = getlogin()) == NULL)
230 		p = "???";
231 	syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
232 }
233 
234 static void
235 usage(void)
236 {
237 	(void)fprintf(stderr,
238 	    "usage: %s [-nu] [-r seconds] [+format]\n", getprogname());
239 	(void)fprintf(stderr, "       %s [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname());
240 	exit(1);
241 	/* NOTREACHED */
242 }
243