Mon May 6 09:51:05 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / misc / error.c
blob49c772dbb58a8cd93c40af14edd3a8af57add6f6
1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96 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 #ifndef _
51 #define _(String) String
52 #endif
54 /* If NULL, error will flush stdout, then print on stderr the program
55 name, a colon and a space. Otherwise, error will call this
56 function without parameters instead. */
57 void (*error_print_progname) (
58 #if __STDC__ - 0
59 void
60 #endif
63 /* This variable is incremented each time `error' is called. */
64 unsigned int error_message_count;
66 #ifdef _LIBC
67 /* In the GNU C library, there is a predefined variable for this. */
69 #define program_name program_invocation_name
70 #include <errno.h>
72 #else
74 /* The calling program should define program_name and set it to the
75 name of the executing program. */
76 extern char *program_name;
78 #if HAVE_STRERROR
79 # ifndef strerror /* On some systems, strerror is a macro */
80 char *strerror ();
81 # endif
82 #else
83 static char *
84 private_strerror (errnum)
85 int errnum;
87 extern char *sys_errlist[];
88 extern int sys_nerr;
90 if (errnum > 0 && errnum <= sys_nerr)
91 return sys_errlist[errnum];
92 return _("Unknown system error");
94 #define strerror private_strerror
95 #endif /* HAVE_STRERROR */
96 #endif /* _LIBC */
98 /* Print the program name and error message MESSAGE, which is a printf-style
99 format string with optional args.
100 If ERRNUM is nonzero, print its corresponding system error message.
101 Exit with status STATUS if it is nonzero. */
102 /* VARARGS */
104 void
105 #if defined(VA_START) && __STDC__
106 error (int status, int errnum, const char *message, ...)
107 #else
108 error (status, errnum, message, va_alist)
109 int status;
110 int errnum;
111 char *message;
112 va_dcl
113 #endif
115 #ifdef VA_START
116 va_list args;
117 #endif
119 if (error_print_progname)
120 (*error_print_progname) ();
121 else
123 fflush (stdout);
124 fprintf (stderr, "%s: ", program_name);
127 #ifdef VA_START
128 VA_START (args, message);
129 # if HAVE_VPRINTF || _LIBC
130 vfprintf (stderr, message, args);
131 # else
132 _doprnt (message, args, stderr);
133 # endif
134 va_end (args);
135 #else
136 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
137 #endif
139 ++error_message_count;
140 if (errnum)
141 fprintf (stderr, ": %s", strerror (errnum));
142 putc ('\n', stderr);
143 fflush (stderr);
144 if (status)
145 exit (status);
148 /* Sometimes we want to have at most one error per line. This
149 variable controls whether this mode is selected or not. */
150 int error_one_per_line;
152 void
153 #if defined(VA_START) && __STDC__
154 error_at_line (int status, int errnum, const char *file_name,
155 unsigned int line_number, const char *message, ...)
156 #else
157 error_at_line (status, errnum, file_name, line_number, message, va_alist)
158 int status;
159 int errnum;
160 const char *file_name;
161 unsigned int line_number;
162 char *message;
163 va_dcl
164 #endif
166 #ifdef VA_START
167 va_list args;
168 #endif
170 if (error_one_per_line)
172 static const char *old_file_name;
173 static unsigned int old_line_number;
175 if (old_line_number == line_number &&
176 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
177 /* Simply return and print nothing. */
178 return;
180 old_file_name = file_name;
181 old_line_number = line_number;
184 if (error_print_progname)
185 (*error_print_progname) ();
186 else
188 fflush (stdout);
189 fprintf (stderr, "%s:", program_name);
192 if (file_name != NULL)
193 fprintf (stderr, "%s:%d: ", file_name, line_number);
195 #ifdef VA_START
196 VA_START (args, message);
197 # if HAVE_VPRINTF || _LIBC
198 vfprintf (stderr, message, args);
199 # else
200 _doprnt (message, args, stderr);
201 # endif
202 va_end (args);
203 #else
204 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
205 #endif
207 ++error_message_count;
208 if (errnum)
209 fprintf (stderr, ": %s", strerror (errnum));
210 putc ('\n', stderr);
211 fflush (stderr);
212 if (status)
213 exit (status);