1 /* $OpenBSD: sleep.c,v 1.18 2009/02/13 20:39:58 deraadt 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 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1988, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; 42 #else 43 static char rcsid[] = "$OpenBSD: sleep.c,v 1.18 2009/02/13 20:39:58 deraadt Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 #include <ctype.h> 48 #include <errno.h> 49 #include <locale.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <time.h> 54 #include <unistd.h> 55 56 extern char *__progname; 57 58 void usage(void); 59 void alarmh(int); 60 61 int 62 main(int argc, char *argv[]) 63 { 64 int ch; 65 time_t secs = 0, t; 66 char *cp; 67 long nsecs = 0; 68 struct timespec rqtp; 69 int i; 70 71 setlocale(LC_ALL, ""); 72 73 signal(SIGALRM, alarmh); 74 75 while ((ch = getopt(argc, argv, "")) != -1) 76 switch(ch) { 77 default: 78 usage(); 79 } 80 argc -= optind; 81 argv += optind; 82 83 if (argc != 1) 84 usage(); 85 86 cp = *argv; 87 while ((*cp != '\0') && (*cp != '.')) { 88 if (!isdigit(*cp)) 89 usage(); 90 t = (secs * 10) + (*cp++ - '0'); 91 if (t / 10 != secs) /* oflow */ 92 return (EINVAL); 93 secs = t; 94 } 95 96 /* Handle fractions of a second */ 97 if (*cp == '.') { 98 cp++; 99 for (i = 100000000; i > 0; i /= 10) { 100 if (*cp == '\0') 101 break; 102 if (!isdigit(*cp)) 103 usage(); 104 nsecs += (*cp++ - '0') * i; 105 } 106 107 /* 108 * We parse all the way down to nanoseconds 109 * in the above for loop. Be pedantic about 110 * checking the rest of the argument. 111 */ 112 while (*cp != '\0') { 113 if (!isdigit(*cp++)) 114 usage(); 115 } 116 } 117 118 rqtp.tv_sec = secs; 119 rqtp.tv_nsec = nsecs; 120 121 if ((secs > 0) || (nsecs > 0)) 122 if (nanosleep(&rqtp, NULL)) 123 return (errno); 124 return (0); 125 } 126 127 void 128 usage(void) 129 { 130 (void)fprintf(stderr, "usage: %s seconds\n", __progname); 131 exit(1); 132 } 133 134 /* 135 * POSIX 1003.2 says sleep should exit with 0 return code on reception 136 * of SIGALRM. 137 */ 138 /* ARGSUSED */ 139 void 140 alarmh(int signo) 141 { 142 /* 143 * exit() flushes stdio buffers, which is not legal in a signal 144 * handler. Use _exit(). 145 */ 146 _exit(0); 147 } 148