1 /* Test of opening a subcommand stream.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 /* Include <config.h> and a form of <stdio.h> first. */
30 main (int argc
, char **argv
)
36 /* Children - use the pipe. */
39 if (*argv
[1] == 'r') /* Parent is reading, so we write. */
40 ASSERT (putchar ('c') == 'c');
41 else /* Parent is writing, so we read. */
42 ASSERT (getchar () == 'p');
43 /* Test that parent can read non-zero status. */
47 /* Parent - create read and write child, once under normal
48 circumstances and once with stdin and stdout closed. */
49 len
= strlen (argv
[0]);
50 cmd
= malloc (len
+ 3); /* Adding " r" and NUL. */
52 /* We count on argv[0] not containing any shell metacharacters. */
53 strcpy (cmd
, argv
[0]);
56 for (i
= 0; i
< 2; i
++)
63 ASSERT (fclose (stdin
) == 0);
64 ASSERT (fclose (stdout
) == 0);
68 ASSERT (child
= popen (cmd
, "r"));
69 ASSERT (fgetc (child
) == 'c');
70 status
= pclose (child
);
71 ASSERT (WIFEXITED (status
));
72 ASSERT (WEXITSTATUS (status
) == 42);
75 ASSERT (dup2 (STDIN_FILENO
, STDIN_FILENO
) == -1);
76 ASSERT (dup2 (STDOUT_FILENO
, STDOUT_FILENO
) == -1);
80 ASSERT (child
= popen (cmd
, "w"));
81 ASSERT (fputc ('p', child
) == 'p');
82 status
= pclose (child
);
83 ASSERT (WIFEXITED (status
));
84 ASSERT (WEXITSTATUS (status
) == 42);
87 ASSERT (dup2 (STDIN_FILENO
, STDIN_FILENO
) == -1);
88 ASSERT (dup2 (STDOUT_FILENO
, STDOUT_FILENO
) == -1);