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