source: trunk/third/findutils/lib/error.c @ 18890

Revision 18890, 6.6 KB checked in by zacheiss, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18889, which included commits to RCS files with non-trunk default branches.
Line 
1/* Error handler for noninteractive utilities
2   Copyright (C) 1990-2000 Free Software Foundation, Inc.
3
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.
6
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.
11
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.
16
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 not,
19   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20   Boston, MA 02111-1307, USA.  */
21
22/* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
23
24#ifdef HAVE_CONFIG_H
25# include <config.h>
26#endif
27
28#include <stdio.h>
29
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
42
43#if STDC_HEADERS || _LIBC
44# include <stdlib.h>
45# include <string.h>
46#else
47void exit ();
48#endif
49
50#include "error.h"
51
52#ifndef _
53# define _(String) String
54#endif
55
56/* If NULL, error will flush stdout, then print on stderr the program
57   name, a colon and a space.  Otherwise, error will call this
58   function without parameters instead.  */
59void (*error_print_progname) (
60#if __STDC__ - 0
61                              void
62#endif
63                              );
64
65/* This variable is incremented each time `error' is called.  */
66unsigned int error_message_count;
67
68#ifdef _LIBC
69/* In the GNU C library, there is a predefined variable for this.  */
70
71# define program_name program_invocation_name
72# include <errno.h>
73
74/* In GNU libc we want do not want to use the common name `error' directly.
75   Instead make it a weak alias.  */
76# define error __error
77# define error_at_line __error_at_line
78
79#else /* not _LIBC */
80
81/* The calling program should define program_name and set it to the
82   name of the executing program.  */
83extern char *program_name;
84
85# ifdef HAVE_STRERROR_R
86#  define __strerror_r strerror_r
87# else
88#  if HAVE_STRERROR
89#   ifndef strerror             /* On some systems, strerror is a macro */
90char *strerror ();
91#   endif
92#  else
93static char *
94private_strerror (errnum)
95     int errnum;
96{
97  extern char *sys_errlist[];
98  extern int sys_nerr;
99
100  if (errnum > 0 && errnum <= sys_nerr)
101    return _(sys_errlist[errnum]);
102  return _("Unknown system error");
103}
104#   define strerror private_strerror
105#  endif /* HAVE_STRERROR */
106# endif /* HAVE_STRERROR_R */
107#endif  /* not _LIBC */
108
109/* Print the program name and error message MESSAGE, which is a printf-style
110   format string with optional args.
111   If ERRNUM is nonzero, print its corresponding system error message.
112   Exit with status STATUS if it is nonzero.  */
113/* VARARGS */
114
115void
116#if defined VA_START && __STDC__
117error (int status, int errnum, const char *message, ...)
118#else
119error (status, errnum, message, va_alist)
120     int status;
121     int errnum;
122     char *message;
123     va_dcl
124#endif
125{
126#ifdef VA_START
127  va_list args;
128#endif
129
130  if (error_print_progname)
131    (*error_print_progname) ();
132  else
133    {
134      fflush (stdout);
135      fprintf (stderr, "%s: ", program_name);
136    }
137
138#ifdef VA_START
139  VA_START (args, message);
140# if HAVE_VPRINTF || _LIBC
141  vfprintf (stderr, message, args);
142# else
143  _doprnt (message, args, stderr);
144# endif
145  va_end (args);
146#else
147  fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
148#endif
149
150  ++error_message_count;
151  if (errnum)
152    {
153#if defined HAVE_STRERROR_R || _LIBC
154      char errbuf[1024];
155# if HAVE_WORKING_STRERROR_R || _LIBC
156      fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
157# else
158      /* Don't use __strerror_r's return value because on some systems
159         (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
160      __strerror_r (errnum, errbuf, sizeof errbuf);
161      fprintf (stderr, ": %s", errbuf);
162# endif
163#else
164      fprintf (stderr, ": %s", strerror (errnum));
165#endif
166    }
167  putc ('\n', stderr);
168  fflush (stderr);
169  if (status)
170    exit (status);
171}
172
173/* Sometimes we want to have at most one error per line.  This
174   variable controls whether this mode is selected or not.  */
175int error_one_per_line;
176
177void
178#if defined VA_START && __STDC__
179error_at_line (int status, int errnum, const char *file_name,
180               unsigned int line_number, const char *message, ...)
181#else
182error_at_line (status, errnum, file_name, line_number, message, va_alist)
183     int status;
184     int errnum;
185     const char *file_name;
186     unsigned int line_number;
187     char *message;
188     va_dcl
189#endif
190{
191#ifdef VA_START
192  va_list args;
193#endif
194
195  if (error_one_per_line)
196    {
197      static const char *old_file_name;
198      static unsigned int old_line_number;
199
200      if (old_line_number == line_number &&
201          (file_name == old_file_name || !strcmp (old_file_name, file_name)))
202        /* Simply return and print nothing.  */
203        return;
204
205      old_file_name = file_name;
206      old_line_number = line_number;
207    }
208
209  if (error_print_progname)
210    (*error_print_progname) ();
211  else
212    {
213      fflush (stdout);
214      fprintf (stderr, "%s:", program_name);
215    }
216
217  if (file_name != NULL)
218    fprintf (stderr, "%s:%d: ", file_name, line_number);
219
220#ifdef VA_START
221  VA_START (args, message);
222# if HAVE_VPRINTF || _LIBC
223  vfprintf (stderr, message, args);
224# else
225  _doprnt (message, args, stderr);
226# endif
227  va_end (args);
228#else
229  fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
230#endif
231
232  ++error_message_count;
233  if (errnum)
234    {
235#if defined HAVE_STRERROR_R || _LIBC
236      char errbuf[1024];
237# if HAVE_WORKING_STRERROR_R || _LIBC
238      fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
239# else
240      /* Don't use __strerror_r's return value because on some systems
241         (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
242      __strerror_r (errnum, errbuf, sizeof errbuf);
243      fprintf (stderr, ": %s", errbuf);
244# endif
245#else
246      fprintf (stderr, ": %s", strerror (errnum));
247#endif
248    }
249  putc ('\n', stderr);
250  fflush (stderr);
251  if (status)
252    exit (status);
253}
254
255#ifdef _LIBC
256/* Make the weak alias.  */
257# undef error
258# undef error_at_line
259weak_alias (__error, error)
260weak_alias (__error_at_line, error_at_line)
261#endif
Note: See TracBrowser for help on using the repository browser.