diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2003-06-13 21:52:26 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2003-06-13 21:52:26 +0000 |
commit | b3452ccaf4e58c11850874ba3378643d8201e4d4 (patch) | |
tree | c9d0af82a16c3790be815e9816424b626275fda0 /usr.bin | |
parent | 5ffa281b51de652157eac5561f8fda39295e69b5 (diff) |
readable code
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/top/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/top/commands.c | 558 | ||||
-rw-r--r-- | usr.bin/top/display.c | 1493 | ||||
-rw-r--r-- | usr.bin/top/machine.c | 117 | ||||
-rw-r--r-- | usr.bin/top/machine.h | 68 | ||||
-rw-r--r-- | usr.bin/top/screen.c | 8 | ||||
-rw-r--r-- | usr.bin/top/top.c | 1467 | ||||
-rw-r--r-- | usr.bin/top/top.h | 3 | ||||
-rw-r--r-- | usr.bin/top/username.c | 13 |
9 files changed, 1635 insertions, 2096 deletions
diff --git a/usr.bin/top/Makefile b/usr.bin/top/Makefile index 432cff31dd0..c93c83e13ce 100644 --- a/usr.bin/top/Makefile +++ b/usr.bin/top/Makefile @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile,v 1.9 2003/05/29 17:34:25 deraadt Exp $ +# $OpenBSD: Makefile,v 1.10 2003/06/13 21:52:24 deraadt Exp $ # # Makefile for OpenBSD top-3.4. PROG= top -CFLAGS+=-I. -DORDER +CFLAGS+=-I. SRCS= commands.c display.c machine.c screen.c top.c username.c utils.c \ version.c DPADD= ${LIBCURSES} ${LIBM} diff --git a/usr.bin/top/commands.c b/usr.bin/top/commands.c index b587a022c15..777ed5b02c0 100644 --- a/usr.bin/top/commands.c +++ b/usr.bin/top/commands.c @@ -1,4 +1,4 @@ -/* $OpenBSD: commands.c,v 1.8 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: commands.c,v 1.9 2003/06/13 21:52:24 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -53,108 +53,96 @@ #include "utils.h" #include "machine.h" -static char *next_field(char *); -static int scanint(char *, int *); -static char *err_string(void); -static size_t str_adderr(char *, size_t, int); -static size_t str_addarg(char *, size_t, char *, int); -static int err_compar(const void *, const void *); +static char *next_field(char *); +static int scanint(char *, int *); +static char *err_string(void); +static size_t str_adderr(char *, size_t, int); +static size_t str_addarg(char *, size_t, char *, int); +static int err_compar(const void *, const void *); /* * show_help() - display the help screen; invoked in response to * either 'h' or '?'. */ - void show_help(void) { - printf("Top version %s, %s\n", version_string(), copyright); - fputs("\n\n\ -A top users display for Unix\n\ -\n\ -These single-character commands are available:\n\ -\n\ -^L - redraw screen\n\ -q - quit\n\ -h or ? - help; show this text\n", stdout); - - /* not all commands are availalbe with overstrike terminals */ - if (overstrike) - { - fputs("\n\ -Other commands are also available, but this terminal is not\n\ -sophisticated enough to handle those commands gracefully.\n\n", stdout); - } - else - { - fputs("\ -d - change number of displays to show\n\ -e - list errors generated by last \"kill\" or \"renice\" command\n\ -i - toggle the displaying of idle processes\n\ -I - same as 'i'\n\ -k - kill processes; send a signal to a list of processes\n\ -n or # - change number of processes to display\n", stdout); + printf("Top version %s, %s\n", version_string(), copyright); + fputs("\n\n" + "A top users display for Unix\n" + "\n" + "These single-character commands are available:\n" + "\n" + "^L - redraw screen\n" + "q - quit\n" + "h or ? - help; show this text\n", stdout); + + /* not all commands are available with overstrike terminals */ + if (overstrike) { + fputs("\n" + "Other commands are also available, but this terminal is not\n" + "sophisticated enough to handle those commands gracefully.\n\n", + stdout); + } else { + fputs( + "d - change number of displays to show\n" + "e - list errors generated by last \"kill\" or \"renice\" command\n" + "i - toggle the displaying of idle processes\n" + "I - same as 'i'\n" + "k - kill processes; send a signal to a list of processes\n" + "n or # - change number of processes to display\n", stdout); #ifdef ORDER - fputs("\ -o - specify sort order (size, res, cpu, time)\n", stdout); + fputs( + "o - specify sort order (size, res, cpu, time)\n", + stdout); #endif - fputs("\ -r - renice a process\n\ -s - change number of seconds to delay between updates\n\ -u - display processes for only one user (+ selects all users)\n\ -\n\ -\n", stdout); - } + fputs( + "r - renice a process\n" + "s - change number of seconds to delay between updates\n" + "u - display processes for only one user (+ selects all users)\n" + "\n\n", stdout); + } } /* * Utility routines that help with some of the commands. */ - static char * next_field(char *str) { - if ((str = strchr(str, ' ')) == NULL) - { - return(NULL); - } - *str = '\0'; - while (*++str == ' ') /* loop */; - - /* if there is nothing left of the string, return NULL */ - /* This fix is dedicated to Greg Earle */ - return (*str == '\0' ? NULL : str); + if ((str = strchr(str, ' ')) == NULL) + return (NULL); + + *str = '\0'; + while (*++str == ' ') /* loop */ + ; + + /* if there is nothing left of the string, return NULL */ + /* This fix is dedicated to Greg Earle */ + return (*str == '\0' ? NULL : str); } -static int scanint(char *str, int *intp) +static int +scanint(char *str, int *intp) { - int val = 0; - char ch; - - /* if there is nothing left of the string, flag it as an error */ - /* This fix is dedicated to Greg Earle */ - if (*str == '\0') - { - return(-1); - } - - while ((ch = *str++) != '\0') - { - if (isdigit(ch)) - { - val = val * 10 + (ch - '0'); - } - else if (isspace(ch)) - { - break; + int val = 0; + char ch; + + /* if there is nothing left of the string, flag it as an error */ + /* This fix is dedicated to Greg Earle */ + if (*str == '\0') + return (-1); + + while ((ch = *str++) != '\0') { + if (isdigit(ch)) + val = val * 10 + (ch - '0'); + else if (isspace(ch)) + break; + else + return (-1); } - else - { - return(-1); - } - } - *intp = val; - return(0); + *intp = val; + return (0); } /* @@ -168,29 +156,28 @@ static int scanint(char *str, int *intp) #define ERRMAX 20 -struct errs /* structure for a system-call error */ -{ - int errno; /* value of errno (that is, the actual error) */ - char *arg; /* argument that caused the error */ +struct errs { /* structure for a system-call error */ + int errno; /* value of errno (that is, the actual error) */ + char *arg; /* argument that caused the error */ }; static struct errs errs[ERRMAX]; -static int errcnt; -static char *err_toomany = " too many errors occurred"; -static char *err_listem = +static int errcnt; +static char *err_toomany = " too many errors occurred"; +static char *err_listem = " Many errors occurred. Press `e' to display the list of errors."; /* These macros get used to reset and log the errors */ #define ERR_RESET errcnt = 0 -#define ERROR(p, e) if (errcnt >= ERRMAX) \ - { \ - return(err_toomany); \ - } \ - else \ - { \ - errs[errcnt].arg = (p); \ - errs[errcnt++].errno = (e); \ - } +#define ERROR(p, e) \ + if (errcnt >= ERRMAX) { \ + return(err_toomany); \ + } else { \ + errs[errcnt].arg = (p); \ + errs[errcnt++].errno = (e); \ + } + +#define STRMAX 80 /* * err_string() - return an appropriate error string. This is what the @@ -198,83 +185,70 @@ static char *err_listem = * return NULL. The maximum length of the error string is defined by * "STRMAX". */ - -#define STRMAX 80 - static char * err_string(void) { - struct errs *errp; - int cnt = 0; - int first = Yes; - int currerr = -1; - static char string[STRMAX]; - - /* if there are no errors, return NULL */ - if (errcnt == 0) - { - return(NULL); - } - - /* sort the errors */ - qsort((char *)errs, errcnt, sizeof(struct errs), err_compar); - - /* need a space at the front of the error string */ - string[0] = ' '; - string[1] = '\0'; - - /* loop thru the sorted list, building an error string */ - while (cnt < errcnt) - { - errp = &(errs[cnt++]); - if (errp->errno != currerr) - { - if (currerr != -1) - { - if (str_adderr(string, sizeof string, currerr) > - (sizeof string - 2)) - { - return(err_listem); + int cnt = 0, first = Yes, currerr = -1; + static char string[STRMAX]; + struct errs *errp; + + /* if there are no errors, return NULL */ + if (errcnt == 0) + return (NULL); + + /* sort the errors */ + qsort((char *) errs, errcnt, sizeof(struct errs), err_compar); + + /* need a space at the front of the error string */ + string[0] = ' '; + string[1] = '\0'; + + /* loop thru the sorted list, building an error string */ + while (cnt < errcnt) { + errp = &(errs[cnt++]); + if (errp->errno != currerr) { + if (currerr != -1) { + if (str_adderr(string, sizeof string, currerr) > + sizeof string - 2) + return (err_listem); + + /* we know there's more */ + (void) strlcat(string, "; ", sizeof string); + } + currerr = errp->errno; + first = Yes; } - /* we know there's more */ - (void) strlcat(string, "; ", sizeof string); - } - currerr = errp->errno; - first = Yes; - } - if (str_addarg(string, sizeof string, errp->arg, first) >= - sizeof string) - { - return(err_listem); + if (str_addarg(string, sizeof string, errp->arg, first) >= + sizeof string) + return (err_listem); + + first = No; } - first = No; - } - /* add final message */ - if (str_adderr(string, sizeof string, currerr) >= sizeof string) - return(err_listem); + /* add final message */ + if (str_adderr(string, sizeof string, currerr) >= sizeof string) + return (err_listem); - /* return the error string */ - return(string); + /* return the error string */ + return (string); } /* * str_adderr(str, len, err) - add an explanation of error "err" to * the string "str". */ - static size_t str_adderr(char *str, size_t len, int err) { - char *msg; - size_t msglen; + size_t msglen; + char *msg; - msg = err == 0 ? "Not a number" : strerror(err); + msg = err == 0 ? "Not a number" : strerror(err); - if ((msglen = strlcat(str, ": ", len)) >= len) - return(msglen); + if ((msglen = strlcat(str, ": ", len)) >= len) + return (msglen); - return(strlcat(str, msg, len)); + return (strlcat(str, msg, len)); } /* @@ -282,222 +256,172 @@ str_adderr(char *str, size_t len, 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). */ - static size_t str_addarg(char *str, size_t len, char *arg, int first) { - size_t msglen; - - if (!first) - { - if ((msglen = strlcat(str, ", ", len)) >= len) - return(msglen); - } + size_t msglen; - return (strlcat(str, arg, len)); + if (!first) { + if ((msglen = strlcat(str, ", ", len)) >= len) + return (msglen); + } + return (strlcat(str, arg, len)); } /* * err_compar(p1, p2) - comparison routine used by "qsort" * for sorting errors. */ - static int err_compar(const void *e1, const void *e2) { - const struct errs *p1 = (struct errs *)e1; - const struct errs *p2 = (struct errs *)e2; - int result; - - if ((result = p1->errno - p2->errno) == 0) - { - return(strcmp(p1->arg, p2->arg)); - } - return(result); + const struct errs *p1 = (struct errs *) e1; + const struct errs *p2 = (struct errs *) e2; + int result; + + if ((result = p1->errno - p2->errno) == 0) + return (strcmp(p1->arg, p2->arg)); + return (result); } /* * error_count() - return the number of errors currently logged. */ - int error_count(void) { - return(errcnt); + return (errcnt); } /* * show_errors() - display on stdout the current log of errors. */ - void show_errors(void) { - int cnt = 0; - struct errs *errp = errs; - - printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s"); - while (cnt++ < errcnt) - { - printf("%5s: %s\n", errp->arg, - errp->errno == 0 ? "Not a number" : strerror(errp->errno)); - errp++; - } + struct errs *errp = errs; + int cnt = 0; + + printf("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s"); + while (cnt++ < errcnt) { + printf("%5s: %s\n", errp->arg, + errp->errno == 0 ? "Not a number" : strerror(errp->errno)); + errp++; + } } /* * kill_procs(str) - send signals to processes, much like the "kill" * command does; invoked in response to 'k'. */ - char * kill_procs(char *str) { - char *nptr; - int signum = SIGTERM; /* default */ - int procnum; - struct sigdesc *sigp; - int uid; - - /* reset error array */ - ERR_RESET; - - /* remember our uid */ - uid = getuid(); - - /* skip over leading white space */ - while (isspace(*str)) str++; - - if (str[0] == '-') - { - /* explicit signal specified */ - if ((nptr = next_field(str)) == NULL) - { - return(" kill: no processes specified"); - } - - if (isdigit(str[1])) - { - (void) scanint(str + 1, &signum); - if (signum <= 0 || signum >= NSIG) - { - return(" invalid signal number"); - } - } - else - { - /* translate the name into a number */ - for (sigp = sigdesc; sigp->name != NULL; sigp++) - { - if (strcmp(sigp->name, str + 1) == 0) - { - signum = sigp->number; - break; + int signum = SIGTERM, procnum, uid; + struct sigdesc *sigp; + char *nptr; + + /* reset error array */ + ERR_RESET; + + /* remember our uid */ + uid = getuid(); + + /* skip over leading white space */ + while (isspace(*str)) + str++; + + if (str[0] == '-') { + /* explicit signal specified */ + if ((nptr = next_field(str)) == NULL) + return (" kill: no processes specified"); + + if (isdigit(str[1])) { + (void) scanint(str + 1, &signum); + if (signum <= 0 || signum >= NSIG) + return (" invalid signal number"); + } else { + /* translate the name into a number */ + for (sigp = sigdesc; sigp->name != NULL; sigp++) { + if (strcmp(sigp->name, str + 1) == 0) { + signum = sigp->number; + break; + } + } + + /* was it ever found */ + if (sigp->name == NULL) + return (" bad signal name"); } - } - - /* was it ever found */ - if (sigp->name == NULL) - { - return(" bad signal name"); - } - } - /* put the new pointer in place */ - str = nptr; - } - - /* loop thru the string, killing processes */ - do - { - if (scanint(str, &procnum) == -1) - { - ERROR(str, 0); + /* put the new pointer in place */ + str = nptr; } - else - { - /* check process owner if we're not root */ - if (uid && (uid != proc_owner(procnum))) - { - ERROR(str, EACCES); - } - /* go in for the kill */ - else if (kill(procnum, signum) == -1) - { - /* chalk up an error */ - ERROR(str, errno); - } - } - } while ((str = next_field(str)) != NULL); + /* loop thru the string, killing processes */ + do { + if (scanint(str, &procnum) == -1) { + ERROR(str, 0); + } else { + /* check process owner if we're not root */ + if (uid && (uid != proc_owner(procnum))) { + ERROR(str, EACCES); + } else if (kill(procnum, signum) == -1) { + ERROR(str, errno); + } + } + } while ((str = next_field(str)) != NULL); - /* return appropriate error string */ - return(err_string()); + /* return appropriate error string */ + return (err_string()); } /* * renice_procs(str) - change the "nice" of processes, much like the * "renice" command does; invoked in response to 'r'. */ - char * renice_procs(char *str) { - char negate; - int prio; - int procnum; - int uid; - - ERR_RESET; - uid = getuid(); - - /* allow for negative priority values */ - if ((negate = (*str == '-')) != 0) - { - /* move past the minus sign */ - str++; - } - - /* use procnum as a temporary holding place and get the number */ - procnum = scanint(str, &prio); - - /* negate if necessary */ - if (negate) - { - prio = -prio; - } + int prio, procnum, uid; + char negate; + + ERR_RESET; + uid = getuid(); + + /* allow for negative priority values */ + if ((negate = (*str == '-')) != 0) { + /* move past the minus sign */ + str++; + } + /* use procnum as a temporary holding place and get the number */ + procnum = scanint(str, &prio); + + /* negate if necessary */ + if (negate) + prio = -prio; #if defined(PRIO_MIN) && defined(PRIO_MAX) - /* check for validity */ - if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX) - { - return(" bad priority value"); - } + /* check for validity */ + if (procnum == -1 || prio < PRIO_MIN || prio > PRIO_MAX) + return (" bad priority value"); #endif - /* move to the first process number */ - if ((str = next_field(str)) == NULL) - { - return(" no processes specified"); - } - - /* loop thru the process numbers, renicing each one */ - do - { - if (scanint(str, &procnum) == -1) - { - ERROR(str, 0); - } + /* move to the first process number */ + if ((str = next_field(str)) == NULL) + return (" no processes specified"); - /* check process owner if we're not root */ - else if (uid && (uid != proc_owner(procnum))) - { - ERROR(str, EACCES); - } - else if (setpriority(PRIO_PROCESS, procnum, prio) == -1) - { - ERROR(str, errno); - } - } while ((str = next_field(str)) != NULL); + /* loop thru the process numbers, renicing each one */ + do { + if (scanint(str, &procnum) == -1) { + ERROR(str, 0); + } + /* check process owner if we're not root */ + else if (uid && (uid != proc_owner(procnum))) { + ERROR(str, EACCES); + } else if (setpriority(PRIO_PROCESS, procnum, prio) == -1) { + ERROR(str, errno); + } + } while ((str = next_field(str)) != NULL); - /* return appropriate error string */ - return(err_string()); + /* return appropriate error string */ + return (err_string()); } diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c index d6d821ac79a..1b63407a62e 100644 --- a/usr.bin/top/display.c +++ b/usr.bin/top/display.c @@ -1,4 +1,4 @@ -/* $OpenBSD: display.c,v 1.13 2003/06/13 04:29:59 pvalchev Exp $ */ +/* $OpenBSD: display.c,v 1.14 2003/06/13 21:52:24 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -65,228 +65,198 @@ #include "utils.h" #ifdef DEBUG -FILE *debug; +FILE *debug; #endif -static pid_t lmpid = 0; -static int last_hi = 0; /* used in u_process and u_endscreen */ -static int lastline = 0; -static int display_width = MAX_COLS; +static pid_t lmpid = 0; +static int last_hi = 0; /* used in u_process and u_endscreen */ +static int lastline = 0; +static int display_width = MAX_COLS; -static char *cpustates_tag(void); -static int string_count(char **); -static void summary_format(char *, int *, char **); -static void line_update(char *, char *, int, int); +static char *cpustates_tag(void); +static int string_count(char **); +static void summary_format(char *, int *, char **); +static void line_update(char *, char *, int, int); #define lineindex(l) ((l)*display_width) /* things initialized by display_init and used thruout */ /* buffer of proc information lines for display updating */ -char *screenbuf = NULL; +char *screenbuf = NULL; -static char **procstate_names; -static char **cpustate_names; -static char **memory_names; +static char **procstate_names; +static char **cpustate_names; +static char **memory_names; -static int num_procstates; -static int num_cpustates; -static int num_memory; +static int num_procstates; +static int num_cpustates; +static int num_memory; -static int *lprocstates; -static int *lcpustates; -static int *lmemory; +static int *lprocstates; +static int *lcpustates; +static int *lmemory; -static int *cpustate_columns; -static int cpustate_total_length; +static int *cpustate_columns; +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(); +static enum { + OFF, ON, ERASE +} header_status = ON; int display_resize(void) { - int display_lines; - - /* first, deallocate any previous buffer that may have been there */ - if (screenbuf != NULL) - { - free(screenbuf); - } - - /* calculate the current dimensions */ - /* if operating in "dumb" mode, we only need one line */ - display_lines = smart_terminal ? screen_length - Header_lines : 1; - - /* 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 */ - display_width = screen_width; - if (display_width >= MAX_COLS) - { - display_width = MAX_COLS - 1; - } - - /* now, allocate space for the screen buffer */ - screenbuf = (char *)malloc(display_lines * display_width); - if (screenbuf == (char *)NULL) - { - /* oops! */ - return(-1); - } - - /* return number of lines available */ - /* for dumb terminals, pretend like we can show any amount */ - return(smart_terminal ? display_lines : Largest); + int display_lines; + + /* first, deallocate any previous buffer that may have been there */ + if (screenbuf != NULL) + free(screenbuf); + + /* calculate the current dimensions */ + /* if operating in "dumb" mode, we only need one line */ + display_lines = smart_terminal ? screen_length - Header_lines : 1; + + /* + * 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 + */ + display_width = screen_width; + if (display_width >= MAX_COLS) + display_width = MAX_COLS - 1; + + /* now, allocate space for the screen buffer */ + screenbuf = (char *) malloc(display_lines * display_width); + if (screenbuf == (char *) NULL) + return (-1); + + /* return number of lines available */ + /* for dumb terminals, pretend like we can show any amount */ + return (smart_terminal ? display_lines : Largest); } int -display_init(struct statics *statics) +display_init(struct statics * statics) { - int display_lines; - char **pp; - int *ip; - int i; - - /* call resize to do the dirty work */ - display_lines = display_resize(); - - /* only do the rest if we need to */ - if (display_lines > -1) - { - /* save pointers and allocate space for names */ - procstate_names = statics->procstate_names; - num_procstates = string_count(procstate_names); - lprocstates = (int *)malloc(num_procstates * sizeof(int)); - - cpustate_names = statics->cpustate_names; - num_cpustates = string_count(cpustate_names); - lcpustates = (int *)malloc(num_cpustates * sizeof(int)); - cpustate_columns = (int *)malloc(num_cpustates * sizeof(int)); - - memory_names = statics->memory_names; - num_memory = string_count(memory_names); - lmemory = (int *)malloc(num_memory * sizeof(int)); - - /* calculate starting columns where needed */ - cpustate_total_length = 0; - pp = cpustate_names; - ip = cpustate_columns; - while (*pp != NULL) - { - if ((i = strlen(*pp++)) > 0) - { - *ip++ = cpustate_total_length; - cpustate_total_length += i + 8; - } + int display_lines, *ip, i; + char **pp; + + /* call resize to do the dirty work */ + display_lines = display_resize(); + + /* only do the rest if we need to */ + if (display_lines > -1) { + /* save pointers and allocate space for names */ + procstate_names = statics->procstate_names; + num_procstates = string_count(procstate_names); + lprocstates = (int *) malloc(num_procstates * sizeof(int)); + + cpustate_names = statics->cpustate_names; + num_cpustates = string_count(cpustate_names); + lcpustates = (int *) malloc(num_cpustates * sizeof(int)); + cpustate_columns = (int *) malloc(num_cpustates * sizeof(int)); + + memory_names = statics->memory_names; + num_memory = string_count(memory_names); + lmemory = (int *) malloc(num_memory * sizeof(int)); + + /* calculate starting columns where needed */ + cpustate_total_length = 0; + pp = cpustate_names; + ip = cpustate_columns; + while (*pp != NULL) { + if ((i = strlen(*pp++)) > 0) { + *ip++ = cpustate_total_length; + cpustate_total_length += i + 8; + } + } } - } - - /* return number of lines available */ - return(display_lines); + /* return number of lines available */ + return (display_lines); } void i_loadave(pid_t mpid, double *avenrun) { - int i; - - /* i_loadave also clears the screen, since it is first */ - clear(); - - /* mpid == -1 implies this system doesn't have an _mpid */ - if (mpid != -1) - { - printf("last pid: %5ld; ", (long)mpid); - } - - printf("load averages"); - - for (i = 0; i < 3; i++) - { - printf("%c %5.2f", - i == 0 ? ':' : ',', - avenrun[i]); - } - lmpid = mpid; + int i; + + /* i_loadave also clears the screen, since it is first */ + clear(); + + /* mpid == -1 implies this system doesn't have an _mpid */ + if (mpid != -1) + printf("last pid: %5ld; ", (long) mpid); + + printf("load averages"); + + for (i = 0; i < 3; i++) + printf("%c %5.2f", i == 0 ? ':' : ',', avenrun[i]); + + lmpid = mpid; } void u_loadave(pid_t mpid, double *avenrun) { - int i; - - if (mpid != -1) - { - /* change screen only when value has really changed */ - if (mpid != lmpid) - { - Move_to(x_lastpid, y_lastpid); - printf("%5ld", (long)mpid); - lmpid = mpid; - } - - /* i remembers x coordinate to move to */ - i = x_loadave; - } - else - { - i = x_loadave_nompid; - } - - /* move into position for load averages */ - Move_to(i, y_loadave); - - /* display new load averages */ - /* we should optimize this and only display changes */ - for (i = 0; i < 3; i++) - { - printf("%s%5.2f", - i == 0 ? "" : ", ", - avenrun[i]); - } + int i; + + if (mpid != -1) { + /* change screen only when value has really changed */ + if (mpid != lmpid) { + Move_to(x_lastpid, y_lastpid); + printf("%5ld", (long) mpid); + lmpid = mpid; + } + /* i remembers x coordinate to move to */ + i = x_loadave; + } else + i = x_loadave_nompid; + + /* move into position for load averages */ + Move_to(i, y_loadave); + + /* display new load averages */ + /* we should optimize this and only display changes */ + for (i = 0; i < 3; i++) + printf("%s%5.2f", i == 0 ? "" : ", ", avenrun[i]); } +/* + * Display the current time. + * "ctime" always returns a string that looks like this: + * + * Sun Sep 16 01:03:52 1973 + * 012345678901234567890123 + * 1 2 + * + * We want indices 11 thru 18 (length 8). + */ + void -i_timeofday(time_t *tod) +i_timeofday(time_t * tod) { - /* - * Display the current time. - * "ctime" always returns a string that looks like this: - * - * Sun Sep 16 01:03:52 1973 - * 012345678901234567890123 - * 1 2 - * - * We want indices 11 thru 18 (length 8). - */ - - if (smart_terminal) - { - Move_to(screen_width - 8, 0); - } - else - { - if (fputs(" ", stdout) == EOF) - exit(1); - } + + if (smart_terminal) { + Move_to(screen_width - 8, 0); + } else { + if (fputs(" ", stdout) == EOF) + exit(1); + } #ifdef DEBUG - { - char *foo; - foo = ctime(tod); - if (fputs(foo, stdout) == EOF) - exit(1); - } + { + char *foo; + foo = ctime(tod); + if (fputs(foo, stdout) == EOF) + exit(1); + } #endif - printf("%-8.8s\n", &(ctime(tod)[11])); - lastline = 1; + printf("%-8.8s\n", &(ctime(tod)[11])); + lastline = 1; } -static int ltotal = 0; -static char procstates_buffer[128]; +static int ltotal = 0; +static char procstates_buffer[128]; /* * *_procstates(total, brkdn, names) - print the process summary line @@ -294,78 +264,70 @@ static char procstates_buffer[128]; * Assumptions: cursor is at the beginning of the line on entry * lastline is valid */ - void i_procstates(int total, int *brkdn) { - int i; + int i; - /* write current number of processes and remember the value */ - printf("%d processes:", total); - ltotal = total; + /* write current number of processes and remember the value */ + printf("%d processes:", total); + ltotal = total; - /* put out enough spaces to get to column 15 */ - i = digits(total); - while (i++ < 4) - { - if (putchar(' ') == EOF) - exit(1); - } + /* put out enough spaces to get to column 15 */ + i = digits(total); + while (i++ < 4) { + if (putchar(' ') == EOF) + exit(1); + } - /* format and print the process state summary */ - summary_format(procstates_buffer, brkdn, procstate_names); - if (fputs(procstates_buffer, stdout) == EOF) - exit(1); + /* format and print the process state summary */ + summary_format(procstates_buffer, brkdn, procstate_names); + if (fputs(procstates_buffer, stdout) == EOF) + exit(1); - /* save the numbers for next time */ - memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); + /* save the numbers for next time */ + memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); } void u_procstates(int total, int *brkdn) { - static char new[128]; - int i; + static char new[128]; + int i; - /* update number of processes only if it has changed */ - if (ltotal != total) - { - /* move and overwrite */ + /* update number of processes only if it has changed */ + if (ltotal != total) { + /* move and overwrite */ #if (x_procstate == 0) - Move_to(x_procstate, y_procstate); + Move_to(x_procstate, y_procstate); #else - /* cursor is already there...no motion needed */ - /* assert(lastline == 1); */ + /* cursor is already there...no motion needed */ + /* assert(lastline == 1); */ #endif - printf("%d", total); - - /* if number of digits differs, rewrite the label */ - if (digits(total) != digits(ltotal)) - { - if (fputs(" processes:", stdout) == EOF) - exit(1); - /* put out enough spaces to get to column 15 */ - i = digits(total); - while (i++ < 4) - { - if (putchar(' ') == EOF) - exit(1); - } - /* cursor may end up right where we want it!!! */ + printf("%d", total); + + /* if number of digits differs, rewrite the label */ + if (digits(total) != digits(ltotal)) { + if (fputs(" processes:", stdout) == EOF) + exit(1); + /* put out enough spaces to get to column 15 */ + i = digits(total); + while (i++ < 4) { + if (putchar(' ') == EOF) + exit(1); + } + /* cursor may end up right where we want it!!! */ + } + /* save new total */ + ltotal = total; + } + /* see if any of the state numbers has changed */ + if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0) { + /* format and update the line */ + summary_format(new, brkdn, procstate_names); + line_update(procstates_buffer, new, x_brkdn, y_brkdn); + memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); } - - /* save new total */ - ltotal = total; - } - - /* see if any of the state numbers has changed */ - if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0) - { - /* format and update the line */ - summary_format(new, brkdn, procstate_names); - line_update(procstates_buffer, new, x_brkdn, y_brkdn); - memcpy(lprocstates, brkdn, num_procstates * sizeof(int)); - } } /* @@ -374,170 +336,148 @@ u_procstates(int total, int *brkdn) * Assumptions: cursor is on the PREVIOUS line */ -static int cpustates_column; +static int cpustates_column; /* cpustates_tag() calculates the correct tag to use to label the line */ static char * cpustates_tag(void) { - char *use; - - static char *short_tag = "CPU: "; - static char *long_tag = "CPU states: "; - - /* if length + strlen(long_tag) >= screen_width, then we have to - use the shorter tag (we subtract 2 to account for ": ") */ - if (cpustate_total_length + (int)strlen(long_tag) - 2 >= screen_width) - { - use = short_tag; - } - else - { - use = long_tag; - } - - /* set cpustates_column accordingly then return result */ - cpustates_column = strlen(use); - return(use); + static char *short_tag = "CPU: "; + static char *long_tag = "CPU states: "; + char *use; + + /* + * if length + strlen(long_tag) >= screen_width, then we have to use + * the shorter tag (we subtract 2 to account for ": ") + */ + if (cpustate_total_length + (int) strlen(long_tag) - 2 >= screen_width) + use = short_tag; + else + use = long_tag; + + /* set cpustates_column accordingly then return result */ + cpustates_column = strlen(use); + return (use); } void i_cpustates(int *states) { - int i = 0; - int value; - char **names = cpustate_names; - char *thisname; - - /* print tag and bump lastline */ - printf("\n%s", cpustates_tag()); - lastline++; - - /* now walk thru the names and print the line */ - while ((thisname = *names++) != NULL) - { - if (*thisname != '\0') - { - /* retrieve the value and remember it */ - value = *states++; - - /* if percentage is >= 1000, print it as 100% */ - printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"), - i++ == 0 ? "" : ", ", - ((float)value)/10., - thisname); + int i = 0, value; + char **names = cpustate_names, *thisname; + + /* print tag and bump lastline */ + printf("\n%s", cpustates_tag()); + lastline++; + + /* now walk thru the names and print the line */ + while ((thisname = *names++) != NULL) { + if (*thisname != '\0') { + /* retrieve the value and remember it */ + value = *states++; + + /* if percentage is >= 1000, print it as 100% */ + printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"), + i++ == 0 ? "" : ", ", + ((float) value) / 10., + thisname); + } } - } - /* copy over values into "last" array */ - memcpy(lcpustates, states, num_cpustates * sizeof(int)); + /* copy over values into "last" array */ + memcpy(lcpustates, states, num_cpustates * sizeof(int)); } void u_cpustates(int *states) { - int value; - char **names = cpustate_names; - char *thisname; - int *lp; - int *colp; - - Move_to(cpustates_column, y_cpustates); - lastline = y_cpustates; - lp = lcpustates; - colp = cpustate_columns; - - /* we could be much more optimal about this */ - while ((thisname = *names++) != NULL) - { - if (*thisname != '\0') - { - /* did the value change since last time? */ - if (*lp != *states) - { - /* yes, move and change */ - Move_to(cpustates_column + *colp, y_cpustates); - lastline = y_cpustates; - - /* retrieve value and remember it */ - value = *states; - - /* if percentage is >= 1000, print it as 100% */ - printf((value >= 1000 ? "%4.0f" : "%4.1f"), - ((double)value)/10.); - - /* remember it for next time */ - *lp = *states; - } + char **names = cpustate_names, *thisname; + int value, *lp, *colp; + + Move_to(cpustates_column, y_cpustates); + lastline = y_cpustates; + lp = lcpustates; + colp = cpustate_columns; + + /* we could be much more optimal about this */ + while ((thisname = *names++) != NULL) { + if (*thisname != '\0') { + /* did the value change since last time? */ + if (*lp != *states) { + /* yes, move and change */ + Move_to(cpustates_column + *colp, y_cpustates); + lastline = y_cpustates; + + /* retrieve value and remember it */ + value = *states; + + /* if percentage is >= 1000, print it as 100% */ + printf((value >= 1000 ? "%4.0f" : "%4.1f"), + ((double) value) / 10.); + + /* remember it for next time */ + *lp = *states; + } + } + /* increment and move on */ + lp++; + states++; + colp++; } - - /* increment and move on */ - lp++; - states++; - colp++; - } } void z_cpustates(void) { - int i = 0; - char **names = cpustate_names; - char *thisname; - int *lp; - - /* show tag and bump lastline */ - printf("\n%s", cpustates_tag()); - lastline++; - - while ((thisname = *names++) != NULL) - { - if (*thisname != '\0') - { - printf("%s %% %s", i++ == 0 ? "" : ", ", thisname); + char **names = cpustate_names, *thisname; + int i = 0, *lp; + + /* show tag and bump lastline */ + printf("\n%s", cpustates_tag()); + lastline++; + + while ((thisname = *names++) != NULL) { + if (*thisname != '\0') + printf("%s %% %s", i++ == 0 ? "" : ", ", thisname); } - } - - /* fill the "last" array with all -1s, to insure correct updating */ - lp = lcpustates; - i = num_cpustates; - while (--i >= 0) - { - *lp++ = -1; - } + + /* fill the "last" array with all -1s, to insure correct updating */ + lp = lcpustates; + i = num_cpustates; + while (--i >= 0) + *lp++ = -1; } +static char memory_buffer[MAX_COLS]; + /* * *_memory(stats) - print "Memory: " followed by the memory summary string * * Assumptions: cursor is on "lastline" * for i_memory ONLY: cursor is on the previous line */ - -static char memory_buffer[MAX_COLS]; - void i_memory(int *stats) { - if (fputs("\nMemory: ", stdout) == EOF) - exit(1); - lastline++; - - /* format and print the memory summary */ - summary_format(memory_buffer, stats, memory_names); - if (fputs(memory_buffer, stdout) == EOF) - exit(1); + if (fputs("\nMemory: ", stdout) == EOF) + exit(1); + lastline++; + + /* format and print the memory summary */ + summary_format(memory_buffer, stats, memory_names); + if (fputs(memory_buffer, stdout) == EOF) + exit(1); } void u_memory(int *stats) { - static char new[MAX_COLS]; + static char new[MAX_COLS]; - /* format the new line */ - summary_format(new, stats, memory_names); - line_update(memory_buffer, new, x_mem, y_mem); + /* format the new line */ + summary_format(new, stats, memory_names); + line_update(memory_buffer, new, x_mem, y_mem); } /* @@ -554,40 +494,38 @@ u_memory(int *stats) * respect to screen updates). */ -static char next_msg[MAX_COLS + 5]; -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). */ +static char next_msg[MAX_COLS + 5]; +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). + */ void i_message(void) { - while (lastline < y_message) - { - if (fputc('\n', stdout) == EOF) - exit(1); - lastline++; - } - if (next_msg[0] != '\0') - { - standout(next_msg); - msglen = strlen(next_msg); - next_msg[0] = '\0'; - } - else if (msglen > 0) - { - (void) clear_eol(msglen); - msglen = 0; - } + while (lastline < y_message) { + if (fputc('\n', stdout) == EOF) + exit(1); + lastline++; + } + if (next_msg[0] != '\0') { + standout(next_msg); + msglen = strlen(next_msg); + next_msg[0] = '\0'; + } else if (msglen > 0) { + (void) clear_eol(msglen); + msglen = 0; + } } void u_message(void) { - i_message(); + i_message(); } -static int header_length; +static int header_length; /* * *_header(text) - print the header for the process area @@ -598,33 +536,29 @@ static int header_length; void i_header(char *text) { - header_length = strlen(text); - if (header_status == ON) - { - if (putchar('\n') == EOF) - exit(1); - if (fputs(text, stdout) == EOF) - exit(1); - lastline++; - } - else if (header_status == ERASE) - { - header_status = OFF; - } + header_length = strlen(text); + if (header_status == ON) { + if (putchar('\n') == EOF) + exit(1); + if (fputs(text, stdout) == EOF) + exit(1); + lastline++; + } else if (header_status == ERASE) { + header_status = OFF; + } } -/*ARGSUSED*/ +/* ARGSUSED */ void u_header(char *text) { - if (header_status == ERASE) - { - if (putchar('\n') == EOF) - exit(1); - lastline++; - clear_eol(header_length); - header_status = OFF; - } + if (header_status == ERASE) { + if (putchar('\n') == EOF) + exit(1); + lastline++; + clear_eol(header_length); + header_status = OFF; + } } /* @@ -636,475 +570,405 @@ u_header(char *text) void i_process(int line, char *thisline) { - char *p; - char *base; - - /* make sure we are on the correct line */ - while (lastline < y_procs + line) - { - if (putchar('\n') == EOF) - exit(1); - lastline++; - } + char *p, *base; - /* truncate the line to conform to our current screen width */ - thisline[display_width] = '\0'; + /* make sure we are on the correct line */ + while (lastline < y_procs + line) { + if (putchar('\n') == EOF) + exit(1); + lastline++; + } - /* write the line out */ - if (fputs(thisline, stdout) == EOF) - exit(1); + /* truncate the line to conform to our current screen width */ + thisline[display_width] = '\0'; + + /* write the line out */ + if (fputs(thisline, stdout) == EOF) + exit(1); - /* copy it in to our buffer */ - base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; - p = strecpy(base, thisline); + /* copy it in to our buffer */ + base = smart_terminal ? screenbuf + lineindex(line) : screenbuf; + p = strecpy(base, thisline); - /* zero fill the rest of it */ - memset(p, 0, display_width - (p - base)); + /* zero fill the rest of it */ + memset(p, 0, display_width - (p - base)); } void u_process(int linenum, char *linebuf) { - char *optr; - int screen_line = linenum + Header_lines; - char *bufferline; - - /* remember a pointer to the current line in the screen buffer */ - bufferline = &screenbuf[lineindex(linenum)]; - - /* truncate the line to conform to our current screen width */ - linebuf[display_width] = '\0'; - - /* is line higher than we went on the last display? */ - if (linenum >= last_hi) - { - /* yes, just ignore screenbuf and write it out directly */ - /* get positioned on the correct line */ - if (screen_line - lastline == 1) - { - if (putchar('\n') == EOF) - exit(1); - lastline++; - } - else - { - Move_to(0, screen_line); - lastline = screen_line; - } + int screen_line = linenum + Header_lines; + char *optr, *bufferline; - /* now write the line */ - if (fputs(linebuf, stdout) == EOF) - exit(1); + /* remember a pointer to the current line in the screen buffer */ + bufferline = &screenbuf[lineindex(linenum)]; - /* copy it in to the buffer */ - optr = strecpy(bufferline, linebuf); + /* truncate the line to conform to our current screen width */ + linebuf[display_width] = '\0'; - /* zero fill the rest of it */ - memset(optr, 0, display_width - (optr - bufferline)); - } - else - { - line_update(bufferline, linebuf, 0, linenum + Header_lines); - } + /* is line higher than we went on the last display? */ + if (linenum >= last_hi) { + /* yes, just ignore screenbuf and write it out directly */ + /* get positioned on the correct line */ + if (screen_line - lastline == 1) { + if (putchar('\n') == EOF) + exit(1); + lastline++; + } else { + Move_to(0, screen_line); + lastline = screen_line; + } + + /* now write the line */ + if (fputs(linebuf, stdout) == EOF) + exit(1); + + /* copy it in to the buffer */ + optr = strecpy(bufferline, linebuf); + + /* zero fill the rest of it */ + memset(optr, 0, display_width - (optr - bufferline)); + } else { + line_update(bufferline, linebuf, 0, linenum + Header_lines); + } } void u_endscreen(int hi) { - int screen_line = hi + Header_lines; - int i; - - if (smart_terminal) - { - if (hi < last_hi) - { - /* need to blank the remainder of the screen */ - /* but only if there is any screen left below this line */ - if (lastline + 1 < screen_length) - { - /* efficiently move to the end of currently displayed info */ - if (screen_line - lastline < 5) - { - while (lastline < screen_line) - { - if (putchar('\n') == EOF) - exit(1); - lastline++; - } - } - else - { - Move_to(0, screen_line); - lastline = screen_line; - } - - if (clear_to_end) - { - /* we can do this the easy way */ - putcap(clear_to_end); - } - else - { - /* use clear_eol on each line */ - i = hi; - while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi) - { - if (putchar('\n') == EOF) - exit(1); - } + int screen_line = hi + Header_lines, i; + + if (smart_terminal) { + if (hi < last_hi) { + /* need to blank the remainder of the screen */ + /* + * but only if there is any screen left below this + * line + */ + if (lastline + 1 < screen_length) { + /* + * efficiently move to the end of currently + * displayed info + */ + if (screen_line - lastline < 5) { + while (lastline < screen_line) { + if (putchar('\n') == EOF) + exit(1); + lastline++; + } + } else { + Move_to(0, screen_line); + lastline = screen_line; + } + + if (clear_to_end) { + /* we can do this the easy way */ + putcap(clear_to_end); + } else { + /* use clear_eol on each line */ + i = hi; + while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi) { + if (putchar('\n') == EOF) + exit(1); + } + } + } } - } + last_hi = hi; + + /* move the cursor to a pleasant place */ + Move_to(x_idlecursor, y_idlecursor); + lastline = y_idlecursor; + } else { + /* + * separate this display from the next with some vertical + * room + */ + if (fputs("\n\n", stdout) == EOF) + exit(1); } - last_hi = hi; - - /* move the cursor to a pleasant place */ - Move_to(x_idlecursor, y_idlecursor); - lastline = y_idlecursor; - } - else - { - /* separate this display from the next with some vertical room */ - if (fputs("\n\n", stdout) == EOF) - exit(1); - } } void display_header(int t) { - if (t) - { - header_status = ON; - } - else if (header_status == ON) - { - header_status = ERASE; - } + if (t) { + header_status = ON; + } else if (header_status == ON) { + header_status = ERASE; + } } void -new_message(int type, const char *msgfmt, ...) +new_message(int type, const char *msgfmt,...) { - int i; - va_list ap; - - va_start(ap, msgfmt); - /* first, format the message */ - vsnprintf(next_msg, sizeof(next_msg), msgfmt, ap); - va_end(ap); - - if (msglen > 0) - { - /* message there already -- can we clear it? */ - if (!overstrike) - { - /* yes -- write it and clear to end */ - i = strlen(next_msg); - if ((type & MT_delayed) == 0) - { - if (type & MT_standout) - standout(next_msg); - else { - if (fputs(next_msg, stdout) == EOF) - exit(1); + va_list ap; + int i; + + va_start(ap, msgfmt); + /* first, format the message */ + vsnprintf(next_msg, sizeof(next_msg), msgfmt, ap); + va_end(ap); + + if (msglen > 0) { + /* message there already -- can we clear it? */ + if (!overstrike) { + /* yes -- write it and clear to end */ + i = strlen(next_msg); + if ((type & MT_delayed) == 0) { + if (type & MT_standout) + standout(next_msg); + else { + if (fputs(next_msg, stdout) == EOF) + exit(1); + } + (void) clear_eol(msglen - i); + msglen = i; + next_msg[0] = '\0'; + } + } + } else { + if ((type & MT_delayed) == 0) { + if (type & MT_standout) + standout(next_msg); + else { + if (fputs(next_msg, stdout) == EOF) + exit(1); + } + msglen = strlen(next_msg); + next_msg[0] = '\0'; } - (void) clear_eol(msglen - i); - msglen = i; - next_msg[0] = '\0'; - } - } - } - else - { - if ((type & MT_delayed) == 0) - { - if (type & MT_standout) - standout(next_msg); - else { - if (fputs(next_msg, stdout) == EOF) - exit(1); - } - msglen = strlen(next_msg); - next_msg[0] = '\0'; } - } } void clear_message(void) { - if (clear_eol(msglen) == 1) - { - if (putchar('\r') == EOF) - exit(1); - } + if (clear_eol(msglen) == 1) { + if (putchar('\r') == EOF) + exit(1); + } } int readline(char *buffer, int size, int numeric) { - char *ptr = buffer; - char ch; - char cnt = 0; - char maxcnt = 0; - - /* allow room for null terminator */ - size -= 1; - - /* read loop */ - while ((fflush(stdout), read(0, ptr, 1) > 0)) - { - /* newline means we are done */ - if ((ch = *ptr) == '\n') - { - break; - } - - /* handle special editing characters */ - if (ch == ch_kill) - { - /* kill line -- account for overstriking */ - if (overstrike) - { - msglen += maxcnt; - } - - /* return null string */ - *buffer = '\0'; - if (putchar('\r') == EOF) - exit(1); - return(-1); - } - else if (ch == ch_erase) - { - /* erase previous character */ - if (cnt <= 0) - { - /* none to erase! */ - if (putchar('\7') == EOF) - exit(1); - } - else - { - if (fputs("\b \b", stdout) == EOF) - exit(1); - ptr--; - cnt--; - } - } - /* check for character validity and buffer overflow */ - else if (cnt == size || (numeric && !isdigit(ch)) || - !isprint(ch)) - { - /* not legal */ - if (putchar('\7') == EOF) - exit(1); - } - else - { - /* echo it and store it in the buffer */ - if (putchar(ch) == EOF) - exit(1); - ptr++; - cnt++; - if (cnt > maxcnt) - { - maxcnt = cnt; - } + char *ptr = buffer, ch, cnt = 0, maxcnt = 0; + + /* allow room for null terminator */ + size -= 1; + + /* read loop */ + while ((fflush(stdout), read(0, ptr, 1) > 0)) { + /* newline means we are done */ + if ((ch = *ptr) == '\n') + break; + + /* handle special editing characters */ + if (ch == ch_kill) { + /* kill line -- account for overstriking */ + if (overstrike) + msglen += maxcnt; + + /* return null string */ + *buffer = '\0'; + if (putchar('\r') == EOF) + exit(1); + return (-1); + } else if (ch == ch_erase) { + /* erase previous character */ + if (cnt <= 0) { + /* none to erase! */ + if (putchar('\7') == EOF) + exit(1); + } else { + if (fputs("\b \b", stdout) == EOF) + exit(1); + ptr--; + cnt--; + } + } + /* check for character validity and buffer overflow */ + else if (cnt == size || (numeric && !isdigit(ch)) || + !isprint(ch)) { + /* not legal */ + if (putchar('\7') == EOF) + exit(1); + } else { + /* echo it and store it in the buffer */ + if (putchar(ch) == EOF) + exit(1); + ptr++; + cnt++; + if (cnt > maxcnt) + maxcnt = cnt; + } } - } - /* all done -- null terminate the string */ - *ptr = '\0'; + /* all done -- null terminate the string */ + *ptr = '\0'; - /* account for the extra characters in the message area */ - /* (if terminal overstrikes, remember the furthest they went) */ - msglen += overstrike ? maxcnt : cnt; + /* account for the extra characters in the message area */ + /* (if terminal overstrikes, remember the furthest they went) */ + msglen += overstrike ? maxcnt : cnt; - /* return either inputted number or string length */ - if (putchar('\r') == EOF) - exit(1); - return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt); + /* return either inputted number or string length */ + if (putchar('\r') == EOF) + exit(1); + return (cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt); } /* internal support routines */ - static int string_count(char **pp) { - int cnt; - - cnt = 0; - while (*pp++ != NULL) - { - cnt++; - } - return(cnt); + int cnt; + + cnt = 0; + while (*pp++ != NULL) + cnt++; + return (cnt); } static void summary_format(char *str, int *numbers, char **names) { - char *p; - int num; - char *thisname; - - /* format each number followed by its string */ - p = str; - while ((thisname = *names++) != NULL) - { - /* get the number to format */ - num = *numbers++; - - if (num >= 0) - { - /* is this number in kilobytes? */ - if (thisname[0] == 'K') - { - /* yes: format it as a memory value */ - p = strecpy(p, format_k(num)); - - /* skip over the K, since it was included by format_k */ - p = strecpy(p, thisname+1); - } - else if (num > 0) - { - p = strecpy(p, itoa(num)); - p = strecpy(p, thisname); - } + char *p, *thisname; + int num; + + /* format each number followed by its string */ + p = str; + while ((thisname = *names++) != NULL) { + /* get the number to format */ + num = *numbers++; + + if (num >= 0) { + /* is this number in kilobytes? */ + if (thisname[0] == 'K') { + /* yes: format it as a memory value */ + p = strecpy(p, format_k(num)); + + /* + * skip over the K, since it was included by + * format_k + */ + p = strecpy(p, thisname + 1); + } else if (num > 0) { + p = strecpy(p, itoa(num)); + p = strecpy(p, thisname); + } + } + /* ignore negative numbers, but display corresponding string */ + else + p = strecpy(p, thisname); } - /* ignore negative numbers, but display corresponding string */ - else - { - p = strecpy(p, thisname); - } - } - - /* if the last two characters in the string are ", ", delete them */ - p -= 2; - if (p >= str && p[0] == ',' && p[1] == ' ') - { - *p = '\0'; - } + /* if the last two characters in the string are ", ", delete them */ + p -= 2; + if (p >= str && p[0] == ',' && p[1] == ' ') + *p = '\0'; } static void line_update(char *old, char *new, int start, int line) { - int ch; - int diff; - int newcol = start + 1; - int lastcol = start; - char cursor_on_line = No; - char *current; - - /* compare the two strings and only rewrite what has changed */ - current = old; + int ch, diff, newcol = start + 1, lastcol = start; + char cursor_on_line = No, *current; + + /* compare the two strings and only rewrite what has changed */ + current = old; #ifdef DEBUG - fprintf(debug, "line_update, starting at %d\n", start); - fputs(old, debug); - fputc('\n', debug); - fputs(new, debug); - fputs("\n-\n", debug); + fprintf(debug, "line_update, starting at %d\n", start); + fputs(old, debug); + fputc('\n', debug); + fputs(new, debug); + fputs("\n-\n", debug); #endif - /* start things off on the right foot */ - /* this is to make sure the invariants get set up right */ - if ((ch = *new++) != *old) - { - if (line - lastline == 1 && start == 0) - { - if (putchar('\n') == EOF) - exit(1); - } - else - { - Move_to(start, line); - } - cursor_on_line = Yes; - if (putchar(ch) == EOF) - exit(1); - *old = ch; - lastcol = 1; - } - old++; - - /* - * main loop -- check each character. If the old and new aren't the - * same, then update the display. When the distance from the - * current cursor position to the new change is small enough, - * the characters that belong there are written to move the - * cursor over. - * - * Invariants: - * lastcol is the column where the cursor currently is sitting - * (always one beyond the end of the last mismatch). - */ - do /* yes, a do...while */ - { - if ((ch = *new++) != *old) - { - /* new character is different from old */ - /* make sure the cursor is on top of this character */ - diff = newcol - lastcol; - if (diff > 0) - { - /* some motion is required--figure out which is shorter */ - if (diff < 6 && cursor_on_line) - { - /* overwrite old stuff--get it out of the old buffer */ - printf("%.*s", diff, ¤t[lastcol-start]); - } - else - { - /* use cursor addressing */ - Move_to(newcol, line); - cursor_on_line = Yes; - } - /* remember where the cursor is */ - lastcol = newcol + 1; - } - else - { - /* already there, update position */ - lastcol++; - } - - /* write what we need to */ - if (ch == '\0') - { - /* at the end--terminate with a clear-to-end-of-line */ - (void) clear_eol(strlen(old)); - } - else - { - /* write the new character */ + /* start things off on the right foot */ + /* this is to make sure the invariants get set up right */ + if ((ch = *new++) != *old) { + if (line - lastline == 1 && start == 0) { + if (putchar('\n') == EOF) + exit(1); + } else + Move_to(start, line); + + cursor_on_line = Yes; if (putchar(ch) == EOF) - exit(1); - } - /* put the new character in the screen buffer */ - *old = ch; + exit(1); + *old = ch; + lastcol = 1; } - - /* update working column and screen buffer pointer */ - newcol++; old++; - - } while (ch != '\0'); - - /* zero out the rest of the line buffer -- MUST BE DONE! */ - diff = display_width - newcol; - if (diff > 0) - { - memset(old, 0, diff); - } - - /* remember where the current line is */ - if (cursor_on_line) - { - lastline = line; - } + + /* + * main loop -- check each character. If the old and new aren't the + * same, then update the display. When the distance from the + * current cursor position to the new change is small enough, + * the characters that belong there are written to move the + * cursor over. + * + * Invariants: + * lastcol is the column where the cursor currently is sitting + * (always one beyond the end of the last mismatch). + */ + do { + if ((ch = *new++) != *old) { + /* new character is different from old */ + /* make sure the cursor is on top of this character */ + diff = newcol - lastcol; + if (diff > 0) { + /* + * some motion is required--figure out which + * is shorter + */ + if (diff < 6 && cursor_on_line) { + /* + * overwrite old stuff--get it out of + * the old buffer + */ + printf("%.*s", diff, ¤t[lastcol - start]); + } else { + /* use cursor addressing */ + Move_to(newcol, line); + cursor_on_line = Yes; + } + /* remember where the cursor is */ + lastcol = newcol + 1; + } else { + /* already there, update position */ + lastcol++; + } + + /* write what we need to */ + if (ch == '\0') { + /* + * at the end--terminate with a + * clear-to-end-of-line + */ + (void) clear_eol(strlen(old)); + } else { + /* write the new character */ + if (putchar(ch) == EOF) + exit(1); + } + /* put the new character in the screen buffer */ + *old = ch; + } + /* update working column and screen buffer pointer */ + newcol++; + old++; + } while (ch != '\0'); + + /* zero out the rest of the line buffer -- MUST BE DONE! */ + diff = display_width - newcol; + if (diff > 0) + memset(old, 0, diff); + + /* remember where the current line is */ + if (cursor_on_line) + lastline = line; } /* @@ -1113,21 +977,16 @@ line_update(char *old, char *new, int start, int line) * characters into '?'. Replacements are done in place and a pointer * to the original buffer is returned. */ - char * printable(char *str) { - char *ptr; - char ch; + char *ptr, ch; - ptr = str; - while ((ch = *ptr) != '\0') - { - if (!isprint(ch)) - { - *ptr = '?'; + ptr = str; + while ((ch = *ptr) != '\0') { + if (!isprint(ch)) + *ptr = '?'; + ptr++; } - ptr++; - } - return(str); + return (str); } diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index e6b819442ff..48436b3f244 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.c,v 1.30 2003/06/12 23:09:29 deraadt Exp $ */ +/* $OpenBSD: machine.c,v 1.31 2003/06/13 21:52:24 deraadt Exp $ */ /*- * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com> @@ -25,21 +25,6 @@ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * top - a top users display for Unix - * - * SYNOPSIS: For an OpenBSD system - * - * DESCRIPTION: - * This is the machine-dependent module for OpenBSD - * Tested on: - * i386 - * - * TERMCAP: -ltermlib - * - * CFLAGS: -DHAVE_GETOPT -DORDER * * AUTHOR: Thorsten Lockert <tholo@sigmasoft.com> * Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu> @@ -51,9 +36,6 @@ #include <sys/types.h> #include <sys/signal.h> #include <sys/param.h> - -#define DOSWAP - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -68,18 +50,16 @@ #include <sys/file.h> #include <sys/time.h> #include <sys/resource.h> - -#ifdef DOSWAP #include <sys/swap.h> #include <err.h> -#endif - -static int swapmode(int *, int *); #include "top.h" #include "display.h" #include "machine.h" #include "utils.h" +#include "loadavg.h" + +static int swapmode(int *, int *); /* get_process_info passes back a handle. This is what it looks like: */ @@ -88,9 +68,6 @@ struct handle { int remaining; /* number of pointers remaining */ }; -/* declarations for load_avg */ -#include "loadavg.h" - #define PP(pp, field) ((pp)->kp_proc . field) #define EP(pp, field) ((pp)->kp_eproc . field) #define VP(pp, field) ((pp)->kp_eproc.e_vm . field) @@ -102,7 +79,8 @@ struct handle { * These definitions control the format of the per-process area */ static char header[] = -" PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND"; + " PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND"; + /* 0123456 -- field to fill in starts at header+6 */ #define UNAME_START 6 @@ -144,16 +122,14 @@ char *cpustatenames[] = { int memory_stats[8]; char *memorynames[] = { "Real: ", "K/", "K act/tot ", "Free: ", "K ", -#ifdef DOSWAP "Swap: ", "K/", "K used/tot", -#endif NULL }; -#ifdef ORDER /* these are names given to allowed sorting orders -- first is default */ -char *ordernames[] = {"cpu", "size", "res", "time", "pri", NULL}; -#endif +char *ordernames[] = { + "cpu", "size", "res", "time", "pri", NULL +}; /* these are for keeping track of the proc array */ static int nproc; @@ -216,9 +192,7 @@ machine_init(struct statics *statics) statics->procstate_names = procstatenames; statics->cpustate_names = cpustatenames; statics->memory_names = memorynames; -#ifdef ORDER statics->order_names = ordernames; -#endif return (0); } @@ -234,8 +208,7 @@ format_header(char *uname_field) } void -get_system_info(si) - struct system_info *si; +get_system_info(struct system_info *si) { static int sysload_mib[] = {CTL_VM, VM_LOADAVG}; static int vmtotal_mib[] = {CTL_VM, VM_METER}; @@ -276,12 +249,11 @@ get_system_info(si) memory_stats[3] = -1; memory_stats[4] = pagetok(vmtotal.t_free); memory_stats[5] = -1; -#ifdef DOSWAP + if (!swapmode(&memory_stats[6], &memory_stats[7])) { memory_stats[6] = 0; memory_stats[7] = 0; } -#endif /* set arrays and strings */ si->cpustates = cpu_states; @@ -295,12 +267,15 @@ struct kinfo_proc * getprocs(int op, int arg, int *cnt) { size_t size = sizeof(int); - int mib[4] = {CTL_KERN, KERN_PROC, op, arg}; + int mib[4] = {CTL_KERN, KERN_PROC, 0, 0}; int smib[2] = {CTL_KERN, KERN_NPROCS}; static int maxslp_mib[] = {CTL_VM, VM_MAXSLP}; static struct kinfo_proc *procbase; int st; + mib[2] = op; + mib[3] = arg; + size = sizeof(maxslp); if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) { warn("sysctl vm.maxslp failed"); @@ -358,7 +333,6 @@ get_process_info(struct system_info *si, struct process_select *sel, show_idle = sel->idle; show_system = sel->system; show_uid = sel->uid != -1; - show_command = sel->command != NULL; /* count up process states and get pointers to interesting procs */ total_procs = 0; @@ -470,7 +444,6 @@ static unsigned char sorted_state[] = 3, /* stop */ 1 /* zombie */ }; -#ifdef ORDER /* * proc_compares - comparison functions for "qsort" @@ -619,7 +592,7 @@ compare_prio(const void *v1, const void *v2) return (result); } -int (*proc_compares[]) () = { +int (*proc_compares[])(const void *, const void *) = { compare_cpu, compare_size, compare_res, @@ -628,62 +601,6 @@ int (*proc_compares[]) () = { NULL }; -#else - -/* - * proc_compare - comparison function for "qsort" - * Compares the resource consumption of two processes using five - * distinct keys. The keys (in descending order of importance) are: - * percent cpu, cpu ticks, state, resident set size, total virtual - * memory usage. The process states are ordered as follows (from least - * to most important): zombie, sleep, stop, start, run. The array - * declaration below maps a process state index into a number that - * reflects this ordering. - */ -int -proc_compare(const void *v1, const void *v2) -{ - struct proc **pp1 = (struct proc **) v1; - struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1, *p2; - pctcpu lresult; - int result; - - /* remove one level of indirection */ - p1 = *(struct kinfo_proc **) pp1; - p2 = *(struct kinfo_proc **) pp2; - - /* compare percent cpu (pctcpu) */ - if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0) { - /* use CPU usage to break the tie */ - if ((result = PP(p2, p_rtime).tv_sec - PP(p1, p_rtime).tv_sec) == 0) { - /* use process state to break the tie */ - if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - - sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) { - /* use priority to break the tie */ - if ((result = PP(p2, p_priority) - - PP(p1, p_priority)) == 0) { - /* - * use resident set size (rssize) to - * break the tie - */ - if ((result = VP(p2, vm_rssize) - - VP(p1, vm_rssize)) == 0) { - /* - * use total memory to break - * the tie - */ - result = PROCSIZE(p2) - PROCSIZE(p1); - } - } - } - } - } else - result = lresult < 0 ? -1 : 1; - return (result); -} -#endif - /* * proc_owner(pid) - returns the uid that owns process "pid", or -1 if * the process does not exist. @@ -709,7 +626,6 @@ proc_owner(pid_t pid) return (-1); } -#ifdef DOSWAP /* * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org> * to be based on the new swapctl(2) system call. @@ -745,4 +661,3 @@ swapmode(int *used, int *total) free(swdev); return 1; } -#endif diff --git a/usr.bin/top/machine.h b/usr.bin/top/machine.h index 97d1d24bf24..5fea2eaede4 100644 --- a/usr.bin/top/machine.h +++ b/usr.bin/top/machine.h @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.h,v 1.6 2002/07/15 17:20:36 deraadt Exp $ */ +/* $OpenBSD: machine.h,v 1.7 2003/06/13 21:52:25 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -37,33 +37,31 @@ /* * the statics struct is filled in by machine_init */ -struct statics -{ - char **procstate_names; - char **cpustate_names; - char **memory_names; -#ifdef ORDER - char **order_names; -#endif +struct statics { + char **procstate_names; + char **cpustate_names; + char **memory_names; + char **order_names; }; /* * the system_info struct is filled in by a machine dependent routine. */ -struct system_info -{ - pid_t last_pid; - double load_avg[NUM_AVERAGES]; - int p_total; - int p_active; /* number of procs considered "active" */ - int *procstates; - int *cpustates; - int *memory; +struct system_info { + pid_t last_pid; + double load_avg[NUM_AVERAGES]; + int p_total; + int p_active; /* number of procs considered + * "active" */ + int *procstates; + int *cpustates; + int *memory; }; -/* cpu_states is an array of percentages * 10. For example, - the (integer) value 105 is 10.5% (or .105). +/* + * cpu_states is an array of percentages * 10. For example, the (integer) + * value 105 is 10.5% (or .105). */ /* @@ -71,23 +69,23 @@ struct system_info * are interested in seeing */ -struct process_select -{ - int idle; /* show idle processes */ - int system; /* show system processes */ - int uid; /* only this uid (unless uid == -1) */ - char *command; /* only this command (unless == NULL) */ +struct process_select { + int idle; /* show idle processes */ + int system; /* show system processes */ + int uid; /* only this uid (unless uid == -1) */ + char *command;/* only this command (unless == NULL) */ }; /* prototypes */ -extern int display_init(struct statics *); +extern int display_init(struct statics *); /* machine.c */ -extern int machine_init(struct statics *); -extern char *format_header(char *); -extern void get_system_info(struct system_info *); -extern caddr_t get_process_info(struct system_info *, struct process_select *, - int (*)(const void *, const void *)); -extern char *format_next_process(caddr_t, char *(*)()); -extern int proc_compate(const void *, const void *); -extern int proc_owner(pid_t); +extern int machine_init(struct statics *); +extern char *format_header(char *); +extern void get_system_info(struct system_info *); +extern caddr_t +get_process_info(struct system_info *, struct process_select *, + int (*) (const void *, const void *)); +extern char *format_next_process(caddr_t, char *(*) ()); +extern int proc_compate(const void *, const void *); +extern int proc_owner(pid_t); diff --git a/usr.bin/top/screen.c b/usr.bin/top/screen.c index 027896e98d9..f13279544b4 100644 --- a/usr.bin/top/screen.c +++ b/usr.bin/top/screen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: screen.c,v 1.10 2003/06/12 23:09:30 deraadt Exp $ */ +/* $OpenBSD: screen.c,v 1.11 2003/06/13 21:52:25 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -51,7 +51,7 @@ #include "screen.h" #include "boolean.h" -extern char *myname; +extern char *__progname; int overstrike; int screen_length; @@ -109,10 +109,10 @@ init_termcap(int interactive) /* now get the termcap entry */ if ((status = tgetent(NULL, term_name)) != 1) { if (status == -1) - fprintf(stderr, "%s: can't open termcap file\n", myname); + fprintf(stderr, "%s: can't open termcap file\n", __progname); else fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n", - myname, term_name); + __progname, term_name); /* pretend it's dumb and proceed */ smart_terminal = No; diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c index 2b1ceddffe0..8bde67d651d 100644 --- a/usr.bin/top/top.c +++ b/usr.bin/top/top.c @@ -1,6 +1,4 @@ -/* $OpenBSD: top.c,v 1.19 2003/06/12 22:30:23 pvalchev Exp $ */ - -const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; +/* $OpenBSD: top.c,v 1.20 2003/06/13 21:52:25 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -30,20 +28,7 @@ const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * See the file "Changes" for information on version-to-version changes. - */ - -/* - * This file contains "main" and other high-level routines. - */ - -/* - * The following preprocessor variables, when defined, are used to - * distinguish between different Unix implementations: - * - * FD_SET - macros FD_SET and FD_ZERO are used when defined - */ +const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; #include <sys/types.h> #include <stdio.h> @@ -66,119 +51,76 @@ const char copyright[] = "Copyright (c) 1984 through 1996, William LeFebvre"; #include "utils.h" /* Size of the stdio buffer given to stdout */ -#define Buffersize 2048 +#define BUFFERSIZE 2048 /* The buffer that stdio will use */ -char stdoutbuf[Buffersize]; +char stdoutbuf[BUFFERSIZE]; -/* imported from screen.c */ -extern int overstrike; +extern int overstrike; /* signal handling routines */ -static void leave(int); -static void onalrm(int); -static void tstop(int); +static void leave(int); +static void onalrm(int); +static void tstop(int); #ifdef SIGWINCH -static void winch(int); +static void winch(int); #endif volatile sig_atomic_t leaveflag; volatile sig_atomic_t tstopflag; volatile sig_atomic_t winchflag; -static void reset_display(void); +static void reset_display(void); +int rundisplay(void); /* values which need to be accessed by signal handlers */ -static int max_topn; /* maximum displayable processes */ +static int max_topn; /* maximum displayable processes */ /* miscellaneous things */ -char *myname = "top"; -jmp_buf jmp_int; +jmp_buf jmp_int; /* routines that don't return int */ -#ifdef ORDER -extern int (*proc_compares[])(); -#else -extern int proc_compare(); -#endif -time_t time(); +extern char *__progname; -caddr_t get_process_info(); +extern int (*proc_compares[])(const void *, const void *); +int order_index; /* pointers to display routines */ -void (*d_loadave)() = i_loadave; -void (*d_procstates)() = i_procstates; -void (*d_cpustates)() = i_cpustates; -void (*d_memory)() = i_memory; -void (*d_message)() = i_message; -void (*d_header)() = i_header; -void (*d_process)() = i_process; - +void (*d_loadave) () = i_loadave; +void (*d_procstates) () = i_procstates; +void (*d_cpustates) () = i_cpustates; +void (*d_memory) () = i_memory; +void (*d_message) () = i_message; +void (*d_header) () = i_header; +void (*d_process) () = i_process; + +int displays = 0; /* indicates unspecified */ +char do_unames = Yes; +struct process_select ps; +char dostates = No; +char interactive = Maybe; +char warnings = 0; +double delay = Default_DELAY; +char *order_name = NULL; +int topn = Default_TOPN; +int no_command = 1; -int -main(int argc, char *argv[]) -{ - int i; - int active_procs; - int change; - - struct system_info system_info; - struct statics statics; - caddr_t processes; - - static char tempbuf1[50]; - static char tempbuf2[50]; - sigset_t mask, oldmask; - int topn = Default_TOPN; - double delay = Default_DELAY; - int displays = 0; /* indicates unspecified */ - time_t curr_time; - char *(*get_userid)() = username; - char *uname_field = "USERNAME"; - char *header_text; - char *env_top; - char **preset_argv; - int preset_argc = 0; - char **av; - int ac; - char dostates = No; - char do_unames = Yes; - char interactive = Maybe; - char warnings = 0; #if Default_TOPN == Infinity - char topn_specified = No; -#endif - char ch; - char *iptr; - char no_command = 1; - struct timeval timeout; - struct process_select ps; -#ifdef ORDER - char *order_name = NULL; - int order_index = 0; +char topn_specified = No; #endif -#ifndef FD_SET - /* FD_SET and friends are not present: fake it */ - typedef int fd_set; -#define FD_ZERO(x) (*(x) = 0) -#define FD_SET(f, x) (*(x) = f) -#endif - fd_set readfds; -#ifdef ORDER - static char command_chars[] = "\f qh?en#sdkriIuSo"; -#else - static char command_chars[] = "\f qh?en#sdkriIuS"; -#endif -/* these defines enumerate the "strchr"s of the commands in command_chars */ +/* + * these defines enumerate the "strchr"s of the commands in + * command_chars + */ #define CMD_redraw 0 #define CMD_update 1 #define CMD_quit 2 #define CMD_help1 3 #define CMD_help2 4 -#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */ -#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */ +#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */ +#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */ #define CMD_number1 6 #define CMD_number2 7 #define CMD_delay 8 @@ -189,815 +131,728 @@ main(int argc, char *argv[]) #define CMD_idletog2 13 #define CMD_user 14 #define CMD_system 15 -#ifdef ORDER #define CMD_order 16 -#endif - /* set the buffer for stdout */ -#ifdef DEBUG - setbuffer(stdout, NULL, 0); -#else - setbuffer(stdout, stdoutbuf, Buffersize); -#endif +void +usage(void) +{ + fprintf(stderr, + "Top version %s\n" + "Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", + version_string(), __progname); +} - /* get our name */ - if (argc > 0) - { - if ((myname = strrchr(argv[0], '/')) == 0) - { - myname = argv[0]; - } - else - { - myname++; - } - } - - /* initialize some selection options */ - ps.idle = Yes; - ps.system = No; - ps.uid = -1; - ps.command = NULL; - - /* get preset options from the environment */ - if ((env_top = getenv("TOP")) != NULL) - { - av = preset_argv = argparse(env_top, &preset_argc); - ac = preset_argc; - - /* set the dummy argument to an explanatory message, in case - getopt encounters a bad argument */ - preset_argv[0] = "while processing environment"; - } - - /* process options */ - do { - /* if we're done doing the presets, then process the real arguments */ - if (preset_argc == 0) - { - ac = argc; - av = argv; - - /* this should keep getopt happy... */ - optind = 1; - } +void +parseargs(int ac, char **av) +{ + char *endp; + int i; + + while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) { + switch (i) { + case 'u': /* toggle uid/username display */ + do_unames = !do_unames; + break; + + case 'U': /* display only username's processes */ + if ((ps.uid = userid(optarg)) == -1) { + fprintf(stderr, "%s: unknown user\n", optarg); + exit(1); + } + break; - while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) - { - switch(i) - { - case 'u': /* toggle uid/username display */ - do_unames = !do_unames; - break; - - case 'U': /* display only username's processes */ - if ((ps.uid = userid(optarg)) == -1) - { - fprintf(stderr, "%s: unknown user\n", optarg); - exit(1); - } - break; - - case 'S': /* show system processes */ - ps.system = !ps.system; - break; - - case 'I': /* show idle processes */ - ps.idle = !ps.idle; - break; - - case 'i': /* go interactive regardless */ - interactive = Yes; - break; - - case 'n': /* batch, or non-interactive */ - case 'b': - interactive = No; - break; - - 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); - warnings++; - } - else - { - displays = i; - } - break; - - case 's': - { - char *endp; - - delay = strtod(optarg, &endp); - - if (delay < 0 || delay >= 1000000 || *endp != '\0') - { - fprintf(stderr, - "%s: warning: delay should be a non-negative number -- using default\n", - myname); - delay = Default_DELAY; - warnings++; - } + case 'S': /* show system processes */ + ps.system = !ps.system; + break; + + case 'I': /* show idle processes */ + ps.idle = !ps.idle; + break; + + case 'i': /* go interactive regardless */ + interactive = Yes; + break; + + case 'n': /* batch, or non-interactive */ + case 'b': + interactive = No; + break; + + case 'd': /* number of displays to show */ + if ((i = atoiwi(optarg)) != Invalid && i != 0) { + displays = i; + break; + } + fprintf(stderr, + "%s: warning: display count should be positive " + "-- option ignored\n", + __progname); + warnings++; + break; + + case 's': + delay = strtod(optarg, &endp); + + if (delay > 0 && delay <= 1000000 && *endp == '\0') + break; + + fprintf(stderr, + "%s: warning: delay should be a non-negative number" + " -- using default\n", + __progname); + delay = Default_DELAY; + warnings++; + break; + + case 'q': /* be quick about it */ + /* only allow this if user is really root */ + if (getuid() == 0) { + /* be very un-nice! */ + (void) nice(-20); + break; + } + fprintf(stderr, + "%s: warning: `-q' option can only be used by root\n", + __progname); + warnings++; + break; + + case 'o': /* select sort order */ + order_name = optarg; + break; + + default: + usage(); + exit(1); } - break; - - case 'q': /* be quick about it */ - /* only allow this if user is really root */ - if (getuid() == 0) - { - /* be very un-nice! */ - (void) nice(-20); + } + + /* get count of top processes to display (if any) */ + if (optind < ac) { + if ((topn = atoiwi(av[optind])) == Invalid) { + fprintf(stderr, + "%s: warning: process display count should " + "be non-negative -- using default\n", + __progname); + warnings++; } +#if Default_TOPN == Infinity else - { - fprintf(stderr, - "%s: warning: `-q' option can only be used by root\n", - myname); - warnings++; - } - break; + topn_specified = Yes; +#endif + } +} + +struct system_info system_info; +struct statics statics; - case 'o': /* select sort order */ -#ifdef ORDER - order_name = optarg; +int +main(int argc, char *argv[]) +{ + char *uname_field = "USERNAME", *header_text, *env_top; + char *(*get_userid)() = username, **preset_argv, **av; + int preset_argc = 0, ac, active_procs, i; + sigset_t mask, oldmask; + time_t curr_time; + caddr_t processes; + + /* set the buffer for stdout */ +#ifdef DEBUG + setbuffer(stdout, NULL, 0); #else - fprintf(stderr, - "%s: this platform does not support arbitrary ordering. Sorry.\n", - myname); - warnings++; + setbuffer(stdout, stdoutbuf, sizeof stdoutbuf); #endif - break; - default: - fprintf(stderr, "\ -Top version %s\n\ -Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", - version_string(), myname); + /* initialize some selection options */ + ps.idle = Yes; + ps.system = No; + ps.uid = -1; + ps.command = NULL; + + /* get preset options from the environment */ + if ((env_top = getenv("TOP")) != NULL) { + av = preset_argv = argparse(env_top, &preset_argc); + ac = preset_argc; + + /* + * set the dummy argument to an explanatory message, in case + * getopt encounters a bad argument + */ + preset_argv[0] = "while processing environment"; + } + /* process options */ + do { + /* + * if we're done doing the presets, then process the real + * arguments + */ + if (preset_argc == 0) { + ac = argc; + av = argv; + optind = 1; + } + parseargs(ac, av); + i = preset_argc; + preset_argc = 0; + } while (i != 0); + + /* set constants for username/uid display correctly */ + if (!do_unames) { + uname_field = " UID "; + get_userid = itoa7; + } + /* initialize the kernel memory interface */ + if (machine_init(&statics) == -1) exit(1); - } + + /* determine sorting order index, if necessary */ + if (order_name != NULL) { + if ((order_index = string_index(order_name, + statics.order_names)) == -1) { + char **pp; + + fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n", + __progname, order_name); + fprintf(stderr, "\tTry one of these:"); + pp = statics.order_names; + while (*pp != NULL) + fprintf(stderr, " %s", *pp++); + fputc('\n', stderr); + exit(1); + } } - /* get count of top processes to display (if any) */ - if (optind < ac) - { - if ((topn = atoiwi(av[optind])) == Invalid) - { + /* initialize termcap */ + init_termcap(interactive); + + /* get the string to use for the process area header */ + header_text = format_header(uname_field); + + /* initialize display interface */ + if ((max_topn = display_init(&statics)) == -1) { + fprintf(stderr, "%s: can't allocate sufficient memory\n", __progname); + exit(4); + } + /* print warning if user requested more processes than we can display */ + if (topn > max_topn) { fprintf(stderr, - "%s: warning: process display count should be non-negative -- using default\n", - myname); + "%s: warning: this terminal can only display %d processes.\n", + __progname, max_topn); warnings++; - } + } + /* adjust for topn == Infinity */ + if (topn == Infinity) { + /* + * For smart terminals, infinity really means everything that can + * be displayed, or Largest. + * On dumb terminals, infinity means every process in the system! + * We only really want to do that if it was explicitly specified. + * This is always the case when "Default_TOPN != Infinity". But if + * topn wasn't explicitly specified and we are on a dumb terminal + * and the default is Infinity, then (and only then) we use + * "Nominal_TOPN" instead. + */ #if Default_TOPN == Infinity - else - { - topn_specified = Yes; - } + topn = smart_terminal ? Largest : + (topn_specified ? Largest : Nominal_TOPN); +#else + topn = Largest; #endif } + /* set header display accordingly */ + display_header(topn > 0); - /* tricky: remember old value of preset_argc & set preset_argc = 0 */ - i = preset_argc; - preset_argc = 0; - - /* repeat only if we really did the preset arguments */ - } while (i != 0); - - /* set constants for username/uid display correctly */ - if (!do_unames) - { - uname_field = " UID "; - get_userid = itoa7; - } - - /* initialize the kernel memory interface */ - if (machine_init(&statics) == -1) - { - exit(1); - } - -#ifdef ORDER - /* determine sorting order index, if necessary */ - if (order_name != NULL) - { - if ((order_index = string_index(order_name, statics.order_names)) == -1) - { - char **pp; - - fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n", - myname, order_name); - fprintf(stderr, "\tTry one of these:"); - pp = statics.order_names; - while (*pp != NULL) - { - fprintf(stderr, " %s", *pp++); - } - fputc('\n', stderr); - exit(1); - } - } -#endif + /* determine interactive state */ + if (interactive == Maybe) + interactive = smart_terminal; -#ifdef no_initialization_needed - /* initialize the hashing stuff */ - if (do_unames) - { - init_hash(); - } -#endif + /* if # of displays not specified, fill it in */ + if (displays == 0) + displays = smart_terminal ? Infinity : 1; - /* initialize termcap */ - init_termcap(interactive); - - /* get the string to use for the process area header */ - header_text = format_header(uname_field); - - /* initialize display interface */ - if ((max_topn = display_init(&statics)) == -1) - { - fprintf(stderr, "%s: can't allocate sufficient memory\n", myname); - exit(4); - } - - /* 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++; - } - - /* adjust for topn == Infinity */ - if (topn == Infinity) - { /* - * For smart terminals, infinity really means everything that can - * be displayed, or Largest. - * On dumb terminals, infinity means every process in the system! - * We only really want to do that if it was explicitly specified. - * This is always the case when "Default_TOPN != Infinity". But if - * topn wasn't explicitly specified and we are on a dumb terminal - * and the default is Infinity, then (and only then) we use - * "Nominal_TOPN" instead. + * block interrupt signals while setting up the screen and the + * handlers */ -#if Default_TOPN == Infinity - topn = smart_terminal ? Largest : - (topn_specified ? Largest : Nominal_TOPN); -#else - topn = Largest; -#endif - } - - /* set header display accordingly */ - display_header(topn > 0); - - /* determine interactive state */ - if (interactive == Maybe) - { - interactive = smart_terminal; - } - - /* if # of displays not specified, fill it in */ - if (displays == 0) - { - displays = smart_terminal ? Infinity : 1; - } - - /* block interrupt signals while setting up the screen and the handlers */ - sigemptyset(&mask); - sigaddset(&mask, SIGINT); - sigaddset(&mask, SIGQUIT); - sigaddset(&mask, SIGTSTP); - sigprocmask(SIG_BLOCK, &mask, &oldmask); - init_screen(); - (void) signal(SIGINT, leave); - (void) signal(SIGQUIT, leave); - (void) signal(SIGTSTP, tstop); + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGQUIT); + sigaddset(&mask, SIGTSTP); + sigprocmask(SIG_BLOCK, &mask, &oldmask); + init_screen(); + (void) signal(SIGINT, leave); + (void) signal(SIGQUIT, leave); + (void) signal(SIGTSTP, tstop); #ifdef SIGWINCH - (void) signal(SIGWINCH, winch); + (void) signal(SIGWINCH, winch); #endif - sigprocmask(SIG_SETMASK, &oldmask, NULL); - if (warnings) - { - fputs("....", stderr); - fflush(stderr); /* why must I do this? */ - sleep((unsigned)(3 * warnings)); - fputc('\n', stderr); - } - -restart: - - /* - * main loop -- repeat while display count is positive or while it - * indicates infinity (by being -1) - */ - - while ((displays == -1) || (displays-- > 0)) - { - /* get the current stats */ - get_system_info(&system_info); - - /* get the current set of processes */ - processes = - get_process_info(&system_info, - &ps, -#ifdef ORDER - proc_compares[order_index]); -#else - proc_compare); -#endif - - /* display the load averages */ - (*d_loadave)(system_info.last_pid, - system_info.load_avg); - - /* display the current time */ - /* this method of getting the time SHOULD be fairly portable */ - time(&curr_time); - i_timeofday(&curr_time); - - /* display process state breakdown */ - (*d_procstates)(system_info.p_total, - system_info.procstates); - - /* display the cpu state percentage breakdown */ - if (dostates) /* but not the first time */ - { - (*d_cpustates)(system_info.cpustates); + sigprocmask(SIG_SETMASK, &oldmask, NULL); + if (warnings) { + fputs("....", stderr); + fflush(stderr); /* why must I do this? */ + sleep((unsigned) (3 * warnings)); + fputc('\n', stderr); } - else - { - /* we'll do it next time */ - if (smart_terminal) - { - z_cpustates(); - } - else - { - if (putchar('\n') == EOF) - exit(1); - } - dostates = Yes; - } - - /* display memory stats */ - (*d_memory)(system_info.memory); - - /* handle message area */ - (*d_message)(); - - /* update the header area */ - (*d_header)(header_text); - - if (topn > 0) - { - /* determine number of processes to actually display */ - /* this number will be the smallest of: active processes, - number user requested, number current screen accommodates */ - active_procs = system_info.p_active; - if (active_procs > topn) - { - active_procs = topn; - } - if (active_procs > max_topn) - { - active_procs = max_topn; - } - - /* now show the top "n" processes. */ - for (i = 0; i < active_procs; i++) - { - (*d_process)(i, format_next_process(processes, get_userid)); - } - } - else - { - i = 0; - } - - /* do end-screen processing */ - u_endscreen(i); - - /* now, flush the output buffer */ - fflush(stdout); +restart: - /* only do the rest if we have more displays to show */ - if (displays) - { - /* switch out for new display on smart terminals */ - if (smart_terminal) - { - if (overstrike) - { - reset_display(); - } - else - { - d_loadave = u_loadave; - d_procstates = u_procstates; - d_cpustates = u_cpustates; - d_memory = u_memory; - d_message = u_message; - d_header = u_header; - d_process = u_process; - } - } - - no_command = Yes; - if (!interactive) - { - /* set up alarm */ - (void) signal(SIGALRM, onalrm); - (void) alarm((unsigned)delay); - - /* wait for the rest of it .... */ - pause(); - } - else while (no_command) - { - /* assume valid command unless told otherwise */ - no_command = No; - - /* set up arguments for select with timeout */ - FD_ZERO(&readfds); - FD_SET(STDIN_FILENO, &readfds); /* for standard input */ - timeout.tv_sec = (long)delay; - timeout.tv_usec = (long)((delay - timeout.tv_sec) * 1000000); - - if (leaveflag) { - end_screen(); - exit(0); + /* + * main loop -- repeat while display count is positive or while it + * indicates infinity (by being -1) + */ + while ((displays == -1) || (displays-- > 0)) { + /* get the current stats */ + get_system_info(&system_info); + + /* get the current set of processes */ + processes = get_process_info(&system_info, &ps, + proc_compares[order_index]); + + /* display the load averages */ + (*d_loadave)(system_info.last_pid, system_info.load_avg); + + /* display the current time */ + /* this method of getting the time SHOULD be fairly portable */ + time(&curr_time); + i_timeofday(&curr_time); + + /* display process state breakdown */ + (*d_procstates)(system_info.p_total, system_info.procstates); + + /* display the cpu state percentage breakdown */ + if (dostates) { /* but not the first time */ + (*d_cpustates) (system_info.cpustates); + } else { + /* we'll do it next time */ + if (smart_terminal) + z_cpustates(); + else { + if (putchar('\n') == EOF) + exit(1); + } + dostates = Yes; } - if (tstopflag) { - /* move to the lower left */ - end_screen(); - fflush(stdout); - - /* default the signal handler action */ - (void) signal(SIGTSTP, SIG_DFL); - - /* unblock the signal and send ourselves one */ - sigemptyset(&mask); - sigaddset(&mask, SIGTSTP); - sigprocmask(SIG_UNBLOCK, &mask, NULL); - (void) kill(0, SIGTSTP); - - /* reset the signal handler */ - (void) signal(SIGTSTP, tstop); - - /* reinit screen */ - reinit_screen(); - reset_display(); - tstopflag = 0; - goto restart; + /* display memory stats */ + (*d_memory) (system_info.memory); + + /* handle message area */ + (*d_message) (); + + /* update the header area */ + (*d_header) (header_text); + + if (topn > 0) { + /* determine number of processes to actually display */ + /* + * this number will be the smallest of: active + * processes, number user requested, number current + * screen accommodates + */ + active_procs = system_info.p_active; + if (active_procs > topn) + active_procs = topn; + if (active_procs > max_topn) + active_procs = max_topn; + /* now show the top "n" processes. */ + for (i = 0; i < active_procs; i++) + (*d_process)(i, format_next_process(processes, + get_userid)); + } else + i = 0; + + /* do end-screen processing */ + u_endscreen(i); + + /* now, flush the output buffer */ + fflush(stdout); + + /* only do the rest if we have more displays to show */ + if (displays) { + /* switch out for new display on smart terminals */ + if (smart_terminal) { + if (overstrike) { + reset_display(); + } else { + d_loadave = u_loadave; + d_procstates = u_procstates; + d_cpustates = u_cpustates; + d_memory = u_memory; + d_message = u_message; + d_header = u_header; + d_process = u_process; + } + } + no_command = Yes; + if (!interactive) { + /* set up alarm */ + (void) signal(SIGALRM, onalrm); + (void) alarm((unsigned) delay); + + /* wait for the rest of it .... */ + pause(); + } else { + while (no_command) + if (rundisplay()) + goto restart; + } } + } - if (winchflag) { - /* reascertain the screen dimensions */ - get_screensize(); - - /* tell display to resize */ - max_topn = display_resize(); + quit(0); + /* NOTREACHED */ + return (0); +} - /* reset the signal handler */ - (void) signal(SIGWINCH, winch); +int +rundisplay(void) +{ + static char tempbuf1[50], tempbuf2[50]; + struct timeval timeout; + fd_set readfds; + sigset_t mask; + char ch, *iptr; + int change, i; + static char command_chars[] = "\f qh?en#sdkriIuSo"; - reset_display(); - winchflag = 0; - goto restart; - } + /* + * assume valid command unless told + * otherwise + */ + no_command = No; - /* wait for either input or the end of the delay period */ - if (select(STDIN_FILENO + 1, &readfds, (fd_set *)NULL, - (fd_set *)NULL, &timeout) > 0) - { - int newval; - char *errmsg; - - /* something to read -- clear the message area first */ - clear_message(); - - /* now read it and convert to command strchr */ - /* (use "change" as a temporary to hold strchr) */ - (void) read(0, &ch, 1); - if ((iptr = strchr(command_chars, ch)) == NULL) - { + /* + * set up arguments for select with + * timeout + */ + FD_ZERO(&readfds); + FD_SET(STDIN_FILENO, &readfds); + timeout.tv_sec = (long) delay; + timeout.tv_usec = (long) ((delay - timeout.tv_sec) * 1000000); + + if (leaveflag) { + end_screen(); + exit(0); + } + if (tstopflag) { + /* move to the lower left */ + end_screen(); + fflush(stdout); + + /* + * default the signal handler + * action + */ + (void) signal(SIGTSTP, SIG_DFL); + + /* + * unblock the signal and + * send ourselves one + */ + sigemptyset(&mask); + sigaddset(&mask, SIGTSTP); + sigprocmask(SIG_UNBLOCK, &mask, NULL); + (void) kill(0, SIGTSTP); + + /* reset the signal handler */ + (void) signal(SIGTSTP, tstop); + + /* reinit screen */ + reinit_screen(); + reset_display(); + tstopflag = 0; + return 1; + } + if (winchflag) { + /* + * reascertain the screen + * dimensions + */ + get_screensize(); + + /* tell display to resize */ + max_topn = display_resize(); + + /* reset the signal handler */ + (void) signal(SIGWINCH, winch); + + reset_display(); + winchflag = 0; + return 1; + } + /* + * wait for either input or the end + * of the delay period + */ + if (select(STDIN_FILENO + 1, &readfds, (fd_set *) NULL, + (fd_set *) NULL, &timeout) > 0) { + char *errmsg; + int newval; + + clear_message(); + + /* + * now read it and convert to + * command strchr + */ + (void) read(0, &ch, 1); + if ((iptr = strchr(command_chars, ch)) == NULL) { /* illegal command */ new_message(MT_standout, " Command not understood"); if (putchar('\r') == EOF) - exit(1); + exit(1); no_command = Yes; - } - else - { - change = iptr - command_chars; - if (overstrike && change > CMD_OSLIMIT) - { - /* error */ - new_message(MT_standout, + fflush(stdout); + return (0); + } + + change = iptr - command_chars; + if (overstrike && change > CMD_OSLIMIT) { + /* error */ + new_message(MT_standout, " Command cannot be handled by this terminal"); - if (putchar('\r') == EOF) - exit(1); - no_command = Yes; + if (putchar('\r') == EOF) + exit(1); + no_command = Yes; + fflush(stdout); + return (0); + } + + switch (change) { + case CMD_redraw: /* redraw screen */ + reset_display(); + break; + + case CMD_update: /* merely update display */ + /* + * is the load average high? + */ + if (system_info.load_avg[0] > LoadMax) { + /* yes, go home for visual feedback */ + go_home(); + fflush(stdout); } - else switch(change) - { - case CMD_redraw: /* redraw screen */ - reset_display(); - break; - - case CMD_update: /* merely update display */ - /* is the load average high? */ - if (system_info.load_avg[0] > LoadMax) - { - /* yes, go home for visual feedback */ - go_home(); - fflush(stdout); - } - break; - - case CMD_quit: /* quit */ - quit(0); - /*NOTREACHED*/ - break; - - case CMD_help1: /* help */ - case CMD_help2: + break; + + case CMD_quit: /* quit */ + quit(0); + break; + + case CMD_help1: /* help */ + case CMD_help2: + reset_display(); + clear(); + show_help(); + standout("Hit any key to continue: "); + fflush(stdout); + (void) read(0, &ch, 1); + break; + + case CMD_errors: /* show errors */ + if (error_count() == 0) { + new_message(MT_standout, + " Currently no errors to report."); + if (putchar('\r') == EOF) + exit(1); + no_command = Yes; + } else { reset_display(); clear(); - show_help(); + show_errors(); standout("Hit any key to continue: "); fflush(stdout); (void) read(0, &ch, 1); - break; - - case CMD_errors: /* show errors */ - if (error_count() == 0) - { - new_message(MT_standout, - " Currently no errors to report."); - if (putchar('\r') == EOF) - exit(1); - no_command = Yes; - } - else - { - reset_display(); - clear(); - show_errors(); - standout("Hit any key to continue: "); - fflush(stdout); - (void) read(0, &ch, 1); - } - break; - - case CMD_number1: /* new number */ - case CMD_number2: - new_message(MT_standout, - "Number of processes to show: "); - newval = readline(tempbuf1, 8, Yes); - if (newval > -1) - { - if (newval > max_topn) - { + } + break; + + case CMD_number1: /* new number */ + case CMD_number2: + new_message(MT_standout, + "Number of processes to show: "); + newval = readline(tempbuf1, 8, Yes); + if (newval > -1) { + if (newval > max_topn) { new_message(MT_standout | MT_delayed, - " This terminal can only display %d processes.", - max_topn); + " This terminal can only " + "display %d processes.", + max_topn); if (putchar('\r') == EOF) - exit(1); - } - - if (newval == 0) - { - /* inhibit the header */ + exit(1); + } + if (newval == 0) display_header(No); - } - else if (newval > topn && topn == 0) - { + else if (newval > topn && topn == 0) { /* redraw the header */ display_header(Yes); d_header = i_header; - } - topn = newval; } - break; - - case CMD_delay: /* new seconds delay */ - new_message(MT_standout, "Seconds to delay: "); - if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) - { - char *endp; - double newdelay = strtod(tempbuf2, &endp); - if (newdelay >= 0 && newdelay < 1000000 && *endp == '\0') - { + topn = newval; + } + break; + + case CMD_delay: /* new seconds delay */ + new_message(MT_standout, "Seconds to delay: "); + if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { + char *endp; + double newdelay = strtod(tempbuf2, &endp); + + if (newdelay >= 0 && newdelay < 1000000 && + *endp == '\0') delay = newdelay; - } - } - clear_message(); - break; - - case CMD_displays: /* change display count */ - new_message(MT_standout, - "Displays to show (currently %s): ", - displays == -1 ? "infinite" : - itoa(displays)); - if ((i = readline(tempbuf1, 10, Yes)) > 0) - { - displays = i; - } - else if (i == 0) - { - quit(0); - } - clear_message(); - break; - - case CMD_kill: /* kill program */ - new_message(0, "kill "); - if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) - { - if ((errmsg = kill_procs(tempbuf2)) != NULL) - { + } + clear_message(); + break; + + case CMD_displays: /* change display count */ + new_message(MT_standout, + "Displays to show (currently %s): ", + displays == -1 ? "infinite" : + itoa(displays)); + if ((i = readline(tempbuf1, 10, Yes)) > 0) + displays = i; + else if (i == 0) + quit(0); + + clear_message(); + break; + + case CMD_kill: /* kill program */ + new_message(0, "kill "); + if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { + if ((errmsg = kill_procs(tempbuf2)) != NULL) { new_message(MT_standout, "%s", errmsg); if (putchar('\r') == EOF) - exit(1); + exit(1); no_command = Yes; - } } - else - { - clear_message(); - } - break; - - case CMD_renice: /* renice program */ - new_message(0, "renice "); - if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) - { - if ((errmsg = renice_procs(tempbuf2)) != NULL) - { + } else + clear_message(); + break; + + case CMD_renice: /* renice program */ + new_message(0, "renice "); + if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { + if ((errmsg = renice_procs(tempbuf2)) != NULL) { new_message(MT_standout, "%s", errmsg); if (putchar('\r') == EOF) - exit(1); + exit(1); no_command = Yes; - } } - else - { - clear_message(); - } - break; - - case CMD_idletog: - case CMD_idletog2: - ps.idle = !ps.idle; - new_message(MT_standout | MT_delayed, - " %sisplaying idle processes.", - ps.idle ? "D" : "Not d"); - if (putchar('\r') == EOF) - exit(1); - break; - - case CMD_user: - new_message(MT_standout, - "Username to show: "); - if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) - { - if (tempbuf2[0] == '+' && - tempbuf2[1] == '\0') - { + } else + clear_message(); + break; + + case CMD_idletog: + case CMD_idletog2: + ps.idle = !ps.idle; + new_message(MT_standout | MT_delayed, + " %sisplaying idle processes.", + ps.idle ? "D" : "Not d"); + if (putchar('\r') == EOF) + exit(1); + break; + + case CMD_user: + new_message(MT_standout, + "Username to show: "); + if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { + if (tempbuf2[0] == '+' && + tempbuf2[1] == '\0') { ps.uid = -1; - } - else if ((i = userid(tempbuf2)) == -1) - { + } else if ((i = userid(tempbuf2)) == -1) { new_message(MT_standout, " %s: unknown user", tempbuf2); no_command = Yes; - } - else - { + } else ps.uid = i; - } - if (putchar('\r') == EOF) - exit(1); - } - else - { - clear_message(); - } - break; - - case CMD_system: - ps.system = !ps.system; - new_message(MT_standout | MT_delayed, - " %sisplaying system processes.", - ps.system ? "D" : "Not d"); - break; - -#ifdef ORDER - case CMD_order: - new_message(MT_standout, - "Order to sort: "); - if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) - { - if ((i = string_index(tempbuf2, statics.order_names)) == -1) - { - new_message(MT_standout, - " %s: unrecognized sorting order", tempbuf2); - no_command = Yes; - } - else - { + if (putchar('\r') == EOF) + exit(1); + } else + clear_message(); + break; + + case CMD_system: + ps.system = !ps.system; + new_message(MT_standout | MT_delayed, + " %sisplaying system processes.", + ps.system ? "D" : "Not d"); + break; + + case CMD_order: + new_message(MT_standout, + "Order to sort: "); + if (readline(tempbuf2, + sizeof(tempbuf2), No) > 0) { + if ((i = string_index(tempbuf2, + statics.order_names)) == -1) { + new_message(MT_standout, + " %s: unrecognized sorting order", + tempbuf2); + no_command = Yes; + } else order_index = i; - } - if (putchar('\r') == EOF) - exit(1); - } - else - { - clear_message(); - } - break; -#endif - - default: - new_message(MT_standout, " BAD CASE IN SWITCH!"); if (putchar('\r') == EOF) - exit(1); - } - } + exit(1); + } else + clear_message(); + break; - /* flush out stuff that may have been written */ - fflush(stdout); + default: + new_message(MT_standout, " BAD CASE IN SWITCH!"); + if (putchar('\r') == EOF) + exit(1); } - } } - } - quit(0); - /*NOTREACHED*/ - return(0); + /* flush out stuff that may have been written */ + fflush(stdout); + return 0; } + /* * reset_display() - reset all the display routine pointers so that entire * screen will get redrawn. */ - static void reset_display(void) { - d_loadave = i_loadave; - d_procstates = i_procstates; - d_cpustates = i_cpustates; - d_memory = i_memory; - d_message = i_message; - d_header = i_header; - d_process = i_process; + d_loadave = i_loadave; + d_procstates = i_procstates; + d_cpustates = i_cpustates; + d_memory = i_memory; + d_message = i_message; + d_header = i_header; + d_process = i_process; } -/* - * signal handlers - */ - void -leave(int unused) /* exit under normal conditions -- INT handler */ +leave(int signo) { - leaveflag = 1; + leaveflag = 1; } void -tstop(int i) /* SIGTSTP handler */ +tstop(int signo) { - tstopflag = 1; + tstopflag = 1; } #ifdef SIGWINCH void -winch(int i) /* SIGWINCH handler */ +winch(int signo) { - winchflag = 1; + winchflag = 1; } #endif void -quit(int status) /* exit under duress */ +onalrm(int signo) { - end_screen(); - exit(status); - /*NOTREACHED*/ } void -onalrm(int unused) /* SIGALRM handler */ +quit(int ret) { - /* this is only used in batch mode to break out of the pause() */ - /* return; */ + end_screen(); + exit(ret); } - diff --git a/usr.bin/top/top.h b/usr.bin/top/top.h index 94e4ed71390..d99393704e5 100644 --- a/usr.bin/top/top.h +++ b/usr.bin/top/top.h @@ -1,4 +1,4 @@ -/* $OpenBSD: top.h,v 1.4 2002/07/15 17:20:36 deraadt Exp $ */ +/* $OpenBSD: top.h,v 1.5 2003/06/13 21:52:25 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -77,7 +77,6 @@ extern char *renice_procs(char *); extern void quit(int); /* username.c */ -extern void init_hash(void); extern char *username(uid_t); extern uid_t userid(char *); diff --git a/usr.bin/top/username.c b/usr.bin/top/username.c index a718529d7ff..5c854167abc 100644 --- a/usr.bin/top/username.c +++ b/usr.bin/top/username.c @@ -1,4 +1,4 @@ -/* $OpenBSD: username.c,v 1.9 2003/06/12 23:09:30 deraadt Exp $ */ +/* $OpenBSD: username.c,v 1.10 2003/06/13 21:52:25 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -76,16 +76,6 @@ static int get_user(uid_t); /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ struct hash_el hash_table[Table_size]; -void -init_hash(void) -{ - /* - * There used to be some steps we had to take to initialize things. - * We don't need to do that anymore, but we will leave this stub in - * just in case future changes require initialization steps. - */ -} - char * username(uid_t uid) { @@ -108,7 +98,6 @@ userid(char *username) * Eventually we want this to enter everything in the hash table, but * for now we just do it simply and remember just the result. */ - if ((pwd = getpwnam(username)) == NULL) return (-1); |