00001 /* Invoke pipe, but avoid some glitches. 00002 Copyright (C) 2005, 2006 Free Software Foundation, Inc. 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2, or (at your option) 00007 any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software Foundation, 00016 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 00017 00018 /* Written by Jim Meyering. */ 00019 00020 #include <config.h> 00021 00022 #include "unistd-safer.h" 00023 00024 #include <unistd.h> 00025 #include <errno.h> 00026 00027 /* Like pipe, but ensure that neither of the file descriptors is 00028 STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO. Fail with ENOSYS on 00029 platforms that lack pipe. */ 00030 00031 int 00032 pipe_safer (int fd[2]) 00033 { 00034 #if HAVE_PIPE 00035 if (pipe (fd) == 0) 00036 { 00037 int i; 00038 for (i = 0; i < 2; i++) 00039 { 00040 fd[i] = fd_safer (fd[i]); 00041 if (fd[i] < 0) 00042 { 00043 int e = errno; 00044 close (fd[1 - i]); 00045 errno = e; 00046 return -1; 00047 } 00048 } 00049 00050 return 0; 00051 } 00052 #else 00053 errno = ENOSYS; 00054 #endif 00055 00056 return -1; 00057 }