]>
Commit | Line | Data |
---|---|---|
a2635361 | 1 | /* 4.4BSD utility functions for error messages. |
688903eb | 2 | Copyright (C) 1995-2018 Free Software Foundation, Inc. |
47707456 | 3 | This file is part of the GNU C Library. |
196980f5 | 4 | |
47707456 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
196980f5 | 9 | |
47707456 UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
196980f5 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
196980f5 RM |
18 | |
19 | #include <stdarg.h> | |
20 | #include <err.h> | |
21 | #include <stdlib.h> | |
755f55b0 RM |
22 | #include <errno.h> |
23 | #include <string.h> | |
196980f5 RM |
24 | #include <stdio.h> |
25 | ||
3ce1f295 UD |
26 | #include <wchar.h> |
27 | #define flockfile(s) _IO_flockfile (s) | |
28 | #define funlockfile(s) _IO_funlockfile (s) | |
50304ef0 | 29 | |
196980f5 RM |
30 | extern char *__progname; |
31 | ||
32 | #define VA(call) \ | |
33 | { \ | |
34 | va_list ap; \ | |
35 | va_start (ap, format); \ | |
36 | call; \ | |
37 | va_end (ap); \ | |
38 | } | |
39 | ||
a2635361 UD |
40 | static void |
41 | convert_and_print (const char *format, __gnuc_va_list ap) | |
42 | { | |
3ce1f295 | 43 | #define ALLOCA_LIMIT 2000 |
a2635361 UD |
44 | size_t len; |
45 | wchar_t *wformat = NULL; | |
46 | mbstate_t st; | |
47 | size_t res; | |
48 | const char *tmp; | |
49 | ||
50 | if (format == NULL) | |
51 | return; | |
52 | ||
53 | len = strlen (format) + 1; | |
54 | ||
55 | do | |
56 | { | |
57 | if (len < ALLOCA_LIMIT) | |
58 | wformat = (wchar_t *) alloca (len * sizeof (wchar_t)); | |
59 | else | |
60 | { | |
61 | if (wformat != NULL && len / 2 < ALLOCA_LIMIT) | |
62 | wformat = NULL; | |
63 | ||
64 | wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t)); | |
65 | ||
66 | if (wformat == NULL) | |
67 | { | |
34ef548a | 68 | fputws_unlocked (L"out of memory\n", stderr); |
a2635361 UD |
69 | return; |
70 | } | |
71 | } | |
72 | ||
73 | memset (&st, '\0', sizeof (st)); | |
74 | tmp =format; | |
75 | } | |
6293b803 | 76 | while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len); |
a2635361 UD |
77 | |
78 | if (res == (size_t) -1) | |
79 | /* The string cannot be converted. */ | |
80 | wformat = (wchar_t *) L"???"; | |
81 | ||
51028f34 | 82 | __vfwprintf (stderr, wformat, ap); |
a2635361 | 83 | } |
a2635361 | 84 | |
196980f5 | 85 | void |
755f55b0 | 86 | vwarnx (const char *format, __gnuc_va_list ap) |
196980f5 | 87 | { |
50304ef0 | 88 | flockfile (stderr); |
a2635361 UD |
89 | if (_IO_fwide (stderr, 0) > 0) |
90 | { | |
51028f34 | 91 | __fwprintf (stderr, L"%s: ", __progname); |
a2635361 UD |
92 | convert_and_print (format, ap); |
93 | putwc_unlocked (L'\n', stderr); | |
94 | } | |
95 | else | |
a2635361 UD |
96 | { |
97 | fprintf (stderr, "%s: ", __progname); | |
98 | if (format) | |
99 | vfprintf (stderr, format, ap); | |
100 | putc_unlocked ('\n', stderr); | |
101 | } | |
50304ef0 | 102 | funlockfile (stderr); |
196980f5 | 103 | } |
cb09a2cd | 104 | libc_hidden_def (vwarnx) |
196980f5 RM |
105 | |
106 | void | |
755f55b0 | 107 | vwarn (const char *format, __gnuc_va_list ap) |
196980f5 | 108 | { |
755f55b0 RM |
109 | int error = errno; |
110 | ||
50304ef0 | 111 | flockfile (stderr); |
a2635361 UD |
112 | if (_IO_fwide (stderr, 0) > 0) |
113 | { | |
51028f34 | 114 | __fwprintf (stderr, L"%s: ", __progname); |
a2635361 UD |
115 | if (format) |
116 | { | |
117 | convert_and_print (format, ap); | |
118 | fputws_unlocked (L": ", stderr); | |
119 | } | |
120 | __set_errno (error); | |
51028f34 | 121 | __fwprintf (stderr, L"%m\n"); |
a2635361 UD |
122 | } |
123 | else | |
755f55b0 | 124 | { |
a2635361 UD |
125 | fprintf (stderr, "%s: ", __progname); |
126 | if (format) | |
127 | { | |
128 | vfprintf (stderr, format, ap); | |
129 | fputs_unlocked (": ", stderr); | |
130 | } | |
131 | __set_errno (error); | |
132 | fprintf (stderr, "%m\n"); | |
755f55b0 | 133 | } |
50304ef0 | 134 | funlockfile (stderr); |
196980f5 | 135 | } |
df962917 | 136 | libc_hidden_def (vwarn) |
196980f5 RM |
137 | |
138 | ||
139 | void | |
140 | warn (const char *format, ...) | |
141 | { | |
142 | VA (vwarn (format, ap)) | |
143 | } | |
a757607d | 144 | libc_hidden_def (warn) |
196980f5 RM |
145 | |
146 | void | |
147 | warnx (const char *format, ...) | |
148 | { | |
149 | VA (vwarnx (format, ap)) | |
150 | } | |
a757607d | 151 | libc_hidden_def (warnx) |
196980f5 RM |
152 | |
153 | void | |
154 | verr (int status, const char *format, __gnuc_va_list ap) | |
155 | { | |
156 | vwarn (format, ap); | |
157 | exit (status); | |
158 | } | |
a757607d | 159 | libc_hidden_def (verr) |
196980f5 RM |
160 | |
161 | void | |
162 | verrx (int status, const char *format, __gnuc_va_list ap) | |
163 | { | |
164 | vwarnx (format, ap); | |
165 | exit (status); | |
166 | } | |
a757607d | 167 | libc_hidden_def (verrx) |
196980f5 RM |
168 | |
169 | void | |
170 | err (int status, const char *format, ...) | |
171 | { | |
172 | VA (verr (status, format, ap)) | |
173 | } | |
174 | ||
175 | void | |
176 | errx (int status, const char *format, ...) | |
177 | { | |
178 | VA (verrx (status, format, ap)) | |
179 | } |