Skip to content

Commit 3aac05f

Browse files
committed
pwait: Fix timeout unit parser
The timeout parser would check the first character after the number and ignore any subsequent ones. While here, switch to bool for booleans and fix some style nits. MFC after: 1 week Reviewed by: 0mp, markj Differential Revision: https://reviews.freebsd.org/D52612
1 parent 5953e7c commit 3aac05f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

bin/pwait/pwait.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <err.h>
4040
#include <errno.h>
4141
#include <signal.h>
42+
#include <stdbool.h>
4243
#include <stdio.h>
4344
#include <stdlib.h>
4445
#include <string.h>
@@ -48,7 +49,6 @@
4849
static void
4950
usage(void)
5051
{
51-
5252
fprintf(stderr, "usage: pwait [-t timeout] [-ov] pid ...\n");
5353
exit(EX_USAGE);
5454
}
@@ -61,15 +61,15 @@ main(int argc, char *argv[])
6161
{
6262
struct itimerval itv;
6363
struct kevent *e;
64-
int oflag, tflag, verbose;
65-
int i, kq, n, nleft, opt, status;
66-
long pid;
6764
char *end, *s;
6865
double timeout;
66+
long pid;
67+
int i, kq, n, nleft, opt, status;
68+
bool oflag, tflag, verbose;
6969

70-
oflag = 0;
71-
tflag = 0;
72-
verbose = 0;
70+
oflag = false;
71+
tflag = false;
72+
verbose = false;
7373
memset(&itv, 0, sizeof(itv));
7474

7575
while ((opt = getopt(argc, argv, "ot:v")) != -1) {
@@ -78,25 +78,31 @@ main(int argc, char *argv[])
7878
oflag = 1;
7979
break;
8080
case 't':
81-
tflag = 1;
81+
tflag = true;
8282
errno = 0;
8383
timeout = strtod(optarg, &end);
8484
if (end == optarg || errno == ERANGE || timeout < 0) {
8585
errx(EX_DATAERR, "timeout value");
8686
}
87-
switch(*end) {
88-
case 0:
87+
switch (*end) {
88+
case '\0':
89+
break;
8990
case 's':
91+
end++;
9092
break;
9193
case 'h':
9294
timeout *= 60;
9395
/* FALLTHROUGH */
9496
case 'm':
9597
timeout *= 60;
98+
end++;
9699
break;
97100
default:
98101
errx(EX_DATAERR, "timeout unit");
99102
}
103+
if (*end != '\0') {
104+
errx(EX_DATAERR, "timeout unit");
105+
}
100106
if (timeout > 100000000L) {
101107
errx(EX_DATAERR, "timeout value");
102108
}
@@ -106,7 +112,7 @@ main(int argc, char *argv[])
106112
(suseconds_t)(timeout * 1000000UL);
107113
break;
108114
case 'v':
109-
verbose = 1;
115+
verbose = true;
110116
break;
111117
default:
112118
usage();
@@ -134,7 +140,7 @@ main(int argc, char *argv[])
134140
for (n = 0; n < argc; n++) {
135141
s = argv[n];
136142
/* Undocumented Solaris compat */
137-
if (!strncmp(s, "/proc/", 6)) {
143+
if (strncmp(s, "/proc/", 6) == 0) {
138144
s += 6;
139145
}
140146
errno = 0;

0 commit comments

Comments
 (0)