1 /* ISC license. */
2 
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <skalibs/djbunix.h>
6 
fd_ensure_open(int fd,int w)7 int fd_ensure_open (int fd, int w)
8 {
9   int dummy ;
10   if (fcntl(fd, F_GETFD, &dummy) < 0)
11   {
12     int newfd ;
13     if (errno != EBADF) return 0 ;
14     newfd = open2("/dev/null", w ? O_WRONLY : O_RDONLY) ;
15     if (newfd < 0) return 0 ;
16     if (fd_move(fd, newfd) < 0)
17     {
18       fd_close(newfd) ;
19       return 0 ;
20     }
21   }
22   return 1 ;
23 }
24