On Thu, 06 Dec 2012 10:55:50 +0100, Alexander Hall wrote:

> If, between the internal grep'ing and the printout, a process has
> disappeared, we currently get an empty line and pgrep will return
> nonzero.

Wouldn't it be better to just defer the printing of the delimiter
under we get the args?  Then you can just return success if errno
is ESRCH or -1 for other errors.

Perhaps something like this (untested).

 - todd

Index: pkill.c
===================================================================
RCS file: /home/cvs/openbsd/src/usr.bin/pkill/pkill.c,v
retrieving revision 1.30
diff -u -r1.30 pkill.c
--- pkill.c     21 Aug 2012 10:32:38 -0000      1.30
+++ pkill.c     8 Dec 2012 18:37:23 -0000
@@ -464,25 +464,26 @@
 grepact(struct kinfo_proc *kp, int printdelim)
 {
        char **argv;
+       const char *prefix = "";
 
        if (quiet)
                return (0);
        if (printdelim)
-               fputs(delim, stdout);
+               prefix = delim;
        if (longfmt && matchargs) {
                if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
-                       return (-1);
+                       return (errno == ESRCH ? 0 : -1);
 
-               printf("%d ", (int)kp->p_pid);
+               printf("%s%d ", prefix, (int)kp->p_pid);
                for (; *argv != NULL; argv++) {
                        printf("%s", *argv);
                        if (argv[1] != NULL)
                                putchar(' ');
                }
        } else if (longfmt)
-               printf("%d %s", (int)kp->p_pid, kp->p_comm);
+               printf("%s%d %s", prefix, (int)kp->p_pid, kp->p_comm);
        else
-               printf("%d", (int)kp->p_pid);
+               printf("%s%d", prefix, (int)kp->p_pid);
 
        return (0);
 }

Reply via email to