Updated to fedora-glibc-20060306T0720
[glibc.git] / misc / err.c
blob716f994517af6ab3b5cb6c2c4a40b0641966eccf
1 /* 4.4BSD utility functions for error messages.
2 Copyright (C) 1995,96,98,2001,02 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 #ifdef USE_IN_LIBIO
28 # include <wchar.h>
29 # define flockfile(s) _IO_flockfile (s)
30 # define funlockfile(s) _IO_funlockfile (s)
31 #endif
33 extern char *__progname;
35 #define VA(call) \
36 { \
37 va_list ap; \
38 va_start (ap, format); \
39 call; \
40 va_end (ap); \
43 #ifdef USE_IN_LIBIO
44 static void
45 convert_and_print (const char *format, __gnuc_va_list ap)
47 # define ALLOCA_LIMIT 2000
48 size_t len;
49 wchar_t *wformat = NULL;
50 mbstate_t st;
51 size_t res;
52 const char *tmp;
54 if (format == NULL)
55 return;
57 len = strlen (format) + 1;
61 if (len < ALLOCA_LIMIT)
62 wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
63 else
65 if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
66 wformat = NULL;
68 wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
70 if (wformat == NULL)
72 fputws_unlocked (L"out of memory\n", stderr);
73 return;
77 memset (&st, '\0', sizeof (st));
78 tmp =format;
80 while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
82 if (res == (size_t) -1)
83 /* The string cannot be converted. */
84 wformat = (wchar_t *) L"???";
86 __vfwprintf (stderr, wformat, ap);
88 #endif
90 void
91 vwarnx (const char *format, __gnuc_va_list ap)
93 flockfile (stderr);
94 #ifdef USE_IN_LIBIO
95 if (_IO_fwide (stderr, 0) > 0)
97 __fwprintf (stderr, L"%s: ", __progname);
98 convert_and_print (format, ap);
99 putwc_unlocked (L'\n', stderr);
101 else
102 #endif
104 fprintf (stderr, "%s: ", __progname);
105 if (format)
106 vfprintf (stderr, format, ap);
107 putc_unlocked ('\n', stderr);
109 funlockfile (stderr);
111 libc_hidden_def (vwarnx)
113 void
114 vwarn (const char *format, __gnuc_va_list ap)
116 int error = errno;
118 flockfile (stderr);
119 #ifdef USE_IN_LIBIO
120 if (_IO_fwide (stderr, 0) > 0)
122 __fwprintf (stderr, L"%s: ", __progname);
123 if (format)
125 convert_and_print (format, ap);
126 fputws_unlocked (L": ", stderr);
128 __set_errno (error);
129 __fwprintf (stderr, L"%m\n");
131 else
132 #endif
134 fprintf (stderr, "%s: ", __progname);
135 if (format)
137 vfprintf (stderr, format, ap);
138 fputs_unlocked (": ", stderr);
140 __set_errno (error);
141 fprintf (stderr, "%m\n");
143 funlockfile (stderr);
145 libc_hidden_def (vwarn)
148 void
149 warn (const char *format, ...)
151 VA (vwarn (format, ap))
153 libc_hidden_def (warn)
155 void
156 warnx (const char *format, ...)
158 VA (vwarnx (format, ap))
160 libc_hidden_def (warnx)
162 void
163 verr (int status, const char *format, __gnuc_va_list ap)
165 vwarn (format, ap);
166 exit (status);
168 libc_hidden_def (verr)
170 void
171 verrx (int status, const char *format, __gnuc_va_list ap)
173 vwarnx (format, ap);
174 exit (status);
176 libc_hidden_def (verrx)
178 void
179 err (int status, const char *format, ...)
181 VA (verr (status, format, ap))
184 void
185 errx (int status, const char *format, ...)
187 VA (verrx (status, format, ap))