1From 9b9d717f484ec913cdd3804e43489b3dc18bd77c Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sat, 31 Oct 2020 22:14:05 -0700
4Subject: [PATCH] tools: Add error.h for non-glibc case
5
6error is glibc specific API, so this patch will mostly not accepted
7upstream given that elfutils has been closely tied to glibc
8
9Upstream-Status: Inappropriate [workaround for musl]
10
11Signed-off-by: Khem Raj <raj.khem@gmail.com>
12
13---
14 tools/elfdeps.c |  6 +++++-
15 tools/error.h   | 27 +++++++++++++++++++++++++++
16 2 files changed, 32 insertions(+), 1 deletion(-)
17 create mode 100644 tools/error.h
18
19diff --git a/tools/elfdeps.c b/tools/elfdeps.c
20index d205935bb..3a8945b33 100644
21--- a/tools/elfdeps.c
22+++ b/tools/elfdeps.c
23@@ -5,10 +5,14 @@
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27-#include <error.h>
28 #include <errno.h>
29 #include <popt.h>
30 #include <gelf.h>
31+#ifdef __GLIBC__
32+#include <error.h>
33+#else
34+#include "error.h"
35+#endif
36
37 #include <rpm/rpmstring.h>
38 #include <rpm/argv.h>
39diff --git a/tools/error.h b/tools/error.h
40new file mode 100644
41index 000000000..ef06827a0
42--- /dev/null
43+++ b/tools/error.h
44@@ -0,0 +1,27 @@
45+#ifndef _ERROR_H_
46+#define _ERROR_H_
47+
48+#include <stdarg.h>
49+#include <stdio.h>
50+#include <stdlib.h>
51+#include <string.h>
52+#include <errno.h>
53+
54+static unsigned int error_message_count = 0;
55+
56+static inline void error(int status, int errnum, const char* format, ...)
57+{
58+	va_list ap;
59+	fprintf(stderr, "%s: ", program_invocation_name);
60+	va_start(ap, format);
61+	vfprintf(stderr, format, ap);
62+	va_end(ap);
63+	if (errnum)
64+		fprintf(stderr, ": %s", strerror(errnum));
65+	fprintf(stderr, "\n");
66+	error_message_count++;
67+	if (status)
68+		exit(status);
69+}
70+
71+#endif	/* _ERROR_H_ */
72