1 /* $OpenBSD: sleep.c,v 1.19 2009/10/27 23:59:22 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 #include <ctype.h> 34 #include <errno.h> 35 #include <locale.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <time.h> 40 #include <unistd.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 setlocale(LC_ALL, ""); 58 59 signal(SIGALRM, alarmh); 60 61 while ((ch = getopt(argc, argv, "")) != -1) 62 switch(ch) { 63 default: 64 usage(); 65 } 66 argc -= optind; 67 argv += optind; 68 69 if (argc != 1) 70 usage(); 71 72 cp = *argv; 73 while ((*cp != '\0') && (*cp != '.')) { 74 if (!isdigit(*cp)) 75 usage(); 76 t = (secs * 10) + (*cp++ - '0'); 77 if (t / 10 != secs) /* oflow */ 78 return (EINVAL); 79 secs = t; 80 } 81 82 /* Handle fractions of a second */ 83 if (*cp == '.') { 84 cp++; 85 for (i = 100000000; i > 0; i /= 10) { 86 if (*cp == '\0') 87 break; 88 if (!isdigit(*cp)) 89 usage(); 90 nsecs += (*cp++ - '0') * i; 91 } 92 93 /* 94 * We parse all the way down to nanoseconds 95 * in the above for loop. Be pedantic about 96 * checking the rest of the argument. 97 */ 98 while (*cp != '\0') { 99 if (!isdigit(*cp++)) 100 usage(); 101 } 102 } 103 104 rqtp.tv_sec = secs; 105 rqtp.tv_nsec = nsecs; 106 107 if ((secs > 0) || (nsecs > 0)) 108 if (nanosleep(&rqtp, NULL)) 109 return (errno); 110 return (0); 111 } 112 113 void 114 usage(void) 115 { 116 (void)fprintf(stderr, "usage: %s seconds\n", __progname); 117 exit(1); 118 } 119 120 /* 121 * POSIX 1003.2 says sleep should exit with 0 return code on reception 122 * of SIGALRM. 123 */ 124 /* ARGSUSED */ 125 void 126 alarmh(int signo) 127 { 128 /* 129 * exit() flushes stdio buffers, which is not legal in a signal 130 * handler. Use _exit(). 131 */ 132 _exit(0); 133 } 134