Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-popen.h
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> and a form of <stdio.h> first.  */
20
21 /* Helpers.  */
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 int
40 main (int argc, char **argv)
41 {
42   size_t len;
43   char *cmd;
44   int i;
45
46   /* Children - use the pipe.  */
47   if (argc > 1)
48     {
49       if (*argv[1] == 'r') /* Parent is reading, so we write.  */
50         ASSERT (putchar ('c') == 'c');
51       else /* Parent is writing, so we read.  */
52         ASSERT (getchar () == 'p');
53       /* Test that parent can read non-zero status.  */
54       return 42;
55     }
56
57   /* Parent - create read and write child, once under normal
58      circumstances and once with stdin and stdout closed.  */
59   len = strlen (argv[0]);
60   cmd = malloc (len + 3); /* Adding " r" and NUL.  */
61   ASSERT (cmd);
62   /* We count on argv[0] not containing any shell metacharacters.  */
63   strcpy (cmd, argv[0]);
64   cmd[len] = ' ';
65   cmd[len + 2] = '\0';
66   for (i = 0; i < 2; i++)
67     {
68       FILE *child;
69       int status;
70
71       if (i)
72         {
73           ASSERT (fclose (stdin) == 0);
74           ASSERT (fclose (stdout) == 0);
75         }
76
77       cmd[len + 1] = 'r';
78       ASSERT (child = popen (cmd, "r"));
79       ASSERT (fgetc (child) == 'c');
80       status = pclose (child);
81       ASSERT (WIFEXITED (status));
82       ASSERT (WEXITSTATUS (status) == 42);
83       if (i)
84         {
85           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
86           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
87         }
88
89       cmd[len + 1] = 'w';
90       ASSERT (child = popen (cmd, "w"));
91       ASSERT (fputc ('p', child) == 'p');
92       status = pclose (child);
93       ASSERT (WIFEXITED (status));
94       ASSERT (WEXITSTATUS (status) == 42);
95       if (i)
96         {
97           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
98           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
99         }
100     }
101   free (cmd);
102   return 0;
103 }