From 03563b1407e436b2863509ebf09d412e79cbd1dd Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 18 Jan 2022 11:27:54 -0500 Subject: util: New close() wrappers to check for EBADF and preserve errno --- util.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index fa7a5b6..71b3c53 100644 --- a/util.c +++ b/util.c @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2016-2021 Tavian Barnes * + * Copyright (C) 2016-2022 Tavian Barnes * * * * 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; +} -- cgit v1.2.3