date.c revision 1.10 1 1.10 cgd /* $NetBSD: date.c,v 1.10 1995/04/23 10:07:25 cgd Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.8 mycroft * Copyright (c) 1985, 1987, 1988, 1993
5 1.8 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.8 mycroft static char copyright[] =
38 1.8 mycroft "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
39 1.8 mycroft The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.10 cgd #if 0
44 1.9 cgd static char sccsid[] = "@(#)date.c 8.1 (Berkeley) 5/31/93";
45 1.10 cgd #else
46 1.10 cgd static char rcsid[] = "$NetBSD: date.c,v 1.10 1995/04/23 10:07:25 cgd Exp $";
47 1.10 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/time.h>
52 1.8 mycroft
53 1.8 mycroft #include <ctype.h>
54 1.8 mycroft #include <err.h>
55 1.8 mycroft #include <fcntl.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <stdlib.h>
58 1.1 cgd #include <string.h>
59 1.7 jtc #include <locale.h>
60 1.8 mycroft #include <syslog.h>
61 1.8 mycroft #include <unistd.h>
62 1.8 mycroft
63 1.8 mycroft #include "extern.h"
64 1.1 cgd
65 1.1 cgd time_t tval;
66 1.1 cgd int retval, nflag;
67 1.1 cgd
68 1.8 mycroft static void setthetime __P((char *));
69 1.8 mycroft static void badformat __P((void));
70 1.8 mycroft static void usage __P((void));
71 1.8 mycroft
72 1.8 mycroft int logwtmp __P((char *, char *, char *));
73 1.8 mycroft
74 1.8 mycroft int
75 1.1 cgd main(argc, argv)
76 1.1 cgd int argc;
77 1.1 cgd char **argv;
78 1.1 cgd {
79 1.1 cgd extern int optind;
80 1.1 cgd extern char *optarg;
81 1.1 cgd struct timezone tz;
82 1.1 cgd int ch, rflag;
83 1.1 cgd char *format, buf[1024];
84 1.1 cgd
85 1.7 jtc setlocale(LC_ALL, "");
86 1.7 jtc
87 1.1 cgd tz.tz_dsttime = tz.tz_minuteswest = 0;
88 1.1 cgd rflag = 0;
89 1.7 jtc while ((ch = getopt(argc, argv, "d:nr:ut:")) != -1)
90 1.1 cgd switch((char)ch) {
91 1.1 cgd case 'd': /* daylight savings time */
92 1.1 cgd tz.tz_dsttime = atoi(optarg) ? 1 : 0;
93 1.1 cgd break;
94 1.1 cgd case 'n': /* don't set network */
95 1.1 cgd nflag = 1;
96 1.1 cgd break;
97 1.1 cgd case 'r': /* user specified seconds */
98 1.1 cgd rflag = 1;
99 1.1 cgd tval = atol(optarg);
100 1.1 cgd break;
101 1.1 cgd case 'u': /* do everything in GMT */
102 1.1 cgd (void)setenv("TZ", "GMT0", 1);
103 1.1 cgd break;
104 1.1 cgd case 't': /* minutes west of GMT */
105 1.1 cgd /* error check; don't allow "PST" */
106 1.1 cgd if (isdigit(*optarg)) {
107 1.1 cgd tz.tz_minuteswest = atoi(optarg);
108 1.1 cgd break;
109 1.1 cgd }
110 1.1 cgd /* FALLTHROUGH */
111 1.1 cgd default:
112 1.1 cgd usage();
113 1.1 cgd }
114 1.1 cgd argc -= optind;
115 1.1 cgd argv += optind;
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * If -d or -t, set the timezone or daylight savings time; this
119 1.1 cgd * doesn't belong here, there kernel should not know about either.
120 1.1 cgd */
121 1.1 cgd if ((tz.tz_minuteswest || tz.tz_dsttime) &&
122 1.8 mycroft settimeofday(NULL, &tz))
123 1.8 mycroft err(1, "settimeofday");
124 1.1 cgd
125 1.8 mycroft if (!rflag && time(&tval) == -1)
126 1.8 mycroft err(1, "time");
127 1.1 cgd
128 1.6 jtc format = "%a %b %e %H:%M:%S %Z %Y";
129 1.1 cgd
130 1.1 cgd /* allow the operands in any order */
131 1.1 cgd if (*argv && **argv == '+') {
132 1.1 cgd format = *argv + 1;
133 1.1 cgd ++argv;
134 1.1 cgd }
135 1.1 cgd
136 1.1 cgd if (*argv) {
137 1.1 cgd setthetime(*argv);
138 1.1 cgd ++argv;
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd if (*argv && **argv == '+')
142 1.1 cgd format = *argv + 1;
143 1.1 cgd
144 1.1 cgd (void)strftime(buf, sizeof(buf), format, localtime(&tval));
145 1.6 jtc (void)printf("%s\n", buf);
146 1.1 cgd exit(retval);
147 1.1 cgd }
148 1.1 cgd
149 1.1 cgd #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
150 1.8 mycroft void
151 1.1 cgd setthetime(p)
152 1.1 cgd register char *p;
153 1.1 cgd {
154 1.1 cgd register struct tm *lt;
155 1.1 cgd struct timeval tv;
156 1.8 mycroft char *dot, *t;
157 1.1 cgd
158 1.8 mycroft for (t = p, dot = NULL; *t; ++t) {
159 1.8 mycroft if (isdigit(*t))
160 1.8 mycroft continue;
161 1.8 mycroft if (*t == '.' && dot == NULL) {
162 1.8 mycroft dot = t;
163 1.8 mycroft continue;
164 1.8 mycroft }
165 1.8 mycroft badformat();
166 1.8 mycroft }
167 1.1 cgd
168 1.1 cgd lt = localtime(&tval);
169 1.1 cgd
170 1.8 mycroft if (dot != NULL) { /* .ss */
171 1.8 mycroft *dot++ = '\0';
172 1.8 mycroft if (strlen(dot) != 2)
173 1.8 mycroft badformat();
174 1.8 mycroft lt->tm_sec = ATOI2(dot);
175 1.1 cgd if (lt->tm_sec > 61)
176 1.1 cgd badformat();
177 1.1 cgd } else
178 1.1 cgd lt->tm_sec = 0;
179 1.1 cgd
180 1.1 cgd switch (strlen(p)) {
181 1.1 cgd case 10: /* yy */
182 1.1 cgd lt->tm_year = ATOI2(p);
183 1.1 cgd if (lt->tm_year < 69) /* hack for 2000 ;-} */
184 1.1 cgd lt->tm_year += 100;
185 1.1 cgd /* FALLTHROUGH */
186 1.1 cgd case 8: /* mm */
187 1.1 cgd lt->tm_mon = ATOI2(p);
188 1.1 cgd if (lt->tm_mon > 12)
189 1.1 cgd badformat();
190 1.1 cgd --lt->tm_mon; /* time struct is 0 - 11 */
191 1.1 cgd /* FALLTHROUGH */
192 1.1 cgd case 6: /* dd */
193 1.1 cgd lt->tm_mday = ATOI2(p);
194 1.1 cgd if (lt->tm_mday > 31)
195 1.1 cgd badformat();
196 1.1 cgd /* FALLTHROUGH */
197 1.1 cgd case 4: /* hh */
198 1.1 cgd lt->tm_hour = ATOI2(p);
199 1.1 cgd if (lt->tm_hour > 23)
200 1.1 cgd badformat();
201 1.1 cgd /* FALLTHROUGH */
202 1.1 cgd case 2: /* mm */
203 1.1 cgd lt->tm_min = ATOI2(p);
204 1.1 cgd if (lt->tm_min > 59)
205 1.1 cgd badformat();
206 1.1 cgd break;
207 1.1 cgd default:
208 1.1 cgd badformat();
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /* convert broken-down time to GMT clock time */
212 1.1 cgd if ((tval = mktime(lt)) == -1)
213 1.1 cgd badformat();
214 1.1 cgd
215 1.1 cgd /* set the time */
216 1.1 cgd if (nflag || netsettime(tval)) {
217 1.1 cgd logwtmp("|", "date", "");
218 1.1 cgd tv.tv_sec = tval;
219 1.1 cgd tv.tv_usec = 0;
220 1.8 mycroft if (settimeofday(&tv, NULL)) {
221 1.1 cgd perror("date: settimeofday");
222 1.1 cgd exit(1);
223 1.1 cgd }
224 1.1 cgd logwtmp("{", "date", "");
225 1.1 cgd }
226 1.8 mycroft
227 1.8 mycroft if ((p = getlogin()) == NULL)
228 1.8 mycroft p = "???";
229 1.8 mycroft syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
230 1.1 cgd }
231 1.1 cgd
232 1.8 mycroft static void
233 1.1 cgd badformat()
234 1.1 cgd {
235 1.8 mycroft warnx("illegal time format");
236 1.1 cgd usage();
237 1.1 cgd }
238 1.1 cgd
239 1.8 mycroft static void
240 1.1 cgd usage()
241 1.1 cgd {
242 1.1 cgd (void)fprintf(stderr,
243 1.1 cgd "usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]\n");
244 1.1 cgd (void)fprintf(stderr, " [yy[mm[dd[hh]]]]mm[.ss]]\n");
245 1.1 cgd exit(1);
246 1.1 cgd }
247