GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / tools / misc / xz / src / common / tuklib_open_stdxxx.c
blob08bc60d8cf87c7bb1fb3bc73d2e82750f31df606
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file tuklib_open_stdxxx.c
4 /// \brief Make sure that file descriptors 0, 1, and 2 are open
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "tuklib_open_stdxxx.h"
15 #ifndef TUKLIB_DOSLIKE
16 # include <stdlib.h>
17 # include <errno.h>
18 # include <fcntl.h>
19 # include <unistd.h>
20 #endif
23 extern void
24 tuklib_open_stdxxx(int err_status)
26 #ifdef TUKLIB_DOSLIKE
27 // Do nothing, just silence warnings.
28 (void)err_status;
30 #else
31 for (int i = 0; i <= 2; ++i) {
32 // We use fcntl() to check if the file descriptor is open.
33 if (fcntl(i, F_GETFD) == -1 && errno == EBADF) {
34 // With stdin, we could use /dev/full so that
35 // writing to stdin would fail. However, /dev/full
36 // is Linux specific, and if the program tries to
37 // write to stdin, there's already a problem anyway.
38 const int fd = open("/dev/null", O_NOCTTY
39 | (i == 0 ? O_WRONLY : O_RDONLY));
41 if (fd != i) {
42 // Something went wrong. Exit with the
43 // exit status we were given. Don't try
44 // to print an error message, since stderr
45 // may very well be non-existent. This
46 // error should be extremely rare.
47 (void)close(fd);
48 exit(err_status);
52 #endif
54 return;