sleep.c revision 99110
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1988, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
3520423Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4136152Scharnier#if 0
4236152Scharnierstatic char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
4336152Scharnier#endif
441556Srgrimes#endif /* not lint */
4599110Sobrien#include <sys/cdefs.h>
4699110Sobrien__FBSDID("$FreeBSD: head/bin/sleep/sleep.c 99110 2002-06-30 05:15:05Z obrien $");
471556Srgrimes
4851835Sru#include <ctype.h>
4951835Sru#include <limits.h>
501556Srgrimes#include <stdio.h>
511556Srgrimes#include <stdlib.h>
5251835Sru#include <time.h>
531556Srgrimes#include <unistd.h>
541556Srgrimes
5590111Simpvoid usage(void);
561556Srgrimes
571556Srgrimesint
5890111Simpmain(int argc, char *argv[])
591556Srgrimes{
6051835Sru	struct timespec time_to_sleep;
6151835Sru	long l;
6251835Sru	int ch, neg;
6351835Sru	char *p;
641556Srgrimes
6524348Simp	while ((ch = getopt(argc, argv, "")) != -1)
661556Srgrimes		switch(ch) {
671556Srgrimes		case '?':
681556Srgrimes		default:
691556Srgrimes			usage();
7051835Sru			/* NOTREACHED */
711556Srgrimes		}
721556Srgrimes	argc -= optind;
731556Srgrimes	argv += optind;
741556Srgrimes
7551835Sru	if (argc != 1) {
761556Srgrimes		usage();
7751835Sru		/* NOTREACHED */
7851835Sru	}
791556Srgrimes
8051835Sru	p = argv[0];
8151835Sru
8251835Sru	/* Skip over leading whitespaces. */
8351835Sru	while (isspace((unsigned char)*p))
8451835Sru		++p;
8551835Sru
8651835Sru	/* Check for optional `+' or `-' sign. */
8751835Sru	neg = 0;
8851835Sru	if (*p == '-') {
8951835Sru		neg = 1;
9051835Sru		++p;
9151835Sru	}
9251835Sru	else if (*p == '+')
9351835Sru		++p;
9451835Sru
9551835Sru	/* Calculate seconds. */
9651835Sru	if (isdigit((unsigned char)*p)) {
9751835Sru		l = strtol(p, &p, 10);
9851835Sru		if (l > INT_MAX) {
9951835Sru			/*
10051835Sru			 * Avoid overflow when `seconds' is huge.  This assumes
10151835Sru			 * that the maximum value for a time_t is >= INT_MAX.
10251835Sru			 */
10351835Sru			l = INT_MAX;
10451835Sru		}
10551835Sru	} else
10651835Sru		l = 0;
10751835Sru	time_to_sleep.tv_sec = (time_t)l;
10851835Sru
10951835Sru	/* Calculate nanoseconds. */
11051835Sru	time_to_sleep.tv_nsec = 0;
11151835Sru
11251835Sru	if (*p == '.') {		/* Decimal point. */
11351835Sru		l = 100000000L;
11451835Sru		do {
11551835Sru			if (isdigit((unsigned char)*++p))
11651835Sru				time_to_sleep.tv_nsec += (*p - '0') * l;
11751835Sru			else
11851835Sru				break;
11951835Sru		} while (l /= 10);
12051835Sru	}
12151835Sru
12251835Sru	if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0))
12351835Sru		(void)nanosleep(&time_to_sleep, (struct timespec *)NULL);
12451835Sru
1251556Srgrimes	exit(0);
1261556Srgrimes}
1271556Srgrimes
1281556Srgrimesvoid
12990111Simpusage(void)
1301556Srgrimes{
1311556Srgrimes
1321556Srgrimes	(void)fprintf(stderr, "usage: sleep seconds\n");
1331556Srgrimes	exit(1);
1341556Srgrimes}
135