Reduce four almost identical uid_from_user(3) calls into one by handling
negation once in a way show/hide agnostic way.
I want to add support for UIDs besides usernames, so this diff is a
preliminary cleanup to allow for this feature addition to happen in the
newly introduced function.
The internal pointer `bufp' to the argument `buf' is neccessary since
the second caller `rundisplay()' passes `char tempbuf[TEMPBUFSIZE]' to
it.
Clearing the optionally prefixed dash is not relevant. It merely ensures
that error messages still look the same, otherwise `top -U -foo' would
print " -foo: unknown user".
Feedback? OK?
Index: top.c
===================================================================
RCS file: /cvs/src/usr.bin/top/top.c,v
retrieving revision 1.94
diff -u -p -r1.94 top.c
--- top.c 5 Oct 2018 18:56:57 -0000 1.94
+++ top.c 27 Oct 2018 15:48:07 -0000
@@ -131,6 +131,27 @@ usage(void)
__progname);
}
+static int
+filteruser(char buf[])
+{
+ char *bufp = buf;
+ uid_t *uidp;
+
+ if (strcmp(bufp, "+") == 0) {
+ ps.uid = (uid_t)-1;
+ ps.huid = (uid_t)-1;
+ return 0;
+ }
+
+ if (bufp[0] == '-') {
+ bufp++[0] = ' ';
+ uidp = &ps.huid;
+ } else
+ uidp = &ps.uid;
+
+ return uid_from_user(bufp, uidp);
+}
+
static void
parseargs(int ac, char **av)
{
@@ -150,17 +171,9 @@ parseargs(int ac, char **av)
break;
case 'U': /* display only username's processes */
- if (optarg[0] == '-') {
- if (uid_from_user(optarg+1, &ps.huid) == -1)
- new_message(MT_delayed,
- "%s: unknown user", optarg);
- else
- ps.uid = (uid_t)-1;
- } else if (uid_from_user(optarg, &ps.uid) == -1)
+ if (filteruser(optarg) == -1)
new_message(MT_delayed, "%s: unknown user",
optarg);
- else
- ps.huid = (uid_t)-1;
break;
case 'p': { /* display only process id */
@@ -798,24 +811,10 @@ rundisplay(void)
new_message(MT_standout,
"Username to show: ");
if (readline(tempbuf, sizeof(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 (uid_from_user(tempbuf+1, &ps.huid)
== -1) {
- new_message(MT_standout,
- " %s: unknown user",
tempbuf+1);
- no_command = Yes;
- } else {
- ps.uid = (uid_t)-1;
- }
- } else if (uid_from_user(tempbuf, &ps.uid) ==
-1) {
- new_message(MT_standout,
- " %s: unknown user",
tempbuf);
- no_command = Yes;
- } else {
- ps.huid = (uid_t)-1;
+ if (filteruser(tempbuf) == -1) {
+ new_message(MT_standout,
+ "%s: unknown user", tempbuf);
+ no_command = Yes;
}
putr();
} else