top: enable user filtering via inclusion and exclusion

With this commit top can now display users which match
a user id/name or just those users which do not match.

The distinction is based on the presence or absence of
a leading exclamation point '!' (C negation operator).

(everything is perfectly justified plus right margins)
(are completely filled, but of course it must be luck)

Reference(s):
Wishlist, http://bugs.debian.org/682086

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2013-02-13 00:00:00 -06:00 committed by Craig Small
parent c856a80ad5
commit d04297843f
3 changed files with 16 additions and 8 deletions

View File

@ -381,6 +381,9 @@ Display only processes with a user id or user name matching that given.
The '\-u' option matches on \fI effective\fR user whereas the '\-U' option The '\-u' option matches on \fI effective\fR user whereas the '\-U' option
matches on\fI any\fR user (real, effective, saved, or filesystem). matches on\fI any\fR user (real, effective, saved, or filesystem).
Prepending an exclamation point ('!') to the user id or name instucts top to
display only processes with users not matching the one provided.
The 'p', 'u' and 'U' \*(COs are mutually exclusive. The 'p', 'u' and 'U' \*(COs are mutually exclusive.
.TP 5 .TP 5
@ -1296,8 +1299,10 @@ matches on\fB any\fR user (real, effective, saved, or filesystem).
Thereafter, in that \*(TW only matching users will be shown, or possibly Thereafter, in that \*(TW only matching users will be shown, or possibly
no processes will be shown. no processes will be shown.
Different \*(TWs can can be used to filter different users. Prepending an exclamation point ('!') to the user id or name instucts top
to display only processes with users not matching the one provided.
Different \*(TWs can can be used to filter different users.
Later, if you wish to monitor all tasks again in the \*(CW, re-issue this Later, if you wish to monitor all tasks again in the \*(CW, re-issue this
command but just press <Enter> at the prompt. command but just press <Enter> at the prompt.

View File

@ -1218,8 +1218,10 @@ static const char *user_certify (WIN_t *q, const char *str, char typ) {
uid_t num; uid_t num;
q->usrseltyp = 0; q->usrseltyp = 0;
q->usrselflg = 1;
Monpidsidx = 0; Monpidsidx = 0;
if (*str) { if (*str) {
if ('!' == *str) { ++str; q->usrselflg = 0; }
num = (uid_t)strtoul(str, &endp, 0); num = (uid_t)strtoul(str, &endp, 0);
if ('\0' == *endp) { if ('\0' == *endp) {
pwd = getpwuid(num); pwd = getpwuid(num);
@ -1247,19 +1249,19 @@ static const char *user_certify (WIN_t *q, const char *str, char typ) {
static inline int user_matched (WIN_t *q, const proc_t *p) { static inline int user_matched (WIN_t *q, const proc_t *p) {
switch(q->usrseltyp) { switch(q->usrseltyp) {
case 0: // uid selection inactive case 0: // uid selection inactive
return 1; return q->usrselflg;
case 'U': // match any uid case 'U': // match any uid
if (p->ruid == q->usrseluid) return 1; if (p->ruid == q->usrseluid) return q->usrselflg;
if (p->suid == q->usrseluid) return 1; if (p->suid == q->usrseluid) return q->usrselflg;
if (p->fuid == q->usrseluid) return 1; if (p->fuid == q->usrseluid) return q->usrselflg;
// fall through... // fall through...
case 'u': // match effective uid case 'u': // match effective uid
if (p->euid == q->usrseluid) return 1; if (p->euid == q->usrseluid) return q->usrselflg;
// fall through... // fall through...
default: // no match, don't display default: // no match...
; ;
} }
return 0; return !q->usrselflg;
} // end: user_matched } // end: user_matched
/*###### Basic Formatting support ######################################*/ /*###### Basic Formatting support ######################################*/

View File

@ -371,6 +371,7 @@ typedef struct WIN_t {
varcolsz, // max length of variable width column(s) varcolsz, // max length of variable width column(s)
usrseluid, // validated uid for 'u/U' user selection usrseluid, // validated uid for 'u/U' user selection
usrseltyp, // the basis for matching above uid usrseltyp, // the basis for matching above uid
usrselflg, // flag denoting include/exclude matches
hdrcaplen; // column header xtra caps len, if any hdrcaplen; // column header xtra caps len, if any
char capclr_sum [CLRBUFSIZ], // terminfo strings built from char capclr_sum [CLRBUFSIZ], // terminfo strings built from
capclr_msg [CLRBUFSIZ], // RCW_t colors (& rebuilt too), capclr_msg [CLRBUFSIZ], // RCW_t colors (& rebuilt too),