1 /* provide a replacement openat function
2 Copyright (C) 2004, 2005 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* written by Jim Meyering */
32 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
36 #define _(msgid) gettext (msgid)
38 /* Replacement for Solaris' openat function.
39 <http://www.google.com/search?q=openat+site:docs.sun.com>
40 Simulate it by doing save_cwd/fchdir/open/restore_cwd.
41 If either the save_cwd or the restore_cwd fails (relatively unlikely,
42 and usually indicative of a problem that deserves close attention),
43 then give a diagnostic and exit nonzero.
44 Otherwise, upon failure, set errno and return -1, as openat does.
45 Upon successful completion, return a file descriptor. */
47 rpl_openat (int fd, char const *file, int flags, ...)
49 struct saved_cwd saved_cwd;
57 va_start (arg, flags);
59 /* Assume that mode_t is passed compatibly with mode_t's type
60 after argument promotion. */
61 mode = va_arg (arg, mode_t);
66 if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
67 return open (file, flags, mode);
69 if (save_cwd (&saved_cwd) != 0)
70 openat_save_fail (errno);
75 free_cwd (&saved_cwd);
80 new_fd = open (file, flags, mode);
83 if (restore_cwd (&saved_cwd) != 0)
84 openat_restore_fail (errno);
86 free_cwd (&saved_cwd);
94 /* Replacement for Solaris' function by the same name.
95 <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
96 Simulate it by doing save_cwd/fchdir/opendir(".")/restore_cwd.
97 If either the save_cwd or the restore_cwd fails (relatively unlikely,
98 and usually indicative of a problem that deserves close attention),
99 then give a diagnostic and exit nonzero.
100 Otherwise, this function works just like Solaris' fdopendir.
103 Unlike the other fd-related functions here, this one
104 effectively consumes its FD parameter. The caller should not
105 close or otherwise manipulate FD if this function returns successfully. */
109 struct saved_cwd saved_cwd;
113 if (save_cwd (&saved_cwd) != 0)
114 openat_save_fail (errno);
116 if (fchdir (fd) != 0)
119 free_cwd (&saved_cwd);
127 if (restore_cwd (&saved_cwd) != 0)
128 openat_restore_fail (errno);
130 free_cwd (&saved_cwd);
140 /* Replacement for Solaris' function by the same name.
141 <http://www.google.com/search?q=fstatat+site:docs.sun.com>
142 Simulate it by doing save_cwd/fchdir/(stat|lstat)/restore_cwd.
143 If either the save_cwd or the restore_cwd fails (relatively unlikely,
144 and usually indicative of a problem that deserves close attention),
145 then give a diagnostic and exit nonzero.
146 Otherwise, this function works just like Solaris' fstatat. */
148 fstatat (int fd, char const *file, struct stat *st, int flag)
150 struct saved_cwd saved_cwd;
155 return (flag == AT_SYMLINK_NOFOLLOW
159 if (save_cwd (&saved_cwd) != 0)
160 openat_save_fail (errno);
162 if (fchdir (fd) != 0)
165 free_cwd (&saved_cwd);
170 err = (flag == AT_SYMLINK_NOFOLLOW
175 if (restore_cwd (&saved_cwd) != 0)
176 openat_restore_fail (errno);
178 free_cwd (&saved_cwd);
184 /* Replacement for Solaris' function by the same name.
185 <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
186 Simulate it by doing save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
187 If either the save_cwd or the restore_cwd fails (relatively unlikely,
188 and usually indicative of a problem that deserves close attention),
189 then give a diagnostic and exit nonzero.
190 Otherwise, this function works just like Solaris' unlinkat. */
192 unlinkat (int fd, char const *file, int flag)
194 struct saved_cwd saved_cwd;
199 return (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file));
201 if (save_cwd (&saved_cwd) != 0)
202 openat_save_fail (errno);
204 if (fchdir (fd) != 0)
207 free_cwd (&saved_cwd);
212 err = (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file));
215 if (restore_cwd (&saved_cwd) != 0)
216 openat_restore_fail (errno);
218 free_cwd (&saved_cwd);