sleep.c revision 51835
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 4436152Scharnierstatic const char rcsid[] = 4550471Speter "$FreeBSD: head/bin/sleep/sleep.c 51835 1999-10-01 07:53:40Z ru $"; 461556Srgrimes#endif /* not lint */ 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 551556Srgrimesvoid usage __P((void)); 561556Srgrimes 571556Srgrimesint 581556Srgrimesmain(argc, argv) 591556Srgrimes int argc; 601556Srgrimes char *argv[]; 611556Srgrimes{ 6251835Sru struct timespec time_to_sleep; 6351835Sru long l; 6451835Sru int ch, neg; 6551835Sru char *p; 661556Srgrimes 6724348Simp while ((ch = getopt(argc, argv, "")) != -1) 681556Srgrimes switch(ch) { 691556Srgrimes case '?': 701556Srgrimes default: 711556Srgrimes usage(); 7251835Sru /* NOTREACHED */ 731556Srgrimes } 741556Srgrimes argc -= optind; 751556Srgrimes argv += optind; 761556Srgrimes 7751835Sru if (argc != 1) { 781556Srgrimes usage(); 7951835Sru /* NOTREACHED */ 8051835Sru } 811556Srgrimes 8251835Sru p = argv[0]; 8351835Sru 8451835Sru /* Skip over leading whitespaces. */ 8551835Sru while (isspace((unsigned char)*p)) 8651835Sru ++p; 8751835Sru 8851835Sru /* Check for optional `+' or `-' sign. */ 8951835Sru neg = 0; 9051835Sru if (*p == '-') { 9151835Sru neg = 1; 9251835Sru ++p; 9351835Sru } 9451835Sru else if (*p == '+') 9551835Sru ++p; 9651835Sru 9751835Sru /* Calculate seconds. */ 9851835Sru if (isdigit((unsigned char)*p)) { 9951835Sru l = strtol(p, &p, 10); 10051835Sru if (l > INT_MAX) { 10151835Sru /* 10251835Sru * Avoid overflow when `seconds' is huge. This assumes 10351835Sru * that the maximum value for a time_t is >= INT_MAX. 10451835Sru */ 10551835Sru l = INT_MAX; 10651835Sru } 10751835Sru } else 10851835Sru l = 0; 10951835Sru time_to_sleep.tv_sec = (time_t)l; 11051835Sru 11151835Sru /* Calculate nanoseconds. */ 11251835Sru time_to_sleep.tv_nsec = 0; 11351835Sru 11451835Sru if (*p == '.') { /* Decimal point. */ 11551835Sru l = 100000000L; 11651835Sru do { 11751835Sru if (isdigit((unsigned char)*++p)) 11851835Sru time_to_sleep.tv_nsec += (*p - '0') * l; 11951835Sru else 12051835Sru break; 12151835Sru } while (l /= 10); 12251835Sru } 12351835Sru 12451835Sru if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) 12551835Sru (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); 12651835Sru 1271556Srgrimes exit(0); 1281556Srgrimes} 1291556Srgrimes 1301556Srgrimesvoid 1311556Srgrimesusage() 1321556Srgrimes{ 1331556Srgrimes 1341556Srgrimes (void)fprintf(stderr, "usage: sleep seconds\n"); 1351556Srgrimes exit(1); 1361556Srgrimes} 137