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 /* 27bde2df36SYuri Pankov * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 2863ff6dceSGarrett D'Amore */ 29*72fb660eSKeith M Wesolowski /* 30*72fb660eSKeith M Wesolowski * Copyright (c) 2012, Joyent, Inc. All rights reserved. 31*72fb660eSKeith M Wesolowski */ 327c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 337c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate /* 367c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 377c478bd9Sstevel@tonic-gate * The Regents of the University of California 387c478bd9Sstevel@tonic-gate * All Rights Reserved 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 417c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 427c478bd9Sstevel@tonic-gate * contributors. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * date - with format capabilities and international flair 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #include <locale.h> 507c478bd9Sstevel@tonic-gate #include <fcntl.h> 517c478bd9Sstevel@tonic-gate #include <langinfo.h> 527c478bd9Sstevel@tonic-gate #include <stdio.h> 537c478bd9Sstevel@tonic-gate #include <stdlib.h> 547c478bd9Sstevel@tonic-gate #include <string.h> 557c478bd9Sstevel@tonic-gate #include <time.h> 567c478bd9Sstevel@tonic-gate #include <unistd.h> 577c478bd9Sstevel@tonic-gate #include <sys/time.h> 587c478bd9Sstevel@tonic-gate #include <sys/types.h> 597c478bd9Sstevel@tonic-gate #include <ctype.h> 607c478bd9Sstevel@tonic-gate #include <utmpx.h> 617c478bd9Sstevel@tonic-gate #include <tzfile.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #define year_size(A) ((isleap(A)) ? 366 : 365) 647c478bd9Sstevel@tonic-gate static char buf[BUFSIZ]; 657c478bd9Sstevel@tonic-gate static time_t clock_val; 667c478bd9Sstevel@tonic-gate static short month_size[12] = 677c478bd9Sstevel@tonic-gate { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 687c478bd9Sstevel@tonic-gate static struct utmpx wtmpx[2] = { 697c478bd9Sstevel@tonic-gate {"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0}, 707c478bd9Sstevel@tonic-gate {"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0} 717c478bd9Sstevel@tonic-gate }; 727c478bd9Sstevel@tonic-gate static char *usage = 73bde2df36SYuri Pankov "usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n\tdate [-Ru] [+format]\n" 747c478bd9Sstevel@tonic-gate "\tdate -a [-]sss[.fff]\n"; 757c478bd9Sstevel@tonic-gate static int uflag = 0; 7663ff6dceSGarrett D'Amore static int Rflag = 0; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate static int get_adj(char *, struct timeval *); 797c478bd9Sstevel@tonic-gate static int setdate(struct tm *, char *); 80*72fb660eSKeith M Wesolowski static void fmt_extensions(char *, size_t, 81*72fb660eSKeith M Wesolowski const char *, const struct timespec *); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate int 847c478bd9Sstevel@tonic-gate main(int argc, char **argv) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate struct tm *tp, tm; 877c478bd9Sstevel@tonic-gate struct timeval tv; 887c478bd9Sstevel@tonic-gate char *fmt; 89*72fb660eSKeith M Wesolowski char fmtbuf[BUFSIZ]; 907c478bd9Sstevel@tonic-gate int c, aflag = 0, illflag = 0; 91*72fb660eSKeith M Wesolowski struct timespec ts; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 967c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 977c478bd9Sstevel@tonic-gate #endif 987c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 997c478bd9Sstevel@tonic-gate 10063ff6dceSGarrett D'Amore while ((c = getopt(argc, argv, "a:uR")) != EOF) 1017c478bd9Sstevel@tonic-gate switch (c) { 1027c478bd9Sstevel@tonic-gate case 'a': 1037c478bd9Sstevel@tonic-gate aflag++; 1047c478bd9Sstevel@tonic-gate if (get_adj(optarg, &tv) < 0) { 1057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1067c478bd9Sstevel@tonic-gate gettext("date: invalid argument -- %s\n"), 1077c478bd9Sstevel@tonic-gate optarg); 1087c478bd9Sstevel@tonic-gate illflag++; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate break; 1117c478bd9Sstevel@tonic-gate case 'u': 1127c478bd9Sstevel@tonic-gate uflag++; 1137c478bd9Sstevel@tonic-gate break; 11463ff6dceSGarrett D'Amore case 'R': 11563ff6dceSGarrett D'Amore Rflag++; 11663ff6dceSGarrett D'Amore break; 1177c478bd9Sstevel@tonic-gate default: 1187c478bd9Sstevel@tonic-gate illflag++; 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate argc -= optind; 1227c478bd9Sstevel@tonic-gate argv = &argv[optind]; 1237c478bd9Sstevel@tonic-gate 12463ff6dceSGarrett D'Amore /* -a is mutually exclusive with -u and -R */ 1257c478bd9Sstevel@tonic-gate if (uflag && aflag) 1267c478bd9Sstevel@tonic-gate illflag++; 12763ff6dceSGarrett D'Amore if (Rflag && aflag) 12863ff6dceSGarrett D'Amore illflag++; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if (illflag) { 1317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(usage)); 1327c478bd9Sstevel@tonic-gate exit(1); 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate 135*72fb660eSKeith M Wesolowski if (clock_gettime(CLOCK_REALTIME, &ts) != 0) { 136*72fb660eSKeith M Wesolowski perror(gettext("date: Failed to obtain system time")); 137*72fb660eSKeith M Wesolowski exit(1); 138*72fb660eSKeith M Wesolowski } 139*72fb660eSKeith M Wesolowski clock_val = ts.tv_sec; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (aflag) { 1427c478bd9Sstevel@tonic-gate if (adjtime(&tv, 0) < 0) { 1437c478bd9Sstevel@tonic-gate perror(gettext("date: Failed to adjust date")); 1447c478bd9Sstevel@tonic-gate exit(1); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate exit(0); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (argc > 0) { 1507c478bd9Sstevel@tonic-gate if (*argv[0] == '+') 1517c478bd9Sstevel@tonic-gate fmt = &argv[0][1]; 1527c478bd9Sstevel@tonic-gate else { 1537c478bd9Sstevel@tonic-gate if (setdate(localtime(&clock_val), argv[0])) { 1547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(usage)); 1557c478bd9Sstevel@tonic-gate exit(1); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate fmt = nl_langinfo(_DATE_FMT); 1587c478bd9Sstevel@tonic-gate } 15963ff6dceSGarrett D'Amore } else if (Rflag) { 16063ff6dceSGarrett D'Amore fmt = "%a, %d %h %Y %H:%M:%S %z"; 1617c478bd9Sstevel@tonic-gate } else 1627c478bd9Sstevel@tonic-gate fmt = nl_langinfo(_DATE_FMT); 1637c478bd9Sstevel@tonic-gate 164*72fb660eSKeith M Wesolowski fmt_extensions(fmtbuf, sizeof (fmtbuf), fmt, &ts); 165*72fb660eSKeith M Wesolowski 1667c478bd9Sstevel@tonic-gate if (uflag) { 1677c478bd9Sstevel@tonic-gate (void) putenv("TZ=GMT0"); 1687c478bd9Sstevel@tonic-gate tzset(); 1697c478bd9Sstevel@tonic-gate tp = gmtime(&clock_val); 1707c478bd9Sstevel@tonic-gate } else 1717c478bd9Sstevel@tonic-gate tp = localtime(&clock_val); 1727c478bd9Sstevel@tonic-gate (void) memcpy(&tm, tp, sizeof (struct tm)); 173*72fb660eSKeith M Wesolowski (void) strftime(buf, BUFSIZ, fmtbuf, &tm); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate (void) puts(buf); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate return (0); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate int 1817c478bd9Sstevel@tonic-gate setdate(struct tm *current_date, char *date) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate int i; 1847c478bd9Sstevel@tonic-gate int mm; 1857c478bd9Sstevel@tonic-gate int hh; 1867c478bd9Sstevel@tonic-gate int min; 1877c478bd9Sstevel@tonic-gate int sec = 0; 1887c478bd9Sstevel@tonic-gate char *secptr; 1897c478bd9Sstevel@tonic-gate int yy; 1907c478bd9Sstevel@tonic-gate int dd = 0; 1917c478bd9Sstevel@tonic-gate int minidx = 6; 1927c478bd9Sstevel@tonic-gate int len; 1937c478bd9Sstevel@tonic-gate int dd_check; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* Parse date string */ 1967c478bd9Sstevel@tonic-gate if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 && 1977c478bd9Sstevel@tonic-gate isdigit(secptr[1]) && isdigit(secptr[2]) && 1987c478bd9Sstevel@tonic-gate (sec = atoi(&secptr[1])) >= 0 && sec < 60) 1997c478bd9Sstevel@tonic-gate secptr[0] = '\0'; /* eat decimal point only on success */ 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate len = strlen(date); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate for (i = 0; i < len; i++) { 2047c478bd9Sstevel@tonic-gate if (!isdigit(date[i])) { 2057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2067c478bd9Sstevel@tonic-gate gettext("date: bad conversion\n")); 2077c478bd9Sstevel@tonic-gate exit(1); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate switch (strlen(date)) { 2117c478bd9Sstevel@tonic-gate case 12: 2127c478bd9Sstevel@tonic-gate yy = atoi(&date[8]); 2137c478bd9Sstevel@tonic-gate date[8] = '\0'; 2147c478bd9Sstevel@tonic-gate break; 2157c478bd9Sstevel@tonic-gate case 10: 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * The YY format has the following representation: 2187c478bd9Sstevel@tonic-gate * 00-68 = 2000 thru 2068 2197c478bd9Sstevel@tonic-gate * 69-99 = 1969 thru 1999 2207c478bd9Sstevel@tonic-gate */ 2217c478bd9Sstevel@tonic-gate if (atoi(&date[8]) <= 68) { 2227c478bd9Sstevel@tonic-gate yy = 1900 + (atoi(&date[8]) + 100); 2237c478bd9Sstevel@tonic-gate } else { 2247c478bd9Sstevel@tonic-gate yy = 1900 + atoi(&date[8]); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate date[8] = '\0'; 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate case 8: 2297c478bd9Sstevel@tonic-gate yy = 1900 + current_date->tm_year; 2307c478bd9Sstevel@tonic-gate break; 2317c478bd9Sstevel@tonic-gate case 4: 2327c478bd9Sstevel@tonic-gate yy = 1900 + current_date->tm_year; 2337c478bd9Sstevel@tonic-gate mm = current_date->tm_mon + 1; /* tm_mon goes from 1 to 11 */ 2347c478bd9Sstevel@tonic-gate dd = current_date->tm_mday; 2357c478bd9Sstevel@tonic-gate minidx = 2; 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate default: 2387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("date: bad conversion\n")); 2397c478bd9Sstevel@tonic-gate return (1); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate min = atoi(&date[minidx]); 2437c478bd9Sstevel@tonic-gate date[minidx] = '\0'; 2447c478bd9Sstevel@tonic-gate hh = atoi(&date[minidx-2]); 2457c478bd9Sstevel@tonic-gate date[minidx-2] = '\0'; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate if (!dd) { 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * if dd is 0 (not between 1 and 31), then 2507c478bd9Sstevel@tonic-gate * read the value supplied by the user. 2517c478bd9Sstevel@tonic-gate */ 2527c478bd9Sstevel@tonic-gate dd = atoi(&date[2]); 2537c478bd9Sstevel@tonic-gate date[2] = '\0'; 2547c478bd9Sstevel@tonic-gate mm = atoi(&date[0]); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (hh == 24) 2587c478bd9Sstevel@tonic-gate hh = 0, dd++; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* Validate date elements */ 2617c478bd9Sstevel@tonic-gate dd_check = 0; 2627c478bd9Sstevel@tonic-gate if (mm >= 1 && mm <= 12) { 2637c478bd9Sstevel@tonic-gate dd_check = month_size[mm - 1]; /* get days in this month */ 2647c478bd9Sstevel@tonic-gate if (mm == 2 && isleap(yy)) /* adjust for leap year */ 2657c478bd9Sstevel@tonic-gate dd_check++; 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) && 2687c478bd9Sstevel@tonic-gate (hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) { 2697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("date: bad conversion\n")); 2707c478bd9Sstevel@tonic-gate return (1); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate /* Build date and time number */ 2747c478bd9Sstevel@tonic-gate for (clock_val = 0, i = 1970; i < yy; i++) 2757c478bd9Sstevel@tonic-gate clock_val += year_size(i); 2767c478bd9Sstevel@tonic-gate /* Adjust for leap year */ 2777c478bd9Sstevel@tonic-gate if (isleap(yy) && mm >= 3) 2787c478bd9Sstevel@tonic-gate clock_val += 1; 2797c478bd9Sstevel@tonic-gate /* Adjust for different month lengths */ 2807c478bd9Sstevel@tonic-gate while (--mm) 2817c478bd9Sstevel@tonic-gate clock_val += (time_t)month_size[mm - 1]; 2827c478bd9Sstevel@tonic-gate /* Load up the rest */ 2837c478bd9Sstevel@tonic-gate clock_val += (time_t)(dd - 1); 2847c478bd9Sstevel@tonic-gate clock_val *= 24; 2857c478bd9Sstevel@tonic-gate clock_val += (time_t)hh; 2867c478bd9Sstevel@tonic-gate clock_val *= 60; 2877c478bd9Sstevel@tonic-gate clock_val += (time_t)min; 2887c478bd9Sstevel@tonic-gate clock_val *= 60; 2897c478bd9Sstevel@tonic-gate clock_val += sec; 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (!uflag) { 2927c478bd9Sstevel@tonic-gate /* convert to GMT assuming standard time */ 2937c478bd9Sstevel@tonic-gate /* correction is made in localtime(3C) */ 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* 2967c478bd9Sstevel@tonic-gate * call localtime to set up "timezone" variable applicable 2977c478bd9Sstevel@tonic-gate * for clock_val time, to support Olson timezones which 2987c478bd9Sstevel@tonic-gate * can allow timezone rules to change. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate (void) localtime(&clock_val); 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate clock_val += (time_t)timezone; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate /* correct if daylight savings time in effect */ 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (localtime(&clock_val)->tm_isdst) 3077c478bd9Sstevel@tonic-gate clock_val = clock_val - (time_t)(timezone - altzone); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate (void) time(&wtmpx[0].ut_xtime); 3117c478bd9Sstevel@tonic-gate if (stime(&clock_val) < 0) { 3127c478bd9Sstevel@tonic-gate perror("date"); 3137c478bd9Sstevel@tonic-gate return (1); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate #if defined(i386) 3167c478bd9Sstevel@tonic-gate /* correct the kernel's "gmt_lag" and the PC's RTC */ 3177c478bd9Sstevel@tonic-gate (void) system("/usr/sbin/rtc -c > /dev/null 2>&1"); 3187c478bd9Sstevel@tonic-gate #endif 3197c478bd9Sstevel@tonic-gate (void) time(&wtmpx[1].ut_xtime); 3207c478bd9Sstevel@tonic-gate (void) pututxline(&wtmpx[0]); 3217c478bd9Sstevel@tonic-gate (void) pututxline(&wtmpx[1]); 3227c478bd9Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx[0]); 3237c478bd9Sstevel@tonic-gate (void) updwtmpx(WTMPX_FILE, &wtmpx[1]); 3247c478bd9Sstevel@tonic-gate return (0); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate int 3287c478bd9Sstevel@tonic-gate get_adj(char *cp, struct timeval *tp) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate register int mult; 3317c478bd9Sstevel@tonic-gate int sign; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* arg must be [-]sss[.fff] */ 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate tp->tv_sec = tp->tv_usec = 0; 3367c478bd9Sstevel@tonic-gate if (*cp == '-') { 3377c478bd9Sstevel@tonic-gate sign = -1; 3387c478bd9Sstevel@tonic-gate cp++; 3397c478bd9Sstevel@tonic-gate } else { 3407c478bd9Sstevel@tonic-gate sign = 1; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9') { 3447c478bd9Sstevel@tonic-gate tp->tv_sec *= 10; 3457c478bd9Sstevel@tonic-gate tp->tv_sec += *cp++ - '0'; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate if (*cp == '.') { 3487c478bd9Sstevel@tonic-gate cp++; 3497c478bd9Sstevel@tonic-gate mult = 100000; 3507c478bd9Sstevel@tonic-gate while (*cp >= '0' && *cp <= '9') { 3517c478bd9Sstevel@tonic-gate tp->tv_usec += (*cp++ - '0') * mult; 3527c478bd9Sstevel@tonic-gate mult /= 10; 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate /* 3567c478bd9Sstevel@tonic-gate * if there's anything left in the string, 3577c478bd9Sstevel@tonic-gate * the input was invalid. 3587c478bd9Sstevel@tonic-gate */ 3597c478bd9Sstevel@tonic-gate if (*cp) { 3607c478bd9Sstevel@tonic-gate return (-1); 3617c478bd9Sstevel@tonic-gate } else { 3627c478bd9Sstevel@tonic-gate tp->tv_sec *= sign; 3637c478bd9Sstevel@tonic-gate tp->tv_usec *= sign; 3647c478bd9Sstevel@tonic-gate return (0); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate } 367*72fb660eSKeith M Wesolowski 368*72fb660eSKeith M Wesolowski /* 369*72fb660eSKeith M Wesolowski * Extensions that cannot be interpreted by strftime are interpreted here. 370*72fb660eSKeith M Wesolowski */ 371*72fb660eSKeith M Wesolowski void 372*72fb660eSKeith M Wesolowski fmt_extensions(char *fmtbuf, size_t len, 373*72fb660eSKeith M Wesolowski const char *fmt, const struct timespec *tsp) 374*72fb660eSKeith M Wesolowski { 375*72fb660eSKeith M Wesolowski const char *p; 376*72fb660eSKeith M Wesolowski char *q; 377*72fb660eSKeith M Wesolowski 378*72fb660eSKeith M Wesolowski for (p = fmt, q = fmtbuf; *p != '\0' && q < fmtbuf + len; ++p) { 379*72fb660eSKeith M Wesolowski if (*p == '%') { 380*72fb660eSKeith M Wesolowski switch (*(p + 1)) { 381*72fb660eSKeith M Wesolowski case 'N': 382*72fb660eSKeith M Wesolowski ++p; 383*72fb660eSKeith M Wesolowski q += snprintf(q, len - (q - fmtbuf), 384*72fb660eSKeith M Wesolowski "%09lu", tsp->tv_nsec); 385*72fb660eSKeith M Wesolowski continue; 386*72fb660eSKeith M Wesolowski } 387*72fb660eSKeith M Wesolowski } 388*72fb660eSKeith M Wesolowski *q++ = *p; 389*72fb660eSKeith M Wesolowski } 390*72fb660eSKeith M Wesolowski 391*72fb660eSKeith M Wesolowski if (q < fmtbuf + len) 392*72fb660eSKeith M Wesolowski *q = '\0'; 393*72fb660eSKeith M Wesolowski else 394*72fb660eSKeith M Wesolowski fmtbuf[len - 1] = '\0'; 395*72fb660eSKeith M Wesolowski } 396