| 1 |
/* Open a directory relative to another directory. |
| 2 |
|
| 3 |
Copyright 2006-2026 Free Software Foundation, Inc. |
| 4 |
|
| 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. |
| 9 |
|
| 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. |
| 14 |
|
| 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/>. |
| 17 |
|
| 18 |
Written by Jim Meyering and Paul Eggert. */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include <opendirat.h> |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#ifdef GNULIB_OPENAT_SAFER |
| 29 |
# include "fcntl--.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
/* Relative to DIR_FD, open the directory DIR, passing EXTRA_FLAGS to |
| 33 |
the underlying openat call. On success, store into *PNEW_FD the |
| 34 |
underlying file descriptor of the newly opened directory and return |
| 35 |
the directory stream. On failure, return NULL and set errno. |
| 36 |
|
| 37 |
On success, *PNEW_FD is at least 3, so this is a "safer" function. */ |
| 38 |
|
| 39 |
DIR * |
| 40 |
opendirat (int dir_fd, char const *dir, int extra_flags, int *pnew_fd) |
| 41 |
{ |
| 42 |
int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY |
| 43 |
| O_NONBLOCK | extra_flags); |
| 44 |
int new_fd = openat (dir_fd, dir, open_flags); |
| 45 |
|
| 46 |
if (new_fd < 0) |
| 47 |
return NULL; |
| 48 |
DIR *dirp = fdopendir (new_fd); |
| 49 |
if (dirp) |
| 50 |
*pnew_fd = new_fd; |
| 51 |
else |
| 52 |
{ |
| 53 |
int fdopendir_errno = errno; |
| 54 |
close (new_fd); |
| 55 |
errno = fdopendir_errno; |
| 56 |
} |
| 57 |
return dirp; |
| 58 |
} |