1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file open_stdxxx.h
4 /// \brief Make sure that file descriptors 0, 1, and 2 are open
6 // This code has been put into the public domain.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 ///////////////////////////////////////////////////////////////////////////////
23 open_stdxxx(int status)
25 for (int i = 0; i <= 2; ++i) {
26 // We use fcntl() to check if the file descriptor is open.
27 if (fcntl(i, F_GETFD) == -1 && errno == EBADF) {
28 // With stdin, we could use /dev/full so that
29 // writing to stdin would fail. However, /dev/full
30 // is Linux specific, and if the program tries to
31 // write to stdin, there's already a problem anyway.
32 const int fd = open("/dev/null", O_NOCTTY
33 | (i == 0 ? O_WRONLY : O_RDONLY));
36 // Something went wrong. Exit with the
37 // exit status we were given. Don't try
38 // to print an error message, since stderr
39 // may very well be non-existent. This
40 // error should be extremely rare.