| /* Copyright 2015-2024 Rivoreo |
| |
| 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 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 <stdarg.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <errno.h> |
| #include <string.h> |
| |
| static const char *err_program_name; |
| |
| static void __attribute__((__noreturn__)) err(int exit_status, const char *format, ...) { |
| va_list ap; |
| int e = errno; |
| fprintf(stderr, "%s: ", err_program_name); |
| va_start(ap, format); |
| vfprintf(stderr, format, ap); |
| va_end(ap); |
| fprintf(stderr, ": %s\n", strerror(e)); |
| exit(exit_status); |
| } |
| |
| static void __attribute__((__noreturn__)) errx(int exit_status, const char *format, ...) { |
| va_list ap; |
| fprintf(stderr, "%s: ", err_program_name); |
| va_start(ap, format); |
| vfprintf(stderr, format, ap); |
| va_end(ap); |
| fputc('\n', stderr); |
| exit(exit_status); |
| } |
| |
| static void warn(const char *format, ...) { |
| va_list ap; |
| int e = errno; |
| fprintf(stderr, "%s: ", err_program_name); |
| va_start(ap, format); |
| vfprintf(stderr, format, ap); |
| va_end(ap); |
| fprintf(stderr, ": %s\n", strerror(e)); |
| } |
| |
| static void warnx(const char *format, ...) { |
| va_list ap; |
| va_start(ap, format); |
| fprintf(stderr, "%s: ", err_program_name); |
| vfprintf(stderr, format, ap); |
| va_end(ap); |
| fputc('\n', stderr); |
| } |
| |
| static void err_set_program_name(const char *name) { |
| err_program_name = strrchr(name, '/'); |
| if(err_program_name) err_program_name++; else err_program_name = name; |
| } |
| |
| #define ERR_NEED_PROGRAM_NAME |
| |
| #endif |