popen: fix cygwin 1.5 bug when stdin closed
[pspp] / tests / test-popen.c
1 /* Test of opening a subcommand stream.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdio.h>
23
24 /* Helpers.  */
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main (int argc, char **argv)
44 {
45   size_t len;
46   char *cmd;
47   int i;
48
49   /* Children - use the pipe.  */
50   if (argc > 1)
51     {
52       if (*argv[1] == 'r') /* Parent is reading, so we write.  */
53         ASSERT (putchar ('c') == 'c');
54       else /* Parent is writing, so we read.  */
55         ASSERT (getchar () == 'p');
56       /* Test that parent can read non-zero status.  */
57       return 42;
58     }
59
60   /* Parent - create read and write child, once under normal
61      circumstances and once with stdin and stdout closed.  */
62   len = strlen (argv[0]);
63   cmd = malloc (len + 3); /* Adding " r" and NUL.  */
64   ASSERT (cmd);
65   /* We count on argv[0] not containing any shell metacharacters.  */
66   strcpy (cmd, argv[0]);
67   cmd[len] = ' ';
68   cmd[len + 2] = '\0';
69   for (i = 0; i < 2; i++)
70     {
71       FILE *child;
72       int status;
73
74       if (i)
75         {
76           ASSERT (fclose (stdin) == 0);
77           ASSERT (fclose (stdout) == 0);
78         }
79
80       cmd[len + 1] = 'r';
81       ASSERT (child = popen (cmd, "r"));
82       ASSERT (fgetc (child) == 'c');
83       status = pclose (child);
84       ASSERT (WIFEXITED (status));
85       ASSERT (WEXITSTATUS (status) == 42);
86       if (i)
87         {
88           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
89           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
90         }
91
92       cmd[len + 1] = 'w';
93       ASSERT (child = popen (cmd, "w"));
94       ASSERT (fputc ('p', child) == 'p');
95       status = pclose (child);
96       ASSERT (WIFEXITED (status));
97       ASSERT (WEXITSTATUS (status) == 42);
98       if (i)
99         {
100           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
101           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
102         }
103     }
104   free (cmd);
105   return 0;
106 }