From e021938408ddaae5e672ad7cdbd87c614f3562f6 Mon Sep 17 00:00:00 2001 From: Nate Lawson Date: Thu, 14 Nov 2002 00:20:58 +0000 Subject: [PATCH] Back out previous commit since there is controversy about changing so much in sleep including duping strtol(3). Code changes also increased dynamic size of sleep(1). --- bin/sleep/sleep.c | 54 ++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c index 6fff014f9328..7b1fb2a25e3e 100644 --- a/bin/sleep/sleep.c +++ b/bin/sleep/sleep.c @@ -47,10 +47,10 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include -#include void usage(void); @@ -59,44 +59,51 @@ main(int argc, char *argv[]) { struct timespec time_to_sleep; long l; + int ch, neg; char *p; - if (argc != 2) { + while ((ch = getopt(argc, argv, "")) != -1) + switch(ch) { + case '?': + default: + usage(); + /* NOTREACHED */ + } + argc -= optind; + argv += optind; + + if (argc != 1) { usage(); /* NOTREACHED */ } - p = argv[1]; + p = argv[0]; /* Skip over leading whitespaces. */ - while (isspace(*p)) + while (isspace((unsigned char)*p)) ++p; - /* Argument must be an int or float with optional +/- */ - if (!isdigit(*p)) { - if (*p == '+') - ++p; - else if (*p == '-' && isdigit(p[1])) - exit(0); - else if (*p != '.') - usage(); - /* NOTREACHED */ + /* Check for optional `+' or `-' sign. */ + neg = 0; + if (*p == '-') { + neg = 1; + ++p; } + else if (*p == '+') + ++p; /* Calculate seconds. */ - l = 0; - while (isdigit(*p)) { - l = (l * 10) + (*p - '0'); - if (l > INT_MAX || l < 0) { + 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; - break; } - ++p; - } + } else + l = 0; time_to_sleep.tv_sec = (time_t)l; /* Calculate nanoseconds. */ @@ -105,14 +112,14 @@ main(int argc, char *argv[]) if (*p == '.') { /* Decimal point. */ l = 100000000L; do { - if (isdigit(*++p)) + if (isdigit((unsigned char)*++p)) time_to_sleep.tv_nsec += (*p - '0') * l; else break; } while (l /= 10); } - if (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0) + if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); exit(0); @@ -121,8 +128,7 @@ main(int argc, char *argv[]) void usage(void) { - const char *msg = "usage: sleep seconds\n"; - write(STDERR_FILENO, msg, strlen(msg)); + (void)fprintf(stderr, "usage: sleep seconds\n"); exit(1); }