From 5754380c82f99ee4ac28aaca53e0589e2160774d Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 30 Jun 2005 20:13:40 +0000 Subject: Rewritten by Paul Eggert. Now, the minimum overhead is just two system calls: dup and close. --- lib/stdopen.c | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) (limited to 'lib/stdopen.c') diff --git a/lib/stdopen.c b/lib/stdopen.c index 6cf521896..2bf2a01b4 100644 --- a/lib/stdopen.c +++ b/lib/stdopen.c @@ -1,6 +1,24 @@ #include #include -#include +#if HAVE_FCNTL_H +# include +#endif +#if HAVE_UNISTD_H +# include +#endif +#include + +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif + +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif + +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif /* Try to ensure that each of the standard file numbers (0, 1, 2) is in use. Without this, each application would have to guard @@ -9,23 +27,19 @@ void stdopen (void) { - int fd; - for (fd = 0; fd <= 2; fd++) + int fd = dup (STDIN_FILENO); + + if (0 <= fd) + close (fd); + else if (errno == EBADF) + fd = open ("/dev/null", O_WRONLY); + + if (STDIN_FILENO <= fd && fd <= STDERR_FILENO) { - struct stat sb; - if (fstat (fd, &sb) < 0) - { - static const int flag[] = { O_WRONLY, O_RDONLY, O_RDONLY }; - /* FIXME: worry about errno != EBADF? */ - char const *file = "/dev/null"; - int fd2 = open (file, flag[fd]); - if (fd2 < 0) - break; - if (fd2 != fd) - { - close (fd2); - break; - } - } + fd = open ("/dev/null", O_RDONLY); + if (fd == STDOUT_FILENO) + fd = dup (fd); + if (STDERR_FILENO < fd) + close (fd); } } -- cgit v1.2.3-54-g00ecf