Hi, I'm personally in favour of using dup2. It is quite clear that dup2(cmdfd, STDIN_FILENO) duplicates the fd into the position of STDIN_FILENO. That's what it says. On the other hand, the combination of close(STDIN_FILENO) and dup(cmdfd) is less efficient (two syscalls) and also means that the reader is forced to infer that because STDIN_FILENO, being fd 0 and therefore now the first available fd, is the one that will be taken by the following dup -- assuming that the close succeeded, which is not checked.
Note that dup2 came later, so earlier texts which use both close and dup may simply be out of date. Regardless, the idiom is out of date, and should be replaced with dup2. And even if you disagree, you have to check the return value(s). Thanks, Connor
