| 1 | /* Error handler for noninteractive utilities
|
|---|
| 2 | Copyright (C) 1990-1998, 2000 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of the GNU C Library. Its master source is NOT part of
|
|---|
| 5 | the C library, however. The master source lives in /gd/gnu/lib.
|
|---|
| 6 |
|
|---|
| 7 | The GNU C Library is free software; you can redistribute it and/or
|
|---|
| 8 | modify it under the terms of the GNU Library General Public License as
|
|---|
| 9 | published by the Free Software Foundation; either version 2 of the
|
|---|
| 10 | License, or (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | The GNU C Library is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | Library General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU Library General Public
|
|---|
| 18 | License along with the GNU C Library; see the file COPYING.LIB. If not,
|
|---|
| 19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|---|
| 20 | Boston, MA 02111-1307, USA. */
|
|---|
| 21 |
|
|---|
| 22 | /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
|
|---|
| 23 |
|
|---|
| 24 | #ifdef HAVE_CONFIG_H
|
|---|
| 25 | # include <config.h>
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | #include <stdio.h>
|
|---|
| 29 | #if HAVE_LIBINTL_H
|
|---|
| 30 | # include <libintl.h>
|
|---|
| 31 | #endif
|
|---|
| 32 |
|
|---|
| 33 | #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
|
|---|
| 34 | # if __STDC__
|
|---|
| 35 | # include <stdarg.h>
|
|---|
| 36 | # define VA_START(args, lastarg) va_start(args, lastarg)
|
|---|
| 37 | # else
|
|---|
| 38 | # include <varargs.h>
|
|---|
| 39 | # define VA_START(args, lastarg) va_start(args)
|
|---|
| 40 | # endif
|
|---|
| 41 | #else
|
|---|
| 42 | # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
|
|---|
| 43 | # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 | #if STDC_HEADERS || _LIBC
|
|---|
| 47 | # include <stdlib.h>
|
|---|
| 48 | # include <string.h>
|
|---|
| 49 | #else
|
|---|
| 50 | void exit ();
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | #include "error.h"
|
|---|
| 54 |
|
|---|
| 55 | #ifndef HAVE_DECL_STRERROR_R
|
|---|
| 56 | "this configure-time declaration test was not run"
|
|---|
| 57 | #endif
|
|---|
| 58 | #if !HAVE_DECL_STRERROR_R
|
|---|
| 59 | char *strerror_r ();
|
|---|
| 60 | #endif
|
|---|
| 61 |
|
|---|
| 62 | #ifndef _
|
|---|
| 63 | # define _(String) String
|
|---|
| 64 | #endif
|
|---|
| 65 |
|
|---|
| 66 | /* If NULL, error will flush stdout, then print on stderr the program
|
|---|
| 67 | name, a colon and a space. Otherwise, error will call this
|
|---|
| 68 | function without parameters instead. */
|
|---|
| 69 | void (*error_print_progname) (
|
|---|
| 70 | #if __STDC__ - 0
|
|---|
| 71 | void
|
|---|
| 72 | #endif
|
|---|
| 73 | );
|
|---|
| 74 |
|
|---|
| 75 | /* This variable is incremented each time `error' is called. */
|
|---|
| 76 | unsigned int error_message_count;
|
|---|
| 77 |
|
|---|
| 78 | #ifdef _LIBC
|
|---|
| 79 | /* In the GNU C library, there is a predefined variable for this. */
|
|---|
| 80 |
|
|---|
| 81 | # define program_name program_invocation_name
|
|---|
| 82 | # include <errno.h>
|
|---|
| 83 |
|
|---|
| 84 | /* In GNU libc we want do not want to use the common name `error' directly.
|
|---|
| 85 | Instead make it a weak alias. */
|
|---|
| 86 | # define error __error
|
|---|
| 87 | # define error_at_line __error_at_line
|
|---|
| 88 |
|
|---|
| 89 | # ifdef USE_IN_LIBIO
|
|---|
| 90 | # include <libio/iolibio.h>
|
|---|
| 91 | # define fflush(s) _IO_fflush (s)
|
|---|
| 92 | # endif
|
|---|
| 93 |
|
|---|
| 94 | #else /* not _LIBC */
|
|---|
| 95 |
|
|---|
| 96 | /* The calling program should define program_name and set it to the
|
|---|
| 97 | name of the executing program. */
|
|---|
| 98 | extern char *program_name;
|
|---|
| 99 |
|
|---|
| 100 | # ifdef HAVE_STRERROR_R
|
|---|
| 101 | # define __strerror_r strerror_r
|
|---|
| 102 | # else
|
|---|
| 103 | # if HAVE_STRERROR
|
|---|
| 104 | # ifndef strerror /* On some systems, strerror is a macro */
|
|---|
| 105 | char *strerror ();
|
|---|
| 106 | # endif
|
|---|
| 107 | # else
|
|---|
| 108 | static char *
|
|---|
| 109 | private_strerror (errnum)
|
|---|
| 110 | int errnum;
|
|---|
| 111 | {
|
|---|
| 112 | extern char *sys_errlist[];
|
|---|
| 113 | extern int sys_nerr;
|
|---|
| 114 |
|
|---|
| 115 | if (errnum > 0 && errnum <= sys_nerr)
|
|---|
| 116 | return _(sys_errlist[errnum]);
|
|---|
| 117 | return _("Unknown system error");
|
|---|
| 118 | }
|
|---|
| 119 | # define strerror private_strerror
|
|---|
| 120 | # endif /* HAVE_STRERROR */
|
|---|
| 121 | # endif /* HAVE_STRERROR_R */
|
|---|
| 122 | #endif /* not _LIBC */
|
|---|
| 123 |
|
|---|
| 124 | /* Print the program name and error message MESSAGE, which is a printf-style
|
|---|
| 125 | format string with optional args.
|
|---|
| 126 | If ERRNUM is nonzero, print its corresponding system error message.
|
|---|
| 127 | Exit with status STATUS if it is nonzero. */
|
|---|
| 128 | /* VARARGS */
|
|---|
| 129 |
|
|---|
| 130 | void
|
|---|
| 131 | #if defined VA_START && __STDC__
|
|---|
| 132 | error (int status, int errnum, const char *message, ...)
|
|---|
| 133 | #else
|
|---|
| 134 | error (status, errnum, message, va_alist)
|
|---|
| 135 | int status;
|
|---|
| 136 | int errnum;
|
|---|
| 137 | char *message;
|
|---|
| 138 | va_dcl
|
|---|
| 139 | #endif
|
|---|
| 140 | {
|
|---|
| 141 | #ifdef VA_START
|
|---|
| 142 | va_list args;
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 | if (error_print_progname)
|
|---|
| 146 | (*error_print_progname) ();
|
|---|
| 147 | else
|
|---|
| 148 | {
|
|---|
| 149 | fflush (stdout);
|
|---|
| 150 | fprintf (stderr, "%s: ", program_name);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | #ifdef VA_START
|
|---|
| 154 | VA_START (args, message);
|
|---|
| 155 | # if HAVE_VPRINTF || _LIBC
|
|---|
| 156 | vfprintf (stderr, message, args);
|
|---|
| 157 | # else
|
|---|
| 158 | _doprnt (message, args, stderr);
|
|---|
| 159 | # endif
|
|---|
| 160 | va_end (args);
|
|---|
| 161 | #else
|
|---|
| 162 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
|
|---|
| 163 | #endif
|
|---|
| 164 |
|
|---|
| 165 | ++error_message_count;
|
|---|
| 166 | if (errnum)
|
|---|
| 167 | {
|
|---|
| 168 | #if defined HAVE_STRERROR_R || _LIBC
|
|---|
| 169 | char errbuf[1024];
|
|---|
| 170 | # if HAVE_WORKING_STRERROR_R || _LIBC
|
|---|
| 171 | fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
|
|---|
| 172 | # else
|
|---|
| 173 | /* Don't use __strerror_r's return value because on some systems
|
|---|
| 174 | (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
|
|---|
| 175 | __strerror_r (errnum, errbuf, sizeof errbuf);
|
|---|
| 176 | fprintf (stderr, ": %s", errbuf);
|
|---|
| 177 | # endif
|
|---|
| 178 | #else
|
|---|
| 179 | fprintf (stderr, ": %s", strerror (errnum));
|
|---|
| 180 | #endif
|
|---|
| 181 | }
|
|---|
| 182 | putc ('\n', stderr);
|
|---|
| 183 | fflush (stderr);
|
|---|
| 184 | if (status)
|
|---|
| 185 | exit (status);
|
|---|
| 186 | }
|
|---|
| 187 | |
|---|
| 188 |
|
|---|
| 189 | /* Sometimes we want to have at most one error per line. This
|
|---|
| 190 | variable controls whether this mode is selected or not. */
|
|---|
| 191 | int error_one_per_line;
|
|---|
| 192 |
|
|---|
| 193 | void
|
|---|
| 194 | #if defined VA_START && __STDC__
|
|---|
| 195 | error_at_line (int status, int errnum, const char *file_name,
|
|---|
| 196 | unsigned int line_number, const char *message, ...)
|
|---|
| 197 | #else
|
|---|
| 198 | error_at_line (status, errnum, file_name, line_number, message, va_alist)
|
|---|
| 199 | int status;
|
|---|
| 200 | int errnum;
|
|---|
| 201 | const char *file_name;
|
|---|
| 202 | unsigned int line_number;
|
|---|
| 203 | char *message;
|
|---|
| 204 | va_dcl
|
|---|
| 205 | #endif
|
|---|
| 206 | {
|
|---|
| 207 | #ifdef VA_START
|
|---|
| 208 | va_list args;
|
|---|
| 209 | #endif
|
|---|
| 210 |
|
|---|
| 211 | if (error_one_per_line)
|
|---|
| 212 | {
|
|---|
| 213 | static const char *old_file_name;
|
|---|
| 214 | static unsigned int old_line_number;
|
|---|
| 215 |
|
|---|
| 216 | if (old_line_number == line_number &&
|
|---|
| 217 | (file_name == old_file_name || !strcmp (old_file_name, file_name)))
|
|---|
| 218 | /* Simply return and print nothing. */
|
|---|
| 219 | return;
|
|---|
| 220 |
|
|---|
| 221 | old_file_name = file_name;
|
|---|
| 222 | old_line_number = line_number;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | if (error_print_progname)
|
|---|
| 226 | (*error_print_progname) ();
|
|---|
| 227 | else
|
|---|
| 228 | {
|
|---|
| 229 | fflush (stdout);
|
|---|
| 230 | fprintf (stderr, "%s:", program_name);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | if (file_name != NULL)
|
|---|
| 234 | fprintf (stderr, "%s:%d: ", file_name, line_number);
|
|---|
| 235 |
|
|---|
| 236 | #ifdef VA_START
|
|---|
| 237 | VA_START (args, message);
|
|---|
| 238 | # if HAVE_VPRINTF || _LIBC
|
|---|
| 239 | vfprintf (stderr, message, args);
|
|---|
| 240 | # else
|
|---|
| 241 | _doprnt (message, args, stderr);
|
|---|
| 242 | # endif
|
|---|
| 243 | va_end (args);
|
|---|
| 244 | #else
|
|---|
| 245 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
|
|---|
| 246 | #endif
|
|---|
| 247 |
|
|---|
| 248 | ++error_message_count;
|
|---|
| 249 | if (errnum)
|
|---|
| 250 | {
|
|---|
| 251 | #if defined HAVE_STRERROR_R || _LIBC
|
|---|
| 252 | char errbuf[1024];
|
|---|
| 253 | # if HAVE_WORKING_STRERROR_R || _LIBC
|
|---|
| 254 | fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
|
|---|
| 255 | # else
|
|---|
| 256 | /* Don't use __strerror_r's return value because on some systems
|
|---|
| 257 | (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
|
|---|
| 258 | __strerror_r (errnum, errbuf, sizeof errbuf);
|
|---|
| 259 | fprintf (stderr, ": %s", errbuf);
|
|---|
| 260 | # endif
|
|---|
| 261 | #else
|
|---|
| 262 | fprintf (stderr, ": %s", strerror (errnum));
|
|---|
| 263 | #endif
|
|---|
| 264 | }
|
|---|
| 265 | putc ('\n', stderr);
|
|---|
| 266 | fflush (stderr);
|
|---|
| 267 | if (status)
|
|---|
| 268 | exit (status);
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | #ifdef _LIBC
|
|---|
| 272 | /* Make the weak alias. */
|
|---|
| 273 | # undef error
|
|---|
| 274 | # undef error_at_line
|
|---|
| 275 | weak_alias (__error, error)
|
|---|
| 276 | weak_alias (__error_at_line, error_at_line)
|
|---|
| 277 | #endif
|
|---|