mountlist: Use Linux code on Android.
[gnulib.git] / lib / opendirat.c
blobad63131ea115d31ffbc68c3cf9ee9a94908ef500
1 /* Open a directory relative to another directory.
3 Copyright 2006-2019 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 Written by Jim Meyering and Paul Eggert. */
20 #include <config.h>
22 #include <opendirat.h>
24 #include <errno.h>
25 #include <fcntl--.h>
26 #include <unistd.h>
28 /* Relative to DIR_FD, open the directory DIR, passing EXTRA_FLAGS to
29 the underlying openat call. On success, store into *PNEW_FD the
30 underlying file descriptor of the newly opened directory and return
31 the directory stream. On failure, return NULL and set errno.
33 On success, *PNEW_FD is at least 3, so this is a "safer" function. */
35 DIR *
36 opendirat (int dir_fd, char const *dir, int extra_flags, int *pnew_fd)
38 int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY
39 | O_NONBLOCK | extra_flags);
40 int new_fd = openat (dir_fd, dir, open_flags);
42 if (new_fd < 0)
43 return NULL;
44 DIR *dirp = fdopendir (new_fd);
45 if (dirp)
46 *pnew_fd = new_fd;
47 else
49 int fdopendir_errno = errno;
50 close (new_fd);
51 errno = fdopendir_errno;
53 return dirp;