iconv: Suppress array out of bounds warning.
[platform/upstream/glibc.git] / misc / err.c
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.
4
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.
9
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.
14
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/>.  */
18
19 #include <stdarg.h>
20 #include <err.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 #include <wchar.h>
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
29
30 extern char *__progname;
31
32 #define VA(call)                                                              \
33 {                                                                             \
34   va_list ap;                                                                 \
35   va_start (ap, format);                                                      \
36   call;                                                                       \
37   va_end (ap);                                                                \
38 }
39
40 static void
41 convert_and_print (const char *format, __gnuc_va_list ap)
42 {
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;
49
50   if (format == NULL)
51     return;
52
53   len = strlen (format) + 1;
54
55   do
56     {
57       if (len < ALLOCA_LIMIT)
58         wformat = (wchar_t *) alloca (len * sizeof (wchar_t));
59       else
60         {
61           if (wformat != NULL && len / 2 < ALLOCA_LIMIT)
62             wformat = NULL;
63
64           wformat = (wchar_t *) realloc (wformat, len * sizeof (wchar_t));
65
66           if (wformat == NULL)
67             {
68               fputws_unlocked (L"out of memory\n", stderr);
69               return;
70             }
71         }
72
73       memset (&st, '\0', sizeof (st));
74       tmp =format;
75     }
76   while ((res = __mbsrtowcs (wformat, &tmp, len, &st)) == len);
77
78   if (res == (size_t) -1)
79     /* The string cannot be converted.  */
80     wformat = (wchar_t *) L"???";
81
82   __vfwprintf (stderr, wformat, ap);
83 }
84
85 void
86 vwarnx (const char *format, __gnuc_va_list ap)
87 {
88   flockfile (stderr);
89   if (_IO_fwide (stderr, 0) > 0)
90     {
91       __fwprintf (stderr, L"%s: ", __progname);
92       convert_and_print (format, ap);
93       putwc_unlocked (L'\n', stderr);
94     }
95   else
96     {
97       fprintf (stderr, "%s: ", __progname);
98       if (format)
99         vfprintf (stderr, format, ap);
100       putc_unlocked ('\n', stderr);
101     }
102   funlockfile (stderr);
103 }
104 libc_hidden_def (vwarnx)
105
106 void
107 vwarn (const char *format, __gnuc_va_list ap)
108 {
109   int error = errno;
110
111   flockfile (stderr);
112   if (_IO_fwide (stderr, 0) > 0)
113     {
114       __fwprintf (stderr, L"%s: ", __progname);
115       if (format)
116         {
117           convert_and_print (format, ap);
118           fputws_unlocked (L": ", stderr);
119         }
120       __set_errno (error);
121       __fwprintf (stderr, L"%m\n");
122     }
123   else
124     {
125       fprintf (stderr, "%s: ", __progname);
126       if (format)
127         {
128           vfprintf (stderr, format, ap);
129           fputs_unlocked (": ", stderr);
130         }
131       __set_errno (error);
132       fprintf (stderr, "%m\n");
133     }
134   funlockfile (stderr);
135 }
136 libc_hidden_def (vwarn)
137
138
139 void
140 warn (const char *format, ...)
141 {
142   VA (vwarn (format, ap))
143 }
144 libc_hidden_def (warn)
145
146 void
147 warnx (const char *format, ...)
148 {
149   VA (vwarnx (format, ap))
150 }
151 libc_hidden_def (warnx)
152
153 void
154 verr (int status, const char *format, __gnuc_va_list ap)
155 {
156   vwarn (format, ap);
157   exit (status);
158 }
159 libc_hidden_def (verr)
160
161 void
162 verrx (int status, const char *format, __gnuc_va_list ap)
163 {
164   vwarnx (format, ap);
165   exit (status);
166 }
167 libc_hidden_def (verrx)
168
169 void
170 err (int status, const char *format, ...)
171 {
172   VA (verr (status, format, ap))
173 }
174
175 void
176 errx (int status, const char *format, ...)
177 {
178   VA (verrx (status, format, ap))
179 }