1 /* Target directory operands for coreutils
2 
3    Copyright 2022-2023 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 #include <fcntl.h>
19 #include <sys/stat.h>
20 
21 #ifndef _GL_INLINE_HEADER_BEGIN
22 # error "Please include config.h first."
23 #endif
24 _GL_INLINE_HEADER_BEGIN
25 #ifndef TARGETDIR_INLINE
26 # define TARGETDIR_INLINE _GL_INLINE
27 #endif
28 
29 /* Return a file descriptor open to FILE, for use in openat.
30    As an optimization, return AT_FDCWD if FILE must be the working directory.
31    As a side effect, possibly set *ST to the file's status.
32    Fail and set errno if FILE is not a directory.
33    On failure return -2 if AT_FDCWD is -1, -1 otherwise.  */
34 extern int target_directory_operand (char const *file, struct stat *st);
35 
36 /* Return true if FD represents success for target_directory_operand.  */
37 TARGETDIR_INLINE _GL_ATTRIBUTE_PURE bool
target_dirfd_valid(int fd)38 target_dirfd_valid (int fd)
39 {
40   return fd != -1 - (AT_FDCWD == -1);
41 }
42 
43 _GL_INLINE_HEADER_END
44