Hi,

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.

--
Scott Cheloha

Index: usr.bin/mg/grep.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/grep.c,v
retrieving revision 1.44
diff -u -p -r1.44 grep.c
--- usr.bin/mg/grep.c   19 Mar 2015 21:48:05 -0000      1.44
+++ usr.bin/mg/grep.c   26 Aug 2017 04:06:35 -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>
@@ -179,7 +181,7 @@ compile_mode(const char *name, const cha
        FILE    *fpipe;
        char    *buf;
        size_t   len;
-       int      ret, n;
+       int      n, ret, status;
        char     cwd[NFILEN], qcmd[NFILEN];
        char     timestr[NTIME];
        time_t   t;
@@ -220,12 +222,13 @@ compile_mode(const char *name, const cha
                addline(bp, buf);
        }
        ret = pclose(fpipe);
+       status = WIFEXITED(ret) ? WEXITSTATUS(ret) : 128 + WTERMSIG(ret);
        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);
+       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