|
Packit |
709fb3 |
/* Invoke pipe, but avoid some glitches.
|
|
Packit |
709fb3 |
Copyright (C) 2005-2006, 2009-2017 Free Software Foundation, Inc.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
709fb3 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
709fb3 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
709fb3 |
(at your option) any later version.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
709fb3 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
709fb3 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
709fb3 |
GNU General Public License for more details.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
You should have received a copy of the GNU General Public License
|
|
Packit |
709fb3 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* Written by Jim Meyering. */
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <config.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include "unistd-safer.h"
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <unistd.h>
|
|
Packit |
709fb3 |
#include <errno.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* Like pipe, but ensure that neither of the file descriptors is
|
|
Packit |
709fb3 |
STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO. Fail with ENOSYS on
|
|
Packit |
709fb3 |
platforms that lack pipe. */
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
int
|
|
Packit |
709fb3 |
pipe_safer (int fd[2])
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
#if HAVE_PIPE
|
|
Packit |
709fb3 |
if (pipe (fd) == 0)
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
int i;
|
|
Packit |
709fb3 |
for (i = 0; i < 2; i++)
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
fd[i] = fd_safer (fd[i]);
|
|
Packit |
709fb3 |
if (fd[i] < 0)
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
int e = errno;
|
|
Packit |
709fb3 |
close (fd[1 - i]);
|
|
Packit |
709fb3 |
errno = e;
|
|
Packit |
709fb3 |
return -1;
|
|
Packit |
709fb3 |
}
|
|
Packit |
709fb3 |
}
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
return 0;
|
|
Packit |
709fb3 |
}
|
|
Packit |
709fb3 |
#else
|
|
Packit |
709fb3 |
errno = ENOSYS;
|
|
Packit |
709fb3 |
#endif
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
return -1;
|
|
Packit |
709fb3 |
}
|