catpoint

Catpoint simple presenting software.
git clone git://r-36.net/catpoint
Log | Files | Refs | LICENSE

commit 2f7d19fadd703f32a7a96555cea17c29ceb846e3
parent 4c69bd0f199e1b018ff9a8bae42bc2ddaaa0d863
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat, 29 May 2021 17:57:41 +0200

remove BSD functions err and errx with die and cleanup more graceful there

Signed-off-by: Christoph Lohmann <20h@r-36.net>

Diffstat:
catpoint.c | 47++++++++++++++++++++++++++++++++++++++---------
1 file changed, 38 insertions(+), 9 deletions(-)

diff --git a/catpoint.c b/catpoint.c @@ -4,16 +4,19 @@ #include <sys/stat.h> #include <sys/types.h> -#include <err.h> #include <curses.h> +#include <errno.h> #include <fcntl.h> #include <locale.h> #include <signal.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +void die(const char *, ...); + char *currentslidep, **slidefiles; /* the slides */ int nslides, currentslide, currentslidelen; @@ -26,7 +29,7 @@ unloadcurrentslide(void) return; if (munmap(currentslidep, currentslidelen) < 0) - err(1, "munmap: %s", slidefiles[currentslide]); + die("munmap: %s", slidefiles[currentslide]); } void @@ -37,6 +40,28 @@ cleanup(void) endwin(); /* restore terminal */ } +/* print to stderr, call cleanup() and _exit(). */ +void +die(const char *fmt, ...) +{ + va_list ap; + int saved_errno; + + saved_errno = errno; + cleanup(); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (saved_errno) + fprintf(stderr, ": %s", strerror(saved_errno)); + fflush(stderr); + write(2, "\n", 1); + + _exit(1); +} + void quit(int sig) { @@ -54,12 +79,14 @@ loadcurrentslide(char **argv, int slide) fd = open(slidefiles[slide], O_RDONLY, 0); if (fd < 0) - err(1, "open: %s", slidefiles[slide]); + die("open: %s", slidefiles[slide]); if (fstat(fd, &statbuf) < 0) - err(1, "fstat: %s", slidefiles[slide]); + die("fstat: %s", slidefiles[slide]); currentslidep = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (currentslidep == MAP_FAILED) - err(1, "mmap: %s", slidefiles[slide]); + if (currentslidep == MAP_FAILED) { + currentslidep = NULL; + die("mmap: %s", slidefiles[slide]); + } currentslidelen = statbuf.st_size; close(fd); } @@ -75,7 +102,7 @@ reloadcurrentslide(int sig) if (sig == SIGHUP) { /* Make ncurses redisplay slide. */ if (raise(SIGWINCH) < 0) - err(1, "raise"); + die("raise"); } } @@ -102,8 +129,10 @@ main(int argc, char *argv[]) { int c; - if (argc == 1) - errx(1, "usage: %s file ...", argv[0]); + if (argc == 1) { + errno = 0; + die("usage: %s file ...", argv[0]); + } slidefiles = ++argv; nslides = --argc;