1 /* provide a replacement openat function
2 Copyright (C) 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* written by Jim Meyering */
35 #define _(msgid) gettext (msgid)
37 /* Replacement for Solaris' openat function.
38 <http://www.google.com/search?q=openat+site:docs.sun.com>
39 Simulate it by doing save_cwd/fchdir/open/restore_cwd.
40 If either the save_cwd or the restore_cwd fails (relatively unlikely,
41 and usually indicative of a problem that deserves close attention),
42 then give a diagnostic and exit nonzero.
43 Otherwise, upon failure, set errno and return -1, as openat does.
44 Upon successful completion, return a file descriptor. */
46 rpl_openat (int fd, char const *filename, int flags, ...)
48 struct saved_cwd saved_cwd;
56 va_start (arg, flags);
58 /* Assume that mode_t is passed compatibly with mode_t's type
59 after argument promotion. */
60 mode = va_arg (arg, mode_t);
65 if (fd == AT_FDCWD || *filename == '/')
66 return open (filename, flags, mode);
68 if (save_cwd (&saved_cwd) != 0)
69 error (exit_failure, errno,
70 _("openat: unable to record current working directory"));
75 free_cwd (&saved_cwd);
80 new_fd = open (filename, flags, mode);
83 if (restore_cwd (&saved_cwd) != 0)
84 error (exit_failure, errno,
85 _("openat: unable to restore working directory"));
87 free_cwd (&saved_cwd);