fopen-safer.c revision 267654
1112158Sdas/* Invoke fopen, but avoid some glitches. 2112158Sdas Copyright (C) 2001, 2004 Free Software Foundation, Inc. 3112158Sdas 4112158Sdas This program is free software; you can redistribute it and/or modify 5112158Sdas it under the terms of the GNU General Public License as published by 6112158Sdas the Free Software Foundation; either version 2, or (at your option) 7112158Sdas any later version. 8112158Sdas 9112158Sdas This program is distributed in the hope that it will be useful, 10112158Sdas but WITHOUT ANY WARRANTY; without even the implied warranty of 11112158Sdas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12112158Sdas GNU General Public License for more details. 13112158Sdas 14112158Sdas You should have received a copy of the GNU General Public License 15112158Sdas along with this program; if not, write to the Free Software Foundation, 16112158Sdas Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17112158Sdas 18112158Sdas/* Written by Paul Eggert. */ 19112158Sdas 20112158Sdas#if HAVE_CONFIG_H 21112158Sdas# include <config.h> 22112158Sdas#endif 23112158Sdas 24112158Sdas#if HAVE_UNISTD_H 25112158Sdas# include <unistd.h> 26112158Sdas#endif 27112158Sdas#include <unistd-safer.h> 28112158Sdas 29165743Sdas#ifndef STDERR_FILENO 30165743Sdas# define STDERR_FILENO 2 31112158Sdas#endif 32112158Sdas 33112158Sdas#include <errno.h> 34112158Sdas#include <stdio.h> 35112158Sdas#include <stdio-safer.h> 36112158Sdas 37112158Sdas/* Like fopen, but do not return stdin, stdout, or stderr. */ 38112158Sdas 39112158SdasFILE * 40112158Sdasfopen_safer (char const *file, char const *mode) 41112158Sdas{ 42112158Sdas FILE *fp = fopen (file, mode); 43112158Sdas 44112158Sdas if (fp) 45112158Sdas { 46112158Sdas int fd = fileno (fp); 47112158Sdas 48112158Sdas if (0 <= fd && fd <= STDERR_FILENO) 49112158Sdas { 50112158Sdas int f = dup_safer (fd); 51112158Sdas 52112158Sdas if (f < 0) 53112158Sdas { 54112158Sdas int e = errno; 55112158Sdas fclose (fp); 56112158Sdas errno = e; 57112158Sdas return NULL; 58112158Sdas } 59112158Sdas 60112158Sdas if (fclose (fp) != 0 61 || ! (fp = fdopen (f, mode))) 62 { 63 int e = errno; 64 close (f); 65 errno = e; 66 return NULL; 67 } 68 } 69 } 70 71 return fp; 72} 73