From f1571ebaf4b38bdf9ebf7f2e9369ead538cd3c28 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 10 Jun 2015 23:12:59 +0200 Subject: [PATCH] Add err(3). --- libc/Makefile | 8 +++++++ libc/err/err.cpp | 34 +++++++++++++++++++++++++++ libc/err/errx.cpp | 34 +++++++++++++++++++++++++++ libc/err/verr.cpp | 33 ++++++++++++++++++++++++++ libc/err/verrx.cpp | 33 ++++++++++++++++++++++++++ libc/err/vwarn.cpp | 43 ++++++++++++++++++++++++++++++++++ libc/err/vwarnx.cpp | 41 ++++++++++++++++++++++++++++++++ libc/err/warn.cpp | 34 +++++++++++++++++++++++++++ libc/err/warnx.cpp | 34 +++++++++++++++++++++++++++ libc/include/err.h | 57 +++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 351 insertions(+) create mode 100644 libc/err/err.cpp create mode 100644 libc/err/errx.cpp create mode 100644 libc/err/verr.cpp create mode 100644 libc/err/verrx.cpp create mode 100644 libc/err/vwarn.cpp create mode 100644 libc/err/vwarnx.cpp create mode 100644 libc/err/warn.cpp create mode 100644 libc/err/warnx.cpp create mode 100644 libc/include/err.h diff --git a/libc/Makefile b/libc/Makefile index 75743d1a..59b1d496 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -323,7 +323,15 @@ dirent/fdopendir.o \ dirent/opendir.o \ dirent/scandir.o \ dlfcn/dlfcn.o \ +err/err.o \ +err/errx.o \ error/gnu_error.o \ +err/verr.o \ +err/verrx.o \ +err/vwarn.o \ +err/vwarnx.o \ +err/warn.o \ +err/warnx.o \ fcntl/creat.o \ fcntl/fcntl.o \ fcntl/openat.o \ diff --git a/libc/err/err.cpp b/libc/err/err.cpp new file mode 100644 index 00000000..2f8e2593 --- /dev/null +++ b/libc/err/err.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/err.cpp + Print an error message to stderr and exit the process. + +*******************************************************************************/ + +#include +#include + +extern "C" void err(int exitcode, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verr(exitcode, fmt, ap); + va_end(ap); +} diff --git a/libc/err/errx.cpp b/libc/err/errx.cpp new file mode 100644 index 00000000..5b1abbad --- /dev/null +++ b/libc/err/errx.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/errx.cpp + Print an error message to stderr and exit the process. + +*******************************************************************************/ + +#include +#include + +extern "C" void errx(int exitcode, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + verrx(exitcode, fmt, ap); + va_end(ap); +} diff --git a/libc/err/verr.cpp b/libc/err/verr.cpp new file mode 100644 index 00000000..83a88478 --- /dev/null +++ b/libc/err/verr.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/verr.cpp + Print an error message to stderr and exit the process. + +*******************************************************************************/ + +#include +#include +#include + +extern "C" void verr(int exitcode, const char* fmt, va_list ap) +{ + vwarn(fmt, ap); + exit(exitcode); +} diff --git a/libc/err/verrx.cpp b/libc/err/verrx.cpp new file mode 100644 index 00000000..e9af7675 --- /dev/null +++ b/libc/err/verrx.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/verrx.cpp + Print an error message to stderr and exit the process. + +*******************************************************************************/ + +#include +#include +#include + +extern "C" void verrx(int exitcode, const char* fmt, va_list ap) +{ + vwarnx(fmt, ap); + exit(exitcode); +} diff --git a/libc/err/vwarn.cpp b/libc/err/vwarn.cpp new file mode 100644 index 00000000..ea231725 --- /dev/null +++ b/libc/err/vwarn.cpp @@ -0,0 +1,43 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/vwarn.cpp + Print an error message to stderr. + +*******************************************************************************/ + +#include +#include +#include +#include +#include + +extern "C" 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); +} diff --git a/libc/err/vwarnx.cpp b/libc/err/vwarnx.cpp new file mode 100644 index 00000000..328b3bb8 --- /dev/null +++ b/libc/err/vwarnx.cpp @@ -0,0 +1,41 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/vwarnx.cpp + Print an error message to stderr. + +*******************************************************************************/ + +#include +#include +#include +#include + +extern "C" void vwarnx(const char* fmt, va_list ap) +{ + flockfile(stderr); + fprintf_unlocked(stderr, "%s", program_invocation_name); + if ( fmt ) + { + fputs_unlocked(": ", stderr); + vfprintf_unlocked(stderr, fmt, ap); + } + fputc_unlocked('\n', stderr); + funlockfile(stderr); +} diff --git a/libc/err/warn.cpp b/libc/err/warn.cpp new file mode 100644 index 00000000..a3c004a8 --- /dev/null +++ b/libc/err/warn.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/warn.cpp + Print an error message to stderr. + +*******************************************************************************/ + +#include +#include + +extern "C" void warn(const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarn(fmt, ap); + va_end(ap); +} diff --git a/libc/err/warnx.cpp b/libc/err/warnx.cpp new file mode 100644 index 00000000..974def1c --- /dev/null +++ b/libc/err/warnx.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err/warnx.cpp + Print an error message to stderr. + +*******************************************************************************/ + +#include +#include + +extern "C" void warnx(const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnx(fmt, ap); + va_end(ap); +} diff --git a/libc/include/err.h b/libc/include/err.h new file mode 100644 index 00000000..a4c97938 --- /dev/null +++ b/libc/include/err.h @@ -0,0 +1,57 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2015. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + err.h + Error reporting functions. + +*******************************************************************************/ + +#ifndef INCLUDE_ERR_H +#define INCLUDE_ERR_H + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((__noreturn__, __format__(__printf__, 2, 3))) +void err(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__, 2, 0))) +void verrx(int, const char*, va_list); +__attribute__((__format__(__printf__, 1, 2))) +void warn(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__, 1, 0))) +void vwarnx(const char*, va_list); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif