From: Matthew Dillon Date: Fri, 21 Aug 2009 10:44:00 +0000 (-0700) Subject: AMD64 - Refactor the 'top' program. X-Git-Tag: v2.4.0~149 X-Git-Url: https://gitweb.dragonflybsd.org/~nant/dragonfly.git/commitdiff_plain/b552171b10e3b71b8e22c7689ea60da032d3299a AMD64 - Refactor the 'top' program. * Ansify pretty much the whole thing. The 64-bit build simply will break and break and break with K&R style declarations. * Fix misc other issues with curses. --- diff --git a/contrib/top/boolean.h b/contrib/top/boolean.h index c6bcf4db73..c873361bf0 100644 --- a/contrib/top/boolean.h +++ b/contrib/top/boolean.h @@ -2,4 +2,3 @@ #define No 0 #define Yes 1 #define Maybe 2 /* tri-state boolean, actually */ - diff --git a/contrib/top/commands.c b/contrib/top/commands.c index 48d7c8165d..3c3cb95e56 100644 --- a/contrib/top/commands.c +++ b/contrib/top/commands.c @@ -22,6 +22,7 @@ #include "os.h" #include #include +#include #include #include #include @@ -30,22 +31,27 @@ #include "top.h" #include "boolean.h" #include "utils.h" +#include "commands.h" -extern char *copyright; +struct errs; + +extern const char *copyright; /* imported from screen.c */ extern int overstrike; -int err_compar(); -char *err_string(); +static int err_compar(const void *, const void *); +static const char *err_string(void); +static int str_adderr(char *str, int len, int err); +static int str_addarg(char *str, int len, char *arg, int first); /* * show_help() - display the help screen; invoked in response to * either 'h' or '?'. */ -show_help() - +void +show_help(void) { printf("Top version %s, %s\n", version_string(), copyright); fputs("\n\n\ @@ -93,10 +99,8 @@ u - display processes for only one user (+ selects all users)\n\ * Utility routines that help with some of the commands. */ -char *next_field(str) - -register char *str; - +char * +next_field(char *str) { if ((str = strchr(str, ' ')) == NULL) { @@ -110,11 +114,8 @@ register char *str; return(*str == '\0' ? NULL : str); } -scanint(str, intp) - -char *str; -int *intp; - +int +scanint(char *str, int *intp) { register int val = 0; register char ch; @@ -164,8 +165,8 @@ struct errs /* structure for a system-call error */ static struct errs errs[ERRMAX]; static int errcnt; -static char *err_toomany = " too many errors occurred"; -static char *err_listem = +static const char *err_toomany = " too many errors occurred"; +static const char *err_listem = " Many errors occurred. Press `e' to display the list of errors."; /* These macros get used to reset and log the errors */ @@ -189,13 +190,13 @@ static char *err_listem = #define STRMAX 80 -char *err_string() - +static const char * +err_string(void) { - register struct errs *errp; - register int cnt = 0; - register int first = Yes; - register int currerr = -1; + struct errs *errp; + int cnt = 0; + int first = Yes; + int currerr = -1; int stringlen; /* characters still available in "string" */ static char string[STRMAX]; @@ -249,15 +250,11 @@ char *err_string() * the string "str". */ -str_adderr(str, len, err) - -char *str; -int len; -int err; - +static int +str_adderr(char *str, int len, int err) { - register char *msg; - register int msglen; + const char *msg; + int msglen; msg = err == 0 ? "Not a number" : errmsg(err); msglen = strlen(msg) + 2; @@ -275,14 +272,8 @@ int err; * the string "str". This is the first in the group when "first" * is set (indicating that a comma should NOT be added to the front). */ - -str_addarg(str, len, arg, first) - -char *str; -int len; -char *arg; -int first; - +static int +str_addarg(char *str, int len, char *arg, int first) { register int arglen; @@ -307,13 +298,12 @@ int first; * err_compar(p1, p2) - comparison routine used by "qsort" * for sorting errors. */ - -err_compar(p1, p2) - -register struct errs *p1, *p2; - +static int +err_compar(const void *arg1, const void *arg2) { - register int result; + const struct errs *p1 = arg1; + const struct errs *p2 = arg2; + int result; if ((result = p1->errnum - p2->errnum) == 0) { @@ -326,8 +316,8 @@ register struct errs *p1, *p2; * error_count() - return the number of errors currently logged. */ -error_count() - +int +error_count(void) { return(errcnt); } @@ -336,8 +326,8 @@ error_count() * show_errors() - display on stdout the current log of errors. */ -show_errors() - +void +show_errors(void) { register int cnt = 0; register struct errs *errp = errs; @@ -356,10 +346,8 @@ show_errors() * command does; invoked in response to 'k'. */ -char *kill_procs(str) - -char *str; - +const char * +kill_procs(char *str) { register char *nptr; int signum = SIGTERM; /* default */ @@ -446,10 +434,8 @@ char *str; * "renice" command does; invoked in response to 'r'. */ -char *renice_procs(str) - -char *str; - +const char * +renice_procs(char *str) { register char negate; int prio; diff --git a/contrib/top/commands.h b/contrib/top/commands.h new file mode 100644 index 0000000000..02d68f1669 --- /dev/null +++ b/contrib/top/commands.h @@ -0,0 +1,13 @@ +/* + * + */ + +void show_help(void); +char *next_field(char *str); +int scanint(char *str, int *intp); + +const char *kill_procs(char *); +const char *renice_procs(char *str); + +int error_count(void); +void show_errors(void); diff --git a/contrib/top/display.c b/contrib/top/display.c index 6612c11b61..a4c4c3c886 100644 --- a/contrib/top/display.c +++ b/contrib/top/display.c @@ -29,10 +29,14 @@ * *_process, u_endscreen. */ +#include +#include #include "os.h" #include #include -#include +#include +#include +#include #include "screen.h" /* interface to screen package */ #include "layout.h" /* defines for screen position layout */ @@ -57,17 +61,17 @@ static int display_width = MAX_COLS; #define lineindex(l) ((l)*display_width) -char *printable(); +char *printable(char *str); /* things initialized by display_init and used thruout */ /* buffer of proc information lines for display updating */ char *screenbuf = NULL; -static char **procstate_names; -static char **cpustate_names; -static char **memory_names; -static char **swap_names; +static const char **procstate_names; +static const char **cpustate_names; +static const char **memory_names; +static const char **swap_names; static int num_procstates; static int num_cpustates; @@ -83,14 +87,14 @@ static int cpustate_total_length; static enum { OFF, ON, ERASE } header_status = ON; -static int string_count(); -static void summary_format(); -static void line_update(); - -int display_resize() +static int string_count(const char **); +static void summary_format(char *str, int *numbers, const char **names); +static void line_update(char *old, char *new, int start, int line); +int +display_resize(void) { - register int lines; + int xlines; /* first, deallocate any previous buffer that may have been there */ if (screenbuf != NULL) @@ -100,10 +104,10 @@ int display_resize() /* calculate the current dimensions */ /* if operating in "dumb" mode, we only need one line */ - lines = smart_terminal ? screen_length - Header_lines : 1; + xlines = smart_terminal ? screen_length - Header_lines : 1; - if (lines < 0) - lines = 0; + if (xlines < 0) + xlines = 0; /* we don't want more than MAX_COLS columns, since the machine-dependent modules make static allocations based on MAX_COLS and we don't want to run off the end of their buffers */ @@ -114,7 +118,7 @@ int display_resize() } /* now, allocate space for the screen buffer */ - screenbuf = (char *)malloc(lines * display_width); + screenbuf = (char *)malloc(xlines * display_width); if (screenbuf == (char *)NULL) { /* oops! */ @@ -123,24 +127,22 @@ int display_resize() /* return number of lines available */ /* for dumb terminals, pretend like we can show any amount */ - return(smart_terminal ? lines : Largest); + return(smart_terminal ? xlines : Largest); } -int display_init(statics) - -struct statics *statics; - +int +display_init(struct statics *statics) { - register int lines; - register char **pp; - register int *ip; - register int i; + int xlines; + const char **pp; + int *ip; + int i; /* call resize to do the dirty work */ - lines = display_resize(); + xlines = display_resize(); /* only do the rest if we need to */ - if (lines > -1) + if (xlines > -1) { /* save pointers and allocate space for names */ procstate_names = statics->procstate_names; @@ -174,19 +176,16 @@ struct statics *statics; } /* return number of lines available */ - return(lines); + return(xlines); } -i_loadave(mpid, avenrun) - -int mpid; -double *avenrun; - +void +i_loadave(int mpid, double *avenrun) { - register int i; + int i; /* i_loadave also clears the screen, since it is first */ - clear(); + clear_myscreen(); /* mpid == -1 implies this system doesn't have an _mpid */ if (mpid != -1) @@ -205,11 +204,8 @@ double *avenrun; lmpid = mpid; } -u_loadave(mpid, avenrun) - -int mpid; -double *avenrun; - +void +u_loadave(int mpid, double *avenrun) { register int i; @@ -244,10 +240,8 @@ double *avenrun; } } -i_timeofday(tod) - -time_t *tod; - +void +i_timeofday(time_t *tod) { /* * Display the current time. @@ -289,11 +283,8 @@ static char procstates_buffer[MAX_COLS]; * lastline is valid */ -i_procstates(total, brkdn) - -int total; -int *brkdn; - +void +i_procstates(int total, int *brkdn) { register int i; @@ -316,11 +307,8 @@ int *brkdn; memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); } -u_procstates(total, brkdn) - -int total; -int *brkdn; - +void +u_procstates(int total, int *brkdn) { static char new[MAX_COLS]; register int i; @@ -373,10 +361,10 @@ int *brkdn; void i_cpustates(struct system_info *si) { - register int i; - register int value; - register char **names; - register char *thisname; + int i; + int value; + const char **names; + const char *thisname; int *states = si->cpustates; int cpu; @@ -408,11 +396,11 @@ i_cpustates(struct system_info *si) } void -z_cpustates(struct system_info *si) +z_cpustates(struct system_info *si __unused) { - register int i; - register char **names; - register char *thisname; + int i; + const char **names; + const char *thisname; int cpu; /* show tag and bump lastline */ @@ -444,10 +432,8 @@ z_cpustates(struct system_info *si) char memory_buffer[MAX_COLS]; -i_memory(stats) - -int *stats; - +void +i_memory(int *stats) { fputs("\nMem: ", stdout); lastline++; @@ -457,10 +443,8 @@ int *stats; fputs(memory_buffer, stdout); } -u_memory(stats) - -int *stats; - +void +u_memory(int *stats) { static char new[MAX_COLS]; @@ -478,10 +462,8 @@ int *stats; char swap_buffer[MAX_COLS]; -i_swap(stats) - -int *stats; - +void +i_swap(int *stats) { fputs("\nSwap: ", stdout); lastline++; @@ -491,10 +473,8 @@ int *stats; fputs(swap_buffer, stdout); } -u_swap(stats) - -int *stats; - +void +u_swap(int *stats) { static char new[MAX_COLS]; @@ -522,8 +502,8 @@ static int msglen = 0; /* Invariant: msglen is always the length of the message currently displayed on the screen (even when next_msg doesn't contain that message). */ -i_message() - +void +i_message(void) { while (lastline < y_message) { @@ -532,7 +512,7 @@ i_message() } if (next_msg[0] != '\0') { - standout(next_msg); + dostandout(next_msg); msglen = strlen(next_msg); next_msg[0] = '\0'; } @@ -543,8 +523,8 @@ i_message() } } -u_message() - +void +u_message(void) { i_message(); } @@ -557,10 +537,8 @@ static int header_length; * Assumptions: cursor is on the previous line and lastline is consistent */ -i_header(text) - -char *text; - +void +i_header(char *text) { header_length = strlen(text); if (header_status == ON) @@ -576,10 +554,8 @@ char *text; } /*ARGSUSED*/ -u_header(text) - -char *text; /* ignored */ - +void +u_header(char *text __unused) { if (header_status == ERASE) { @@ -596,11 +572,8 @@ char *text; /* ignored */ * Assumptions: lastline is consistent */ -i_process(line, thisline) - -int line; -char *thisline; - +void +i_process(int line, char *thisline) { register char *p; register char *base; @@ -626,24 +599,21 @@ char *thisline; memzero(p, display_width - (p - base)); } -u_process(line, newline) - -int line; -char *newline; - +void +u_process(int xline, char *xnewline) { - register char *optr; - register int screen_line = line + Header_lines; - register char *bufferline; + char *optr; + int screen_line = xline + Header_lines; + char *bufferline; /* remember a pointer to the current line in the screen buffer */ - bufferline = &screenbuf[lineindex(line)]; + bufferline = &screenbuf[lineindex(xline)]; /* truncate the line to conform to our current screen width */ newline[display_width] = '\0'; /* is line higher than we went on the last display? */ - if (line >= last_hi) + if (xline >= last_hi) { /* yes, just ignore screenbuf and write it out directly */ /* get positioned on the correct line */ @@ -659,27 +629,25 @@ char *newline; } /* now write the line */ - fputs(newline, stdout); + fputs(xnewline, stdout); /* copy it in to the buffer */ - optr = strecpy(bufferline, newline); + optr = strecpy(bufferline, xnewline); /* zero fill the rest of it */ memzero(optr, display_width - (optr - bufferline)); } else { - line_update(bufferline, newline, 0, line + Header_lines); + line_update(bufferline, xnewline, 0, xline + Header_lines); } } -u_endscreen(hi) - -register int hi; - +void +u_endscreen(int hi) { - register int screen_line = hi + Header_lines; - register int i; + int screen_line = hi + Header_lines; + int i; if (smart_terminal) { @@ -733,10 +701,8 @@ register int hi; } } -display_header(t) - -int t; - +void +display_header(int t) { if (t) { @@ -749,17 +715,16 @@ int t; } /*VARARGS2*/ -new_message(type, msgfmt, a1, a2, a3) - -int type; -char *msgfmt; -caddr_t a1, a2, a3; - +void +new_message(int type, const char *msgfmt, ...) { - register int i; + va_list va; + int i; /* first, format the message */ - (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3); + va_start(va, msgfmt); + vsnprintf(next_msg, sizeof(next_msg), msgfmt, va); + va_end(va); if (msglen > 0) { @@ -770,8 +735,10 @@ caddr_t a1, a2, a3; i = strlen(next_msg); if ((type & MT_delayed) == 0) { - type & MT_standout ? standout(next_msg) : - fputs(next_msg, stdout); + if (type & MT_standout) + dostandout(next_msg); + else + fputs(next_msg, stdout); (void) clear_eol(msglen - i); msglen = i; next_msg[0] = '\0'; @@ -782,15 +749,18 @@ caddr_t a1, a2, a3; { if ((type & MT_delayed) == 0) { - type & MT_standout ? standout(next_msg) : fputs(next_msg, stdout); + if (type & MT_standout) + dostandout(next_msg); + else + fputs(next_msg, stdout); msglen = strlen(next_msg); next_msg[0] = '\0'; } } } -clear_message() - +void +clear_message(void) { if (clear_eol(msglen) == 1) { @@ -798,17 +768,13 @@ clear_message() } } -readline(buffer, size, numeric) - -char *buffer; -int size; -int numeric; - +int +readline(char *buffer, int size, int numeric) { - register char *ptr = buffer; - register char ch; - register char cnt = 0; - register char maxcnt = 0; + char *ptr = buffer; + char ch; + char cnt = 0; + char maxcnt = 0; /* allow room for null terminator */ size -= 1; @@ -885,12 +851,10 @@ int numeric; /* internal support routines */ -static int string_count(pp) - -register char **pp; - +static int +string_count(const char **pp) { - register int cnt; + int cnt; cnt = 0; while (*pp++ != NULL) @@ -900,17 +864,12 @@ register char **pp; return(cnt); } -static void summary_format(str, numbers, names) - -char *str; -int *numbers; -register char **names; - +static void +summary_format(char *str, int *numbers, const char **names) { - register char *p; - register int num; - register char *thisname; - register int useM = No; + char *p; + int num; + const char *thisname; /* format each number followed by its string */ p = str; @@ -933,7 +892,7 @@ register char **names; } else { - p = strecpy(p, itoa(num)); + p = strecpy(p, ltoa(num)); p = strecpy(p, thisname); } } @@ -953,13 +912,8 @@ register char **names; } } -static void line_update(old, new, start, line) - -register char *old; -register char *new; -int start; -int line; - +static void +line_update(char *old, char *new, int start, int line) { register int ch; register int diff; @@ -1080,13 +1034,11 @@ int line; * to the original buffer is returned. */ -char *printable(str) - -char *str; - +char * +printable(char *str) { - register char *ptr; - register char ch; + char *ptr; + char ch; ptr = str; while ((ch = *ptr) != '\0') @@ -1100,11 +1052,8 @@ char *str; return(str); } -i_uptime(bt, tod) - -struct timeval* bt; -time_t *tod; - +void +i_uptime(struct timeval *bt, time_t *tod) { time_t uptime; int days, hrs, mins, secs; diff --git a/contrib/top/display.h b/contrib/top/display.h index 4bd7ce77e0..a5a32947b4 100644 --- a/contrib/top/display.h +++ b/contrib/top/display.h @@ -5,3 +5,31 @@ #define MT_standout 1 #define MT_delayed 2 +struct statics; +struct system_info; + +int display_resize(void); +int display_init(struct statics *statics); +void i_loadave(int mpid, double *avenrun); +void u_loadave(int mpid, double *avenrun); +void i_timeofday(time_t *tod); +void i_procstates(int total, int *brkdn); +void u_procstates(int total, int *brkdn); +void u_process(int xline, char *xnewline); +void i_cpustates(struct system_info *si); +void z_cpustates(struct system_info *si); +void i_memory(int *stats); +void u_memory(int *stats); +void i_swap(int *stats); +void u_swap(int *stats); +void i_message(void); +void u_message(void); +void i_header(char *text); +void u_header(char *text __unused); +void i_process(int line, char *thisline); +void u_endscreen(int hi); +void i_uptime(struct timeval *bt, time_t *tod); +void display_header(int t); +void new_message(int type, const char *msgfmt, ...); +void clear_message(void); +int readline(char *buffer, int size, int numeric); diff --git a/contrib/top/machine.h b/contrib/top/machine.h index 04eeefad1d..4bed00094e 100644 --- a/contrib/top/machine.h +++ b/contrib/top/machine.h @@ -14,12 +14,12 @@ */ struct statics { - char **procstate_names; - char **cpustate_names; - char **memory_names; - char **swap_names; + const char **procstate_names; + const char **cpustate_names; + const char **memory_names; + const char **swap_names; #ifdef ORDER - char **order_names; + const char **order_names; #endif }; @@ -69,8 +69,7 @@ struct process_select /* routines defined by the machine dependent module */ -char *format_header(); -char *format_next_process(); - -/* non-int routines typically used by the machine dependent module */ -char *printable(); +char *format_header(const char *); +char *format_next_process(caddr_t, char *(*func)(long)); +char *printable(char *); +int machine_init(struct statics *statics); diff --git a/contrib/top/screen.c b/contrib/top/screen.c index 3f141adfff..8429c62722 100644 --- a/contrib/top/screen.c +++ b/contrib/top/screen.c @@ -25,34 +25,14 @@ #include "top.h" #include -#ifdef CBREAK -# include -# define SGTTY -#else -# ifdef TCGETA -# define TERMIO -# include -# else -# define TERMIOS -# include -# endif -#endif -#if defined(TERMIO) || defined(TERMIOS) -# ifndef TAB3 -# ifdef OXTABS -# define TAB3 OXTABS -# else -# define TAB3 0 -# endif -# endif -#endif +#include +#include +#include #include "screen.h" #include "boolean.h" extern char *myname; -int putstdout(); - int overstrike; int screen_length; int screen_width; @@ -60,14 +40,12 @@ char ch_erase; char ch_kill; char smart_terminal; char PC; -char *tgetstr(); -char *tgoto(); char termcap_buf[1024]; char string_buffer[1024]; char home[15]; char lower_left[15]; char *clear_line; -char *clear_screen; +char *clear_the_screen; char *clear_to_end; char *cursor_motion; char *start_standout; @@ -75,37 +53,20 @@ char *end_standout; char *terminal_init; char *terminal_end; -#ifdef SGTTY -static struct sgttyb old_settings; -static struct sgttyb new_settings; -#endif -#ifdef TERMIO -static struct termio old_settings; -static struct termio new_settings; -#endif -#ifdef TERMIOS static struct termios old_settings; static struct termios new_settings; -#endif static char is_a_terminal = No; -#ifdef TOStop -static int old_lword; -static int new_lword; -#endif #define STDIN 0 #define STDOUT 1 #define STDERR 2 -init_termcap(interactive) - -int interactive; - +void +init_termcap(int interactive) { char *bufptr; char *PCptr; char *term_name; - char *getenv(); int status; /* set defaults in case we aren't smart */ @@ -188,7 +149,7 @@ int interactive; } /* get necessary capabilities */ - if ((clear_screen = tgetstr("cl", &bufptr)) == NULL || + if ((clear_the_screen = tgetstr("cl", &bufptr)) == NULL || (cursor_motion = tgetstr("cm", &bufptr)) == NULL) { smart_terminal = No; @@ -216,85 +177,15 @@ int interactive; get_screensize(); /* if stdout is not a terminal, pretend we are a dumb terminal */ -#ifdef SGTTY - if (ioctl(STDOUT, TIOCGETP, &old_settings) == -1) - { - smart_terminal = No; - } -#endif -#ifdef TERMIO - if (ioctl(STDOUT, TCGETA, &old_settings) == -1) - { - smart_terminal = No; - } -#endif -#ifdef TERMIOS if (tcgetattr(STDOUT, &old_settings) == -1) { smart_terminal = No; } -#endif } -init_screen() - +void +init_screen(void) { - /* get the old settings for safe keeping */ -#ifdef SGTTY - if (ioctl(STDOUT, TIOCGETP, &old_settings) != -1) - { - /* copy the settings so we can modify them */ - new_settings = old_settings; - - /* turn on CBREAK and turn off character echo and tab expansion */ - new_settings.sg_flags |= CBREAK; - new_settings.sg_flags &= ~(ECHO|XTABS); - (void) ioctl(STDOUT, TIOCSETP, &new_settings); - - /* remember the erase and kill characters */ - ch_erase = old_settings.sg_erase; - ch_kill = old_settings.sg_kill; - -#ifdef TOStop - /* get the local mode word */ - (void) ioctl(STDOUT, TIOCLGET, &old_lword); - - /* modify it */ - new_lword = old_lword | LTOSTOP; - (void) ioctl(STDOUT, TIOCLSET, &new_lword); -#endif - /* remember that it really is a terminal */ - is_a_terminal = Yes; - - /* send the termcap initialization string */ - putcap(terminal_init); - } -#endif -#ifdef TERMIO - if (ioctl(STDOUT, TCGETA, &old_settings) != -1) - { - /* copy the settings so we can modify them */ - new_settings = old_settings; - - /* turn off ICANON, character echo and tab expansion */ - new_settings.c_lflag &= ~(ICANON|ECHO); - new_settings.c_oflag &= ~(TAB3); - new_settings.c_cc[VMIN] = 1; - new_settings.c_cc[VTIME] = 0; - (void) ioctl(STDOUT, TCSETA, &new_settings); - - /* remember the erase and kill characters */ - ch_erase = old_settings.c_cc[VERASE]; - ch_kill = old_settings.c_cc[VKILL]; - - /* remember that it really is a terminal */ - is_a_terminal = Yes; - - /* send the termcap initialization string */ - putcap(terminal_init); - } -#endif -#ifdef TERMIOS if (tcgetattr(STDOUT, &old_settings) != -1) { /* copy the settings so we can modify them */ @@ -302,7 +193,7 @@ init_screen() /* turn off ICANON, character echo and tab expansion */ new_settings.c_lflag &= ~(ICANON|ECHO); - new_settings.c_oflag &= ~(TAB3); + new_settings.c_oflag &= ~(OXTABS); new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings); @@ -317,7 +208,6 @@ init_screen() /* send the termcap initialization string */ putcap(terminal_init); } -#endif if (!is_a_terminal) { @@ -326,8 +216,8 @@ init_screen() } } -end_screen() - +void +end_screen(void) { /* move to the lower left, clear the line and send "te" */ if (smart_terminal) @@ -341,39 +231,17 @@ end_screen() /* if we have settings to reset, then do so */ if (is_a_terminal) { -#ifdef SGTTY - (void) ioctl(STDOUT, TIOCSETP, &old_settings); -#ifdef TOStop - (void) ioctl(STDOUT, TIOCLSET, &old_lword); -#endif -#endif -#ifdef TERMIO - (void) ioctl(STDOUT, TCSETA, &old_settings); -#endif -#ifdef TERMIOS (void) tcsetattr(STDOUT, TCSADRAIN, &old_settings); -#endif } } -reinit_screen() - +void +reinit_screen(void) { /* install our settings if it is a terminal */ if (is_a_terminal) { -#ifdef SGTTY - (void) ioctl(STDOUT, TIOCSETP, &new_settings); -#ifdef TOStop - (void) ioctl(STDOUT, TIOCLSET, &new_lword); -#endif -#endif -#ifdef TERMIO - (void) ioctl(STDOUT, TCSETA, &new_settings); -#endif -#ifdef TERMIOS (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings); -#endif } /* send init string */ @@ -383,8 +251,8 @@ reinit_screen() } } -get_screensize() - +void +get_screensize(void) { #ifdef TIOCGWINSZ @@ -428,10 +296,8 @@ get_screensize() lower_left[sizeof(lower_left) - 1] = '\0'; } -standout(msg) - -char *msg; - +void +dostandout(const char *msg) { if (smart_terminal) { @@ -445,19 +311,17 @@ char *msg; } } -clear() - +void +clear_myscreen(void) { if (smart_terminal) { - putcap(clear_screen); + putcap(clear_screen); } } -clear_eol(len) - -int len; - +int +clear_eol(int len) { if (smart_terminal && !overstrike && len > 0) { @@ -478,8 +342,8 @@ int len; return(-1); } -go_home() - +void +go_home(void) { if (smart_terminal) { @@ -489,11 +353,9 @@ go_home() /* This has to be defined as a subroutine for tputs (instead of a macro) */ -putstdout(ch) - -char ch; - +int +putstdout(int ch) { - putchar(ch); + return(putchar(ch)); } diff --git a/contrib/top/screen.h b/contrib/top/screen.h index 50105e4390..ce39c33930 100644 --- a/contrib/top/screen.h +++ b/contrib/top/screen.h @@ -10,8 +10,6 @@ #define Move_to(x, y) TCputs(tgoto(cursor_motion, x, y)) /* declare return values for termcap functions */ -char *tgetstr(); -char *tgoto(); extern char ch_erase; /* set to the user's erase character */ extern char ch_kill; /* set to the user's kill character */ @@ -28,4 +26,13 @@ extern int screen_length; extern int screen_width; /* a function that puts a single character on stdout */ -int putstdout(); +int putstdout(int ch); +void init_termcap(int interactive); +void get_screensize(void); +void init_screen(void); +void reinit_screen(void); +void end_screen(void); +void clear_myscreen(void); +int clear_eol(int len); +void go_home(void); +void dostandout(const char *msg); diff --git a/contrib/top/top.c b/contrib/top/top.c index 32cc2c8bca..958ed8db22 100644 --- a/contrib/top/top.c +++ b/contrib/top/top.c @@ -1,4 +1,4 @@ -char *copyright = +const char *copyright = "Copyright (c) 1984 through 1996, William LeFebvre"; /* @@ -36,6 +36,9 @@ char *copyright = #include "os.h" #include +#include +#include +#include #include #include #include @@ -48,6 +51,8 @@ char *copyright = #include "top.local.h" #include "boolean.h" #include "machine.h" +#include "username.h" +#include "commands.h" #include "utils.h" /* Size of the stdio buffer given to stdout */ @@ -67,11 +72,11 @@ extern char *optarg; extern int overstrike; /* signal handling routines */ -sigret_t leave(); -sigret_t onalrm(); -sigret_t tstop(); +sigret_t leave(int signo); +sigret_t onalrm(int signo); +sigret_t tstop(int signo); #ifdef SIGWINCH -sigret_t winch(); +sigret_t mywinch(int signo); #endif volatile sig_atomic_t leaveflag; @@ -79,75 +84,41 @@ volatile sig_atomic_t tstopflag; volatile sig_atomic_t winchflag; /* internal routines */ -void quit(); +void quit(int signo); /* values which need to be accessed by signal handlers */ static int max_topn; /* maximum displayable processes */ +static void reset_display(void); + /* miscellaneous things */ -char *myname = "top"; +const char *myname = "top"; jmp_buf jmp_int; -/* routines that don't return int */ - -char *username(); -char *ctime(); -char *kill_procs(); -char *renice_procs(); - #ifdef ORDER -extern int (*proc_compares[])(); +extern int (*proc_compares[])(const void *, const void *); #else -extern int proc_compare(); +extern int proc_compare(const void *, const void *); #endif -time_t time(); - -caddr_t get_process_info(); - -/* different routines for displaying the user's identification */ -/* (values assigned to get_userid) */ -char *username(); -char *itoa7(); - -/* display routines that need to be predeclared */ -int i_loadave(); -int u_loadave(); -int i_procstates(); -int u_procstates(); -int i_cpustates(struct system_info *); -int u_cpustates(struct system_info *); -int i_memory(); -int u_memory(); -int i_swap(); -int u_swap(); -int i_message(); -int u_message(); -int i_header(); -int u_header(); -int i_process(); -int u_process(); /* pointers to display routines */ -int (*d_loadave)() = i_loadave; -int (*d_procstates)() = i_procstates; -int (*d_cpustates)(struct system_info *) = i_cpustates; -int (*d_memory)() = i_memory; -int (*d_swap)() = i_swap; -int (*d_message)() = i_message; -int (*d_header)() = i_header; -int (*d_process)() = i_process; +void (*d_loadave)(int, double *) = i_loadave; +void (*d_procstates)(int, int *) = i_procstates; +void (*d_cpustates)(struct system_info *) = i_cpustates; +void (*d_memory)(int *) = i_memory; +void (*d_swap)(int *) = i_swap; +void (*d_message)(void) = i_message; +void (*d_header)(char *) = i_header; +void (*d_process)(int, char *) = i_process; int n_cpus = 0; -main(argc, argv) - -int argc; -char *argv[]; - +int +main(int argc, char **argv) { - register int i; - register int active_procs; - register int change; + int i; + int active_procs; + int change; struct system_info system_info; struct statics statics; @@ -161,8 +132,8 @@ char *argv[]; int displays = 0; /* indicates unspecified */ int sel_ret = 0; time_t curr_time; - char *(*get_userid)() = username; - char *uname_field = "USERNAME"; + char *(*get_userid)(long) = username; + const char *uname_field = "USERNAME"; char *header_text; char *env_top; char **preset_argv; @@ -179,7 +150,7 @@ char *argv[]; char ch; char *iptr; char no_command = 1; - struct timeval timeout; + struct timeval mytimeout; struct process_select ps; #ifdef ORDER char *order_name = NULL; @@ -262,7 +233,7 @@ char *argv[]; /* set the dummy argument to an explanatory message, in case getopt encounters a bad argument */ - preset_argv[0] = "while processing environment"; + preset_argv[0] = strdup("while processing environment"); } /* process options */ @@ -418,7 +389,7 @@ Usage: %s [-ISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n", if (!do_unames) { uname_field = " UID "; - get_userid = itoa7; + get_userid = ltoa7; } /* initialize the kernel memory interface */ @@ -433,7 +404,7 @@ Usage: %s [-ISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n", { if ((order_index = string_index(order_name, statics.order_names)) == -1) { - char **pp; + const char **pp; fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n", myname, order_name); @@ -528,7 +499,7 @@ Usage: %s [-ISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n", (void) signal(SIGQUIT, leave); (void) signal(SIGTSTP, tstop); #ifdef SIGWINCH - (void) signal(SIGWINCH, winch); + (void) signal(SIGWINCH, mywinch); #endif #ifdef SIGRELSE sigrelse(SIGINT); @@ -691,8 +662,8 @@ restart: /* set up arguments for select with timeout */ FD_ZERO(&readfds); FD_SET(0, &readfds); /* for standard input */ - timeout.tv_sec = delay; - timeout.tv_usec = 0; + mytimeout.tv_sec = delay; + mytimeout.tv_usec = 0; if (leaveflag) { end_screen(); @@ -733,7 +704,7 @@ restart: max_topn = display_resize(); /* reset the signal handler */ - (void) signal(SIGWINCH, winch); + (void) signal(SIGWINCH, mywinch); reset_display(); winchflag = 0; @@ -741,13 +712,13 @@ restart: } /* wait for either input or the end of the delay period */ - sel_ret = select(2, &readfds, NULL, NULL, &timeout); + sel_ret = select(2, &readfds, NULL, NULL, &mytimeout); if (sel_ret < 0 && errno != EINTR) quit(0); if (sel_ret > 0) { int newval; - char *errmsg; + const char *xerrmsg; /* something to read -- clear the message area first */ clear_message(); @@ -807,9 +778,9 @@ restart: case CMD_help1: /* help */ case CMD_help2: reset_display(); - clear(); + clear_myscreen(); show_help(); - standout("Hit any key to continue: "); + dostandout("Hit any key to continue: "); fflush(stdout); (void) read(0, &ch, 1); break; @@ -825,9 +796,9 @@ restart: else { reset_display(); - clear(); + clear_myscreen(); show_errors(); - standout("Hit any key to continue: "); + dostandout("Hit any key to continue: "); fflush(stdout); (void) read(0, &ch, 1); } @@ -879,7 +850,7 @@ restart: new_message(MT_standout, "Displays to show (currently %s): ", displays == -1 ? "infinite" : - itoa(displays)); + ltoa(displays)); if ((i = readline(tempbuf1, 10, Yes)) > 0) { displays = i; @@ -895,9 +866,9 @@ restart: new_message(0, "kill "); if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { - if ((errmsg = kill_procs(tempbuf2)) != NULL) + if ((xerrmsg = kill_procs(tempbuf2)) != NULL) { - new_message(MT_standout, "%s", errmsg); + new_message(MT_standout, "%s", xerrmsg); putchar('\r'); no_command = Yes; } @@ -912,9 +883,9 @@ restart: new_message(0, "renice "); if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { - if ((errmsg = renice_procs(tempbuf2)) != NULL) + if ((xerrmsg = renice_procs(tempbuf2)) != NULL) { - new_message(MT_standout, "%s", errmsg); + new_message(MT_standout, "%s", xerrmsg); putchar('\r'); no_command = Yes; } @@ -1038,6 +1009,7 @@ restart: #endif quit(0); /*NOTREACHED*/ + return(0); } /* @@ -1045,8 +1017,8 @@ restart: * screen will get redrawn. */ -reset_display() - +static void +reset_display(void) { d_loadave = i_loadave; d_procstates = i_procstates; @@ -1062,41 +1034,37 @@ reset_display() * signal handlers */ -sigret_t leave() /* exit under normal conditions -- INT handler */ - +/* exit under normal conditions -- INT handler */ +sigret_t +leave(int signo __unused) { leaveflag = 1; } -sigret_t tstop(i) /* SIGTSTP handler */ - -int i; - +sigret_t +tstop(int signo __unused) /* SIGTSTP handler */ { tstopflag = 1; } #ifdef SIGWINCH -sigret_t winch(i) /* SIGWINCH handler */ - -int i; - +sigret_t +mywinch(int signo __unused) /* SIGWINCH handler */ { winchflag = 1; } #endif -void quit(status) /* exit under duress */ - -int status; - +void +quit(int status) /* exit under duress */ { end_screen(); exit(status); /*NOTREACHED*/ } -sigret_t onalrm() /* SIGALRM handler */ +sigret_t +onalrm(int signo __unused) /* SIGALRM handler */ { /* this is only used in batch mode to break out of the pause() */ diff --git a/contrib/top/top.h b/contrib/top/top.h index d979b70b4a..78b2e571a9 100644 --- a/contrib/top/top.h +++ b/contrib/top/top.h @@ -19,10 +19,7 @@ extern int n_cpus; extern int screen_width; -char *itoa(); -char *itoa7(); - -char *version_string(); +char *version_string(void); /* Special atoi routine returns either a non-negative number or one of: */ #define Infinity -1 @@ -36,4 +33,3 @@ char *version_string(); */ #define NUM_AVERAGES 3 - diff --git a/contrib/top/username.c b/contrib/top/username.c index 4d49cc44b8..d58bc52278 100644 --- a/contrib/top/username.c +++ b/contrib/top/username.c @@ -33,17 +33,22 @@ #include #include +#include +#include #include #include #include "top.local.h" #include "utils.h" +#include "username.h" struct hash_el { int uid; char name[UT_NAMESIZE + 1]; }; +static int get_user(int uid); + #define is_empty_hash(x) (hash_table[x].name[0] == 0) /* simple minded hashing function */ @@ -56,8 +61,8 @@ struct hash_el { /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ struct hash_el hash_table[Table_size]; -init_hash() - +void +init_hash(void) { /* * There used to be some steps we had to take to initialize things. @@ -66,12 +71,10 @@ init_hash() */ } -char *username(uid) - -register int uid; - +char * +username(long uid) { - register int hashindex; + int hashindex; hashindex = hashit(uid); if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) @@ -82,10 +85,8 @@ register int uid; return(hash_table[hashindex].name); } -int userid(username) - -char *username; - +int +userid(char *xusername) { struct passwd *pwd; @@ -93,26 +94,22 @@ char *username; but for now we just do it simply and remember just the result. */ - if ((pwd = getpwnam(username)) == NULL) + if ((pwd = getpwnam(xusername)) == NULL) { return(-1); } /* enter the result in the hash table */ - enter_user(pwd->pw_uid, username, 1); + enter_user(pwd->pw_uid, xusername, 1); /* return our result */ return(pwd->pw_uid); } -int enter_user(uid, name, wecare) - -register int uid; -register char *name; -int wecare; /* 1 = enter it always, 0 = nice to have */ - +int +enter_user(int uid, char *name, int wecare) { - register int hashindex; + int hashindex; #ifdef DEBUG fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare); @@ -141,10 +138,8 @@ int wecare; /* 1 = enter it always, 0 = nice to have */ * and cache any entries we pass over while looking. */ -int get_user(uid) - -register int uid; - +static int +get_user(int uid) { struct passwd *pwd; @@ -186,5 +181,5 @@ register int uid; } #endif /* if we can't find the name at all, then use the uid as the name */ - return(enter_user(uid, itoa7(uid), 1)); + return(enter_user(uid, ltoa7(uid), 1)); } diff --git a/contrib/top/username.h b/contrib/top/username.h new file mode 100644 index 0000000000..7001734d1b --- /dev/null +++ b/contrib/top/username.h @@ -0,0 +1,8 @@ +/* + * + */ + +void init_hash(void); +char *username(long); +int userid(char *username); +int enter_user(int uid, char *name, int wecare); diff --git a/contrib/top/utils.c b/contrib/top/utils.c index ab0a33db5e..1c429fcd3f 100644 --- a/contrib/top/utils.c +++ b/contrib/top/utils.c @@ -18,13 +18,12 @@ #include "top.h" #include "os.h" +#include "utils.h" -int atoiwi(str) - -char *str; - +int +atoiwi(const char *str) { - register int len; + int len; len = strlen(str); if (len != 0) @@ -48,28 +47,15 @@ char *str; } /* - * itoa - convert integer (decimal) to ascii string for positive numbers + * ltoa - convert integer (decimal) to ascii string for positive numbers * only (we don't bother with negative numbers since we know we * don't use them). */ - - /* - * How do we know that 16 will suffice? - * Because the biggest number that we will - * ever convert will be 2^32-1, which is 10 - * digits. - */ - -char *itoa(val) - -register int val; - +char * +ltoa(long val) { - register char *ptr; - static char buffer[16]; /* result is built here */ - /* 16 is sufficient since the largest number - we will ever convert will be 2^32-1, - which is 10 digits. */ + char *ptr; + static char buffer[32]; ptr = buffer + sizeof(buffer); *--ptr = '\0'; @@ -86,18 +72,16 @@ register int val; } /* - * itoa7(val) - like itoa, except the number is right justified in a 7 - * character field. This code is a duplication of itoa instead of + * ltoa7(val) - like ltoa, except the number is right justified in a 7 + * character field. This code is a duplication of ltoa instead of * a front end to a more general routine for efficiency. */ -char *itoa7(val) - -register int val; - +char * +ltoa7(long val) { - register char *ptr; - static char buffer[16]; /* result is built here */ + char *ptr; + static char buffer[32]; /* result is built here */ /* 16 is sufficient since the largest number we will ever convert will be 2^32-1, which is 10 digits. */ @@ -125,12 +109,10 @@ register int val; * positive numbers. If val <= 0 then digits(val) == 0. */ -int digits(val) - -int val; - +int +digits(long val) { - register int cnt = 0; + int cnt = 0; while (val > 0) { @@ -145,11 +127,8 @@ int val; * to the END of the string "to". */ -char *strecpy(to, from) - -register char *to; -register char *from; - +char * +strecpy(char *to, const char *from) { while ((*to++ = *from++) != '\0'); return(--to); @@ -159,11 +138,8 @@ register char *from; * string_index(string, array) - find string in array and return index */ -int string_index(string, array) - -char *string; -char **array; - +int +string_index(char *string, const char **array) { register int i = 0; @@ -186,16 +162,13 @@ char **array; * squat about quotes. */ -char **argparse(line, cntp) - -char *line; -int *cntp; - +char ** +argparse(char *line, int *cntp) { - register char *from; - register char *to; - register int cnt; - register int ch; + char *from; + char *to; + int cnt; + int ch; int length; int lastch; register char **argv; @@ -271,14 +244,8 @@ int *cntp; * useful on BSD mchines for calculating cpu state percentages. */ -long percentages(cnt, out, new, old, diffs) - -int cnt; -int *out; -register long *new; -register long *old; -long *diffs; - +long +percentages(int cnt, int *out, long *new, long *old, long *diffs) { register int i; register long change; @@ -346,10 +313,8 @@ extern char *sys_errlist[]; extern int sys_nerr; #endif -char *errmsg(errnum) - -int errnum; - +const char * +errmsg(int errnum) { #ifdef HAVE_STRERROR char *msg = strerror(errnum); @@ -382,14 +347,8 @@ int errnum; exceed 9999.9, we use "???". */ -char *format_time(seconds) - -long seconds; - +char *format_time(long seconds) { - register int value; - register int digit; - register char *ptr; static char result[10]; /* sanity protection */ @@ -412,7 +371,7 @@ long seconds; { /* standard method produces MMM:SS */ /* we avoid printf as must as possible to make this quick */ - sprintf(result, "%3ld:%02ld", + snprintf(result, sizeof(result), "%3ld:%02ld", (long)(seconds / 60), (long)(seconds % 60)); } return(result); @@ -441,21 +400,19 @@ long seconds; * to convert the modulo operation into something quicker. What a hack! */ -#define NUM_STRINGS 8 - -char *format_k(amt) - -int amt; +#define NUM_STRINGS 16 +char * +format_k(long amt) { static char retarray[NUM_STRINGS][16]; - static int index = 0; - register char *p; - register char *ret; - register char tag = 'K'; + static int xindex = 0; + char *p; + char *ret; + char tag = 'K'; - p = ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; + p = ret = retarray[xindex]; + xindex = (xindex + 1) % NUM_STRINGS; if (amt >= 10000) { @@ -468,26 +425,24 @@ int amt; } } - p = strecpy(p, itoa(amt)); + p = strecpy(p, ltoa(amt)); *p++ = tag; *p = '\0'; return(ret); } -char *format_k2(amt) - -int amt; - +char * +format_k2(long amt) { - static char retarray[NUM_STRINGS][16]; - static int index = 0; - register char *p; - register char *ret; - register char tag = 'K'; + static char retarray[NUM_STRINGS][32]; + static int xindex = 0; + char *p; + char *ret; + char tag = 'K'; - p = ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; + p = ret = retarray[xindex]; + xindex = (xindex + 1) % NUM_STRINGS; if (amt >= 100000) { @@ -499,8 +454,7 @@ int amt; tag = 'G'; } } - - p = strecpy(p, itoa(amt)); + p = strecpy(p, ltoa(amt)); *p++ = tag; *p = '\0'; diff --git a/contrib/top/utils.h b/contrib/top/utils.h index 6717092002..b5eefc64ce 100644 --- a/contrib/top/utils.h +++ b/contrib/top/utils.h @@ -11,14 +11,30 @@ /* prototypes for functions found in utils.c */ -int atoiwi(); -char *itoa(); -char *itoa7(); -int digits(); -char *strecpy(); -char **argparse(); -long percentages(); -char *errmsg(); -char *format_time(); -char *format_k(); -char *format_k2(); +int atoiwi(const char *); +char *ltoa(long); +char *ltoa7(long); +int digits(long); +char *strecpy(char *, const char *); +char **argparse(char *, int *); +long percentages(int cnt, int *out, long *new, long *old, long *diffs); +const char *errmsg(int); +char *format_time(long); +char *format_k(long); +char *format_k2(long); +int string_index(char *string, const char **array); + +struct proc; +struct process_select; +struct system_info; +#ifdef ORDER +extern int (*proc_compares[])(const void *, const void *); +extern int compare_cpu(const void *, const void *); +#else +extern int proc_compare(const void *, const void *); +#endif +int proc_owner(int pid); +caddr_t get_process_info(struct system_info *si, struct process_select *sel, + int (*compare)(const void *, const void *)); +void get_system_info(struct system_info *si); +void quit(int); diff --git a/contrib/top/version.c b/contrib/top/version.c index 5f360fd32f..59709f0c02 100644 --- a/contrib/top/version.c +++ b/contrib/top/version.c @@ -9,13 +9,15 @@ * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University */ +#include +#include #include "top.h" #include "patchlevel.h" static char version[16]; -char *version_string() - +char * +version_string(void) { sprintf(version, "%d.%d", VERSION, PATCHLEVEL); #ifdef BETA diff --git a/usr.bin/top/Makefile b/usr.bin/top/Makefile index 02e5bd552f..ee1fbe84fe 100644 --- a/usr.bin/top/Makefile +++ b/usr.bin/top/Makefile @@ -1,6 +1,7 @@ # $FreeBSD: src/usr.bin/top/Makefile,v 1.5.6.2 2002/08/11 17:09:54 dwmalone Exp $ # $DragonFly: src/usr.bin/top/Makefile,v 1.8 2007/08/27 16:50:59 pavalos Exp $ PROG= top +WARNS?= 4 TOPDIR= ${.CURDIR}/../../contrib/top .PATH: ${TOPDIR} diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 2ece09449c..89e14df46a 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,7 @@ /* Swap */ #include +#include #include #include /* for changes in kernel structures */ @@ -55,9 +57,12 @@ #include #include "top.h" #include "machine.h" +#include "utils.h" +#if 0 static int check_nlist(struct nlist *); static int getkval(unsigned long, int *, int, char *); +#endif extern char* printable(char *); int swapmode(int *retavail, int *retfree); static int smpmode; @@ -114,7 +119,7 @@ static char up_header[] = /* the extra nulls in the string "run" are for adding a slash and the processor number when needed */ -char *state_abbrev[] = +const char *state_abbrev[] = { "", "RUN\0\0\0", "STOP", "SLEEP", }; @@ -127,7 +132,6 @@ static kvm_t *kd; static double logcpu; static long lastpid; -static long cnt; static int ccpu; /* these are for calculating cpu state percentages */ @@ -137,7 +141,7 @@ static struct kinfo_cputime *cp_time, *cp_old; /* these are for detailing the process states */ int process_states[6]; -char *procstatenames[] = { +const char *procstatenames[] = { "", " starting, ", " running, ", " sleeping, ", " stopped, ", " zombie, ", NULL @@ -146,20 +150,20 @@ char *procstatenames[] = { /* these are for detailing the cpu states */ #define CPU_STATES 5 int *cpu_states; -char *cpustatenames[CPU_STATES + 1] = { +const char *cpustatenames[CPU_STATES + 1] = { "user", "nice", "system", "interrupt", "idle", NULL }; /* these are for detailing the memory statistics */ int memory_stats[7]; -char *memorynames[] = { +const char *memorynames[] = { "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", NULL }; int swap_stats[7]; -char *swapnames[] = { +const char *swapnames[] = { /* 0 1 2 3 4 5 */ "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", NULL @@ -184,7 +188,7 @@ static int pageshift; /* log base 2 of the pagesize */ #ifdef ORDER /* sorting orders. first is default */ -char *ordernames[] = { +const char *ordernames[] = { "cpu", "size", "res", "time", "pri", "thr", NULL }; #endif @@ -229,7 +233,6 @@ cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new, int machine_init(struct statics *statics) { - int i = 0; int pagesize; size_t modelen; struct passwd *pw; @@ -245,7 +248,7 @@ machine_init(struct statics *statics) smpmode = 0; while ((pw = getpwent()) != NULL) { - if (strlen(pw->pw_name) > namelength) + if ((int)strlen(pw->pw_name) > namelength) namelength = strlen(pw->pw_name); } if (namelength < 8) @@ -295,9 +298,9 @@ machine_init(struct statics *statics) return(0); } -char *format_header(char *uname_field) +char * +format_header(const char *uname_field) { - char *ptr; static char Header[128]; snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, @@ -320,7 +323,6 @@ extern struct timeval timeout; void get_system_info(struct system_info *si) { - long total; int mib[2]; struct timeval boottime; size_t bt_size; @@ -445,7 +447,7 @@ get_system_info(struct system_info *si) static struct handle handle; caddr_t get_process_info(struct system_info *si, struct process_select *sel, - int (*compare)()) + int (*compare)(const void *, const void *)) { int i; int total_procs; @@ -532,7 +534,8 @@ caddr_t get_process_info(struct system_info *si, struct process_select *sel, char fmt[128]; /* static area where result is built */ -char *format_next_process(caddr_t handle, char *(*get_userid)()) +char * +format_next_process(caddr_t xhandle, char *(*get_userid)(long)) { struct kinfo_proc *pp; long cputime; @@ -541,10 +544,10 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) char status[16]; char const *wrapper; int state; - int nice; + int xnice; /* find and remember the next proc structure */ - hp = (struct handle *)handle; + hp = (struct handle *)xhandle; pp = *(hp->next_proc++); hp->remaining--; @@ -598,7 +601,7 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) default: if (state >= 0 && - state < sizeof(state_abbrev) / sizeof(*state_abbrev)) + (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev)) sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); else sprintf(status, "?%5d", state); @@ -616,33 +619,32 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) */ switch(LP(pp, rtprio.type)) { case RTP_PRIO_REALTIME: - nice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio); + xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio); break; case RTP_PRIO_IDLE: - nice = PRIO_MAX + 1 + LP(pp, rtprio.prio); + xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio); break; case RTP_PRIO_THREAD: - nice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio); + xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio); break; default: - nice = PP(pp, nice); + xnice = PP(pp, nice); break; } - /* format this entry */ snprintf(fmt, sizeof(fmt), smpmode ? smp_Proc_format : up_Proc_format, - PP(pp, pid), + (int)PP(pp, pid), namelength, namelength, - (*get_userid)(PP(pp, ruid)), - (show_threads && (LP(pp, pid) == -1)) ? LP(pp, tdprio) : - LP(pp, prio), - nice, + get_userid(PP(pp, ruid)), + (int)((show_threads && (LP(pp, pid) == -1)) ? + LP(pp, tdprio) : LP(pp, prio)), + (int)xnice, format_k2(PROCSIZE(pp)), format_k2(pagetok(VP(pp, rssize))), status, - smpmode ? LP(pp, cpuid) : 0, + (int)(smpmode ? LP(pp, cpuid) : 0), format_time(cputime), 100.0 * weighted_cpu(pct, pp), 100.0 * pct, @@ -653,15 +655,15 @@ char *format_next_process(caddr_t handle, char *(*get_userid)()) return(fmt); } - +#if 0 /* * check_nlist(nlst) - checks the nlist to see if any symbols were not * found. For every symbol that was not found, a one-line * message is printed to stderr. The routine returns the * number of symbols NOT found. */ - -static int check_nlist(struct nlist *nlst) +static int +check_nlist(struct nlist *nlst) { int i; @@ -683,6 +685,7 @@ static int check_nlist(struct nlist *nlst) return(i); } +#endif /* comparison routines for qsort */ @@ -742,19 +745,21 @@ static unsigned char sorted_state[] = int #ifdef ORDER -compare_cpu(struct proc **pp1, struct proc **pp2) +compare_cpu(const void *arg1, const void *arg2) #else -proc_compare(struct proc **pp1, struct proc **pp2) +proc_compare(const void *arg1, const void *arg2) #endif { - struct kinfo_proc *p1; - struct kinfo_proc *p2; + const struct proc *const*pp1 = arg1; + const struct proc *const*pp2 = arg2; + const struct kinfo_proc *p1; + const struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(const struct kinfo_proc *const *) pp1; + p2 = *(const struct kinfo_proc *const *) pp2; ORDERKEY_PCTCPU ORDERKEY_CPTICKS @@ -769,9 +774,13 @@ proc_compare(struct proc **pp1, struct proc **pp2) #ifdef ORDER /* compare routines */ -int compare_size(), compare_res(), compare_time(), compare_prio(), compare_thr(); +int compare_size(const void *, const void *); +int compare_res(const void *, const void *); +int compare_time(const void *, const void *); +int compare_prio(const void *, const void *); +int compare_thr(const void *, const void *); -int (*proc_compares[])() = { +int (*proc_compares[])(const void *, const void *) = { compare_cpu, compare_size, compare_res, @@ -784,16 +793,18 @@ int (*proc_compares[])() = { /* compare_size - the comparison function for sorting by total memory usage */ int -compare_size(struct proc **pp1, struct proc **pp2) +compare_size(const void *arg1, const void *arg2) { + struct proc *const *pp1 = arg1; + struct proc *const *pp2 = arg2; struct kinfo_proc *p1; struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(struct kinfo_proc *const*) pp1; + p2 = *(struct kinfo_proc *const*) pp2; ORDERKEY_MEM ORDERKEY_RSSIZE @@ -809,16 +820,18 @@ compare_size(struct proc **pp1, struct proc **pp2) /* compare_res - the comparison function for sorting by resident set size */ int -compare_res(struct proc **pp1, struct proc **pp2) +compare_res(const void *arg1, const void *arg2) { + struct proc *const *pp1 = arg1; + struct proc *const *pp2 = arg2; struct kinfo_proc *p1; struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(struct kinfo_proc *const*) pp1; + p2 = *(struct kinfo_proc *const*) pp2; ORDERKEY_RSSIZE ORDERKEY_MEM @@ -834,16 +847,18 @@ compare_res(struct proc **pp1, struct proc **pp2) /* compare_time - the comparison function for sorting by total cpu time */ int -compare_time(struct proc **pp1, struct proc **pp2) +compare_time(const void *arg1, const void *arg2) { - struct kinfo_proc *p1; - struct kinfo_proc *p2; + struct proc *const *pp1 = arg1; + struct proc *const *pp2 = arg2; + const struct kinfo_proc *p1; + const struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(struct kinfo_proc *const*) pp1; + p2 = *(struct kinfo_proc *const*) pp2; ORDERKEY_CPTICKS ORDERKEY_PCTCPU @@ -861,16 +876,18 @@ compare_time(struct proc **pp1, struct proc **pp2) /* compare_prio - the comparison function for sorting by cpu percentage */ int -compare_prio(struct proc **pp1, struct proc **pp2) +compare_prio(const void *arg1, const void *arg2) { - struct kinfo_proc *p1; - struct kinfo_proc *p2; + struct proc *const *pp1 = arg1; + struct proc *const *pp2 = arg2; + const struct kinfo_proc *p1; + const struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(struct kinfo_proc *const*) pp1; + p2 = *(struct kinfo_proc *const*) pp2; ORDERKEY_KTHREADS ORDERKEY_KTHREADS_PRIO @@ -886,16 +903,18 @@ compare_prio(struct proc **pp1, struct proc **pp2) } int -compare_thr(struct proc **pp1, struct proc **pp2) +compare_thr(const void *arg1, const void *arg2) { - struct kinfo_proc *p1; - struct kinfo_proc *p2; + struct proc *const *pp1 = arg1; + struct proc *const *pp2 = arg2; + const struct kinfo_proc *p1; + const struct kinfo_proc *p2; int result; pctcpu lresult; /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; + p1 = *(struct kinfo_proc *const*) pp1; + p2 = *(struct kinfo_proc *const*) pp2; ORDERKEY_KTHREADS ORDERKEY_KTHREADS_PRIO @@ -922,15 +941,16 @@ compare_thr(struct proc **pp1, struct proc **pp2) * and "renice" commands. */ -int proc_owner(int pid) +int +proc_owner(int pid) { - int cnt; + int xcnt; struct kinfo_proc **prefp; struct kinfo_proc *pp; prefp = pref; - cnt = pref_len; - while (--cnt >= 0) + xcnt = pref_len; + while (--xcnt >= 0) { pp = *prefp++; if (PP(pp, pid) == (pid_t)pid) diff --git a/usr.bin/top/sigdesc.h b/usr.bin/top/sigdesc.h index 75bc9d7b2f..ddfc3685c9 100644 --- a/usr.bin/top/sigdesc.h +++ b/usr.bin/top/sigdesc.h @@ -2,41 +2,41 @@ /* by the awk script "sigconv.awk". */ struct sigdesc { - char *name; + const char *name; int number; }; struct sigdesc sigdesc[] = { - "HUP", 1, - "INT", 2, - "QUIT", 3, - "ILL", 4, - "TRAP", 5, - "ABRT", 6, - "EMT", 7, - "FPE", 8, - "KILL", 9, - "BUS", 10, - "SEGV", 11, - "SYS", 12, - "PIPE", 13, - "ALRM", 14, - "TERM", 15, - "URG", 16, - "STOP", 17, - "TSTP", 18, - "CONT", 19, - "CHLD", 20, - "TTIN", 21, - "TTOU", 22, - "IO", 23, - "XCPU", 24, - "XFSZ", 25, - "VTALRM", 26, - "PROF", 27, - "WINCH", 28, - "INFO", 29, - "USR1", 30, - "USR2", 31, - NULL, 0 + { "HUP", 1 }, + { "INT", 2 }, + { "QUIT", 3 }, + { "ILL", 4 }, + { "TRAP", 5 }, + { "ABRT", 6 }, + { "EMT", 7 }, + { "FPE", 8 }, + { "KILL", 9 }, + { "BUS", 10 }, + { "SEGV", 11 }, + { "SYS", 12 }, + { "PIPE", 13 }, + { "ALRM", 14 }, + { "TERM", 15 }, + { "URG", 16 }, + { "STOP", 17 }, + { "TSTP", 18 }, + { "CONT", 19 }, + { "CHLD", 20 }, + { "TTIN", 21 }, + { "TTOU", 22 }, + { "IO", 23 }, + { "XCPU", 24 }, + { "XFSZ", 25 }, + { "VTALRM", 26 }, + { "PROF", 27 }, + { "WINCH", 28 }, + { "INFO", 29 }, + { "USR1", 30 }, + { "USR2", 31 }, + { NULL, 0 } };