dhcpcd: Update README
[dragonfly.git] / bin / date / date.c
blob5df335228f8b691b2fa8a78e0340104b386d292e
1 /*
2 * Copyright (c) 1985, 1987, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)date.c 8.2 (Berkeley) 4/28/95
31 * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $
34 #include <sys/param.h>
35 #include <sys/time.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
44 #include <locale.h>
45 #include <libutil.h>
46 #include <utmpx.h>
48 #include "vary.h"
50 #ifndef TM_YEAR_BASE
51 #define TM_YEAR_BASE 1900
52 #endif
54 static time_t tval;
56 static void setthetime(const char *, const char *, int);
57 static void badformat(void);
58 static void usage(void);
60 static const char *rfc2822_format ="%a, %d %b %Y %T %z";
62 int
63 main(int argc, char **argv)
65 int ch, rflag;
66 int jflag, Rflag;
67 const char *format;
68 char buf[1024];
69 char *fmt;
70 char *tmp;
71 struct vary *v;
72 const struct vary *badv;
73 struct tm lt;
75 v = NULL;
76 fmt = NULL;
77 setlocale(LC_TIME, "");
78 rflag = 0;
79 jflag = Rflag = 0;
80 while ((ch = getopt(argc, argv, "f:jnRr:uv:")) != -1)
81 switch(ch) {
82 case 'f':
83 fmt = optarg;
84 break;
85 case 'j':
86 jflag = 1; /* don't set time */
87 break;
88 case 'n': /* don't set network */
89 break;
90 case 'R':
91 Rflag = 1; /*RFC 2822 datetime format */
92 break;
93 case 'r': /* user specified seconds */
94 rflag = 1;
95 tval = strtoll(optarg, &tmp, 0);
96 if (*tmp != 0)
97 usage();
98 break;
99 case 'u': /* do everything in UTC */
100 if (setenv("TZ", "UTC0", 1) != 0)
101 err(1, "setenv: cannot set TZ=UTC0");
102 break;
103 case 'v':
104 v = vary_append(v, optarg);
105 break;
106 default:
107 usage();
109 argc -= optind;
110 argv += optind;
112 if (!rflag && time(&tval) == -1)
113 err(1, "time");
115 format = "%+";
117 if (Rflag)
118 format = rfc2822_format;
120 /* allow the operands in any order */
121 if (*argv && **argv == '+') {
122 format = *argv + 1;
123 ++argv;
126 if (*argv) {
127 setthetime(fmt, *argv, jflag);
128 ++argv;
129 } else if (fmt != NULL)
130 usage();
132 if (*argv && **argv == '+')
133 format = *argv + 1;
135 lt = *localtime(&tval);
136 badv = vary_apply(v, &lt);
137 if (badv) {
138 fprintf(stderr, "%s: Cannot apply date adjustment\n",
139 badv->arg);
140 vary_destroy(v);
141 usage();
143 vary_destroy(v);
145 if (format == rfc2822_format) {
147 * When using RFC 2822 datetime format, don't honor the
148 * locale.
150 setlocale(LC_TIME, "C");
153 strftime(buf, sizeof(buf), format, &lt);
154 printf("%s\n", buf);
155 if (fflush(stdout) != 0)
156 err(1, "stdout");
157 exit(EXIT_SUCCESS);
160 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
162 static void
163 setthetime(const char *fmt, const char *p, int jflag)
165 struct tm *lt;
166 struct timeval tv;
167 const char *dot, *t;
168 int century;
170 if (fmt != NULL) {
171 lt = localtime(&tval);
172 t = strptime(p, fmt, lt);
173 if (t == NULL) {
174 fprintf(stderr, "Failed conversion of ``%s''"
175 " using format ``%s''\n", p, fmt);
176 badformat();
177 } else if (*t != '\0')
178 fprintf(stderr, "Warning: Ignoring %ld extraneous"
179 " characters in date string (%s)\n",
180 (long) strlen(t), t);
181 } else {
182 for (t = p, dot = NULL; *t; ++t) {
183 if (isdigit(*t))
184 continue;
185 if (*t == '.' && dot == NULL) {
186 dot = t;
187 continue;
189 badformat();
192 lt = localtime(&tval);
194 if (dot != NULL) { /* .ss */
195 dot++; /* *dot++ = '\0'; */
196 if (strlen(dot) != 2)
197 badformat();
198 lt->tm_sec = ATOI2(dot);
199 if (lt->tm_sec > 61)
200 badformat();
201 } else
202 lt->tm_sec = 0;
204 century = 0;
205 /* if p has a ".ss" field then let's pretend it's not there */
206 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
207 case 12: /* cc */
208 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
209 century = 1;
210 /* FALLTHROUGH */
211 case 10: /* yy */
212 if (century)
213 lt->tm_year += ATOI2(p);
214 else { /* hack for 2000 ;-} */
215 lt->tm_year = ATOI2(p);
216 if (lt->tm_year < 69)
217 lt->tm_year += 2000 - TM_YEAR_BASE;
218 else
219 lt->tm_year += 1900 - TM_YEAR_BASE;
221 /* FALLTHROUGH */
222 case 8: /* mm */
223 lt->tm_mon = ATOI2(p);
224 if (lt->tm_mon > 12)
225 badformat();
226 --lt->tm_mon; /* time struct is 0 - 11 */
227 /* FALLTHROUGH */
228 case 6: /* dd */
229 lt->tm_mday = ATOI2(p);
230 if (lt->tm_mday > 31)
231 badformat();
232 /* FALLTHROUGH */
233 case 4: /* HH */
234 lt->tm_hour = ATOI2(p);
235 if (lt->tm_hour > 23)
236 badformat();
237 /* FALLTHROUGH */
238 case 2: /* MM */
239 lt->tm_min = ATOI2(p);
240 if (lt->tm_min > 59)
241 badformat();
242 break;
243 default:
244 badformat();
248 /* Let mktime() decide whether summer time is in effect. */
249 lt->tm_isdst = -1;
251 /* convert broken-down time to GMT clock time */
252 if ((tval = mktime(lt)) == -1)
253 errx(1, "nonexistent time");
255 if (!jflag) {
256 logwtmpx("|", "date", "", 0, OLD_TIME);
257 tv.tv_sec = tval;
258 tv.tv_usec = 0;
259 if (settimeofday(&tv, NULL))
260 err(1, "settimeofday (timeval)");
261 logwtmpx("{", "date", "", 0, NEW_TIME);
263 if ((p = getlogin()) == NULL)
264 p = "???";
265 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
269 static void
270 badformat(void)
272 warnx("illegal time format");
273 usage();
276 static void
277 usage(void)
279 fprintf(stderr, "%s\n%s\n",
280 "usage: date [-jnRu] [-r seconds] [-v[+|-]val[ymwdHMS]] ... ",
282 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");
283 exit(1);