logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: fda61c4a476450193bb0baf6f55c09f69ee680d8
parent d4ebbce4d1af9f1b4d654c79a996daa120783dd5
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  7 Dec 2024 03:59:29 +0100

cmd/test: drop usage of <err.h>

Diffstat:

Mcmd/test.c33+++++++++++++++++++--------------
1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/cmd/test.c b/cmd/test.c @@ -15,25 +15,30 @@ #define _XOPEN_SOURCE 700 // S_ISVTX for -k option #include <ctype.h> -#include <err.h> #include <errno.h> #include <inttypes.h> #include <locale.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> -static void +const char *argv0 = "test"; + +_Noreturn static void error(const char *msg, ...) { va_list ap; + + fprintf(stderr, "%s: error: ", argv0); va_start(ap, msg); - verrx(2, msg, ap); - /*NOTREACHED*/ + vfprintf(stderr, msg, ap); va_end(ap); + putc('\n', stderr); + exit(2); } /* test(1) accepts the following grammar: @@ -198,7 +203,8 @@ main(int argc, char **argv) p++; if(strcmp(p, "[") == 0) { - if(strcmp(argv[--argc], "]") != 0) error("error: missing ]"); + argv0 = "["; + if(strcmp(argv[--argc], "]") != 0) error("missing ]"); argv[argc] = NULL; } @@ -227,11 +233,10 @@ main(int argc, char **argv) static void syntax(const char *op, const char *msg) { - if(op && *op) - error("error: %s: %s", op, msg); + error("%s: %s", op, msg); else - error("error: %s", msg); + error("%s", msg); } static int @@ -514,14 +519,14 @@ getn(const char *s) errno = 0; r = strtol(s, &p, 10); - if(s == p) error("error: %s: bad number", s); + if(s == p) error("%s: bad number", s); - if(errno != 0) error((errno == EINVAL) ? "error: %s: bad number" : "error: %s: out of range", s); + if(errno != 0) error((errno == EINVAL) ? "%s: bad number" : "%s: out of range", s); while(isspace((unsigned char)*p)) p++; - if(*p) error("error: %s: bad number", s); + if(*p) error("%s: bad number", s); return (int)r; } @@ -536,14 +541,14 @@ getq(const char *s) errno = 0; r = strtoimax(s, &p, 10); - if(s == p) error("error: %s: bad number", s); + if(s == p) error("%s: bad number", s); - if(errno != 0) error((errno == EINVAL) ? "error: %s: bad number" : "error: %s: out of range", s); + if(errno != 0) error((errno == EINVAL) ? "%s: bad number" : "%s: out of range", s); while(isspace((unsigned char)*p)) p++; - if(*p) error("error: %s: bad number", s); + if(*p) error("%s: bad number", s); return r; }