From a942b15f9195a5449e12a4a8fed7f3e6d81ad38e Mon Sep 17 00:00:00 2001 From: Nicholas De Nova Date: Sun, 6 Nov 2016 15:31:52 -0600 Subject: [PATCH] Add errc(3) family. --- libc/Makefile | 4 ++++ libc/err/errc.c | 29 +++++++++++++++++++++++++++++ libc/err/verrc.c | 28 ++++++++++++++++++++++++++++ libc/err/vwarn.c | 11 +---------- libc/err/vwarnc.c | 37 +++++++++++++++++++++++++++++++++++++ libc/err/warnc.c | 29 +++++++++++++++++++++++++++++ libc/include/err.h | 8 ++++++++ 7 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 libc/err/errc.c create mode 100644 libc/err/verrc.c create mode 100644 libc/err/vwarnc.c create mode 100644 libc/err/warnc.c diff --git a/libc/Makefile b/libc/Makefile index 14f9cbc8..0edd038c 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -331,13 +331,17 @@ dirent/rewinddir.o \ dirent/scandir.o \ dlfcn/dlfcn.o \ err/err.o \ +err/errc.o \ err/errx.o \ error/gnu_error.o \ err/verr.o \ +err/verrc.o \ err/verrx.o \ err/vwarn.o \ +err/vwarnc.o \ err/vwarnx.o \ err/warn.o \ +err/warnc.o \ err/warnx.o \ fcntl/creat.o \ fcntl/fcntl.o \ diff --git a/libc/err/errc.c b/libc/err/errc.c new file mode 100644 index 00000000..70b65e84 --- /dev/null +++ b/libc/err/errc.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016 Nicholas De Nova. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * err/errc.c + * Print an error message to stderr. + */ + +#include +#include + +void errc(int exitcode, int errnum, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrc(exitcode, errnum, fmt, ap); + va_end(ap); +} diff --git a/libc/err/verrc.c b/libc/err/verrc.c new file mode 100644 index 00000000..812ace59 --- /dev/null +++ b/libc/err/verrc.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016 Nicholas De Nova. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * err/verrc.c + * Print an error message to stderr. + */ + +#include +#include +#include + +void verrc(int exitcode, int errnum, const char* fmt, va_list ap) +{ + vwarnc(errnum, fmt, ap); + exit(exitcode); +} diff --git a/libc/err/vwarn.c b/libc/err/vwarn.c index f728fc22..3a17460c 100644 --- a/libc/err/vwarn.c +++ b/libc/err/vwarn.c @@ -25,14 +25,5 @@ void vwarn(const char* fmt, va_list ap) { - int errnum = errno; - flockfile(stderr); - fprintf_unlocked(stderr, "%s: ", program_invocation_name); - if ( fmt ) - { - vfprintf_unlocked(stderr, fmt, ap); - fputs_unlocked(": ", stderr); - } - fprintf_unlocked(stderr, "%s\n", strerror(errnum)); - funlockfile(stderr); + vwarnc(errno, fmt, ap); } diff --git a/libc/err/vwarnc.c b/libc/err/vwarnc.c new file mode 100644 index 00000000..e6ad19f5 --- /dev/null +++ b/libc/err/vwarnc.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * err/vwarnc.c + * Print an error message to stderr. + */ + +#include +#include +#include +#include +#include + +void vwarnc(int errnum, const char* fmt, va_list ap) +{ + flockfile(stderr); + fprintf_unlocked(stderr, "%s: ", program_invocation_name); + if ( fmt ) + { + vfprintf_unlocked(stderr, fmt, ap); + fputs_unlocked(": ", stderr); + } + fprintf_unlocked(stderr, "%s\n", strerror(errnum)); + funlockfile(stderr); +} diff --git a/libc/err/warnc.c b/libc/err/warnc.c new file mode 100644 index 00000000..67f26c45 --- /dev/null +++ b/libc/err/warnc.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2016 Nicholas De Nova. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * err/warnc.c + * Print an error message to stderr. + */ + +#include +#include + +void warnc(int errnum, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(errnum, fmt, ap); + va_end(ap); +} diff --git a/libc/include/err.h b/libc/include/err.h index b5c51750..c5c365e8 100644 --- a/libc/include/err.h +++ b/libc/include/err.h @@ -30,18 +30,26 @@ extern "C" { __attribute__((__noreturn__, __format__(__printf__, 2, 3))) void err(int, const char*, ...); +__attribute__((__noreturn__, __format__(__printf__, 3, 4))) +void errc(int, int, const char*, ...); __attribute__((__noreturn__, __format__(__printf__, 2, 3))) void errx(int, const char*, ...); __attribute__((__noreturn__, __format__(__printf__, 2, 0))) void verr(int, const char*, va_list); +__attribute__((__noreturn__, __format__(__printf__, 3, 0))) +void verrc(int, int, const char*, va_list); __attribute__((__noreturn__, __format__(__printf__, 2, 0))) void verrx(int, const char*, va_list); __attribute__((__format__(__printf__, 1, 2))) void warn(const char*, ...); +__attribute__((__format__(__printf__, 2, 3))) +void warnc(int, const char*, ...); __attribute__((__format__(__printf__, 1, 2))) void warnx(const char*, ...); __attribute__((__format__(__printf__, 1, 0))) void vwarn(const char*, va_list); +__attribute__((__format__(__printf__, 2, 0))) +void vwarnc(int, const char*, va_list); __attribute__((__format__(__printf__, 1, 0))) void vwarnx(const char*, va_list);