xref: /illumos-gate/usr/src/cmd/date/date.c (revision bde2df36)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
2663ff6dceSGarrett D'Amore /*
27*bde2df36SYuri Pankov  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
2863ff6dceSGarrett D'Amore  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
317c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
357c478bd9Sstevel@tonic-gate  * The Regents of the University of California
367c478bd9Sstevel@tonic-gate  * All Rights Reserved
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
397c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
407c478bd9Sstevel@tonic-gate  * contributors.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  *	date - with format capabilities and international flair
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include	<locale.h>
487c478bd9Sstevel@tonic-gate #include	<fcntl.h>
497c478bd9Sstevel@tonic-gate #include	<langinfo.h>
507c478bd9Sstevel@tonic-gate #include	<stdio.h>
517c478bd9Sstevel@tonic-gate #include	<stdlib.h>
527c478bd9Sstevel@tonic-gate #include	<string.h>
537c478bd9Sstevel@tonic-gate #include	<time.h>
547c478bd9Sstevel@tonic-gate #include	<unistd.h>
557c478bd9Sstevel@tonic-gate #include	<sys/time.h>
567c478bd9Sstevel@tonic-gate #include	<sys/types.h>
577c478bd9Sstevel@tonic-gate #include	<ctype.h>
587c478bd9Sstevel@tonic-gate #include	<utmpx.h>
597c478bd9Sstevel@tonic-gate #include	<tzfile.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	year_size(A)	((isleap(A)) ? 366 : 365)
627c478bd9Sstevel@tonic-gate static 	char	buf[BUFSIZ];
637c478bd9Sstevel@tonic-gate static	time_t	clock_val;
647c478bd9Sstevel@tonic-gate static  short	month_size[12] =
657c478bd9Sstevel@tonic-gate 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
667c478bd9Sstevel@tonic-gate static  struct  utmpx wtmpx[2] = {
677c478bd9Sstevel@tonic-gate 	{"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
687c478bd9Sstevel@tonic-gate 	{"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
697c478bd9Sstevel@tonic-gate 	};
707c478bd9Sstevel@tonic-gate static char *usage =
71*bde2df36SYuri Pankov 	"usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n\tdate [-Ru] [+format]\n"
727c478bd9Sstevel@tonic-gate 	"\tdate -a [-]sss[.fff]\n";
737c478bd9Sstevel@tonic-gate static int uflag = 0;
7463ff6dceSGarrett D'Amore static int Rflag = 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static int get_adj(char *, struct timeval *);
777c478bd9Sstevel@tonic-gate static int setdate(struct tm *, char *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
807c478bd9Sstevel@tonic-gate main(int argc, char **argv)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	struct tm *tp, tm;
837c478bd9Sstevel@tonic-gate 	struct timeval tv;
847c478bd9Sstevel@tonic-gate 	char *fmt;
857c478bd9Sstevel@tonic-gate 	int c, aflag = 0, illflag = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
907c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
937c478bd9Sstevel@tonic-gate 
9463ff6dceSGarrett D'Amore 	while ((c = getopt(argc, argv, "a:uR")) != EOF)
957c478bd9Sstevel@tonic-gate 		switch (c) {
967c478bd9Sstevel@tonic-gate 		case 'a':
977c478bd9Sstevel@tonic-gate 			aflag++;
987c478bd9Sstevel@tonic-gate 			if (get_adj(optarg, &tv) < 0) {
997c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1007c478bd9Sstevel@tonic-gate 				    gettext("date: invalid argument -- %s\n"),
10163ff6dceSGarrett D'Amore 				    optarg);
1027c478bd9Sstevel@tonic-gate 				illflag++;
1037c478bd9Sstevel@tonic-gate 			}
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 'u':
1067c478bd9Sstevel@tonic-gate 			uflag++;
1077c478bd9Sstevel@tonic-gate 			break;
10863ff6dceSGarrett D'Amore 		case 'R':
10963ff6dceSGarrett D'Amore 			Rflag++;
11063ff6dceSGarrett D'Amore 			break;
1117c478bd9Sstevel@tonic-gate 		default:
1127c478bd9Sstevel@tonic-gate 			illflag++;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	argc -= optind;
1167c478bd9Sstevel@tonic-gate 	argv  = &argv[optind];
1177c478bd9Sstevel@tonic-gate 
11863ff6dceSGarrett D'Amore 	/* -a is mutually exclusive with -u and -R */
1197c478bd9Sstevel@tonic-gate 	if (uflag && aflag)
1207c478bd9Sstevel@tonic-gate 		illflag++;
12163ff6dceSGarrett D'Amore 	if (Rflag && aflag)
12263ff6dceSGarrett D'Amore 		illflag++;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (illflag) {
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(usage));
1267c478bd9Sstevel@tonic-gate 		exit(1);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	(void) time(&clock_val);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (aflag) {
1327c478bd9Sstevel@tonic-gate 		if (adjtime(&tv, 0) < 0) {
1337c478bd9Sstevel@tonic-gate 			perror(gettext("date: Failed to adjust date"));
1347c478bd9Sstevel@tonic-gate 			exit(1);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		exit(0);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (argc > 0) {
1407c478bd9Sstevel@tonic-gate 		if (*argv[0] == '+')
1417c478bd9Sstevel@tonic-gate 			fmt = &argv[0][1];
1427c478bd9Sstevel@tonic-gate 		else {
1437c478bd9Sstevel@tonic-gate 			if (setdate(localtime(&clock_val), argv[0])) {
1447c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(usage));
1457c478bd9Sstevel@tonic-gate 				exit(1);
1467c478bd9Sstevel@tonic-gate 			}
1477c478bd9Sstevel@tonic-gate 			fmt = nl_langinfo(_DATE_FMT);
1487c478bd9Sstevel@tonic-gate 		}
14963ff6dceSGarrett D'Amore 	} else if (Rflag) {
15063ff6dceSGarrett D'Amore 		fmt = "%a, %d %h %Y %H:%M:%S %z";
1517c478bd9Sstevel@tonic-gate 	} else
1527c478bd9Sstevel@tonic-gate 		fmt = nl_langinfo(_DATE_FMT);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (uflag) {
1557c478bd9Sstevel@tonic-gate 		(void) putenv("TZ=GMT0");
1567c478bd9Sstevel@tonic-gate 		tzset();
1577c478bd9Sstevel@tonic-gate 		tp = gmtime(&clock_val);
1587c478bd9Sstevel@tonic-gate 	} else
1597c478bd9Sstevel@tonic-gate 		tp = localtime(&clock_val);
1607c478bd9Sstevel@tonic-gate 	(void) memcpy(&tm, tp, sizeof (struct tm));
1617c478bd9Sstevel@tonic-gate 	(void) strftime(buf, BUFSIZ, fmt, &tm);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	(void) puts(buf);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (0);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
1697c478bd9Sstevel@tonic-gate setdate(struct tm *current_date, char *date)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	int	i;
1727c478bd9Sstevel@tonic-gate 	int	mm;
1737c478bd9Sstevel@tonic-gate 	int	hh;
1747c478bd9Sstevel@tonic-gate 	int	min;
1757c478bd9Sstevel@tonic-gate 	int	sec = 0;
1767c478bd9Sstevel@tonic-gate 	char	*secptr;
1777c478bd9Sstevel@tonic-gate 	int	yy;
1787c478bd9Sstevel@tonic-gate 	int	dd	= 0;
1797c478bd9Sstevel@tonic-gate 	int	minidx	= 6;
1807c478bd9Sstevel@tonic-gate 	int	len;
1817c478bd9Sstevel@tonic-gate 	int	dd_check;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*  Parse date string  */
1847c478bd9Sstevel@tonic-gate 	if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 &&
1857c478bd9Sstevel@tonic-gate 	    isdigit(secptr[1]) && isdigit(secptr[2]) &&
1867c478bd9Sstevel@tonic-gate 	    (sec = atoi(&secptr[1])) >= 0 && sec < 60)
1877c478bd9Sstevel@tonic-gate 		secptr[0] = '\0';	/* eat decimal point only on success */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	len = strlen(date);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
1927c478bd9Sstevel@tonic-gate 		if (!isdigit(date[i])) {
1937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1947c478bd9Sstevel@tonic-gate 			gettext("date: bad conversion\n"));
1957c478bd9Sstevel@tonic-gate 			exit(1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	switch (strlen(date)) {
1997c478bd9Sstevel@tonic-gate 	case 12:
2007c478bd9Sstevel@tonic-gate 		yy = atoi(&date[8]);
2017c478bd9Sstevel@tonic-gate 		date[8] = '\0';
2027c478bd9Sstevel@tonic-gate 		break;
2037c478bd9Sstevel@tonic-gate 	case 10:
2047c478bd9Sstevel@tonic-gate 		/*
2057c478bd9Sstevel@tonic-gate 		 * The YY format has the following representation:
2067c478bd9Sstevel@tonic-gate 		 * 00-68 = 2000 thru 2068
2077c478bd9Sstevel@tonic-gate 		 * 69-99 = 1969 thru 1999
2087c478bd9Sstevel@tonic-gate 		 */
2097c478bd9Sstevel@tonic-gate 		if (atoi(&date[8]) <= 68) {
2107c478bd9Sstevel@tonic-gate 			yy = 1900 + (atoi(&date[8]) + 100);
2117c478bd9Sstevel@tonic-gate 		} else {
2127c478bd9Sstevel@tonic-gate 			yy = 1900 + atoi(&date[8]);
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		date[8] = '\0';
2157c478bd9Sstevel@tonic-gate 		break;
2167c478bd9Sstevel@tonic-gate 	case 8:
2177c478bd9Sstevel@tonic-gate 		yy = 1900 + current_date->tm_year;
2187c478bd9Sstevel@tonic-gate 		break;
2197c478bd9Sstevel@tonic-gate 	case 4:
2207c478bd9Sstevel@tonic-gate 		yy = 1900 + current_date->tm_year;
2217c478bd9Sstevel@tonic-gate 		mm = current_date->tm_mon + 1; 	/* tm_mon goes from 1 to 11 */
2227c478bd9Sstevel@tonic-gate 		dd = current_date->tm_mday;
2237c478bd9Sstevel@tonic-gate 		minidx = 2;
2247c478bd9Sstevel@tonic-gate 		break;
2257c478bd9Sstevel@tonic-gate 	default:
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("date: bad conversion\n"));
2277c478bd9Sstevel@tonic-gate 		return (1);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	min = atoi(&date[minidx]);
2317c478bd9Sstevel@tonic-gate 	date[minidx] = '\0';
2327c478bd9Sstevel@tonic-gate 	hh = atoi(&date[minidx-2]);
2337c478bd9Sstevel@tonic-gate 	date[minidx-2] = '\0';
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (!dd) {
2367c478bd9Sstevel@tonic-gate 		/*
2377c478bd9Sstevel@tonic-gate 		 * if dd is 0 (not between 1 and 31), then
2387c478bd9Sstevel@tonic-gate 		 * read the value supplied by the user.
2397c478bd9Sstevel@tonic-gate 		 */
2407c478bd9Sstevel@tonic-gate 		dd = atoi(&date[2]);
2417c478bd9Sstevel@tonic-gate 		date[2] = '\0';
2427c478bd9Sstevel@tonic-gate 		mm = atoi(&date[0]);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (hh == 24)
2467c478bd9Sstevel@tonic-gate 		hh = 0, dd++;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*  Validate date elements  */
2497c478bd9Sstevel@tonic-gate 	dd_check = 0;
2507c478bd9Sstevel@tonic-gate 	if (mm >= 1 && mm <= 12) {
2517c478bd9Sstevel@tonic-gate 		dd_check = month_size[mm - 1];	/* get days in this month */
2527c478bd9Sstevel@tonic-gate 		if (mm == 2 && isleap(yy))	/* adjust for leap year */
2537c478bd9Sstevel@tonic-gate 			dd_check++;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) &&
25663ff6dceSGarrett D'Amore 	    (hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) {
2577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("date: bad conversion\n"));
2587c478bd9Sstevel@tonic-gate 		return (1);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*  Build date and time number  */
2627c478bd9Sstevel@tonic-gate 	for (clock_val = 0, i = 1970; i < yy; i++)
2637c478bd9Sstevel@tonic-gate 		clock_val += year_size(i);
2647c478bd9Sstevel@tonic-gate 	/*  Adjust for leap year  */
2657c478bd9Sstevel@tonic-gate 	if (isleap(yy) && mm >= 3)
2667c478bd9Sstevel@tonic-gate 		clock_val += 1;
2677c478bd9Sstevel@tonic-gate 	/*  Adjust for different month lengths  */
2687c478bd9Sstevel@tonic-gate 	while (--mm)
2697c478bd9Sstevel@tonic-gate 		clock_val += (time_t)month_size[mm - 1];
2707c478bd9Sstevel@tonic-gate 	/*  Load up the rest  */
2717c478bd9Sstevel@tonic-gate 	clock_val += (time_t)(dd - 1);
2727c478bd9Sstevel@tonic-gate 	clock_val *= 24;
2737c478bd9Sstevel@tonic-gate 	clock_val += (time_t)hh;
2747c478bd9Sstevel@tonic-gate 	clock_val *= 60;
2757c478bd9Sstevel@tonic-gate 	clock_val += (time_t)min;
2767c478bd9Sstevel@tonic-gate 	clock_val *= 60;
2777c478bd9Sstevel@tonic-gate 	clock_val += sec;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (!uflag) {
2807c478bd9Sstevel@tonic-gate 		/* convert to GMT assuming standard time */
2817c478bd9Sstevel@tonic-gate 		/* correction is made in localtime(3C) */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		/*
2847c478bd9Sstevel@tonic-gate 		 * call localtime to set up "timezone" variable applicable
2857c478bd9Sstevel@tonic-gate 		 * for clock_val time, to support Olson timezones which
2867c478bd9Sstevel@tonic-gate 		 * can allow timezone rules to change.
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		(void) localtime(&clock_val);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		clock_val += (time_t)timezone;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		/* correct if daylight savings time in effect */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		if (localtime(&clock_val)->tm_isdst)
2957c478bd9Sstevel@tonic-gate 			clock_val = clock_val - (time_t)(timezone - altzone);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	(void) time(&wtmpx[0].ut_xtime);
2997c478bd9Sstevel@tonic-gate 	if (stime(&clock_val) < 0) {
3007c478bd9Sstevel@tonic-gate 		perror("date");
3017c478bd9Sstevel@tonic-gate 		return (1);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate #if defined(i386)
3047c478bd9Sstevel@tonic-gate 	/* correct the kernel's "gmt_lag" and the PC's RTC */
3057c478bd9Sstevel@tonic-gate 	(void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
3067c478bd9Sstevel@tonic-gate #endif
3077c478bd9Sstevel@tonic-gate 	(void) time(&wtmpx[1].ut_xtime);
3087c478bd9Sstevel@tonic-gate 	(void) pututxline(&wtmpx[0]);
3097c478bd9Sstevel@tonic-gate 	(void) pututxline(&wtmpx[1]);
3107c478bd9Sstevel@tonic-gate 	(void) updwtmpx(WTMPX_FILE, &wtmpx[0]);
3117c478bd9Sstevel@tonic-gate 	(void) updwtmpx(WTMPX_FILE, &wtmpx[1]);
3127c478bd9Sstevel@tonic-gate 	return (0);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate int
3167c478bd9Sstevel@tonic-gate get_adj(char *cp, struct timeval *tp)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	register int mult;
3197c478bd9Sstevel@tonic-gate 	int sign;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/* arg must be [-]sss[.fff] */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	tp->tv_sec = tp->tv_usec = 0;
3247c478bd9Sstevel@tonic-gate 	if (*cp == '-') {
3257c478bd9Sstevel@tonic-gate 		sign = -1;
3267c478bd9Sstevel@tonic-gate 		cp++;
3277c478bd9Sstevel@tonic-gate 	} else {
3287c478bd9Sstevel@tonic-gate 		sign = 1;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '9') {
3327c478bd9Sstevel@tonic-gate 		tp->tv_sec *= 10;
3337c478bd9Sstevel@tonic-gate 		tp->tv_sec += *cp++ - '0';
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	if (*cp == '.') {
3367c478bd9Sstevel@tonic-gate 		cp++;
3377c478bd9Sstevel@tonic-gate 		mult = 100000;
3387c478bd9Sstevel@tonic-gate 		while (*cp >= '0' && *cp <= '9') {
3397c478bd9Sstevel@tonic-gate 			tp->tv_usec += (*cp++ - '0') * mult;
3407c478bd9Sstevel@tonic-gate 			mult /= 10;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	/*
3447c478bd9Sstevel@tonic-gate 	 * if there's anything left in the string,
3457c478bd9Sstevel@tonic-gate 	 * the input was invalid.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	if (*cp) {
3487c478bd9Sstevel@tonic-gate 		return (-1);
3497c478bd9Sstevel@tonic-gate 	} else {
3507c478bd9Sstevel@tonic-gate 		tp->tv_sec *= sign;
3517c478bd9Sstevel@tonic-gate 		tp->tv_usec *= sign;
3527c478bd9Sstevel@tonic-gate 		return (0);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate }
355