1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide a replacement fdopendir function
4 Copyright (C) 2004-2010 Free Software Foundation, Inc.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* written by Jim Meyering */
31 # include "openat-priv.h"
32 # include "save-cwd.h"
34 # if GNULIB_DIRENT_SAFER
35 # include "dirent--.h"
38 /* Replacement for Solaris' function by the same name.
39 <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
40 First, try to simulate it via opendir ("/proc/self/fd/FD"). Failing
41 that, simulate it by using fchdir metadata, or by doing
42 save_cwd/fchdir/opendir(".")/restore_cwd.
43 If either the save_cwd or the restore_cwd fails (relatively unlikely),
44 then give a diagnostic and exit nonzero.
45 Otherwise, this function works just like Solaris' fdopendir.
48 Unlike other fd-related functions, this one effectively consumes
49 its FD parameter. The caller should not close or otherwise
50 manipulate FD if this function returns successfully. Also, this
51 implementation does not guarantee that dirfd(fdopendir(n))==n;
52 the open directory stream may use a clone of FD, or have no
53 associated fd at all. */
60 char buf[OPENAT_BUFFER_SIZE];
61 char *proc_file = openat_proc_name (buf, fd, ".");
64 dir = opendir (proc_file);
70 saved_errno = EOPNOTSUPP;
73 /* If the syscall fails with an expected errno value, resort to
74 save_cwd/restore_cwd. */
75 if (! dir && EXPECTED_ERRNO (saved_errno))
78 const char *name = _gl_directory_name (fd);
82 # else /* !REPLACE_FCHDIR */
83 struct saved_cwd saved_cwd;
84 if (save_cwd (&saved_cwd) != 0)
85 openat_save_fail (errno);
97 if (restore_cwd (&saved_cwd) != 0)
98 openat_restore_fail (errno);
101 free_cwd (&saved_cwd);
102 # endif /* !REPLACE_FCHDIR */
107 if (proc_file != buf)
113 #else /* HAVE_FDOPENDIR */
116 # include <sys/stat.h>
120 /* Like fdopendir, but work around GNU/Hurd bug by validating FD. */
123 rpl_fdopendir (int fd)
128 if (!S_ISDIR (st.st_mode))
133 return fdopendir (fd);
136 #endif /* HAVE_FDOPENDIR */