commit: 658d4c6b431f534627d3a6314f8ada65969f958b
parent b4aa88bb06d5c73090b560e632eb0fc48a70863a
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 30 Nov 2024 11:43:34 +0100
merge error.c into error.h
Allows to avoid needing special linking to make use of error.h
Diffstat:
M | Makefile | 4 | ++-- |
D | error.c | 88 | ------------------------------------------------------------------------------- |
M | error.h | 89 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- |
3 files changed, 86 insertions(+), 95 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,5 +3,5 @@
all:
-test-error: test-error.c error.c error.h
- ${CC} ${CFLAGS} -o test-error test-error.c error.c ${LDFLAGS} ${LDSTATIC}
+test-error: test-error.c error.h
+ ${CC} ${CFLAGS} -o test-error test-error.c ${LDFLAGS} ${LDSTATIC}
diff --git a/error.c b/error.c
@@ -1,88 +0,0 @@
-// 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,15 +8,94 @@
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
-void error(int, int, const char *, ...);
-void error_at_line(int, int, const char *, unsigned int, const char *, ...);
+#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);
+}
-extern unsigned int error_message_count;
-extern int error_one_per_line;
+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 void (*error_print_progname)(void);
+ 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);
+}
#ifdef __cplusplus
}