]> git.saurik.com Git - apple/shell_cmds.git/blame - date/date.c
shell_cmds-74.1.tar.gz
[apple/shell_cmds.git] / date / date.c
CommitLineData
44bd5ea7
A
1/* $NetBSD: date.c,v 1.25 1998/07/28 11:41:47 mycroft Exp $ */
2
3/*
4 * Copyright (c) 1985, 1987, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__COPYRIGHT(
39"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
46#else
47__RCSID("$NetBSD: date.c,v 1.25 1998/07/28 11:41:47 mycroft Exp $");
48#endif
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/time.h>
53
54#include <ctype.h>
55#include <err.h>
56#include <fcntl.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <locale.h>
61#include <syslog.h>
62#include <time.h>
63#include <tzfile.h>
64#include <unistd.h>
65#include <util.h>
66
1c4c78a5
A
67#include "get_compat.h"
68
44bd5ea7
A
69#include "extern.h"
70
71time_t tval;
1c4c78a5
A
72int nflag;
73int retval = 0;
74int unix2003_std = 0; /* to determine legacy vs std mode */
44bd5ea7
A
75
76int main __P((int, char *[]));
77static void setthetime __P((const char *));
78static void badformat __P((void));
79static void badtime __P((void));
80static void usage __P((void));
81
82int
83main(argc, argv)
84 int argc;
85 char *argv[];
86{
87 extern int optind;
88 extern char *optarg;
89 int ch, rflag;
90 char *format, buf[1024];
91
92 (void)setlocale(LC_ALL, "");
93
94 rflag = 0;
95 while ((ch = getopt(argc, argv, "nr:u")) != -1)
96 switch((char)ch) {
97 case 'n': /* don't set network */
98 nflag = 1;
99 break;
100 case 'r': /* user specified seconds */
101 rflag = 1;
102 tval = atol(optarg);
103 break;
104 case 'u': /* do everything in GMT */
105 (void)setenv("TZ", "GMT0", 1);
106 break;
107 default:
108 usage();
109 }
110 argc -= optind;
111 argv += optind;
112
113 if (!rflag && time(&tval) == -1)
114 err(1, "time");
115
116 format = "%a %b %e %H:%M:%S %Z %Y";
117
118 /* allow the operands in any order */
119 if (*argv && **argv == '+') {
120 format = *argv + 1;
121 ++argv;
122 }
123
124 if (*argv) {
125 setthetime(*argv);
126 ++argv;
127 }
128
129 if (*argv && **argv == '+')
130 format = *argv + 1;
131
132 (void)strftime(buf, sizeof(buf), format, localtime(&tval));
133 (void)printf("%s\n", buf);
1c4c78a5
A
134
135 /* if date/time could not be set/notified in the other hosts as
136 determined by netsetval() a return value 2 is set, which is
137 to be propogated back to shell in the legacy mode.
138 */
139 if( unix2003_std )
140 exit(0); /* set/notify time thru NTPD isn't stds */
141 else
142 exit(retval); /* Propogate the error condition set, if any */
44bd5ea7
A
143}
144
145#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
146
147void
148setthetime(p)
149 const char *p;
150{
1c4c78a5 151
44bd5ea7
A
152 struct tm *lt;
153 struct timeval tv;
154 const char *dot, *t;
155 int yearset, len;
156
1c4c78a5
A
157 char tmp1_p[5] = ""; /* to hold ccyy and reformat */
158 char tmp2_p[16] = ""; /* ccyyMMddhhmm.ss is 15 chars */
159
44bd5ea7
A
160 for (t = p, dot = NULL; *t; ++t) {
161 if (isdigit(*t))
162 continue;
163 if (*t == '.' && dot == NULL) {
164 dot = t;
165 continue;
166 }
167 badformat();
168 }
169
170 lt = localtime(&tval);
171
172 if (dot != NULL) { /* .ss */
173 len = strlen(dot);
174 if (len != 3)
175 badformat();
176 ++dot;
177 lt->tm_sec = ATOI2(dot);
178 } else {
179 len = 0;
180 lt->tm_sec = 0;
181 }
182
183 yearset = 0;
1c4c78a5
A
184
185 if (compat_mode("bin/date", "unix2003")) /* Determine the STD */
186 unix2003_std = 1;
187 else
188 unix2003_std = 0;
189
44bd5ea7
A
190 switch (strlen(p) - len) {
191 case 12: /* cc */
1c4c78a5
A
192 if(unix2003_std) {
193 /* The last 4 chars are ccyy;
194 reformat it to be in the first */
195 strncpy(tmp1_p, &p[8], 4);
196 tmp1_p[4] = '\0';
197 p[8] = '\0'; /* .ss already processed; so no harm */
198 strcpy(tmp2_p, p);
199 strcpy(p, tmp1_p);
200 strcat(p, tmp2_p);
201 }
202
44bd5ea7
A
203 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
204 yearset = 1;
205 /* FALLTHROUGH */
206 case 10: /* yy */
1c4c78a5
A
207 if(unix2003_std) {
208 /* The last 2 chars are yy; reformat it to be in the
209 first, only if already not done. */
210 if (tmp1_p[0] == '\0') {
211 strncpy(tmp1_p, &p[8], 2);
212 tmp1_p[2] = '\0';
213 p[8] = '\0'; /* .ss done; so no harm */
214 strcpy(tmp2_p, p);
215 strcpy(p, tmp1_p);
216 strcat(p, tmp2_p);
217 } else
218 ; /* do nothing, already reformatted */
219 }
220
44bd5ea7
A
221 if (yearset) {
222 lt->tm_year += ATOI2(p);
223 } else {
224 yearset = ATOI2(p);
225 if (yearset < 69)
226 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
227 else
228 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
229 }
230 /* FALLTHROUGH */
231 case 8: /* mm */
232 lt->tm_mon = ATOI2(p);
233 --lt->tm_mon; /* time struct is 0 - 11 */
234 /* FALLTHROUGH */
235 case 6: /* dd */
236 lt->tm_mday = ATOI2(p);
237 /* FALLTHROUGH */
238 case 4: /* hh */
239 lt->tm_hour = ATOI2(p);
240 /* FALLTHROUGH */
241 case 2: /* mm */
242 lt->tm_min = ATOI2(p);
243 break;
244 default:
245 badformat();
246 }
247
248 /* convert broken-down time to GMT clock time */
249 if ((tval = mktime(lt)) == -1)
250 badtime();
251
252 /* set the time */
253 if (nflag || netsettime(tval)) {
254 logwtmp("|", "date", "");
255 tv.tv_sec = tval;
256 tv.tv_usec = 0;
257 if (settimeofday(&tv, NULL)) {
258 perror("date: settimeofday");
259 exit(1);
260 }
261 logwtmp("{", "date", "");
262 }
263
264 if ((p = getlogin()) == NULL)
265 p = "???";
266 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
267}
268
269static void
270badformat()
271{
272 warnx("illegal time format");
273 usage();
274}
275
276static void
277badtime()
278{
279 errx(1, "illegal time");
280}
281
282static void
283usage()
284{
285 (void)fprintf(stderr,
286 "usage: date [-nu] [-r seconds] [+format]\n");
1c4c78a5
A
287 if (unix2003_std)
288 (void)fprintf(stderr, " date [-u] mmddhhmm[[cc]yy]\n");
289 else
290 (void)fprintf(stderr, " date [[[[[cc]yy]mm]dd]hh]mm[.ss]\n");
44bd5ea7
A
291 exit(1);
292 /* NOTREACHED */
293}