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