* sysdeps/m68k/fpu/s_isinf.c (INTDEFX): Define to get correct
[glibc.git] / misc / err.c
blob1e681b399dfe5151f010e56676985af2c1827004
1 /* 4.4BSD utility functions for error messages.
2 Copyright (C) 1995, 1996, 1998, 2001 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);
112 void
113 vwarn (const char *format, __gnuc_va_list ap)
115 int error = errno;
117 flockfile (stderr);
118 #ifdef USE_IN_LIBIO
119 if (_IO_fwide (stderr, 0) > 0)
121 __fwprintf (stderr, L"%s: ", __progname);
122 if (format)
124 convert_and_print (format, ap);
125 fputws_unlocked (L": ", stderr);
127 __set_errno (error);
128 __fwprintf (stderr, L"%m\n");
130 else
131 #endif
133 fprintf (stderr, "%s: ", __progname);
134 if (format)
136 vfprintf (stderr, format, ap);
137 fputs_unlocked (": ", stderr);
139 __set_errno (error);
140 fprintf (stderr, "%m\n");
142 funlockfile (stderr);
146 void
147 warn (const char *format, ...)
149 VA (vwarn (format, ap))
152 void
153 warnx (const char *format, ...)
155 VA (vwarnx (format, ap))
158 void
159 verr (int status, const char *format, __gnuc_va_list ap)
161 vwarn (format, ap);
162 exit (status);
165 void
166 verrx (int status, const char *format, __gnuc_va_list ap)
168 vwarnx (format, ap);
169 exit (status);
172 void
173 err (int status, const char *format, ...)
175 VA (verr (status, format, ap))
178 void
179 errx (int status, const char *format, ...)
181 VA (verrx (status, format, ap))