mirror of
https://git.savannah.gnu.org/git/coreutils.git
synced 2026-11-17 18:02:09 +03:00
(stdopen): Return `bool' so caller can detect failure.
This commit is contained in:
1 parent
159c7aa02c
commit
cc6d255ed5
1 file changed
+11
-3
+11
-3
@@ -44,9 +44,11 @@
|
||||
|
||||
/* Try to ensure that each of the standard file numbers (0, 1, 2)
|
||||
is in use. Without this, each application would have to guard
|
||||
every call to open, dup, and fopen with tests to ensure they don't
|
||||
use one of the special file numbers when opening a user's file. */
|
||||
void
|
||||
every call to open, dup, fopen, etc. with tests to ensure they
|
||||
don't use one of the special file numbers when opening a file.
|
||||
Return false if at least one of the file descriptors is initially
|
||||
closed and an attempt to reopen it fails. Otherwise, return true. */
|
||||
bool
|
||||
stdopen (void)
|
||||
{
|
||||
int fd = dup (STDIN_FILENO);
|
||||
@@ -55,13 +57,19 @@ stdopen (void)
|
||||
close (fd);
|
||||
else if (errno == EBADF)
|
||||
fd = open ("/dev/null", O_WRONLY);
|
||||
else
|
||||
return false;
|
||||
|
||||
if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
|
||||
{
|
||||
fd = open ("/dev/null", O_RDONLY);
|
||||
if (fd == STDOUT_FILENO)
|
||||
fd = dup (fd);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
|
||||
if (STDERR_FILENO < fd)
|
||||
close (fd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user