summaryrefslogtreecommitdiffstats
path: root/bar.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2022-01-18 11:27:54 -0500
committerTavian Barnes <tavianator@tavianator.com>2022-01-18 12:27:29 -0500
commit03563b1407e436b2863509ebf09d412e79cbd1dd (patch)
treed2e5b735c8be3078b7b76c31c9168330a7f2557b /bar.c
parentabbb00766a8d10f63bbafb60bb13eb4672d7f44a (diff)
downloadbfs-03563b1407e436b2863509ebf09d412e79cbd1dd.tar.xz
util: New close() wrappers to check for EBADF and preserve errno
Diffstat (limited to 'bar.c')
-rw-r--r--bar.c15
1 files changed, 5 insertions, 10 deletions
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 <tavianator@tavianator.com> *
+ * Copyright (C) 2020-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. *
@@ -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;
}