Mercurial > nsnake
changeset 59:30012b993f7a
nsnake: remove err(3) shims
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 28 Jan 2020 20:10:00 +0100 |
parents | d8613c3942fd |
children | 7014eeb633a8 |
files | CHANGES.md extern/err.c nsnake.c sysconfig.sh |
diffstat | 4 files changed, 32 insertions(+), 139 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.md Sun Jan 26 10:40:04 2020 +0100 +++ b/CHANGES.md Tue Jan 28 20:10:00 2020 +0100 @@ -1,6 +1,11 @@ NSnake CHANGES ============== +nsnake current +----------------------- + +- Removal of non-POSIX err(3) functions. + nsnake 2.2.1 2020-01-26 -----------------------
--- a/extern/err.c Sun Jan 26 10:40:04 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -/* - * err.c -- formtted error messages (portable version) - * - * Copyright (c) 2011-2018 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <stdarg.h> -#include <string.h> -#include <errno.h> - -#include "err.h" - -/* - * These functions implements at least the same functions that can be found - * in the NetBSD err(3) man page without printing the programe name due to - * a portability issue. - */ - -void -err(int val, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verr(val, fmt, ap); - va_end(ap); -} - -void -verr(int val, const char *fmt, va_list ap) -{ - if (fmt) { - vfprintf(stderr, fmt, ap); - fprintf(stderr, ": "); - } - - fprintf(stderr, "%s\n", strerror(errno)); - exit(val); -} - -void -errx(int val, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - verrx(val, fmt, ap); - va_end(ap); -} - -void -verrx(int val, const char *fmt, va_list ap) -{ - if (fmt) - vfprintf(stderr, fmt, ap); - - fprintf(stderr, "\n"); - - exit(val); -} - -void -warn(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vwarn(fmt, ap); - va_end(ap); -} - -void -vwarn(const char *fmt, va_list ap) -{ - if (fmt) { - vfprintf(stderr, fmt, ap); - fprintf(stderr, ": "); - } - - fprintf(stderr, "%s\n", strerror(errno)); -} - -void -warnx(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vwarnx(fmt, ap); - va_end(ap); -} - -void -vwarnx(const char *fmt, va_list ap) -{ - if (fmt) - vfprintf(stderr, fmt, ap); - - fprintf(stderr, "\n"); -}
--- a/nsnake.c Sun Jan 26 10:40:04 2020 +0100 +++ b/nsnake.c Tue Jan 28 20:10:00 2020 +0100 @@ -17,10 +17,13 @@ */ #include <sys/stat.h> +#include <errno.h> #include <signal.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <stdint.h> #include <string.h> #include <time.h> @@ -43,12 +46,6 @@ # define srandom srand #endif -#if defined(HAVE_ERR) -# include <err.h> -#else -# include "extern/err.c" -#endif - #if !defined(HAVE_GETOPT) # include "extern/getopt.c" #endif @@ -110,6 +107,23 @@ static WINDOW *frame = NULL; static void +die(bool sys, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (sys) + fprintf(stderr, ": %s\n", strerror(errno)); + else + fprintf(stderr, "\n"); + + exit(1); +} + +static void wset(WINDOW *frame, int pair) { if (setcolors) @@ -395,14 +409,14 @@ struct score sc; if (!(fp = fopen(DATABASE, "rb"))) - err(1, "Could not read %s", DATABASE); + die(true, "Could not read %s", DATABASE); if (verbose) printf("Wall crossing %s\n", (warp) ? "enabled" : "disabled"); fread(header, sizeof (header), 1, fp); if (strncmp(header, "nsnake-score", sizeof (header)) != 0) - errx(1, "Not a valid nsnake score file"); + die(false, "Not a valid nsnake score file"); fread(&nscore, sizeof (nscore), 1, fp); for (i = 0; i < nscore; ++i) { @@ -480,7 +494,7 @@ if (x < WIDTH || y < HEIGHT) { quit(NULL); - errx(1, "Terminal has been resized too small, aborting"); + die(false, "Terminal has been resized too small, aborting"); } mvwin(frame, (y / 2) - (HEIGHT / 2), (x / 2) - (WIDTH / 2)); @@ -549,12 +563,12 @@ if (!init()) { quit(NULL); - errx(1, "Terminal too small, aborting"); + die(false, "Terminal too small, aborting"); } if (top == NULL || frame == NULL) { endwin(); - errx(1, "ncurses failed to init"); + die(false, "ncurses failed to init"); } /* Apply GRID_WALL to the edges */ @@ -656,7 +670,7 @@ printf("%sScore: %d\n", is_dead(&sn) ? "You died...\n" : "", sn.score); if (!noscore && !register_score(&sn)) - err(1, "Could not write score file %s", DATABASE); + die(true, "Could not write score file %s", DATABASE); return 0; }
--- a/sysconfig.sh Sun Jan 26 10:40:04 2020 +0100 +++ b/sysconfig.sh Tue Jan 28 20:10:00 2020 +0100 @@ -47,18 +47,6 @@ } EOF -# err(3) family functions. -cat << EOF | compile "#define HAVE_ERR" -#include <err.h> - -int -main(void) -{ - err(1, ""); - errx(1, ""); -} -EOF - # getopt(3) function. cat << EOF | compile "#define HAVE_GETOPT" #include <unistd.h>