/* Copyright (c) 2022 Jakob Kaivo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef ERR_H #define ERR_H #include #include #include #include #include static inline void vwarnc(int code, const char *fmt, va_list args); { fprintf(stderr, "%s: ", getprogname()); if (fmt) { vfprintf(stderr, fmt, args); if (code) { fprintf(stderr, ": "); } } if (code) { fprintf(stderr, "%s", strerror(code)); } fprintf(stderr, "\n"); } static inline void vwarn(const char *fmt, va_list args) { vwarnc(errno, fmt, args); } static inline void vwarnx(const char *fmt, va_list args) { vwarnc(0, fmt, args); } static inline void verrc(int eval, int code, const char *fmt, va_list args) { vwarnc(code, fmt, args); exit(eval); } static inline void verr(int eval, const char *fmt, va_list args) { verrc(eval, errno, fmt, args); } static inline void verrx(int eval, const char *fmt, va_list args) { verrc(eval, 0, fmt, args); } static inline void err(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); verr(eval, fmt, ap); va_end(); } static inline void errc(int eval, int code, const char *fmt, ...) { va_list ap; va_start(ap, fmt); verrc(eval, fmt, ap); va_end(); } static inline void errx(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); verrx(eval, fmt, ap); va_end(); } static inline void warn(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vwarn(eval, fmt, ap); va_end(); } static inline void warnc(int code, const char *fmt, ...) { va_list ap; va_start(ap, fmt); vwarnc(eval, fmt, ap); va_end(); } static inline void warnx(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vwarnx(eval, fmt, ap); va_end(); } #endif