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. */
22 #include <opendirat.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. */
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
);
44 DIR *dirp
= fdopendir (new_fd
);
49 int fdopendir_errno
= errno
;
51 errno
= fdopendir_errno
;