id: fix "id <user>" case. Requires getgrouplist().
[oweals/busybox.git] / procps / pgrep.c
index 2bedabf72a409111887e61587bbdc012a02a343b..0e8e5294e8f3a9ada5aa5679b62c6cb4794b1787 100644 (file)
@@ -7,8 +7,6 @@
  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
 
-#include <getopt.h>
-
 #include "libbb.h"
 #include "xregex.h"
 
@@ -44,8 +42,8 @@ static void act(unsigned pid, char *cmd, int signo, unsigned opt)
                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;
@@ -72,8 +70,9 @@ int pgrep_main(int argc, char **argv)
                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++;
@@ -112,16 +111,23 @@ int pgrep_main(int argc, char **argv)
                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);