Re: [dev] [st] [PATCH] Replace close and dup with dup2.

From: Connor Lane Smith <cls_AT_lubutu.com>
Date: Wed, 29 Apr 2015 23:21:35 +0100

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
Received on Thu Apr 30 2015 - 00:21:35 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 30 2015 - 00:24:09 CEST