version 1.34, 2004/09/14 22:55:48 |
version 1.35, 2004/10/07 06:26:12 |
|
|
#include <string.h> |
#include <string.h> |
#include <poll.h> |
#include <poll.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <limits.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
/* includes specific to top */ |
/* includes specific to top */ |
|
|
char *order_name = NULL; |
char *order_name = NULL; |
int topn = Default_TOPN; |
int topn = Default_TOPN; |
int no_command = Yes; |
int no_command = Yes; |
|
int old_system = No; |
|
|
#if Default_TOPN == Infinity |
#if Default_TOPN == Infinity |
char topn_specified = No; |
char topn_specified = No; |
|
|
#define CMD_user 14 |
#define CMD_user 14 |
#define CMD_system 15 |
#define CMD_system 15 |
#define CMD_order 16 |
#define CMD_order 16 |
|
#define CMD_pid 17 |
|
|
static void |
static void |
usage(void) |
usage(void) |
|
|
extern char *__progname; |
extern char *__progname; |
|
|
fprintf(stderr, |
fprintf(stderr, |
"usage: %s [-biInqSu] [-d count] [-o field] [-s time] [-U username] [number]\n", |
"usage: %s [-biInqSu] [-d count] [-o field] [-p pid] [-s time] [-U username] [number]\n", |
__progname); |
__progname); |
} |
} |
|
|
|
|
char *endp; |
char *endp; |
int i; |
int i; |
|
|
while ((i = getopt(ac, av, "SIbinqus:d:U:o:")) != -1) { |
while ((i = getopt(ac, av, "SIbinqus:d:p:U:o:")) != -1) { |
switch (i) { |
switch (i) { |
case 'u': /* toggle uid/username display */ |
case 'u': /* toggle uid/username display */ |
do_unames = !do_unames; |
do_unames = !do_unames; |
|
|
} |
} |
break; |
break; |
|
|
|
case 'p': { /* display only process id */ |
|
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': /* 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 = Yes; |
ps.idle = Yes; |
ps.system = No; |
ps.system = No; |
ps.uid = (uid_t)-1; |
ps.uid = (uid_t)-1; |
|
ps.pid = (pid_t)-1; |
ps.command = NULL; |
ps.command = NULL; |
|
|
/* get preset options from the environment */ |
/* get preset options from the environment */ |
|
|
int change, i; |
int change, i; |
struct pollfd pfd[1]; |
struct pollfd pfd[1]; |
uid_t uid; |
uid_t uid; |
static char command_chars[] = "\f qh?en#sdkriIuSo"; |
static char command_chars[] = "\f qh?en#sdkriIuSop"; |
|
|
/* |
/* |
* assume valid command unless told |
* assume valid command unless told |
|
|
|
|
case CMD_system: |
case CMD_system: |
ps.system = !ps.system; |
ps.system = !ps.system; |
|
old_system = ps.system; |
new_message(MT_standout | MT_delayed, |
new_message(MT_standout | MT_delayed, |
" %sisplaying system processes.", |
" %sisplaying system processes.", |
ps.system ? "D" : "Not d"); |
ps.system ? "D" : "Not d"); |
|
|
no_command = Yes; |
no_command = Yes; |
} else |
} else |
order_index = i; |
order_index = i; |
|
if (putchar('\r') == EOF) |
|
exit(1); |
|
} else |
|
clear_message(); |
|
break; |
|
|
|
case CMD_pid: |
|
new_message(MT_standout, "Process id to show: "); |
|
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; |
|
|
|
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; |
|
} |
|
} |
if (putchar('\r') == EOF) |
if (putchar('\r') == EOF) |
exit(1); |
exit(1); |
} else |
} else |