summaryrefslogtreecommitdiff
path: root/lib/stdopen.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2005-08-19 17:18:04 +0000
committerJim Meyering <jim@meyering.net>2005-08-19 17:18:04 +0000
commitcc6d255ed5332e5fb7f7be80e2ea13da66317a23 (patch)
treeb52af41c38affec6245f93ccd275208bcec882b5 /lib/stdopen.c
parent159c7aa02c0aa9f863d20e8dd6ac2d8d4bdd9f14 (diff)
downloadcoreutils-cc6d255ed5332e5fb7f7be80e2ea13da66317a23.tar.xz
(stdopen): Return `bool' so caller can detect failure.
Diffstat (limited to 'lib/stdopen.c')
-rw-r--r--lib/stdopen.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/stdopen.c b/lib/stdopen.c
index 6ea2264b8..00914307d 100644
--- a/lib/stdopen.c
+++ b/lib/stdopen.c
@@ -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;
}