nstrftime, c-nstrftime tests: Avoid test failures on native Windows.
[gnulib.git] / tests / test-dirent-safer.c
blobf9fc10731df8fb7187dd9c72059450fe8377ef02
1 /* Test that directory streams leave standard fds alone.
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 #include "dirent--.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.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 (void)
41 int i;
42 DIR *dp;
43 /* The dirent-safer module works without the use of fdopendir (which
44 would also pull in fchdir and openat); but if those modules were
45 also used, we ensure that they are safe. In particular, the
46 gnulib version of fdopendir is unable to guarantee that
47 dirfd(fdopendir(fd))==fd, but we can at least guarantee that if
48 they are not equal, the fd returned by dirfd is safe. */
49 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
50 int dfd;
51 #endif
53 /* We close fd 2 later, so save it in fd 10. */
54 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
55 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
56 return 2;
58 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
59 dfd = open (".", O_RDONLY);
60 ASSERT (STDERR_FILENO < dfd);
61 #endif
63 /* Four iterations, with progressively more standard descriptors
64 closed. */
65 for (i = -1; i <= STDERR_FILENO; i++)
67 if (0 <= i)
68 ASSERT (close (i) == 0);
69 dp = opendir (".");
70 ASSERT (dp);
71 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp));
72 ASSERT (closedir (dp) == 0);
74 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
76 int fd = fcntl (dfd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
77 ASSERT (STDERR_FILENO < fd);
78 dp = fdopendir (fd);
79 ASSERT (dp);
80 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp));
81 ASSERT (closedir (dp) == 0);
82 errno = 0;
83 ASSERT (close (fd) == -1);
84 ASSERT (errno == EBADF);
86 #endif
89 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR
90 ASSERT (close (dfd) == 0);
91 #endif
93 return test_exit_status;