xref: /dflybsd-src/contrib/grep/lib/openat-safer.c (revision cf28ed851948818eef148d616edca961f756ade8)
1*cf28ed85SJohn Marino /* Invoke openat, but avoid some glitches.
2*cf28ed85SJohn Marino 
3*cf28ed85SJohn Marino    Copyright (C) 2005-2006, 2008-2012 Free Software Foundation, Inc.
4*cf28ed85SJohn Marino 
5*cf28ed85SJohn Marino    This program is free software: you can redistribute it and/or modify
6*cf28ed85SJohn Marino    it under the terms of the GNU General Public License as published by
7*cf28ed85SJohn Marino    the Free Software Foundation; either version 3 of the License, or
8*cf28ed85SJohn Marino    (at your option) any later version.
9*cf28ed85SJohn Marino 
10*cf28ed85SJohn Marino    This program is distributed in the hope that it will be useful,
11*cf28ed85SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*cf28ed85SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*cf28ed85SJohn Marino    GNU General Public License for more details.
14*cf28ed85SJohn Marino 
15*cf28ed85SJohn Marino    You should have received a copy of the GNU General Public License
16*cf28ed85SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17*cf28ed85SJohn Marino 
18*cf28ed85SJohn Marino /* Written by Paul Eggert for open, ported by Eric Blake for openat.  */
19*cf28ed85SJohn Marino 
20*cf28ed85SJohn Marino #include <config.h>
21*cf28ed85SJohn Marino 
22*cf28ed85SJohn Marino #include "fcntl-safer.h"
23*cf28ed85SJohn Marino 
24*cf28ed85SJohn Marino #include <fcntl.h>
25*cf28ed85SJohn Marino #include <stdarg.h>
26*cf28ed85SJohn Marino #include "unistd-safer.h"
27*cf28ed85SJohn Marino 
28*cf28ed85SJohn Marino int
29*cf28ed85SJohn Marino openat_safer (int fd, char const *file, int flags, ...)
30*cf28ed85SJohn Marino {
31*cf28ed85SJohn Marino   mode_t mode = 0;
32*cf28ed85SJohn Marino 
33*cf28ed85SJohn Marino   if (flags & O_CREAT)
34*cf28ed85SJohn Marino     {
35*cf28ed85SJohn Marino       va_list ap;
36*cf28ed85SJohn Marino       va_start (ap, flags);
37*cf28ed85SJohn Marino 
38*cf28ed85SJohn Marino       /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
39*cf28ed85SJohn Marino          creates crashing code when 'mode_t' is smaller than 'int'.  */
40*cf28ed85SJohn Marino       mode = va_arg (ap, PROMOTED_MODE_T);
41*cf28ed85SJohn Marino 
42*cf28ed85SJohn Marino       va_end (ap);
43*cf28ed85SJohn Marino     }
44*cf28ed85SJohn Marino 
45*cf28ed85SJohn Marino   return fd_safer (openat (fd, file, flags, mode));
46*cf28ed85SJohn Marino }
47