Tue Jan 2 00:50:10 1996 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc/pb-stable.git] / misc / error.c
blobda3c6ee1b25871c5fd27e1e8d06ba6aae67dcb1b
1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95 Free Software Foundation, Inc.
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.
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.
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.
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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <stdio.h>
30 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
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
43 #if STDC_HEADERS || _LIBC
44 # include <stdlib.h>
45 # include <string.h>
46 #else
47 void exit ();
48 #endif
50 /* This variable is incremented each time `error' is called. */
51 unsigned int error_message_count;
53 /* If NULL, error will flush stdout, then print on stderr the program
54 name, a colon and a space. Otherwise, error will call this
55 function without parameters instead. */
56 void (*error_print_progname) () = NULL;
58 #ifdef _LIBC
59 #define program_name program_invocation_name
60 #endif
62 /* The calling program should define program_name and set it to the
63 name of the executing program. */
64 extern char *program_name;
66 #if HAVE_STRERROR || _LIBC
67 # ifndef strerror /* On some systems, strerror is a macro */
68 char *strerror ();
69 # endif
70 #else
71 static char *
72 private_strerror (errnum)
73 int errnum;
75 extern char *sys_errlist[];
76 extern int sys_nerr;
78 if (errnum > 0 && errnum <= sys_nerr)
79 return sys_errlist[errnum];
80 return "Unknown system error";
82 #define strerror private_strerror
83 #endif
85 /* Print the program name and error message MESSAGE, which is a printf-style
86 format string with optional args.
87 If ERRNUM is nonzero, print its corresponding system error message.
88 Exit with status STATUS if it is nonzero. */
89 /* VARARGS */
91 void
92 #if defined(VA_START) && __STDC__
93 error (int status, int errnum, const char *message, ...)
94 #else
95 error (status, errnum, message, va_alist)
96 int status;
97 int errnum;
98 char *message;
99 va_dcl
100 #endif
102 #ifdef VA_START
103 va_list args;
104 #endif
106 if (error_print_progname)
107 (*error_print_progname) ();
108 else
110 fflush (stdout);
111 fprintf (stderr, "%s: ", program_name);
114 #ifdef VA_START
115 VA_START (args, message);
116 # if HAVE_VPRINTF || _LIBC
117 vfprintf (stderr, message, args);
118 # else
119 _doprnt (message, args, stderr);
120 # endif
121 va_end (args);
122 #else
123 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
124 #endif
126 ++error_message_count;
128 if (errnum)
129 fprintf (stderr, ": %s", strerror (errnum));
130 putc ('\n', stderr);
131 fflush (stderr);
132 if (status)
133 exit (status);