1 /* Test changing to a directory named by a file descriptor.
2 Copyright (C) 2009, 2010 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
23 #include "signature.h"
24 SIGNATURE_CHECK (fchdir, int, (int));
37 char *cwd = getcwd (NULL, 0);
38 int fd = open (".", O_RDONLY);
44 /* Check for failure cases. */
46 int bad_fd = open ("/dev/null", O_RDONLY);
49 ASSERT (fchdir (bad_fd) == -1);
50 ASSERT (errno == ENOTDIR);
51 ASSERT (close (bad_fd) == 0);
53 ASSERT (fchdir (-1) == -1);
54 ASSERT (errno == EBADF);
57 /* Repeat test twice, once in '.' and once in '..'. */
58 for (i = 0; i < 2; i++)
60 ASSERT (chdir (".." + 1 - i) == 0);
61 ASSERT (fchdir (fd) == 0);
63 size_t len = strlen (cwd) + 1;
64 char *new_dir = malloc (len);
66 ASSERT (getcwd (new_dir, len) == new_dir);
67 ASSERT (strcmp (cwd, new_dir) == 0);
71 /* For second iteration, use a cloned fd, to ensure that dup
72 remembers whether an fd was associated with a directory. */
75 int new_fd = dup (fd);
77 ASSERT (close (fd) == 0);
78 ASSERT (dup2 (new_fd, fd) == fd);
79 ASSERT (close (new_fd) == 0);
80 ASSERT (dup_cloexec (fd) == new_fd);
81 ASSERT (dup2 (new_fd, fd) == fd);
82 ASSERT (close (new_fd) == 0);
83 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, new_fd) == new_fd);
84 ASSERT (close (fd) == 0);
85 ASSERT (fcntl (new_fd, F_DUPFD, fd) == fd);
86 ASSERT (close (new_fd) == 0);
88 ASSERT (dup3 (fd, new_fd, 0) == new_fd);
89 ASSERT (dup3 (new_fd, fd, 0) == fd);
90 ASSERT (close (new_fd) == 0);