Commit | Line | Data |
---|---|---|
60a17f4e JH |
1 | /* error.c -- error handler for noninteractive utilities |
2 | Copyright (C) 1990, 1991, 1992 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. */ | |
19 | ||
20 | #include <stdio.h> | |
21 | ||
22 | #ifdef HAVE_VPRINTF | |
23 | ||
24 | #if __STDC__ | |
25 | #include <stdarg.h> | |
26 | #define VA_START(args, lastarg) va_start(args, lastarg) | |
27 | #else /* !__STDC__ */ | |
28 | #include <varargs.h> | |
29 | #define VA_START(args, lastarg) va_start(args) | |
30 | #endif /* !__STDC__ */ | |
31 | ||
32 | #else /* !HAVE_VPRINTF */ | |
33 | ||
34 | #ifdef HAVE_DOPRNT | |
35 | #define va_alist args | |
36 | #define va_dcl int args; | |
37 | #else /* !HAVE_DOPRNT */ | |
38 | #define va_alist a1, a2, a3, a4, a5, a6, a7, a8 | |
39 | #define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; | |
40 | #endif /* !HAVE_DOPRNT */ | |
41 | ||
42 | #endif /* !HAVE_VPRINTF */ | |
43 | ||
44 | #ifdef STDC_HEADERS | |
45 | #include <stdlib.h> | |
46 | #include <string.h> | |
47 | #else /* !STDC_HEADERS */ | |
48 | void exit (); | |
49 | #endif /* !STDC_HEADERS */ | |
50 | ||
51 | extern char *program_name; | |
52 | ||
53 | #ifndef HAVE_STRERROR | |
54 | static char * | |
55 | private_strerror (errnum) | |
56 | int errnum; | |
57 | { | |
58 | extern char *sys_errlist[]; | |
59 | extern int sys_nerr; | |
60 | ||
61 | if (errnum > 0 && errnum <= sys_nerr) | |
62 | return sys_errlist[errnum]; | |
63 | return "Unknown system error"; | |
64 | } | |
65 | #define strerror private_strerror | |
66 | #endif /* !HAVE_STRERROR */ | |
67 | ||
68 | /* Print the program name and error message MESSAGE, which is a printf-style | |
69 | format string with optional args. | |
70 | If ERRNUM is nonzero, print its corresponding system error message. | |
71 | Exit with status STATUS if it is nonzero. */ | |
72 | /* VARARGS */ | |
73 | void | |
74 | #if defined (HAVE_VPRINTF) && __STDC__ | |
75 | error (int status, int errnum, char *message, ...) | |
76 | #else /* !HAVE_VPRINTF or !__STDC__ */ | |
77 | error (status, errnum, message, va_alist) | |
78 | int status; | |
79 | int errnum; | |
80 | char *message; | |
81 | va_dcl | |
82 | #endif /* !HAVE_VPRINTF or !__STDC__ */ | |
83 | { | |
84 | #ifdef HAVE_VPRINTF | |
85 | va_list args; | |
86 | #endif /* HAVE_VPRINTF */ | |
87 | ||
88 | fprintf (stderr, "%s: ", program_name); | |
89 | #ifdef HAVE_VPRINTF | |
90 | VA_START (args, message); | |
91 | vfprintf (stderr, message, args); | |
92 | va_end (args); | |
93 | #else /* !HAVE_VPRINTF */ | |
94 | #ifdef HAVE_DOPRNT | |
95 | _doprnt (message, &args, stderr); | |
96 | #else /* !HAVE_DOPRNT */ | |
97 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); | |
98 | #endif /* !HAVE_DOPRNT */ | |
99 | #endif /* !HAVE_VPRINTF */ | |
100 | if (errnum) | |
101 | fprintf (stderr, ": %s", strerror (errnum)); | |
102 | putc ('\n', stderr); | |
103 | fflush (stderr); | |
104 | if (status) | |
105 | exit (status); | |
106 | } |