]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/gnu-sort/lib/error.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / gnu-sort / lib / error.c
1 /* Error handler for noninteractive utilities
2    Copyright (C) 1990-1998, 2000-2002, 2003, 2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "error.h"
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifdef _LIBC
33 # include <libintl.h>
34 #else
35 # include "gettext.h"
36 #endif
37
38 #ifdef _LIBC
39 # include <wchar.h>
40 # define mbsrtowcs __mbsrtowcs
41 #endif
42
43 #if !_LIBC
44 # include "unlocked-io.h"
45 #endif
46
47 #ifndef _
48 # define _(String) String
49 #endif
50
51 /* If NULL, error will flush stdout, then print on stderr the program
52    name, a colon and a space.  Otherwise, error will call this
53    function without parameters instead.  */
54 void (*error_print_progname) (void);
55
56 /* This variable is incremented each time `error' is called.  */
57 unsigned int error_message_count;
58
59 #ifdef _LIBC
60 /* In the GNU C library, there is a predefined variable for this.  */
61
62 # define program_name program_invocation_name
63 # include <errno.h>
64 # include <libio/libioP.h>
65
66 /* In GNU libc we want do not want to use the common name `error' directly.
67    Instead make it a weak alias.  */
68 extern void __error (int status, int errnum, const char *message, ...)
69      __attribute__ ((__format__ (__printf__, 3, 4)));
70 extern void __error_at_line (int status, int errnum, const char *file_name,
71                              unsigned int line_number, const char *message,
72                              ...)
73      __attribute__ ((__format__ (__printf__, 5, 6)));;
74 # define error __error
75 # define error_at_line __error_at_line
76
77 # include <libio/iolibio.h>
78 # define fflush(s) INTUSE(_IO_fflush) (s)
79 # undef putc
80 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
81
82 # include <bits/libc-lock.h>
83
84 #else /* not _LIBC */
85
86 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
87 #  ifndef HAVE_DECL_STRERROR_R
88 "this configure-time declaration test was not run"
89 #  endif
90 char *strerror_r ();
91 # endif
92
93 # ifndef SIZE_MAX
94 #  define SIZE_MAX ((size_t) -1)
95 # endif
96
97 /* The calling program should define program_name and set it to the
98    name of the executing program.  */
99 extern char *program_name;
100
101 # if HAVE_STRERROR_R || defined strerror_r
102 #  define __strerror_r strerror_r
103 # endif
104 #endif  /* not _LIBC */
105
106 static void
107 print_errno_message (int errnum)
108 {
109   char const *s = NULL;
110
111 #if defined HAVE_STRERROR_R || _LIBC
112   char errbuf[1024];
113 # if STRERROR_R_CHAR_P || _LIBC
114   s = __strerror_r (errnum, errbuf, sizeof errbuf);
115 # else
116   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
117     s = errbuf;
118 # endif
119 #endif
120
121 #if !_LIBC
122   if (! s && ! (s = strerror (errnum)))
123     s = _("Unknown system error");
124 #endif
125
126 #if _LIBC
127   if (_IO_fwide (stderr, 0) > 0)
128     {
129       __fwprintf (stderr, L": %s", s);
130       return;
131     }
132 #endif
133
134   fprintf (stderr, ": %s", s);
135 }
136
137 static void
138 error_tail (int status, int errnum, const char *message, va_list args)
139 {
140 #if _LIBC
141   if (_IO_fwide (stderr, 0) > 0)
142     {
143 # define ALLOCA_LIMIT 2000
144       size_t len = strlen (message) + 1;
145       const wchar_t *wmessage = L"out of memory";
146       wchar_t *wbuf = (len < ALLOCA_LIMIT
147                        ? alloca (len * sizeof *wbuf)
148                        : len <= SIZE_MAX / sizeof *wbuf
149                        ? malloc (len * sizeof *wbuf)
150                        : NULL);
151
152       if (wbuf)
153         {
154           size_t res;
155           mbstate_t st;
156           const char *tmp = message;
157           memset (&st, '\0', sizeof (st));
158           res = mbsrtowcs (wbuf, &tmp, len, &st);
159           wmessage = res == (size_t) -1 ? L"???" : wbuf;
160         }
161
162       __vfwprintf (stderr, wmessage, args);
163       if (! (len < ALLOCA_LIMIT))
164         free (wbuf);
165     }
166   else
167 #endif
168     vfprintf (stderr, message, args);
169   va_end (args);
170
171   ++error_message_count;
172   if (errnum)
173     print_errno_message (errnum);
174 #if _LIBC
175   if (_IO_fwide (stderr, 0) > 0)
176     putwc (L'\n', stderr);
177   else
178 #endif
179     putc ('\n', stderr);
180   fflush (stderr);
181   if (status)
182     exit (status);
183 }
184
185
186 /* Print the program name and error message MESSAGE, which is a printf-style
187    format string with optional args.
188    If ERRNUM is nonzero, print its corresponding system error message.
189    Exit with status STATUS if it is nonzero.  */
190 void
191 error (int status, int errnum, const char *message, ...)
192 {
193   va_list args;
194
195 #if defined _LIBC && defined __libc_ptf_call
196   /* We do not want this call to be cut short by a thread
197      cancellation.  Therefore disable cancellation for now.  */
198   int state = PTHREAD_CANCEL_ENABLE;
199   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
200                    0);
201 #endif
202
203   fflush (stdout);
204 #ifdef _LIBC
205   _IO_flockfile (stderr);
206 #endif
207   if (error_print_progname)
208     (*error_print_progname) ();
209   else
210     {
211 #if _LIBC
212       if (_IO_fwide (stderr, 0) > 0)
213         __fwprintf (stderr, L"%s: ", program_name);
214       else
215 #endif
216         fprintf (stderr, "%s: ", program_name);
217     }
218
219   va_start (args, message);
220   error_tail (status, errnum, message, args);
221
222 #ifdef _LIBC
223   _IO_funlockfile (stderr);
224 # ifdef __libc_ptf_call
225   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
226 # endif
227 #endif
228 }
229 \f
230 /* Sometimes we want to have at most one error per line.  This
231    variable controls whether this mode is selected or not.  */
232 int error_one_per_line;
233
234 void
235 error_at_line (int status, int errnum, const char *file_name,
236                unsigned int line_number, const char *message, ...)
237 {
238   va_list args;
239
240   if (error_one_per_line)
241     {
242       static const char *old_file_name;
243       static unsigned int old_line_number;
244
245       if (old_line_number == line_number
246           && (file_name == old_file_name
247               || strcmp (old_file_name, file_name) == 0))
248         /* Simply return and print nothing.  */
249         return;
250
251       old_file_name = file_name;
252       old_line_number = line_number;
253     }
254
255 #if defined _LIBC && defined __libc_ptf_call
256   /* We do not want this call to be cut short by a thread
257      cancellation.  Therefore disable cancellation for now.  */
258   int state = PTHREAD_CANCEL_ENABLE;
259   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
260                    0);
261 #endif
262
263   fflush (stdout);
264 #ifdef _LIBC
265   _IO_flockfile (stderr);
266 #endif
267   if (error_print_progname)
268     (*error_print_progname) ();
269   else
270     {
271 #if _LIBC
272       if (_IO_fwide (stderr, 0) > 0)
273         __fwprintf (stderr, L"%s: ", program_name);
274       else
275 #endif
276         fprintf (stderr, "%s:", program_name);
277     }
278
279   if (file_name != NULL)
280     {
281 #if _LIBC
282       if (_IO_fwide (stderr, 0) > 0)
283         __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
284       else
285 #endif
286         fprintf (stderr, "%s:%d: ", file_name, line_number);
287     }
288
289   va_start (args, message);
290   error_tail (status, errnum, message, args);
291
292 #ifdef _LIBC
293   _IO_funlockfile (stderr);
294 # ifdef __libc_ptf_call
295   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
296 # endif
297 #endif
298 }
299
300 #ifdef _LIBC
301 /* Make the weak alias.  */
302 # undef error
303 # undef error_at_line
304 weak_alias (__error, error)
305 weak_alias (__error_at_line, error_at_line)
306 #endif