maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-popen.h
blob5928d6b2ae98847113224bf415fc3439ef3b999e
1 /* Test of opening a subcommand stream.
2 Copyright (C) 2009-2024 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. */
21 /* Helpers. */
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
27 #include "macros.h"
29 int
30 main (int argc, char **argv)
32 size_t len;
33 char *cmd;
34 int i;
36 /* Children - use the pipe. */
37 if (argc > 1)
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. */
44 return 42;
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. */
51 ASSERT (cmd);
52 /* We count on argv[0] not containing any shell metacharacters. */
53 strcpy (cmd, argv[0]);
54 cmd[len] = ' ';
55 cmd[len + 2] = '\0';
56 for (i = 0; i < 2; i++)
58 FILE *child;
59 int status;
61 if (i)
63 ASSERT (fclose (stdin) == 0);
64 ASSERT (fclose (stdout) == 0);
67 cmd[len + 1] = 'r';
68 ASSERT (child = popen (cmd, "r"));
69 ASSERT (fgetc (child) == 'c');
70 status = pclose (child);
71 ASSERT (WIFEXITED (status));
72 ASSERT (WEXITSTATUS (status) == 42);
73 if (i)
75 ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
76 ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
79 cmd[len + 1] = 'w';
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);
85 if (i)
87 ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
88 ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
91 free (cmd);
92 return test_exit_status;