Add -c count option to pkill

Possibly by a side-effect but pkill -c option used to work which would
print the number of killed processes.  This small change restores this
functionality.

Bug-Debian: http://bugs.debian.org/693783

Signed-off-by: Craig Small <csmall@enc.com.au>
This commit is contained in:
Craig Small 2012-11-21 22:11:17 +11:00
parent c5ee091e4c
commit ce95dd53eb
2 changed files with 8 additions and 6 deletions

View File

@ -49,7 +49,7 @@ only.)
\fB\-c\fR, \fB\-\-count\fR \fB\-c\fR, \fB\-\-count\fR
Suppress normal output; instead print a count of matching processes. When Suppress normal output; instead print a count of matching processes. When
count does not match anything, e.g. returns zero, the command will return count does not match anything, e.g. returns zero, the command will return
non-zero value. (\fBpgrep\fP only.) non-zero value.
.TP .TP
\fB\-d\fR, \fB\-\-delimiter\fR \fIdelimiter\fP \fB\-d\fR, \fB\-\-delimiter\fR \fIdelimiter\fP
Sets the string used to delimit each process ID in the output (by default a Sets the string used to delimit each process ID in the output (by default a

12
pgrep.c
View File

@ -96,8 +96,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
fprintf(fp, _(" %s [options] <pattern>\n"), program_invocation_short_name); fprintf(fp, _(" %s [options] <pattern>\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, fp); fputs(USAGE_OPTIONS, fp);
if (i_am_pkill == 0) { if (i_am_pkill == 0) {
fputs(_(" -c, --count count of matching processes\n" fputs(_(" -d, --delimiter <string> specify output delimiter\n"
" -d, --delimiter <string> specify output delimiter\n"
" -l, --list-name list PID and process name\n" " -l, --list-name list PID and process name\n"
" -v, --inverse negates the matching\n"), fp); " -v, --inverse negates the matching\n"), fp);
} }
@ -105,7 +104,8 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
fputs(_(" -<sig>, --signal <sig> signal to send (either number or name)\n" fputs(_(" -<sig>, --signal <sig> signal to send (either number or name)\n"
" -e, --echo display what is killed\n"), fp); " -e, --echo display what is killed\n"), fp);
} }
fputs(_(" -f, --full use full process name to match\n" fputs(_(" -c, --count count of matching processes\n"
" -f, --full use full process name to match\n"
" -g, --pgroup <id,...> match listed process group IDs\n" " -g, --pgroup <id,...> match listed process group IDs\n"
" -G, --group <gid,...> match real group IDs\n" " -G, --group <gid,...> match real group IDs\n"
" -n, --newest select most recently started\n" " -n, --newest select most recently started\n"
@ -620,10 +620,10 @@ static void parse_opts (int argc, char **argv)
strcat (opts, "e"); strcat (opts, "e");
} else { } else {
/* These options are for pgrep only */ /* These options are for pgrep only */
strcat (opts, "clad:v"); strcat (opts, "lad:v");
} }
strcat (opts, "LF:fnoxP:g:s:u:U:G:t:?Vh"); strcat (opts, "LF:cfnoxP:g:s:u:U:G:t:?Vh");
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != -1) { while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != -1) {
switch (opt) { switch (opt) {
@ -810,6 +810,8 @@ int main (int argc, char **argv)
continue; continue;
xwarn(_("killing pid %ld failed"), procs[i].num); xwarn(_("killing pid %ld failed"), procs[i].num);
} }
if (opt_count)
fprintf(stdout, "%d\n", num);
} else { } else {
if (opt_count) { if (opt_count) {
fprintf(stdout, "%d\n", num); fprintf(stdout, "%d\n", num);