date.c revision 1.18 1 /* $NetBSD: date.c,v 1.18 1998/01/20 21:16:39 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
45 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
46 #else
47 __RCSID("$NetBSD: date.c,v 1.18 1998/01/20 21:16:39 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 <tzfile.h>
63 #include <unistd.h>
64 #include <util.h>
65
66 #include "extern.h"
67
68 time_t tval;
69 int retval, nflag;
70
71 int main __P((int, char *[]));
72 static void setthetime __P((char *));
73 static void badformat __P((void));
74 static void usage __P((void));
75
76 int
77 main(argc, argv)
78 int argc;
79 char *argv[];
80 {
81 extern int optind;
82 extern char *optarg;
83 int ch, rflag;
84 char *format, buf[1024];
85
86 (void)setlocale(LC_ALL, "");
87
88 rflag = 0;
89 while ((ch = getopt(argc, argv, "nr:u")) != -1)
90 switch((char)ch) {
91 case 'n': /* don't set network */
92 nflag = 1;
93 break;
94 case 'r': /* user specified seconds */
95 rflag = 1;
96 tval = atol(optarg);
97 break;
98 case 'u': /* do everything in GMT */
99 (void)setenv("TZ", "GMT0", 1);
100 break;
101 default:
102 usage();
103 }
104 argc -= optind;
105 argv += optind;
106
107 if (!rflag && time(&tval) == -1)
108 err(1, "time");
109
110 format = "%a %b %e %H:%M:%S %Z %Y";
111
112 /* allow the operands in any order */
113 if (*argv && **argv == '+') {
114 format = *argv + 1;
115 ++argv;
116 }
117
118 if (*argv) {
119 setthetime(*argv);
120 ++argv;
121 }
122
123 if (*argv && **argv == '+')
124 format = *argv + 1;
125
126 (void)strftime(buf, sizeof(buf), format, localtime(&tval));
127 (void)printf("%s\n", buf);
128 exit(retval);
129 /* NOTREACHED */
130 }
131
132 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
133 void
134 setthetime(p)
135 char *p;
136 {
137 struct tm *lt;
138 struct timeval tv;
139 char *dot, *t;
140 int yearset;
141
142 for (t = p, dot = NULL; *t; ++t) {
143 if (isdigit(*t))
144 continue;
145 if (*t == '.' && dot == NULL) {
146 dot = t;
147 continue;
148 }
149 badformat();
150 }
151
152 lt = localtime(&tval);
153
154 if (dot != NULL) { /* .ss */
155 *dot++ = '\0';
156 if (strlen(dot) != 2)
157 badformat();
158 lt->tm_sec = ATOI2(dot);
159 if (lt->tm_sec > 61)
160 badformat();
161 } else
162 lt->tm_sec = 0;
163
164 yearset = 0;
165 switch (strlen(p)) {
166 case 12:
167 lt->tm_year = ATOI2(p);
168 lt->tm_year *= 100;
169 yearset = 1;
170 /* FALLTHROUGH */
171 case 10: /* yy */
172 if (yearset) {
173 yearset = ATOI2(p);
174 lt->tm_year += yearset;
175 } else {
176 yearset = ATOI2(p);
177 if (yearset < 69) /* hack for 2000 ;-} */
178 lt->tm_year = yearset + 2000;
179 else
180 lt->tm_year = yearset + 1900;
181 }
182 lt->tm_year -= TM_YEAR_BASE;
183 /* FALLTHROUGH */
184 case 8: /* mm */
185 lt->tm_mon = ATOI2(p);
186 if (lt->tm_mon > 12)
187 badformat();
188 --lt->tm_mon; /* time struct is 0 - 11 */
189 /* FALLTHROUGH */
190 case 6: /* dd */
191 lt->tm_mday = ATOI2(p);
192 if (lt->tm_mday > 31)
193 badformat();
194 /* FALLTHROUGH */
195 case 4: /* hh */
196 lt->tm_hour = ATOI2(p);
197 if (lt->tm_hour > 23)
198 badformat();
199 /* FALLTHROUGH */
200 case 2: /* mm */
201 lt->tm_min = ATOI2(p);
202 if (lt->tm_min > 59)
203 badformat();
204 break;
205 default:
206 badformat();
207 }
208
209 /* convert broken-down time to GMT clock time */
210 if ((tval = mktime(lt)) == -1)
211 badformat();
212
213 /* set the time */
214 if (nflag || netsettime(tval)) {
215 logwtmp("|", "date", "");
216 tv.tv_sec = tval;
217 tv.tv_usec = 0;
218 if (settimeofday(&tv, NULL)) {
219 perror("date: settimeofday");
220 exit(1);
221 }
222 logwtmp("{", "date", "");
223 }
224
225 if ((p = getlogin()) == NULL)
226 p = "???";
227 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
228 }
229
230 static void
231 badformat()
232 {
233 warnx("illegal time format");
234 usage();
235 }
236
237 static void
238 usage()
239 {
240 (void)fprintf(stderr,
241 "usage: date [-nu] [-r seconds] [+format]\n");
242 (void)fprintf(stderr, " [yy[mm[dd[hh]]]]mm[.ss]]\n");
243 exit(1);
244 }
245