1 | /* error.c -- error handler for noninteractive utilities
|
---|
2 | Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2, or (at your option)
|
---|
7 | any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
17 |
|
---|
18 | /* Written by David MacKenzie <[email protected]>. */
|
---|
19 |
|
---|
20 | #ifdef HAVE_CONFIG_H
|
---|
21 | # ifdef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
|
---|
22 | # include <win32cfg.h>
|
---|
23 | # else
|
---|
24 | # include <sysfuncs.h>
|
---|
25 | # endif
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include <stdio.h>
|
---|
29 |
|
---|
30 | #if HAVE_VPRINTF || HAVE_DOPRNT
|
---|
31 | # if __STDC__
|
---|
32 | # include <stdarg.h>
|
---|
33 | # define VA_START(args, lastarg) va_start(args, lastarg)
|
---|
34 | # else
|
---|
35 | # include <varargs.h>
|
---|
36 | # define VA_START(args, lastarg) va_start(args)
|
---|
37 | # endif
|
---|
38 | #else
|
---|
39 | # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
|
---|
40 | # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #if STDC_HEADERS
|
---|
44 | # include <stdlib.h>
|
---|
45 | # include <string.h>
|
---|
46 | #else
|
---|
47 | void exit ();
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | /* If NULL, error will flush stdout, then print on stderr the program
|
---|
51 | name, a colon and a space. Otherwise, error will call this
|
---|
52 | function without parameters instead. */
|
---|
53 | void (*error_print_progname) () = NULL;
|
---|
54 |
|
---|
55 | /* The calling program should define program_name and set it to the
|
---|
56 | name of the executing program. */
|
---|
57 | #if 0 /* [RB/TS:10/95] do not use program_name */
|
---|
58 | extern char *program_name;
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #if HAVE_STRERROR
|
---|
62 | char *strerror (int errnum);
|
---|
63 | #else
|
---|
64 | static char *
|
---|
65 | private_strerror (errnum)
|
---|
66 | int errnum;
|
---|
67 | {
|
---|
68 | extern char *sys_errlist[];
|
---|
69 | extern int sys_nerr;
|
---|
70 |
|
---|
71 | if (errnum > 0 && errnum <= sys_nerr)
|
---|
72 | return sys_errlist[errnum];
|
---|
73 | return "Unknown system error";
|
---|
74 | }
|
---|
75 | #define strerror private_strerror
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | /* Print the program name and error message MESSAGE, which is a printf-style
|
---|
79 | format string with optional args.
|
---|
80 | If ERRNUM is nonzero, print its corresponding system error message.
|
---|
81 | Exit with status STATUS if it is nonzero. */
|
---|
82 | /* VARARGS */
|
---|
83 |
|
---|
84 | void
|
---|
85 | #if defined(VA_START) && __STDC__
|
---|
86 | error (int status, int errnum, const char *message, ...)
|
---|
87 | #else
|
---|
88 | error (status, errnum, message, va_alist)
|
---|
89 | int status;
|
---|
90 | int errnum;
|
---|
91 | char *message;
|
---|
92 | va_dcl
|
---|
93 | #endif
|
---|
94 | {
|
---|
95 | #ifdef VA_START
|
---|
96 | va_list args;
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | if (error_print_progname)
|
---|
100 | (*error_print_progname) ();
|
---|
101 | else
|
---|
102 | {
|
---|
103 | fflush (stdout);
|
---|
104 | #if 0 /* [RB/TS:10/95] do not use program_name */
|
---|
105 | fprintf (stderr, "%s: ", program_name);
|
---|
106 | #endif
|
---|
107 | }
|
---|
108 |
|
---|
109 | #ifdef VA_START
|
---|
110 | VA_START (args, message);
|
---|
111 | # if HAVE_VPRINTF
|
---|
112 | vfprintf (stderr, message, args);
|
---|
113 | # else
|
---|
114 | _doprnt (message, args, stderr);
|
---|
115 | # endif
|
---|
116 | va_end (args);
|
---|
117 | #else
|
---|
118 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | if (errnum)
|
---|
122 | fprintf (stderr, ": %s", strerror (errnum));
|
---|
123 | putc ('\n', stderr);
|
---|
124 | fflush (stderr);
|
---|
125 | if (status)
|
---|
126 | exit (status);
|
---|
127 | }
|
---|