> On Aug 25, 2017, at 11:27 PM, Scott Cheloha <[email protected]> wrote:
> 
> compile_mode() currently just reports the value returned by
> pclose(3).  This is incorrect because pclose gives you
> whatever wait4(2) returned, which needs to be examined
> with the various W* macros in <sys/wait.h> to derive a proper
> exit status.
> 
> This patch checks how the popen'd process exited and chooses
> an exit status as the shell would.

bump and rebase

--
Scott Cheloha

Index: usr.bin/mg/grep.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/grep.c,v
retrieving revision 1.45
diff -u -p -r1.45 grep.c
--- usr.bin/mg/grep.c   12 Oct 2017 14:12:00 -0000      1.45
+++ usr.bin/mg/grep.c   24 Oct 2017 02:59:15 -0000
@@ -2,8 +2,10 @@
 
 /* This file is in the public domain */
 
-#include <sys/queue.h>
 #include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/wait.h>
+
 #include <ctype.h>
 #include <libgen.h>
 #include <limits.h>
@@ -180,7 +182,7 @@ compile_mode(const char *name, const cha
        char    *buf;
        size_t   sz;
        ssize_t  len;
-       int      ret, n;
+       int      n, ret, status;
        char     cwd[NFILEN], qcmd[NFILEN];
        char     timestr[NTIME];
        time_t   t;
@@ -226,9 +228,10 @@ compile_mode(const char *name, const cha
        t = time(NULL);
        strftime(timestr, sizeof(timestr), "%a %b %e %T %Y", localtime(&t));
        addline(bp, "");
-       if (ret != 0)
-               addlinef(bp, "Command exited abnormally with code %d"
-                   " at %s", ret, timestr);
+       status = WIFEXITED(ret) ? WEXITSTATUS(ret) : 128 + WTERMSIG(ret);
+       if (status != 0)
+               addlinef(bp, "Command exited abnormally with status %d"
+                   " at %s", status, timestr);
        else
                addlinef(bp, "Command finished at %s", timestr);
 

Reply via email to