1 |
|
|
/* $OpenBSD: date.c,v 1.50 2016/10/19 18:20:25 schwarze Exp $ */ |
2 |
|
|
/* $NetBSD: date.c,v 1.11 1995/09/07 06:21:05 jtc Exp $ */ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* Copyright (c) 1985, 1987, 1988, 1993 |
6 |
|
|
* The Regents of the University of California. All rights reserved. |
7 |
|
|
* |
8 |
|
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
|
* modification, are permitted provided that the following conditions |
10 |
|
|
* are met: |
11 |
|
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
|
* documentation and/or other materials provided with the distribution. |
16 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
17 |
|
|
* may be used to endorse or promote products derived from this software |
18 |
|
|
* without specific prior written permission. |
19 |
|
|
* |
20 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
21 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
22 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
23 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
24 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
25 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
26 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
28 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
29 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 |
|
|
* SUCH DAMAGE. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
#include <sys/types.h> |
34 |
|
|
#include <sys/time.h> |
35 |
|
|
|
36 |
|
|
#include <ctype.h> |
37 |
|
|
#include <err.h> |
38 |
|
|
#include <fcntl.h> |
39 |
|
|
#include <stdio.h> |
40 |
|
|
#include <stdlib.h> |
41 |
|
|
#include <string.h> |
42 |
|
|
#include <syslog.h> |
43 |
|
|
#include <time.h> |
44 |
|
|
#include <unistd.h> |
45 |
|
|
#include <util.h> |
46 |
|
|
|
47 |
|
|
extern char *__progname; |
48 |
|
|
|
49 |
|
|
time_t tval; |
50 |
|
|
int jflag; |
51 |
|
|
int slidetime; |
52 |
|
|
|
53 |
|
|
static void setthetime(char *); |
54 |
|
|
static void badformat(void); |
55 |
|
|
static void __dead usage(void); |
56 |
|
|
|
57 |
|
|
int |
58 |
|
|
main(int argc, char *argv[]) |
59 |
|
|
{ |
60 |
|
222 |
struct timezone tz; |
61 |
|
111 |
const char *errstr; |
62 |
|
|
struct tm *tp; |
63 |
|
|
int ch, rflag; |
64 |
|
111 |
char *format, buf[1024], *outzone = NULL; |
65 |
|
|
|
66 |
|
111 |
tz.tz_dsttime = tz.tz_minuteswest = 0; |
67 |
|
|
rflag = 0; |
68 |
✗✓ |
222 |
while ((ch = getopt(argc, argv, "ad:jr:ut:z:")) != -1) |
69 |
|
|
switch(ch) { |
70 |
|
|
case 'd': /* daylight saving time */ |
71 |
|
|
tz.tz_dsttime = atoi(optarg) ? 1 : 0; |
72 |
|
|
break; |
73 |
|
|
case 'a': |
74 |
|
|
slidetime = 1; |
75 |
|
|
break; |
76 |
|
|
case 'j': /* don't set */ |
77 |
|
|
jflag = 1; |
78 |
|
|
break; |
79 |
|
|
case 'r': /* user specified seconds */ |
80 |
|
|
rflag = 1; |
81 |
|
|
tval = atoll(optarg); |
82 |
|
|
break; |
83 |
|
|
case 'u': /* do everything in UTC */ |
84 |
|
|
if (setenv("TZ", "UTC", 1) == -1) |
85 |
|
|
err(1, "cannot unsetenv TZ"); |
86 |
|
|
break; |
87 |
|
|
case 't': /* minutes west of GMT */ |
88 |
|
|
tz.tz_minuteswest = strtonum(optarg, 0, 24*60-1, &errstr); |
89 |
|
|
if (errstr) |
90 |
|
|
errx(1, "-t %s: %s", optarg, errstr); |
91 |
|
|
break; |
92 |
|
|
case 'z': |
93 |
|
|
outzone = optarg; |
94 |
|
|
break; |
95 |
|
|
default: |
96 |
|
|
usage(); |
97 |
|
|
} |
98 |
|
111 |
argc -= optind; |
99 |
|
111 |
argv += optind; |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* If -d or -t, set the timezone or daylight saving time; this |
103 |
|
|
* doesn't belong here, the kernel should not know about either. |
104 |
|
|
*/ |
105 |
✓✗✗✓ ✗✗ |
222 |
if ((tz.tz_minuteswest || tz.tz_dsttime) && |
106 |
|
|
settimeofday(NULL, &tz)) |
107 |
|
|
err(1, "settimeofday"); |
108 |
|
|
|
109 |
✓✗✗✓
|
222 |
if (!rflag && time(&tval) == -1) |
110 |
|
|
err(1, "time"); |
111 |
|
|
|
112 |
|
|
format = "%a %b %e %H:%M:%S %Z %Y"; |
113 |
|
|
|
114 |
|
|
/* allow the operands in any order */ |
115 |
✓✓✓✗
|
199 |
if (*argv && **argv == '+') { |
116 |
|
88 |
format = *argv + 1; |
117 |
|
88 |
argv++; |
118 |
|
88 |
argc--; |
119 |
|
88 |
} |
120 |
|
|
|
121 |
✗✓ |
111 |
if (*argv) { |
122 |
|
|
setthetime(*argv); |
123 |
|
|
argv++; |
124 |
|
|
argc--; |
125 |
|
|
} |
126 |
|
|
|
127 |
✗✓ |
111 |
if (pledge("stdio rpath wpath flock cpath", NULL) == -1) |
128 |
|
|
err(1, "pledge"); |
129 |
|
|
|
130 |
✗✓✗✗
|
111 |
if (*argv && **argv == '+') { |
131 |
|
|
format = *argv + 1; |
132 |
|
|
argc--; |
133 |
|
|
} |
134 |
|
|
|
135 |
✗✓ |
111 |
if (argc > 0) |
136 |
|
|
errx(1, "too many arguments"); |
137 |
|
|
|
138 |
✗✓ |
111 |
if (outzone) |
139 |
|
|
setenv("TZ", outzone, 1); |
140 |
|
|
|
141 |
|
111 |
tp = localtime(&tval); |
142 |
✗✓ |
111 |
if (tp == NULL) |
143 |
|
|
errx(1, "conversion error"); |
144 |
|
111 |
(void)strftime(buf, sizeof(buf), format, tp); |
145 |
|
111 |
(void)printf("%s\n", buf); |
146 |
|
111 |
return 0; |
147 |
|
111 |
} |
148 |
|
|
|
149 |
|
|
#define ATOI2(ar) ((ar) += 2, ((ar)[-2] - '0') * 10 + ((ar)[-1] - '0')) |
150 |
|
|
void |
151 |
|
|
setthetime(char *p) |
152 |
|
|
{ |
153 |
|
|
struct tm *lt; |
154 |
|
|
struct timeval tv; |
155 |
|
|
char *dot, *t; |
156 |
|
|
int yearset = 0; |
157 |
|
|
|
158 |
|
|
for (t = p, dot = NULL; *t; ++t) { |
159 |
|
|
if (isdigit((unsigned char)*t)) |
160 |
|
|
continue; |
161 |
|
|
if (*t == '.' && dot == NULL) { |
162 |
|
|
dot = t; |
163 |
|
|
continue; |
164 |
|
|
} |
165 |
|
|
badformat(); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
lt = localtime(&tval); |
169 |
|
|
|
170 |
|
|
lt->tm_isdst = -1; /* correct for DST */ |
171 |
|
|
|
172 |
|
|
if (dot != NULL) { /* .SS */ |
173 |
|
|
*dot++ = '\0'; |
174 |
|
|
if (strlen(dot) != 2) |
175 |
|
|
badformat(); |
176 |
|
|
lt->tm_sec = ATOI2(dot); |
177 |
|
|
if (lt->tm_sec > 61) |
178 |
|
|
badformat(); |
179 |
|
|
} else |
180 |
|
|
lt->tm_sec = 0; |
181 |
|
|
|
182 |
|
|
switch (strlen(p)) { |
183 |
|
|
case 12: /* cc */ |
184 |
|
|
lt->tm_year = (ATOI2(p) * 100) - 1900; |
185 |
|
|
yearset = 1; |
186 |
|
|
/* FALLTHROUGH */ |
187 |
|
|
case 10: /* yy */ |
188 |
|
|
if (!yearset) { |
189 |
|
|
/* mask out current year, leaving only century */ |
190 |
|
|
lt->tm_year = ((lt->tm_year / 100) * 100); |
191 |
|
|
} |
192 |
|
|
lt->tm_year += ATOI2(p); |
193 |
|
|
/* FALLTHROUGH */ |
194 |
|
|
case 8: /* mm */ |
195 |
|
|
lt->tm_mon = ATOI2(p); |
196 |
|
|
if ((lt->tm_mon > 12) || !lt->tm_mon) |
197 |
|
|
badformat(); |
198 |
|
|
--lt->tm_mon; /* time struct is 0 - 11 */ |
199 |
|
|
/* FALLTHROUGH */ |
200 |
|
|
case 6: /* dd */ |
201 |
|
|
lt->tm_mday = ATOI2(p); |
202 |
|
|
if ((lt->tm_mday > 31) || !lt->tm_mday) |
203 |
|
|
badformat(); |
204 |
|
|
/* FALLTHROUGH */ |
205 |
|
|
case 4: /* HH */ |
206 |
|
|
lt->tm_hour = ATOI2(p); |
207 |
|
|
if (lt->tm_hour > 23) |
208 |
|
|
badformat(); |
209 |
|
|
/* FALLTHROUGH */ |
210 |
|
|
case 2: /* MM */ |
211 |
|
|
lt->tm_min = ATOI2(p); |
212 |
|
|
if (lt->tm_min > 59) |
213 |
|
|
badformat(); |
214 |
|
|
break; |
215 |
|
|
default: |
216 |
|
|
badformat(); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
/* convert broken-down time to UTC clock time */ |
220 |
|
|
if ((tval = mktime(lt)) < 0) |
221 |
|
|
errx(1, "specified date is outside allowed range"); |
222 |
|
|
|
223 |
|
|
if (jflag) |
224 |
|
|
return; |
225 |
|
|
|
226 |
|
|
/* set the time */ |
227 |
|
|
if (slidetime) { |
228 |
|
|
struct timeval tv_current; |
229 |
|
|
|
230 |
|
|
if (gettimeofday(&tv_current, NULL) == -1) |
231 |
|
|
err(1, "Could not get local time of day"); |
232 |
|
|
|
233 |
|
|
tv.tv_sec = tval - tv_current.tv_sec; |
234 |
|
|
tv.tv_usec = 0; |
235 |
|
|
if (adjtime(&tv, NULL) == -1) |
236 |
|
|
errx(1, "adjtime"); |
237 |
|
|
} else { |
238 |
|
|
#ifndef SMALL |
239 |
|
|
logwtmp("|", "date", ""); |
240 |
|
|
#endif |
241 |
|
|
tv.tv_sec = tval; |
242 |
|
|
tv.tv_usec = 0; |
243 |
|
|
if (settimeofday(&tv, NULL)) |
244 |
|
|
err(1, "settimeofday"); |
245 |
|
|
#ifndef SMALL |
246 |
|
|
logwtmp("{", "date", ""); |
247 |
|
|
#endif |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
if ((p = getlogin()) == NULL) |
251 |
|
|
p = "???"; |
252 |
|
|
syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
static void |
256 |
|
|
badformat(void) |
257 |
|
|
{ |
258 |
|
|
warnx("illegal time format"); |
259 |
|
|
usage(); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
static void __dead |
263 |
|
|
usage(void) |
264 |
|
|
{ |
265 |
|
|
(void)fprintf(stderr, |
266 |
|
|
"usage: %s [-aju] [-d dst] [-r seconds] [-t minutes_west] [-z output_zone]\n", |
267 |
|
|
__progname); |
268 |
|
|
(void)fprintf(stderr, |
269 |
|
|
"%-*s[+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]]\n", (int)strlen(__progname) + 8, ""); |
270 |
|
|
exit(1); |
271 |
|
|
} |