release.sh: allow REPO and GITBRANCH env override
[minix.git] / bin / date / date.c
blob9a3d22c24a242223b5b765891eec308708f24d71
1 /* $NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg Exp $ */
3 /*
4 * Copyright (c) 1985, 1987, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
42 #else
43 __RCSID("$NetBSD: date.c,v 1.60 2011/08/27 12:55:09 joerg Exp $");
44 #endif
45 #endif /* not lint */
47 #include <sys/param.h>
48 #include <sys/time.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 #include <locale.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <syslog.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62 #include <util.h>
64 #include "extern.h"
66 static time_t tval;
67 static int aflag, jflag, rflag, nflag;
69 __dead static void badformat(void);
70 __dead static void badtime(void);
71 __dead static void badvalue(const char *);
72 static void setthetime(const char *);
73 __dead static void usage(void);
75 int
76 main(int argc, char *argv[])
78 char *buf;
79 size_t bufsiz;
80 const char *format;
81 int ch;
82 long long val;
83 struct tm *tm;
85 setprogname(argv[0]);
86 (void)setlocale(LC_ALL, "");
88 while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) {
89 switch (ch) {
90 case 'a': /* adjust time slowly */
91 aflag = 1;
92 nflag = 1;
93 break;
94 case 'd':
95 #ifndef __minix
96 rflag = 1;
97 tval = parsedate(optarg, NULL, NULL);
98 if (tval == -1)
99 #endif
100 badarg: errx(EXIT_FAILURE,
101 "Cannot parse `%s'", optarg);
102 break;
103 case 'j': /* don't set time */
104 jflag = 1;
105 break;
106 case 'n': /* don't set network */
107 nflag = 1;
108 break;
109 case 'r': /* user specified seconds */
110 errno = 0;
111 val = strtoll(optarg, &buf, 0);
112 if (optarg[0] == '\0' || *buf != '\0')
113 goto badarg;
114 if (errno == ERANGE && (val == LLONG_MAX ||
115 val == LLONG_MIN))
116 err(EXIT_FAILURE, "Bad number `%s'", optarg);
117 rflag = 1;
118 tval = (time_t)val;
119 break;
120 case 'u': /* do everything in UTC */
121 (void)setenv("TZ", "UTC0", 1);
122 break;
123 default:
124 usage();
127 argc -= optind;
128 argv += optind;
130 if (!rflag && time(&tval) == -1)
131 err(EXIT_FAILURE, "time");
134 /* allow the operands in any order */
135 if (*argv && **argv == '+') {
136 format = *argv;
137 ++argv;
138 } else
139 format = "+%a %b %e %H:%M:%S %Z %Y";
141 if (*argv) {
142 setthetime(*argv);
143 ++argv;
146 if (*argv && **argv == '+')
147 format = *argv;
149 if ((buf = malloc(bufsiz = 1024)) == NULL)
150 goto bad;
152 if ((tm = localtime(&tval)) == NULL)
153 err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
155 while (strftime(buf, bufsiz, format, tm) == 0)
156 if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
157 goto bad;
159 (void)printf("%s\n", buf + 1);
160 free(buf);
161 return 0;
162 bad:
163 err(EXIT_FAILURE, "Cannot allocate format buffer");
166 static void
167 badformat(void)
169 warnx("illegal time format");
170 usage();
173 static void
174 badtime(void)
176 errx(EXIT_FAILURE, "illegal time");
177 /* NOTREACHED */
180 static void
181 badvalue(const char *param)
183 warnx("invalid %s supplied", param);
184 usage();
187 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
189 static void
190 setthetime(const char *p)
192 struct timeval tv;
193 time_t new_time;
194 struct tm *lt;
195 const char *dot, *t;
196 size_t len;
197 int yearset;
199 for (t = p, dot = NULL; *t; ++t) {
200 if (isdigit((unsigned char)*t))
201 continue;
202 if (*t == '.' && dot == NULL) {
203 dot = t;
204 continue;
206 badformat();
209 if ((lt = localtime(&tval)) == NULL)
210 err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
212 lt->tm_isdst = -1; /* Divine correct DST */
214 if (dot != NULL) { /* .ss */
215 len = strlen(dot);
216 if (len != 3)
217 badformat();
218 ++dot;
219 lt->tm_sec = ATOI2(dot);
220 if (lt->tm_sec > 61)
221 badvalue("seconds");
222 } else {
223 len = 0;
224 lt->tm_sec = 0;
227 yearset = 0;
228 switch (strlen(p) - len) {
229 case 12: /* cc */
230 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
231 if (lt->tm_year < 0)
232 badtime();
233 yearset = 1;
234 /* FALLTHROUGH */
235 case 10: /* yy */
236 if (yearset) {
237 lt->tm_year += ATOI2(p);
238 } else {
239 yearset = ATOI2(p);
240 if (yearset < 69)
241 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
242 else
243 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
245 /* FALLTHROUGH */
246 case 8: /* mm */
247 lt->tm_mon = ATOI2(p);
248 if (lt->tm_mon > 12 || lt->tm_mon == 0)
249 badvalue("month");
250 --lt->tm_mon; /* time struct is 0 - 11 */
251 /* FALLTHROUGH */
252 case 6: /* dd */
253 lt->tm_mday = ATOI2(p);
254 switch (lt->tm_mon) {
255 case 0:
256 case 2:
257 case 4:
258 case 6:
259 case 7:
260 case 9:
261 case 11:
262 if (lt->tm_mday > 31 || lt->tm_mday == 0)
263 badvalue("day of month");
264 break;
265 case 3:
266 case 5:
267 case 8:
268 case 10:
269 if (lt->tm_mday > 30 || lt->tm_mday == 0)
270 badvalue("day of month");
271 break;
272 case 1:
273 if (lt->tm_mday > 29 || lt->tm_mday == 0 ||
274 (lt->tm_mday == 29 &&
275 !isleap(lt->tm_year + TM_YEAR_BASE)))
276 badvalue("day of month");
277 break;
278 default:
279 badvalue("month");
280 break;
282 /* FALLTHROUGH */
283 case 4: /* hh */
284 lt->tm_hour = ATOI2(p);
285 if (lt->tm_hour > 23)
286 badvalue("hour");
287 /* FALLTHROUGH */
288 case 2: /* mm */
289 lt->tm_min = ATOI2(p);
290 if (lt->tm_min > 59)
291 badvalue("minute");
292 break;
293 case 0: /* was just .sss */
294 if (len != 0)
295 break;
296 /* FALLTHROUGH */
297 default:
298 badformat();
301 /* convert broken-down time to UTC clock time */
302 if ((new_time = mktime(lt)) == -1)
303 badtime();
305 /* if jflag is set, don't actually change the time, just return */
306 if (jflag) {
307 tval = new_time;
308 return;
311 /* set the time */
312 if (nflag || netsettime(new_time)) {
313 logwtmp("|", "date", "");
314 #if HAVE_ADJTIME
315 if (aflag) {
316 tv.tv_sec = new_time - tval;
317 tv.tv_usec = 0;
318 if (adjtime(&tv, NULL))
319 err(EXIT_FAILURE, "adjtime");
321 #else
322 if (aflag) {
323 err(EXIT_FAILURE, "no adjtime");
325 #endif
326 else {
327 tval = new_time;
328 tv.tv_sec = tval;
329 tv.tv_usec = 0;
330 if (settimeofday(&tv, NULL))
331 err(EXIT_FAILURE, "settimeofday");
333 logwtmp("{", "date", "");
336 if ((p = getlogin()) == NULL)
337 p = "???";
338 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
341 static void
342 usage(void)
344 (void)fprintf(stderr,
345 "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
346 getprogname());
347 (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
348 exit(EXIT_FAILURE);
349 /* NOTREACHED */