sleep.c revision 210679
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 210679 2010-07-31 14:30:11Z kib $"); 431556Srgrimes 4451835Sru#include <ctype.h> 45210679Skib#include <err.h> 4651835Sru#include <limits.h> 47210679Skib#include <signal.h> 48114575Smarkm#include <stdio.h> 491556Srgrimes#include <stdlib.h> 5051835Sru#include <time.h> 511556Srgrimes#include <unistd.h> 521556Srgrimes 53210679Skibstatic void usage(void); 541556Srgrimes 55210679Skibstatic volatile sig_atomic_t report_requested; 56210679Skibstatic void 57210679Skibreport_request(int signo __unused) 58210679Skib{ 59210679Skib 60210679Skib report_requested = 1; 61210679Skib} 62210679Skib 631556Srgrimesint 6490111Simpmain(int argc, char *argv[]) 651556Srgrimes{ 6651835Sru struct timespec time_to_sleep; 67210679Skib long l, original; 68106883Snjl int neg; 6951835Sru char *p; 701556Srgrimes 71210679Skib if (argc != 2) 721556Srgrimes usage(); 731556Srgrimes 74106883Snjl p = argv[1]; 7551835Sru 7651835Sru /* Skip over leading whitespaces. */ 77106882Snjl while (isspace((unsigned char)*p)) 7851835Sru ++p; 7951835Sru 80106882Snjl /* Check for optional `+' or `-' sign. */ 81106882Snjl neg = 0; 82106882Snjl if (*p == '-') { 83106882Snjl neg = 1; 84106882Snjl ++p; 85210679Skib if (!isdigit((unsigned char)*p) && *p != '.') 86106883Snjl usage(); 8751835Sru } 88106882Snjl else if (*p == '+') 89106882Snjl ++p; 9051835Sru 9151835Sru /* Calculate seconds. */ 92106882Snjl if (isdigit((unsigned char)*p)) { 93106882Snjl l = strtol(p, &p, 10); 94210679Skib 95210679Skib /* 96210679Skib * Avoid overflow when `seconds' is huge. This assumes 97210679Skib * that the maximum value for a time_t is <= INT_MAX. 98210679Skib */ 99210679Skib if (l > INT_MAX) 10051835Sru l = INT_MAX; 101106882Snjl } else 102106882Snjl l = 0; 10351835Sru time_to_sleep.tv_sec = (time_t)l; 10451835Sru 10551835Sru /* Calculate nanoseconds. */ 10651835Sru time_to_sleep.tv_nsec = 0; 10751835Sru 10851835Sru if (*p == '.') { /* Decimal point. */ 10951835Sru l = 100000000L; 11051835Sru do { 111106882Snjl if (isdigit((unsigned char)*++p)) 11251835Sru time_to_sleep.tv_nsec += (*p - '0') * l; 11351835Sru else 11451835Sru break; 115114575Smarkm l /= 10; 116114575Smarkm } while (l); 11751835Sru } 11851835Sru 119210679Skib /* Skip over the trailing whitespace. */ 120210679Skib while (isspace((unsigned char)*p)) 121210679Skib ++p; 122210679Skib if (*p != '\0') 123210679Skib usage(); 12451835Sru 125210679Skib signal(SIGINFO, report_request); 126210679Skib if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) { 127210679Skib original = time_to_sleep.tv_sec; 128210679Skib while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) { 129210679Skib if (report_requested) { 130210679Skib /* 131210679Skib * Reporting does not bother with 132210679Skib * fractions of a second... 133210679Skib */ 134210679Skib warnx("about %ld second(s) left" 135210679Skib " out of the original %ld", 136210679Skib time_to_sleep.tv_sec, original); 137210679Skib report_requested = 0; 138210679Skib } else 139210679Skib break; 140210679Skib } 141210679Skib } 142210679Skib 143210679Skib return (0); 1441556Srgrimes} 1451556Srgrimes 146210679Skibstatic void 14790111Simpusage(void) 1481556Srgrimes{ 149210679Skib static const char msg[] = "usage: sleep seconds\n"; 1501556Srgrimes 151114575Smarkm write(STDERR_FILENO, msg, sizeof(msg) - 1); 152210679Skib exit(1); 1531556Srgrimes} 154