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