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).
This commit is contained in:
parent
9fb92b64ad
commit
e021938408
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user