1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file tuklib_open_stdxxx.c
6 /// \brief Make sure that file descriptors 0, 1, and 2 are open
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tuklib_open_stdxxx.h"
14 #ifndef TUKLIB_DOSLIKE
23 tuklib_open_stdxxx(int err_status
)
26 // Do nothing, just silence warnings.
30 for (int i
= 0; i
<= 2; ++i
) {
31 // We use fcntl() to check if the file descriptor is open.
32 if (fcntl(i
, F_GETFD
) == -1 && errno
== EBADF
) {
33 // With stdin, we could use /dev/full so that
34 // writing to stdin would fail. However, /dev/full
35 // is Linux specific, and if the program tries to
36 // write to stdin, there's already a problem anyway.
37 const int fd
= open("/dev/null", O_NOCTTY
38 | (i
== 0 ? O_WRONLY
: O_RDONLY
));
44 // Something went wrong. Exit with the
45 // exit status we were given. Don't try
46 // to print an error message, since stderr
47 // may very well be non-existent. This
48 // error should be extremely rare.