Add comment to clarify how the test can fail
[glibc.git] / misc / err.c
blob7b98157d59871b07453c1ceac5dd8d8358ecae9a
1 /* 4.4BSD utility functions for error messages.
2 Copyright (C) 1995-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdarg.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
26 #include <wchar.h>
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
30 extern char *__progname;
32 #define VA(call) \
33 { \
34 va_list ap; \
35 va_start (ap, format); \
36 call; \
37 va_end (ap); \
40 static void
41 convert_and_print (const char *format, __gnuc_va_list ap)
43 #define ALLOCA_LIMIT 2000
44 size_t len;
45 wchar_t *wformat = NULL;
46 mbstate_t st;
47 size_t res;
48 const char *tmp;
50 if (format == NULL)
51 return;
53 len = strlen (format) + 1;
57 if (len < ALLOCA_LIMIT)
58 wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
59 else
61 if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
62 wformat = NULL;
64 wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
66 if (wformat == NULL)
68 fputws_unlocked (L"out of memory\n", stderr);
69 return;
73 memset (&st, '\0', sizeof (st));
74 tmp =format;
76 while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
78 if (res == (size_t) -1)
79 /* The string cannot be converted. */
80 wformat = (wchar_t *) L"???";
82 __vfwprintf (stderr, wformat, ap);
85 void
86 vwarnx (const char *format, __gnuc_va_list ap)
88 flockfile (stderr);
89 if (_IO_fwide (stderr, 0) > 0)
91 __fwprintf (stderr, L"%s: ", __progname);
92 convert_and_print (format, ap);
93 putwc_unlocked (L'\n', stderr);
95 else
97 fprintf (stderr, "%s: ", __progname);
98 if (format)
99 vfprintf (stderr, format, ap);
100 putc_unlocked ('\n', stderr);
102 funlockfile (stderr);
104 libc_hidden_def (vwarnx)
106 void
107 vwarn (const char *format, __gnuc_va_list ap)
109 int error = errno;
111 flockfile (stderr);
112 if (_IO_fwide (stderr, 0) > 0)
114 __fwprintf (stderr, L"%s: ", __progname);
115 if (format)
117 convert_and_print (format, ap);
118 fputws_unlocked (L": ", stderr);
120 __set_errno (error);
121 __fwprintf (stderr, L"%m\n");
123 else
125 fprintf (stderr, "%s: ", __progname);
126 if (format)
128 vfprintf (stderr, format, ap);
129 fputs_unlocked (": ", stderr);
131 __set_errno (error);
132 fprintf (stderr, "%m\n");
134 funlockfile (stderr);
136 libc_hidden_def (vwarn)
139 void
140 warn (const char *format, ...)
142 VA (vwarn (format, ap))
144 libc_hidden_def (warn)
146 void
147 warnx (const char *format, ...)
149 VA (vwarnx (format, ap))
151 libc_hidden_def (warnx)
153 void
154 verr (int status, const char *format, __gnuc_va_list ap)
156 vwarn (format, ap);
157 exit (status);
159 libc_hidden_def (verr)
161 void
162 verrx (int status, const char *format, __gnuc_va_list ap)
164 vwarnx (format, ap);
165 exit (status);
167 libc_hidden_def (verrx)
169 void
170 err (int status, const char *format, ...)
172 VA (verr (status, format, ap))
175 void
176 errx (int status, const char *format, ...)
178 VA (verrx (status, format, ap))