fchdir: port to mingw
[gnulib.git] / tests / test-fchdir.c
1 /* Test changing to a directory named by a file descriptor.
2    Copyright (C) 2009 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 <unistd.h>
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main ()
43 {
44   char *cwd = getcwd (NULL, 0);
45   int fd = open (".", O_RDONLY);
46   int i;
47
48   ASSERT (cwd);
49   ASSERT (0 <= fd);
50
51   /* Check for failure cases.  */
52   {
53     int bad_fd = open ("/dev/null", O_RDONLY);
54     ASSERT (0 <= bad_fd);
55     errno = 0;
56     ASSERT (fchdir (bad_fd) == -1);
57     ASSERT (errno == ENOTDIR);
58     ASSERT (close (bad_fd) == 0);
59     errno = 0;
60     ASSERT (fchdir (-1) == -1);
61     ASSERT (errno == EBADF);
62   }
63
64   /* Repeat test twice, once in '.' and once in '..'.  */
65   for (i = 0; i < 2; i++)
66     {
67       ASSERT (chdir (".." + 1 - i) == 0);
68       ASSERT (fchdir (fd) == 0);
69       {
70         size_t len = strlen (cwd) + 1;
71         char *new_dir = malloc (len);
72         ASSERT (new_dir);
73         ASSERT (getcwd (new_dir, len) == new_dir);
74         ASSERT (strcmp (cwd, new_dir) == 0);
75         free (new_dir);
76       }
77
78       /* For second iteration, use a cloned fd, to ensure that dup
79          remembers whether an fd was associated with a directory.  */
80       if (!i)
81         {
82           int new_fd = dup (fd);
83           ASSERT (0 <= new_fd);
84           ASSERT (close (fd) == 0);
85           fd = new_fd;
86         }
87     }
88
89   free (cwd);
90   return 0;
91 }