sleep.c revision 139969
1139969Simp/*- 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 * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 30114433Sobrien#if 0 311556Srgrimes#ifndef lint 3220423Sstevestatic char const copyright[] = 331556Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 341556Srgrimes The Regents of the University of California. All rights reserved.\n"; 351556Srgrimes#endif /* not lint */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836152Scharnierstatic char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; 39114433Sobrien#endif /* not lint */ 4036152Scharnier#endif 4199110Sobrien#include <sys/cdefs.h> 4299110Sobrien__FBSDID("$FreeBSD: head/bin/sleep/sleep.c 139969 2005-01-10 08:39:26Z imp $"); 431556Srgrimes 4451835Sru#include <ctype.h> 4551835Sru#include <limits.h> 46114575Smarkm#include <stdio.h> 471556Srgrimes#include <stdlib.h> 4851835Sru#include <time.h> 491556Srgrimes#include <unistd.h> 501556Srgrimes 5190111Simpvoid usage(void); 521556Srgrimes 531556Srgrimesint 5490111Simpmain(int argc, char *argv[]) 551556Srgrimes{ 5651835Sru struct timespec time_to_sleep; 5751835Sru long l; 58106883Snjl int neg; 5951835Sru char *p; 601556Srgrimes 61106883Snjl if (argc != 2) { 621556Srgrimes usage(); 63114575Smarkm return(1); 6451835Sru } 651556Srgrimes 66106883Snjl p = argv[1]; 6751835Sru 6851835Sru /* Skip over leading whitespaces. */ 69106882Snjl while (isspace((unsigned char)*p)) 7051835Sru ++p; 7151835Sru 72106882Snjl /* Check for optional `+' or `-' sign. */ 73106882Snjl neg = 0; 74106882Snjl if (*p == '-') { 75106882Snjl neg = 1; 76106882Snjl ++p; 77114575Smarkm if (!isdigit((unsigned char)*p) && *p != '.') { 78106883Snjl usage(); 79114575Smarkm return(1); 80114575Smarkm } 8151835Sru } 82106882Snjl else if (*p == '+') 83106882Snjl ++p; 8451835Sru 8551835Sru /* Calculate seconds. */ 86106882Snjl if (isdigit((unsigned char)*p)) { 87106882Snjl l = strtol(p, &p, 10); 88106882Snjl if (l > INT_MAX) { 8951835Sru /* 9051835Sru * Avoid overflow when `seconds' is huge. This assumes 9151835Sru * that the maximum value for a time_t is >= INT_MAX. 9251835Sru */ 9351835Sru l = INT_MAX; 9451835Sru } 95106882Snjl } else 96106882Snjl l = 0; 9751835Sru time_to_sleep.tv_sec = (time_t)l; 9851835Sru 9951835Sru /* Calculate nanoseconds. */ 10051835Sru time_to_sleep.tv_nsec = 0; 10151835Sru 10251835Sru if (*p == '.') { /* Decimal point. */ 10351835Sru l = 100000000L; 10451835Sru do { 105106882Snjl if (isdigit((unsigned char)*++p)) 10651835Sru time_to_sleep.tv_nsec += (*p - '0') * l; 10751835Sru else 10851835Sru break; 109114575Smarkm l /= 10; 110114575Smarkm } while (l); 11151835Sru } 11251835Sru 113106882Snjl if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) 11451835Sru (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); 11551835Sru 116114575Smarkm return(0); 1171556Srgrimes} 1181556Srgrimes 1191556Srgrimesvoid 12090111Simpusage(void) 1211556Srgrimes{ 122114575Smarkm const char msg[] = "usage: sleep seconds\n"; 1231556Srgrimes 124114575Smarkm write(STDERR_FILENO, msg, sizeof(msg) - 1); 1251556Srgrimes} 126