sleep.c revision 114575
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 34114433Sobrien#if 0 351556Srgrimes#ifndef lint 3620423Sstevestatic char const copyright[] = 371556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 381556Srgrimes The Regents of the University of California. All rights reserved.\n"; 391556Srgrimes#endif /* not lint */ 401556Srgrimes 411556Srgrimes#ifndef lint 4236152Scharnierstatic char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; 43114433Sobrien#endif /* not lint */ 4436152Scharnier#endif 4599110Sobrien#include <sys/cdefs.h> 4699110Sobrien__FBSDID("$FreeBSD: head/bin/sleep/sleep.c 114575 2003-05-03 10:14:26Z markm $"); 471556Srgrimes 4851835Sru#include <ctype.h> 4951835Sru#include <limits.h> 50114575Smarkm#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; 62106883Snjl int neg; 6351835Sru char *p; 641556Srgrimes 65106883Snjl if (argc != 2) { 661556Srgrimes usage(); 67114575Smarkm return(1); 6851835Sru } 691556Srgrimes 70106883Snjl p = argv[1]; 7151835Sru 7251835Sru /* Skip over leading whitespaces. */ 73106882Snjl while (isspace((unsigned char)*p)) 7451835Sru ++p; 7551835Sru 76106882Snjl /* Check for optional `+' or `-' sign. */ 77106882Snjl neg = 0; 78106882Snjl if (*p == '-') { 79106882Snjl neg = 1; 80106882Snjl ++p; 81114575Smarkm if (!isdigit((unsigned char)*p) && *p != '.') { 82106883Snjl usage(); 83114575Smarkm return(1); 84114575Smarkm } 8551835Sru } 86106882Snjl else if (*p == '+') 87106882Snjl ++p; 8851835Sru 8951835Sru /* Calculate seconds. */ 90106882Snjl if (isdigit((unsigned char)*p)) { 91106882Snjl l = strtol(p, &p, 10); 92106882Snjl if (l > INT_MAX) { 9351835Sru /* 9451835Sru * Avoid overflow when `seconds' is huge. This assumes 9551835Sru * that the maximum value for a time_t is >= INT_MAX. 9651835Sru */ 9751835Sru l = INT_MAX; 9851835Sru } 99106882Snjl } else 100106882Snjl l = 0; 10151835Sru time_to_sleep.tv_sec = (time_t)l; 10251835Sru 10351835Sru /* Calculate nanoseconds. */ 10451835Sru time_to_sleep.tv_nsec = 0; 10551835Sru 10651835Sru if (*p == '.') { /* Decimal point. */ 10751835Sru l = 100000000L; 10851835Sru do { 109106882Snjl if (isdigit((unsigned char)*++p)) 11051835Sru time_to_sleep.tv_nsec += (*p - '0') * l; 11151835Sru else 11251835Sru break; 113114575Smarkm l /= 10; 114114575Smarkm } while (l); 11551835Sru } 11651835Sru 117106882Snjl if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) 11851835Sru (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); 11951835Sru 120114575Smarkm return(0); 1211556Srgrimes} 1221556Srgrimes 1231556Srgrimesvoid 12490111Simpusage(void) 1251556Srgrimes{ 126114575Smarkm const char msg[] = "usage: sleep seconds\n"; 1271556Srgrimes 128114575Smarkm write(STDERR_FILENO, msg, sizeof(msg) - 1); 1291556Srgrimes} 130