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