1a7c91847Schristos /* provide a replacement openat function
2a7c91847Schristos Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3a7c91847Schristos
4a7c91847Schristos This program is free software; you can redistribute it and/or modify
5a7c91847Schristos it under the terms of the GNU General Public License as published by
6a7c91847Schristos the Free Software Foundation; either version 2, or (at your option)
7a7c91847Schristos any later version.
8a7c91847Schristos
9a7c91847Schristos This program is distributed in the hope that it will be useful,
10a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
11a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a7c91847Schristos GNU General Public License for more details.
13a7c91847Schristos
14a7c91847Schristos You should have received a copy of the GNU General Public License
15a7c91847Schristos along with this program; if not, write to the Free Software Foundation,
16a7c91847Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17*5a6c14c8Schristos #include <sys/cdefs.h>
18*5a6c14c8Schristos __RCSID("$NetBSD: openat.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
19*5a6c14c8Schristos
20a7c91847Schristos
21a7c91847Schristos /* written by Jim Meyering */
22a7c91847Schristos
23a7c91847Schristos #ifdef HAVE_CONFIG_H
24a7c91847Schristos # include <config.h>
25a7c91847Schristos #endif
26a7c91847Schristos
27a7c91847Schristos #include "openat.h"
28a7c91847Schristos
29a7c91847Schristos #include <stdlib.h>
30a7c91847Schristos #include <stdarg.h>
31a7c91847Schristos #include <unistd.h>
32a7c91847Schristos #include <errno.h>
33a7c91847Schristos #include <fcntl.h>
34a7c91847Schristos
35a7c91847Schristos #include "error.h"
36a7c91847Schristos #include "exitfail.h"
37a7c91847Schristos #include "save-cwd.h"
38a7c91847Schristos
39a7c91847Schristos #include "gettext.h"
40a7c91847Schristos #define _(msgid) gettext (msgid)
41a7c91847Schristos
42a7c91847Schristos /* Replacement for Solaris' openat function.
43a7c91847Schristos <http://www.google.com/search?q=openat+site:docs.sun.com>
44a7c91847Schristos Simulate it by doing save_cwd/fchdir/open/restore_cwd.
45a7c91847Schristos If either the save_cwd or the restore_cwd fails (relatively unlikely,
46a7c91847Schristos and usually indicative of a problem that deserves close attention),
47a7c91847Schristos then give a diagnostic and exit nonzero.
48a7c91847Schristos Otherwise, upon failure, set errno and return -1, as openat does.
49a7c91847Schristos Upon successful completion, return a file descriptor. */
50a7c91847Schristos int
rpl_openat(int fd,char const * file,int flags,...)51a7c91847Schristos rpl_openat (int fd, char const *file, int flags, ...)
52a7c91847Schristos {
53a7c91847Schristos struct saved_cwd saved_cwd;
54a7c91847Schristos int saved_errno;
55a7c91847Schristos int new_fd;
56a7c91847Schristos mode_t mode = 0;
57a7c91847Schristos
58a7c91847Schristos if (flags & O_CREAT)
59a7c91847Schristos {
60a7c91847Schristos va_list arg;
61a7c91847Schristos va_start (arg, flags);
62a7c91847Schristos
63a7c91847Schristos /* Assume that mode_t is passed compatibly with mode_t's type
64a7c91847Schristos after argument promotion. */
65a7c91847Schristos mode = va_arg (arg, mode_t);
66a7c91847Schristos
67a7c91847Schristos va_end (arg);
68a7c91847Schristos }
69a7c91847Schristos
70a7c91847Schristos if (fd == AT_FDCWD || *file == '/')
71a7c91847Schristos return open (file, flags, mode);
72a7c91847Schristos
73a7c91847Schristos if (save_cwd (&saved_cwd) != 0)
74a7c91847Schristos error (exit_failure, errno,
75a7c91847Schristos _("openat: unable to record current working directory"));
76a7c91847Schristos
77a7c91847Schristos if (fchdir (fd) != 0)
78a7c91847Schristos {
79a7c91847Schristos saved_errno = errno;
80a7c91847Schristos free_cwd (&saved_cwd);
81a7c91847Schristos errno = saved_errno;
82a7c91847Schristos return -1;
83a7c91847Schristos }
84a7c91847Schristos
85a7c91847Schristos new_fd = open (file, flags, mode);
86a7c91847Schristos saved_errno = errno;
87a7c91847Schristos
88a7c91847Schristos if (restore_cwd (&saved_cwd) != 0)
89a7c91847Schristos error (exit_failure, errno,
90a7c91847Schristos _("openat: unable to restore working directory"));
91a7c91847Schristos
92a7c91847Schristos free_cwd (&saved_cwd);
93a7c91847Schristos
94a7c91847Schristos errno = saved_errno;
95a7c91847Schristos return new_fd;
96a7c91847Schristos }
97a7c91847Schristos
98a7c91847Schristos /* Replacement for Solaris' function by the same name.
99a7c91847Schristos <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
100a7c91847Schristos Simulate it by doing save_cwd/fchdir/opendir(".")/restore_cwd.
101a7c91847Schristos If either the save_cwd or the restore_cwd fails (relatively unlikely,
102a7c91847Schristos and usually indicative of a problem that deserves close attention),
103a7c91847Schristos then give a diagnostic and exit nonzero.
104a7c91847Schristos Otherwise, this function works just like Solaris' fdopendir. */
105a7c91847Schristos DIR *
fdopendir(int fd)106a7c91847Schristos fdopendir (int fd)
107a7c91847Schristos {
108a7c91847Schristos struct saved_cwd saved_cwd;
109a7c91847Schristos int saved_errno;
110a7c91847Schristos DIR *dir;
111a7c91847Schristos
112a7c91847Schristos if (fd == AT_FDCWD)
113a7c91847Schristos return opendir (".");
114a7c91847Schristos
115a7c91847Schristos if (save_cwd (&saved_cwd) != 0)
116a7c91847Schristos error (exit_failure, errno,
117a7c91847Schristos _("fdopendir: unable to record current working directory"));
118a7c91847Schristos
119a7c91847Schristos if (fchdir (fd) != 0)
120a7c91847Schristos {
121a7c91847Schristos saved_errno = errno;
122a7c91847Schristos free_cwd (&saved_cwd);
123a7c91847Schristos errno = saved_errno;
124a7c91847Schristos return NULL;
125a7c91847Schristos }
126a7c91847Schristos
127a7c91847Schristos dir = opendir (".");
128a7c91847Schristos saved_errno = errno;
129a7c91847Schristos
130a7c91847Schristos if (restore_cwd (&saved_cwd) != 0)
131a7c91847Schristos error (exit_failure, errno,
132a7c91847Schristos _("fdopendir: unable to restore working directory"));
133a7c91847Schristos
134a7c91847Schristos free_cwd (&saved_cwd);
135a7c91847Schristos
136a7c91847Schristos errno = saved_errno;
137a7c91847Schristos return dir;
138a7c91847Schristos }
139a7c91847Schristos
140a7c91847Schristos /* Replacement for Solaris' function by the same name.
141a7c91847Schristos <http://www.google.com/search?q=fstatat+site:docs.sun.com>
142a7c91847Schristos Simulate it by doing save_cwd/fchdir/(stat|lstat)/restore_cwd.
143a7c91847Schristos If either the save_cwd or the restore_cwd fails (relatively unlikely,
144a7c91847Schristos and usually indicative of a problem that deserves close attention),
145a7c91847Schristos then give a diagnostic and exit nonzero.
146a7c91847Schristos Otherwise, this function works just like Solaris' fstatat. */
147a7c91847Schristos int
fstatat(int fd,char const * file,struct stat * st,int flag)148a7c91847Schristos fstatat (int fd, char const *file, struct stat *st, int flag)
149a7c91847Schristos {
150a7c91847Schristos struct saved_cwd saved_cwd;
151a7c91847Schristos int saved_errno;
152a7c91847Schristos int err;
153a7c91847Schristos
154a7c91847Schristos if (fd == AT_FDCWD)
155a7c91847Schristos return (flag == AT_SYMLINK_NOFOLLOW
156a7c91847Schristos ? lstat (file, st)
157a7c91847Schristos : stat (file, st));
158a7c91847Schristos
159a7c91847Schristos if (save_cwd (&saved_cwd) != 0)
160a7c91847Schristos error (exit_failure, errno,
161a7c91847Schristos _("fstatat: unable to record current working directory"));
162a7c91847Schristos
163a7c91847Schristos if (fchdir (fd) != 0)
164a7c91847Schristos {
165a7c91847Schristos saved_errno = errno;
166a7c91847Schristos free_cwd (&saved_cwd);
167a7c91847Schristos errno = saved_errno;
168a7c91847Schristos return -1;
169a7c91847Schristos }
170a7c91847Schristos
171a7c91847Schristos err = (flag == AT_SYMLINK_NOFOLLOW
172a7c91847Schristos ? lstat (file, st)
173a7c91847Schristos : stat (file, st));
174a7c91847Schristos saved_errno = errno;
175a7c91847Schristos
176a7c91847Schristos if (restore_cwd (&saved_cwd) != 0)
177a7c91847Schristos error (exit_failure, errno,
178a7c91847Schristos _("fstatat: unable to restore working directory"));
179a7c91847Schristos
180a7c91847Schristos free_cwd (&saved_cwd);
181a7c91847Schristos
182a7c91847Schristos errno = saved_errno;
183a7c91847Schristos return err;
184a7c91847Schristos }
185