Changeset View
Changeset View
Standalone View
Standalone View
top/top.c
Show First 20 Lines • Show All 272 Lines • ▼ Show 20 Lines | |||||
#endif | #endif | ||||
char ch; | char ch; | ||||
char *iptr; | char *iptr; | ||||
char no_command = 1; | char no_command = 1; | ||||
struct timeval timeout; | struct timeval timeout; | ||||
char *order_name = NULL; | char *order_name = NULL; | ||||
int order_index = 0; | int order_index = 0; | ||||
fd_set readfds; | fd_set readfds; | ||||
char old_system = No; | |||||
static char command_chars[] = "\f qh?en#sdkriIutHmSCajzPJwo"; | static char command_chars[] = "\f qh?en#sdkriIutHmSCajzPJwop"; | ||||
/* 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_redraw 0 | ||||
#define CMD_update 1 | #define CMD_update 1 | ||||
#define CMD_quit 2 | #define CMD_quit 2 | ||||
#define CMD_help1 3 | #define CMD_help1 3 | ||||
#define CMD_help2 4 | #define CMD_help2 4 | ||||
#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */ | #define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */ | ||||
#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */ | #define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */ | ||||
Show All 12 Lines | |||||
#define CMD_viewsys 18 | #define CMD_viewsys 18 | ||||
#define CMD_wcputog 19 | #define CMD_wcputog 19 | ||||
#define CMD_showargs 20 | #define CMD_showargs 20 | ||||
#define CMD_jidtog 21 | #define CMD_jidtog 21 | ||||
#define CMD_kidletog 22 | #define CMD_kidletog 22 | ||||
#define CMD_pcputog 23 | #define CMD_pcputog 23 | ||||
#define CMD_jail 24 | #define CMD_jail 24 | ||||
#define CMD_swaptog 25 | #define CMD_swaptog 25 | ||||
#define CMD_order 26 | #define CMD_order 26 | ||||
#define CMD_pid 27 | |||||
/* set the buffer for stdout */ | /* set the buffer for stdout */ | ||||
#ifdef DEBUG | #ifdef DEBUG | ||||
extern FILE *debug; | extern FILE *debug; | ||||
debug = fopen("debug.run", "w"); | debug = fopen("debug.run", "w"); | ||||
setbuffer(stdout, NULL, 0); | setbuffer(stdout, NULL, 0); | ||||
#else | #else | ||||
setbuffer(stdout, stdoutbuf, Buffersize); | setbuffer(stdout, stdoutbuf, Buffersize); | ||||
Show All 18 Lines | else | ||||
ps.system = No; | ps.system = No; | ||||
reset_uids(); | reset_uids(); | ||||
ps.thread = No; | ps.thread = No; | ||||
ps.wcpu = 1; | ps.wcpu = 1; | ||||
ps.jid = -1; | ps.jid = -1; | ||||
ps.jail = No; | ps.jail = No; | ||||
ps.swap = No; | ps.swap = No; | ||||
ps.kidle = Yes; | ps.kidle = Yes; | ||||
ps.pid = -1; | |||||
ps.command = NULL; | ps.command = NULL; | ||||
/* get preset options from the environment */ | /* get preset options from the environment */ | ||||
if ((env_top = getenv("TOP")) != NULL) | if ((env_top = getenv("TOP")) != NULL) | ||||
{ | { | ||||
av = preset_argv = argparse(env_top, &preset_argc); | av = preset_argv = argparse(env_top, &preset_argc); | ||||
ac = preset_argc; | ac = preset_argc; | ||||
Show All 9 Lines | #endif | ||||
{ | { | ||||
ac = argc; | ac = argc; | ||||
av = argv; | av = argv; | ||||
/* this should keep getopt happy... */ | /* this should keep getopt happy... */ | ||||
optind = 1; | optind = 1; | ||||
} | } | ||||
while ((i = getopt(ac, av, "CSIHPabijJ:nquvzs:d:U:m:o:tw")) != EOF) | while ((i = getopt(ac, av, "CSIHPabijJ:nquvzs:d:U:m:o:p:tw")) != EOF) | ||||
{ | { | ||||
switch(i) | switch(i) | ||||
{ | { | ||||
case 'v': /* show version number */ | case 'v': /* show version number */ | ||||
fprintf(stderr, "%s: version FreeBSD\n", myname); | fprintf(stderr, "%s: version FreeBSD\n", myname); | ||||
exit(1); | exit(1); | ||||
break; | break; | ||||
case 'u': /* toggle uid/username display */ | case 'u': /* toggle uid/username display */ | ||||
do_unames = !do_unames; | do_unames = !do_unames; | ||||
break; | break; | ||||
case 'U': /* display only username's processes */ | case 'U': /* display only username's processes */ | ||||
if ((ps.uid[0] = userid(optarg)) == -1) | if ((ps.uid[0] = userid(optarg)) == -1) | ||||
{ | { | ||||
fprintf(stderr, "%s: unknown user\n", optarg); | fprintf(stderr, "%s: unknown user\n", optarg); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
break; | break; | ||||
case 'S': /* show system processes */ | case 'S': /* show system processes */ | ||||
ps.system = !ps.system; | ps.system = Yes; | ||||
old_system = Yes; | |||||
break; | break; | ||||
case 'I': /* show idle processes */ | case 'I': /* show idle processes */ | ||||
ps.idle = !ps.idle; | ps.idle = !ps.idle; | ||||
break; | break; | ||||
case 'i': /* go interactive regardless */ | case 'i': /* go interactive regardless */ | ||||
interactive = Yes; | interactive = Yes; | ||||
Show All 16 Lines | if ((i = atoiwi(optarg)) == Invalid || i == 0) | ||||
myname); | myname); | ||||
warnings++; | warnings++; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
displays = i; | displays = i; | ||||
} | } | ||||
break; | break; | ||||
case 'p': { | |||||
unsigned long long num; | |||||
const char *errstr; | |||||
num = strtonum(optarg, 0, INT_MAX, &errstr); | |||||
if (errstr != NULL || !find_pid(num)) { | |||||
fprintf(stderr, "%s: unknown pid\n", optarg); | |||||
exit(1); | |||||
} | |||||
ps.pid = (pid_t)num; | |||||
ps.system = Yes; | |||||
break; | |||||
} | |||||
case 's': | case 's': | ||||
if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0)) | if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0)) | ||||
{ | { | ||||
fprintf(stderr, | fprintf(stderr, | ||||
"%s: warning: seconds delay should be positive -- using default\n", | "%s: warning: seconds delay should be positive -- using default\n", | ||||
myname); | myname); | ||||
delay = Default_DELAY; | delay = Default_DELAY; | ||||
warnings++; | warnings++; | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | while ((i = getopt(ac, av, "CSIHPabijJ:nquvzs:d:U:m:o:p:tw")) != EOF) | ||||
break; | break; | ||||
case 'z': | case 'z': | ||||
ps.kidle = !ps.kidle; | ps.kidle = !ps.kidle; | ||||
break; | break; | ||||
default: | default: | ||||
fprintf(stderr, | fprintf(stderr, | ||||
"Usage: %s [-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-s time]\n" | "Usage: %s [-abCHIijnPqStuvwz] [-d count] [-m io | cpu] [-o field] [-p pid]\n" | ||||
" [-J jail] [-U username] [number]\n", | " [-s time] [-J jail] [-U username] [number]\n", | ||||
myname); | myname); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
} | } | ||||
/* get count of top processes to display (if any) */ | /* get count of top processes to display (if any) */ | ||||
if (optind < ac) | if (optind < ac) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 574 Lines • ▼ Show 20 Lines | if (sel_ret > 0) | ||||
displaymode = 0; | displaymode = 0; | ||||
header_text = format_header(uname_field); | header_text = format_header(uname_field); | ||||
display_header(Yes); | display_header(Yes); | ||||
d_header = i_header; | d_header = i_header; | ||||
reset_display(); | reset_display(); | ||||
break; | break; | ||||
case CMD_viewsys: | case CMD_viewsys: | ||||
ps.system = !ps.system; | ps.system = !ps.system; | ||||
old_system = ps.system; | |||||
break; | break; | ||||
case CMD_showargs: | case CMD_showargs: | ||||
fmt_flags ^= FMT_SHOWARGS; | fmt_flags ^= FMT_SHOWARGS; | ||||
break; | break; | ||||
case CMD_order: | case CMD_order: | ||||
new_message(MT_standout, | new_message(MT_standout, | ||||
"Order to sort: "); | "Order to sort: "); | ||||
if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) | if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | if (sel_ret > 0) | ||||
case CMD_swaptog: | case CMD_swaptog: | ||||
ps.swap = !ps.swap; | ps.swap = !ps.swap; | ||||
new_message(MT_standout | MT_delayed, | new_message(MT_standout | MT_delayed, | ||||
" %sisplaying per-process swap usage.", | " %sisplaying per-process swap usage.", | ||||
ps.swap ? "D" : "Not d"); | ps.swap ? "D" : "Not d"); | ||||
header_text = format_header(uname_field); | header_text = format_header(uname_field); | ||||
reset_display(); | reset_display(); | ||||
putchar('\r'); | putchar('\r'); | ||||
break; | |||||
case CMD_pid: | |||||
new_message(MT_standout, | |||||
"Process id to show (+ for all): "); | |||||
if (readline(tempbuf2, sizeof(tempbuf2), No) > 0) { | |||||
if (tempbuf2[0] == '+' && | |||||
tempbuf2[1] == '\0') { | |||||
ps.pid = (pid_t)-1; | |||||
ps.system = old_system; | |||||
} else { | |||||
unsigned long long num; | |||||
const char *errstr; | |||||
eadler: Personally, I like declaring variables close to the use, but its atypical in freebsd code. Lets… | |||||
num = strtonum(tempbuf2, 0, INT_MAX, | |||||
&errstr); | |||||
if (errstr != NULL || !find_pid(num)) { | |||||
new_message(MT_standout, | |||||
" %s: unknown pid", | |||||
tempbuf2); | |||||
no_command = Yes; | |||||
} else { | |||||
if (ps.system == No) | |||||
old_system = No; | |||||
ps.pid = (pid_t)num; | |||||
ps.system = Yes; | |||||
} | |||||
} | |||||
putchar('\r'); | |||||
} else | |||||
clear_message(); | |||||
break; | break; | ||||
default: | default: | ||||
new_message(MT_standout, " BAD CASE IN SWITCH!"); | new_message(MT_standout, " BAD CASE IN SWITCH!"); | ||||
putchar('\r'); | putchar('\r'); | ||||
} | } | ||||
} | } | ||||
/* flush out stuff that may have been written */ | /* flush out stuff that may have been written */ | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |
Personally, I like declaring variables close to the use, but its atypical in freebsd code. Lets see if we could sneak it in. :-p