1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate * The Regents of the University of California
33*0Sstevel@tonic-gate * All Rights Reserved
34*0Sstevel@tonic-gate *
35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate * contributors.
38*0Sstevel@tonic-gate */
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate * date - with format capabilities and international flair
44*0Sstevel@tonic-gate */
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <fcntl.h>
48*0Sstevel@tonic-gate #include <langinfo.h>
49*0Sstevel@tonic-gate #include <stdio.h>
50*0Sstevel@tonic-gate #include <stdlib.h>
51*0Sstevel@tonic-gate #include <string.h>
52*0Sstevel@tonic-gate #include <time.h>
53*0Sstevel@tonic-gate #include <unistd.h>
54*0Sstevel@tonic-gate #include <sys/time.h>
55*0Sstevel@tonic-gate #include <sys/types.h>
56*0Sstevel@tonic-gate #include <ctype.h>
57*0Sstevel@tonic-gate #include <utmpx.h>
58*0Sstevel@tonic-gate #include <tzfile.h>
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate #define year_size(A) ((isleap(A)) ? 366 : 365)
61*0Sstevel@tonic-gate static char buf[BUFSIZ];
62*0Sstevel@tonic-gate static time_t clock_val;
63*0Sstevel@tonic-gate static short month_size[12] =
64*0Sstevel@tonic-gate { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
65*0Sstevel@tonic-gate static struct utmpx wtmpx[2] = {
66*0Sstevel@tonic-gate {"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
67*0Sstevel@tonic-gate {"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate static char *usage =
70*0Sstevel@tonic-gate "usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n\tdate [-u] [+format]\n"
71*0Sstevel@tonic-gate "\tdate -a [-]sss[.fff]\n";
72*0Sstevel@tonic-gate static int uflag = 0;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate static int get_adj(char *, struct timeval *);
75*0Sstevel@tonic-gate static int setdate(struct tm *, char *);
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate int
main(int argc,char ** argv)78*0Sstevel@tonic-gate main(int argc, char **argv)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate struct tm *tp, tm;
81*0Sstevel@tonic-gate struct timeval tv;
82*0Sstevel@tonic-gate char *fmt;
83*0Sstevel@tonic-gate int c, aflag = 0, illflag = 0;
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
88*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
89*0Sstevel@tonic-gate #endif
90*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "a:u")) != EOF)
93*0Sstevel@tonic-gate switch (c) {
94*0Sstevel@tonic-gate case 'a':
95*0Sstevel@tonic-gate aflag++;
96*0Sstevel@tonic-gate if (get_adj(optarg, &tv) < 0) {
97*0Sstevel@tonic-gate (void) fprintf(stderr,
98*0Sstevel@tonic-gate gettext("date: invalid argument -- %s\n"),
99*0Sstevel@tonic-gate optarg);
100*0Sstevel@tonic-gate illflag++;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate break;
103*0Sstevel@tonic-gate case 'u':
104*0Sstevel@tonic-gate uflag++;
105*0Sstevel@tonic-gate break;
106*0Sstevel@tonic-gate default:
107*0Sstevel@tonic-gate illflag++;
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate argc -= optind;
111*0Sstevel@tonic-gate argv = &argv[optind];
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate /* -u and -a are mutually exclusive */
114*0Sstevel@tonic-gate if (uflag && aflag)
115*0Sstevel@tonic-gate illflag++;
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate if (illflag) {
118*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(usage));
119*0Sstevel@tonic-gate exit(1);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate (void) time(&clock_val);
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate if (aflag) {
125*0Sstevel@tonic-gate if (adjtime(&tv, 0) < 0) {
126*0Sstevel@tonic-gate perror(gettext("date: Failed to adjust date"));
127*0Sstevel@tonic-gate exit(1);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate exit(0);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate if (argc > 0) {
133*0Sstevel@tonic-gate if (*argv[0] == '+')
134*0Sstevel@tonic-gate fmt = &argv[0][1];
135*0Sstevel@tonic-gate else {
136*0Sstevel@tonic-gate if (setdate(localtime(&clock_val), argv[0])) {
137*0Sstevel@tonic-gate (void) fprintf(stderr, gettext(usage));
138*0Sstevel@tonic-gate exit(1);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate fmt = nl_langinfo(_DATE_FMT);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate } else
143*0Sstevel@tonic-gate fmt = nl_langinfo(_DATE_FMT);
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate if (uflag) {
146*0Sstevel@tonic-gate (void) putenv("TZ=GMT0");
147*0Sstevel@tonic-gate tzset();
148*0Sstevel@tonic-gate tp = gmtime(&clock_val);
149*0Sstevel@tonic-gate } else
150*0Sstevel@tonic-gate tp = localtime(&clock_val);
151*0Sstevel@tonic-gate (void) memcpy(&tm, tp, sizeof (struct tm));
152*0Sstevel@tonic-gate (void) strftime(buf, BUFSIZ, fmt, &tm);
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate (void) puts(buf);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate return (0);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate int
setdate(struct tm * current_date,char * date)160*0Sstevel@tonic-gate setdate(struct tm *current_date, char *date)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate int i;
163*0Sstevel@tonic-gate int mm;
164*0Sstevel@tonic-gate int hh;
165*0Sstevel@tonic-gate int min;
166*0Sstevel@tonic-gate int sec = 0;
167*0Sstevel@tonic-gate char *secptr;
168*0Sstevel@tonic-gate int yy;
169*0Sstevel@tonic-gate int dd = 0;
170*0Sstevel@tonic-gate int minidx = 6;
171*0Sstevel@tonic-gate int len;
172*0Sstevel@tonic-gate int dd_check;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate /* Parse date string */
175*0Sstevel@tonic-gate if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 &&
176*0Sstevel@tonic-gate isdigit(secptr[1]) && isdigit(secptr[2]) &&
177*0Sstevel@tonic-gate (sec = atoi(&secptr[1])) >= 0 && sec < 60)
178*0Sstevel@tonic-gate secptr[0] = '\0'; /* eat decimal point only on success */
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate len = strlen(date);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate for (i = 0; i < len; i++) {
183*0Sstevel@tonic-gate if (!isdigit(date[i])) {
184*0Sstevel@tonic-gate (void) fprintf(stderr,
185*0Sstevel@tonic-gate gettext("date: bad conversion\n"));
186*0Sstevel@tonic-gate exit(1);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate switch (strlen(date)) {
190*0Sstevel@tonic-gate case 12:
191*0Sstevel@tonic-gate yy = atoi(&date[8]);
192*0Sstevel@tonic-gate date[8] = '\0';
193*0Sstevel@tonic-gate break;
194*0Sstevel@tonic-gate case 10:
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate * The YY format has the following representation:
197*0Sstevel@tonic-gate * 00-68 = 2000 thru 2068
198*0Sstevel@tonic-gate * 69-99 = 1969 thru 1999
199*0Sstevel@tonic-gate */
200*0Sstevel@tonic-gate if (atoi(&date[8]) <= 68) {
201*0Sstevel@tonic-gate yy = 1900 + (atoi(&date[8]) + 100);
202*0Sstevel@tonic-gate } else {
203*0Sstevel@tonic-gate yy = 1900 + atoi(&date[8]);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate date[8] = '\0';
206*0Sstevel@tonic-gate break;
207*0Sstevel@tonic-gate case 8:
208*0Sstevel@tonic-gate yy = 1900 + current_date->tm_year;
209*0Sstevel@tonic-gate break;
210*0Sstevel@tonic-gate case 4:
211*0Sstevel@tonic-gate yy = 1900 + current_date->tm_year;
212*0Sstevel@tonic-gate mm = current_date->tm_mon + 1; /* tm_mon goes from 1 to 11 */
213*0Sstevel@tonic-gate dd = current_date->tm_mday;
214*0Sstevel@tonic-gate minidx = 2;
215*0Sstevel@tonic-gate break;
216*0Sstevel@tonic-gate default:
217*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("date: bad conversion\n"));
218*0Sstevel@tonic-gate return (1);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate min = atoi(&date[minidx]);
222*0Sstevel@tonic-gate date[minidx] = '\0';
223*0Sstevel@tonic-gate hh = atoi(&date[minidx-2]);
224*0Sstevel@tonic-gate date[minidx-2] = '\0';
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate if (!dd) {
227*0Sstevel@tonic-gate /*
228*0Sstevel@tonic-gate * if dd is 0 (not between 1 and 31), then
229*0Sstevel@tonic-gate * read the value supplied by the user.
230*0Sstevel@tonic-gate */
231*0Sstevel@tonic-gate dd = atoi(&date[2]);
232*0Sstevel@tonic-gate date[2] = '\0';
233*0Sstevel@tonic-gate mm = atoi(&date[0]);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate if (hh == 24)
237*0Sstevel@tonic-gate hh = 0, dd++;
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /* Validate date elements */
240*0Sstevel@tonic-gate dd_check = 0;
241*0Sstevel@tonic-gate if (mm >= 1 && mm <= 12) {
242*0Sstevel@tonic-gate dd_check = month_size[mm - 1]; /* get days in this month */
243*0Sstevel@tonic-gate if (mm == 2 && isleap(yy)) /* adjust for leap year */
244*0Sstevel@tonic-gate dd_check++;
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) &&
247*0Sstevel@tonic-gate (hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) {
248*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("date: bad conversion\n"));
249*0Sstevel@tonic-gate return (1);
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /* Build date and time number */
253*0Sstevel@tonic-gate for (clock_val = 0, i = 1970; i < yy; i++)
254*0Sstevel@tonic-gate clock_val += year_size(i);
255*0Sstevel@tonic-gate /* Adjust for leap year */
256*0Sstevel@tonic-gate if (isleap(yy) && mm >= 3)
257*0Sstevel@tonic-gate clock_val += 1;
258*0Sstevel@tonic-gate /* Adjust for different month lengths */
259*0Sstevel@tonic-gate while (--mm)
260*0Sstevel@tonic-gate clock_val += (time_t)month_size[mm - 1];
261*0Sstevel@tonic-gate /* Load up the rest */
262*0Sstevel@tonic-gate clock_val += (time_t)(dd - 1);
263*0Sstevel@tonic-gate clock_val *= 24;
264*0Sstevel@tonic-gate clock_val += (time_t)hh;
265*0Sstevel@tonic-gate clock_val *= 60;
266*0Sstevel@tonic-gate clock_val += (time_t)min;
267*0Sstevel@tonic-gate clock_val *= 60;
268*0Sstevel@tonic-gate clock_val += sec;
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate if (!uflag) {
271*0Sstevel@tonic-gate /* convert to GMT assuming standard time */
272*0Sstevel@tonic-gate /* correction is made in localtime(3C) */
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate /*
275*0Sstevel@tonic-gate * call localtime to set up "timezone" variable applicable
276*0Sstevel@tonic-gate * for clock_val time, to support Olson timezones which
277*0Sstevel@tonic-gate * can allow timezone rules to change.
278*0Sstevel@tonic-gate */
279*0Sstevel@tonic-gate (void) localtime(&clock_val);
280*0Sstevel@tonic-gate
281*0Sstevel@tonic-gate clock_val += (time_t)timezone;
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate /* correct if daylight savings time in effect */
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate if (localtime(&clock_val)->tm_isdst)
286*0Sstevel@tonic-gate clock_val = clock_val - (time_t)(timezone - altzone);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate (void) time(&wtmpx[0].ut_xtime);
290*0Sstevel@tonic-gate if (stime(&clock_val) < 0) {
291*0Sstevel@tonic-gate perror("date");
292*0Sstevel@tonic-gate return (1);
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate #if defined(i386)
295*0Sstevel@tonic-gate /* correct the kernel's "gmt_lag" and the PC's RTC */
296*0Sstevel@tonic-gate (void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
297*0Sstevel@tonic-gate #endif
298*0Sstevel@tonic-gate (void) time(&wtmpx[1].ut_xtime);
299*0Sstevel@tonic-gate (void) pututxline(&wtmpx[0]);
300*0Sstevel@tonic-gate (void) pututxline(&wtmpx[1]);
301*0Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx[0]);
302*0Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx[1]);
303*0Sstevel@tonic-gate return (0);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate int
get_adj(char * cp,struct timeval * tp)307*0Sstevel@tonic-gate get_adj(char *cp, struct timeval *tp)
308*0Sstevel@tonic-gate {
309*0Sstevel@tonic-gate register int mult;
310*0Sstevel@tonic-gate int sign;
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate /* arg must be [-]sss[.fff] */
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate tp->tv_sec = tp->tv_usec = 0;
315*0Sstevel@tonic-gate if (*cp == '-') {
316*0Sstevel@tonic-gate sign = -1;
317*0Sstevel@tonic-gate cp++;
318*0Sstevel@tonic-gate } else {
319*0Sstevel@tonic-gate sign = 1;
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9') {
323*0Sstevel@tonic-gate tp->tv_sec *= 10;
324*0Sstevel@tonic-gate tp->tv_sec += *cp++ - '0';
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate if (*cp == '.') {
327*0Sstevel@tonic-gate cp++;
328*0Sstevel@tonic-gate mult = 100000;
329*0Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9') {
330*0Sstevel@tonic-gate tp->tv_usec += (*cp++ - '0') * mult;
331*0Sstevel@tonic-gate mult /= 10;
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate /*
335*0Sstevel@tonic-gate * if there's anything left in the string,
336*0Sstevel@tonic-gate * the input was invalid.
337*0Sstevel@tonic-gate */
338*0Sstevel@tonic-gate if (*cp) {
339*0Sstevel@tonic-gate return (-1);
340*0Sstevel@tonic-gate } else {
341*0Sstevel@tonic-gate tp->tv_sec *= sign;
342*0Sstevel@tonic-gate tp->tv_usec *= sign;
343*0Sstevel@tonic-gate return (0);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate }
346