On Sun, Apr 16, 2023 at 01:37:30AM +0000, Klemens Nanni wrote:
> ps(1) -U expects names, top(1) and pgrep(1) -U take numbers as well.
>
> With the new tree view, I start using 'ps -fU55' more often instead of
> 'top -U55' to watch ports builds, but keep doing
>
> $ ps -fU55
> ps: p55: no such user
> $ ps -fU_pbuild
> ...
>
> The stray "p" in current error messages comes kludge_oldps_options()
> treats numbers as PIDs, but that makes no sense for a mandatory -U
> argument, imho, so exlucde -U here to make '-U55' work like '-U 55'.
>
> Make ps take both formats and use top's argument name/error message
> for consistency.
>
> $ ./ojb/ps -fUnope
> ps: nope: unknown user
> $ ps -fU55
> ...
>
> Did I miss anything wrt. ps' old vs. new option parsing logic?
> Feedback? Objection? OK?
Anyone? fstat(1) also takes both name and number.
Index: ps.1
===================================================================
RCS file: /cvs/src/bin/ps/ps.1,v
retrieving revision 1.129
diff -u -p -r1.129 ps.1
--- ps.1 13 Oct 2022 21:37:05 -0000 1.129
+++ ps.1 24 Apr 2023 11:19:22 -0000
@@ -47,7 +47,7 @@
.Op Fl o Ar fmt
.Op Fl p Ar pid
.Op Fl t Ar tty
-.Op Fl U Ar username
+.Op Fl U Ar user
.Op Fl W Ar swap
.Sh DESCRIPTION
The
@@ -150,9 +150,10 @@ with the standard input.
.It Fl t Ar tty
Display information about processes attached to the specified terminal
device.
-.It Fl U Ar username
+.It Fl U Ar user
Display the processes belonging to the specified
-.Ar username .
+username or UID
+.Ar user .
.It Fl u
Display information associated with the following keywords:
user, pid, %cpu, %mem, vsz, rss, tt, state, start, time, and command.
Index: ps.c
===================================================================
RCS file: /cvs/src/bin/ps/ps.c,v
retrieving revision 1.79
diff -u -p -r1.79 ps.c
--- ps.c 1 Sep 2022 21:15:54 -0000 1.79
+++ ps.c 24 Apr 2023 11:19:22 -0000
@@ -226,11 +226,24 @@ main(int argc, char *argv[])
ttydev = sb.st_rdev;
break;
}
- case 'U':
- if (uid_from_user(optarg, &uid) == -1)
- errx(1, "%s: no such user", optarg);
+ case 'U': {
+ int found = 0;
+
+ if (uid_from_user(optarg, &uid) == 0)
+ found = 1;
+ else {
+ const char *errstr;
+
+ uid = strtonum(optarg, 0, UID_MAX, &errstr);
+ if (errstr == NULL &&
+ user_from_uid(uid, 1) != NULL)
+ found = 1;
+ }
+ if (!found)
+ errx(1, "%s: unknown user", optarg);
Uflag = xflg = 1;
break;
+ }
case 'u':
parsefmt(ufmt);
sortby = SORTCPU;
@@ -480,11 +493,12 @@ kludge_oldps_options(char *s)
memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */
ns += cp - s;
/*
- * if there's a trailing number, and not a preceding 'p' (pid) or
- * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
+ * if there's a trailing number, and not a preceding 'p' (pid),
+ * 't' (tty) or 'U' (user) flag,
+ * then assume it's a pid and insert a 'p' flag.
*/
if (isdigit((unsigned char)*cp) &&
- (cp == s || (cp[-1] != 't' && cp[-1] != 'p' &&
+ (cp == s || (cp[-1] != 't' && cp[-1] != 'p' && cp[-1] != 'U' &&
(cp - 1 == s || cp[-2] != 't'))))
*ns++ = 'p';
/* and append the number */
@@ -611,7 +625,7 @@ usage(void)
{
fprintf(stderr, "usage: %s [-AacefHhjkLlmrSTuvwx] [-M core] [-N system]"
" [-O fmt] [-o fmt] [-p pid]\n", __progname);
- fprintf(stderr, "%-*s[-t tty] [-U username] [-W swap]\n",
+ fprintf(stderr, "%-*s[-t tty] [-U user] [-W swap]\n",
(int)strlen(__progname) + 8, "");
exit(1);
}