nstrftime, c-nstrftime tests: Avoid test failures on native Windows.
[gnulib.git] / tests / test-popen-safer.c
blob835786a74db78b538981a1551368131a2ec973cb
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>
21 /* Specification. */
22 #include "stdio--.h"
24 /* Helpers. */
25 #include <sys/wait.h>
26 #include <unistd.h>
28 /* This test intentionally closes stderr. So, we arrange to have fd 10
29 (outside the range of interesting fd's during the test) set up to
30 duplicate the original stderr. */
32 #define BACKUP_STDERR_FILENO 10
33 #define ASSERT_STREAM myerr
34 #include "macros.h"
36 static FILE *myerr;
38 int
39 main (int argc, char **argv)
41 FILE *fp;
42 int status;
44 /* We close fd 2 later, so save it in fd 10. */
45 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
46 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
47 return 2;
49 ASSERT (fp = popen ("exit 0", "r"));
50 ASSERT (STDERR_FILENO < fileno (fp));
51 status = pclose (fp);
52 ASSERT (WIFEXITED (status));
53 ASSERT (!WEXITSTATUS (status));
55 ASSERT (fclose (stdin) == 0);
56 ASSERT (fp = popen ("exit 0", "r"));
57 ASSERT (STDERR_FILENO < fileno (fp));
58 status = pclose (fp);
59 ASSERT (WIFEXITED (status));
60 ASSERT (!WEXITSTATUS (status));
62 ASSERT (fclose (stdout) == 0);
63 ASSERT (fp = popen ("exit 0", "r"));
64 ASSERT (STDERR_FILENO < fileno (fp));
65 status = pclose (fp);
66 ASSERT (WIFEXITED (status));
67 ASSERT (!WEXITSTATUS (status));
69 ASSERT (fclose (stderr) == 0);
70 ASSERT (fp = popen ("exit 0", "r"));
71 ASSERT (STDERR_FILENO < fileno (fp));
72 status = pclose (fp);
73 ASSERT (WIFEXITED (status));
74 ASSERT (!WEXITSTATUS (status));
76 return test_exit_status;