diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2003-06-12 23:09:31 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2003-06-12 23:09:31 +0000 |
commit | 23fc31409a630b6090b03af12f3645ec89b464b9 (patch) | |
tree | feb94967f414aca9e216b57fb39892afcbac8ce2 /usr.bin | |
parent | f578f4296df2db8bc10718f8fb418ac1699f1785 (diff) |
knf
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/top/machine.c | 285 | ||||
-rw-r--r-- | usr.bin/top/screen.c | 465 | ||||
-rw-r--r-- | usr.bin/top/username.c | 169 | ||||
-rw-r--r-- | usr.bin/top/utils.c | 506 | ||||
-rw-r--r-- | usr.bin/top/version.c | 11 |
5 files changed, 654 insertions, 782 deletions
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 6dd238e7483..e6b819442ff 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.c,v 1.29 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: machine.c,v 1.30 2003/06/12 23:09:29 deraadt Exp $ */ /*- * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com> @@ -74,7 +74,7 @@ #include <err.h> #endif -static int swapmode(int *, int *); +static int swapmode(int *, int *); #include "top.h" #include "display.h" @@ -85,7 +85,7 @@ static int swapmode(int *, int *); struct handle { struct kinfo_proc **next_proc; /* points to next valid proc pointer */ - int remaining; /* number of pointers remaining */ + int remaining; /* number of pointers remaining */ }; /* declarations for load_avg */ @@ -101,7 +101,7 @@ struct handle { /* * These definitions control the format of the per-process area */ -static char header[] = +static char header[] = " PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND"; /* 0123456 -- field to fill in starts at header+6 */ #define UNAME_START 6 @@ -109,39 +109,40 @@ static char header[] = #define Proc_format \ "%5d %-8.8s %3d %4d %5s %5s %-5s %-6.6s %6s %5.2f%% %.14s" - /* process state names for the "STATE" column of the display */ -/* the extra nulls in the string "run" are for adding a slash and - the processor number when needed */ +/* + * the extra nulls in the string "run" are for adding a slash and the + * processor number when needed + */ -char *state_abbrev[] = { +char *state_abbrev[] = { "", "start", "run\0\0\0", "sleep", "stop", "zomb", }; - -static int stathz; +static int stathz; /* these are for calculating cpu state percentages */ -static long cp_time[CPUSTATES]; -static long cp_old[CPUSTATES]; -static long cp_diff[CPUSTATES]; +static long cp_time[CPUSTATES]; +static long cp_old[CPUSTATES]; +static long cp_diff[CPUSTATES]; /* these are for detailing the process states */ -int process_states[7]; -char *procstatenames[] = { - "", " starting, ", " running, ", " idle, ", " stopped, ", " zombie, ", +int process_states[7]; +char *procstatenames[] = { + "", " starting, ", " running, ", " idle, ", + " stopped, ", " zombie, ", NULL }; /* these are for detailing the cpu states */ -int cpu_states[CPUSTATES]; -char *cpustatenames[] = { +int cpu_states[CPUSTATES]; +char *cpustatenames[] = { "user", "nice", "system", "interrupt", "idle", NULL }; /* these are for detailing the memory statistics */ -int memory_stats[8]; -char *memorynames[] = { +int memory_stats[8]; +char *memorynames[] = { "Real: ", "K/", "K act/tot ", "Free: ", "K ", #ifdef DOSWAP "Swap: ", "K/", "K used/tot", @@ -151,30 +152,30 @@ char *memorynames[] = { #ifdef ORDER /* these are names given to allowed sorting orders -- first is default */ -char *ordernames[] = {"cpu", "size", "res", "time", "pri", NULL}; +char *ordernames[] = {"cpu", "size", "res", "time", "pri", NULL}; #endif /* these are for keeping track of the proc array */ -static int nproc; -static int onproc = -1; -static int pref_len; +static int nproc; +static int onproc = -1; +static int pref_len; static struct kinfo_proc *pbase; static struct kinfo_proc **pref; /* these are for getting the memory statistics */ -static int pageshift; /* log base 2 of the pagesize */ +static int pageshift; /* log base 2 of the pagesize */ /* define pagetok in terms of pageshift */ #define pagetok(size) ((size) << pageshift) -int maxslp; +int maxslp; int getstathz(void) { struct clockinfo cinf; - size_t size = sizeof(cinf); - int mib[2]; + size_t size = sizeof(cinf); + int mib[2]; mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; @@ -197,8 +198,10 @@ machine_init(struct statics *statics) onproc = -1; nproc = 0; - /* get the page size with "getpagesize" and calculate pageshift from - * it */ + /* + * get the page size with "getpagesize" and calculate pageshift from + * it + */ pagesize = getpagesize(); pageshift = 0; while (pagesize > 1) { @@ -225,9 +228,8 @@ format_header(char *uname_field) char *ptr; ptr = header + UNAME_START; - while (*uname_field != '\0') { + while (*uname_field != '\0') *ptr++ = *uname_field++; - } return (header); } @@ -237,19 +239,18 @@ get_system_info(si) { static int sysload_mib[] = {CTL_VM, VM_LOADAVG}; static int vmtotal_mib[] = {CTL_VM, VM_METER}; - static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME }; + static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; struct loadavg sysload; struct vmtotal vmtotal; double *infoloadp; int total, i; - size_t size; - + size_t size; + size = sizeof(cp_time); if (sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0) < 0) { warn("sysctl kern.cp_time failed"); total = 0; } - size = sizeof(sysload); if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0) { warn("sysctl failed"); @@ -305,7 +306,6 @@ getprocs(int op, int arg, int *cnt) warn("sysctl vm.maxslp failed"); return (0); } - st = sysctl(smib, 2, cnt, &size, NULL, 0); if (st == -1) { /* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */ @@ -314,7 +314,7 @@ getprocs(int op, int arg, int *cnt) if (procbase) free(procbase); size = (6 * (*cnt) * sizeof(struct kinfo_proc)) / 5; - procbase = (struct kinfo_proc *)malloc(size); + procbase = (struct kinfo_proc *) malloc(size); if (procbase == NULL) return (0); st = sysctl(mib, 4, procbase, &size, NULL, 0); @@ -323,17 +323,18 @@ getprocs(int op, int arg, int *cnt) return (0); } if (size % sizeof(struct kinfo_proc) != 0) { - /* _kvm_err(kd, kd->program, - "proc size mismatch (%d total, %d chunks)", - size, sizeof(struct kinfo_proc)); */ + /* + * _kvm_err(kd, kd->program, "proc size mismatch (%d total, + * %d chunks)", size, sizeof(struct kinfo_proc)); + */ return (0); } return (procbase); } -caddr_t +caddr_t get_process_info(struct system_info *si, struct process_select *sel, - int (*compare)(const void *, const void *)) + int (*compare) (const void *, const void *)) { int show_idle, show_system, show_uid, show_command; int total_procs, active_procs, i; @@ -344,8 +345,8 @@ get_process_info(struct system_info *si, struct process_select *sel, quit(23); } if (nproc > onproc) - pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) - * (onproc = nproc)); + pref = (struct kinfo_proc **)realloc(pref, + sizeof(struct kinfo_proc *) * (onproc = nproc)); if (pref == NULL) { warnx("Out of memory."); quit(23); @@ -377,7 +378,7 @@ get_process_info(struct system_info *si, struct process_select *sel, process_states[(unsigned char) PP(pp, p_stat)]++; if ((PP(pp, p_stat) != SZOMB) && (show_idle || (PP(pp, p_pctcpu) != 0) || - (PP(pp, p_stat) == SRUN)) && + (PP(pp, p_stat) == SRUN)) && (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t) sel->uid)) { *prefp++ = pp; active_procs++; @@ -386,9 +387,9 @@ get_process_info(struct system_info *si, struct process_select *sel, } /* if requested, sort the "interesting" processes */ - if (compare != NULL) { - qsort((char *) pref, active_procs, sizeof(struct kinfo_proc *), compare); - } + if (compare != NULL) + qsort((char *) pref, active_procs, + sizeof(struct kinfo_proc *), compare); /* remember active and total counts */ si->p_total = total_procs; si->p_active = pref_len = active_procs; @@ -399,15 +400,14 @@ get_process_info(struct system_info *si, struct process_select *sel, return ((caddr_t) & handle); } -char fmt[MAX_COLS]; /* static area where result is built */ +char fmt[MAX_COLS]; /* static area where result is built */ char * format_next_process(caddr_t handle, char *(*get_userid)()) { - char waddr[sizeof(void *) * 2 + 3]; /* Hexify void pointer */ + char *p_wait, waddr[sizeof(void *) * 2 + 3]; /* Hexify void pointer */ struct kinfo_proc *pp; struct handle *hp; - char *p_wait; int cputime; double pct; @@ -421,22 +421,20 @@ format_next_process(caddr_t handle, char *(*get_userid)()) /* * Print swapped processes as <pname> */ - char *comm = PP(pp, p_comm); -#define COMSIZ sizeof(PP(pp, p_comm)) - char buf[COMSIZ]; - (void) strncpy(buf, comm, COMSIZ); + char *comm = PP(pp, p_comm); + char buf[sizeof(PP(pp, p_comm))]; + + (void) strlcpy(buf, comm, sizeof comm); comm[0] = '<'; - (void) strncpy(&comm[1], buf, COMSIZ - 2); - comm[COMSIZ - 2] = '\0'; - (void) strncat(comm, ">", COMSIZ - 1); - comm[COMSIZ - 1] = '\0'; + (void) strlcpy(&comm[1], buf, sizeof comm - 1); + (void) strlcat(comm, ">", sizeof comm); } cputime = (PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks)) / stathz; /* calculate the base for cpu percentages */ pct = pctdouble(PP(pp, p_pctcpu)); - if (PP(pp, p_wchan)) + if (PP(pp, p_wchan)) { if (PP(pp, p_wmesg)) p_wait = EP(pp, e_wmesg); else { @@ -444,23 +442,18 @@ format_next_process(caddr_t handle, char *(*get_userid)()) (unsigned long) (PP(pp, p_wchan)) & ~KERNBASE); p_wait = waddr; } - else + } else p_wait = "-"; /* format this entry */ - snprintf(fmt, MAX_COLS, - Proc_format, - PP(pp, p_pid), - (*get_userid) (EP(pp, e_pcred.p_ruid)), - PP(pp, p_priority) - PZERO, - PP(pp, p_nice) - NZERO, + snprintf(fmt, sizeof fmt, Proc_format, + PP(pp, p_pid), (*get_userid) (EP(pp, e_pcred.p_ruid)), + PP(pp, p_priority) - PZERO, PP(pp, p_nice) - NZERO, format_k(pagetok(PROCSIZE(pp))), format_k(pagetok(VP(pp, vm_rssize))), - (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > maxslp) - ? "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)], - p_wait, - format_time(cputime), - 100.0 * pct, + (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > maxslp) ? + "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)], + p_wait, format_time(cputime), 100.0 * pct, printable(PP(pp, p_comm))); /* return the result */ @@ -489,7 +482,6 @@ static unsigned char sorted_state[] = * desired ordering. */ - #define ORDERKEY_PCTCPU \ if (lresult = (pctcpu)PP(p2, p_pctcpu) - (pctcpu)PP(p1, p_pctcpu), \ (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) @@ -507,29 +499,27 @@ static unsigned char sorted_state[] = #define ORDERKEY_MEM \ if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0) - /* compare_cpu - the comparison function for sorting by cpu percentage */ int compare_cpu(const void *v1, const void *v2) { struct proc **pp1 = (struct proc **) v1; struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1; - struct kinfo_proc *p2; - int result; + struct kinfo_proc *p1, *p2; pctcpu lresult; + int result; /* remove one level of indirection */ p1 = *(struct kinfo_proc **) pp1; p2 = *(struct kinfo_proc **) pp2; ORDERKEY_PCTCPU - ORDERKEY_CPUTIME - ORDERKEY_STATE - ORDERKEY_PRIO - ORDERKEY_RSSIZE - ORDERKEY_MEM - ; + ORDERKEY_CPUTIME + ORDERKEY_STATE + ORDERKEY_PRIO + ORDERKEY_RSSIZE + ORDERKEY_MEM + ; return (result); } @@ -539,22 +529,21 @@ compare_size(const void *v1, const void *v2) { struct proc **pp1 = (struct proc **) v1; struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1; - struct kinfo_proc *p2; - int result; + struct kinfo_proc *p1, *p2; pctcpu lresult; + int result; /* remove one level of indirection */ p1 = *(struct kinfo_proc **) pp1; p2 = *(struct kinfo_proc **) pp2; ORDERKEY_MEM - ORDERKEY_RSSIZE - ORDERKEY_PCTCPU - ORDERKEY_CPUTIME - ORDERKEY_STATE - ORDERKEY_PRIO - ; + ORDERKEY_RSSIZE + ORDERKEY_PCTCPU + ORDERKEY_CPUTIME + ORDERKEY_STATE + ORDERKEY_PRIO + ; return (result); } @@ -564,22 +553,21 @@ compare_res(const void *v1, const void *v2) { struct proc **pp1 = (struct proc **) v1; struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1; - struct kinfo_proc *p2; - int result; + struct kinfo_proc *p1, *p2; pctcpu lresult; + int result; /* remove one level of indirection */ p1 = *(struct kinfo_proc **) pp1; p2 = *(struct kinfo_proc **) pp2; ORDERKEY_RSSIZE - ORDERKEY_MEM - ORDERKEY_PCTCPU - ORDERKEY_CPUTIME - ORDERKEY_STATE - ORDERKEY_PRIO - ; + ORDERKEY_MEM + ORDERKEY_PCTCPU + ORDERKEY_CPUTIME + ORDERKEY_STATE + ORDERKEY_PRIO + ; return (result); } @@ -589,22 +577,21 @@ compare_time(const void *v1, const void *v2) { struct proc **pp1 = (struct proc **) v1; struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1; - struct kinfo_proc *p2; - int result; + struct kinfo_proc *p1, *p2; pctcpu lresult; + int result; /* remove one level of indirection */ p1 = *(struct kinfo_proc **) pp1; p2 = *(struct kinfo_proc **) pp2; ORDERKEY_CPUTIME - ORDERKEY_PCTCPU - ORDERKEY_STATE - ORDERKEY_PRIO - ORDERKEY_MEM - ORDERKEY_RSSIZE - ; + ORDERKEY_PCTCPU + ORDERKEY_STATE + ORDERKEY_PRIO + ORDERKEY_MEM + ORDERKEY_RSSIZE + ; return (result); } @@ -612,28 +599,27 @@ compare_time(const void *v1, const void *v2) int compare_prio(const void *v1, const void *v2) { - struct proc **pp1 = (struct proc **) v1; - struct proc **pp2 = (struct proc **) v2; - struct kinfo_proc *p1; - struct kinfo_proc *p2; - int result; + 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; ORDERKEY_PRIO - ORDERKEY_PCTCPU - ORDERKEY_CPUTIME - ORDERKEY_STATE - ORDERKEY_RSSIZE - ORDERKEY_MEM - ; + ORDERKEY_PCTCPU + ORDERKEY_CPUTIME + ORDERKEY_STATE + ORDERKEY_RSSIZE + ORDERKEY_MEM + ; return (result); } -int (*proc_compares[]) () = { +int (*proc_compares[]) () = { compare_cpu, compare_size, compare_res, @@ -641,26 +627,27 @@ int (*proc_compares[]) () = { compare_prio, 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. + * 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; - struct kinfo_proc *p2; - int result; + struct kinfo_proc *p1, *p2; pctcpu lresult; + int result; /* remove one level of indirection */ p1 = *(struct kinfo_proc **) pp1; @@ -672,24 +659,27 @@ proc_compare(const void *v1, const void *v2) 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) { + 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 */ + 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 */ + VP(p1, vm_rssize)) == 0) { + /* + * use total memory to break + * the tie + */ result = PROCSIZE(p2) - PROCSIZE(p1); } } } } - } else { + } else result = lresult < 0 ? -1 : 1; - } return (result); } #endif @@ -703,7 +693,7 @@ proc_compare(const void *v1, const void *v2) * security problem. It validates requests for the "kill" * and "renice" commands. */ -int +int proc_owner(pid_t pid) { struct kinfo_proc **prefp, *pp; @@ -713,12 +703,12 @@ proc_owner(pid_t pid) cnt = pref_len; while (--cnt >= 0) { pp = *prefp++; - if (PP(pp, p_pid) == pid) { + if (PP(pp, p_pid) == pid) return ((int) EP(pp, e_pcred.p_ruid)); - } } return (-1); } + #ifdef DOSWAP /* * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org> @@ -727,8 +717,8 @@ proc_owner(pid_t pid) static int swapmode(int *used, int *total) { - int nswap, rnswap, i; struct swapent *swdev; + int nswap, rnswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); if (nswap == 0) @@ -752,7 +742,6 @@ swapmode(int *used, int *total) *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); } } - free(swdev); return 1; } diff --git a/usr.bin/top/screen.c b/usr.bin/top/screen.c index c1f33f09d4f..027896e98d9 100644 --- a/usr.bin/top/screen.c +++ b/usr.bin/top/screen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: screen.c,v 1.9 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: screen.c,v 1.10 2003/06/12 23:09:30 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -28,13 +28,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* This file contains the routines that interface to termcap and stty/gtty. - * - * Paul Vixie, February 1987: converted to use ioctl() instead of stty/gtty. - * - * I put in code to turn on the TOSTOP bit while top was running, but I - * didn't really like the results. If you desire it, turn on the - * preprocessor variable "TOStop". --wnl +/* + * This file contains the routines that interface to termcap and stty/gtty. + * + * Paul Vixie, February 1987: converted to use ioctl() instead of stty/gtty. + * + * I put in code to turn on the TOSTOP bit while top was running, but I didn't + * really like the results. If you desire it, turn on the preprocessor + * variable "TOStop". --wnl */ #include <sys/types.h> @@ -50,313 +51,261 @@ #include "screen.h" #include "boolean.h" -extern char *myname; - -int overstrike; -int screen_length; -int screen_width; -char ch_erase; -char ch_kill; -char smart_terminal; -char PC; -char string_buffer[1024]; -char home[15]; -char lower_left[15]; -char *clear_line; -char *clear_scr; -char *clear_to_end; -char *cursor_motion; -char *start_standout; -char *end_standout; -char *terminal_init; -char *terminal_end; -short ospeed; +extern char *myname; + +int overstrike; +int screen_length; +int screen_width; +char ch_erase; +char ch_kill; +char smart_terminal; +char PC; +char string_buffer[1024]; +char home[15]; +char lower_left[15]; +char *clear_line; +char *clear_scr; +char *clear_to_end; +char *cursor_motion; +char *start_standout; +char *end_standout; +char *terminal_init; +char *terminal_end; +short ospeed; static struct termios old_settings; static struct termios new_settings; -static char is_a_terminal = No; +static char is_a_terminal = No; void init_termcap(int interactive) { - char *bufptr; - char *PCptr; - char *term_name; - int status; - - /* set defaults in case we aren't smart */ - screen_width = MAX_COLS; - screen_length = 0; - - if (!interactive) - { - /* pretend we have a dumb terminal */ - smart_terminal = No; - return; - } - - /* assume we have a smart terminal until proven otherwise */ - smart_terminal = Yes; - - /* get the terminal name */ - term_name = getenv("TERM"); - - /* if there is no TERM, assume it's a dumb terminal */ - /* patch courtesy of Sam Horrocks at telegraph.ics.uci.edu */ - if (term_name == NULL) - { - smart_terminal = No; - return; - } - - /* 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); + char *bufptr, *PCptr, *term_name; + int status; + + /* set defaults in case we aren't smart */ + screen_width = MAX_COLS; + screen_length = 0; + + if (!interactive) { + /* pretend we have a dumb terminal */ + smart_terminal = No; + return; } - else - { - fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n", - myname, term_name); + /* assume we have a smart terminal until proven otherwise */ + smart_terminal = Yes; + + /* get the terminal name */ + term_name = getenv("TERM"); + + /* if there is no TERM, assume it's a dumb terminal */ + /* patch courtesy of Sam Horrocks at telegraph.ics.uci.edu */ + if (term_name == NULL) { + smart_terminal = No; + return; } - /* pretend it's dumb and proceed */ - smart_terminal = No; - return; - } - - /* "hardcopy" immediately indicates a very stupid terminal */ - if (tgetflag("hc")) - { - smart_terminal = No; - return; - } - - /* set up common terminal capabilities */ - if ((screen_length = tgetnum("li")) <= Header_lines) - { - screen_length = smart_terminal = 0; - return; - } - - /* screen_width is a little different */ - if ((screen_width = tgetnum("co")) == -1) - { - screen_width = 79; - } - else - { - screen_width -= 1; - } - - /* terminals that overstrike need special attention */ - overstrike = tgetflag("os"); - - /* initialize the pointer into the termcap string buffer */ - bufptr = string_buffer; - - /* get "ce", clear to end */ - if (!overstrike) - { - clear_line = tgetstr("ce", &bufptr); - } - - /* get necessary capabilities */ - if ((clear_scr = tgetstr("cl", &bufptr)) == NULL || - (cursor_motion = tgetstr("cm", &bufptr)) == NULL) - { - smart_terminal = No; - return; - } - - /* get some more sophisticated stuff -- these are optional */ - clear_to_end = tgetstr("cd", &bufptr); - terminal_init = tgetstr("ti", &bufptr); - terminal_end = tgetstr("te", &bufptr); - start_standout = tgetstr("so", &bufptr); - end_standout = tgetstr("se", &bufptr); - - /* pad character */ - PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0; - - /* set convenience strings */ - (void) strncpy(home, tgoto(cursor_motion, 0, 0), sizeof (home) -1); - home[sizeof (home) -1] = 0; - /* (lower_left is set in get_screensize) */ - - /* get the actual screen size with an ioctl, if needed */ - /* This may change screen_width and screen_length, and it always - sets lower_left. */ - get_screensize(); - - /* if stdout is not a terminal, pretend we are a dumb terminal */ - if (tcgetattr(STDOUT_FILENO, &old_settings) == -1) - { - smart_terminal = No; - } + /* 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); + else + fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n", + myname, term_name); + + /* pretend it's dumb and proceed */ + smart_terminal = No; + return; + } + + /* "hardcopy" immediately indicates a very stupid terminal */ + if (tgetflag("hc")) { + smart_terminal = No; + return; + } + /* set up common terminal capabilities */ + if ((screen_length = tgetnum("li")) <= Header_lines) { + screen_length = smart_terminal = 0; + return; + } + + /* screen_width is a little different */ + if ((screen_width = tgetnum("co")) == -1) + screen_width = 79; + else + screen_width -= 1; + + /* terminals that overstrike need special attention */ + overstrike = tgetflag("os"); + + /* initialize the pointer into the termcap string buffer */ + bufptr = string_buffer; + + /* get "ce", clear to end */ + if (!overstrike) { + clear_line = tgetstr("ce", &bufptr); + } + /* get necessary capabilities */ + if ((clear_scr = tgetstr("cl", &bufptr)) == NULL || + (cursor_motion = tgetstr("cm", &bufptr)) == NULL) { + smart_terminal = No; + return; + } + /* get some more sophisticated stuff -- these are optional */ + clear_to_end = tgetstr("cd", &bufptr); + terminal_init = tgetstr("ti", &bufptr); + terminal_end = tgetstr("te", &bufptr); + start_standout = tgetstr("so", &bufptr); + end_standout = tgetstr("se", &bufptr); + + /* pad character */ + PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0; + + /* set convenience strings */ + (void) strlcpy(home, tgoto(cursor_motion, 0, 0), sizeof(home)); + /* (lower_left is set in get_screensize) */ + + /* get the actual screen size with an ioctl, if needed */ + /* + * This may change screen_width and screen_length, and it always sets + * lower_left. + */ + get_screensize(); + + /* if stdout is not a terminal, pretend we are a dumb terminal */ + if (tcgetattr(STDOUT_FILENO, &old_settings) == -1) + smart_terminal = No; } void init_screen(void) { - /* get the old settings for safe keeping */ - if (tcgetattr(STDOUT_FILENO, &old_settings) != -1) - { - /* copy the settings so we can modify them */ - new_settings = old_settings; - - /* turn off ICANON, character echo and tab expansion */ - new_settings.c_lflag &= ~(ICANON|ECHO); - new_settings.c_oflag &= ~(OXTABS); - new_settings.c_cc[VMIN] = 1; - new_settings.c_cc[VTIME] = 0; - (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings); - - /* remember the erase and kill characters */ - ch_erase = old_settings.c_cc[VERASE]; - ch_kill = old_settings.c_cc[VKILL]; - - /* remember that it really is a terminal */ - is_a_terminal = Yes; - - /* send the termcap initialization string */ - putcap(terminal_init); - } - - if (!is_a_terminal) - { - /* not a terminal at all---consider it dumb */ - smart_terminal = No; - } + /* get the old settings for safe keeping */ + if (tcgetattr(STDOUT_FILENO, &old_settings) != -1) { + /* copy the settings so we can modify them */ + new_settings = old_settings; + + /* turn off ICANON, character echo and tab expansion */ + new_settings.c_lflag &= ~(ICANON | ECHO); + new_settings.c_oflag &= ~(OXTABS); + new_settings.c_cc[VMIN] = 1; + new_settings.c_cc[VTIME] = 0; + (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings); + + /* remember the erase and kill characters */ + ch_erase = old_settings.c_cc[VERASE]; + ch_kill = old_settings.c_cc[VKILL]; + + /* remember that it really is a terminal */ + is_a_terminal = Yes; + + /* send the termcap initialization string */ + putcap(terminal_init); + } + if (!is_a_terminal) { + /* not a terminal at all---consider it dumb */ + smart_terminal = No; + } } void end_screen(void) { - /* move to the lower left, clear the line and send "te" */ - if (smart_terminal) - { - putcap(lower_left); - putcap(clear_line); - fflush(stdout); - putcap(terminal_end); - } - - /* if we have settings to reset, then do so */ - if (is_a_terminal) - { - (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &old_settings); - } + /* move to the lower left, clear the line and send "te" */ + if (smart_terminal) { + putcap(lower_left); + putcap(clear_line); + fflush(stdout); + putcap(terminal_end); + } + + /* if we have settings to reset, then do so */ + if (is_a_terminal) + (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &old_settings); } void reinit_screen(void) { - /* install our settings if it is a terminal */ - if (is_a_terminal) - { - (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings); - } - - /* send init string */ - if (smart_terminal) - { - putcap(terminal_init); - } + /* install our settings if it is a terminal */ + if (is_a_terminal) + (void) tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings); + + /* send init string */ + if (smart_terminal) + putcap(terminal_init); } void get_screensize(void) { - struct winsize ws; + struct winsize ws; - if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) - { - if (ws.ws_row != 0) - { - screen_length = ws.ws_row; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { + if (ws.ws_row != 0) + screen_length = ws.ws_row; + if (ws.ws_col != 0) + screen_width = ws.ws_col - 1; } - if (ws.ws_col != 0) - { - screen_width = ws.ws_col - 1; - } - } - - (void) strncpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1), - sizeof (lower_left) -1); - lower_left[sizeof(lower_left) -1] = 0; + (void) strlcpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1), + sizeof(lower_left)); } void standout(char *msg) { - if (smart_terminal) - { - putcap(start_standout); - if (fputs(msg, stdout) == EOF) - exit(1); - putcap(end_standout); - } - else - { - if (fputs(msg, stdout) == EOF) - exit(1); - } + if (smart_terminal) { + putcap(start_standout); + if (fputs(msg, stdout) == EOF) + exit(1); + putcap(end_standout); + } else { + if (fputs(msg, stdout) == EOF) + exit(1); + } } -void clear() - +void +clear() { - if (smart_terminal) - { - putcap(clear_scr); - } + if (smart_terminal) + putcap(clear_scr); } int clear_eol(int len) { - if (smart_terminal && !overstrike && len > 0) - { - if (clear_line) - { - putcap(clear_line); - return(0); + if (smart_terminal && !overstrike && len > 0) { + if (clear_line) { + putcap(clear_line); + return (0); + } else { + while (len-- > 0) { + if (putchar(' ') == EOF) + exit(1); + } + return (1); + } } - else - { - while (len-- > 0) - { - if (putchar(' ') == EOF) - exit(1); - } - return(1); - } - } - return(-1); + return (-1); } void go_home(void) { - if (smart_terminal) - { - putcap(home); - } + if (smart_terminal) + putcap(home); } /* This has to be defined as a subroutine for tputs (instead of a macro) */ - int putstdout(int ch) { - int ret; + int ret; - ret = putchar(ch); - if (ret == EOF) - exit(1); - return (ret); + ret = putchar(ch); + if (ret == EOF) + exit(1); + return (ret); } diff --git a/usr.bin/top/username.c b/usr.bin/top/username.c index 6cd4bd9793b..a718529d7ff 100644 --- a/usr.bin/top/username.c +++ b/usr.bin/top/username.c @@ -1,4 +1,4 @@ -/* $OpenBSD: username.c,v 1.8 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: username.c,v 1.9 2003/06/12 23:09:30 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -56,95 +56,94 @@ #include "utils.h" struct hash_el { - uid_t uid; - char name[9]; + uid_t uid; + char name[9]; }; -static int enter_user(uid_t, char *, int); -static int get_user(uid_t); +static int enter_user(uid_t, char *, int); +static int get_user(uid_t); #define is_empty_hash(x) (hash_table[x].name[0] == 0) /* simple minded hashing function */ -/* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for - the hash_table. Applied abs() function to fix. 2/16/96 tpugh -*/ +/* + * Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for + * the hash_table. Applied abs() function to fix. 2/16/96 tpugh + */ #define hashit(i) (abs(i) % Table_size) /* K&R requires that statically declared tables be initialized to zero. */ /* We depend on that for hash_table and YOUR compiler had BETTER do it! */ -struct hash_el hash_table[Table_size]; +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. - */ + /* + * 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) { - int hashindex; - - hashindex = hashit(uid); - if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) - { - /* not here or not right -- get it out of passwd */ - hashindex = get_user(uid); - } - return(hash_table[hashindex].name); + int hashindex; + + hashindex = hashit(uid); + if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid)) { + /* not here or not right -- get it out of passwd */ + hashindex = get_user(uid); + } + return (hash_table[hashindex].name); } uid_t userid(char *username) { - struct passwd *pwd; + struct passwd *pwd; - /* Eventually we want this to enter everything in the hash table, - but for now we just do it simply and remember just the result. - */ + /* + * 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); - } + if ((pwd = getpwnam(username)) == NULL) + return (-1); - /* enter the result in the hash table */ - enter_user(pwd->pw_uid, username, 1); + /* enter the result in the hash table */ + enter_user(pwd->pw_uid, username, 1); - /* return our result */ - return(pwd->pw_uid); + /* return our result */ + return (pwd->pw_uid); } -/* wecare: 1 = enter it always, 0 = nice to have */ +/* + * wecare: 1 = enter it always, 0 = nice to have + */ static int enter_user(uid_t uid, char *name, int wecare) { - int hashindex; + int hashindex; #ifdef DEBUG - fprintf(stderr, "enter_hash(%u, %s, %d)\n", uid, name, wecare); + fprintf(stderr, "enter_hash(%u, %s, %d)\n", uid, name, wecare); #endif - hashindex = hashit(uid); - - if (!is_empty_hash(hashindex)) - { - if (!wecare) - return 0; /* Don't clobber a slot for trash */ - if (hash_table[hashindex].uid == uid) - return(hashindex); /* Fortuitous find */ - } - - /* empty or wrong slot -- fill it with new value */ - hash_table[hashindex].uid = uid; - (void) strlcpy(hash_table[hashindex].name, name, - sizeof(hash_table[hashindex].name)); - return(hashindex); + hashindex = hashit(uid); + + if (!is_empty_hash(hashindex)) { + if (!wecare) + return 0; /* Don't clobber a slot for trash */ + if (hash_table[hashindex].uid == uid) + return (hashindex); /* Fortuitous find */ + } + /* empty or wrong slot -- fill it with new value */ + hash_table[hashindex].uid = uid; + (void) strlcpy(hash_table[hashindex].name, name, + sizeof(hash_table[hashindex].name)); + return (hashindex); } /* @@ -153,49 +152,41 @@ enter_user(uid_t uid, char *name, int wecare) * just handle this uid. Otherwise we scan the passwd file * and cache any entries we pass over while looking. */ - static int get_user(uid_t uid) { - struct passwd *pwd; + struct passwd *pwd; #ifdef RANDOM_PW - /* no performance penalty for using getpwuid makes it easy */ - if ((pwd = getpwuid(uid)) != NULL) - { - return(enter_user(pwd->pw_uid, pwd->pw_name, 1)); - } + /* no performance penalty for using getpwuid makes it easy */ + if ((pwd = getpwuid(uid)) != NULL) + return (enter_user(pwd->pw_uid, pwd->pw_name, 1)); #else - int from_start = 0; - - /* - * If we just called getpwuid each time, things would be very slow - * since that just iterates through the passwd file each time. So, - * we walk through the file instead (using getpwent) and cache each - * entry as we go. Once the right record is found, we cache it and - * return immediately. The next time we come in, getpwent will get - * the next record. In theory, we never have to read the passwd file - * a second time (because we cache everything we read). But in - * practice, the cache may not be large enough, so if we don't find - * it the first time we have to scan the file a second time. This - * is not very efficient, but it will do for now. - */ - - while (from_start++ < 2) - { - while ((pwd = getpwent()) != NULL) - { - if (pwd->pw_uid == uid) - { - return(enter_user(pwd->pw_uid, pwd->pw_name, 1)); - } - (void) enter_user(pwd->pw_uid, pwd->pw_name, 0); + int from_start = 0; + + /* + * If we just called getpwuid each time, things would be very slow + * since that just iterates through the passwd file each time. So, + * we walk through the file instead (using getpwent) and cache each + * entry as we go. Once the right record is found, we cache it and + * return immediately. The next time we come in, getpwent will get + * the next record. In theory, we never have to read the passwd file + * a second time (because we cache everything we read). But in + * practice, the cache may not be large enough, so if we don't find + * it the first time we have to scan the file a second time. This + * is not very efficient, but it will do for now. + */ + while (from_start++ < 2) { + while ((pwd = getpwent()) != NULL) { + if (pwd->pw_uid == uid) + return (enter_user(pwd->pw_uid, pwd->pw_name, 1)); + (void) enter_user(pwd->pw_uid, pwd->pw_name, 0); + } + /* try again */ + setpwent(); } - /* try again */ - setpwent(); - } #endif - /* if we can't find the name at all, then use the uid as the name */ - return(enter_user(uid, itoa7(uid), 1)); + /* if we can't find the name at all, then use the uid as the name */ + return (enter_user(uid, itoa7(uid), 1)); } diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c index 86184e80fd6..8d22390cf8d 100644 --- a/usr.bin/top/utils.c +++ b/usr.bin/top/utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.c,v 1.9 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: utils.c,v 1.10 2003/06/12 23:09:30 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -43,332 +43,284 @@ int atoiwi(char *str) { - int len; - - len = strlen(str); - if (len != 0) - { - if (strncmp(str, "infinity", len) == 0 || - strncmp(str, "all", len) == 0 || - strncmp(str, "maximum", len) == 0) - { - return(Infinity); + int len; + + len = strlen(str); + if (len != 0) { + if (strncmp(str, "infinity", len) == 0 || + strncmp(str, "all", len) == 0 || + strncmp(str, "maximum", len) == 0) { + return (Infinity); + } else if (str[0] == '-') + return (Invalid); + else + return (atoi(str)); } - else if (str[0] == '-') - { - return(Invalid); - } - else - { - return(atoi(str)); - } - } - return(0); + return (0); } /* - * itoa - convert integer (decimal) to ascii string for positive numbers - * only (we don't bother with negative numbers since we know we - * don't use them). + * itoa - convert integer (decimal) to ascii string for positive numbers + * only (we don't bother with negative numbers since we know we + * don't use them). */ - - /* - * How do we know that 16 will suffice? - * Because the biggest number that we will - * ever convert will be 2^32-1, which is 10 - * digits. - */ - char * itoa(int val) { - char *ptr; - static char buffer[16]; /* result is built here */ - /* 16 is sufficient since the largest number - we will ever convert will be 2^32-1, - which is 10 digits. */ - - ptr = buffer + sizeof(buffer); - *--ptr = '\0'; - if (val == 0) - { - *--ptr = '0'; - } - else while (val != 0) - { - *--ptr = (val % 10) + '0'; - val /= 10; - } - return(ptr); + static char buffer[16]; /* result is built here */ + char *ptr; + + /* + * 16 is sufficient since the largest number we will ever convert + * will be 2^32-1, which is 10 digits. + */ + ptr = buffer + sizeof(buffer); + *--ptr = '\0'; + if (val == 0) { + *--ptr = '0'; + } else { + while (val != 0) { + *--ptr = (val % 10) + '0'; + val /= 10; + } + } + return (ptr); } /* - * itoa7(val) - like itoa, except the number is right justified in a 7 - * character field. This code is a duplication of itoa instead of - * a front end to a more general routine for efficiency. + * itoa7(val) - like itoa, except the number is right justified in a 7 + * character field. This code is a duplication of itoa instead of + * a front end to a more general routine for efficiency. */ - char * itoa7(int val) { - char *ptr; - static char buffer[25]; /* result is built here */ - - ptr = buffer + sizeof(buffer); - *--ptr = '\0'; - if (val == 0) - { - *--ptr = '0'; - } - else while (val != 0) - { - *--ptr = (val % 10) + '0'; - val /= 10; - } - while (ptr > buffer + sizeof(buffer) - 7) - { - *--ptr = ' '; - } - return(ptr); + static char buffer[25]; /* result is built here */ + char *ptr; + + ptr = buffer + sizeof(buffer); + *--ptr = '\0'; + if (val == 0) { + *--ptr = '0'; + } else { + while (val != 0) { + *--ptr = (val % 10) + '0'; + val /= 10; + } + } + while (ptr > buffer + sizeof(buffer) - 7) + *--ptr = ' '; + return (ptr); } /* - * digits(val) - return number of decimal digits in val. Only works for - * positive numbers. If val <= 0 then digits(val) == 0. + * digits(val) - return number of decimal digits in val. Only works for + * positive numbers. If val <= 0 then digits(val) == 0. */ - int digits(int val) { - int cnt = 0; - - while (val > 0) - { - cnt++; - val /= 10; - } - return(cnt); + int cnt = 0; + + while (val > 0) { + cnt++; + val /= 10; + } + return (cnt); } /* - * strecpy(to, from) - copy string "from" into "to" and return a pointer - * to the END of the string "to". + * strecpy(to, from) - copy string "from" into "to" and return a pointer + * to the END of the string "to". */ - char * strecpy(char *to, char *from) { - while ((*to++ = *from++) != '\0'); - return(--to); + while ((*to++ = *from++) != '\0') + ; + return (--to); } /* * string_index(string, array) - find string in array and return index */ - int string_index(char *string, char **array) { - int i = 0; + int i = 0; - while (*array != NULL) - { - if (strcmp(string, *array) == 0) - { - return(i); + while (*array != NULL) { + if (strcmp(string, *array) == 0) + return (i); + array++; + i++; } - array++; - i++; - } - return(-1); + return (-1); } /* * argparse(line, cntp) - parse arguments in string "line", separating them - * out into an argv-like array, and setting *cntp to the number of - * arguments encountered. This is a simple parser that doesn't understand - * squat about quotes. + * out into an argv-like array, and setting *cntp to the number of + * arguments encountered. This is a simple parser that doesn't understand + * squat about quotes. */ - char ** argparse(char *line, int *cntp) { - char *from; - char *to; - int cnt; - int ch; - int length; - int lastch; - char **argv; - char **argarray; - char *args; - - /* unfortunately, the only real way to do this is to go thru the - input string twice. */ - - /* step thru the string counting the white space sections */ - from = line; - lastch = cnt = length = 0; - while ((ch = *from++) != '\0') - { - length++; - if (ch == ' ' && lastch != ' ') - { - cnt++; + char **argv, **argarray, *args, *from, *to; + int cnt, ch, length, lastch; + + /* + * unfortunately, the only real way to do this is to go thru the + * input string twice. + */ + + /* step thru the string counting the white space sections */ + from = line; + lastch = cnt = length = 0; + while ((ch = *from++) != '\0') { + length++; + if (ch == ' ' && lastch != ' ') + cnt++; + lastch = ch; } - lastch = ch; - } - - /* add three to the count: one for the initial "dummy" argument, - one for the last argument and one for NULL */ - cnt += 3; - - /* allocate a char * array to hold the pointers */ - argarray = (char **)malloc(cnt * sizeof(char *)); - - /* allocate another array to hold the strings themselves */ - args = (char *)malloc(length+2); - - /* initialization for main loop */ - from = line; - to = args; - argv = argarray; - lastch = '\0'; - - /* create a dummy argument to keep getopt happy */ - *argv++ = to; - *to++ = '\0'; - cnt = 2; - - /* now build argv while copying characters */ - *argv++ = to; - while ((ch = *from++) != '\0') - { - if (ch != ' ') - { - if (lastch == ' ') - { - *to++ = '\0'; - *argv++ = to; - cnt++; - } - *to++ = ch; + + /* + * add three to the count: one for the initial "dummy" argument, one + * for the last argument and one for NULL + */ + cnt += 3; + + /* allocate a char * array to hold the pointers */ + argarray = (char **) malloc(cnt * sizeof(char *)); + + /* allocate another array to hold the strings themselves */ + args = (char *) malloc(length + 2); + + /* initialization for main loop */ + from = line; + to = args; + argv = argarray; + lastch = '\0'; + + /* create a dummy argument to keep getopt happy */ + *argv++ = to; + *to++ = '\0'; + cnt = 2; + + /* now build argv while copying characters */ + *argv++ = to; + while ((ch = *from++) != '\0') { + if (ch != ' ') { + if (lastch == ' ') { + *to++ = '\0'; + *argv++ = to; + cnt++; + } + *to++ = ch; + } + lastch = ch; } - lastch = ch; - } - *to++ = '\0'; + *to++ = '\0'; - /* set cntp and return the allocated array */ - *cntp = cnt; - return(argarray); + /* set cntp and return the allocated array */ + *cntp = cnt; + return (argarray); } /* - * percentages(cnt, out, new, old, diffs) - calculate percentage change - * between array "old" and "new", putting the percentages i "out". - * "cnt" is size of each array and "diffs" is used for scratch space. - * The array "old" is updated on each call. - * The routine assumes modulo arithmetic. This function is especially - * useful on BSD mchines for calculating cpu state percentages. + * percentages(cnt, out, new, old, diffs) - calculate percentage change + * between array "old" and "new", putting the percentages i "out". + * "cnt" is size of each array and "diffs" is used for scratch space. + * The array "old" is updated on each call. + * The routine assumes modulo arithmetic. This function is especially + * useful on BSD mchines for calculating cpu state percentages. */ - int percentages(int cnt, int *out, long *new, long *old, long *diffs) { - int i; - long change; - long total_change; - long *dp; - long half_total; - - /* initialization */ - total_change = 0; - dp = diffs; - - /* calculate changes for each state and the overall change */ - for (i = 0; i < cnt; i++) - { - if ((change = *new - *old) < 0) - { - /* this only happens when the counter wraps */ - change = ((unsigned int)*new-(unsigned int)*old); + long change, total_change, *dp, half_total; + int i; + + /* initialization */ + total_change = 0; + dp = diffs; + + /* calculate changes for each state and the overall change */ + for (i = 0; i < cnt; i++) { + if ((change = *new - *old) < 0) { + /* this only happens when the counter wraps */ + change = ((unsigned int)*new - (unsigned int)*old); + } + total_change += (*dp++ = change); + *old++ = *new++; } - total_change += (*dp++ = change); - *old++ = *new++; - } - - /* avoid divide by zero potential */ - if (total_change == 0) - { - total_change = 1; - } - - /* calculate percentages based on overall change, rounding up */ - half_total = total_change / 2l; - for (i = 0; i < cnt; i++) - { - *out++ = ((*diffs++ * 1000 + half_total) / total_change); - } - - /* return the total in case the caller wants to use it */ - return(total_change); + + /* avoid divide by zero potential */ + if (total_change == 0) + total_change = 1; + + /* calculate percentages based on overall change, rounding up */ + half_total = total_change / 2l; + for (i = 0; i < cnt; i++) + *out++ = ((*diffs++ * 1000 + half_total) / total_change); + + /* return the total in case the caller wants to use it */ + return (total_change); } -/* format_time(seconds) - format number of seconds into a suitable - * display that will fit within 6 characters. Note that this - * routine builds its string in a static area. If it needs - * to be called more than once without overwriting previous data, - * then we will need to adopt a technique similar to the - * one used for format_k. +/* + * format_time(seconds) - format number of seconds into a suitable display + * that will fit within 6 characters. Note that this routine builds its + * string in a static area. If it needs to be called more than once without + * overwriting previous data, then we will need to adopt a technique similar + * to the one used for format_k. */ -/* Explanation: - We want to keep the output within 6 characters. For low values we use - the format mm:ss. For values that exceed 999:59, we switch to a format - that displays hours and fractions: hhh.tH. For values that exceed - 999.9, we use hhhh.t and drop the "H" designator. For values that - exceed 9999.9, we use "???". +/* + * Explanation: We want to keep the output within 6 characters. For low + * values we use the format mm:ss. For values that exceed 999:59, we switch + * to a format that displays hours and fractions: hhh.tH. For values that + * exceed 999.9, we use hhhh.t and drop the "H" designator. For values that + * exceed 9999.9, we use "???". */ char * format_time(time_t seconds) { - static char result[10]; - - /* sanity protection */ - if (seconds < 0 || seconds > (99999l * 360l)) - { - strlcpy(result, " ???", sizeof result); - } - else if (seconds >= (1000l * 60l)) - { - /* alternate (slow) method displaying hours and tenths */ - snprintf(result, sizeof(result), "%5.1fH", - (double)seconds / (double)(60l * 60l)); - - /* It is possible that the snprintf took more than 6 characters. - If so, then the "H" appears as result[6]. If not, then there - is a \0 in result[6]. Either way, it is safe to step on. - */ - result[6] = '\0'; - } - else - { - /* standard method produces MMM:SS */ - /* we avoid printf as must as possible to make this quick */ - snprintf(result, sizeof(result), "%3d:%02d", seconds / 60, - seconds % 60); - } - return(result); + static char result[10]; + + /* sanity protection */ + if (seconds < 0 || seconds > (99999l * 360l)) { + strlcpy(result, " ???", sizeof result); + } else if (seconds >= (1000l * 60l)) { + /* alternate (slow) method displaying hours and tenths */ + snprintf(result, sizeof(result), "%5.1fH", + (double) seconds / (double) (60l * 60l)); + + /* + * It is possible that the snprintf took more than 6 + * characters. If so, then the "H" appears as result[6]. If + * not, then there is a \0 in result[6]. Either way, it is + * safe to step on. + */ + result[6] = '\0'; + } else { + /* standard method produces MMM:SS */ + /* we avoid printf as must as possible to make this quick */ + snprintf(result, sizeof(result), "%3d:%02d", seconds / 60, + seconds % 60); + } + return (result); } /* * format_k(amt) - format a kilobyte memory value, returning a string - * suitable for display. Returns a pointer to a static - * area that changes each call. "amt" is converted to a - * string with a trailing "K". If "amt" is 10000 or greater, - * then it is formatted as megabytes (rounded) with a - * trailing "M". + * suitable for display. Returns a pointer to a static + * area that changes each call. "amt" is converted to a + * string with a trailing "K". If "amt" is 10000 or greater, + * then it is formatted as megabytes (rounded) with a + * trailing "M". */ /* @@ -390,29 +342,23 @@ format_time(time_t seconds) char * format_k(int amt) { - static char retarray[NUM_STRINGS][16]; - static int index = 0; - char *p; - char *ret; - char tag = 'K'; - - p = ret = retarray[index]; - index = (index + 1) % NUM_STRINGS; - - if (amt >= 10000) - { - amt = (amt + 512) / 1024; - tag = 'M'; - if (amt >= 10000) - { - amt = (amt + 512) / 1024; - tag = 'G'; + static char retarray[NUM_STRINGS][16]; + static int index = 0; + char *p, *ret, tag = 'K'; + + p = ret = retarray[index]; + index = (index + 1) % NUM_STRINGS; + + if (amt >= 10000) { + amt = (amt + 512) / 1024; + tag = 'M'; + if (amt >= 10000) { + amt = (amt + 512) / 1024; + tag = 'G'; + } } - } - - p = strecpy(p, itoa(amt)); - *p++ = tag; - *p = '\0'; - - return(ret); + p = strecpy(p, itoa(amt)); + *p++ = tag; + *p = '\0'; + return (ret); } diff --git a/usr.bin/top/version.c b/usr.bin/top/version.c index d3b8086a5d3..0993d14a2fe 100644 --- a/usr.bin/top/version.c +++ b/usr.bin/top/version.c @@ -1,4 +1,4 @@ -/* $OpenBSD: version.c,v 1.5 2003/06/12 22:30:23 pvalchev Exp $ */ +/* $OpenBSD: version.c,v 1.6 2003/06/12 23:09:30 deraadt Exp $ */ /* * Top users/processes display for Unix @@ -37,11 +37,8 @@ char * version_string(void) { - static char version[16]; + static char version[16]; - snprintf(version, sizeof(version), "%d.%d", VERSION, PATCHLEVEL); -#ifdef BETA - strlcat(version, BETA, sizeof version); -#endif - return(version); + snprintf(version, sizeof(version), "%d.%d", VERSION, PATCHLEVEL); + return(version); } |