On Sat, Oct 27, 2018 at 06:28:41PM +0200, Klemens Nanni wrote:
> +     } else
> +             uidp = &ps.uid;
> +             ps.huid = -1;
And here I was sloppy. Sorry.

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 16:31:50 -0000
@@ -131,6 +131,30 @@ 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;
+               ps.uid = -1;
+       } else {
+               uidp = &ps.uid;
+               ps.huid = -1;
+       }
+
+       return uid_from_user(bufp, uidp);
+}
+
 static void
 parseargs(int ac, char **av)
 {
@@ -150,17 +174,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 +814,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

Reply via email to