From 83a802c8d5b78c6a50afc57d91fcf0e291359906 Mon Sep 17 00:00:00 2001 From: denny Date: Sun, 29 Jun 1997 07:33:09 +0000 Subject: Make sleep handle fractions of a second. Why not? --- bin/sleep/sleep.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'bin/sleep/sleep.c') diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c index 7f1a9eeab99..2c5ef0ed105 100644 --- a/bin/sleep/sleep.c +++ b/bin/sleep/sleep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sleep.c,v 1.4 1997/01/15 23:40:28 millert Exp $ */ +/* $OpenBSD: sleep.c,v 1.5 1997/06/29 07:33:08 denny Exp $ */ /* $NetBSD: sleep.c,v 1.8 1995/03/21 09:11:11 cgd Exp $ */ /* @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; #else -static char rcsid[] = "$OpenBSD: sleep.c,v 1.4 1997/01/15 23:40:28 millert Exp $"; +static char rcsid[] = "$OpenBSD: sleep.c,v 1.5 1997/06/29 07:33:08 denny Exp $"; #endif #endif /* not lint */ @@ -52,6 +52,8 @@ static char rcsid[] = "$OpenBSD: sleep.c,v 1.4 1997/01/15 23:40:28 millert Exp $ #include #include #include +#include +#include void usage __P((void)); @@ -60,7 +62,11 @@ main(argc, argv) int argc; char *argv[]; { - int ch, secs; + int ch; + int secs = 0; + unsigned char *fp; + long nsecs = 0; + struct timespec rqtp; setlocale(LC_ALL, ""); @@ -75,8 +81,25 @@ main(argc, argv) if (argc != 1) usage(); - if ((secs = atoi(*argv)) > 0) - (void)sleep(secs); + /* Handle fractions of a second */ + fp = strchr(*argv, '.'); + if (fp != NULL) { + int i; + + *fp++ = '\0'; + for (i = 100000000; i > 0; i /= 10) { + if (*fp == '\0') break; + nsecs += (*fp++ - '0') * i; + } + } + + secs = atoi(*argv); + + rqtp.tv_sec = (time_t) secs; + rqtp.tv_nsec = nsecs; + + if ((secs > 0) || (nsecs > 0)) + (void)nanosleep(&rqtp, NULL); exit(0); } -- cgit v1.2.3