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