On Thu, Nov 01, 2018 at 02:56:38PM -0400, Ted Unangst wrote:
> this looks to be in the wrong order. if 1000 is a username, that should be
> matched first before numeric lookup.
Preferring UIDs indeed clobbers usernames with the respective numerical
username.

However, other parts in base such as getent(1) treat UIDs with higher
priority and behave like that already:

        $ doas useradd -- 1000
        useradd: Warning: home directory `/home/1000' doesn't exist, and -m was 
not specified
        $ getent passwd 1000
        kn:*:1000:1000:Klemens Nanni:/home/kn:/bin/ksh
        $ getent passwd $(id -u 1000)
        1000:*:1003:1003::/home/1000:/bin/ksh

Here's a diff that tries UIDs only if username lookup fails.

Index: top.1
===================================================================
RCS file: /cvs/src/usr.bin/top/top.1,v
retrieving revision 1.69
diff -u -p -r1.69 top.1
--- top.1       25 Jul 2018 17:24:14 -0000      1.69
+++ top.1       1 Nov 2018 20:01:02 -0000
@@ -182,8 +182,7 @@ Show only those processes owned by
 The prefix
 .Sq -
 hides processes owned by that user.
-This option currently only accepts usernames and does not understand
-UID numbers.
+This option accepts usernames and UID numbers.
 .It Fl u
 Do not take the time to map UID numbers to usernames.
 Normally,
Index: top.c
===================================================================
RCS file: /cvs/src/usr.bin/top/top.c,v
retrieving revision 1.95
diff -u -p -r1.95 top.c
--- top.c       1 Nov 2018 18:04:13 -0000       1.95
+++ top.c       1 Nov 2018 20:01:02 -0000
@@ -134,8 +134,10 @@ usage(void)
 static int
 filteruser(char buf[])
 {
+       const char *errstr;
        char *bufp = buf;
        uid_t *uidp;
+       uid_t uid;
 
        if (bufp[0] == '-') {
                bufp++;
@@ -146,7 +148,16 @@ filteruser(char buf[])
                ps.huid = (pid_t)-1;
        }
 
-       return uid_from_user(bufp, uidp);
+       if (uid_from_user(bufp, uidp) == 0)
+               return 0;
+
+       uid = strtonum(bufp, 0, UID_MAX, &errstr);
+       if (errstr == NULL && user_from_uid(uid, 1) != NULL) {
+               *uidp = uid;
+               return 0;
+       }
+
+       return -1;
 }
 
 static int

Reply via email to