* system.h [rindex, incl, bcopy, bzero]: Ditto.
[gnulib.git] / lib / error.c
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 #ifndef HAVE_STRERROR
52 static char *
53 private_strerror (errnum)
54      int errnum;
55 {
56   extern char *sys_errlist[];
57   extern int sys_nerr;
58
59   if (errnum > 0 && errnum <= sys_nerr)
60     return sys_errlist[errnum];
61   return "Unknown system error";
62 }
63 #define strerror private_strerror
64 #endif /* !HAVE_STRERROR */
65
66 /* Print the program name and error message MESSAGE, which is a printf-style
67    format string with optional args.
68    If ERRNUM is nonzero, print its corresponding system error message.
69    Exit with status STATUS if it is nonzero.  */
70 /* VARARGS */
71 void
72 #if defined (HAVE_VPRINTF) && __STDC__
73 error (int status, int errnum, char *message, ...)
74 #else /* !HAVE_VPRINTF or !__STDC__ */
75 error (status, errnum, message, va_alist)
76      int status;
77      int errnum;
78      char *message;
79      va_dcl
80 #endif /* !HAVE_VPRINTF or !__STDC__ */
81 {
82   extern char *program_name;
83 #ifdef HAVE_VPRINTF
84   va_list args;
85 #endif /* HAVE_VPRINTF */
86
87   fprintf (stderr, "%s: ", program_name);
88 #ifdef HAVE_VPRINTF
89   VA_START (args, message);
90   vfprintf (stderr, message, args);
91   va_end (args);
92 #else /* !HAVE_VPRINTF */
93 #ifdef HAVE_DOPRNT
94   _doprnt (message, &args, stderr);
95 #else /* !HAVE_DOPRNT */
96   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
97 #endif /* !HAVE_DOPRNT */
98 #endif /* !HAVE_VPRINTF */
99   if (errnum)
100     fprintf (stderr, ": %s", strerror (errnum));
101   putc ('\n', stderr);
102   fflush (stderr);
103   if (status)
104     exit (status);
105 }