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 --- bar.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'bar.c') diff --git a/bar.c b/bar.c index 31734ac..b0e595e 100644 --- a/bar.c +++ b/bar.c @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2020 Tavian Barnes * + * Copyright (C) 2020-2022 Tavian Barnes * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -155,28 +155,24 @@ static int bfs_bar_printf(struct bfs_bar *bar, const char *format, ...) { } struct bfs_bar *bfs_bar_show(void) { - int error; - if (the_bar.fd >= 0) { - error = EBUSY; + errno = EBUSY; goto fail; } char term[L_ctermid]; ctermid(term); if (strlen(term) == 0) { - error = ENOTTY; + errno = ENOTTY; goto fail; } the_bar.fd = open(term, O_RDWR | O_CLOEXEC); if (the_bar.fd < 0) { - error = errno; goto fail; } if (bfs_bar_getsize(&the_bar) != 0) { - error = errno; goto fail_close; } @@ -207,10 +203,9 @@ struct bfs_bar *bfs_bar_show(void) { return &the_bar; fail_close: - close(the_bar.fd); + close_quietly(the_bar.fd); the_bar.fd = -1; fail: - errno = error; return NULL; } @@ -248,6 +243,6 @@ void bfs_bar_hide(struct bfs_bar *bar) { bfs_bar_reset(bar); - close(bar->fd); + xclose(bar->fd); bar->fd = -1; } -- cgit v1.2.3