xref: /openbsd-src/bin/sleep/sleep.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: sleep.c,v 1.24 2015/10/11 20:17:49 guenther Exp $	*/
2 /*	$NetBSD: sleep.c,v 1.8 1995/03/21 09:11:11 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <ctype.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <time.h>
39 #include <unistd.h>
40 #include <err.h>
41 
42 extern char *__progname;
43 
44 void usage(void);
45 void alarmh(int);
46 
47 int
48 main(int argc, char *argv[])
49 {
50 	int ch;
51 	time_t secs = 0, t;
52 	char *cp;
53 	long nsecs = 0;
54 	struct timespec rqtp;
55 	int i;
56 
57 	if (pledge("stdio", NULL) == -1)
58 		err(1, "pledge");
59 
60 	signal(SIGALRM, alarmh);
61 
62 	while ((ch = getopt(argc, argv, "")) != -1)
63 		switch(ch) {
64 		default:
65 			usage();
66 		}
67 	argc -= optind;
68 	argv += optind;
69 
70 	if (argc != 1)
71 		usage();
72 
73 	cp = *argv;
74 	while ((*cp != '\0') && (*cp != '.')) {
75 		if (!isdigit((unsigned char)*cp))
76 			usage();
77 		t = (secs * 10) + (*cp++ - '0');
78 		if (t / 10 != secs)	/* oflow */
79 			return (EINVAL);
80 		secs = t;
81 	}
82 
83 	/* Handle fractions of a second */
84 	if (*cp == '.') {
85 		cp++;
86 		for (i = 100000000; i > 0; i /= 10) {
87 			if (*cp == '\0')
88 				break;
89 			if (!isdigit((unsigned char)*cp))
90 				usage();
91 			nsecs += (*cp++ - '0') * i;
92 		}
93 
94 		/*
95 		 * We parse all the way down to nanoseconds
96 		 * in the above for loop. Be pedantic about
97 		 * checking the rest of the argument.
98 		 */
99 		while (*cp != '\0') {
100 			if (!isdigit((unsigned char)*cp++))
101 				usage();
102 		}
103 	}
104 
105 	rqtp.tv_sec = secs;
106 	rqtp.tv_nsec = nsecs;
107 
108 	if ((secs > 0) || (nsecs > 0))
109 		if (nanosleep(&rqtp, NULL))
110 			err(1, NULL);
111 	return (0);
112 }
113 
114 void
115 usage(void)
116 {
117 	(void)fprintf(stderr, "usage: %s seconds\n", __progname);
118 	exit(1);
119 }
120 
121 /*
122  * POSIX 1003.2 says sleep should exit with 0 return code on reception
123  * of SIGALRM.
124  */
125 /* ARGSUSED */
126 void
127 alarmh(int signo)
128 {
129 	/*
130 	 * exit() flushes stdio buffers, which is not legal in a signal
131 	 * handler. Use _exit().
132 	 */
133 	_exit(0);
134 }
135