xref: /freebsd/bin/sleep/sleep.c (revision f6e6dc63abcbb1f306c3f8cf0238525b2b399b54)
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>
46ac14c311SRuslan Ermilov #include <limits.h>
4713d8eedaSKonstantin Belousov #include <signal.h>
48bc9bbbe0SKonstantin Belousov #include <stdint.h>
49fcfa4c95SMark Murray #include <stdio.h>
504b88c807SRodney W. Grimes #include <stdlib.h>
51ac14c311SRuslan Ermilov #include <time.h>
524b88c807SRodney W. Grimes 
5313d8eedaSKonstantin Belousov static void usage(void);
5413d8eedaSKonstantin Belousov 
5513d8eedaSKonstantin Belousov static volatile sig_atomic_t report_requested;
5613d8eedaSKonstantin Belousov static void
5713d8eedaSKonstantin Belousov report_request(int signo __unused)
5813d8eedaSKonstantin Belousov {
5913d8eedaSKonstantin Belousov 
6013d8eedaSKonstantin Belousov 	report_requested = 1;
6113d8eedaSKonstantin Belousov }
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes int
645134c3f7SWarner Losh main(int argc, char *argv[])
654b88c807SRodney W. Grimes {
66ac14c311SRuslan Ermilov 	struct timespec time_to_sleep;
67*f6e6dc63SKonstantin Belousov 	double d;
68*f6e6dc63SKonstantin Belousov 	time_t original;
69*f6e6dc63SKonstantin Belousov 	char buf[2];
704b88c807SRodney W. Grimes 
7113d8eedaSKonstantin Belousov 	if (argc != 2)
724b88c807SRodney W. Grimes 		usage();
734b88c807SRodney W. Grimes 
74*f6e6dc63SKonstantin Belousov 	if (sscanf(argv[1], "%lf%1s", &d, buf) != 1)
75e9a4552bSNate Lawson 		usage();
76*f6e6dc63SKonstantin Belousov 	if (d > INT_MAX)
7713d8eedaSKonstantin Belousov 		usage();
78*f6e6dc63SKonstantin Belousov 	if (d <= 0)
79*f6e6dc63SKonstantin Belousov 		return (0);
80*f6e6dc63SKonstantin Belousov 	original = time_to_sleep.tv_sec = (time_t)d;
81*f6e6dc63SKonstantin Belousov 	time_to_sleep.tv_nsec = 1e9 * (d - time_to_sleep.tv_sec);
8213d8eedaSKonstantin Belousov 
8313d8eedaSKonstantin Belousov 	signal(SIGINFO, report_request);
8413d8eedaSKonstantin Belousov 	while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) {
8513d8eedaSKonstantin Belousov 		if (report_requested) {
86*f6e6dc63SKonstantin Belousov 			/* Reporting does not bother with nanoseconds. */
87*f6e6dc63SKonstantin Belousov 			warnx("about %d second(s) left out of the original %d",
88*f6e6dc63SKonstantin Belousov 			    (int)time_to_sleep.tv_sec, (int)original);
8913d8eedaSKonstantin Belousov 			report_requested = 0;
9013d8eedaSKonstantin Belousov 		} else
9113d8eedaSKonstantin Belousov 			break;
9213d8eedaSKonstantin Belousov 	}
93fcfa4c95SMark Murray 	return (0);
944b88c807SRodney W. Grimes }
954b88c807SRodney W. Grimes 
9613d8eedaSKonstantin Belousov static void
975134c3f7SWarner Losh usage(void)
984b88c807SRodney W. Grimes {
994b88c807SRodney W. Grimes 
100*f6e6dc63SKonstantin Belousov 	fprintf(stderr, "usage: sleep seconds\n");
10113d8eedaSKonstantin Belousov 	exit(1);
1024b88c807SRodney W. Grimes }
103