]>
Commit | Line | Data |
---|---|---|
e1a085ba | 1 | /*- |
44bd5ea7 A |
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. | |
44bd5ea7 A |
13 | * 4. 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. | |
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. | |
28 | */ | |
29 | ||
44bd5ea7 | 30 | #ifndef lint |
e1a085ba | 31 | static char const copyright[] = |
44bd5ea7 | 32 | "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ |
e1a085ba | 33 | The Regents of the University of California. All rights reserved.\n"; |
44bd5ea7 A |
34 | #endif /* not lint */ |
35 | ||
44bd5ea7 | 36 | #if 0 |
e1a085ba | 37 | #ifndef lint |
44bd5ea7 | 38 | static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; |
44bd5ea7 | 39 | #endif /* not lint */ |
e1a085ba A |
40 | #endif |
41 | ||
42 | #include <sys/cdefs.h> | |
43 | __FBSDID("$FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $"); | |
44bd5ea7 A |
44 | |
45 | #include <sys/param.h> | |
46 | #include <sys/time.h> | |
47 | ||
48 | #include <ctype.h> | |
49 | #include <err.h> | |
e1a085ba A |
50 | #include <locale.h> |
51 | #ifndef __APPLE__ | |
52 | #include <libutil.h> | |
53 | #endif /* !__APPLE__ */ | |
44bd5ea7 A |
54 | #include <stdio.h> |
55 | #include <stdlib.h> | |
56 | #include <string.h> | |
44bd5ea7 | 57 | #include <syslog.h> |
44bd5ea7 | 58 | #include <unistd.h> |
e1a085ba | 59 | #include <utmpx.h> |
44bd5ea7 | 60 | |
e1a085ba A |
61 | #ifdef __APPLE__ |
62 | #include <get_compat.h> | |
63 | #include <util.h> | |
64 | #else | |
65 | #define COMPAT_MODE(a,b) (1) | |
66 | #endif /* __APPLE__ */ | |
1c4c78a5 | 67 | |
44bd5ea7 | 68 | #include "extern.h" |
e1a085ba | 69 | #include "vary.h" |
44bd5ea7 | 70 | |
e1a085ba A |
71 | #ifndef TM_YEAR_BASE |
72 | #define TM_YEAR_BASE 1900 | |
73 | #endif | |
74 | ||
75 | static time_t tval; | |
76 | int retval; | |
77 | int unix2003_std = 0; /* to determine legacy vs std mode */ | |
44bd5ea7 | 78 | |
e1a085ba A |
79 | static void setthetime(const char *, const char *, int, int); |
80 | static void badformat(void); | |
81 | static void usage(void); | |
44bd5ea7 A |
82 | |
83 | int | |
e1a085ba | 84 | main(int argc, char *argv[]) |
44bd5ea7 | 85 | { |
e1a085ba | 86 | struct timezone tz; |
44bd5ea7 | 87 | int ch, rflag; |
e1a085ba A |
88 | int jflag, nflag; |
89 | const char *format; | |
90 | char buf[1024]; | |
91 | char *endptr, *fmt; | |
92 | char *tmp; | |
93 | int set_timezone; | |
94 | struct vary *v; | |
95 | const struct vary *badv; | |
96 | struct tm lt; | |
97 | ||
98 | unix2003_std = COMPAT_MODE("bin/date", "unix2003"); /* Determine the STD */ | |
99 | ||
100 | v = NULL; | |
101 | fmt = NULL; | |
102 | (void) setlocale(LC_TIME, ""); | |
103 | tz.tz_dsttime = tz.tz_minuteswest = 0; | |
44bd5ea7 | 104 | rflag = 0; |
e1a085ba A |
105 | jflag = nflag = 0; |
106 | set_timezone = 0; | |
107 | while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) | |
44bd5ea7 | 108 | switch((char)ch) { |
e1a085ba A |
109 | case 'd': /* daylight savings time */ |
110 | tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; | |
111 | if (endptr == optarg || *endptr != '\0') | |
112 | usage(); | |
113 | set_timezone = 1; | |
114 | break; | |
115 | case 'f': | |
116 | fmt = optarg; | |
117 | break; | |
118 | case 'j': | |
119 | jflag = 1; /* don't set time */ | |
120 | break; | |
44bd5ea7 A |
121 | case 'n': /* don't set network */ |
122 | nflag = 1; | |
123 | break; | |
124 | case 'r': /* user specified seconds */ | |
125 | rflag = 1; | |
e1a085ba A |
126 | tval = strtoq(optarg, &tmp, 0); |
127 | if (*tmp != 0) | |
128 | usage(); | |
129 | break; | |
130 | case 't': /* minutes west of UTC */ | |
131 | /* error check; don't allow "PST" */ | |
132 | tz.tz_minuteswest = strtol(optarg, &endptr, 10); | |
133 | if (endptr == optarg || *endptr != '\0') | |
134 | usage(); | |
135 | set_timezone = 1; | |
136 | break; | |
137 | case 'u': /* do everything in UTC */ | |
138 | (void)setenv("TZ", "UTC0", 1); | |
44bd5ea7 | 139 | break; |
e1a085ba A |
140 | case 'v': |
141 | v = vary_append(v, optarg); | |
44bd5ea7 A |
142 | break; |
143 | default: | |
144 | usage(); | |
145 | } | |
146 | argc -= optind; | |
147 | argv += optind; | |
148 | ||
e1a085ba A |
149 | /* |
150 | * If -d or -t, set the timezone or daylight savings time; this | |
151 | * doesn't belong here; the kernel should not know about either. | |
152 | */ | |
153 | if (set_timezone && settimeofday((struct timeval *)NULL, &tz)) | |
154 | err(1, "settimeofday (timezone)"); | |
155 | ||
44bd5ea7 A |
156 | if (!rflag && time(&tval) == -1) |
157 | err(1, "time"); | |
158 | ||
e1a085ba | 159 | format = "%+"; |
44bd5ea7 A |
160 | |
161 | /* allow the operands in any order */ | |
162 | if (*argv && **argv == '+') { | |
163 | format = *argv + 1; | |
164 | ++argv; | |
165 | } | |
166 | ||
167 | if (*argv) { | |
e1a085ba | 168 | setthetime(fmt, *argv, jflag, nflag); |
44bd5ea7 | 169 | ++argv; |
e1a085ba A |
170 | } else if (fmt != NULL) |
171 | usage(); | |
44bd5ea7 A |
172 | |
173 | if (*argv && **argv == '+') | |
174 | format = *argv + 1; | |
175 | ||
e1a085ba A |
176 | lt = *localtime(&tval); |
177 | badv = vary_apply(v, <); | |
178 | if (badv) { | |
179 | fprintf(stderr, "%s: Cannot apply date adjustment\n", | |
180 | badv->arg); | |
181 | vary_destroy(v); | |
182 | usage(); | |
183 | } | |
184 | vary_destroy(v); | |
185 | (void)strftime(buf, sizeof(buf), format, <); | |
44bd5ea7 | 186 | (void)printf("%s\n", buf); |
e1a085ba A |
187 | if (fflush(stdout)) |
188 | err(1, "stdout"); | |
189 | /* | |
190 | * If date/time could not be set/notified in the other hosts as | |
191 | * determined by netsetval(), a return value 2 is set, which is | |
192 | * only propagated back to shell in legacy mode. | |
193 | */ | |
194 | if (unix2003_std) | |
195 | exit(0); | |
196 | exit(retval); | |
44bd5ea7 A |
197 | } |
198 | ||
199 | #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) | |
e1a085ba | 200 | #define ATOI2_OFFSET(s, o) (((s)[o] - '0') * 10 + ((s)[o + 1] - '0')) |
44bd5ea7 | 201 | |
e1a085ba A |
202 | static void |
203 | setthetime(const char *fmt, const char *p, int jflag, int nflag) | |
44bd5ea7 A |
204 | { |
205 | struct tm *lt; | |
44bd5ea7 | 206 | const char *dot, *t; |
e1a085ba A |
207 | int century; |
208 | size_t length; | |
209 | ||
210 | if (fmt != NULL) { | |
211 | lt = localtime(&tval); | |
212 | t = strptime(p, fmt, lt); | |
213 | if (t == NULL) { | |
214 | fprintf(stderr, "Failed conversion of ``%s''" | |
215 | " using format ``%s''\n", p, fmt); | |
44bd5ea7 | 216 | badformat(); |
e1a085ba A |
217 | } else if (*t != '\0') |
218 | fprintf(stderr, "Warning: Ignoring %ld extraneous" | |
219 | " characters in date string (%s)\n", | |
220 | (long) strlen(t), t); | |
44bd5ea7 | 221 | } else { |
e1a085ba A |
222 | for (t = p, dot = NULL; *t; ++t) { |
223 | if (isdigit(*t)) | |
224 | continue; | |
225 | if (*t == '.' && dot == NULL) { | |
226 | dot = t; | |
227 | continue; | |
228 | } | |
229 | badformat(); | |
1c4c78a5 A |
230 | } |
231 | ||
e1a085ba A |
232 | lt = localtime(&tval); |
233 | ||
234 | if (dot != NULL) { /* .ss */ | |
235 | dot++; /* *dot++ = '\0'; */ | |
236 | if (strlen(dot) != 2) | |
237 | badformat(); | |
238 | lt->tm_sec = ATOI2(dot); | |
239 | if (lt->tm_sec > 61) | |
240 | badformat(); | |
241 | } else | |
242 | lt->tm_sec = 0; | |
243 | ||
244 | century = 0; | |
245 | /* if p has a ".ss" field then let's pretend it's not there */ | |
246 | switch (length = strlen(p) - ((dot != NULL) ? 3 : 0)) { | |
247 | case 12: /* cc */ | |
248 | lt->tm_year = (unix2003_std ? ATOI2_OFFSET(p, length - 4) : ATOI2(p)) * 100 - TM_YEAR_BASE; | |
249 | century = 1; | |
250 | /* FALLTHROUGH */ | |
251 | case 10: /* yy */ | |
252 | if (century) | |
253 | lt->tm_year += (unix2003_std ? ATOI2_OFFSET(p, length - 2) : ATOI2(p)); | |
254 | else { | |
255 | lt->tm_year = (unix2003_std ? ATOI2_OFFSET(p, length - 2) : ATOI2(p)); | |
256 | if (lt->tm_year < 69) /* hack for 2000 ;-} */ | |
257 | lt->tm_year += 2000 - TM_YEAR_BASE; | |
258 | else | |
259 | lt->tm_year += 1900 - TM_YEAR_BASE; | |
260 | } | |
261 | /* FALLTHROUGH */ | |
262 | case 8: /* mm */ | |
263 | lt->tm_mon = ATOI2(p); | |
264 | if (lt->tm_mon > 12) | |
265 | badformat(); | |
266 | --lt->tm_mon; /* time struct is 0 - 11 */ | |
267 | /* FALLTHROUGH */ | |
268 | case 6: /* dd */ | |
269 | lt->tm_mday = ATOI2(p); | |
270 | if (lt->tm_mday > 31) | |
271 | badformat(); | |
272 | /* FALLTHROUGH */ | |
273 | case 4: /* HH */ | |
274 | lt->tm_hour = ATOI2(p); | |
275 | if (lt->tm_hour > 23) | |
276 | badformat(); | |
277 | /* FALLTHROUGH */ | |
278 | case 2: /* MM */ | |
279 | lt->tm_min = ATOI2(p); | |
280 | if (lt->tm_min > 59) | |
281 | badformat(); | |
282 | break; | |
283 | default: | |
284 | badformat(); | |
44bd5ea7 | 285 | } |
44bd5ea7 A |
286 | } |
287 | ||
e1a085ba A |
288 | /* Let mktime() decide whether summer time is in effect. */ |
289 | lt->tm_isdst = -1; | |
290 | ||
44bd5ea7 A |
291 | /* convert broken-down time to GMT clock time */ |
292 | if ((tval = mktime(lt)) == -1) | |
e1a085ba A |
293 | errx(1, "nonexistent time"); |
294 | ||
295 | if (!jflag) { | |
296 | /* set the time */ | |
297 | if (nflag || netsettime(tval)) { | |
298 | struct utmpx ut; | |
299 | bzero(&ut, sizeof(ut)); | |
300 | ut.ut_type = OLD_TIME; | |
301 | (void)gettimeofday(&ut.ut_tv, NULL); | |
302 | pututxline(&ut); | |
303 | ut.ut_tv.tv_sec = tval; | |
304 | ut.ut_tv.tv_usec = 0; | |
305 | if (settimeofday(&ut.ut_tv, NULL)) | |
306 | err(1, "settimeofday (timeval)"); | |
307 | ut.ut_type = NEW_TIME; | |
308 | pututxline(&ut); | |
44bd5ea7 | 309 | } |
44bd5ea7 | 310 | |
e1a085ba A |
311 | if ((p = getlogin()) == NULL) |
312 | p = "???"; | |
313 | syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); | |
314 | } | |
44bd5ea7 A |
315 | } |
316 | ||
317 | static void | |
e1a085ba | 318 | badformat(void) |
44bd5ea7 A |
319 | { |
320 | warnx("illegal time format"); | |
321 | usage(); | |
322 | } | |
323 | ||
324 | static void | |
e1a085ba | 325 | usage(void) |
44bd5ea7 | 326 | { |
e1a085ba A |
327 | (void)fprintf(stderr, "%s\n%s\n", |
328 | "usage: date [-jnu] [-d dst] [-r seconds] [-t west] " | |
329 | "[-v[+|-]val[ymwdHMS]] ... ", | |
330 | unix2003_std ? " " | |
331 | "[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]" : | |
332 | " " | |
333 | "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); | |
44bd5ea7 | 334 | exit(1); |
44bd5ea7 | 335 | } |