Skip to content

Commit a2fdfe9

Browse files
committed
Refer to -h on incorrect usage
* defs.h (error_msg_and_help): New prototype. * strace.c (error_msg_and_help): New function. (error_opt_arg, init): Use it. (usage): Remove unnecessary arguments. * count.c: Use the new function.
1 parent 3272d29 commit a2fdfe9

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

count.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ set_sortby(const char *sortby)
140140
else if (strcmp(sortby, "nothing") == 0)
141141
sortfun = NULL;
142142
else {
143-
error_msg_and_die("invalid sortby: '%s'", sortby);
143+
error_msg_and_help("invalid sortby: '%s'", sortby);
144144
}
145145
}
146146

defs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,8 @@ void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
500500
void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
501501
void error_msg_and_die(const char *fmt, ...)
502502
ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
503+
void error_msg_and_help(const char *fmt, ...)
504+
ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
503505
void perror_msg_and_die(const char *fmt, ...)
504506
ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
505507
void die_out_of_memory(void) ATTRIBUTE_NORETURN;

strace.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ strerror(int err_no)
196196
#endif /* HAVE_STERRROR */
197197

198198
static void
199-
usage(FILE *ofp, int exitval)
199+
usage()
200200
{
201-
fprintf(ofp, "\
201+
printf("\
202202
usage: strace [-CdffhiqrtttTvVwxxy] [-I n] [-e expr]...\n\
203203
[-a column] [-o file] [-s strsize] [-P path]...\n\
204204
-p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]\n\
@@ -267,7 +267,7 @@ Miscellaneous:\n\
267267
-z -- print only succeeding syscalls\n\
268268
*/
269269
, DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY);
270-
exit(exitval);
270+
exit(0);
271271
}
272272

273273
static void ATTRIBUTE_NORETURN
@@ -327,6 +327,17 @@ void error_msg_and_die(const char *fmt, ...)
327327
die();
328328
}
329329

330+
void error_msg_and_help(const char *fmt, ...)
331+
{
332+
if (fmt != NULL) {
333+
va_list p;
334+
va_start(p, fmt);
335+
verror_msg(0, fmt, p);
336+
}
337+
fprintf(stderr, "Try '%s -h' for more information.\n", progname);
338+
die();
339+
}
340+
330341
void perror_msg(const char *fmt, ...)
331342
{
332343
va_list p;
@@ -346,7 +357,7 @@ void perror_msg_and_die(const char *fmt, ...)
346357
static void
347358
error_opt_arg(int opt, const char *arg)
348359
{
349-
error_msg_and_die("Invalid -%c argument: '%s'", opt, arg);
360+
error_msg_and_help("invalid -%c argument: '%s'", opt, arg);
350361
}
351362

352363
#if USE_SEIZE
@@ -1488,13 +1499,13 @@ init(int argc, char *argv[])
14881499
break;
14891500
case 'c':
14901501
if (cflag == CFLAG_BOTH) {
1491-
error_msg_and_die("-c and -C are mutually exclusive");
1502+
error_msg_and_help("-c and -C are mutually exclusive");
14921503
}
14931504
cflag = CFLAG_ONLY_STATS;
14941505
break;
14951506
case 'C':
14961507
if (cflag == CFLAG_ONLY_STATS) {
1497-
error_msg_and_die("-c and -C are mutually exclusive");
1508+
error_msg_and_help("-c and -C are mutually exclusive");
14981509
}
14991510
cflag = CFLAG_BOTH;
15001511
break;
@@ -1511,7 +1522,7 @@ init(int argc, char *argv[])
15111522
followfork++;
15121523
break;
15131524
case 'h':
1514-
usage(stdout, 0);
1525+
usage();
15151526
break;
15161527
case 'i':
15171528
iflag = 1;
@@ -1597,7 +1608,7 @@ init(int argc, char *argv[])
15971608
error_opt_arg(c, optarg);
15981609
break;
15991610
default:
1600-
usage(stderr, 1);
1611+
error_msg_and_help(NULL);
16011612
break;
16021613
}
16031614
}
@@ -1609,22 +1620,23 @@ init(int argc, char *argv[])
16091620
acolumn_spaces[acolumn] = '\0';
16101621

16111622
/* Must have PROG [ARGS], or -p PID. Not both. */
1612-
if (!argv[0] == !nprocs)
1613-
usage(stderr, 1);
1623+
if (!argv[0] == !nprocs) {
1624+
error_msg_and_help("must have PROG [ARGS] or -p PID");
1625+
}
16141626

16151627
if (nprocs != 0 && daemonized_tracer) {
1616-
error_msg_and_die("-D and -p are mutually exclusive");
1628+
error_msg_and_help("-D and -p are mutually exclusive");
16171629
}
16181630

16191631
if (!followfork)
16201632
followfork = optF;
16211633

16221634
if (followfork >= 2 && cflag) {
1623-
error_msg_and_die("(-c or -C) and -ff are mutually exclusive");
1635+
error_msg_and_help("(-c or -C) and -ff are mutually exclusive");
16241636
}
16251637

16261638
if (count_wallclock && !cflag) {
1627-
error_msg_and_die("-w must be given with (-c or -C)");
1639+
error_msg_and_help("-w must be given with (-c or -C)");
16281640
}
16291641

16301642
if (cflag == CFLAG_ONLY_STATS) {
@@ -1685,7 +1697,7 @@ init(int argc, char *argv[])
16851697
* when using popen, so prohibit it.
16861698
*/
16871699
if (followfork >= 2)
1688-
error_msg_and_die("Piping the output and -ff are mutually exclusive");
1700+
error_msg_and_help("piping the output and -ff are mutually exclusive");
16891701
shared_log = strace_popen(outfname + 1);
16901702
}
16911703
else if (followfork < 2)

0 commit comments

Comments
 (0)