From 52e706cafb7498c8135e3e00753151ef3ee86902 Mon Sep 17 00:00:00 2001 From: kn Date: Fri, 10 Nov 2023 09:17:02 +0000 Subject: [PATCH] accept numerical user IDs Turn [-U username] into [-U user] to match top(1)/pgrep(1)/fstat(1) -U/-u taking both "root" and "0". Feedback OK millert --- bin/ps/ps.1 | 12 ++++++------ bin/ps/ps.c | 30 ++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/bin/ps/ps.1 b/bin/ps/ps.1 index 9ce71cb3aa0..3abfd34f3bc 100644 --- a/bin/ps/ps.1 +++ b/bin/ps/ps.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ps.1,v 1.130 2023/07/06 10:07:09 deraadt Exp $ +.\" $OpenBSD: ps.1,v 1.131 2023/11/10 09:17:02 kn Exp $ .\" $NetBSD: ps.1,v 1.16 1996/03/21 01:36:28 jtc Exp $ .\" .\" Copyright (c) 1980, 1990, 1991, 1993, 1994 @@ -30,7 +30,7 @@ .\" .\" @(#)ps.1 8.3 (Berkeley) 4/18/94 .\" -.Dd $Mdocdate: July 6 2023 $ +.Dd $Mdocdate: November 10 2023 $ .Dt PS 1 .Os .Sh NAME @@ -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,9 @@ with the standard input. .It Fl t Ar tty Display information about processes attached to the specified terminal device. -.It Fl U Ar username -Display the processes belonging to the specified -.Ar username . +.It Fl U Ar user +Only display processes belonging to 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. diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 4cf9bfd066b..fe62dad044e 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ps.c,v 1.79 2022/09/01 21:15:54 job Exp $ */ +/* $OpenBSD: ps.c,v 1.80 2023/11/10 09:17:02 kn Exp $ */ /* $NetBSD: ps.c,v 1.15 1995/05/18 20:33:25 mycroft Exp $ */ /*- @@ -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); } -- 2.20.1