pgrep: add -c option for counting number of matched proceesses

A patch from Debian.

Bug-Debian: http://bugs.debian.org/375791
Backported-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Craig Small 2009-11-24 11:00:45 +01:00 committed by Craig Small
parent 5d29bfedc8
commit 6bc22a379f
2 changed files with 17 additions and 7 deletions

View File

@ -8,7 +8,7 @@ pgrep, pkill \- look up or signal processes based on name and other attributes
.SH SYNOPSIS .SH SYNOPSIS
.na .na
\fBpgrep\fR [\fB\-flvx\fR] [\fB\-d\ \fIdelimiter\fR] [\fB\-n\fR|\fB\-o\fR] \ \fBpgrep\fR [\fB\-cflvx\fR] [\fB\-d\ \fIdelimiter\fR] [\fB\-n\fR|\fB\-o\fR] \
[\fB\-P\ \fIppid\fR,...] [\fB\-g\ \fIpgrp\fR,...] [\fB\-s\ \fIsid\fR,...] \ [\fB\-P\ \fIppid\fR,...] [\fB\-g\ \fIpgrp\fR,...] [\fB\-s\ \fIsid\fR,...] \
[\fB\-u\ \fIeuid\fR,...] [\fB\-U\ \fIuid\fR,...] [\fB\-G\ \fIgid\fR,...] \ [\fB\-u\ \fIeuid\fR,...] [\fB\-U\ \fIuid\fR,...] [\fB\-G\ \fIgid\fR,...] \
[\fB\-t\ \fIterm\fR,...] [\fIpattern\fR] [\fB\-t\ \fIterm\fR,...] [\fIpattern\fR]
@ -41,6 +41,8 @@ will list the processes owned by \fBroot\fP OR \fBdaemon\fP.
to each process instead of listing them on stdout. to each process instead of listing them on stdout.
.SH OPTIONS .SH OPTIONS
\-c
Suppress normal output; instead print a count of matching processes.
.TP .TP
\fB\-d \fIdelimiter\fP \fB\-d \fIdelimiter\fP
Sets the string used to delimit each process ID in the output (by Sets the string used to delimit each process ID in the output (by

20
pgrep.c
View File

@ -54,6 +54,7 @@ static int opt_oldest = 0;
static int opt_newest = 0; static int opt_newest = 0;
static int opt_negate = 0; static int opt_negate = 0;
static int opt_exact = 0; static int opt_exact = 0;
static int opt_count = 0;
static int opt_signal = SIGTERM; static int opt_signal = SIGTERM;
static int opt_lock = 0; static int opt_lock = 0;
static int opt_case = 0; static int opt_case = 0;
@ -79,7 +80,7 @@ static int usage (int opt)
if (i_am_pkill) if (i_am_pkill)
fprintf (fp, "Usage: pkill [-SIGNAL] [-fvx] "); fprintf (fp, "Usage: pkill [-SIGNAL] [-fvx] ");
else else
fprintf (fp, "Usage: pgrep [-flvx] [-d DELIM] "); fprintf (fp, "Usage: pgrep [-cflvx] [-d DELIM] ");
fprintf (fp, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n" fprintf (fp, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n"
"\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] " "\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] "
"[PATTERN]\n"); "[PATTERN]\n");
@ -567,7 +568,7 @@ static void parse_opts (int argc, char **argv)
strcat (opts, "ld:"); strcat (opts, "ld:");
} }
strcat (opts, "LF:fnovxP:g:s:u:U:G:t:?V"); strcat (opts, "LF:cfnovxP:g:s:u:U:G:t:?V");
while ((opt = getopt (argc, argv, opts)) != -1) { while ((opt = getopt (argc, argv, opts)) != -1) {
switch (opt) { switch (opt) {
@ -615,6 +616,9 @@ static void parse_opts (int argc, char **argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
// case 'c': // Solaris: match by contract ID // case 'c': // Solaris: match by contract ID
// break; // break;
case 'c':
opt_count = 1;
break;
case 'd': // Solaris: change the delimiter case 'd': // Solaris: change the delimiter
opt_delim = strdup (optarg); opt_delim = strdup (optarg);
break; break;
@ -726,10 +730,14 @@ int main (int argc, char *argv[])
procs[i].num, strerror (errno)); procs[i].num, strerror (errno));
} }
} else { } else {
if (opt_long) if (opt_count) {
output_strlist(procs,num); fprintf(stdout, "%ld\n", num);
else } else {
output_numlist(procs,num); if (opt_long)
output_strlist (procs,num);
else
output_numlist (procs,num);
}
} }
return !num; // exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE) return !num; // exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE)
} }