summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authornjl <njl@FreeBSD.org>2002-11-14 00:20:58 +0000
committernjl <njl@FreeBSD.org>2002-11-14 00:20:58 +0000
commita274e06113a656aa1742ad77624a7a8ca4dee0ce (patch)
treed4509ce7d4c27dce57806632dbfc5e0da72d4f0f /bin
parent87416e10edff8b02b89cf2766321a8c3676bf84b (diff)
downloadFreeBSD-src-a274e06113a656aa1742ad77624a7a8ca4dee0ce.zip
FreeBSD-src-a274e06113a656aa1742ad77624a7a8ca4dee0ce.tar.gz
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).
Diffstat (limited to 'bin')
-rw-r--r--bin/sleep/sleep.c54
1 files changed, 30 insertions, 24 deletions
diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c
index 6fff014..7b1fb2a 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,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);
}
OpenPOWER on IntegriCloud