doc: Document version-etc, version-etc, and argp-version-etc.
[gnulib.git] / lib / opendirat.c
blob0638065bdc6d95c00d7ff184c270884890ff5d90
1 /* Open a directory relative to another directory.
3 Copyright 2006-2025 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 #ifdef GNULIB_OPENAT_SAFER
29 # include "fcntl--.h"
30 #endif
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.
37 On success, *PNEW_FD is at least 3, so this is a "safer" function. */
39 DIR *
40 opendirat (int dir_fd, char const *dir, int extra_flags, int *pnew_fd)
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);
46 if (new_fd < 0)
47 return NULL;
48 DIR *dirp = fdopendir (new_fd);
49 if (dirp)
50 *pnew_fd = new_fd;
51 else
53 int fdopendir_errno = errno;
54 close (new_fd);
55 errno = fdopendir_errno;
57 return dirp;