.
[coreutils.git] / lib / error.c
blobb167b830c9278d12eea84794e2377844f228cf07
1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
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.
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.
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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdio.h>
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
27 # if __STDC__
28 # include <stdarg.h>
29 # define VA_START(args, lastarg) va_start(args, lastarg)
30 # else
31 # include <varargs.h>
32 # define VA_START(args, lastarg) va_start(args)
33 # endif
34 #else
35 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
36 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
37 #endif
39 #if STDC_HEADERS || _LIBC
40 # include <stdlib.h>
41 # include <string.h>
42 #else
43 void exit ();
44 #endif
46 #ifndef _
47 # define _(String) String
48 #endif
50 /* Get prototypes for the functions defined here. */
51 #include <error.h>
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) (
57 #if __STDC__ - 0
58 void
59 #endif
62 /* This variable is incremented each time `error' is called. */
63 unsigned int error_message_count;
65 #ifdef _LIBC
66 /* In the GNU C library, there is a predefined variable for this. */
68 # define program_name program_invocation_name
69 # include <errno.h>
71 #else /* not _LIBC */
73 /* The calling program should define program_name and set it to the
74 name of the executing program. */
75 extern char *program_name;
77 # if HAVE_STRERROR
78 # ifndef strerror /* On some systems, strerror is a macro */
79 char *strerror ();
80 # endif
81 # else
82 static char *
83 private_strerror (errnum)
84 int errnum;
86 extern char *sys_errlist[];
87 extern int sys_nerr;
89 if (errnum > 0 && errnum <= sys_nerr)
90 return sys_errlist[errnum];
91 return _("Unknown system error");
93 # define strerror private_strerror
94 # endif /* HAVE_STRERROR */
95 #endif /* not _LIBC */
97 /* Print the program name and error message MESSAGE, which is a printf-style
98 format string with optional args.
99 If ERRNUM is nonzero, print its corresponding system error message.
100 Exit with status STATUS if it is nonzero. */
101 /* VARARGS */
103 void
104 #if defined(VA_START) && __STDC__
105 error (int status, int errnum, const char *message, ...)
106 #else
107 error (status, errnum, message, va_alist)
108 int status;
109 int errnum;
110 char *message;
111 va_dcl
112 #endif
114 #ifdef VA_START
115 va_list args;
116 #endif
118 if (error_print_progname)
119 (*error_print_progname) ();
120 else
122 fflush (stdout);
123 fprintf (stderr, "%s: ", program_name);
126 #ifdef VA_START
127 VA_START (args, message);
128 # if HAVE_VPRINTF || _LIBC
129 vfprintf (stderr, message, args);
130 # else
131 _doprnt (message, args, stderr);
132 # endif
133 va_end (args);
134 #else
135 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
136 #endif
138 ++error_message_count;
139 if (errnum)
140 fprintf (stderr, ": %s", strerror (errnum));
141 putc ('\n', stderr);
142 fflush (stderr);
143 if (status)
144 exit (status);
147 /* Sometimes we want to have at most one error per line. This
148 variable controls whether this mode is selected or not. */
149 int error_one_per_line;
151 void
152 #if defined(VA_START) && __STDC__
153 error_at_line (int status, int errnum, const char *file_name,
154 unsigned int line_number, const char *message, ...)
155 #else
156 error_at_line (status, errnum, file_name, line_number, message, va_alist)
157 int status;
158 int errnum;
159 const char *file_name;
160 unsigned int line_number;
161 char *message;
162 va_dcl
163 #endif
165 #ifdef VA_START
166 va_list args;
167 #endif
169 if (error_one_per_line)
171 static const char *old_file_name;
172 static unsigned int old_line_number;
174 if (old_line_number == line_number &&
175 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
176 /* Simply return and print nothing. */
177 return;
179 old_file_name = file_name;
180 old_line_number = line_number;
183 if (error_print_progname)
184 (*error_print_progname) ();
185 else
187 fflush (stdout);
188 fprintf (stderr, "%s:", program_name);
191 if (file_name != NULL)
192 fprintf (stderr, "%s:%d: ", file_name, line_number);
194 #ifdef VA_START
195 VA_START (args, message);
196 # if HAVE_VPRINTF || _LIBC
197 vfprintf (stderr, message, args);
198 # else
199 _doprnt (message, args, stderr);
200 # endif
201 va_end (args);
202 #else
203 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
204 #endif
206 ++error_message_count;
207 if (errnum)
208 fprintf (stderr, ": %s", strerror (errnum));
209 putc ('\n', stderr);
210 fflush (stderr);
211 if (status)
212 exit (status);