diff options
author | njl <njl@FreeBSD.org> | 2002-11-13 20:12:26 +0000 |
---|---|---|
committer | njl <njl@FreeBSD.org> | 2002-11-13 20:12:26 +0000 |
commit | 926d93e39281f0c768ecb87809c7589d60c8672b (patch) | |
tree | bf571d906ed8a3bfeb381a328b5d87cfe6dbb382 /bin | |
parent | 9a2b5d5d203ab40ddb50a7934236d6881229e839 (diff) | |
download | FreeBSD-src-926d93e39281f0c768ecb87809c7589d60c8672b.zip FreeBSD-src-926d93e39281f0c768ecb87809c7589d60c8672b.tar.gz |
Remove getopt and strtol dependencies, reducing size of static exe.
Preserve older desired behavior, accept [+-]*[0-9]*\.[0-9]*
Remove a few unnecessary casts.
%ls -l /bin/sleep
-r-xr-xr-x 1 root wheel 61332 Oct 28 05:16 /bin/sleep
%ls -l /usr/obj/usr/src/bin/sleep/sleep
-rwxr-xr-x 1 root wheel 19124 Nov 13 12:12 /usr/obj/usr/src/bin/sleep/sleep
Submitted by: Tim Kientzle <kientzle@acm.org>
Diffstat (limited to 'bin')
-rw-r--r-- | bin/sleep/sleep.c | 54 |
1 files changed, 24 insertions, 30 deletions
diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c index 7b1fb2a..6fff014 100644 --- a/bin/sleep/sleep.c +++ b/bin/sleep/sleep.c @@ -47,10 +47,10 @@ __FBSDID("$FreeBSD$"); #include <ctype.h> #include <limits.h> -#include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> +#include <string.h> void usage(void); @@ -59,51 +59,44 @@ main(int argc, char *argv[]) { struct timespec time_to_sleep; long l; - int ch, neg; char *p; - while ((ch = getopt(argc, argv, "")) != -1) - switch(ch) { - case '?': - default: - usage(); - /* NOTREACHED */ - } - argc -= optind; - argv += optind; - - if (argc != 1) { + if (argc != 2) { usage(); /* NOTREACHED */ } - p = argv[0]; + p = argv[1]; /* Skip over leading whitespaces. */ - while (isspace((unsigned char)*p)) + while (isspace(*p)) ++p; - /* Check for optional `+' or `-' sign. */ - neg = 0; - if (*p == '-') { - neg = 1; - ++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 */ } - else if (*p == '+') - ++p; /* Calculate seconds. */ - if (isdigit((unsigned char)*p)) { - l = strtol(p, &p, 10); - if (l > INT_MAX) { + l = 0; + while (isdigit(*p)) { + l = (l * 10) + (*p - '0'); + if (l > INT_MAX || l < 0) { /* * Avoid overflow when `seconds' is huge. This assumes * that the maximum value for a time_t is >= INT_MAX. */ l = INT_MAX; + break; } - } else - l = 0; + ++p; + } time_to_sleep.tv_sec = (time_t)l; /* Calculate nanoseconds. */ @@ -112,14 +105,14 @@ main(int argc, char *argv[]) if (*p == '.') { /* Decimal point. */ l = 100000000L; do { - if (isdigit((unsigned char)*++p)) + if (isdigit(*++p)) time_to_sleep.tv_nsec += (*p - '0') * l; else break; } while (l /= 10); } - if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) + if (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0) (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); exit(0); @@ -128,7 +121,8 @@ main(int argc, char *argv[]) void usage(void) { + const char *msg = "usage: sleep seconds\n"; - (void)fprintf(stderr, "usage: sleep seconds\n"); + write(STDERR_FILENO, msg, strlen(msg)); exit(1); } |