xref: /dragonfly/contrib/grep/lib/openat.h (revision 09d4459f)
1cf28ed85SJohn Marino /* provide a replacement openat function
2*09d4459fSDaniel Fojt    Copyright (C) 2004-2006, 2008-2020 Free Software Foundation, Inc.
3cf28ed85SJohn Marino 
4cf28ed85SJohn Marino    This program is free software: you can redistribute it and/or modify
5cf28ed85SJohn Marino    it under the terms of the GNU General Public License as published by
6cf28ed85SJohn Marino    the Free Software Foundation; either version 3 of the License, or
7cf28ed85SJohn Marino    (at your option) any later version.
8cf28ed85SJohn Marino 
9cf28ed85SJohn Marino    This program is distributed in the hope that it will be useful,
10cf28ed85SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
11cf28ed85SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12cf28ed85SJohn Marino    GNU General Public License for more details.
13cf28ed85SJohn Marino 
14cf28ed85SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16cf28ed85SJohn Marino 
17cf28ed85SJohn Marino /* written by Jim Meyering */
18cf28ed85SJohn Marino 
19cf28ed85SJohn Marino #ifndef _GL_HEADER_OPENAT
20cf28ed85SJohn Marino #define _GL_HEADER_OPENAT
21cf28ed85SJohn Marino 
22cf28ed85SJohn Marino #include <fcntl.h>
23cf28ed85SJohn Marino 
24cf28ed85SJohn Marino #include <sys/types.h>
25cf28ed85SJohn Marino #include <sys/stat.h>
26cf28ed85SJohn Marino #include <unistd.h>
27cf28ed85SJohn Marino #include <stdbool.h>
28cf28ed85SJohn Marino 
29680a9cb8SJohn Marino #ifndef _GL_INLINE_HEADER_BEGIN
30680a9cb8SJohn Marino  #error "Please include config.h first."
31680a9cb8SJohn Marino #endif
32680a9cb8SJohn Marino _GL_INLINE_HEADER_BEGIN
33680a9cb8SJohn Marino 
34cf28ed85SJohn Marino #if !HAVE_OPENAT
35cf28ed85SJohn Marino 
36cf28ed85SJohn Marino int openat_permissive (int fd, char const *file, int flags, mode_t mode,
37cf28ed85SJohn Marino                        int *cwd_errno);
38cf28ed85SJohn Marino bool openat_needs_fchdir (void);
39cf28ed85SJohn Marino 
40cf28ed85SJohn Marino #else
41cf28ed85SJohn Marino 
42cf28ed85SJohn Marino # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
43cf28ed85SJohn Marino     openat (Fd, File, Flags, Mode)
44cf28ed85SJohn Marino # define openat_needs_fchdir() false
45cf28ed85SJohn Marino 
46cf28ed85SJohn Marino #endif
47cf28ed85SJohn Marino 
48cf28ed85SJohn Marino _Noreturn void openat_restore_fail (int);
49cf28ed85SJohn Marino _Noreturn void openat_save_fail (int);
50cf28ed85SJohn Marino 
51cf28ed85SJohn Marino /* Using these function names makes application code
52cf28ed85SJohn Marino    slightly more readable than it would be with
53cf28ed85SJohn Marino    fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW).  */
54cf28ed85SJohn Marino 
55cf28ed85SJohn Marino #if GNULIB_FCHOWNAT
56cf28ed85SJohn Marino 
57680a9cb8SJohn Marino # ifndef FCHOWNAT_INLINE
58680a9cb8SJohn Marino #  define FCHOWNAT_INLINE _GL_INLINE
59680a9cb8SJohn Marino # endif
60680a9cb8SJohn Marino 
61680a9cb8SJohn Marino FCHOWNAT_INLINE int
chownat(int fd,char const * file,uid_t owner,gid_t group)62cf28ed85SJohn Marino chownat (int fd, char const *file, uid_t owner, gid_t group)
63cf28ed85SJohn Marino {
64cf28ed85SJohn Marino   return fchownat (fd, file, owner, group, 0);
65cf28ed85SJohn Marino }
66cf28ed85SJohn Marino 
67680a9cb8SJohn Marino FCHOWNAT_INLINE int
lchownat(int fd,char const * file,uid_t owner,gid_t group)68cf28ed85SJohn Marino lchownat (int fd, char const *file, uid_t owner, gid_t group)
69cf28ed85SJohn Marino {
70cf28ed85SJohn Marino   return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
71cf28ed85SJohn Marino }
72cf28ed85SJohn Marino 
73cf28ed85SJohn Marino #endif
74cf28ed85SJohn Marino 
75cf28ed85SJohn Marino #if GNULIB_FCHMODAT
76cf28ed85SJohn Marino 
77680a9cb8SJohn Marino # ifndef FCHMODAT_INLINE
78680a9cb8SJohn Marino #  define FCHMODAT_INLINE _GL_INLINE
79680a9cb8SJohn Marino # endif
80680a9cb8SJohn Marino 
81680a9cb8SJohn Marino FCHMODAT_INLINE int
chmodat(int fd,char const * file,mode_t mode)82cf28ed85SJohn Marino chmodat (int fd, char const *file, mode_t mode)
83cf28ed85SJohn Marino {
84cf28ed85SJohn Marino   return fchmodat (fd, file, mode, 0);
85cf28ed85SJohn Marino }
86cf28ed85SJohn Marino 
87680a9cb8SJohn Marino FCHMODAT_INLINE int
lchmodat(int fd,char const * file,mode_t mode)88cf28ed85SJohn Marino lchmodat (int fd, char const *file, mode_t mode)
89cf28ed85SJohn Marino {
90cf28ed85SJohn Marino   return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
91cf28ed85SJohn Marino }
92cf28ed85SJohn Marino 
93cf28ed85SJohn Marino #endif
94cf28ed85SJohn Marino 
95680a9cb8SJohn Marino #if GNULIB_STATAT
96cf28ed85SJohn Marino 
97680a9cb8SJohn Marino # ifndef STATAT_INLINE
98680a9cb8SJohn Marino #  define STATAT_INLINE _GL_INLINE
99680a9cb8SJohn Marino # endif
100680a9cb8SJohn Marino 
101680a9cb8SJohn Marino STATAT_INLINE int
statat(int fd,char const * name,struct stat * st)102cf28ed85SJohn Marino statat (int fd, char const *name, struct stat *st)
103cf28ed85SJohn Marino {
104cf28ed85SJohn Marino   return fstatat (fd, name, st, 0);
105cf28ed85SJohn Marino }
106cf28ed85SJohn Marino 
107680a9cb8SJohn Marino STATAT_INLINE int
lstatat(int fd,char const * name,struct stat * st)108cf28ed85SJohn Marino lstatat (int fd, char const *name, struct stat *st)
109cf28ed85SJohn Marino {
110cf28ed85SJohn Marino   return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW);
111cf28ed85SJohn Marino }
112cf28ed85SJohn Marino 
113cf28ed85SJohn Marino #endif
114cf28ed85SJohn Marino 
115cf28ed85SJohn Marino /* For now, there are no wrappers named laccessat or leuidaccessat,
116cf28ed85SJohn Marino    since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and
117cf28ed85SJohn Marino    since access rights on symlinks are of limited utility.  Likewise,
118cf28ed85SJohn Marino    wrappers are not provided for accessat or euidaccessat, so as to
119cf28ed85SJohn Marino    avoid dragging in -lgen on some platforms.  */
120cf28ed85SJohn Marino 
121680a9cb8SJohn Marino _GL_INLINE_HEADER_END
122680a9cb8SJohn Marino 
123cf28ed85SJohn Marino #endif /* _GL_HEADER_OPENAT */
124