top(1): Use warnx and errx instead of fprintf

This also makes make "-v" exit without error, since it isn't.
This commit is contained in:
eadler 2018-08-22 10:07:15 +00:00
parent 270d043936
commit 78480951cf
4 changed files with 22 additions and 56 deletions

View File

@ -34,6 +34,7 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include <err.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@ -1351,9 +1352,7 @@ setup_buffer(char *buffer, int addlen)
} }
if (NULL == b) { if (NULL == b) {
fprintf(stderr, "%s: can't allocate sufficient memory\n", errx(4, "can't allocate sufficient memory");
myname);
exit(4);
} }
return b; return b;

View File

@ -21,6 +21,8 @@
*/ */
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <err.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
@ -94,12 +96,11 @@ init_termcap(bool interactive)
{ {
if (status == -1) if (status == -1)
{ {
fprintf(stderr, "%s: can't open termcap file\n", myname); warnx("can't open termcap file");
} }
else else
{ {
fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n", warnx("no termcap entry for a `%s' terminal", term_name);
myname, term_name);
} }
/* pretend it's dumb and proceed */ /* pretend it's dumb and proceed */

View File

@ -68,7 +68,6 @@ static int max_topn; /* maximum displayable processes */
/* miscellaneous things */ /* miscellaneous things */
struct process_select ps; struct process_select ps;
const char * myname = "top";
pid_t mypid; pid_t mypid;
/* pointers to display routines */ /* pointers to display routines */
@ -265,18 +264,6 @@ main(int argc, char *argv[])
setbuffer(stdout, stdoutbuf, Buffersize); setbuffer(stdout, stdoutbuf, Buffersize);
#endif #endif
if (argc > 0)
{
if ((myname = strrchr(argv[0], '/')) == 0)
{
myname = argv[0];
}
else
{
myname++;
}
}
mypid = getpid(); mypid = getpid();
/* get our name */ /* get our name */
@ -323,9 +310,8 @@ main(int argc, char *argv[])
switch(i) switch(i)
{ {
case 'v': /* show version number */ case 'v': /* show version number */
fprintf(stderr, "%s: version FreeBSD\n", myname); errx(0, "version FreeBSD");
exit(1); break;
break;
case 'u': /* toggle uid/username display */ case 'u': /* toggle uid/username display */
do_unames = !do_unames; do_unames = !do_unames;
@ -334,8 +320,7 @@ main(int argc, char *argv[])
case 'U': /* display only username's processes */ case 'U': /* display only username's processes */
if ((ps.uid[0] = userid(optarg)) == -1) if ((ps.uid[0] = userid(optarg)) == -1)
{ {
fprintf(stderr, "%s: unknown user\n", optarg); errx(1, "%s: unknown user\n", optarg);
exit(1);
} }
break; break;
@ -363,9 +348,7 @@ main(int argc, char *argv[])
case 'd': /* number of displays to show */ case 'd': /* number of displays to show */
if ((i = atoiwi(optarg)) == Invalid || i == 0) if ((i = atoiwi(optarg)) == Invalid || i == 0)
{ {
fprintf(stderr, warnx("warning: display count should be positive -- option ignored");
"%s: warning: display count should be positive -- option ignored\n",
myname);
warnings++; warnings++;
} }
else else
@ -395,9 +378,7 @@ main(int argc, char *argv[])
warnings++; warnings++;
} }
if (delay < 0) { if (delay < 0) {
fprintf(stderr, warnx("warning: seconds delay should be positive -- using default");
"%s: warning: seconds delay should be positive -- using default\n",
myname);
delay = 2; delay = 2;
warnings++; warnings++;
} }
@ -408,8 +389,7 @@ main(int argc, char *argv[])
errno = 0; errno = 0;
i = setpriority(PRIO_PROCESS, 0, PRIO_MIN); i = setpriority(PRIO_PROCESS, 0, PRIO_MIN);
if (i == -1 && errno != 0) { if (i == -1 && errno != 0) {
fprintf(stderr, warnx("warning: `-q' option failed (%m)");
"%s: warning: `-q' option failed (%m)\n", myname);
warnings++; warnings++;
} }
break; break;
@ -420,11 +400,7 @@ main(int argc, char *argv[])
} else if (strcmp(optarg, "cpu") == 0) { } else if (strcmp(optarg, "cpu") == 0) {
displaymode = DISP_CPU; displaymode = DISP_CPU;
} else { } else {
fprintf(stderr, errx(1, "warning: `-m' option can only take args 'io' or 'cpu'");
"%s: warning: `-m' option can only take args "
"'io' or 'cpu'\n",
myname);
exit(1);
} }
break; break;
@ -474,11 +450,9 @@ main(int argc, char *argv[])
break; break;
default: default:
fprintf(stderr, errx(1,
"Usage: %s [-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n" "[-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n"
" [-s time] [-J jail] [-U username] [number]\n", " [-s time] [-J jail] [-U username] [number]");
myname);
exit(1);
} }
} }
@ -487,10 +461,8 @@ main(int argc, char *argv[])
{ {
if ((topn = atoiwi(av[optind])) == Invalid) if ((topn = atoiwi(av[optind])) == Invalid)
{ {
fprintf(stderr, warnx("warning: process display count should be non-negative -- using default");
"%s: warning: process display count should be non-negative -- using default\n", warnings++;
myname);
warnings++;
} }
else else
{ {
@ -525,8 +497,7 @@ main(int argc, char *argv[])
{ {
const char * const *pp; const char * const *pp;
fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n", warnx("'%s' is not a recognized sorting order.", order_name);
myname, order_name);
fprintf(stderr, "\tTry one of these:"); fprintf(stderr, "\tTry one of these:");
pp = statics.order_names; pp = statics.order_names;
while (*pp != NULL) while (*pp != NULL)
@ -547,17 +518,14 @@ main(int argc, char *argv[])
/* initialize display interface */ /* initialize display interface */
if ((max_topn = display_init(&statics)) == -1) if ((max_topn = display_init(&statics)) == -1)
{ {
fprintf(stderr, "%s: can't allocate sufficient memory\n", myname); errx(4, "can't allocate sufficient memory");
exit(4);
} }
/* print warning if user requested more processes than we can display */ /* print warning if user requested more processes than we can display */
if (topn > max_topn) if (topn > max_topn)
{ {
fprintf(stderr, warnx("warning: this terminal can only display %d processes.", max_topn);
"%s: warning: this terminal can only display %d processes.\n", warnings++;
myname, max_topn);
warnings++;
} }
/* adjust for topn == Infinity */ /* adjust for topn == Infinity */

View File

@ -35,8 +35,6 @@ extern int pcpu_stats;
extern int overstrike; extern int overstrike;
extern pid_t mypid; extern pid_t mypid;
extern const char * myname;
extern int (*compares[])(const void*, const void*); extern int (*compares[])(const void*, const void*);
const char* kill_procs(char *); const char* kill_procs(char *);