1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
2 *
3 * GNU Library General Public License (LGPL) version 2 or later.
4 *
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <errno.h>
13 #include <err.h>
14
15 #ifdef __UCLIBC_MJN3_ONLY__
16 #warning REMINDER: Deal with wide oriented stderr case.
17 #endif
18
19 #if defined __USE_BSD
20
21
22
vwarn_work(const char * format,va_list args,int showerr)23 static void vwarn_work(const char *format, va_list args, int showerr)
24 {
25 /* 0123 45678 9 a b*/
26 static const char fmt[] = "%s: \0: %s\n\0\n";
27 const char *f;
28 char buf[64];
29 __STDIO_AUTO_THREADLOCK_VAR;
30
31 /* Do this first, in case something below changes errno. */
32 f = fmt + 11; /* At 11. */
33 if (showerr) {
34 f -= 4; /* At 7. */
35 __xpg_strerror_r(errno, buf, sizeof(buf));
36 }
37
38 __STDIO_AUTO_THREADLOCK(stderr);
39
40 fprintf(stderr, fmt, __uclibc_progname);
41 if (format) {
42 vfprintf(stderr, format, args);
43 f -= 2; /* At 5 (showerr) or 9. */
44 }
45 fprintf(stderr, f, buf);
46
47 __STDIO_AUTO_THREADUNLOCK(stderr);
48 }
49
__vwarn(const char * format,va_list args)50 static void __vwarn(const char *format, va_list args)
51 {
52 vwarn_work(format, args, 1);
53 }
strong_alias(__vwarn,vwarn)54 strong_alias(__vwarn,vwarn)
55
56 void warn(const char *format, ...)
57 {
58 va_list args;
59
60 va_start(args, format);
61 __vwarn(format, args);
62 va_end(args);
63 }
64
__vwarnx(const char * format,va_list args)65 static void __vwarnx(const char *format, va_list args)
66 {
67 vwarn_work(format, args, 0);
68 }
strong_alias(__vwarnx,vwarnx)69 strong_alias(__vwarnx,vwarnx)
70
71 void warnx(const char *format, ...)
72 {
73 va_list args;
74
75 va_start(args, format);
76 __vwarnx(format, args);
77 va_end(args);
78 }
79
__verr(int status,const char * format,va_list args)80 static void attribute_noreturn __verr(int status, const char *format, va_list args)
81 {
82 __vwarn(format, args);
83 exit(status);
84 }
strong_alias(__verr,verr)85 strong_alias(__verr,verr)
86
87 void err(int status, const char *format, ...)
88 {
89 va_list args;
90
91 va_start(args, format);
92 __verr(status, format, args);
93 /* This should get optimized away. We'll leave it now for safety. */
94 /* The loop is added only to keep gcc happy. */
95 while(1)
96 va_end(args);
97 }
98
__verrx(int status,const char * format,va_list args)99 static void attribute_noreturn __verrx(int status, const char *format, va_list args)
100 {
101 __vwarnx(format, args);
102 exit(status);
103 }
strong_alias(__verrx,verrx)104 strong_alias(__verrx,verrx)
105
106 void errx(int status, const char *format, ...)
107 {
108 va_list args;
109
110 va_start(args, format);
111 __verrx(status, format, args);
112 /* This should get optimized away. We'll leave it now for safety. */
113 /* The loop is added only to keep gcc happy. */
114 while(1)
115 va_end(args);
116 }
117 #endif
118