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$"); 431556Srgrimes 4451835Sru#include <ctype.h> 45210679Skib#include <err.h> 46251078Sjilles#include <errno.h> 4751835Sru#include <limits.h> 48210679Skib#include <signal.h> 49210696Skib#include <stdint.h> 50114575Smarkm#include <stdio.h> 511556Srgrimes#include <stdlib.h> 5251835Sru#include <time.h> 531556Srgrimes 54210679Skibstatic void usage(void); 551556Srgrimes 56210679Skibstatic volatile sig_atomic_t report_requested; 57210679Skibstatic void 58210679Skibreport_request(int signo __unused) 59210679Skib{ 60210679Skib 61210679Skib report_requested = 1; 62210679Skib} 63210679Skib 641556Srgrimesint 6590111Simpmain(int argc, char *argv[]) 661556Srgrimes{ 6751835Sru struct timespec time_to_sleep; 68210749Skib double d; 69210749Skib time_t original; 70210749Skib char buf[2]; 711556Srgrimes 72210679Skib if (argc != 2) 731556Srgrimes usage(); 741556Srgrimes 75210749Skib if (sscanf(argv[1], "%lf%1s", &d, buf) != 1) 76210679Skib usage(); 77210749Skib if (d > INT_MAX) 78210749Skib usage(); 79210749Skib if (d <= 0) 80210749Skib return (0); 81210749Skib original = time_to_sleep.tv_sec = (time_t)d; 82210749Skib time_to_sleep.tv_nsec = 1e9 * (d - time_to_sleep.tv_sec); 8351835Sru 84210679Skib signal(SIGINFO, report_request); 85251433Sjilles 86251433Sjilles /* 87251433Sjilles * Note: [EINTR] is supposed to happen only when a signal was handled 88251433Sjilles * but the kernel also returns it when a ptrace-based debugger 89251433Sjilles * attaches. This is a bug but it is hard to fix. 90251433Sjilles */ 91210749Skib while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) { 92210749Skib if (report_requested) { 93210749Skib /* Reporting does not bother with nanoseconds. */ 94210749Skib warnx("about %d second(s) left out of the original %d", 95210749Skib (int)time_to_sleep.tv_sec, (int)original); 96210749Skib report_requested = 0; 97251078Sjilles } else if (errno != EINTR) 98251078Sjilles err(1, "nanosleep"); 99210679Skib } 100210679Skib return (0); 1011556Srgrimes} 1021556Srgrimes 103210679Skibstatic void 10490111Simpusage(void) 1051556Srgrimes{ 1061556Srgrimes 107210749Skib fprintf(stderr, "usage: sleep seconds\n"); 108210679Skib exit(1); 1091556Srgrimes} 110