Commit | Line | Data |
---|---|---|
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. | |
dc71b7ab | 13 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
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. | |
1de703da MD |
28 | * |
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 | |
8dbbbc95 | 31 | * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $ |
984263bc MD |
32 | */ |
33 | ||
984263bc MD |
34 | #include <sys/param.h> |
35 | #include <sys/time.h> | |
36 | ||
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> | |
1c755102 | 45 | #include <libutil.h> |
984263bc MD |
46 | |
47 | #include "extern.h" | |
48 | #include "vary.h" | |
49 | ||
50 | #ifndef TM_YEAR_BASE | |
51 | #define TM_YEAR_BASE 1900 | |
52 | #endif | |
53 | ||
8dbbbc95 | 54 | static time_t tval; |
984263bc MD |
55 | int retval; |
56 | ||
8dbbbc95 CP |
57 | static void setthetime(const char *, const char *, int, int); |
58 | static void badformat(void); | |
59 | static void usage(void); | |
984263bc | 60 | |
3ea318c6 RH |
61 | static const char *rfc2822_format ="%a, %d %b %Y %T %z"; |
62 | ||
984263bc | 63 | int |
b5744197 | 64 | main(int argc, char **argv) |
984263bc MD |
65 | { |
66 | struct timezone tz; | |
67 | int ch, rflag; | |
3ea318c6 | 68 | int jflag, nflag, Rflag; |
1c755102 CP |
69 | const char *format; |
70 | char buf[1024]; | |
984263bc MD |
71 | char *endptr, *fmt; |
72 | char *tmp; | |
73 | int set_timezone; | |
74 | struct vary *v; | |
75 | const struct vary *badv; | |
76 | struct tm lt; | |
77 | ||
78 | v = NULL; | |
79 | fmt = NULL; | |
57fed2af | 80 | setlocale(LC_TIME, ""); |
984263bc MD |
81 | tz.tz_dsttime = tz.tz_minuteswest = 0; |
82 | rflag = 0; | |
3ea318c6 | 83 | jflag = nflag = Rflag = 0; |
984263bc | 84 | set_timezone = 0; |
3ea318c6 | 85 | while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1) |
8dbbbc95 | 86 | switch(ch) { |
984263bc MD |
87 | case 'd': /* daylight savings time */ |
88 | tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; | |
89 | if (endptr == optarg || *endptr != '\0') | |
90 | usage(); | |
91 | set_timezone = 1; | |
92 | break; | |
93 | case 'f': | |
94 | fmt = optarg; | |
95 | break; | |
96 | case 'j': | |
97 | jflag = 1; /* don't set time */ | |
98 | break; | |
99 | case 'n': /* don't set network */ | |
100 | nflag = 1; | |
101 | break; | |
3ea318c6 RH |
102 | case 'R': |
103 | Rflag = 1; /*RFC 2822 datetime format */ | |
104 | break; | |
984263bc MD |
105 | case 'r': /* user specified seconds */ |
106 | rflag = 1; | |
1c755102 | 107 | tval = strtoll(optarg, &tmp, 0); |
984263bc MD |
108 | if (*tmp != 0) |
109 | usage(); | |
110 | break; | |
111 | case 't': /* minutes west of UTC */ | |
112 | /* error check; don't allow "PST" */ | |
113 | tz.tz_minuteswest = strtol(optarg, &endptr, 10); | |
114 | if (endptr == optarg || *endptr != '\0') | |
115 | usage(); | |
116 | set_timezone = 1; | |
117 | break; | |
118 | case 'u': /* do everything in UTC */ | |
6fb88001 SS |
119 | if (setenv("TZ", "UTC0", 1) != 0) |
120 | err(1, "setenv: cannot set TZ=UTC0"); | |
984263bc MD |
121 | break; |
122 | case 'v': | |
123 | v = vary_append(v, optarg); | |
124 | break; | |
125 | default: | |
126 | usage(); | |
127 | } | |
128 | argc -= optind; | |
129 | argv += optind; | |
130 | ||
131 | /* | |
132 | * If -d or -t, set the timezone or daylight savings time; this | |
133 | * doesn't belong here; the kernel should not know about either. | |
134 | */ | |
2038fb68 | 135 | if (set_timezone && settimeofday(NULL, &tz)) |
984263bc MD |
136 | err(1, "settimeofday (timezone)"); |
137 | ||
138 | if (!rflag && time(&tval) == -1) | |
139 | err(1, "time"); | |
140 | ||
25c9e89d | 141 | format = "%+"; |
984263bc | 142 | |
3ea318c6 RH |
143 | if (Rflag) |
144 | format = rfc2822_format; | |
145 | ||
984263bc MD |
146 | /* allow the operands in any order */ |
147 | if (*argv && **argv == '+') { | |
148 | format = *argv + 1; | |
149 | ++argv; | |
150 | } | |
151 | ||
152 | if (*argv) { | |
153 | setthetime(fmt, *argv, jflag, nflag); | |
154 | ++argv; | |
155 | } else if (fmt != NULL) | |
156 | usage(); | |
157 | ||
158 | if (*argv && **argv == '+') | |
159 | format = *argv + 1; | |
160 | ||
161 | lt = *localtime(&tval); | |
162 | badv = vary_apply(v, <); | |
163 | if (badv) { | |
164 | fprintf(stderr, "%s: Cannot apply date adjustment\n", | |
165 | badv->arg); | |
166 | vary_destroy(v); | |
167 | usage(); | |
168 | } | |
169 | vary_destroy(v); | |
3ea318c6 RH |
170 | |
171 | if (format == rfc2822_format) { | |
172 | /* | |
173 | * When using RFC 2822 datetime format, don't honor the | |
174 | * locale. | |
175 | */ | |
176 | setlocale(LC_TIME, "C"); | |
177 | } | |
178 | ||
57fed2af EN |
179 | strftime(buf, sizeof(buf), format, <); |
180 | printf("%s\n", buf); | |
b44d61c2 CP |
181 | if (fflush(stdout) != 0) |
182 | err(1, "stdout"); | |
984263bc MD |
183 | exit(retval); |
184 | } | |
185 | ||
186 | #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) | |
187 | ||
81438c6e | 188 | static void |
86a586bb | 189 | setthetime(const char *fmt, const char *p, int jflag, int nflag) |
984263bc | 190 | { |
86a586bb | 191 | struct tm *lt; |
984263bc MD |
192 | struct timeval tv; |
193 | const char *dot, *t; | |
194 | int century; | |
195 | ||
196 | if (fmt != NULL) { | |
197 | lt = localtime(&tval); | |
198 | t = strptime(p, fmt, lt); | |
199 | if (t == NULL) { | |
200 | fprintf(stderr, "Failed conversion of ``%s''" | |
201 | " using format ``%s''\n", p, fmt); | |
202 | badformat(); | |
203 | } else if (*t != '\0') | |
204 | fprintf(stderr, "Warning: Ignoring %ld extraneous" | |
205 | " characters in date string (%s)\n", | |
206 | (long) strlen(t), t); | |
207 | } else { | |
208 | for (t = p, dot = NULL; *t; ++t) { | |
209 | if (isdigit(*t)) | |
210 | continue; | |
211 | if (*t == '.' && dot == NULL) { | |
212 | dot = t; | |
213 | continue; | |
214 | } | |
215 | badformat(); | |
216 | } | |
217 | ||
218 | lt = localtime(&tval); | |
219 | ||
220 | if (dot != NULL) { /* .ss */ | |
221 | dot++; /* *dot++ = '\0'; */ | |
222 | if (strlen(dot) != 2) | |
223 | badformat(); | |
224 | lt->tm_sec = ATOI2(dot); | |
225 | if (lt->tm_sec > 61) | |
226 | badformat(); | |
227 | } else | |
228 | lt->tm_sec = 0; | |
229 | ||
230 | century = 0; | |
231 | /* if p has a ".ss" field then let's pretend it's not there */ | |
232 | switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { | |
233 | case 12: /* cc */ | |
234 | lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; | |
235 | century = 1; | |
236 | /* FALLTHROUGH */ | |
237 | case 10: /* yy */ | |
238 | if (century) | |
239 | lt->tm_year += ATOI2(p); | |
240 | else { /* hack for 2000 ;-} */ | |
241 | lt->tm_year = ATOI2(p); | |
242 | if (lt->tm_year < 69) | |
243 | lt->tm_year += 2000 - TM_YEAR_BASE; | |
244 | else | |
245 | lt->tm_year += 1900 - TM_YEAR_BASE; | |
246 | } | |
247 | /* FALLTHROUGH */ | |
248 | case 8: /* mm */ | |
249 | lt->tm_mon = ATOI2(p); | |
250 | if (lt->tm_mon > 12) | |
251 | badformat(); | |
252 | --lt->tm_mon; /* time struct is 0 - 11 */ | |
253 | /* FALLTHROUGH */ | |
254 | case 6: /* dd */ | |
255 | lt->tm_mday = ATOI2(p); | |
256 | if (lt->tm_mday > 31) | |
257 | badformat(); | |
258 | /* FALLTHROUGH */ | |
259 | case 4: /* HH */ | |
260 | lt->tm_hour = ATOI2(p); | |
261 | if (lt->tm_hour > 23) | |
262 | badformat(); | |
263 | /* FALLTHROUGH */ | |
264 | case 2: /* MM */ | |
265 | lt->tm_min = ATOI2(p); | |
266 | if (lt->tm_min > 59) | |
267 | badformat(); | |
268 | break; | |
269 | default: | |
270 | badformat(); | |
271 | } | |
272 | } | |
273 | ||
274 | /* Let mktime() decide whether summer time is in effect. */ | |
275 | lt->tm_isdst = -1; | |
276 | ||
277 | /* convert broken-down time to GMT clock time */ | |
278 | if ((tval = mktime(lt)) == -1) | |
279 | errx(1, "nonexistent time"); | |
280 | ||
281 | if (!jflag) { | |
282 | /* set the time */ | |
283 | if (nflag || netsettime(tval)) { | |
284 | logwtmp("|", "date", ""); | |
285 | tv.tv_sec = tval; | |
286 | tv.tv_usec = 0; | |
2038fb68 | 287 | if (settimeofday(&tv, NULL)) |
984263bc MD |
288 | err(1, "settimeofday (timeval)"); |
289 | logwtmp("{", "date", ""); | |
290 | } | |
291 | ||
292 | if ((p = getlogin()) == NULL) | |
293 | p = "???"; | |
294 | syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); | |
295 | } | |
296 | } | |
297 | ||
298 | static void | |
b5744197 | 299 | badformat(void) |
984263bc MD |
300 | { |
301 | warnx("illegal time format"); | |
302 | usage(); | |
303 | } | |
304 | ||
305 | static void | |
b5744197 | 306 | usage(void) |
984263bc | 307 | { |
57fed2af | 308 | fprintf(stderr, "%s\n%s\n", |
3ea318c6 | 309 | "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] " |
984263bc MD |
310 | "[-v[+|-]val[ymwdHMS]] ... ", |
311 | " " | |
312 | "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); | |
313 | exit(1); | |
314 | } |