From 78480951cf4483bd1f98eabb2d66e25d206d9ce6 Mon Sep 17 00:00:00 2001 From: eadler Date: Wed, 22 Aug 2018 10:07:15 +0000 Subject: [PATCH] top(1): Use warnx and errx instead of fprintf This also makes make "-v" exit without error, since it isn't. --- usr.bin/top/display.c | 5 ++-- usr.bin/top/screen.c | 7 +++-- usr.bin/top/top.c | 64 +++++++++++-------------------------------- usr.bin/top/top.h | 2 -- 4 files changed, 22 insertions(+), 56 deletions(-) diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c index 8c82f7e8f0fb..862d2a944c89 100644 --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -1351,9 +1352,7 @@ setup_buffer(char *buffer, int addlen) } if (NULL == b) { - fprintf(stderr, "%s: can't allocate sufficient memory\n", - myname); - exit(4); + errx(4, "can't allocate sufficient memory"); } return b; diff --git a/usr.bin/top/screen.c b/usr.bin/top/screen.c index 11807216adfc..9e56b9e3a1a5 100644 --- a/usr.bin/top/screen.c +++ b/usr.bin/top/screen.c @@ -21,6 +21,8 @@ */ #include + +#include #include #include #include @@ -94,12 +96,11 @@ init_termcap(bool interactive) { if (status == -1) { - fprintf(stderr, "%s: can't open termcap file\n", myname); + warnx("can't open termcap file"); } else { - fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n", - myname, term_name); + warnx("no termcap entry for a `%s' terminal", term_name); } /* pretend it's dumb and proceed */ diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index b76dbb54676d..2279479409b1 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -68,7 +68,6 @@ static int max_topn; /* maximum displayable processes */ /* miscellaneous things */ struct process_select ps; -const char * myname = "top"; pid_t mypid; /* pointers to display routines */ @@ -265,18 +264,6 @@ main(int argc, char *argv[]) setbuffer(stdout, stdoutbuf, Buffersize); #endif - if (argc > 0) - { - if ((myname = strrchr(argv[0], '/')) == 0) - { - myname = argv[0]; - } - else - { - myname++; - } - } - mypid = getpid(); /* get our name */ @@ -323,9 +310,8 @@ main(int argc, char *argv[]) switch(i) { case 'v': /* show version number */ - fprintf(stderr, "%s: version FreeBSD\n", myname); - exit(1); - break; + errx(0, "version FreeBSD"); + break; case 'u': /* toggle uid/username display */ do_unames = !do_unames; @@ -334,8 +320,7 @@ main(int argc, char *argv[]) case 'U': /* display only username's processes */ if ((ps.uid[0] = userid(optarg)) == -1) { - fprintf(stderr, "%s: unknown user\n", optarg); - exit(1); + errx(1, "%s: unknown user\n", optarg); } break; @@ -363,9 +348,7 @@ main(int argc, char *argv[]) case 'd': /* number of displays to show */ if ((i = atoiwi(optarg)) == Invalid || i == 0) { - fprintf(stderr, - "%s: warning: display count should be positive -- option ignored\n", - myname); + warnx("warning: display count should be positive -- option ignored"); warnings++; } else @@ -395,9 +378,7 @@ main(int argc, char *argv[]) warnings++; } if (delay < 0) { - fprintf(stderr, - "%s: warning: seconds delay should be positive -- using default\n", - myname); + warnx("warning: seconds delay should be positive -- using default"); delay = 2; warnings++; } @@ -408,8 +389,7 @@ main(int argc, char *argv[]) errno = 0; i = setpriority(PRIO_PROCESS, 0, PRIO_MIN); if (i == -1 && errno != 0) { - fprintf(stderr, - "%s: warning: `-q' option failed (%m)\n", myname); + warnx("warning: `-q' option failed (%m)"); warnings++; } break; @@ -420,11 +400,7 @@ main(int argc, char *argv[]) } else if (strcmp(optarg, "cpu") == 0) { displaymode = DISP_CPU; } else { - fprintf(stderr, - "%s: warning: `-m' option can only take args " - "'io' or 'cpu'\n", - myname); - exit(1); + errx(1, "warning: `-m' option can only take args 'io' or 'cpu'"); } break; @@ -474,11 +450,9 @@ main(int argc, char *argv[]) break; default: - fprintf(stderr, -"Usage: %s [-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n" -" [-s time] [-J jail] [-U username] [number]\n", - myname); - exit(1); + errx(1, +"[-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n" +" [-s time] [-J jail] [-U username] [number]"); } } @@ -487,10 +461,8 @@ main(int argc, char *argv[]) { if ((topn = atoiwi(av[optind])) == Invalid) { - fprintf(stderr, - "%s: warning: process display count should be non-negative -- using default\n", - myname); - warnings++; + warnx("warning: process display count should be non-negative -- using default"); + warnings++; } else { @@ -525,8 +497,7 @@ main(int argc, char *argv[]) { const char * const *pp; - fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n", - myname, order_name); + warnx("'%s' is not a recognized sorting order.", order_name); fprintf(stderr, "\tTry one of these:"); pp = statics.order_names; while (*pp != NULL) @@ -547,17 +518,14 @@ main(int argc, char *argv[]) /* initialize display interface */ if ((max_topn = display_init(&statics)) == -1) { - fprintf(stderr, "%s: can't allocate sufficient memory\n", myname); - exit(4); + errx(4, "can't allocate sufficient memory"); } /* print warning if user requested more processes than we can display */ if (topn > max_topn) { - fprintf(stderr, - "%s: warning: this terminal can only display %d processes.\n", - myname, max_topn); - warnings++; + warnx("warning: this terminal can only display %d processes.", max_topn); + warnings++; } /* adjust for topn == Infinity */ diff --git a/usr.bin/top/top.h b/usr.bin/top/top.h index e59ecb061a4b..7ec2d7f2c199 100644 --- a/usr.bin/top/top.h +++ b/usr.bin/top/top.h @@ -35,8 +35,6 @@ extern int pcpu_stats; extern int overstrike; extern pid_t mypid; -extern const char * myname; - extern int (*compares[])(const void*, const void*); const char* kill_procs(char *);