diff options
Diffstat (limited to 'contrib/top')
-rw-r--r-- | contrib/top/commands.c | 14 | ||||
-rw-r--r-- | contrib/top/display.c | 93 | ||||
-rw-r--r-- | contrib/top/layout.h | 12 | ||||
-rw-r--r-- | contrib/top/loadavg.h | 2 | ||||
-rw-r--r-- | contrib/top/machine.h | 12 | ||||
-rw-r--r-- | contrib/top/screen.c | 10 | ||||
-rw-r--r-- | contrib/top/top.X | 38 | ||||
-rw-r--r-- | contrib/top/top.c | 200 | ||||
-rw-r--r-- | contrib/top/top.h | 2 | ||||
-rw-r--r-- | contrib/top/username.c | 5 | ||||
-rw-r--r-- | contrib/top/utils.c | 75 | ||||
-rw-r--r-- | contrib/top/utils.h | 1 |
12 files changed, 293 insertions, 171 deletions
diff --git a/contrib/top/commands.c b/contrib/top/commands.c index a852a47..b18db72 100644 --- a/contrib/top/commands.c +++ b/contrib/top/commands.c @@ -74,7 +74,7 @@ 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); +o - specify sort order (pri, size, res, cpu, time)\n", stdout); #endif fputs("\ r - renice a process\n\ @@ -154,7 +154,7 @@ int *intp; struct errs /* structure for a system-call error */ { - int errnum; /* value of errno (that is, the actual error) */ + int error; /* value of errno (that is, the actual error) */ char *arg; /* argument that caused the error */ }; @@ -173,7 +173,7 @@ static char *err_listem = else \ { \ errs[errcnt].arg = (p); \ - errs[errcnt++].errnum = (e); \ + errs[errcnt++].error = (e); \ } /* @@ -213,7 +213,7 @@ char *err_string() while (cnt < errcnt) { errp = &(errs[cnt++]); - if (errp->errnum != currerr) + if (errp->error != currerr) { if (currerr != -1) { @@ -223,7 +223,7 @@ char *err_string() } (void) strcat(string, "; "); /* we know there's more */ } - currerr = errp->errnum; + currerr = errp->error; first = Yes; } if ((stringlen = str_addarg(string, stringlen, errp->arg, first)) ==0) @@ -311,7 +311,7 @@ register struct errs *p1, *p2; { register int result; - if ((result = p1->errnum - p2->errnum) == 0) + if ((result = p1->error - p2->error) == 0) { return(strcmp(p1->arg, p2->arg)); } @@ -342,7 +342,7 @@ show_errors() while (cnt++ < errcnt) { printf("%5s: %s\n", errp->arg, - errp->errnum == 0 ? "Not a number" : errmsg(errp->errnum)); + errp->error == 0 ? "Not a number" : errmsg(errp->error)); errp++; } } diff --git a/contrib/top/display.c b/contrib/top/display.c index 13cecc0..2a39072 100644 --- a/contrib/top/display.c +++ b/contrib/top/display.c @@ -7,6 +7,8 @@ * * Copyright (c) 1984, 1989, William LeFebvre, Rice University * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University + * + * $FreeBSD$ */ /* @@ -29,6 +31,7 @@ #include "os.h" #include <ctype.h> #include <time.h> +#include <sys/time.h> #include "screen.h" /* interface to screen package */ #include "layout.h" /* defines for screen position layout */ @@ -63,14 +66,17 @@ char *screenbuf = NULL; static char **procstate_names; static char **cpustate_names; static char **memory_names; +static char **swap_names; static int num_procstates; static int num_cpustates; static int num_memory; +static int num_swap; static int *lprocstates; static int *lcpustates; static int *lmemory; +static int *lswap; static int *cpustate_columns; static int cpustate_total_length; @@ -140,6 +146,10 @@ struct statics *statics; lprocstates = (int *)malloc(num_procstates * sizeof(int)); cpustate_names = statics->cpustate_names; + + swap_names = statics->swap_names; + num_swap = string_count(swap_names); + lswap = (int *)malloc(num_swap * sizeof(int)); num_cpustates = string_count(cpustate_names); lcpustates = (int *)malloc(num_cpustates * sizeof(int)); cpustate_columns = (int *)malloc(num_cpustates * sizeof(int)); @@ -154,9 +164,9 @@ struct statics *statics; ip = cpustate_columns; while (*pp != NULL) { - *ip++ = cpustate_total_length; if ((i = strlen(*pp++)) > 0) { + *ip++ = cpustate_total_length; cpustate_total_length += i + 8; } } @@ -269,7 +279,7 @@ time_t *tod; } static int ltotal = 0; -static char procstates_buffer[MAX_COLS]; +static char procstates_buffer[128]; /* * *_procstates(total, brkdn, names) - print the process summary line @@ -311,7 +321,7 @@ int total; int *brkdn; { - static char new[MAX_COLS]; + static char new[128]; register int i; /* update number of processes only if it has changed */ @@ -457,7 +467,7 @@ register int *states; ((double)value)/10.); /* remember it for next time */ - *lp = value; + *lp = *states; } } @@ -511,7 +521,7 @@ i_memory(stats) int *stats; { - fputs("\nMemory: ", stdout); + fputs("\nMem: ", stdout); lastline++; /* format and print the memory summary */ @@ -532,6 +542,40 @@ int *stats; } /* + * *_swap(stats) - print "Swap: " followed by the swap summary string + * + * Assumptions: cursor is on "lastline" + * for i_swap ONLY: cursor is on the previous line + */ + +char swap_buffer[MAX_COLS]; + +i_swap(stats) + +int *stats; + +{ + fputs("\nSwap: ", stdout); + lastline++; + + /* format and print the swap summary */ + summary_format(swap_buffer, stats, swap_names); + fputs(swap_buffer, stdout); +} + +u_swap(stats) + +int *stats; + +{ + static char new[MAX_COLS]; + + /* format the new line */ + summary_format(new, stats, swap_names); + line_update(swap_buffer, new, x_swap, y_swap); +} + +/* * *_message() - print the next pending message line, or erase the one * that is there. * @@ -787,7 +831,7 @@ caddr_t a1, a2, a3; register int i; /* first, format the message */ - (void) sprintf(next_msg, msgfmt, a1, a2, a3); + (void) snprintf(next_msg, sizeof(next_msg), msgfmt, a1, a2, a3); if (msglen > 0) { @@ -845,7 +889,7 @@ int numeric; while ((fflush(stdout), read(0, ptr, 1) > 0)) { /* newline means we are done */ - if ((ch = *ptr) == '\n') + if ((ch = *ptr) == '\n' || ch == '\r') { break; } @@ -1127,3 +1171,38 @@ char *str; } return(str); } + +i_uptime(bt, tod) + +struct timeval* bt; +time_t *tod; + +{ + time_t uptime; + int days, hrs, mins, secs; + + if (bt->tv_sec != -1) { + uptime = *tod - bt->tv_sec; + uptime += 30; + days = uptime / 86400; + uptime %= 86400; + hrs = uptime / 3600; + uptime %= 3600; + mins = uptime / 60; + secs = uptime % 60; + + /* + * Display the uptime. + */ + + if (smart_terminal) + { + Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0); + } + else + { + fputs(" ", stdout); + } + printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs); + } +} diff --git a/contrib/top/layout.h b/contrib/top/layout.h index 77ce102..5db383f 100644 --- a/contrib/top/layout.h +++ b/contrib/top/layout.h @@ -15,13 +15,15 @@ #define y_procstate 1 #define x_brkdn 15 #define y_brkdn 1 -#define x_mem 8 +#define x_mem 5 #define y_mem 3 -#define y_message 4 +#define x_swap 6 +#define y_swap 4 +#define y_message 5 #define x_header 0 -#define y_header 5 +#define y_header 6 #define x_idlecursor 0 -#define y_idlecursor 4 -#define y_procs 6 +#define y_idlecursor 5 +#define y_procs 7 #define y_cpustates 2 diff --git a/contrib/top/loadavg.h b/contrib/top/loadavg.h index f49541e..e3c156c 100644 --- a/contrib/top/loadavg.h +++ b/contrib/top/loadavg.h @@ -47,7 +47,7 @@ typedef double pctcpu; #endif #ifdef FIXED_LOADAVG - typedef long load_avg; + typedef fixpt_t load_avg; # define loaddouble(la) ((double)(la) / FIXED_LOADAVG) # define intload(i) ((int)((i) * FIXED_LOADAVG)) #else diff --git a/contrib/top/machine.h b/contrib/top/machine.h index 0a356a4..0f14bf3 100644 --- a/contrib/top/machine.h +++ b/contrib/top/machine.h @@ -12,6 +12,7 @@ struct statics char **procstate_names; char **cpustate_names; char **memory_names; + char **swap_names; #ifdef ORDER char **order_names; #endif @@ -21,21 +22,17 @@ struct statics * the system_info struct is filled in by a machine dependent routine. */ -#ifdef p_active /* uw7 define macro p_active */ -#define P_ACTIVE p_pactive -#else -#define P_ACTIVE p_active -#endif - struct system_info { int last_pid; double load_avg[NUM_AVERAGES]; int p_total; - int P_ACTIVE; /* number of procs considered "active" */ + int p_active; /* number of procs considered "active" */ int *procstates; int *cpustates; int *memory; + int *swap; + struct timeval boottime; }; /* cpu_states is an array of percentages * 10. For example, @@ -50,6 +47,7 @@ struct system_info struct process_select { int idle; /* show idle processes */ + int self; /* show self */ int system; /* show system processes */ int uid; /* only this uid (unless uid == -1) */ char *command; /* only this command (unless == NULL) */ diff --git a/contrib/top/screen.c b/contrib/top/screen.c index b5d0388..348304c 100644 --- a/contrib/top/screen.c +++ b/contrib/top/screen.c @@ -7,6 +7,8 @@ * * Copyright (c) 1984, 1989, William LeFebvre, Rice University * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University + * + * $FreeBSD$ */ /* This file contains the routines that interface to termcap and stty/gtty. @@ -71,7 +73,6 @@ char *start_standout; char *end_standout; char *terminal_init; char *terminal_end; -short ospeed; #ifdef SGTTY static struct sgttyb old_settings; @@ -204,7 +205,8 @@ int interactive; PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0; /* set convenience strings */ - (void) strcpy(home, tgoto(cursor_motion, 0, 0)); + (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 */ @@ -420,7 +422,9 @@ get_screensize() #endif /* TIOCGSIZE */ #endif /* TIOCGWINSZ */ - (void) strcpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1)); + (void) strncpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1), + sizeof(lower_left) - 1); + lower_left[sizeof(lower_left) - 1] = '\0'; } standout(msg) diff --git a/contrib/top/top.X b/contrib/top/top.X index b8ea13a..97649c3 100644 --- a/contrib/top/top.X +++ b/contrib/top/top.X @@ -1,5 +1,6 @@ .\" NOTE: changes to the manual page for "top" should be made in the .\" file "top.X" and NOT in the file "top.1". +.\" $FreeBSD$ .nr N %topn% .nr D %delay% .TH TOP 1 Local @@ -9,7 +10,7 @@ top \- display and update information about the top cpu processes .SH SYNOPSIS .B top [ -.B \-SbiInquv +.B \-SbiInqut ] [ .BI \-d count ] [ @@ -29,7 +30,7 @@ top \- display and update information about the top cpu processes .if t .ds rq '' .\" Just in case these number registers aren't set yet... .if \nN==0 .nr N 10 -.if \nD==0 .nr D 5 +.if \nD==0 .nr D 2 .I Top displays the top .if !\nN==-1 \nN @@ -81,8 +82,13 @@ intelligent terminal. Do not display idle processes. By default, top displays both active and idle processes. .TP +.B \-t +Do not display the +.I top +process. +.TP .B \-n -Use \*(lqnon-interactive\*(rq mode. This is indentical to \*(lqbatch\*(rq +Use \*(lqnon-interactive\*(rq mode. This is identical to \*(lqbatch\*(rq mode. .TP .B \-q @@ -100,11 +106,6 @@ all the user id numbers it encounters into login names. This option disables all that, while possibly decreasing execution time. The uid numbers are displayed instead of the names. .TP -.B \-v -Write version number information to stderr then exit immediately. -No other processing takes place when this option is used. To see current -revision information while top is running, use the help command \*(lq?\*(rq. -.TP .BI \-d count Show only .I count @@ -158,8 +159,9 @@ can also be specified in the environment variable The options .BR \-I , .BR \-S , +.BR \-u , and -.B \-u +.B \-t are actually toggles. A second specification of any of these options will negate the first. Thus a user who has the environment variable .B TOP @@ -192,8 +194,7 @@ These commands are currently recognized (^L refers to control-L): .B ^L Redraw the screen. .IP "\fBh\fP\ or\ \fB?\fP" -Display a summary of the commands (help screen). Version information -is included in this display. +Display a summary of the commands (help screen). .TP .B q Quit @@ -229,12 +230,6 @@ Display only processes owned by a specific username (prompt for username). If the username specified is simply \*(lq+\*(rq, then processes belonging to all users will be displayed. .TP -.B o -Change the order in which the display is sorted. This command is not -available on all systems. The sort key names vary fron system to system -but usually include: \*(lqcpu\*(rq, \*(lqres\*(rq, \*(lqsize\*(rq, -\*(lqtime\*(rq. The default is cpu. -.TP .B e Display a list of system errors (if any) generated by the last .BR k ill @@ -246,6 +241,11 @@ command. (or .BR I) Toggle the display of idle processes. +.TP +.B t +Toggle the display of the +.I top +process. .SH "THE DISPLAY" The actual display varies depending on the specific variant of Unix that the machine is running. This description may not exactly match @@ -262,7 +262,7 @@ the number of processes in each state (sleeping, running, starting, zombies, and stopped), and a percentage of time spent in each of the processor states (user, nice, system, and idle). -It also includes information about physial and virtual memory allocation. +It also includes information about physical and virtual memory allocation. .PP The remainder of the screen displays information about individual processes. This display is similar in spirit to @@ -303,7 +303,7 @@ TOP user-configurable defaults for options. .br /etc/passwd used to map uid numbers to user names .br -/vmunix system image +/kernel system image .SH BUGS Don't shoot me, but the default for .B \-I diff --git a/contrib/top/top.c b/contrib/top/top.c index aa119ea..9b9b2b2 100644 --- a/contrib/top/top.c +++ b/contrib/top/top.c @@ -9,9 +9,9 @@ char *copyright = * but this entire comment MUST remain intact. * * Copyright (c) 1984, 1989, William LeFebvre, Rice University - * Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University - * Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory - * Copyright (c) 1996, William LeFebvre, Group sys Consulting + * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University + * + * $FreeBSD$ */ /* @@ -32,6 +32,7 @@ char *copyright = */ #include "os.h" +#include <errno.h> #include <signal.h> #include <setjmp.h> #include <ctype.h> @@ -55,6 +56,9 @@ char stdoutbuf[Buffersize]; /* build Signal masks */ #define Smask(s) (1 << ((s) - 1)) +/* for system errors */ +extern int errno; + /* for getopt: */ extern int optind; extern char *optarg; @@ -70,6 +74,10 @@ sigret_t tstop(); sigret_t winch(); #endif +volatile sig_atomic_t leaveflag; +volatile sig_atomic_t tstopflag; +volatile sig_atomic_t winchflag; + /* internal routines */ void quit(); @@ -110,6 +118,8 @@ int i_cpustates(); int u_cpustates(); int i_memory(); int u_memory(); +int i_swap(); +int u_swap(); int i_message(); int u_message(); int i_header(); @@ -122,6 +132,7 @@ int (*d_loadave)() = i_loadave; int (*d_procstates)() = i_procstates; int (*d_cpustates)() = i_cpustates; int (*d_memory)() = i_memory; +int (*d_swap)() = i_swap; int (*d_message)() = i_message; int (*d_header)() = i_header; int (*d_process)() = i_process; @@ -147,6 +158,7 @@ char *argv[]; int topn = Default_TOPN; int delay = Default_DELAY; int displays = 0; /* indicates unspecified */ + int sel_ret = 0; time_t curr_time; char *(*get_userid)() = username; char *uname_field = "USERNAME"; @@ -176,14 +188,14 @@ char *argv[]; /* 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) = 1<<f) +#define FD_SET(f, x) (*(x) = f) #endif fd_set readfds; #ifdef ORDER - static char command_chars[] = "\f qh?en#sdkriIuo"; + static char command_chars[] = "\f qh?en#sdkriIuto"; #else - static char command_chars[] = "\f qh?en#sdkriIu"; + static char command_chars[] = "\f qh?en#sdkriIut"; #endif /* these defines enumerate the "strchr"s of the commands in command_chars */ #define CMD_redraw 0 @@ -202,14 +214,13 @@ char *argv[]; #define CMD_idletog 12 #define CMD_idletog2 13 #define CMD_user 14 +#define CMD_selftog 15 #ifdef ORDER -#define CMD_order 15 +#define CMD_order 16 #endif /* set the buffer for stdout */ #ifdef DEBUG - extern FILE *debug; - debug = fopen("debug.run", "w"); setbuffer(stdout, NULL, 0); #else setbuffer(stdout, stdoutbuf, Buffersize); @@ -230,6 +241,7 @@ char *argv[]; /* initialize some selection options */ ps.idle = Yes; + ps.self = -1; ps.system = No; ps.uid = -1; ps.command = NULL; @@ -257,16 +269,10 @@ char *argv[]; optind = 1; } - while ((i = getopt(ac, av, "SIbinquvs:d:U:o:")) != EOF) + while ((i = getopt(ac, av, "SIbinqus:d:U:o:t")) != EOF) { switch(i) { - case 'v': /* show version number */ - fprintf(stderr, "%s: version %s\n", - myname, version_string()); - exit(1); - break; - case 'u': /* toggle uid/username display */ do_unames = !do_unames; break; @@ -311,10 +317,10 @@ char *argv[]; break; case 's': - if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0)) + if ((delay = atoi(optarg)) < 0) { fprintf(stderr, - "%s: warning: seconds delay should be positive -- using default\n", + "%s: warning: seconds delay should be non-negative -- using default\n", myname); delay = Default_DELAY; warnings++; @@ -348,10 +354,14 @@ char *argv[]; #endif break; + case 't': + ps.self = (ps.self == -1) ? getpid() : -1; + break; + default: fprintf(stderr, "\ Top version %s\n\ -Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", +Usage: %s [-ISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n", version_string(), myname); exit(1); } @@ -513,12 +523,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", fputc('\n', stderr); } - /* setup the jump buffer for stops */ - if (setjmp(jmp_int) != 0) - { - /* control ends up here after an interrupt */ - reset_display(); - } +restart: /* * main loop -- repeat while display count is positive or while it @@ -547,6 +552,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", /* display the current time */ /* this method of getting the time SHOULD be fairly portable */ time(&curr_time); + i_uptime(&system_info.boottime, &curr_time); i_timeofday(&curr_time); /* display process state breakdown */ @@ -575,6 +581,9 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", /* display memory stats */ (*d_memory)(system_info.memory); + /* display swap stats */ + (*d_swap)(system_info.swap); + /* handle message area */ (*d_message)(); @@ -586,7 +595,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", /* determine number of processes to actually display */ /* this number will be the smallest of: active processes, number user requested, number current screen accomodates */ - active_procs = system_info.P_ACTIVE; + active_procs = system_info.p_active; if (active_procs > topn) { active_procs = topn; @@ -611,13 +620,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", u_endscreen(i); /* now, flush the output buffer */ - if (fflush(stdout) != 0) - { - new_message(MT_standout, " Write error on stdout"); - putchar('\r'); - quit(1); - /*NOTREACHED*/ - } + fflush(stdout); /* only do the rest if we have more displays to show */ if (displays) @@ -635,6 +638,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", d_procstates = u_procstates; d_cpustates = u_cpustates; d_memory = u_memory; + d_swap = u_swap; d_message = u_message; d_header = u_header; d_process = u_process; @@ -658,12 +662,61 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", /* set up arguments for select with timeout */ FD_ZERO(&readfds); - FD_SET(0, &readfds); /* for standard input */ + FD_SET(1, &readfds); /* for standard input */ timeout.tv_sec = delay; timeout.tv_usec = 0; + 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 */ +#ifdef SIGRELSE + sigrelse(SIGTSTP); +#else + (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1))); +#endif + (void) kill(0, SIGTSTP); + + /* reset the signal handler */ + (void) signal(SIGTSTP, tstop); + + /* reinit screen */ + reinit_screen(); + reset_display(); + tstopflag = 0; + goto restart; + } + + 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; + goto restart; + } + /* wait for either input or the end of the delay period */ - if (select(32, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout) > 0) + sel_ret = select(2, &readfds, NULL, NULL, &timeout); + if (sel_ret < 0 && errno != EINTR) + quit(0); + if (sel_ret > 0) { int newval; char *errmsg; @@ -674,17 +727,14 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", /* now read it and convert to command strchr */ /* (use "change" as a temporary to hold strchr) */ if (read(0, &ch, 1) != 1) - { - /* read error: either 0 or -1 */ - new_message(MT_standout, " Read error on stdin"); - putchar('\r'); - quit(1); - /*NOTREACHED*/ - } + quit(0); if ((iptr = strchr(command_chars, ch)) == NULL) { - /* illegal command */ - new_message(MT_standout, " Command not understood"); + if (ch != '\r' && ch != '\n') + { + /* illegal command */ + new_message(MT_standout, " Command not understood"); + } putchar('\r'); no_command = Yes; } @@ -783,10 +833,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", new_message(MT_standout, "Seconds to delay: "); if ((i = readline(tempbuf1, 8, Yes)) > -1) { - if ((delay = i) == 0 && getuid() != 0) - { - delay = 1; - } + delay = i; } clear_message(); break; @@ -813,7 +860,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", { if ((errmsg = kill_procs(tempbuf2)) != NULL) { - new_message(MT_standout, errmsg); + new_message(MT_standout, "%s", errmsg); putchar('\r'); no_command = Yes; } @@ -830,7 +877,7 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", { if ((errmsg = renice_procs(tempbuf2)) != NULL) { - new_message(MT_standout, errmsg); + new_message(MT_standout, "%s", errmsg); putchar('\r'); no_command = Yes; } @@ -850,6 +897,14 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", putchar('\r'); break; + case CMD_selftog: + ps.self = (ps.self == -1) ? getpid() : -1; + new_message(MT_standout | MT_delayed, + " %sisplaying self.", + (ps.self == -1) ? "D" : "Not d"); + putchar('\r'); + break; + case CMD_user: new_message(MT_standout, "Username to show: "); @@ -916,9 +971,6 @@ Usage: %s [-ISbinqu] [-d x] [-s x] [-o field] [-U username] [number]\n", } } -#ifdef DEBUG - fclose(debug); -#endif quit(0); /*NOTREACHED*/ } @@ -935,6 +987,7 @@ reset_display() d_procstates = i_procstates; d_cpustates = i_cpustates; d_memory = i_memory; + d_swap = i_swap; d_message = i_message; d_header = i_header; d_process = i_process; @@ -947,8 +1000,7 @@ reset_display() sigret_t leave() /* exit under normal conditions -- INT handler */ { - end_screen(); - exit(0); + leaveflag = 1; } sigret_t tstop(i) /* SIGTSTP handler */ @@ -956,31 +1008,7 @@ sigret_t tstop(i) /* SIGTSTP handler */ int i; { - /* 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 */ -#ifdef SIGRELSE - sigrelse(SIGTSTP); -#else - (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1))); -#endif - (void) kill(0, SIGTSTP); - - /* reset the signal handler */ - (void) signal(SIGTSTP, tstop); - - /* reinit screen */ - reinit_screen(); - - /* jump to appropriate place */ - longjmp(jmp_int, 1); - - /*NOTREACHED*/ + tstopflag = 1; } #ifdef SIGWINCH @@ -989,17 +1017,7 @@ sigret_t winch(i) /* SIGWINCH handler */ int i; { - /* reascertain the screen dimensions */ - get_screensize(); - - /* tell display to resize */ - max_topn = display_resize(); - - /* reset the signal handler */ - (void) signal(SIGWINCH, winch); - - /* jump to appropriate place */ - longjmp(jmp_int, 1); + winchflag = 1; } #endif diff --git a/contrib/top/top.h b/contrib/top/top.h index 8f50922..aeed136 100644 --- a/contrib/top/top.h +++ b/contrib/top/top.h @@ -8,7 +8,7 @@ #define VERSION 3 /* Number of lines of header information on the standard screen */ -#define Header_lines 6 +#define Header_lines 7 /* Maximum number of columns allowed for display */ #define MAX_COLS 128 diff --git a/contrib/top/username.c b/contrib/top/username.c index 4a98a2b..dbead50 100644 --- a/contrib/top/username.c +++ b/contrib/top/username.c @@ -30,13 +30,14 @@ #include <stdio.h> #include <pwd.h> +#include <utmp.h> #include "top.local.h" #include "utils.h" struct hash_el { int uid; - char name[9]; + char name[UT_NAMESIZE + 1]; }; #define is_empty_hash(x) (hash_table[x].name[0] == 0) @@ -125,7 +126,7 @@ int wecare; /* 1 = enter it always, 0 = nice to have */ /* empty or wrong slot -- fill it with new value */ hash_table[hashindex].uid = uid; - (void) strncpy(hash_table[hashindex].name, name, 8); + (void) strncpy(hash_table[hashindex].name, name, UT_NAMESIZE); return(hashindex); } diff --git a/contrib/top/utils.c b/contrib/top/utils.c index e84fa3a..94233de 100644 --- a/contrib/top/utils.c +++ b/contrib/top/utils.c @@ -308,9 +308,13 @@ long *diffs; /* calculate percentages based on overall change, rounding up */ half_total = total_change / 2l; - for (i = 0; i < cnt; i++) - { - *out++ = (int)((*diffs++ * 1000 + half_total) / total_change); + + /* Do not divide by 0. Causes Floating point exception */ + if(total_change) { + for (i = 0; i < cnt; i++) + { + *out++ = (int)((*diffs++ * 1000 + half_total) / total_change); + } } /* return the total in case the caller wants to use it */ @@ -320,42 +324,24 @@ long *diffs; /* * errmsg(errnum) - return an error message string appropriate to the * error number "errnum". This is a substitute for the System V - * function "strerror". There appears to be no reliable way to - * determine if "strerror" exists at compile time, so I make do - * by providing something of similar functionality. For those - * systems that have strerror and NOT errlist, define - * -DHAVE_STRERROR in the module file and this function will - * use strerror. + * function "strerror" with one important difference: the string + * returned by this function does NOT end in a newline! + * N.B.: there appears to be no reliable way to determine if + * "strerror" exists at compile time, so I make do by providing + * something of similar functionality. */ /* externs referenced by errmsg */ -#ifndef HAVE_STRERROR -#ifndef SYS_ERRLIST_DECLARED -#define SYS_ERRLIST_DECLARED -extern char *sys_errlist[]; -#endif - -extern int sys_nerr; -#endif - char *errmsg(errnum) int errnum; { -#ifdef HAVE_STRERROR - char *msg = strerror(errnum); - if (msg != NULL) - { - return msg; - } -#else if (errnum > 0 && errnum < sys_nerr) { - return(sys_errlist[errnum]); + return((char *)sys_errlist[errnum]); } -#endif return("No error"); } @@ -405,7 +391,8 @@ long seconds; { /* standard method produces MMM:SS */ /* we avoid printf as must as possible to make this quick */ - sprintf(result, "%3d:%02d", seconds / 60l, seconds % 60l); + sprintf(result, "%3ld:%02ld", + (long)(seconds / 60), (long)(seconds % 60)); } return(result); } @@ -466,3 +453,35 @@ int amt; return(ret); } + +char *format_k2(amt) + +int amt; + +{ + static char retarray[NUM_STRINGS][16]; + static int index = 0; + register char *p; + register char *ret; + register char tag = 'K'; + + p = ret = retarray[index]; + index = (index + 1) % NUM_STRINGS; + + if (amt >= 100000) + { + amt = (amt + 512) / 1024; + tag = 'M'; + if (amt >= 100000) + { + amt = (amt + 512) / 1024; + tag = 'G'; + } + } + + p = strecpy(p, itoa(amt)); + *p++ = tag; + *p = '\0'; + + return(ret); +} diff --git a/contrib/top/utils.h b/contrib/top/utils.h index 628a0be..6717092 100644 --- a/contrib/top/utils.h +++ b/contrib/top/utils.h @@ -21,3 +21,4 @@ long percentages(); char *errmsg(); char *format_time(); char *format_k(); +char *format_k2(); |