xref: /illumos-gate/usr/src/cmd/sleep/sleep.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3	*/
27 /*
28 **	sleep -- suspend execution for an interval
29 **
30 **		sleep time
31 */
32 
33 #include	<stdio.h>
34 #include	<signal.h>
35 #include	<locale.h>
36 #include	<unistd.h>
37 #include	<limits.h>
38 #include	<stdlib.h>
39 
40 static void catch_sig(int sig);
41 
42 int
43 main(int argc, char **argv)
44 {
45 	unsigned long n;
46 	unsigned long leftover;
47 	int	c;
48 	char	*s;
49 
50 	n = 0;
51 	(void) setlocale(LC_ALL, "");
52 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
53 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
54 #endif
55 	(void) textdomain(TEXT_DOMAIN);
56 	while ((c = getopt(argc, argv, "")) != -1)
57 		switch (c) {
58 		case '?':
59 		(void) fprintf(stderr, gettext("usage: sleep time\n"));
60 		(void) exit(2);
61 		}
62 	argc -= optind-1;
63 	argv += optind-1;
64 	if (argc < 2) {
65 		(void) fprintf(stderr, gettext("usage: sleep time\n"));
66 		(void) exit(2);
67 	}
68 
69 	/*
70 	* XCU4: utility must terminate with zero exit status upon receiving
71 	* SIGALRM signal
72 	*/
73 
74 	signal(SIGALRM, catch_sig);
75 	s = argv[1];
76 	while (c = *s++) {
77 		if (c < '0' || c > '9') {
78 			(void) fprintf(stderr,
79 				gettext("sleep: bad character in argument\n"));
80 			(void) exit(2);
81 		}
82 		n = n*10 + c - '0';
83 	}
84 
85 	/*
86 	* to fix - sleep fails silently when on "long sleep" BUG: 1164064.
87 	* logic is to repeatedly sleep for unslept remaining time after sleep
88 	* of USHRT_MAX seconds, via reset and repeat call to sleep()
89 	* library routine until there is none remaining time to sleep.
90 	*
91 	* The fix for 1164064 introduced bug 1263997 : This is a fix for
92 	* these problems.
93 	*/
94 
95 	leftover = 0;
96         while (n != 0) {
97                 if (n >= USHRT_MAX) {
98                         leftover = n - USHRT_MAX;
99                         leftover += sleep(USHRT_MAX);
100                 }
101                 else {
102                         leftover = sleep(n);
103                 }
104                 n = leftover;
105         }
106 	return (0);
107 }
108 
109 static void
110 catch_sig(int sig)
111 {
112 }
113