summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-10-01 10:24:40 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-10-01 10:24:40 -0400
commit958d591c888a4b9ab457ae414ef120dae77ee589 (patch)
tree0eac4395fc11d5f30144aad3528cb77cadfde64c /main.c
parent80ac731c907c04f60148a696162cb95d7cedc90a (diff)
downloadbfs-958d591c888a4b9ab457ae414ef120dae77ee589.tar.xz
main: Preserve errno over close() in redirect()
Diffstat (limited to 'main.c')
-rw-r--r--main.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/main.c b/main.c
index f77d787..2d6649e 100644
--- a/main.c
+++ b/main.c
@@ -71,14 +71,15 @@ static bool isopen(int fd) {
* Open a file and redirect it to a particular descriptor.
*/
static int redirect(int fd, const char *path, int flags) {
- int ret = open(path, flags);
-
- if (ret >= 0 && ret != fd) {
- int orig = ret;
- ret = dup2(orig, fd);
- close(orig);
+ int newfd = open(path, flags);
+ if (newfd < 0 || newfd == fd) {
+ return newfd;
}
+ int ret = dup2(newfd, fd);
+ int err = errno;
+ close(newfd);
+ errno = err;
return ret;
}