00001 /* Invoke open, but avoid some glitches. 00002 00003 Copyright (C) 2005, 2006 Free Software Foundation, Inc. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2, or (at your option) 00008 any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software Foundation, 00017 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 00018 00019 /* Written by Paul Eggert. */ 00020 00021 #include <config.h> 00022 00023 #include "fcntl-safer.h" 00024 00025 #include <fcntl.h> 00026 #include <stdarg.h> 00027 #include "unistd-safer.h" 00028 00029 int 00030 open_safer (char const *file, int flags, ...) 00031 { 00032 mode_t mode = 0; 00033 00034 if (flags & O_CREAT) 00035 { 00036 va_list ap; 00037 va_start (ap, flags); 00038 00039 /* Assume mode_t promotes to int if and only if it is smaller. 00040 This assumption isn't guaranteed by the C standard, but we 00041 don't know of any real-world counterexamples. */ 00042 mode = (sizeof (mode_t) < sizeof (int) 00043 ? va_arg (ap, int) 00044 : va_arg (ap, mode_t)); 00045 00046 va_end (ap); 00047 } 00048 00049 return fd_safer (open (file, flags, mode)); 00050 }