libc: add BSD err/warn/errx/warnx interfaces
authorAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 22:22:06 +0000 (22:22 +0000)
committerAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 22:22:06 +0000 (22:22 +0000)
Library/libs/Makefile
Library/libs/__argv.c [new file with mode: 0644]
Library/libs/crt0.s
Library/libs/err.c [new file with mode: 0644]

index dd8d66a..6a6e439 100644 (file)
@@ -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 (file)
index 0000000..0ede031
--- /dev/null
@@ -0,0 +1,2 @@
+char **__argv;
+
index 0b4c2c6..a817c79 100644 (file)
@@ -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 (file)
index 0000000..f631af7
--- /dev/null
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <err.h>
+
+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);
+}
+