blob: 9ddb5962429c9376b9f726a022af3214d811af85 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* 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#ifdef __UCLIBC_HAS_THREADS__
15#include <pthread.h>
16#endif
17
18#ifdef __UCLIBC_MJN3_ONLY__
19#warning REMINDER: Deal with wide oriented stderr case.
20#endif
21
22#if defined __USE_BSD
23
24
25
26static void vwarn_work(const char *format, va_list args, int showerr)
27{
28 /* 0123 45678 9 a b*/
29 static const char fmt[] = "%s: \0: %s\n\0\n";
30 const char *f;
31 char buf[64];
32 __STDIO_AUTO_THREADLOCK_VAR;
33
34 /* Do this first, in case something below changes errno. */
35 f = fmt + 11; /* At 11. */
36 if (showerr) {
37 f -= 4; /* At 7. */
38 __xpg_strerror_r(errno, buf, sizeof(buf));
39 }
40
41 __STDIO_AUTO_THREADLOCK(stderr);
42
43 fprintf(stderr, fmt, __uclibc_progname);
44 if (format) {
45 vfprintf(stderr, format, args);
46 f -= 2; /* At 5 (showerr) or 9. */
47 }
48 fprintf(stderr, f, buf);
49
50 __STDIO_AUTO_THREADUNLOCK(stderr);
51}
52
53void vwarn(const char *format, va_list args)
54{
55 vwarn_work(format, args, 1);
56}
57libc_hidden_def(vwarn)
58
59void warn(const char *format, ...)
60{
61 va_list args;
62
63 va_start(args, format);
64 vwarn(format, args);
65 va_end(args);
66}
67
68void vwarnx(const char *format, va_list args)
69{
70 vwarn_work(format, args, 0);
71}
72libc_hidden_def(vwarnx)
73
74void warnx(const char *format, ...)
75{
76 va_list args;
77
78 va_start(args, format);
79 vwarnx(format, args);
80 va_end(args);
81}
82
83void verr(int status, const char *format, va_list args)
84{
85 vwarn(format, args);
86 exit(status);
87}
88libc_hidden_def(verr)
89
90void attribute_noreturn err(int status, const char *format, ...)
91{
92 va_list args;
93
94 va_start(args, format);
95 verr(status, format, args);
96 /* This should get optimized away. We'll leave it now for safety. */
97 /* The loop is added only to keep gcc happy. */
98 while(1)
99 va_end(args);
100}
101
102void verrx(int status, const char *format, va_list args)
103{
104 vwarnx(format, args);
105 exit(status);
106}
107libc_hidden_def(verrx)
108
109void attribute_noreturn errx(int status, const char *format, ...)
110{
111 va_list args;
112
113 va_start(args, format);
114 verrx(status, format, args);
115 /* This should get optimized away. We'll leave it now for safety. */
116 /* The loop is added only to keep gcc happy. */
117 while(1)
118 va_end(args);
119}
120#endif