1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include "stdopen.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- bool
- stdopen (void)
- {
- int fd;
- bool ok = true;
- for (fd = 0; fd <= 2; fd++)
- {
- if (fcntl (fd, F_GETFD) < 0)
- {
- if (errno != EBADF)
- ok = false;
- else
- {
- static const int contrary_mode[]
- = { O_WRONLY, O_RDONLY, O_RDONLY };
- int mode = contrary_mode[fd];
- int new_fd;
-
- if (mode == O_RDONLY
- || (new_fd = open ("/dev/full", mode) != fd))
- new_fd = open ("/dev/null", mode);
- if (new_fd != fd)
- {
- if (0 <= new_fd)
- close (new_fd);
- ok = false;
- }
- }
- }
- }
- return ok;
- }
|