Fix version.sh compatiblity with Solaris
[xz/debian.git] / src / common / tuklib_open_stdxxx.c
blobb93e61d3b6885ada3e1308ee41ba482dc3916236
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file tuklib_open_stdxxx.c
6 /// \brief Make sure that file descriptors 0, 1, and 2 are open
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "tuklib_open_stdxxx.h"
14 #ifndef TUKLIB_DOSLIKE
15 # include <stdlib.h>
16 # include <errno.h>
17 # include <fcntl.h>
18 # include <unistd.h>
19 #endif
22 extern void
23 tuklib_open_stdxxx(int err_status)
25 #ifdef TUKLIB_DOSLIKE
26 // Do nothing, just silence warnings.
27 (void)err_status;
29 #else
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));
40 if (fd != i) {
41 if (fd != -1)
42 (void)close(fd);
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.
49 exit(err_status);
53 #endif
55 return;