pgrep: Enable case-insensitive process matching

FreeBSD has case-insensitive matching of processes in pgrep and
pkill, which can be super-useful. This patch uncomments and
documents the code needed to make this work.

Signed-off-by: Craig Small <csmall@enc.com.au>
This commit is contained in:
William Orr 2015-05-10 17:42:09 +10:00 committed by Craig Small
parent 1209e75903
commit 9742c74e7c
2 changed files with 11 additions and 6 deletions

View File

@ -76,6 +76,9 @@ own process group.
Only match processes whose real group ID is listed. Either the numerical or Only match processes whose real group ID is listed. Either the numerical or
symbolical value may be used. symbolical value may be used.
.TP .TP
\fB\-i\fR, \fB\-\-ignore\-case\fR
Match processes case-insensitively.
.TP
\fB\-l\fR, \fB\-\-list\-name\fR \fB\-l\fR, \fB\-\-list\-name\fR
List the process name as well as the process ID. List the process name as well as the process ID.
.RB ( pgrep .RB ( pgrep

14
pgrep.c
View File

@ -118,6 +118,7 @@ static int __attribute__ ((__noreturn__)) usage(int opt)
fputs(_(" -f, --full use full process name to match\n"), fp); fputs(_(" -f, --full use full process name to match\n"), fp);
fputs(_(" -g, --pgroup <PGID,...> match listed process group IDs\n"), fp); fputs(_(" -g, --pgroup <PGID,...> match listed process group IDs\n"), fp);
fputs(_(" -G, --group <GID,...> match real group IDs\n"), fp); fputs(_(" -G, --group <GID,...> match real group IDs\n"), fp);
fputs(_(" -i, --ignore-case match case insensitively\n"), fp);
fputs(_(" -n, --newest select most recently started\n"), fp); fputs(_(" -n, --newest select most recently started\n"), fp);
fputs(_(" -o, --oldest select least recently started\n"), fp); fputs(_(" -o, --oldest select least recently started\n"), fp);
fputs(_(" -P, --parent <PPID,...> match only child processes of the given parent\n"), fp); fputs(_(" -P, --parent <PPID,...> match only child processes of the given parent\n"), fp);
@ -694,6 +695,7 @@ static void parse_opts (int argc, char **argv)
{"full", no_argument, NULL, 'f'}, {"full", no_argument, NULL, 'f'},
{"pgroup", required_argument, NULL, 'g'}, {"pgroup", required_argument, NULL, 'g'},
{"group", required_argument, NULL, 'G'}, {"group", required_argument, NULL, 'G'},
{"ignore-case", no_argument, NULL, 'i'},
{"newest", no_argument, NULL, 'n'}, {"newest", no_argument, NULL, 'n'},
{"oldest", no_argument, NULL, 'o'}, {"oldest", no_argument, NULL, 'o'},
{"parent", required_argument, NULL, 'P'}, {"parent", required_argument, NULL, 'P'},
@ -727,7 +729,7 @@ static void parse_opts (int argc, char **argv)
strcat (opts, "lad:vw"); strcat (opts, "lad:vw");
} }
strcat (opts, "LF:cfnoxP:g:s:u:U:G:t:?Vh"); strcat (opts, "LF:cfinoxP: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) {
@ -798,11 +800,11 @@ static void parse_opts (int argc, char **argv)
usage ('?'); usage ('?');
++criteria_count; ++criteria_count;
break; break;
/* case 'i': / * FreeBSD: ignore case. OpenBSD: withdrawn. See -I. This sucks. * / case 'i': /* FreeBSD: ignore case. OpenBSD: withdrawn. See -I. This sucks. */
* if (opt_case) if (opt_case)
* usage (opt); usage (opt);
* opt_case = REG_ICASE; opt_case = REG_ICASE;
* break; */ break;
/* case 'j': / * FreeBSD: restricted to the given jail ID * / /* case 'j': / * FreeBSD: restricted to the given jail ID * /
* break; */ * break; */
case 'l': /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */ case 'l': /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */