pipe-safer.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Invoke pipe, but avoid some glitches.
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Jim Meyering. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "unistd-safer.h"
  19. #include <unistd.h>
  20. /* Like pipe, but ensure that neither of the file descriptors is
  21. STDIN_FILENO, STDOUT_FILENO, or STDERR_FILENO. */
  22. int
  23. pipe_safer (int fd[2])
  24. {
  25. int fail = pipe (fd);
  26. if (fail)
  27. return fail;
  28. {
  29. int i;
  30. for (i = 0; i < 2; i++)
  31. {
  32. int f = fd_safer (fd[i]);
  33. if (f < 0)
  34. return -1;
  35. fd[i] = f;
  36. }
  37. }
  38. return 0;
  39. }