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)
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>. */
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
29 # define VA_START(args, lastarg) va_start(args, lastarg)
32 # define VA_START(args, lastarg) va_start(args)
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;
39 #if STDC_HEADERS || _LIBC
47 # define _(String) String
50 /* Get prototypes for the functions defined here. */
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
) (
62 /* This variable is incremented each time `error' is called. */
63 unsigned int error_message_count
;
66 /* In the GNU C library, there is a predefined variable for this. */
68 # define program_name program_invocation_name
73 /* The calling program should define program_name and set it to the
74 name of the executing program. */
75 extern char *program_name
;
78 # ifndef strerror /* On some systems, strerror is a macro */
83 private_strerror (errnum
)
86 extern char *sys_errlist
[];
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. */
104 #if defined(VA_START) && __STDC__
105 error (int status
, int errnum
, const char *message
, ...)
107 error (status
, errnum
, message
, va_alist
)
118 if (error_print_progname
)
119 (*error_print_progname
) ();
123 fprintf (stderr
, "%s: ", program_name
);
127 VA_START (args
, message
);
128 # if HAVE_VPRINTF || _LIBC
129 vfprintf (stderr
, message
, args
);
131 _doprnt (message
, args
, stderr
);
135 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
138 ++error_message_count
;
140 fprintf (stderr
, ": %s", strerror (errnum
));
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
;
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
, ...)
156 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
159 const char *file_name
;
160 unsigned int line_number
;
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. */
179 old_file_name
= file_name
;
180 old_line_number
= line_number
;
183 if (error_print_progname
)
184 (*error_print_progname
) ();
188 fprintf (stderr
, "%s:", program_name
);
191 if (file_name
!= NULL
)
192 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
195 VA_START (args
, message
);
196 # if HAVE_VPRINTF || _LIBC
197 vfprintf (stderr
, message
, args
);
199 _doprnt (message
, args
, stderr
);
203 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
206 ++error_message_count
;
208 fprintf (stderr
, ": %s", strerror (errnum
));