bug-gnulib
[Top][All Lists]
Advanced

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

Re: popen bugs, popen_safer


From: Eric Blake
Subject: Re: popen bugs, popen_safer
Date: Fri, 21 Aug 2009 07:38:07 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Bruno Haible on 8/20/2009 2:14 AM:
> This is becoming confusing: Why should the 'fopen' test suddenly test 
> fopen_safer,
> not fopen??
> 
> I would find it better to introduce a new test module fopen-safer-tests, that 
> shares
> the bulk of the code with fopen-tests. File structure:
>   test-fopen.h : all common code, starting with the definition of ASSERT,
>   test-fopen.c : just the includes and #include "test-fopen.h"
>   test-fopen-safer.c : just the includes, then #include "stdio--.h", then 
> #include "test-fopen.h"
> 
> See test-snprintf-posix.h, test-snprintf-posix.c, test-vsnprintf-posix.c for
> an example of this coding pattern.
> 
> Likewise for test-open.c and fcntl--.h.
> 
> Likewise for test-popen.c and stdio--.h.

Done.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqOoz4ACgkQ84KuGfSFAYAEvACeL0hJEWru3V2v44O2gjKdmoRa
kn0AoJq39SDjqOJmrtWCvvxIRkyPVKUR
=xnLZ
-----END PGP SIGNATURE-----
From 41fc74b9ab67321453d103d056a6e8eb8897042a Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Thu, 20 Aug 2009 23:19:20 -0600
Subject: [PATCH 1/3] test-fopen-safer: split from test-fopen

* tests/test-fopen.c (main): Move...
* tests/test-fopen.h: ...into new file.
* tests/test-fopen-safer.c: New file.
* modules/fopen-tests (Files): Add test-fopen.h.
* modules/fopen-safer-tests: New file.
Suggested by Bruno Haible.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                 |   10 ++++++++++
 modules/fopen-safer-tests |   11 +++++++++++
 modules/fopen-tests       |    1 +
 tests/test-fopen-safer.c  |   23 +++++++++++++++++++++++
 tests/test-fopen.c        |   28 +---------------------------
 tests/test-fopen.h        |   44 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 90 insertions(+), 27 deletions(-)
 create mode 100644 modules/fopen-safer-tests
 create mode 100644 tests/test-fopen-safer.c
 create mode 100644 tests/test-fopen.h

diff --git a/ChangeLog b/ChangeLog
index 565d70a..1530f82 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2009-08-21  Eric Blake  <address@hidden>
+
+       test-fopen-safer: split from test-fopen
+       * tests/test-fopen.c (main): Move...
+       * tests/test-fopen.h: ...into new file.
+       * tests/test-fopen-safer.c: New file.
+       * modules/fopen-tests (Files): Add test-fopen.h.
+       * modules/fopen-safer-tests: New file.
+       Suggested by Bruno Haible.
+
 2009-08-21  Paolo Bonzini  <address@hidden>

        popen-safer: test O_CLOEXEC at run-time.
diff --git a/modules/fopen-safer-tests b/modules/fopen-safer-tests
new file mode 100644
index 0000000..21116e6
--- /dev/null
+++ b/modules/fopen-safer-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-fopen.h
+tests/test-fopen-safer.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-fopen-safer
+check_PROGRAMS += test-fopen-safer
diff --git a/modules/fopen-tests b/modules/fopen-tests
index cf87389..fb0ee1d 100644
--- a/modules/fopen-tests
+++ b/modules/fopen-tests
@@ -1,4 +1,5 @@
 Files:
+tests/test-fopen.h
 tests/test-fopen.c

 Depends-on:
diff --git a/tests/test-fopen-safer.c b/tests/test-fopen-safer.c
new file mode 100644
index 0000000..701af35
--- /dev/null
+++ b/tests/test-fopen-safer.c
@@ -0,0 +1,23 @@
+/* Test of opening a file stream.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <address@hidden>, 2007.  */
+
+#include <config.h>
+
+#include "stdio--.h"
+
+#include "test-fopen.h"
diff --git a/tests/test-fopen.c b/tests/test-fopen.c
index b9e3694..473d274 100644
--- a/tests/test-fopen.c
+++ b/tests/test-fopen.c
@@ -19,31 +19,5 @@
 #include <config.h>

 #include <stdio.h>
-#include <stdlib.h>

-#if GNULIB_FOPEN_SAFER
-# include "stdio--.h"
-#endif
-
-#define ASSERT(expr) \
-  do                                                                        \
-    {                                                                       \
-      if (!(expr))                                                          \
-        {                                                                   \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                  \
-          abort ();                                                         \
-        }                                                                   \
-    }                                                                       \
-  while (0)
-
-int
-main ()
-{
-  ASSERT (fopen ("nonexist.ent/", "w") == NULL);
-  ASSERT (fopen ("/dev/null/", "r") == NULL);
-
-  ASSERT (fopen ("/dev/null", "r") != NULL);
-
-  return 0;
-}
+#include "test-fopen.h"
diff --git a/tests/test-fopen.h b/tests/test-fopen.h
new file mode 100644
index 0000000..b1dafbb
--- /dev/null
+++ b/tests/test-fopen.h
@@ -0,0 +1,44 @@
+/* Test of opening a file stream.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <address@hidden>, 2007.  */
+
+/* Include <config.h> and a form of <stdio.h> first.  */
+
+#include <stdlib.h>
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main ()
+{
+  ASSERT (fopen ("nonexist.ent/", "w") == NULL);
+  ASSERT (fopen ("/dev/null/", "r") == NULL);
+
+  ASSERT (fopen ("/dev/null", "r") != NULL);
+
+  return 0;
+}
-- 
1.6.3.3.334.g916e1


From 45b8409ffe3828d32d20281f1038816573e753df Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Fri, 21 Aug 2009 07:18:10 -0600
Subject: [PATCH 2/3] test-fcntl-safer: split from test-open

* tests/test-open.c (main): Move...
* tests/test-open.h: ...into new file.
* tests/test-fcntl-safer.c: New file.
* modules/open-tests (Files): Add test-open.h.
* modules/fcntl-safer-tests: New file.
Suggested by Bruno Haible.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                 |    8 ++++++++
 modules/fcntl-safer-tests |   11 +++++++++++
 modules/open-tests        |    1 +
 tests/test-fcntl-safer.c  |   23 +++++++++++++++++++++++
 tests/test-open.c         |   30 +-----------------------------
 tests/test-open.h         |   45 +++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 29 deletions(-)
 create mode 100644 modules/fcntl-safer-tests
 create mode 100644 tests/test-fcntl-safer.c
 create mode 100644 tests/test-open.h

diff --git a/ChangeLog b/ChangeLog
index 1530f82..7b9bb01 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2009-08-21  Eric Blake  <address@hidden>

+       test-fcntl-safer: split from test-open
+       * tests/test-open.c (main): Move...
+       * tests/test-open.h: ...into new file.
+       * tests/test-fcntl-safer.c: New file.
+       * modules/open-tests (Files): Add test-open.h.
+       * modules/fcntl-safer-tests: New file.
+       Suggested by Bruno Haible.
+
        test-fopen-safer: split from test-fopen
        * tests/test-fopen.c (main): Move...
        * tests/test-fopen.h: ...into new file.
diff --git a/modules/fcntl-safer-tests b/modules/fcntl-safer-tests
new file mode 100644
index 0000000..6229142
--- /dev/null
+++ b/modules/fcntl-safer-tests
@@ -0,0 +1,11 @@
+Files:
+tests/test-open.h
+tests/test-fcntl-safer.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-fcntl-safer
+check_PROGRAMS += test-fcntl-safer
diff --git a/modules/open-tests b/modules/open-tests
index 43366cf..42aa93c 100644
--- a/modules/open-tests
+++ b/modules/open-tests
@@ -1,4 +1,5 @@
 Files:
+tests/test-open.h
 tests/test-open.c

 Depends-on:
diff --git a/tests/test-fcntl-safer.c b/tests/test-fcntl-safer.c
new file mode 100644
index 0000000..3b3ff75
--- /dev/null
+++ b/tests/test-fcntl-safer.c
@@ -0,0 +1,23 @@
+/* Test of opening a file descriptor.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <address@hidden>, 2007.  */
+
+#include <config.h>
+
+#include "fcntl--.h"
+
+#include "test-open.h"
diff --git a/tests/test-open.c b/tests/test-open.c
index c9f3641..df7e36f 100644
--- a/tests/test-open.c
+++ b/tests/test-open.c
@@ -20,32 +20,4 @@

 #include <fcntl.h>

-#include <stdio.h>
-#include <stdlib.h>
-
-#if GNULIB_FCNTL_SAFER
-# include "fcntl--.h"
-#endif
-
-#define ASSERT(expr) \
-  do                                                                        \
-    {                                                                       \
-      if (!(expr))                                                          \
-        {                                                                   \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                  \
-          abort ();                                                         \
-        }                                                                   \
-    }                                                                       \
-  while (0)
-
-int
-main ()
-{
-  ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) < 0);
-  ASSERT (open ("/dev/null/", O_RDONLY) < 0);
-
-  ASSERT (open ("/dev/null", O_RDONLY) >= 0);
-
-  return 0;
-}
+#include "test-open.h"
diff --git a/tests/test-open.h b/tests/test-open.h
new file mode 100644
index 0000000..466cab3
--- /dev/null
+++ b/tests/test-open.h
@@ -0,0 +1,45 @@
+/* Test of opening a file descriptor.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <address@hidden>, 2007.  */
+
+/* Include <config.h> and a form of <fcntl.h> first.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main ()
+{
+  ASSERT (open ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) < 0);
+  ASSERT (open ("/dev/null/", O_RDONLY) < 0);
+
+  ASSERT (open ("/dev/null", O_RDONLY) >= 0);
+
+  return 0;
+}
-- 
1.6.3.3.334.g916e1


From 9076085f888c8abc5c80ea344be0ad504a26cc0f Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Fri, 21 Aug 2009 07:22:12 -0600
Subject: [PATCH 3/3] test-popen-safer: split from test-popen

* tests/test-popen.c (main): Move...
* tests/test-popen.h: ...into new file.
* tests/test-popen-safer2.c: New file.
* modules/popen-tests (Files): Add test-popen.h.
* modules/popen-safer-tests (Files): Add test-popen-safer2.c.
Suggested by Bruno Haible.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                 |    8 ++++
 modules/popen-safer-tests |    6 ++-
 modules/popen-tests       |    1 +
 tests/test-popen-safer2.c |   24 ++++++++++
 tests/test-popen.c        |   88 +--------------------------------------
 tests/test-popen.h        |  103 +++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 141 insertions(+), 89 deletions(-)
 create mode 100644 tests/test-popen-safer2.c
 create mode 100644 tests/test-popen.h

diff --git a/ChangeLog b/ChangeLog
index 7b9bb01..410e38c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2009-08-21  Eric Blake  <address@hidden>

+       test-popen-safer: split from test-popen
+       * tests/test-popen.c (main): Move...
+       * tests/test-popen.h: ...into new file.
+       * tests/test-popen-safer2.c: New file.
+       * modules/popen-tests (Files): Add test-popen.h.
+       * modules/popen-safer-tests (Files): Add test-popen-safer2.c.
+       Suggested by Bruno Haible.
+
        test-fcntl-safer: split from test-open
        * tests/test-open.c (main): Move...
        * tests/test-open.h: ...into new file.
diff --git a/modules/popen-safer-tests b/modules/popen-safer-tests
index 3dd67f2..0e1b93a 100644
--- a/modules/popen-safer-tests
+++ b/modules/popen-safer-tests
@@ -1,5 +1,7 @@
 Files:
+tests/test-popen.h
 tests/test-popen-safer.c
+tests/test-popen-safer2.c

 Depends-on:
 dup2
@@ -8,5 +10,5 @@ sys_wait
 configure.ac:

 Makefile.am:
-TESTS += test-popen-safer
-check_PROGRAMS += test-popen-safer
+TESTS += test-popen-safer test-popen-safer2
+check_PROGRAMS += test-popen-safer test-popen-safer2
diff --git a/modules/popen-tests b/modules/popen-tests
index ee7760e..9d22fca 100644
--- a/modules/popen-tests
+++ b/modules/popen-tests
@@ -1,4 +1,5 @@
 Files:
+tests/test-popen.h
 tests/test-popen.c

 Depends-on:
diff --git a/tests/test-popen-safer2.c b/tests/test-popen-safer2.c
new file mode 100644
index 0000000..4df1e36
--- /dev/null
+++ b/tests/test-popen-safer2.c
@@ -0,0 +1,24 @@
+/* Test of opening a subcommand stream.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <address@hidden>, 2009.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "stdio--.h"
+
+#include "test-popen.h"
diff --git a/tests/test-popen.c b/tests/test-popen.c
index 4e43bd7..342e363 100644
--- a/tests/test-popen.c
+++ b/tests/test-popen.c
@@ -21,90 +21,4 @@
 /* Specification.  */
 #include <stdio.h>

-/* Helpers.  */
-#include <stdlib.h>
-#include <string.h>
-#include <sys/wait.h>
-#include <unistd.h>
-
-#if GNULIB_POPEN_SAFER
-# include "stdio--.h"
-#endif
-
-#define ASSERT(expr) \
-  do                                                                        \
-    {                                                                       \
-      if (!(expr))                                                          \
-        {                                                                   \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                  \
-          abort ();                                                         \
-        }                                                                   \
-    }                                                                       \
-  while (0)
-
-int
-main (int argc, char **argv)
-{
-  size_t len;
-  char *cmd;
-  int i;
-
-  /* Children - use the pipe.  */
-  if (argc > 1)
-    {
-      if (*argv[1] == 'r') /* Parent is reading, so we write.  */
-       ASSERT (putchar ('c') == 'c');
-      else /* Parent is writing, so we read.  */
-       ASSERT (getchar () == 'p');
-      /* Test that parent can read non-zero status.  */
-      return 42;
-    }
-
-  /* Parent - create read and write child, once under normal
-     circumstances and once with stdin and stdout closed.  */
-  len = strlen (argv[0]);
-  cmd = malloc (len + 3); /* Adding " r" and NUL.  */
-  ASSERT (cmd);
-  /* We count on argv[0] not containing any shell metacharacters.  */
-  strcpy (cmd, argv[0]);
-  cmd[len] = ' ';
-  cmd[len + 2] = '\0';
-  for (i = 0; i < 2; i++)
-    {
-      FILE *child;
-      int status;
-
-      if (i)
-       {
-         ASSERT (fclose (stdin) == 0);
-         ASSERT (fclose (stdout) == 0);
-       }
-
-      cmd[len + 1] = 'r';
-      ASSERT (child = popen (cmd, "r"));
-      ASSERT (fgetc (child) == 'c');
-      status = pclose (child);
-      ASSERT (WIFEXITED (status));
-      ASSERT (WEXITSTATUS (status) == 42);
-      if (i)
-       {
-         ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
-         ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
-       }
-
-      cmd[len + 1] = 'w';
-      ASSERT (child = popen (cmd, "w"));
-      ASSERT (fputc ('p', child) == 'p');
-      status = pclose (child);
-      ASSERT (WIFEXITED (status));
-      ASSERT (WEXITSTATUS (status) == 42);
-      if (i)
-       {
-         ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
-         ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
-       }
-    }
-  free (cmd);
-  return 0;
-}
+#include "test-popen.h"
diff --git a/tests/test-popen.h b/tests/test-popen.h
new file mode 100644
index 0000000..9364c5a
--- /dev/null
+++ b/tests/test-popen.h
@@ -0,0 +1,103 @@
+/* Test of opening a subcommand stream.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Eric Blake <address@hidden>, 2009.  */
+
+/* Include <config.h> and a form of <stdio.h> first.  */
+
+/* Helpers.  */
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main (int argc, char **argv)
+{
+  size_t len;
+  char *cmd;
+  int i;
+
+  /* Children - use the pipe.  */
+  if (argc > 1)
+    {
+      if (*argv[1] == 'r') /* Parent is reading, so we write.  */
+       ASSERT (putchar ('c') == 'c');
+      else /* Parent is writing, so we read.  */
+       ASSERT (getchar () == 'p');
+      /* Test that parent can read non-zero status.  */
+      return 42;
+    }
+
+  /* Parent - create read and write child, once under normal
+     circumstances and once with stdin and stdout closed.  */
+  len = strlen (argv[0]);
+  cmd = malloc (len + 3); /* Adding " r" and NUL.  */
+  ASSERT (cmd);
+  /* We count on argv[0] not containing any shell metacharacters.  */
+  strcpy (cmd, argv[0]);
+  cmd[len] = ' ';
+  cmd[len + 2] = '\0';
+  for (i = 0; i < 2; i++)
+    {
+      FILE *child;
+      int status;
+
+      if (i)
+       {
+         ASSERT (fclose (stdin) == 0);
+         ASSERT (fclose (stdout) == 0);
+       }
+
+      cmd[len + 1] = 'r';
+      ASSERT (child = popen (cmd, "r"));
+      ASSERT (fgetc (child) == 'c');
+      status = pclose (child);
+      ASSERT (WIFEXITED (status));
+      ASSERT (WEXITSTATUS (status) == 42);
+      if (i)
+       {
+         ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
+         ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
+       }
+
+      cmd[len + 1] = 'w';
+      ASSERT (child = popen (cmd, "w"));
+      ASSERT (fputc ('p', child) == 'p');
+      status = pclose (child);
+      ASSERT (WIFEXITED (status));
+      ASSERT (WEXITSTATUS (status) == 42);
+      if (i)
+       {
+         ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
+         ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
+       }
+    }
+  free (cmd);
+  return 0;
+}
-- 
1.6.3.3.334.g916e1


reply via email to

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