On Sat, Jun 02, 2012 at 03:09:54PM -0400, Brynet wrote: > I wasn't sure about the interactions between getopt(3) and having an > '-' as an optarg. > > Seems there isn't anything obvious, so this allows users to be hidden > using: top -U -user > > Any comments? > -Bryan.
Hopefully this is the final diff. Showing a single user and then hiding them will now work as one would expect. I also forgot to update usage(). -Bryan. Index: display.c =================================================================== RCS file: /cvs/src/usr.bin/top/display.c,v retrieving revision 1.42 diff -u -p -u -r1.42 display.c --- display.c 15 Apr 2012 19:52:16 -0000 1.42 +++ display.c 3 Jun 2012 17:27:17 -0000 @@ -782,7 +782,7 @@ show_help(void) "r count pid - renice process `pid' to nice value `count'\n" "S - toggle the display of system processes\n" "s time - change delay between displays to `time' seconds\n" - "u user - display processes for `user' (u+ selects all users)\n" + "u [-]user - show processes for `user' (u+ shows all, u -user hides user)\n" "\n"); if (smart_terminal) { Index: machine.c =================================================================== RCS file: /cvs/src/usr.bin/top/machine.c,v retrieving revision 1.72 diff -u -p -u -r1.72 machine.c --- machine.c 21 Apr 2012 03:14:50 -0000 1.72 +++ machine.c 3 Jun 2012 17:27:18 -0000 @@ -330,6 +330,7 @@ get_process_info(struct system_info *si, int (*compare) (const void *, const void *)) { int show_idle, show_system, show_threads, show_uid, show_pid, show_cmd; + int hide_uid; int total_procs, active_procs; struct kinfo_proc **prefp, *pp; int what = KERN_PROC_KTHREAD; @@ -356,6 +357,7 @@ get_process_info(struct system_info *si, show_system = sel->system; show_threads = sel->threads; show_uid = sel->uid != (uid_t)-1; + hide_uid = sel->huid != (uid_t)-1; show_pid = sel->pid != (pid_t)-1; show_cmd = sel->command != NULL; @@ -381,6 +383,7 @@ get_process_info(struct system_info *si, if (pp->p_stat != SZOMB && (show_idle || pp->p_pctcpu != 0 || pp->p_stat == SRUN) && + (!hide_uid || pp->p_ruid != sel->huid) && (!show_uid || pp->p_ruid == sel->uid) && (!show_pid || pp->p_pid == sel->pid) && (!show_cmd || strstr(pp->p_comm, Index: machine.h =================================================================== RCS file: /cvs/src/usr.bin/top/machine.h,v retrieving revision 1.16 diff -u -p -u -r1.16 machine.h --- machine.h 10 Apr 2011 03:20:59 -0000 1.16 +++ machine.h 3 Jun 2012 17:27:18 -0000 @@ -74,6 +74,7 @@ struct process_select { int system; /* show system processes */ int threads; /* show threads */ uid_t uid; /* only this uid (unless uid == -1) */ + uid_t huid; /* hide this uid (unless huid == -1) */ pid_t pid; /* only this pid (unless pid == -1) */ char *command;/* only this command (unless == NULL) */ }; Index: top.1 =================================================================== RCS file: /cvs/src/usr.bin/top/top.1,v retrieving revision 1.59 diff -u -p -u -r1.59 top.1 --- top.1 16 Dec 2011 14:50:24 -0000 1.59 +++ top.1 3 Jun 2012 17:27:18 -0000 @@ -38,7 +38,7 @@ .Op Fl o Ar field .Op Fl p Ar pid .Op Fl s Ar time -.Op Fl U Ar user +.Oo Fl U Op Ar - Oc Ns Ar user .Op Ar number .Ek .Sh DESCRIPTION @@ -173,9 +173,14 @@ Set the delay between screen updates to seconds. The value may be fractional, to permit delays of less than 1 second. The default delay between updates is 5 seconds. -.It Fl U Ar user +.It Fl U Oo Ar - Oc Ns Ar user Show only those processes owned by .Ar user . +Prefix +.Ar - +with +.Ar user +to hide processes owned by that user. This option currently only accepts usernames and will not understand UID numbers. .It Fl u @@ -354,11 +359,15 @@ Toggle the display of system processes. Set the delay between screen updates to .Ar time seconds. -.It u Ar user +.It u Oo Ar - Oc Ns Ar user Show only those processes owned by .Ar user . .Sq u+ shows processes belonging to all users. +The +.Ar - +prefix hides processes belonging to a single +.Ar user . .El .Sh THE DISPLAY .\" The actual display varies depending on the specific variant of Unix Index: top.c =================================================================== RCS file: /cvs/src/usr.bin/top/top.c,v retrieving revision 1.77 diff -u -p -u -r1.77 top.c --- top.c 20 Apr 2012 16:36:11 -0000 1.77 +++ top.c 3 Jun 2012 17:27:18 -0000 @@ -126,7 +126,7 @@ usage(void) fprintf(stderr, "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o field] " - "[-p pid] [-s time]\n\t[-U user] [number]\n", + "[-p pid] [-s time]\n\t[-U [-]user] [number]\n", __progname); } @@ -149,7 +149,11 @@ parseargs(int ac, char **av) break; case 'U': /* display only username's processes */ - if ((ps.uid = userid(optarg)) == (uid_t)-1) + if (optarg[0] == '-') { + if ((ps.huid = userid(optarg+1)) == (uid_t)-1) + new_message(MT_delayed, "%s: unknown user", + optarg); + } else if ((ps.uid = userid(optarg)) == (uid_t)-1) new_message(MT_delayed, "%s: unknown user", optarg); break; @@ -282,6 +286,7 @@ main(int argc, char *argv[]) ps.idle = Yes; ps.system = No; ps.uid = (uid_t)-1; + ps.huid = (uid_t)-1; ps.pid = (pid_t)-1; ps.command = NULL; @@ -540,7 +545,7 @@ rundisplay(void) char ch, *iptr; int change, i; struct pollfd pfd[1]; - uid_t uid; + uid_t uid, huid; static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P1"; /* @@ -774,15 +779,27 @@ rundisplay(void) new_message(MT_standout, "Username to show: "); if (readline(tempbuf, sizeof(tempbuf)) > 0) { - if (tempbuf[0] == '+' && + if ((tempbuf[0] == '+' || tempbuf[0] == '-') && tempbuf[1] == '\0') { ps.uid = (uid_t)-1; + ps.huid = (uid_t)-1; + } else if (tempbuf[0] == '-') { + if ((huid = userid(tempbuf+1)) == (uid_t)-1) { + new_message(MT_standout, + " %s: unknown user", tempbuf); + no_command = Yes; + } else { + ps.huid = huid; + ps.uid = (uid_t)-1; + } } else if ((uid = userid(tempbuf)) == (uid_t)-1) { - new_message(MT_standout, - " %s: unknown user", tempbuf); - no_command = Yes; - } else + new_message(MT_standout, + " %s: unknown user", tempbuf); + no_command = Yes; + } else { ps.uid = uid; + ps.huid = (uid_t)-1; + } putr(); } else clear_message(); @@ -899,6 +916,7 @@ rundisplay(void) case CMD_add: ps.uid = (uid_t)-1; /* uid */ + ps.huid = (uid_t)-1; ps.pid = (pid_t)-1; /* pid */ ps.system = old_system; ps.command = NULL; /* grep */