2 Copyright (C) 2006-2008 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/>. */
27 #include <sys/types.h>
31 #include "canonicalize.h"
34 /* This replacement assumes that a directory is not renamed while opened
35 through a file descriptor. */
37 /* Array of file descriptors opened. If it points to a directory, it stores
38 info about this directory; otherwise it stores an errno value of ENOTDIR. */
41 char *name; /* Absolute name of the directory, or NULL. */
42 int saved_errno; /* If name == NULL: The error code describing the failure
45 static dir_info_t *dirs;
46 static size_t dirs_allocated;
48 /* Try to ensure dirs has enough room for a slot at index fd. */
50 ensure_dirs_slot (size_t fd)
52 if (fd >= dirs_allocated)
58 new_allocated = 2 * dirs_allocated + 1;
59 if (new_allocated <= fd)
60 new_allocated = fd + 1;
63 ? (dir_info_t *) realloc (dirs, new_allocated * sizeof (dir_info_t))
64 : (dir_info_t *) malloc (new_allocated * sizeof (dir_info_t)));
67 for (i = dirs_allocated; i < new_allocated; i++)
69 new_dirs[i].name = NULL;
70 new_dirs[i].saved_errno = ENOTDIR;
73 dirs_allocated = new_allocated;
78 /* Hook into the gnulib replacements for open() and close() to keep track
79 of the open file descriptors. */
82 _gl_unregister_fd (int fd)
84 if (fd >= 0 && fd < dirs_allocated)
86 if (dirs[fd].name != NULL)
89 dirs[fd].saved_errno = ENOTDIR;
94 _gl_register_fd (int fd, const char *filename)
98 ensure_dirs_slot (fd);
99 if (fd < dirs_allocated
100 && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
102 dirs[fd].name = canonicalize_file_name (filename);
103 if (dirs[fd].name == NULL)
104 dirs[fd].saved_errno = errno;
108 /* Override open() and close(), to keep track of the open file descriptors. */
114 int retval = close (fd);
117 _gl_unregister_fd (fd);
122 rpl_open (const char *filename, int flags, ...)
133 va_start (arg, flags);
135 /* If mode_t is narrower than int, use the promoted type (int),
136 not mode_t. Use sizeof to guess whether mode_t is narrower;
137 we don't know of any practical counterexamples. */
138 mode = (sizeof (mode_t) < sizeof (int)
140 : va_arg (arg, mode_t));
144 #if defined GNULIB_OPEN && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
145 if (strcmp (filename, "/dev/null") == 0)
148 fd = open (filename, flags, mode);
150 _gl_register_fd (fd, filename);
154 /* Override opendir() and closedir(), to keep track of the open file
155 descriptors. Needed because there is a function dirfd(). */
158 rpl_closedir (DIR *dp)
162 int retval = closedir (dp);
165 _gl_unregister_fd (fd);
170 rpl_opendir (const char *filename)
175 dp = opendir (filename);
180 _gl_register_fd (fd, filename);
185 /* Override dup() and dup2(), to keep track of open file descriptors. */
191 int newfd = dup (oldfd);
193 if (oldfd >= 0 && newfd >= 0)
195 ensure_dirs_slot (newfd);
196 if (newfd < dirs_allocated)
198 if (oldfd < dirs_allocated)
200 if (dirs[oldfd].name != NULL)
202 dirs[newfd].name = strdup (dirs[oldfd].name);
203 if (dirs[newfd].name == NULL)
204 dirs[newfd].saved_errno = ENOMEM;
208 dirs[newfd].name = NULL;
209 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
214 dirs[newfd].name = NULL;
215 dirs[newfd].saved_errno = ENOMEM;
223 rpl_dup2 (int oldfd, int newfd)
226 int retval = dup2 (oldfd, newfd);
228 if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
230 ensure_dirs_slot (newfd);
231 if (newfd < dirs_allocated)
233 if (oldfd < dirs_allocated)
235 if (dirs[oldfd].name != NULL)
237 dirs[newfd].name = strdup (dirs[oldfd].name);
238 if (dirs[newfd].name == NULL)
239 dirs[newfd].saved_errno = ENOMEM;
243 dirs[newfd].name = NULL;
244 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
249 dirs[newfd].name = NULL;
250 dirs[newfd].saved_errno = ENOMEM;
257 /* Implement fchdir() in terms of chdir(). */
264 if (fd < dirs_allocated)
266 if (dirs[fd].name != NULL)
267 return chdir (dirs[fd].name);
270 errno = dirs[fd].saved_errno;