/*-1* Copyright (c) 1988, 1993, 19942* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <capsicum_helpers.h>30#include <err.h>31#include <errno.h>32#include <limits.h>33#include <math.h>34#include <signal.h>35#include <stdio.h>36#include <stdlib.h>37#include <time.h>38#include <unistd.h>3940static volatile sig_atomic_t report_requested;4142static void43report_request(int signo __unused)44{45report_requested = 1;46}4748static void __dead249usage(void)50{51fprintf(stderr, "usage: sleep number[unit] [...]\n"52"Unit can be 's' (seconds, the default), "53"m (minutes), h (hours), or d (days).\n");54exit(1);55}5657static double58parse_interval(const char *arg)59{60double num;61char unit, extra;6263switch (sscanf(arg, "%lf%c%c", &num, &unit, &extra)) {64case 2:65switch (unit) {66case 'd':67num *= 24;68/* FALLTHROUGH */69case 'h':70num *= 60;71/* FALLTHROUGH */72case 'm':73num *= 60;74/* FALLTHROUGH */75case 's':76if (!isnan(num))77return (num);78}79break;80case 1:81if (!isnan(num))82return (num);83}84warnx("invalid time interval: %s", arg);85return (INFINITY);86}8788int89main(int argc, char *argv[])90{91struct timespec time_to_sleep;92double seconds;93time_t original;9495if (caph_limit_stdio() < 0 || caph_enter() < 0)96err(1, "capsicum");9798while (getopt(argc, argv, "") != -1)99usage();100argc -= optind;101argv += optind;102if (argc < 1)103usage();104105seconds = 0;106while (argc--)107seconds += parse_interval(*argv++);108if (seconds > INT_MAX)109usage();110if (seconds < 1e-9)111exit(0);112original = time_to_sleep.tv_sec = (time_t)seconds;113time_to_sleep.tv_nsec = 1e9 * (seconds - time_to_sleep.tv_sec);114115signal(SIGINFO, report_request);116117while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) {118if (errno != EINTR)119err(1, "nanosleep");120if (report_requested) {121/* Reporting does not bother with nanoseconds. */122warnx("about %ld second(s) left out of the original %ld",123(long)time_to_sleep.tv_sec, (long)original);124report_requested = 0;125}126}127128exit(0);129}130131132