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