* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
-#include <getopt.h>
-
#include "libbb.h"
#include "xregex.h"
kill(pid, signo);
}
-int pgrep_main(int argc, char **argv);
-int pgrep_main(int argc, char **argv)
+int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int pgrep_main(int argc UNUSED_PARAM, char **argv)
{
unsigned pid = getpid();
int signo = SIGTERM;
first_arg = argv[first_arg_idx];
if (!first_arg)
break;
+ /* not "-<small_letter>..."? */
if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 'z') {
- argv[first_arg_idx] = NULL;
+ argv[first_arg_idx] = NULL; /* terminate argv here */
break;
}
first_arg_idx++;
if (proc->pid == pid)
continue;
cmd = proc->argv0;
- if (!cmd)
+ if (!cmd) {
cmd = proc->comm;
+ } else {
+ int i = proc->argv_len;
+ while (i) {
+ if (!cmd[i]) cmd[i] = ' ';
+ i--;
+ }
+ }
/* NB: OPT_INVERT is always 0 or 1 */
if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
- && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == strlen(cmd)))) ^ OPT_INVERT
+ && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT
) {
matched_pid = proc->pid;
if (OPT_LAST) {
free(cmd_last);
- cmd_last = xstrdup(cmd_last);
+ cmd_last = xstrdup(cmd);
continue;
}
act(proc->pid, cmd, signo, opt);