xref: /freebsd/bin/sleep/sleep.c (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 /*-
2  * Copyright (c) 1988, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <capsicum_helpers.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <math.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <time.h>
39 #include <unistd.h>
40 
41 static volatile sig_atomic_t report_requested;
42 
43 static void
44 report_request(int signo __unused)
45 {
46 	report_requested = 1;
47 }
48 
49 static void __dead2
50 usage(void)
51 {
52 	fprintf(stderr, "usage: sleep number[unit] [...]\n"
53 	    "Unit can be 's' (seconds, the default), "
54 	    "m (minutes), h (hours), or d (days).\n");
55 	exit(1);
56 }
57 
58 static double
59 parse_interval(const char *arg)
60 {
61 	double num;
62 	char unit, extra;
63 
64 	switch (sscanf(arg, "%lf%c%c", &num, &unit, &extra)) {
65 	case 2:
66 		switch (unit) {
67 		case 'd':
68 			num *= 24;
69 			/* FALLTHROUGH */
70 		case 'h':
71 			num *= 60;
72 			/* FALLTHROUGH */
73 		case 'm':
74 			num *= 60;
75 			/* FALLTHROUGH */
76 		case 's':
77 			if (!isnan(num))
78 				return (num);
79 		}
80 		break;
81 	case 1:
82 		if (!isnan(num))
83 			return (num);
84 	}
85 	warnx("invalid time interval: %s", arg);
86 	return (INFINITY);
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	struct timespec time_to_sleep;
93 	double seconds;
94 	time_t original;
95 
96 	if (caph_limit_stdio() < 0 || caph_enter() < 0)
97 		err(1, "capsicum");
98 
99 	while (getopt(argc, argv, "") != -1)
100 		usage();
101 	argc -= optind;
102 	argv += optind;
103 	if (argc < 1)
104 		usage();
105 
106 	seconds = 0;
107 	while (argc--)
108 		seconds += parse_interval(*argv++);
109 	if (seconds > INT_MAX)
110 		usage();
111 	if (seconds < 1e-9)
112 		exit(0);
113 	original = time_to_sleep.tv_sec = (time_t)seconds;
114 	time_to_sleep.tv_nsec = 1e9 * (seconds - time_to_sleep.tv_sec);
115 
116 	signal(SIGINFO, report_request);
117 
118 	while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) {
119 		if (errno != EINTR)
120 			err(1, "nanosleep");
121 		if (report_requested) {
122 			/* Reporting does not bother with nanoseconds. */
123 			warnx("about %ld second(s) left out of the original %ld",
124 			    (long)time_to_sleep.tv_sec, (long)original);
125 			report_requested = 0;
126 		}
127 	}
128 
129 	exit(0);
130 }
131