logo

error-standalone

standalone <error.h> implementation intended for muslgit clone https://anongit.hacktivis.me/git/error-standalone.git/
commit: 3e5e732a72105103a5ab75b9c34131bb4fa89f7a
parent 39409c313b84b8adb4163a380140442a0228de8f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 14 Dec 2024 05:27:15 +0100

Revert "merge error.c into error.h"

This reverts commit 658d4c6b431f534627d3a6314f8ada65969f958b.

Diffstat:

MMakefile4++--
Aerror.c88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Merror.h89+++++--------------------------------------------------------------------------
3 files changed, 95 insertions(+), 86 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,8 +5,8 @@ PREFIX ?= /usr all: -test-error: test-error.c error.h - ${CC} ${CFLAGS} -o test-error test-error.c ${LDFLAGS} ${LDSTATIC} +test-error: test-error.c error.c error.h + ${CC} ${CFLAGS} -o test-error test-error.c error.c ${LDFLAGS} ${LDSTATIC} install: mkdir -p ${DESTDIR}${PREFIX}/include diff --git a/error.c b/error.c @@ -0,0 +1,88 @@ +// Copyright © 2024 Haelwenn (lanodan) Monnier <contact@hacktivis.me> +// SPDX-License-Identifier: MIT +#define _GNU_SOURCE // program_invocation_name + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "error.h" + +unsigned int error_message_count = 0; +void (*error_print_progname)(void) = NULL; +int error_one_per_line = 0; + +void +error(int status, int errnum, const char *format, ...) +{ + fflush(stdout); + + if(error_print_progname != NULL) + { + (*error_print_progname) (); + } + else + { + fprintf(stderr, "%s:", program_invocation_name); + } + + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + + if(errnum != 0) + fprintf(stderr, ": %s", strerror(errnum)); + + putc('\n', stderr); + + error_message_count++; + + if(status != 0) exit(status); +} + +void +error_at_line(int status, int errnum, const char *filename, unsigned int linenum, const char *format, ...) +{ + if(error_one_per_line != 0) + { + static const char *prev_filename; + static unsigned int prev_linenum; + + if(prev_linenum == linenum && + (prev_filename == filename || strcmp(prev_filename, filename) == 0) + ) return; // already printed, skip + + prev_filename = filename; + prev_linenum = linenum; + } + + fflush(stdout); + + if(error_print_progname != NULL) + { + (*error_print_progname) (); + } + else + { + fprintf(stderr, "%s:", program_invocation_name); + } + + fprintf(stderr, " %s:%d: ", filename, linenum); + + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + + if(errnum != 0) + fprintf(stderr, ": %s", strerror(errnum)); + + putc('\n', stderr); + + error_message_count++; + + if(status != 0) exit(status); +} diff --git a/error.h b/error.h @@ -8,94 +8,15 @@ extern "C" { #endif -#ifndef _GNU_SOURCE -#warning _GNU_SOURCE is undefined but error()/error_at_line() relies on program_invocation_name -#endif - #warning usage of non-standard #include <error.h> is deprecated -#include <errno.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -static unsigned int error_message_count = 0; -static void (*error_print_progname)(void) = NULL; -static int error_one_per_line = 0; - -static void -error(int status, int errnum, const char *format, ...) -{ - fflush(stdout); - - if(error_print_progname != NULL) - { - (*error_print_progname) (); - } - else - { - fprintf(stderr, "%s:", program_invocation_name); - } - - va_list args; - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - if(errnum != 0) - fprintf(stderr, ": %s", strerror(errnum)); - - putc('\n', stderr); - - error_message_count++; - - if(status != 0) exit(status); -} +void error(int, int, const char *, ...); +void error_at_line(int, int, const char *, unsigned int, const char *, ...); -static void -error_at_line(int status, int errnum, const char *filename, unsigned int linenum, const char *format, ...) -{ - if(error_one_per_line != 0) - { - static const char *prev_filename; - static unsigned int prev_linenum; +extern unsigned int error_message_count; +extern int error_one_per_line; - if(prev_linenum == linenum && - (prev_filename == filename || strcmp(prev_filename, filename) == 0) - ) return; // already printed, skip - - prev_filename = filename; - prev_linenum = linenum; - } - - fflush(stdout); - - if(error_print_progname != NULL) - { - (*error_print_progname) (); - } - else - { - fprintf(stderr, "%s:", program_invocation_name); - } - - fprintf(stderr, " %s:%d: ", filename, linenum); - - va_list args; - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - if(errnum != 0) - fprintf(stderr, ": %s", strerror(errnum)); - - putc('\n', stderr); - - error_message_count++; - - if(status != 0) exit(status); -} +extern void (*error_print_progname)(void); #ifdef __cplusplus }