iconv mapping of 0xA8 0xEC in CP1258 is non-canonical
[glibc.git] / misc / err.c
blob2a15bac08dc6cbf9af218d1c2f7f48a2453a81a8
1 /* 4.4BSD utility functions for error messages.
2 Copyright (C) 1995,1996,1998,2001,2002,2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdarg.h>
21 #include <err.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdio.h>
27 #include <wchar.h>
28 #define flockfile(s) _IO_flockfile (s)
29 #define funlockfile(s) _IO_funlockfile (s)
31 extern char *__progname;
33 #define VA(call) \
34 { \
35 va_list ap; \
36 va_start (ap, format); \
37 call; \
38 va_end (ap); \
41 static void
42 convert_and_print (const char *format, __gnuc_va_list ap)
44 #define ALLOCA_LIMIT 2000
45 size_t len;
46 wchar_t *wformat = NULL;
47 mbstate_t st;
48 size_t res;
49 const char *tmp;
51 if (format == NULL)
52 return;
54 len = strlen (format) + 1;
58 if (len < ALLOCA_LIMIT)
59 wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
60 else
62 if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
63 wformat = NULL;
65 wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
67 if (wformat == NULL)
69 fputws_unlocked (L"out of memory\n", stderr);
70 return;
74 memset (&st, '\0', sizeof (st));
75 tmp =format;
77 while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
79 if (res == (size_t) -1)
80 /* The string cannot be converted. */
81 wformat = (wchar_t *) L"???";
83 __vfwprintf (stderr, wformat, ap);
86 void
87 vwarnx (const char *format, __gnuc_va_list ap)
89 flockfile (stderr);
90 if (_IO_fwide (stderr, 0) > 0)
92 __fwprintf (stderr, L"%s: ", __progname);
93 convert_and_print (format, ap);
94 putwc_unlocked (L'\n', stderr);
96 else
98 fprintf (stderr, "%s: ", __progname);
99 if (format)
100 vfprintf (stderr, format, ap);
101 putc_unlocked ('\n', stderr);
103 funlockfile (stderr);
105 libc_hidden_def (vwarnx)
107 void
108 vwarn (const char *format, __gnuc_va_list ap)
110 int error = errno;
112 flockfile (stderr);
113 if (_IO_fwide (stderr, 0) > 0)
115 __fwprintf (stderr, L"%s: ", __progname);
116 if (format)
118 convert_and_print (format, ap);
119 fputws_unlocked (L": ", stderr);
121 __set_errno (error);
122 __fwprintf (stderr, L"%m\n");
124 else
126 fprintf (stderr, "%s: ", __progname);
127 if (format)
129 vfprintf (stderr, format, ap);
130 fputs_unlocked (": ", stderr);
132 __set_errno (error);
133 fprintf (stderr, "%m\n");
135 funlockfile (stderr);
137 libc_hidden_def (vwarn)
140 void
141 warn (const char *format, ...)
143 VA (vwarn (format, ap))
145 libc_hidden_def (warn)
147 void
148 warnx (const char *format, ...)
150 VA (vwarnx (format, ap))
152 libc_hidden_def (warnx)
154 void
155 verr (int status, const char *format, __gnuc_va_list ap)
157 vwarn (format, ap);
158 exit (status);
160 libc_hidden_def (verr)
162 void
163 verrx (int status, const char *format, __gnuc_va_list ap)
165 vwarnx (format, ap);
166 exit (status);
168 libc_hidden_def (verrx)
170 void
171 err (int status, const char *format, ...)
173 VA (verr (status, format, ap))
176 void
177 errx (int status, const char *format, ...)
179 VA (verrx (status, format, ap))