diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 10:24:40 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-10-01 10:24:40 -0400 |
commit | 958d591c888a4b9ab457ae414ef120dae77ee589 (patch) | |
tree | 0eac4395fc11d5f30144aad3528cb77cadfde64c /main.c | |
parent | 80ac731c907c04f60148a696162cb95d7cedc90a (diff) | |
download | bfs-958d591c888a4b9ab457ae414ef120dae77ee589.tar.xz |
main: Preserve errno over close() in redirect()
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 13 |
1 files changed, 7 insertions, 6 deletions
@@ -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; } |