| 1 | /* Test opening a directory stream from a file descriptor.
|
|---|
| 2 | Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
|---|
| 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 "signature.h"
|
|---|
| 24 | SIGNATURE_CHECK (fdopendir, DIR *, (int));
|
|---|
| 25 |
|
|---|
| 26 | #include <errno.h>
|
|---|
| 27 | #include <fcntl.h>
|
|---|
| 28 | #include <unistd.h>
|
|---|
| 29 |
|
|---|
| 30 | #include "progname.h"
|
|---|
| 31 | #include "macros.h"
|
|---|
| 32 |
|
|---|
| 33 | int
|
|---|
| 34 | main (int argc _GL_UNUSED, char *argv[])
|
|---|
| 35 | {
|
|---|
| 36 | DIR *d;
|
|---|
| 37 | int fd;
|
|---|
| 38 |
|
|---|
| 39 | set_program_name (argv[0]);
|
|---|
| 40 |
|
|---|
| 41 | /* A non-directory cannot be turned into a directory stream. */
|
|---|
| 42 | fd = open ("test-fdopendir.tmp", O_RDONLY | O_CREAT, 0600);
|
|---|
| 43 | ASSERT (0 <= fd);
|
|---|
| 44 | errno = 0;
|
|---|
| 45 | ASSERT (fdopendir (fd) == NULL);
|
|---|
| 46 | ASSERT (errno == ENOTDIR);
|
|---|
| 47 | ASSERT (close (fd) == 0);
|
|---|
| 48 | ASSERT (unlink ("test-fdopendir.tmp") == 0);
|
|---|
| 49 |
|
|---|
| 50 | /* A bad fd cannot be turned into a stream. */
|
|---|
| 51 | {
|
|---|
| 52 | errno = 0;
|
|---|
| 53 | ASSERT (fdopendir (-1) == NULL);
|
|---|
| 54 | ASSERT (errno == EBADF);
|
|---|
| 55 | }
|
|---|
| 56 | {
|
|---|
| 57 | errno = 0;
|
|---|
| 58 | ASSERT (fdopendir (99) == NULL);
|
|---|
| 59 | ASSERT (errno == EBADF);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /* This should work. */
|
|---|
| 63 | fd = open (".", O_RDONLY);
|
|---|
| 64 | ASSERT (0 <= fd);
|
|---|
| 65 | d = fdopendir (fd);
|
|---|
| 66 | /* We know that fd is now out of our reach, but it is not specified
|
|---|
| 67 | whether it is closed now or at the closedir. We also can't
|
|---|
| 68 | guarantee whether dirfd returns fd, some other descriptor, or
|
|---|
| 69 | -1. */
|
|---|
| 70 | ASSERT (d);
|
|---|
| 71 | ASSERT (closedir (d) == 0);
|
|---|
| 72 | /* Now we can guarantee that fd must be closed. */
|
|---|
| 73 | errno = 0;
|
|---|
| 74 | ASSERT (dup2 (fd, fd) == -1);
|
|---|
| 75 | ASSERT (errno == EBADF);
|
|---|
| 76 |
|
|---|
| 77 | return 0;
|
|---|
| 78 | }
|
|---|