blob: 1b0ff79d4274fdea2f7ce2585b98478bc4040604 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001This patch adds error() API implementation for non-glibc system C libs
2
3Upstream-Status: Pending
4Signed-off-by: Khem Raj <raj.khem@gmail.com>
5
6Index: util-linux-2.27.1/tests/helpers/test_uuidd.c
7===================================================================
8--- util-linux-2.27.1.orig/tests/helpers/test_uuidd.c
9+++ util-linux-2.27.1/tests/helpers/test_uuidd.c
10@@ -23,7 +23,6 @@
11 *
12 * make uuidd uuidgen localstatedir=/var
13 */
14-#include <error.h>
15 #include <libgen.h>
16 #include <pthread.h>
17 #include <stdio.h>
18@@ -39,6 +38,17 @@
19 #include "xalloc.h"
20 #include "strutils.h"
21
22+#ifdef __GLIBC__
23+#include <error.h>
24+#else
25+extern void (*error_print_progname)(void);
26+extern unsigned int error_message_count;
27+extern int error_one_per_line;
28+
29+void error(int, int, const char *, ...);
30+void error_at_line(int, int, const char *, unsigned int, const char *, ...);
31+#endif
32+
33 #define LOG(level,args) if (loglev >= level) { fprintf args; }
34
35 size_t nprocesses = 4;
36@@ -257,6 +267,56 @@ static void object_dump(size_t idx, obje
37 fprintf(stderr, "}\n");
38 }
39
40+#ifndef __GLIBC__
41+extern char *__progname;
42+
43+void (*error_print_progname)(void) = 0;
44+unsigned int error_message_count = 0;
45+int error_one_per_line = 0;
46+
47+static void eprint(int status, int e, const char *file, unsigned int line, const char *fmt, va_list ap)
48+{
49+ if (file && error_one_per_line) {
50+ static const char *oldfile;
51+ static unsigned int oldline;
52+ if (line == oldline && strcmp(file, oldfile) == 0)
53+ return;
54+ oldfile = file;
55+ oldline = line;
56+ }
57+ if (error_print_progname)
58+ error_print_progname();
59+ else
60+ fprintf(stderr, "%s: ", __progname);
61+ if (file)
62+ fprintf(stderr, "%s:%u: ", file, line);
63+ vfprintf(stderr, fmt, ap);
64+ if (e)
65+ fprintf(stderr, ": %s", strerror(e));
66+ putc('\n', stderr);
67+ fflush(stderr);
68+ error_message_count++;
69+ if (status)
70+ exit(status);
71+}
72+
73+void error(int status, int e, const char *fmt, ...)
74+{
75+ va_list ap;
76+ va_start(ap,fmt);
77+ eprint(status, e, 0, 0, fmt, ap);
78+ va_end(ap);
79+}
80+
81+void error_at_line(int status, int e, const char *file, unsigned int line, const char *fmt, ...)
82+{
83+ va_list ap;
84+ va_start(ap,fmt);
85+ eprint(status, e, file, line, fmt, ap);
86+ va_end(ap);
87+}
88+#endif /* __GLIBC__ */
89+
90 int main(int argc, char *argv[])
91 {
92 size_t i, nfailed = 0, nignored = 0;