bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: execute: Add tests


From: Bruno Haible
Subject: Re: execute: Add tests
Date: Fri, 11 Dec 2020 11:48:44 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-193-generic; KDE/5.18.0; x86_64; ; )

On 2020-11-19 I did:
> ========================== tests/test-execute-child.c 
> ==========================
> /* In this file, we use only system functions, no overrides from gnulib.  */
> ...
> #undef fstat
> #undef raise
> #undef sprintf
> #undef stat
> ...

Oops, this does not work on AIX: On AIX 7.2 in 32-bit mode, it produces a
compilation error:

../../gltests/test-execute-child.c: In function 'main':
../../gltests/test-execute-child.c:131:21: error: storage size of 'st' isn't 
known
         struct stat st;
                     ^~
../../gltests/test-execute-child.c:132:18: warning: implicit declaration of 
function 'fstat'; did you mean 'fstatx'? [-Wimplicit-function-declaration]
         return !(fstat (STDOUT_FILENO, &st) >= 0 && !S_ISREG (st.st_mode));
                  ^~~~~
                  fstatx

This patch fixes it.


2020-12-11  Bruno Haible  <bruno@clisp.org>

        execute-tests: Fix compilation error on AIX in 32-bit mode.
        * tests/test-execute-child.c: In order to get the original definition of
        fstat, don't use '#undef fstat' and '#undef stat'. Instead, arrange to
        include the system's <sys/stat.h> and use it before including other
        header files.

diff --git a/tests/test-execute-child.c b/tests/test-execute-child.c
index 8197c30..16ccff7 100644
--- a/tests/test-execute-child.c
+++ b/tests/test-execute-child.c
@@ -14,8 +14,32 @@
    You should have received a copy of the GNU General Public License
    along with this program; if not, see <https://www.gnu.org/licenses/>.  */
 
+/* If the user's config.h happens to include <sys/stat.h>, let it include only
+   the system's <sys/stat.h> here.  */
+#define __need_system_sys_stat_h
 #include <config.h>
 
+/* Get the original definition of fstat.  It might be defined as a macro.
+   Also, 'stat' might be defined as a macro.  */
+#include <sys/types.h>
+#include <sys/stat.h>
+#undef __need_system_sys_stat_h
+
+/* Return non-zero if FD is opened to a device.  */
+static int
+is_device (int fd)
+{
+  struct stat st;
+  return
+#if defined _WIN32 && ! defined __CYGWIN__
+         _fstat (fd, &st) >= 0
+#else
+         fstat (fd, &st) >= 0
+#endif
+         && !S_ISREG (st.st_mode);
+}
+
+/* Now include the other header files.  */
 #include <fcntl.h>
 #include <signal.h>
 #include <stdint.h>
@@ -23,7 +47,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/stat.h>
 
 #if defined _WIN32 && ! defined __CYGWIN__
 /* Get declarations of the native Windows API functions.  */
@@ -40,13 +63,11 @@
 #undef fgetc
 #undef fprintf
 #undef fputs
-#undef fstat
 #undef getcwd
 #undef isatty
 #undef raise
 #undef read
 #undef sprintf
-#undef stat
 #undef strcmp
 #undef strlen
 #undef write
@@ -127,10 +148,7 @@ main (int argc, char *argv[])
       /* Check stdout is inherited, part 2 (device).  */
     case 10:
       /* Check null_stdout = true.  */
-      {
-        struct stat st;
-        return !(fstat (STDOUT_FILENO, &st) >= 0 && !S_ISREG (st.st_mode));
-      }
+      return !is_device (STDOUT_FILENO);
     case 11:
       /* Check stderr is inherited, part 1 (regular file).  */
       return !(fputs ("bar", stderr) != EOF && fflush (stderr) == 0);
@@ -138,10 +156,7 @@ main (int argc, char *argv[])
       /* Check stderr is inherited, part 2 (device).  */
     case 13:
       /* Check null_stderr = true.  */
-      {
-        struct stat st;
-        return !(fstat (STDERR_FILENO, &st) >= 0 && !S_ISREG (st.st_mode));
-      }
+      return !is_device (STDERR_FILENO);
     case 14:
     case 15:
       /* Check file descriptors >= 3 can be inherited.  */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]