diff mbox series

[V2,2/3] bsd-headers: Add error.h

Message ID 20230920220935.970957-2-raj.khem@gmail.com
State New
Headers show
Series [v2,1/3] bsd-headers: Define __CONCAT and __STRING | expand

Commit Message

Khem Raj Sept. 20, 2023, 10:09 p.m. UTC
error.h is used in many packages and we can use this to provide for
these packages and report the portability problems upstream without
patching the packages here.

Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
v2: Make Subject more relevant to change

 meta/recipes-core/musl/bsd-headers.bb      |  4 +-
 meta/recipes-core/musl/bsd-headers/error.h | 60 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 meta/recipes-core/musl/bsd-headers/error.h
diff mbox series

Patch

diff --git a/meta/recipes-core/musl/bsd-headers.bb b/meta/recipes-core/musl/bsd-headers.bb
index 887a8160313..91dc239f888 100644
--- a/meta/recipes-core/musl/bsd-headers.bb
+++ b/meta/recipes-core/musl/bsd-headers.bb
@@ -1,7 +1,7 @@ 
 # Copyright (C) 2016 Khem Raj <raj.khem@gmail.com>
 # Released under the MIT license (see COPYING.MIT for the terms)
 
-SUMMARY = "BSD compatible headers"
+SUMMARY = "Legacy compatible headers for musl libc"
 LICENSE = "BSD-3-Clause & BSD-2-Clause"
 LIC_FILES_CHKSUM = "file://sys-queue.h;beginline=1;endline=32;md5=c6352b0f03bb448600456547d334b56f"
 SECTION = "devel"
@@ -9,6 +9,7 @@  SECTION = "devel"
 SRC_URI = "file://sys-queue.h \
            file://sys-tree.h \
            file://sys-cdefs.h \
+           file://error.h \
           "
 do_configure[noexec] = "1"
 do_compile[noexec] = "1"
@@ -21,6 +22,7 @@  do_install() {
 	install -Dm 0644 ${S}/sys-queue.h ${D}${includedir}/sys/queue.h
 	install -Dm 0644 ${S}/sys-tree.h ${D}${includedir}/sys/tree.h
 	install -Dm 0644 ${S}/sys-cdefs.h ${D}${includedir}/sys/cdefs.h
+	install -Dm 0644 ${S}/error.h ${D}${includedir}/error.h
 }
 #
 # We will skip parsing for non-musl systems
diff --git a/meta/recipes-core/musl/bsd-headers/error.h b/meta/recipes-core/musl/bsd-headers/error.h
new file mode 100644
index 00000000000..9a4e1f8d006
--- /dev/null
+++ b/meta/recipes-core/musl/bsd-headers/error.h
@@ -0,0 +1,60 @@ 
+#ifndef _ERROR_H_
+#define _ERROR_H_
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#warning usage of non-standard #include <error.h> is deprecated
+
+static unsigned int error_message_count = 0;
+
+static inline void error(int status, int errnum, const char* format, ...)
+{
+	/* should be fflush(stdout), but that's unspecified if stdout has been closed;
+	 * stick with fflush(NULL) for simplicity (glibc checks if the fd is still valid) */
+	fflush(NULL);
+
+	va_list ap;
+	fprintf(stderr, "%s: ", program_invocation_name);
+	va_start(ap, format);
+	vfprintf(stderr, format, ap);
+	va_end(ap);
+	if (errnum)
+		fprintf(stderr, ": %s", strerror(errnum));
+	fprintf(stderr, "\n");
+	error_message_count++;
+	if (status)
+		exit(status);
+}
+
+static int error_one_per_line = 0;
+
+static inline void error_at_line(int status, int errnum, const char *filename,
+		unsigned int linenum, const char *format, ...)
+{
+	va_list ap;
+	if (error_one_per_line) {
+		static const char *old_filename;
+		static int old_linenum;
+		if (linenum == old_linenum && filename == old_filename)
+			return;
+		old_filename = filename;
+		old_linenum = linenum;
+	}
+	fprintf(stderr, "%s: %s:%u: ", program_invocation_name, filename, linenum);
+	va_start(ap, format);
+	vfprintf(stderr, format, ap);
+	va_end(ap);
+	if (errnum)
+		fprintf(stderr, ": %s", strerror(errnum));
+	fprintf(stderr, "\n");
+	error_message_count++;
+	if (status)
+		exit(status);
+}
+
+
+#endif	/* _ERROR_H_ */