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