scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit f769c4f5a9b0c0a59fb1b9093b3def0d628897db
parent fb8066c837ba6dc3da72fdb4d687e6b4f5ec025b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  7 Dec 2017 09:07:39 +0000

[lib/c] Add perror() and strerror()

Diffstat:
Mlib/c/include/errno.h | 2++
Mlib/c/src/Makefile | 1+
Alib/c/src/perror.c | 17+++++++++++++++++
Alib/c/src/strerror.c | 13+++++++++++++
4 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/lib/c/include/errno.h b/lib/c/include/errno.h @@ -8,5 +8,7 @@ #define EBADF 5 extern int errno; +extern char *_sys_errlist[]; +extern int _sys_nerr; #endif diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -4,6 +4,7 @@ include ../../../config.mk OBJ = bsearch.o \ abs.o __abs.o labs.o __labs.o llabs.o __labs.o \ + perror.o strerror.o \ printf.o fprintf.o vfprintf.o \ fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \ fputs.o puts.o fread.o fwrite.o \ diff --git a/lib/c/src/perror.c b/lib/c/src/perror.c @@ -0,0 +1,17 @@ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#undef perror + +void +perror(const char *msg) +{ + if (msg && *msg) { + fputs(msg, stderr); + putc(':', stderr); + putc(' ', stderr); + } + fputs(strerror(errno), stderr); + putc('\n', stderr); +} diff --git a/lib/c/src/strerror.c b/lib/c/src/strerror.c @@ -0,0 +1,13 @@ + +#include <errno.h> +#include <string.h> +#undef strerror + +char * +strerror(int errnum) +{ + if (errnum < _sys_nerr) + return _sys_errlist[errnum]; + else + return "Unknown error"; +}