date.c revision 1.42 1 /* $NetBSD: date.c,v 1.42 2005/07/22 14:27:08 peter 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT(
35 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
36 The Regents of the University of California. All rights reserved.\n");
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
42 #else
43 __RCSID("$NetBSD: date.c,v 1.42 2005/07/22 14:27:08 peter Exp $");
44 #endif
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/time.h>
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <locale.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include <time.h>
59 #include <tzfile.h>
60 #include <unistd.h>
61 #include <util.h>
62
63 #include "extern.h"
64
65 static time_t tval;
66 static int aflag, rflag, nflag;
67 int retval;
68
69 static void badformat(void);
70 static void badtime(void);
71 static void setthetime(const char *);
72 static void usage(void);
73
74 int
75 main(int argc, char *argv[])
76 {
77 char buf[1024];
78 const char *format;
79 int ch;
80
81 setprogname(argv[0]);
82 (void)setlocale(LC_ALL, "");
83
84 while ((ch = getopt(argc, argv, "anr:u")) != -1) {
85 switch (ch) {
86 case 'a': /* adjust time slowly */
87 aflag = 1;
88 nflag = 1;
89 break;
90 case 'n': /* don't set network */
91 nflag = 1;
92 break;
93 case 'r': /* user specified seconds */
94 rflag = 1;
95 tval = strtol(optarg, NULL, 0);
96 break;
97 case 'u': /* do everything in UTC */
98 (void)putenv("TZ=UTC0");
99 break;
100 default:
101 usage();
102 }
103 }
104 argc -= optind;
105 argv += optind;
106
107 if (!rflag && time(&tval) == -1)
108 err(EXIT_FAILURE, "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 static void
133 badformat(void)
134 {
135 warnx("illegal time format");
136 usage();
137 }
138
139 static void
140 badtime(void)
141 {
142 errx(EXIT_FAILURE, "illegal time");
143 /* NOTREACHED */
144 }
145
146 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
147
148 static void
149 setthetime(const char *p)
150 {
151 struct timeval tv;
152 time_t new_time;
153 struct tm *lt;
154 const char *dot, *t;
155 int len, yearset;
156
157 for (t = p, dot = NULL; *t; ++t) {
158 if (isdigit((unsigned char)*t))
159 continue;
160 if (*t == '.' && dot == NULL) {
161 dot = t;
162 continue;
163 }
164 badformat();
165 }
166
167 lt = localtime(&tval);
168
169 lt->tm_isdst = -1; /* Divine correct DST */
170
171 if (dot != NULL) { /* .ss */
172 len = strlen(dot);
173 if (len != 3)
174 badformat();
175 ++dot;
176 lt->tm_sec = ATOI2(dot);
177 } else {
178 len = 0;
179 lt->tm_sec = 0;
180 }
181
182 yearset = 0;
183 switch (strlen(p) - len) {
184 case 12: /* cc */
185 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
186 yearset = 1;
187 /* FALLTHROUGH */
188 case 10: /* yy */
189 if (yearset) {
190 lt->tm_year += ATOI2(p);
191 } else {
192 yearset = ATOI2(p);
193 if (yearset < 69)
194 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
195 else
196 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
197 }
198 /* FALLTHROUGH */
199 case 8: /* mm */
200 lt->tm_mon = ATOI2(p);
201 --lt->tm_mon; /* time struct is 0 - 11 */
202 /* FALLTHROUGH */
203 case 6: /* dd */
204 lt->tm_mday = ATOI2(p);
205 /* FALLTHROUGH */
206 case 4: /* hh */
207 lt->tm_hour = ATOI2(p);
208 /* FALLTHROUGH */
209 case 2: /* mm */
210 lt->tm_min = ATOI2(p);
211 break;
212 case 0: /* was just .sss */
213 if (len != 0)
214 break;
215 /* FALLTHROUGH */
216 default:
217 badformat();
218 }
219
220 /* convert broken-down time to UTC clock time */
221 if ((new_time = mktime(lt)) == -1)
222 badtime();
223
224 /* set the time */
225 if (nflag || netsettime(new_time)) {
226 logwtmp("|", "date", "");
227 if (aflag) {
228 tv.tv_sec = new_time - tval;
229 tv.tv_usec = 0;
230 if (adjtime(&tv, NULL))
231 err(EXIT_FAILURE, "date: adjtime");
232 } else {
233 tval = new_time;
234 tv.tv_sec = tval;
235 tv.tv_usec = 0;
236 if (settimeofday(&tv, NULL))
237 err(EXIT_FAILURE, "date: settimeofday");
238 }
239 logwtmp("{", "date", "");
240 }
241
242 if ((p = getlogin()) == NULL)
243 p = "???";
244 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
245 }
246
247 static void
248 usage(void)
249 {
250 (void)fprintf(stderr,
251 "usage: %s [-u] [-r seconds] [+format]\n", getprogname());
252 (void)fprintf(stderr, " %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n",
253 getprogname());
254 exit(EXIT_FAILURE);
255 /* NOTREACHED */
256 }
257