--- Makefile-1.8 Thu Apr 25 15:24:31 2002 +++ Makefile Thu Apr 25 15:09:25 2002 @@ -1,6 +1,8 @@ # @(#)Makefile 8.1 (Berkeley) 5/31/93 -# $FreeBSD: src/bin/sleep/Makefile,v 1.8 2001/12/04 01:57:46 obrien Exp $ +# $FreeBSD: src/bin/sleep/Makefile,v 1.5.2.1 2001/08/01 05:23:25 obrien Exp $ PROG= sleep + +LDADD= -lm .include --- sleep.1-1.17 Thu Apr 25 15:25:03 2002 +++ sleep.1 Thu Apr 25 15:18:15 2002 @@ -33,7 +33,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)sleep.1 8.3 (Berkeley) 4/18/94 -.\" $FreeBSD: src/bin/sleep/sleep.1,v 1.17 2001/08/07 15:48:27 ru Exp $ +.\" $FreeBSD: src/bin/sleep/sleep.1,v 1.15.2.2 2001/08/16 10:01:09 ru Exp $ .\" .Dd April 18, 1994 .Dt SLEEP 1 @@ -44,12 +44,18 @@ .Sh SYNOPSIS .Nm .Ar seconds +.Nm +.Ar number[multiplier] .Sh DESCRIPTION The .Nm command suspends execution for a minimum of -.Ar seconds . +.Ar seconds +or for the time specified by +.Ar number[multiplier] . +The multiplier can be `s' for seconds, `m' for minutes, `h' for +hours and `d' for days. .Pp If the .Nm @@ -85,6 +91,10 @@ number seconds later: .Pp .Dl (sleep 1800; sh command_file >& errors)& +.Pp +or +.Pp +.Dl (sleep .5h; sh command_file >& errors)& .Pp This incantation would wait a half hour before running the script command_file. --- sleep.c-1.11 Thu Apr 25 15:23:38 2002 +++ sleep.c Thu Apr 25 15:38:19 2002 @@ -42,7 +42,7 @@ static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; #endif static const char rcsid[] = - "$FreeBSD: src/bin/sleep/sleep.c,v 1.11 2002/02/02 06:50:56 imp Exp $"; + "$FreeBSD: src/bin/sleep/sleep.c,v 1.9.2.1 2001/08/01 05:23:25 obrien Exp $"; #endif /* not lint */ #include @@ -51,6 +51,7 @@ #include #include #include +#include void usage(void); @@ -58,7 +59,7 @@ main(int argc, char *argv[]) { struct timespec time_to_sleep; - long l; + double d; int ch, neg; char *p; @@ -92,33 +93,43 @@ else if (*p == '+') ++p; - /* Calculate seconds. */ - if (isdigit((unsigned char)*p)) { - l = strtol(p, &p, 10); - if (l > INT_MAX) { - /* - * Avoid overflow when `seconds' is huge. This assumes - * that the maximum value for a time_t is >= INT_MAX. - */ - l = INT_MAX; - } - } else - l = 0; - time_to_sleep.tv_sec = (time_t)l; - - /* Calculate nanoseconds. */ - time_to_sleep.tv_nsec = 0; - - if (*p == '.') { /* Decimal point. */ - l = 100000000L; - do { - if (isdigit((unsigned char)*++p)) - time_to_sleep.tv_nsec += (*p - '0') * l; - else - break; - } while (l /= 10); + d = atof(p); + while (*p != 0) { + if (!isdigit(*p) && *p != '.') + break; + p++; } + /* Do multiplier trick. */ + switch (*p) { + case 0: + case 's': + break; + case 'm': + d *= 60; + break; + case 'h': + d *= 60 * 60; + break; + case 'd': + d *= 24 * 60 * 60; + break; + default: + usage(); + /* NOTREACHED */ + } + + if (d > INT_MAX) + d = INT_MAX; + + /* + * Split up the double into an integer (seconds) and + * fractional (nanoseconds) part. + */ + time_to_sleep.tv_sec = (time_t)floor(d); + time_to_sleep.tv_nsec = + (time_t)1000000000L * (d - time_to_sleep.tv_sec); + if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); @@ -130,5 +141,6 @@ { (void)fprintf(stderr, "usage: sleep seconds\n"); + (void)fprintf(stderr, " or: sleep number[multiplier]\n"); exit(1); }