17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
2663ff6dcGarrett D'Amore/*
27bde2df3Yuri Pankov * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
2863ff6dcGarrett D'Amore */
2972fb660Keith M Wesolowski/*
302f4149eRobert Mustacchi * Copyright (c) 2017, Joyent, Inc.
3172fb660Keith M Wesolowski */
327c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
337c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate/*
367c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
377c478bdstevel@tonic-gate * The Regents of the University of California
387c478bdstevel@tonic-gate * All Rights Reserved
397c478bdstevel@tonic-gate *
407c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
417c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
427c478bdstevel@tonic-gate * contributors.
437c478bdstevel@tonic-gate */
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate/*
467c478bdstevel@tonic-gate *	date - with format capabilities and international flair
477c478bdstevel@tonic-gate */
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#include	<locale.h>
507c478bdstevel@tonic-gate#include	<fcntl.h>
517c478bdstevel@tonic-gate#include	<langinfo.h>
527c478bdstevel@tonic-gate#include	<stdio.h>
537c478bdstevel@tonic-gate#include	<stdlib.h>
547c478bdstevel@tonic-gate#include	<string.h>
557c478bdstevel@tonic-gate#include	<time.h>
567c478bdstevel@tonic-gate#include	<unistd.h>
577c478bdstevel@tonic-gate#include	<sys/time.h>
587c478bdstevel@tonic-gate#include	<sys/types.h>
592f4149eRobert Mustacchi#include	<sys/stat.h>
607c478bdstevel@tonic-gate#include	<ctype.h>
612f4149eRobert Mustacchi#include	<errno.h>
627c478bdstevel@tonic-gate#include	<utmpx.h>
637c478bdstevel@tonic-gate#include	<tzfile.h>
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate#define	year_size(A)	((isleap(A)) ? 366 : 365)
667c478bdstevel@tonic-gatestatic 	char	buf[BUFSIZ];
677c478bdstevel@tonic-gatestatic	time_t	clock_val;
687c478bdstevel@tonic-gatestatic  short	month_size[12] =
697c478bdstevel@tonic-gate	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
707c478bdstevel@tonic-gatestatic  struct  utmpx wtmpx[2] = {
717c478bdstevel@tonic-gate	{"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
727c478bdstevel@tonic-gate	{"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
737c478bdstevel@tonic-gate	};
747c478bdstevel@tonic-gatestatic char *usage =
752f4149eRobert Mustacchi	"usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n"
762f4149eRobert Mustacchi	"\tdate [-Ru] [-r seconds | filename] [+format]\n"
777c478bdstevel@tonic-gate	"\tdate -a [-]sss[.fff]\n";
787c478bdstevel@tonic-gatestatic int uflag = 0;
7963ff6dcGarrett D'Amorestatic int Rflag = 0;
802f4149eRobert Mustacchistatic int rflag = 0;
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gatestatic int get_adj(char *, struct timeval *);
837c478bdstevel@tonic-gatestatic int setdate(struct tm *, char *);
8472fb660Keith M Wesolowskistatic void fmt_extensions(char *, size_t,
8572fb660Keith M Wesolowski    const char *, const struct timespec *);
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gateint
887c478bdstevel@tonic-gatemain(int argc, char **argv)
897c478bdstevel@tonic-gate{
907c478bdstevel@tonic-gate	struct tm *tp, tm;
917c478bdstevel@tonic-gate	struct timeval tv;
922f4149eRobert Mustacchi	char *fmt, *eptr;
9372fb660Keith M Wesolowski	char fmtbuf[BUFSIZ];
947c478bdstevel@tonic-gate	int c, aflag = 0, illflag = 0;
9572fb660Keith M Wesolowski	struct timespec ts;
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate	(void) setlocale(LC_ALL, "");
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate#if !defined(TEXT_DOMAIN)
1007c478bdstevel@tonic-gate#define	TEXT_DOMAIN "SYS_TEST"
1017c478bdstevel@tonic-gate#endif
1027c478bdstevel@tonic-gate	(void) textdomain(TEXT_DOMAIN);
1037c478bdstevel@tonic-gate
1042f4149eRobert Mustacchi	while ((c = getopt(argc, argv, "a:uRr:")) != EOF)
1057c478bdstevel@tonic-gate		switch (c) {
1067c478bdstevel@tonic-gate		case 'a':
1077c478bdstevel@tonic-gate			aflag++;
1087c478bdstevel@tonic-gate			if (get_adj(optarg, &tv) < 0) {
1097c478bdstevel@tonic-gate				(void) fprintf(stderr,
1107c478bdstevel@tonic-gate				    gettext("date: invalid argument -- %s\n"),
11163ff6dcGarrett D'Amore				    optarg);
1127c478bdstevel@tonic-gate				illflag++;
1137c478bdstevel@tonic-gate			}
1147c478bdstevel@tonic-gate			break;
1157c478bdstevel@tonic-gate		case 'u':
1167c478bdstevel@tonic-gate			uflag++;
1177c478bdstevel@tonic-gate			break;
11863ff6dcGarrett D'Amore		case 'R':
11963ff6dcGarrett D'Amore			Rflag++;
12063ff6dcGarrett D'Amore			break;
1212f4149eRobert Mustacchi		case 'r':
1222f4149eRobert Mustacchi
1232f4149eRobert Mustacchi			/*
1242f4149eRobert Mustacchi			 * BSD originally used -r to specify a unix time. GNU
1252f4149eRobert Mustacchi			 * used -r to specify a reference to a file. Now, like
1262f4149eRobert Mustacchi			 * some BSDs we attempt to parse the time. If we can,
1272f4149eRobert Mustacchi			 * then we use that, otherwise we fall back and treat it
1282f4149eRobert Mustacchi			 * like GNU.
1292f4149eRobert Mustacchi			 */
1302f4149eRobert Mustacchi			rflag++;
1312f4149eRobert Mustacchi			errno = 0;
1322f4149eRobert Mustacchi			ts.tv_sec = strtol(optarg, &eptr, 0);
1332f4149eRobert Mustacchi			if (errno == EINVAL || *eptr != '\0') {
1342f4149eRobert Mustacchi				struct stat st;
1352f4149eRobert Mustacchi				if (stat(optarg, &st) == 0) {
1362f4149eRobert Mustacchi					ts.tv_sec = st.st_mtime;
1372f4149eRobert Mustacchi				} else {
1382f4149eRobert Mustacchi					(void) fprintf(stderr,
1392f4149eRobert Mustacchi					    gettext("date: failed to get stat "
1402f4149eRobert Mustacchi					    "information about %s: %s\n"),
1412f4149eRobert Mustacchi					    optarg, strerror(errno));
1422f4149eRobert Mustacchi					exit(1);
1432f4149eRobert Mustacchi				}
1442f4149eRobert Mustacchi			} else if (errno != 0) {
1452f4149eRobert Mustacchi				(void) fprintf(stderr,
1462f4149eRobert Mustacchi				    gettext("date: failed to parse -r "
1472f4149eRobert Mustacchi				    "argument: %s\n"), optarg);
1482f4149eRobert Mustacchi				exit(1);
1492f4149eRobert Mustacchi			}
1502f4149eRobert Mustacchi			break;
1517c478bdstevel@tonic-gate		default:
1527c478bdstevel@tonic-gate			illflag++;
1537c478bdstevel@tonic-gate		}
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate	argc -= optind;
1567c478bdstevel@tonic-gate	argv  = &argv[optind];
1577c478bdstevel@tonic-gate
1582f4149eRobert Mustacchi	/* -a is mutually exclusive with -u, -R, and -r */
1597c478bdstevel@tonic-gate	if (uflag && aflag)
1607c478bdstevel@tonic-gate		illflag++;
16163ff6dcGarrett D'Amore	if (Rflag && aflag)
16263ff6dcGarrett D'Amore		illflag++;
1632f4149eRobert Mustacchi	if (rflag && aflag)
1642f4149eRobert Mustacchi		illflag++;
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	if (illflag) {
1677c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(usage));
1687c478bdstevel@tonic-gate		exit(1);
1697c478bdstevel@tonic-gate	}
1707c478bdstevel@tonic-gate
1712f4149eRobert Mustacchi	if (rflag == 0) {
1722f4149eRobert Mustacchi		if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
1732f4149eRobert Mustacchi			perror(gettext("date: Failed to obtain system time"));
1742f4149eRobert Mustacchi			exit(1);
1752f4149eRobert Mustacchi		}
17672fb660Keith M Wesolowski	}
17772fb660Keith M Wesolowski	clock_val = ts.tv_sec;
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate	if (aflag) {
1807c478bdstevel@tonic-gate		if (adjtime(&tv, 0) < 0) {
1817c478bdstevel@tonic-gate			perror(gettext("date: Failed to adjust date"));
1827c478bdstevel@tonic-gate			exit(1);
1837c478bdstevel@tonic-gate		}
1847c478bdstevel@tonic-gate		exit(0);
1857c478bdstevel@tonic-gate	}
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gate	if (argc > 0) {
1887c478bdstevel@tonic-gate		if (*argv[0] == '+')
1897c478bdstevel@tonic-gate			fmt = &argv[0][1];
1907c478bdstevel@tonic-gate		else {
1917c478bdstevel@tonic-gate			if (setdate(localtime(&clock_val), argv[0])) {
1927c478bdstevel@tonic-gate				(void) fprintf(stderr, gettext(usage));
1937c478bdstevel@tonic-gate				exit(1);
1947c478bdstevel@tonic-gate			}
1957c478bdstevel@tonic-gate			fmt = nl_langinfo(_DATE_FMT);
1967c478bdstevel@tonic-gate		}
19763ff6dcGarrett D'Amore	} else if (Rflag) {
19863ff6dcGarrett D'Amore		fmt = "%a, %d %h %Y %H:%M:%S %z";
1997c478bdstevel@tonic-gate	} else
2007c478bdstevel@tonic-gate		fmt = nl_langinfo(_DATE_FMT);
2017c478bdstevel@tonic-gate
20272fb660Keith M Wesolowski	fmt_extensions(fmtbuf, sizeof (fmtbuf), fmt, &ts);
20372fb660Keith M Wesolowski
2047c478bdstevel@tonic-gate	if (uflag) {
2057c478bdstevel@tonic-gate		(void) putenv("TZ=GMT0");
2067c478bdstevel@tonic-gate		tzset();
2077c478bdstevel@tonic-gate		tp = gmtime(&clock_val);
2087c478bdstevel@tonic-gate	} else
2097c478bdstevel@tonic-gate		tp = localtime(&clock_val);
2107c478bdstevel@tonic-gate	(void) memcpy(&tm, tp, sizeof (struct tm));
21172fb660Keith M Wesolowski	(void) strftime(buf, BUFSIZ, fmtbuf, &tm);
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate	(void) puts(buf);
2147c478bdstevel@tonic-gate
2157c478bdstevel@tonic-gate	return (0);
2167c478bdstevel@tonic-gate}
2177c478bdstevel@tonic-gate
2187c478bdstevel@tonic-gateint
2197c478bdstevel@tonic-gatesetdate(struct tm *current_date, char *date)
2207c478bdstevel@tonic-gate{
2217c478bdstevel@tonic-gate	int	i;
2227c478bdstevel@tonic-gate	int	mm;
2237c478bdstevel@tonic-gate	int	hh;
2247c478bdstevel@tonic-gate	int	min;
2257c478bdstevel@tonic-gate	int	sec = 0;
2267c478bdstevel@tonic-gate	char	*secptr;
2277c478bdstevel@tonic-gate	int	yy;
2287c478bdstevel@tonic-gate	int	dd	= 0;
2297c478bdstevel@tonic-gate	int	minidx	= 6;
2307c478bdstevel@tonic-gate	int	len;
2317c478bdstevel@tonic-gate	int	dd_check;
2327c478bdstevel@tonic-gate
2337c478bdstevel@tonic-gate	/*  Parse date string  */
2347c478bdstevel@tonic-gate	if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 &&
2357c478bdstevel@tonic-gate	    isdigit(secptr[1]) && isdigit(secptr[2]) &&
2367c478bdstevel@tonic-gate	    (sec = atoi(&secptr[1])) >= 0 && sec < 60)
2377c478bdstevel@tonic-gate		secptr[0] = '\0';	/* eat decimal point only on success */
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate	len = strlen(date);
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate	for (i = 0; i < len; i++) {
2427c478bdstevel@tonic-gate		if (!isdigit(date[i])) {
2437c478bdstevel@tonic-gate			(void) fprintf(stderr,
2447c478bdstevel@tonic-gate			gettext("date: bad conversion\n"));
2457c478bdstevel@tonic-gate			exit(1);
2467c478bdstevel@tonic-gate		}
2477c478bdstevel@tonic-gate	}
2487c478bdstevel@tonic-gate	switch (strlen(date)) {
2497c478bdstevel@tonic-gate	case 12:
2507c478bdstevel@tonic-gate		yy = atoi(&date[8]);
2517c478bdstevel@tonic-gate		date[8] = '\0';
2527c478bdstevel@tonic-gate		break;
2537c478bdstevel@tonic-gate	case 10:
2547c478bdstevel@tonic-gate		/*
2557c478bdstevel@tonic-gate		 * The YY format has the following representation:
2567c478bdstevel@tonic-gate		 * 00-68 = 2000 thru 2068
2577c478bdstevel@tonic-gate		 * 69-99 = 1969 thru 1999
2587c478bdstevel@tonic-gate		 */
2597c478bdstevel@tonic-gate		if (atoi(&date[8]) <= 68) {
2607c478bdstevel@tonic-gate			yy = 1900 + (atoi(&date[8]) + 100);
2617c478bdstevel@tonic-gate		} else {
2627c478bdstevel@tonic-gate			yy = 1900 + atoi(&date[8]);
2637c478bdstevel@tonic-gate		}
2647c478bdstevel@tonic-gate		date[8] = '\0';
2657c478bdstevel@tonic-gate		break;
2667c478bdstevel@tonic-gate	case 8:
2677c478bdstevel@tonic-gate		yy = 1900 + current_date->tm_year;
2687c478bdstevel@tonic-gate		break;
2697c478bdstevel@tonic-gate	case 4:
2707c478bdstevel@tonic-gate		yy = 1900 + current_date->tm_year;
2717c478bdstevel@tonic-gate		mm = current_date->tm_mon + 1; 	/* tm_mon goes from 1 to 11 */
2727c478bdstevel@tonic-gate		dd = current_date->tm_mday;
2737c478bdstevel@tonic-gate		minidx = 2;
2747c478bdstevel@tonic-gate		break;
2757c478bdstevel@tonic-gate	default:
2767c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext("date: bad conversion\n"));
2777c478bdstevel@tonic-gate		return (1);
2787c478bdstevel@tonic-gate	}
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate	min = atoi(&date[minidx]);
2817c478bdstevel@tonic-gate	date[minidx] = '\0';
2827c478bdstevel@tonic-gate	hh = atoi(&date[minidx-2]);
2837c478bdstevel@tonic-gate	date[minidx-2] = '\0';
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate	if (!dd) {
2867c478bdstevel@tonic-gate		/*
2877c478bdstevel@tonic-gate		 * if dd is 0 (not between 1 and 31), then
2887c478bdstevel@tonic-gate		 * read the value supplied by the user.
2897c478bdstevel@tonic-gate		 */
2907c478bdstevel@tonic-gate		dd = atoi(&date[2]);
2917c478bdstevel@tonic-gate		date[2] = '\0';
2927c478bdstevel@tonic-gate		mm = atoi(&date[0]);
2937c478bdstevel@tonic-gate	}
2947c478bdstevel@tonic-gate
2957c478bdstevel@tonic-gate	if (hh == 24)
2967c478bdstevel@tonic-gate		hh = 0, dd++;
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate	/*  Validate date elements  */
2997c478bdstevel@tonic-gate	dd_check = 0;
3007c478bdstevel@tonic-gate	if (mm >= 1 && mm <= 12) {
3017c478bdstevel@tonic-gate		dd_check = month_size[mm - 1];	/* get days in this month */
3027c478bdstevel@tonic-gate		if (mm == 2 && isleap(yy))	/* adjust for leap year */
3037c478bdstevel@tonic-gate			dd_check++;
3047c478bdstevel@tonic-gate	}
3057c478bdstevel@tonic-gate	if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) &&
30663ff6dcGarrett D'Amore	    (hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) {
3077c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext("date: bad conversion\n"));
3087c478bdstevel@tonic-gate		return (1);
3097c478bdstevel@tonic-gate	}
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	/*  Build date and time number  */
3127c478bdstevel@tonic-gate	for (clock_val = 0, i = 1970; i < yy; i++)
3137c478bdstevel@tonic-gate		clock_val += year_size(i);
3147c478bdstevel@tonic-gate	/*  Adjust for leap year  */
3157c478bdstevel@tonic-gate	if (isleap(yy) && mm >= 3)
3167c478bdstevel@tonic-gate		clock_val += 1;
3177c478bdstevel@tonic-gate	/*  Adjust for different month lengths  */
3187c478bdstevel@tonic-gate	while (--mm)
3197c478bdstevel@tonic-gate		clock_val += (time_t)month_size[mm - 1];
3207c478bdstevel@tonic-gate	/*  Load up the rest  */
3217c478bdstevel@tonic-gate	clock_val += (time_t)(dd - 1);
3227c478bdstevel@tonic-gate	clock_val *= 24;
3237c478bdstevel@tonic-gate	clock_val += (time_t)hh;
3247c478bdstevel@tonic-gate	clock_val *= 60;
3257c478bdstevel@tonic-gate	clock_val += (time_t)min;
3267c478bdstevel@tonic-gate	clock_val *= 60;
3277c478bdstevel@tonic-gate	clock_val += sec;
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate	if (!uflag) {
3307c478bdstevel@tonic-gate		/* convert to GMT assuming standard time */
3317c478bdstevel@tonic-gate		/* correction is made in localtime(3C) */
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gate		/*
3347c478bdstevel@tonic-gate		 * call localtime to set up "timezone" variable applicable
3357c478bdstevel@tonic-gate		 * for clock_val time, to support Olson timezones which
3367c478bdstevel@tonic-gate		 * can allow timezone rules to change.
3377c478bdstevel@tonic-gate		 */
3387c478bdstevel@tonic-gate		(void) localtime(&clock_val);
3397c478bdstevel@tonic-gate
3407c478bdstevel@tonic-gate		clock_val += (time_t)timezone;
3417c478bdstevel@tonic-gate
3427c478bdstevel@tonic-gate		/* correct if daylight savings time in effect */
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate		if (localtime(&clock_val)->tm_isdst)
3457c478bdstevel@tonic-gate			clock_val = clock_val - (time_t)(timezone - altzone);
3467c478bdstevel@tonic-gate	}
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate	(void) time(&wtmpx[0].ut_xtime);
3497c478bdstevel@tonic-gate	if (stime(&clock_val) < 0) {
3507c478bdstevel@tonic-gate		perror("date");
3517c478bdstevel@tonic-gate		return (1);
3527c478bdstevel@tonic-gate	}
3537c478bdstevel@tonic-gate#if defined(i386)
3547c478bdstevel@tonic-gate	/* correct the kernel's "gmt_lag" and the PC's RTC */
3557c478bdstevel@tonic-gate	(void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
3567c478bdstevel@tonic-gate#endif
3577c478bdstevel@tonic-gate	(void) time(&wtmpx[1].ut_xtime);
3587c478bdstevel@tonic-gate	(void) pututxline(&wtmpx[0]);
3597c478bdstevel@tonic-gate	(void) pututxline(&wtmpx[1]);
3607c478bdstevel@tonic-gate	(void) updwtmpx(WTMPX_FILE, &wtmpx[0]);
3617c478bdstevel@tonic-gate	(void) updwtmpx(WTMPX_FILE, &wtmpx[1]);
3627c478bdstevel@tonic-gate	return (0);
3637c478bdstevel@tonic-gate}
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gateint
3667c478bdstevel@tonic-gateget_adj(char *cp, struct timeval *tp)
3677c478bdstevel@tonic-gate{
3687c478bdstevel@tonic-gate	register int mult;
3697c478bdstevel@tonic-gate	int sign;
3707c478bdstevel@tonic-gate
3717c478bdstevel@tonic-gate	/* arg must be [-]sss[.fff] */
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate	tp->tv_sec = tp->tv_usec = 0;
3747c478bdstevel@tonic-gate	if (*cp == '-') {
3757c478bdstevel@tonic-gate		sign = -1;
3767c478bdstevel@tonic-gate		cp++;
3777c478bdstevel@tonic-gate	} else {
3787c478bdstevel@tonic-gate		sign = 1;
3797c478bdstevel@tonic-gate	}
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	while (*cp >= '0' && *cp <= '9') {
3827c478bdstevel@tonic-gate		tp->tv_sec *= 10;
3837c478bdstevel@tonic-gate		tp->tv_sec += *cp++ - '0';
3847c478bdstevel@tonic-gate	}
3857c478bdstevel@tonic-gate	if (*cp == '.') {
3867c478bdstevel@tonic-gate		cp++;
3877c478bdstevel@tonic-gate		mult = 100000;
3887c478bdstevel@tonic-gate		while (*cp >= '0' && *cp <= '9') {
3897c478bdstevel@tonic-gate			tp->tv_usec += (*cp++ - '0') * mult;
3907c478bdstevel@tonic-gate			mult /= 10;
3917c478bdstevel@tonic-gate		}
3927c478bdstevel@tonic-gate	}
3937c478bdstevel@tonic-gate	/*
3947c478bdstevel@tonic-gate	 * if there's anything left in the string,
3957c478bdstevel@tonic-gate	 * the input was invalid.
3967c478bdstevel@tonic-gate	 */
3977c478bdstevel@tonic-gate	if (*cp) {
3987c478bdstevel@tonic-gate		return (-1);
3997c478bdstevel@tonic-gate	} else {
4007c478bdstevel@tonic-gate		tp->tv_sec *= sign;
4017c478bdstevel@tonic-gate		tp->tv_usec *= sign;
4027c478bdstevel@tonic-gate		return (0);
4037c478bdstevel@tonic-gate	}
4047c478bdstevel@tonic-gate}
40572fb660Keith M Wesolowski
40672fb660Keith M Wesolowski/*
40772fb660Keith M Wesolowski * Extensions that cannot be interpreted by strftime are interpreted here.
40872fb660Keith M Wesolowski */
40972fb660Keith M Wesolowskivoid
41072fb660Keith M Wesolowskifmt_extensions(char *fmtbuf, size_t len,
41172fb660Keith M Wesolowski    const char *fmt, const struct timespec *tsp)
41272fb660Keith M Wesolowski{
41372fb660Keith M Wesolowski	const char *p;
41472fb660Keith M Wesolowski	char *q;
41572fb660Keith M Wesolowski
41672fb660Keith M Wesolowski	for (p = fmt, q = fmtbuf; *p != '\0' && q < fmtbuf + len; ++p) {
41772fb660Keith M Wesolowski		if (*p == '%') {
41872fb660Keith M Wesolowski			switch (*(p + 1)) {
41972fb660Keith M Wesolowski			case 'N':
42072fb660Keith M Wesolowski				++p;
42172fb660Keith M Wesolowski				q += snprintf(q, len - (q - fmtbuf),
42272fb660Keith M Wesolowski				    "%09lu", tsp->tv_nsec);
42372fb660Keith M Wesolowski				continue;
42472fb660Keith M Wesolowski			}
42572fb660Keith M Wesolowski		}
42672fb660Keith M Wesolowski		*q++ = *p;
42772fb660Keith M Wesolowski	}
42872fb660Keith M Wesolowski
42972fb660Keith M Wesolowski	if (q < fmtbuf + len)
43072fb660Keith M Wesolowski		*q = '\0';
43172fb660Keith M Wesolowski	else
43272fb660Keith M Wesolowski		fmtbuf[len - 1] = '\0';
43372fb660Keith M Wesolowski}
434