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 <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
@ -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;

View File

@ -21,6 +21,8 @@
*/
#include <sys/ioctl.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
@ -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 */

View File

@ -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 */

View File

@ -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 *);