From 9433d2b803644098ce690a52e1205c541d09eba5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 4 Jun 2012 14:14:08 +0100 Subject: [PATCH] Add errno program --- Makefile | 10 ++++++-- errno.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ errnos | 6 +++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 errno.c create mode 100755 errnos diff --git a/Makefile b/Makefile index 3ffd562..501701f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -BINS=isutf8 ifdata ifne pee sponge mispipe lckdo parallel +BINS=isutf8 ifdata ifne pee sponge mispipe lckdo parallel errno PERLSCRIPTS=vidir vipe ts combine zrun chronic MANS=sponge.1 vidir.1 vipe.1 isutf8.1 ts.1 combine.1 ifdata.1 ifne.1 pee.1 zrun.1 chronic.1 mispipe.1 lckdo.1 parallel.1 CFLAGS=-O2 -g -Wall @@ -10,7 +10,7 @@ DOCBOOK2XMAN=docbook2x-man all: $(BINS) $(MANS) clean: - rm -f $(BINS) $(MANS) + rm -f $(BINS) $(MANS) dump.c errnos.h errno.o install: mkdir -p $(DESTDIR)$(PREFIX)/bin @@ -46,6 +46,12 @@ lckdo.1: lckdo.docbook parallel.1: parallel.docbook $(DOCBOOK2XMAN) $< + +errno.o: errnos.h +errnos.h: + echo '#include ' > dump.c + $(CC) -E -dD dump.c | ./errnos > errnos.h + rm -f dump.c %.1: % pod2man --center=" " --release="moreutils" $< > $@; diff --git a/errno.c b/errno.c new file mode 100644 index 0000000..aa5307b --- /dev/null +++ b/errno.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + + +static struct { + const char *name; + int code; +} errnos[] = { + #include "errnos.h" +}; +static const int num_errnos = sizeof(errnos) / sizeof(errnos[0]); + + +static void +report(const char *name, int code) +{ + printf("%s %d %s\n", name, code, strerror(code)); +} + + +static bool +report_from_name(const char *name) +{ + int i; + for (i = 0; i < num_errnos; ++i) { + if (strcasecmp(errnos[i].name, name) == 0) { + report(errnos[i].name, errnos[i].code); + return true; + } + } + return false; +} + + +static bool +report_from_code(int code) +{ + int i; + for (i = 0; i < num_errnos; ++i) { + if (errnos[i].code == code) { + report(errnos[i].name, code); + return true; + } + } + return false; +} + + +int +main(int argc, char **argv) +{ + int i; + int exit_code; + + exit_code = EXIT_SUCCESS; + for (i = 1; i < argc; ++i) { + const char *arg = argv[i]; + if (toupper(arg[0]) == 'E') { + if (!report_from_name(arg)) + exit_code = EXIT_FAILURE; + } else if (isdigit(arg[0])) { + if (!report_from_code(atoi(arg))) + exit_code = EXIT_FAILURE; + } else { + fprintf(stderr, "ERROR: Not understood: %s\n", arg); + exit_code = EXIT_FAILURE; + } + } + return exit_code; +} diff --git a/errnos b/errnos new file mode 100755 index 0000000..62a0429 --- /dev/null +++ b/errnos @@ -0,0 +1,6 @@ +#!/usr/bin/awk -f + +/^#define E/ { + printf "{\"%s\",%d},\n", $2, $3 +} + -- 2.39.5