19ddb49cbSWarner Losh /*- 24b88c807SRodney W. Grimes * Copyright (c) 1988, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 64b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 74b88c807SRodney W. Grimes * are met: 84b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 94b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 104b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 124b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 134b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 144b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 154b88c807SRodney W. Grimes * without specific prior written permission. 164b88c807SRodney W. Grimes * 174b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 184b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 214b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274b88c807SRodney W. Grimes * SUCH DAMAGE. 284b88c807SRodney W. Grimes */ 294b88c807SRodney W. Grimes 3009a80d48SDavid E. O'Brien #if 0 314b88c807SRodney W. Grimes #ifndef lint 32db5b8cafSSteve Price static char const copyright[] = 334b88c807SRodney W. Grimes "@(#) Copyright (c) 1988, 1993, 1994\n\ 344b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 354b88c807SRodney W. Grimes #endif /* not lint */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 389ba8bd65SPhilippe Charnier static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; 394b88c807SRodney W. Grimes #endif /* not lint */ 4009a80d48SDavid E. O'Brien #endif 412749b141SDavid E. O'Brien #include <sys/cdefs.h> 422749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 434b88c807SRodney W. Grimes 44ac14c311SRuslan Ermilov #include <ctype.h> 4513d8eedaSKonstantin Belousov #include <err.h> 46c2cfcb60SJilles Tjoelker #include <errno.h> 47ac14c311SRuslan Ermilov #include <limits.h> 4813d8eedaSKonstantin Belousov #include <signal.h> 49bc9bbbe0SKonstantin Belousov #include <stdint.h> 50fcfa4c95SMark Murray #include <stdio.h> 514b88c807SRodney W. Grimes #include <stdlib.h> 52ac14c311SRuslan Ermilov #include <time.h> 534b88c807SRodney W. Grimes 5413d8eedaSKonstantin Belousov static void usage(void); 5513d8eedaSKonstantin Belousov 5613d8eedaSKonstantin Belousov static volatile sig_atomic_t report_requested; 5713d8eedaSKonstantin Belousov static void 5813d8eedaSKonstantin Belousov report_request(int signo __unused) 5913d8eedaSKonstantin Belousov { 6013d8eedaSKonstantin Belousov 6113d8eedaSKonstantin Belousov report_requested = 1; 6213d8eedaSKonstantin Belousov } 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes int 655134c3f7SWarner Losh main(int argc, char *argv[]) 664b88c807SRodney W. Grimes { 67ac14c311SRuslan Ermilov struct timespec time_to_sleep; 68f6e6dc63SKonstantin Belousov double d; 69f6e6dc63SKonstantin Belousov time_t original; 70f6e6dc63SKonstantin Belousov char buf[2]; 714b88c807SRodney W. Grimes 7213d8eedaSKonstantin Belousov if (argc != 2) 734b88c807SRodney W. Grimes usage(); 744b88c807SRodney W. Grimes 75f6e6dc63SKonstantin Belousov if (sscanf(argv[1], "%lf%1s", &d, buf) != 1) 76e9a4552bSNate Lawson usage(); 77f6e6dc63SKonstantin Belousov if (d > INT_MAX) 7813d8eedaSKonstantin Belousov usage(); 79f6e6dc63SKonstantin Belousov if (d <= 0) 80f6e6dc63SKonstantin Belousov return (0); 81f6e6dc63SKonstantin Belousov original = time_to_sleep.tv_sec = (time_t)d; 82f6e6dc63SKonstantin Belousov time_to_sleep.tv_nsec = 1e9 * (d - time_to_sleep.tv_sec); 8313d8eedaSKonstantin Belousov 8413d8eedaSKonstantin Belousov signal(SIGINFO, report_request); 85*c6ef00e3SJilles Tjoelker 86*c6ef00e3SJilles Tjoelker /* 87*c6ef00e3SJilles Tjoelker * Note: [EINTR] is supposed to happen only when a signal was handled 88*c6ef00e3SJilles Tjoelker * but the kernel also returns it when a ptrace-based debugger 89*c6ef00e3SJilles Tjoelker * attaches. This is a bug but it is hard to fix. 90*c6ef00e3SJilles Tjoelker */ 9113d8eedaSKonstantin Belousov while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) { 9213d8eedaSKonstantin Belousov if (report_requested) { 93f6e6dc63SKonstantin Belousov /* Reporting does not bother with nanoseconds. */ 94f6e6dc63SKonstantin Belousov warnx("about %d second(s) left out of the original %d", 95f6e6dc63SKonstantin Belousov (int)time_to_sleep.tv_sec, (int)original); 9613d8eedaSKonstantin Belousov report_requested = 0; 97c2cfcb60SJilles Tjoelker } else if (errno != EINTR) 98c2cfcb60SJilles Tjoelker err(1, "nanosleep"); 9913d8eedaSKonstantin Belousov } 100fcfa4c95SMark Murray return (0); 1014b88c807SRodney W. Grimes } 1024b88c807SRodney W. Grimes 10313d8eedaSKonstantin Belousov static void 1045134c3f7SWarner Losh usage(void) 1054b88c807SRodney W. Grimes { 1064b88c807SRodney W. Grimes 107f6e6dc63SKonstantin Belousov fprintf(stderr, "usage: sleep seconds\n"); 10813d8eedaSKonstantin Belousov exit(1); 1094b88c807SRodney W. Grimes } 110