diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2022-01-18 11:27:54 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2022-01-18 12:27:29 -0500 |
commit | 03563b1407e436b2863509ebf09d412e79cbd1dd (patch) | |
tree | d2e5b735c8be3078b7b76c31c9168330a7f2557b /util.c | |
parent | abbb00766a8d10f63bbafb60bb13eb4672d7f44a (diff) | |
download | bfs-03563b1407e436b2863509ebf09d412e79cbd1dd.tar.xz |
util: New close() wrappers to check for EBADF and preserve errno
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 28 |
1 files changed, 19 insertions, 9 deletions
@@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2016-2021 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2016-2022 Tavian Barnes <tavianator@tavianator.com> * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -89,7 +89,7 @@ int dup_cloexec(int fd) { } if (fcntl(ret, F_SETFD, FD_CLOEXEC) == -1) { - close(ret); + close_quietly(ret); return -1; } @@ -106,10 +106,8 @@ int pipe_cloexec(int pipefd[2]) { } if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1) { - int error = errno; - close(pipefd[1]); - close(pipefd[0]); - errno = error; + close_quietly(pipefd[1]); + close_quietly(pipefd[0]); return -1; } @@ -464,11 +462,23 @@ FILE *xfopen(const char *path, int flags) { FILE *ret = fdopen(fd, mode); if (!ret) { - int error = errno; - close(fd); - errno = error; + close_quietly(fd); return NULL; } return ret; } + +int xclose(int fd) { + int ret = close(fd); + if (ret != 0) { + assert(errno != EBADF); + } + return ret; +} + +void close_quietly(int fd) { + int error = errno; + xclose(fd); + errno = error; +} |