xref: /illumos-gate/usr/src/ucbcmd/touch/touch.c (revision 8c0b080c)
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 2002 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/file.h>
327c478bd9Sstevel@tonic-gate #include <sys/time.h>
337c478bd9Sstevel@tonic-gate #include <time.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate struct	stat	stbuf;
407c478bd9Sstevel@tonic-gate int	status;
417c478bd9Sstevel@tonic-gate #ifdef S5EMUL
427c478bd9Sstevel@tonic-gate int dmsize[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
43*cc6c5292Schin #endif /* S5EMUL */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char usage[] =
467c478bd9Sstevel@tonic-gate #ifdef S5EMUL
477c478bd9Sstevel@tonic-gate 		"[-amc] [mmddhhmm[yy]]";
487c478bd9Sstevel@tonic-gate #else /*!S5EMUL*/
497c478bd9Sstevel@tonic-gate 		"[-amcf]";
507c478bd9Sstevel@tonic-gate int	force = 0;
517c478bd9Sstevel@tonic-gate int	nowrite;
527c478bd9Sstevel@tonic-gate #endif /*!S5EMUL*/
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate int	mflg=1, aflg=1, cflg=0, nflg=0;
557c478bd9Sstevel@tonic-gate char	*prog;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #ifdef S5EMUL
587c478bd9Sstevel@tonic-gate char	*cbp;
59*cc6c5292Schin #endif /* S5EMUL */
607c478bd9Sstevel@tonic-gate time_t	time();
617c478bd9Sstevel@tonic-gate off_t	lseek();
627c478bd9Sstevel@tonic-gate time_t	timelocal(), timegm();
637c478bd9Sstevel@tonic-gate struct timeval timbuf;
647c478bd9Sstevel@tonic-gate static void timestruc_to_timeval(timestruc_t *, struct timeval *);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifdef S5EMUL
677c478bd9Sstevel@tonic-gate struct tm *
gtime()687c478bd9Sstevel@tonic-gate gtime()
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	static struct tm newtime;
717c478bd9Sstevel@tonic-gate 	long nt;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	newtime.tm_mon = gpair() - 1;
747c478bd9Sstevel@tonic-gate 	newtime.tm_mday = gpair();
757c478bd9Sstevel@tonic-gate 	newtime.tm_hour = gpair();
767c478bd9Sstevel@tonic-gate 	if (newtime.tm_hour == 24) {
777c478bd9Sstevel@tonic-gate 		newtime.tm_hour = 0;
787c478bd9Sstevel@tonic-gate 		newtime.tm_mday++;
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	newtime.tm_min = gpair();
817c478bd9Sstevel@tonic-gate 	newtime.tm_sec = 0;
827c478bd9Sstevel@tonic-gate 	newtime.tm_year = gpair();
837c478bd9Sstevel@tonic-gate 	if (newtime.tm_year < 0) {
847c478bd9Sstevel@tonic-gate 		(void) time(&nt);
857c478bd9Sstevel@tonic-gate 		newtime.tm_year = localtime(&nt)->tm_year;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	return (&newtime);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
gpair()907c478bd9Sstevel@tonic-gate gpair()
917c478bd9Sstevel@tonic-gate {
92*cc6c5292Schin 	int c, d;
93*cc6c5292Schin 	char *cp;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	cp = cbp;
967c478bd9Sstevel@tonic-gate 	if (*cp == 0)
977c478bd9Sstevel@tonic-gate 		return (-1);
987c478bd9Sstevel@tonic-gate 	c = (*cp++ - '0') * 10;
997c478bd9Sstevel@tonic-gate 	if (c<0 || c>100)
1007c478bd9Sstevel@tonic-gate 		return (-1);
1017c478bd9Sstevel@tonic-gate 	if (*cp == 0)
1027c478bd9Sstevel@tonic-gate 		return (-1);
1037c478bd9Sstevel@tonic-gate 	if ((d = *cp++ - '0') < 0 || d > 9)
1047c478bd9Sstevel@tonic-gate 		return (-1);
1057c478bd9Sstevel@tonic-gate 	cbp = cp;
1067c478bd9Sstevel@tonic-gate 	return (c+d);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate #endif /*S5EMUL*/
1097c478bd9Sstevel@tonic-gate 
110*cc6c5292Schin int
main(int argc,char * argv[])111*cc6c5292Schin main(int argc, char *argv[])
1127c478bd9Sstevel@tonic-gate {
113*cc6c5292Schin 	int c;
1147c478bd9Sstevel@tonic-gate #ifdef S5EMUL
1157c478bd9Sstevel@tonic-gate 	int days_in_month;
1167c478bd9Sstevel@tonic-gate 	struct tm *tp;
117*cc6c5292Schin #endif /* S5EMUL */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	int errflg=0, optc;
1207c478bd9Sstevel@tonic-gate 	extern char *optarg;
1217c478bd9Sstevel@tonic-gate 	extern int optind;
1227c478bd9Sstevel@tonic-gate 	extern int opterr;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	prog = argv[0];
1257c478bd9Sstevel@tonic-gate 	opterr = 0;			/* disable getopt() error msgs */
1267c478bd9Sstevel@tonic-gate 	while ((optc=getopt(argc, argv, "amcf")) != EOF)
1277c478bd9Sstevel@tonic-gate 		switch (optc) {
1287c478bd9Sstevel@tonic-gate 		case 'm':
1297c478bd9Sstevel@tonic-gate 			mflg++;
1307c478bd9Sstevel@tonic-gate 			aflg--;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case 'a':
1337c478bd9Sstevel@tonic-gate 			aflg++;
1347c478bd9Sstevel@tonic-gate 			mflg--;
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case 'c':
1377c478bd9Sstevel@tonic-gate 			cflg++;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate #ifndef S5EMUL
1407c478bd9Sstevel@tonic-gate 		case 'f':
1417c478bd9Sstevel@tonic-gate 			force++;	/* SysV version ignores -f */
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate #endif /*!S5EMUL*/
1447c478bd9Sstevel@tonic-gate 		case '?':
1457c478bd9Sstevel@tonic-gate 			errflg++;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (((argc-optind) < 1) || errflg) {
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "usage: %s %s file ...\n", prog, usage);
1507c478bd9Sstevel@tonic-gate 		exit(2);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	status = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #ifdef S5EMUL
1557c478bd9Sstevel@tonic-gate 	if (!isnumber(argv[optind])) {	/* BSD version only sets Present */
1567c478bd9Sstevel@tonic-gate #endif /*S5EMUL*/
1577c478bd9Sstevel@tonic-gate 		if ((aflg <= 0) || (mflg <= 0))
1587c478bd9Sstevel@tonic-gate 			(void) gettimeofday(&timbuf, NULL);
1597c478bd9Sstevel@tonic-gate 		else
1607c478bd9Sstevel@tonic-gate 			nflg++;		/* no -a, -m, or date seen */
1617c478bd9Sstevel@tonic-gate #ifdef S5EMUL
1627c478bd9Sstevel@tonic-gate 	} else {			/* SysV version sets arbitrary date */
1637c478bd9Sstevel@tonic-gate 		cbp = (char *)argv[optind++];
1647c478bd9Sstevel@tonic-gate 		if ((tp = gtime()) == NULL) {
1657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: bad date conversion\n",
1667c478bd9Sstevel@tonic-gate 			    prog);
1677c478bd9Sstevel@tonic-gate 			exit(2);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		days_in_month = dmsize[tp->tm_mon];
1707c478bd9Sstevel@tonic-gate 		if (tp->tm_mon == 1 && isleap(tp->tm_year + 1900))
1717c478bd9Sstevel@tonic-gate 			days_in_month = 29;	/* February in leap year */
1727c478bd9Sstevel@tonic-gate 		if (tp->tm_mon < 0 || tp->tm_mon > 11 ||
1737c478bd9Sstevel@tonic-gate 		    tp->tm_mday < 1 || tp->tm_mday > days_in_month ||
1747c478bd9Sstevel@tonic-gate 		    tp->tm_hour < 0 || tp->tm_hour > 23 ||
1757c478bd9Sstevel@tonic-gate 		    tp->tm_min < 0 || tp->tm_min > 59 ||
1767c478bd9Sstevel@tonic-gate 		    tp->tm_sec < 0 || tp->tm_sec > 59) {
1777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: bad date conversion\n",
1787c478bd9Sstevel@tonic-gate 			    prog);
1797c478bd9Sstevel@tonic-gate 			exit(2);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 		timbuf = timelocal(tp);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate #endif /*S5EMUL*/
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++) {
1867c478bd9Sstevel@tonic-gate 		if (touch(argv[c]) < 0)
1877c478bd9Sstevel@tonic-gate 			status++;
1887c478bd9Sstevel@tonic-gate 	}
189*cc6c5292Schin 	return (status);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate int
touch(filename)1937c478bd9Sstevel@tonic-gate touch(filename)
1947c478bd9Sstevel@tonic-gate 	char *filename;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	struct timeval times[2];
197*cc6c5292Schin 	int fd;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (stat(filename, &stbuf)) {
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * if stat failed for reasons other than ENOENT,
2027c478bd9Sstevel@tonic-gate 		 * the file should not be created, since this
2037c478bd9Sstevel@tonic-gate 		 * can clobber the contents of an existing file
2047c478bd9Sstevel@tonic-gate 		 * (for example, a large file that results in overflow).
2057c478bd9Sstevel@tonic-gate 		 */
2067c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
2077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,"%s: cannot stat ", prog);
2087c478bd9Sstevel@tonic-gate 			perror(filename);
2097c478bd9Sstevel@tonic-gate 			return (-1);
2107c478bd9Sstevel@tonic-gate 		} else if (cflg) {
2117c478bd9Sstevel@tonic-gate 			return (-1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		else if ((fd = creat(filename, 0666)) < 0) {
2147c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot create ", prog);
2157c478bd9Sstevel@tonic-gate 			perror(filename);
2167c478bd9Sstevel@tonic-gate 			return (-1);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 		else {
2197c478bd9Sstevel@tonic-gate 			(void) close(fd);
2207c478bd9Sstevel@tonic-gate 			if (stat(filename, &stbuf)) {
2217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,"%s: cannot stat ", prog);
2227c478bd9Sstevel@tonic-gate 				perror(filename);
2237c478bd9Sstevel@tonic-gate 				return (-1);
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		if (nflg)
2277c478bd9Sstevel@tonic-gate 			return (0);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	times[0] = times[1] = timbuf;
2317c478bd9Sstevel@tonic-gate 	if (mflg <= 0)
2327c478bd9Sstevel@tonic-gate 		timestruc_to_timeval(&stbuf.st_mtim, times + 1);
2337c478bd9Sstevel@tonic-gate 	if (aflg <= 0)
2347c478bd9Sstevel@tonic-gate 		timestruc_to_timeval(&stbuf.st_atim, times);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #ifndef S5EMUL
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Since utime() allows the owner to change file times without
2397c478bd9Sstevel@tonic-gate 	 * regard to access permission, enforce BSD semantics here
2407c478bd9Sstevel@tonic-gate 	 * (cannot touch if read-only and not -f).
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	nowrite = access(filename, R_OK|W_OK);
2437c478bd9Sstevel@tonic-gate 	if (nowrite && !force) {
2447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2457c478bd9Sstevel@tonic-gate 		    "%s: cannot touch %s: no write permission\n",
2467c478bd9Sstevel@tonic-gate 		    prog, filename);
2477c478bd9Sstevel@tonic-gate 		return (-1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate #endif /*!S5EMUL*/
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (utimes(filename, nflg ? NULL : times)) {
2527c478bd9Sstevel@tonic-gate 		if (nflg && (errno != EROFS) && (errno != EACCES)) {
2537c478bd9Sstevel@tonic-gate 			/*
2547c478bd9Sstevel@tonic-gate 			 * If utime() failed to set the Present, it
2557c478bd9Sstevel@tonic-gate 			 * could be a BSD server that is complaining.
2567c478bd9Sstevel@tonic-gate 			 * If that's the case, try the old read/write trick.
2577c478bd9Sstevel@tonic-gate 			 */
2587c478bd9Sstevel@tonic-gate 			return (oldtouch(filename, &stbuf));
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,"%s: cannot change times on ", prog);
2617c478bd9Sstevel@tonic-gate 		perror(filename);
2627c478bd9Sstevel@tonic-gate 		return (-1);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate int
oldtouch(filename,statp)2687c478bd9Sstevel@tonic-gate oldtouch(filename, statp)
2697c478bd9Sstevel@tonic-gate 	char *filename;
270*cc6c5292Schin 	struct stat *statp;
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	int rwstatus;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if ((statp->st_mode & S_IFMT) != S_IFREG) {
2757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2767c478bd9Sstevel@tonic-gate 		    "%s: %s: only owner may touch special files on this filesystem\n",
2777c478bd9Sstevel@tonic-gate 		    prog, filename);
2787c478bd9Sstevel@tonic-gate 		return (-1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #ifndef S5EMUL
2827c478bd9Sstevel@tonic-gate 	if (nowrite && force) {
2837c478bd9Sstevel@tonic-gate 		if (chmod(filename, 0666)) {
2847c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: could not chmod ", prog);
2857c478bd9Sstevel@tonic-gate 			perror(filename);
2867c478bd9Sstevel@tonic-gate 			return (-1);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		rwstatus = readwrite(filename, statp->st_size);
2897c478bd9Sstevel@tonic-gate 		if (chmod(filename, (int)statp->st_mode)) {
2907c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: could not chmod back ", prog);
2917c478bd9Sstevel@tonic-gate 			perror(filename);
2927c478bd9Sstevel@tonic-gate 			return (-1);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		return (rwstatus);
2957c478bd9Sstevel@tonic-gate 	} else
2967c478bd9Sstevel@tonic-gate #endif /*!S5EMUL*/
2977c478bd9Sstevel@tonic-gate 		return (readwrite(filename, statp->st_size));
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate int
readwrite(filename,size)3017c478bd9Sstevel@tonic-gate readwrite(filename, size)
3027c478bd9Sstevel@tonic-gate 	char	*filename;
3037c478bd9Sstevel@tonic-gate 	off_t	size;
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	int fd;
3067c478bd9Sstevel@tonic-gate 	char first;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (size) {
3097c478bd9Sstevel@tonic-gate 		if ((fd = open(filename, 2)) < 0)
3107c478bd9Sstevel@tonic-gate 			goto error;
3117c478bd9Sstevel@tonic-gate 		if (read(fd, &first, 1) != 1)
3127c478bd9Sstevel@tonic-gate 			goto closeerror;
3137c478bd9Sstevel@tonic-gate 		if (lseek(fd, 0L, 0) == -1)
3147c478bd9Sstevel@tonic-gate 			goto closeerror;
3157c478bd9Sstevel@tonic-gate 		if (write(fd, &first, 1) != 1)
3167c478bd9Sstevel@tonic-gate 			goto closeerror;
3177c478bd9Sstevel@tonic-gate 	} else {
3187c478bd9Sstevel@tonic-gate 		if ((fd = creat(filename, 0666)) < 0)
3197c478bd9Sstevel@tonic-gate 			goto error;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 	if (close(fd) < 0)
3227c478bd9Sstevel@tonic-gate 		goto error;
3237c478bd9Sstevel@tonic-gate 	return (0);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate closeerror:
3267c478bd9Sstevel@tonic-gate 	(void) close(fd);
3277c478bd9Sstevel@tonic-gate error:
3287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: could not touch ", prog);
3297c478bd9Sstevel@tonic-gate 	perror(filename);
3307c478bd9Sstevel@tonic-gate 	return (-1);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate #ifdef S5EMUL
isnumber(s)3347c478bd9Sstevel@tonic-gate isnumber(s)
3357c478bd9Sstevel@tonic-gate 	char *s;
3367c478bd9Sstevel@tonic-gate {
337*cc6c5292Schin 	char c;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	while (c = *s++)
3407c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
3417c478bd9Sstevel@tonic-gate 			return (0);
3427c478bd9Sstevel@tonic-gate 	return (1);
3437c478bd9Sstevel@tonic-gate }
344*cc6c5292Schin #endif /* S5EMUL */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * nanoseconds are rounded off to microseconds by flooring.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate static void
timestruc_to_timeval(timestruc_t * ts,struct timeval * tv)3507c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	tv->tv_sec = ts->tv_sec;
3537c478bd9Sstevel@tonic-gate 	tv->tv_usec = ts->tv_nsec / 1000;
3547c478bd9Sstevel@tonic-gate }
355