Add a -q option for pgrep to not print anything; similar to grep -q.

This commit is contained in:
simonb 2022-10-29 08:17:16 +00:00
parent 5abcf2f695
commit 49ef36cef5
2 changed files with 23 additions and 7 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: pkill.1,v 1.31 2022/01/01 08:34:35 msaitoh Exp $
.\" $NetBSD: pkill.1,v 1.32 2022/10/29 08:17:16 simonb Exp $
.\"
.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -27,7 +27,7 @@
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd December 3, 2018
.Dd October 29, 2022
.Dt PKILL 1
.Os
.Sh NAME
@ -37,7 +37,7 @@
.Nd find or signal processes by name
.Sh SYNOPSIS
.Nm pgrep
.Op Fl filnvx
.Op Fl filnqvx
.Op Fl d Ar delim
.Op Fl G Ar gid
.Op Fl g Ar pgrp
@ -179,6 +179,13 @@ This option is valid only when given as the first argument to
.Nm pkill .
.El
.Pp
The following option is also available for
.Nm pgrep :
.Bl -tag -width xxxxxxxx
.It Fl q
Quiet; do not write anything to standard output.
.El
.Pp
The
.Fl l
flag is also available for

View File

@ -1,4 +1,4 @@
/* $NetBSD: pkill.c,v 1.32 2022/07/02 20:50:26 ad Exp $ */
/* $NetBSD: pkill.c,v 1.33 2022/10/29 08:17:16 simonb Exp $ */
/*-
* Copyright (c) 2002, 2022 The NetBSD Foundation, Inc.
@ -31,7 +31,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: pkill.c,v 1.32 2022/07/02 20:50:26 ad Exp $");
__RCSID("$NetBSD: pkill.c,v 1.33 2022/10/29 08:17:16 simonb Exp $");
#endif /* !lint */
#include <sys/types.h>
@ -87,6 +87,7 @@ static int prenice;
static int signum = SIGTERM;
static int nicenum;
static int newest;
static int quiet;
static int inverse;
static int longfmt;
static int matchargs;
@ -181,7 +182,7 @@ main(int argc, char **argv)
} else
usage();
} else {
while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1)
while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilnqs:t:u:vx")) != -1)
switch (ch) {
case 'G':
makelist(&rgidlist, LT_GROUP, optarg);
@ -217,6 +218,11 @@ main(int argc, char **argv)
newest = 1;
criteria = 1;
break;
case 'q':
if (!pgrep)
usage();
quiet = 1;
break;
case 's':
makelist(&sidlist, LT_SID, optarg);
criteria = 1;
@ -443,7 +449,7 @@ usage(void)
getprogname());
else {
if (pgrep)
ustr = "[-filnvx] [-d delim]";
ustr = "[-filnqvx] [-d delim]";
else
ustr = "[-signal] [-filnvx]";
@ -514,6 +520,9 @@ grepact(const struct kinfo_proc2 *kp)
{
char **argv;
if (quiet)
return 1;
if (longfmt && matchargs) {
/*