Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / bin / date / date.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1985, 1987, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)date.c 8.2 (Berkeley) 4/28/95
35 * $FreeBSD: src/bin/date/date.c,v 1.32.2.6 2001/10/31 17:31:51 dillon Exp $
36 * $DragonFly: src/bin/date/date.c,v 1.2 2003/06/17 04:22:49 dillon Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/time.h>
41
42#include <ctype.h>
43#include <err.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <syslog.h>
48#include <unistd.h>
49#include <locale.h>
50
51#include "extern.h"
52#include "vary.h"
53
54#ifndef TM_YEAR_BASE
55#define TM_YEAR_BASE 1900
56#endif
57
58time_t tval;
59int retval;
60
61static void setthetime __P((const char *, const char *, int, int));
62static void badformat __P((void));
63static void usage __P((void));
64
65int logwtmp __P((char *, char *, char *));
66
67int
68main(argc, argv)
69 int argc;
70 char **argv;
71{
72 struct timezone tz;
73 int ch, rflag;
74 int jflag, nflag;
75 char *format, buf[1024];
76 char *endptr, *fmt;
77 char *tmp;
78 int set_timezone;
79 struct vary *v;
80 const struct vary *badv;
81 struct tm lt;
82
83 v = NULL;
84 fmt = NULL;
85 (void) setlocale(LC_TIME, "");
86 tz.tz_dsttime = tz.tz_minuteswest = 0;
87 rflag = 0;
88 jflag = nflag = 0;
89 set_timezone = 0;
90 while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1)
91 switch((char)ch) {
92 case 'd': /* daylight savings time */
93 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
94 if (endptr == optarg || *endptr != '\0')
95 usage();
96 set_timezone = 1;
97 break;
98 case 'f':
99 fmt = optarg;
100 break;
101 case 'j':
102 jflag = 1; /* don't set time */
103 break;
104 case 'n': /* don't set network */
105 nflag = 1;
106 break;
107 case 'r': /* user specified seconds */
108 rflag = 1;
109 tval = strtoq(optarg, &tmp, 0);
110 if (*tmp != 0)
111 usage();
112 break;
113 case 't': /* minutes west of UTC */
114 /* error check; don't allow "PST" */
115 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
116 if (endptr == optarg || *endptr != '\0')
117 usage();
118 set_timezone = 1;
119 break;
120 case 'u': /* do everything in UTC */
121 (void)setenv("TZ", "UTC0", 1);
122 break;
123 case 'v':
124 v = vary_append(v, optarg);
125 break;
126 default:
127 usage();
128 }
129 argc -= optind;
130 argv += optind;
131
132 /*
133 * If -d or -t, set the timezone or daylight savings time; this
134 * doesn't belong here; the kernel should not know about either.
135 */
136 if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
137 err(1, "settimeofday (timezone)");
138
139 if (!rflag && time(&tval) == -1)
140 err(1, "time");
141
142 format = "%+";
143
144 /* allow the operands in any order */
145 if (*argv && **argv == '+') {
146 format = *argv + 1;
147 ++argv;
148 }
149
150 if (*argv) {
151 setthetime(fmt, *argv, jflag, nflag);
152 ++argv;
153 } else if (fmt != NULL)
154 usage();
155
156 if (*argv && **argv == '+')
157 format = *argv + 1;
158
159 lt = *localtime(&tval);
160 badv = vary_apply(v, &lt);
161 if (badv) {
162 fprintf(stderr, "%s: Cannot apply date adjustment\n",
163 badv->arg);
164 vary_destroy(v);
165 usage();
166 }
167 vary_destroy(v);
168 (void)strftime(buf, sizeof(buf), format, &lt);
169 (void)printf("%s\n", buf);
170 exit(retval);
171}
172
173#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
174
175void
176setthetime(fmt, p, jflag, nflag)
177 const char *fmt;
178 register const char *p;
179 int jflag, nflag;
180{
181 register struct tm *lt;
182 struct timeval tv;
183 const char *dot, *t;
184 int century;
185
186 if (fmt != NULL) {
187 lt = localtime(&tval);
188 t = strptime(p, fmt, lt);
189 if (t == NULL) {
190 fprintf(stderr, "Failed conversion of ``%s''"
191 " using format ``%s''\n", p, fmt);
192 badformat();
193 } else if (*t != '\0')
194 fprintf(stderr, "Warning: Ignoring %ld extraneous"
195 " characters in date string (%s)\n",
196 (long) strlen(t), t);
197 } else {
198 for (t = p, dot = NULL; *t; ++t) {
199 if (isdigit(*t))
200 continue;
201 if (*t == '.' && dot == NULL) {
202 dot = t;
203 continue;
204 }
205 badformat();
206 }
207
208 lt = localtime(&tval);
209
210 if (dot != NULL) { /* .ss */
211 dot++; /* *dot++ = '\0'; */
212 if (strlen(dot) != 2)
213 badformat();
214 lt->tm_sec = ATOI2(dot);
215 if (lt->tm_sec > 61)
216 badformat();
217 } else
218 lt->tm_sec = 0;
219
220 century = 0;
221 /* if p has a ".ss" field then let's pretend it's not there */
222 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
223 case 12: /* cc */
224 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
225 century = 1;
226 /* FALLTHROUGH */
227 case 10: /* yy */
228 if (century)
229 lt->tm_year += ATOI2(p);
230 else { /* hack for 2000 ;-} */
231 lt->tm_year = ATOI2(p);
232 if (lt->tm_year < 69)
233 lt->tm_year += 2000 - TM_YEAR_BASE;
234 else
235 lt->tm_year += 1900 - TM_YEAR_BASE;
236 }
237 /* FALLTHROUGH */
238 case 8: /* mm */
239 lt->tm_mon = ATOI2(p);
240 if (lt->tm_mon > 12)
241 badformat();
242 --lt->tm_mon; /* time struct is 0 - 11 */
243 /* FALLTHROUGH */
244 case 6: /* dd */
245 lt->tm_mday = ATOI2(p);
246 if (lt->tm_mday > 31)
247 badformat();
248 /* FALLTHROUGH */
249 case 4: /* HH */
250 lt->tm_hour = ATOI2(p);
251 if (lt->tm_hour > 23)
252 badformat();
253 /* FALLTHROUGH */
254 case 2: /* MM */
255 lt->tm_min = ATOI2(p);
256 if (lt->tm_min > 59)
257 badformat();
258 break;
259 default:
260 badformat();
261 }
262 }
263
264 /* Let mktime() decide whether summer time is in effect. */
265 lt->tm_isdst = -1;
266
267 /* convert broken-down time to GMT clock time */
268 if ((tval = mktime(lt)) == -1)
269 errx(1, "nonexistent time");
270
271 if (!jflag) {
272 /* set the time */
273 if (nflag || netsettime(tval)) {
274 logwtmp("|", "date", "");
275 tv.tv_sec = tval;
276 tv.tv_usec = 0;
277 if (settimeofday(&tv, (struct timezone *)NULL))
278 err(1, "settimeofday (timeval)");
279 logwtmp("{", "date", "");
280 }
281
282 if ((p = getlogin()) == NULL)
283 p = "???";
284 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
285 }
286}
287
288static void
289badformat()
290{
291 warnx("illegal time format");
292 usage();
293}
294
295static void
296usage()
297{
298 (void)fprintf(stderr, "%s\n%s\n",
299 "usage: date [-jnu] [-d dst] [-r seconds] [-t west] "
300 "[-v[+|-]val[ymwdHMS]] ... ",
301 " "
302 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");
303 exit(1);
304}