From ece57cc509cce67000407c2ce49c62b5cef3e8a0 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Fri, 2 Jan 2015 22:22:06 +0000 Subject: [PATCH] libc: add BSD err/warn/errx/warnx interfaces --- Library/libs/Makefile | 7 +++-- Library/libs/__argv.c | 2 ++ Library/libs/crt0.s | 6 ++++ Library/libs/err.c | 71 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 Library/libs/__argv.c create mode 100644 Library/libs/err.c diff --git a/Library/libs/Makefile b/Library/libs/Makefile index dd8d66a5..6a6e439a 100644 --- a/Library/libs/Makefile +++ b/Library/libs/Makefile @@ -12,11 +12,12 @@ SRC_CRT0 = crt0.s OBJ_CRT0 = $(SRC_CRT0:.s=.rel) SRC_ASM = OBJ_ASM = $(SRC_ASM:.s=.rel) -SRC_C = abort.c asctime.c assert.c atexit.c +SRC_C = __argv.c abort.c asctime.c assert.c atexit.c SRC_C += bcmp.c bcopy.c bsearch.c bzero.c calloc.c cfree.c clock.c closedir.c SRC_C += clock_gettime.c clock_getres.c clock_settime.c -SRC_C += creat.c crypt.c ctime.c ctype.c difftime.c errno.c error.c execl.c execv.c -SRC_C += execvp.c exit.c fclose.c fflush.c fgetc.c fgetgrent.c fgetpwent.c +SRC_C += creat.c crypt.c ctime.c ctype.c difftime.c err.c errno.c error.c +SRC_C += execl.c execv.c execvp.c exit.c +SRC_C += fclose.c fflush.c fgetc.c fgetgrent.c fgetpwent.c SRC_C += fgetpos.c fgets.c fopen.c fprintf.c fputc.c fputs.c fread.c free.c SRC_C += fsetpos.c ftell.c fwrite.c getcwd.c SRC_C += getenv.c __getgrent.c getgrgid.c getgrnam.c getopt.c diff --git a/Library/libs/__argv.c b/Library/libs/__argv.c new file mode 100644 index 00000000..0ede031a --- /dev/null +++ b/Library/libs/__argv.c @@ -0,0 +1,2 @@ +char **__argv; + diff --git a/Library/libs/crt0.s b/Library/libs/crt0.s index 0b4c2c6f..a817c79a 100644 --- a/Library/libs/crt0.s +++ b/Library/libs/crt0.s @@ -19,6 +19,7 @@ .globl _main .globl _exit .globl _environ + .globl ___argv .globl s__DATA .globl l__DATA @@ -62,6 +63,11 @@ start2: ld hl, #l__DATA - 1 ; work around linker limit ld hl, #4 add hl, sp ld (_environ), hl + pop de ; argc + pop hl ; argv + push hl + ld (___argv), hl ; needed for stuff like err() + push de ld hl, #_exit ; return vector push hl jp _main ; go diff --git a/Library/libs/err.c b/Library/libs/err.c new file mode 100644 index 00000000..f631af79 --- /dev/null +++ b/Library/libs/err.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + +extern char **__argv; + +static void _vdo(int flags, const char *fmt, va_list args) +{ + fprintf(stderr, "%s", __argv[0]); + if (fmt) { + vfprintf(stderr, fmt, args); + if (flags & 1) + fprintf(stderr, ": %s", strerror(errno)); + fputc('\n', stderr); + } +} + +void verr(int eval, const char *fmt, va_list args) +{ + _vdo(1, fmt, args); + exit(eval); +} + +void verrx(int eval, const char *fmt, va_list args) +{ + _vdo(0, fmt, args); + exit(eval); +} + +void vwarn(const char *fmt, va_list args) +{ + _vdo(1, fmt, args); +} + +void vwarnx(const char *fmt, va_list args) +{ + _vdo(0, fmt, args); +} + +void err(int eval, const char *fmt, ...) +{ + va_list ptr; + va_start(ptr, fmt); + _vdo(1, fmt, ptr); + exit(eval); +} + +void errx(int eval, const char *fmt, ...) +{ + va_list ptr; + va_start(ptr, fmt); + _vdo(0, fmt, ptr); + exit(eval); +} + +void warn(const char *fmt, ...) +{ + va_list ptr; + va_start(ptr, fmt); + _vdo(1, fmt, ptr); +} + +void warnx(const char *fmt, ...) +{ + va_list ptr; + va_start(ptr, fmt); + _vdo(0, fmt, ptr); +} + -- 2.34.1