Version 0.7.4.
[cboard.git] / osdep / err.c
blob0d50f1ea65d420331543e02e1641e8450362503b
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 #ifndef HAVE_FPUTS_UNLOCKED
45 #ifndef HAVE_PUTWC_UNLOCKED
46 #ifdef HAVE_PUTW_UNLOCKED
47 #define putwc_unlocked putw_unlocked
48 #else
49 #define putwc_unlocked putc_unlocked
50 #define putw_unlocked putc_unlocked
51 #endif
52 #endif
54 int fputs_unlocked(const char *str, FILE *fp)
56 char *p = str;
58 while (*p) {
59 if (putc_unlocked(*p++, fp) == EOF)
60 return EOF;
63 return 1;
66 int fputws_unlocked(const char *str, FILE *fp)
68 char *p = str;
70 while (*p) {
71 if (putw_unlocked(*p++, fp) == EOF)
72 return EOF;
75 return 1;
77 #endif
79 #ifdef USE_IN_LIBIO
80 static void
81 convert_and_print (const char *format, __gnuc_va_list ap)
83 # define ALLOCA_LIMIT 2000
84 size_t len;
85 wchar_t *wformat = NULL;
86 mbstate_t st;
87 size_t res;
88 const char *tmp;
90 if (format == NULL)
91 return;
93 len = strlen (format) + 1;
97 if (len < ALLOCA_LIMIT)
98 wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
99 else
101 if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
102 wformat = NULL;
104 wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
106 if (wformat == NULL)
108 fputws_unlocked (L"out of memory\n", stderr);
109 return;
113 memset (&st, '\0', sizeof (st));
114 tmp =format;
116 while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
118 if (res == (size_t) -1)
119 /* The string cannot be converted. */
120 wformat = (wchar_t *) L"???";
122 __vfwprintf (stderr, wformat, ap);
124 #endif
126 void
127 vwarnx (const char *format, __gnuc_va_list ap)
129 flockfile (stderr);
130 #ifdef USE_IN_LIBIO
131 if (_IO_fwide (stderr, 0) > 0)
133 __fwprintf (stderr, L"%s: ", __progname);
134 convert_and_print (format, ap);
135 putwc_unlocked (L'\n', stderr);
137 else
138 #endif
140 fprintf (stderr, "%s: ", __progname);
141 if (format)
142 vfprintf (stderr, format, ap);
143 putc_unlocked ('\n', stderr);
145 funlockfile (stderr);
147 //libc_hidden_def (vwarnx)
149 void
150 vwarn (const char *format, __gnuc_va_list ap)
152 int error = errno;
154 flockfile (stderr);
155 #ifdef USE_IN_LIBIO
156 if (_IO_fwide (stderr, 0) > 0)
158 __fwprintf (stderr, L"%s: ", __progname);
159 if (format)
161 convert_and_print (format, ap);
162 fputws_unlocked (L": ", stderr);
164 //__set_errno (error);
165 __fwprintf (stderr, L"%s\n", strerror(error));
167 else
168 #endif
170 fprintf (stderr, "%s: ", __progname);
171 if (format)
173 vfprintf (stderr, format, ap);
174 fputs_unlocked (": ", stderr);
176 //__set_errno (error);
177 fprintf (stderr, "%s\n", strerror(error));
179 funlockfile (stderr);
181 //libc_hidden_def (vwarn)
184 void
185 warn (const char *format, ...)
187 VA (vwarn (format, ap))
189 //libc_hidden_def (warn)
191 void
192 warnx (const char *format, ...)
194 VA (vwarnx (format, ap))
196 //libc_hidden_def (warnx)
198 void
199 verr (int status, const char *format, __gnuc_va_list ap)
201 vwarn (format, ap);
202 exit (status);
204 //libc_hidden_def (verr)
206 void
207 verrx (int status, const char *format, __gnuc_va_list ap)
209 vwarnx (format, ap);
210 exit (status);
212 //libc_hidden_def (verrx)
214 void
215 err (int status, const char *format, ...)
217 VA (verr (status, format, ap))
220 void
221 errx (int status, const char *format, ...)
223 VA (verrx (status, format, ap))