Lines 42-48
Link Here
|
42 |
static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; |
42 |
static char sccsid[] = "@(#)sleep.c 8.3 (Berkeley) 4/2/94"; |
43 |
#endif |
43 |
#endif |
44 |
static const char rcsid[] = |
44 |
static const char rcsid[] = |
45 |
"$FreeBSD: src/bin/sleep/sleep.c,v 1.11 2002/02/02 06:50:56 imp Exp $"; |
45 |
"$FreeBSD: src/bin/sleep/sleep.c,v 1.9.2.1 2001/08/01 05:23:25 obrien Exp $"; |
46 |
#endif /* not lint */ |
46 |
#endif /* not lint */ |
47 |
|
47 |
|
48 |
#include <ctype.h> |
48 |
#include <ctype.h> |
Lines 51-56
Link Here
|
51 |
#include <stdlib.h> |
51 |
#include <stdlib.h> |
52 |
#include <time.h> |
52 |
#include <time.h> |
53 |
#include <unistd.h> |
53 |
#include <unistd.h> |
|
|
54 |
#include <math.h> |
54 |
|
55 |
|
55 |
void usage(void); |
56 |
void usage(void); |
56 |
|
57 |
|
Lines 58-64
Link Here
|
58 |
main(int argc, char *argv[]) |
59 |
main(int argc, char *argv[]) |
59 |
{ |
60 |
{ |
60 |
struct timespec time_to_sleep; |
61 |
struct timespec time_to_sleep; |
61 |
long l; |
62 |
double d; |
62 |
int ch, neg; |
63 |
int ch, neg; |
63 |
char *p; |
64 |
char *p; |
64 |
|
65 |
|
Lines 92-124
Link Here
|
92 |
else if (*p == '+') |
93 |
else if (*p == '+') |
93 |
++p; |
94 |
++p; |
94 |
|
95 |
|
95 |
/* Calculate seconds. */ |
96 |
d = atof(p); |
96 |
if (isdigit((unsigned char)*p)) { |
97 |
while (*p != 0) { |
97 |
l = strtol(p, &p, 10); |
98 |
if (!isdigit(*p) && *p != '.') |
98 |
if (l > INT_MAX) { |
99 |
break; |
99 |
/* |
100 |
p++; |
100 |
* Avoid overflow when `seconds' is huge. This assumes |
|
|
101 |
* that the maximum value for a time_t is >= INT_MAX. |
102 |
*/ |
103 |
l = INT_MAX; |
104 |
} |
105 |
} else |
106 |
l = 0; |
107 |
time_to_sleep.tv_sec = (time_t)l; |
108 |
|
109 |
/* Calculate nanoseconds. */ |
110 |
time_to_sleep.tv_nsec = 0; |
111 |
|
112 |
if (*p == '.') { /* Decimal point. */ |
113 |
l = 100000000L; |
114 |
do { |
115 |
if (isdigit((unsigned char)*++p)) |
116 |
time_to_sleep.tv_nsec += (*p - '0') * l; |
117 |
else |
118 |
break; |
119 |
} while (l /= 10); |
120 |
} |
101 |
} |
121 |
|
102 |
|
|
|
103 |
/* Do multiplier trick. */ |
104 |
switch (*p) { |
105 |
case 0: |
106 |
case 's': |
107 |
break; |
108 |
case 'm': |
109 |
d *= 60; |
110 |
break; |
111 |
case 'h': |
112 |
d *= 60 * 60; |
113 |
break; |
114 |
case 'd': |
115 |
d *= 24 * 60 * 60; |
116 |
break; |
117 |
default: |
118 |
usage(); |
119 |
/* NOTREACHED */ |
120 |
} |
121 |
|
122 |
if (d > INT_MAX) |
123 |
d = INT_MAX; |
124 |
|
125 |
/* |
126 |
* Split up the double into an integer (seconds) and |
127 |
* fractional (nanoseconds) part. |
128 |
*/ |
129 |
time_to_sleep.tv_sec = (time_t)floor(d); |
130 |
time_to_sleep.tv_nsec = |
131 |
(time_t)1000000000L * (d - time_to_sleep.tv_sec); |
132 |
|
122 |
if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) |
133 |
if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) |
123 |
(void)nanosleep(&time_to_sleep, (struct timespec *)NULL); |
134 |
(void)nanosleep(&time_to_sleep, (struct timespec *)NULL); |
124 |
|
135 |
|
Lines 130-134
Link Here
|
130 |
{ |
141 |
{ |
131 |
|
142 |
|
132 |
(void)fprintf(stderr, "usage: sleep seconds\n"); |
143 |
(void)fprintf(stderr, "usage: sleep seconds\n"); |
|
|
144 |
(void)fprintf(stderr, " or: sleep number[multiplier]\n"); |
133 |
exit(1); |
145 |
exit(1); |
134 |
} |
146 |
} |