xref: /freebsd/bin/sleep/sleep.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)sleep.c	8.3 (Berkeley) 4/2/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <ctype.h>
49 #include <limits.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 void usage __P((void));
56 
57 int
58 main(argc, argv)
59 	int argc;
60 	char *argv[];
61 {
62 	struct timespec time_to_sleep;
63 	long l;
64 	int ch, neg;
65 	char *p;
66 
67 	while ((ch = getopt(argc, argv, "")) != -1)
68 		switch(ch) {
69 		case '?':
70 		default:
71 			usage();
72 			/* NOTREACHED */
73 		}
74 	argc -= optind;
75 	argv += optind;
76 
77 	if (argc != 1) {
78 		usage();
79 		/* NOTREACHED */
80 	}
81 
82 	p = argv[0];
83 
84 	/* Skip over leading whitespaces. */
85 	while (isspace((unsigned char)*p))
86 		++p;
87 
88 	/* Check for optional `+' or `-' sign. */
89 	neg = 0;
90 	if (*p == '-') {
91 		neg = 1;
92 		++p;
93 	}
94 	else if (*p == '+')
95 		++p;
96 
97 	/* Calculate seconds. */
98 	if (isdigit((unsigned char)*p)) {
99 		l = strtol(p, &p, 10);
100 		if (l > INT_MAX) {
101 			/*
102 			 * Avoid overflow when `seconds' is huge.  This assumes
103 			 * that the maximum value for a time_t is >= INT_MAX.
104 			 */
105 			l = INT_MAX;
106 		}
107 	} else
108 		l = 0;
109 	time_to_sleep.tv_sec = (time_t)l;
110 
111 	/* Calculate nanoseconds. */
112 	time_to_sleep.tv_nsec = 0;
113 
114 	if (*p == '.') {		/* Decimal point. */
115 		l = 100000000L;
116 		do {
117 			if (isdigit((unsigned char)*++p))
118 				time_to_sleep.tv_nsec += (*p - '0') * l;
119 			else
120 				break;
121 		} while (l /= 10);
122 	}
123 
124 	if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0))
125 		(void)nanosleep(&time_to_sleep, (struct timespec *)NULL);
126 
127 	exit(0);
128 }
129 
130 void
131 usage()
132 {
133 
134 	(void)fprintf(stderr, "usage: sleep seconds\n");
135 	exit(1);
136 }
137