Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-dirent-safer.c @ 17415:6550127da196
argp: typo fix
* lib/argp-help.c: Typo in comment.
author | Alexandre Duret-Lutz <adl@lrde.epita.fr> |
---|---|
date | Fri, 17 May 2013 19:01:14 +0200 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
11936 | 1 /* Test that directory streams leave standard fds alone. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2009-2013 Free Software Foundation, Inc. |
11936 | 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> | |
20 | |
21 #include "dirent--.h" | |
22 | |
23 #include <errno.h> | |
24 #include <fcntl.h> | |
25 #include <stdio.h> | |
26 #include <unistd.h> | |
27 | |
28 #include "unistd-safer.h" | |
29 | |
30 /* This test intentionally closes stderr. So, we arrange to have fd 10 | |
31 (outside the range of interesting fd's during the test) set up to | |
32 duplicate the original stderr. */ | |
33 | |
34 #define BACKUP_STDERR_FILENO 10 | |
12510
618282a72d9b
tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents:
12197
diff
changeset
|
35 #define ASSERT_STREAM myerr |
618282a72d9b
tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents:
12197
diff
changeset
|
36 #include "macros.h" |
11936 | 37 |
12510
618282a72d9b
tests: use macros.h in more places
Eric Blake <ebb9@byu.net>
parents:
12197
diff
changeset
|
38 static FILE *myerr; |
11936 | 39 |
40 int | |
12197
e45d9bb2233e
tests: avoid several compiler warnings
Eric Blake <ebb9@byu.net>
parents:
11936
diff
changeset
|
41 main (void) |
11936 | 42 { |
43 int i; | |
44 DIR *dp; | |
45 /* The dirent-safer module works without the use of fdopendir (which | |
46 would also pull in fchdir and openat); but if those modules were | |
47 also used, we ensure that they are safe. In particular, the | |
48 gnulib version of fdopendir is unable to guarantee that | |
49 dirfd(fdopendir(fd))==fd, but we can at least guarantee that if | |
50 they are not equal, the fd returned by dirfd is safe. */ | |
13073
03de1efbfdd3
Resolve conflict between the two kinds of module indicators.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
51 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR |
11936 | 52 int dfd; |
53 #endif | |
54 | |
55 /* We close fd 2 later, so save it in fd 10. */ | |
56 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO | |
57 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL) | |
58 return 2; | |
59 | |
13073
03de1efbfdd3
Resolve conflict between the two kinds of module indicators.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
60 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR |
11936 | 61 dfd = open (".", O_RDONLY); |
62 ASSERT (STDERR_FILENO < dfd); | |
63 #endif | |
64 | |
65 /* Four iterations, with progressively more standard descriptors | |
66 closed. */ | |
67 for (i = -1; i <= STDERR_FILENO; i++) | |
68 { | |
69 if (0 <= i) | |
70 ASSERT (close (i) == 0); | |
71 dp = opendir ("."); | |
72 ASSERT (dp); | |
73 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp)); | |
74 ASSERT (closedir (dp) == 0); | |
75 | |
13073
03de1efbfdd3
Resolve conflict between the two kinds of module indicators.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
76 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR |
11936 | 77 { |
78 int fd = dup_safer (dfd); | |
79 ASSERT (STDERR_FILENO < fd); | |
80 dp = fdopendir (fd); | |
81 ASSERT (dp); | |
82 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp)); | |
83 ASSERT (closedir (dp) == 0); | |
84 errno = 0; | |
85 ASSERT (close (fd) == -1); | |
86 ASSERT (errno == EBADF); | |
87 } | |
88 #endif | |
89 } | |
90 | |
13073
03de1efbfdd3
Resolve conflict between the two kinds of module indicators.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
91 #if HAVE_FDOPENDIR || GNULIB_TEST_FDOPENDIR |
11936 | 92 ASSERT (close (dfd) == 0); |
93 #endif | |
94 | |
95 return 0; | |
96 } |