diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2021-01-08 17:34:19 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2021-01-08 17:46:32 +0200 |
commit | e4d1edadefdeefcca3b72ec32744c52020fe4642 (patch) | |
tree | d2b5096292d4088045c2c882b4fd413bf33b8136 /lib | |
parent | 0836a5114770e12ef4f4ebb3972868ba844f43f5 (diff) | |
download | tar-e4d1edadefdeefcca3b72ec32744c52020fe4642.tar.gz |
Actually prefer /dev/full over /dev/null as a replacement for stdin
* lib/stdopen.c (stdopen): Fix improper condition.
Avoid leaking extra file descriptor.
* src/tar.c (main): Set name of the stdout for diagnostics.
Bail out if stdopen fails.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/stdopen.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/lib/stdopen.c b/lib/stdopen.c index d9775727..8c891168 100644 --- a/lib/stdopen.c +++ b/lib/stdopen.c @@ -52,15 +52,22 @@ stdopen (void) static const int contrary_mode[] = { O_WRONLY, O_RDONLY, O_RDONLY }; int mode = contrary_mode[fd]; - int new_fd; + int new_fd = -1; /* Open /dev/null with the contrary mode so that the typical read (stdin) or write (stdout, stderr) operation will fail. With descriptor 0, we can do even better on systems that have /dev/full, by opening that write-only instead of /dev/null. The only drawback is that a write-provoked failure comes with a misleading errno value, ENOSPC. */ - if (mode == O_RDONLY - || (new_fd = open ("/dev/full", mode) != fd)) + if (mode == O_WRONLY) + { + if ((new_fd = open ("/dev/full", mode)) != fd) + { + close (new_fd); + new_fd = -1; + } + } + if (new_fd == -1) new_fd = open ("/dev/null", mode); if (new_fd != fd) { |