3d2777bc2c75820c6a0f93467db2fad15a7b928e
[platform/upstream/glibc.git] / misc / error.c
1 /* error.c -- error handler for noninteractive utilities
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96 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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, 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
47 void 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.  */
59 void (*error_print_progname) (
60 #if __STDC__ - 0
61                               void
62 #endif
63                               );
64
65 /* This variable is incremented each time `error' is called.  */
66 unsigned 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 #else
75
76 /* The calling program should define program_name and set it to the
77    name of the executing program.  */
78 extern char *program_name;
79
80 #if HAVE_STRERROR
81 # ifndef strerror               /* On some systems, strerror is a macro */
82 char *strerror ();
83 # endif
84 #else
85 static char *
86 private_strerror (errnum)
87      int errnum;
88 {
89   extern char *sys_errlist[];
90   extern int sys_nerr;
91
92   if (errnum > 0 && errnum <= sys_nerr)
93     return _(sys_errlist[errnum]);
94   return _("Unknown system error");
95 }
96 #define strerror private_strerror
97 #endif  /* HAVE_STRERROR */
98 #endif  /* _LIBC */
99
100 /* Print the program name and error message MESSAGE, which is a printf-style
101    format string with optional args.
102    If ERRNUM is nonzero, print its corresponding system error message.
103    Exit with status STATUS if it is nonzero.  */
104 /* VARARGS */
105
106 void
107 #if defined(VA_START) && __STDC__
108 error (int status, int errnum, const char *message, ...)
109 #else
110 error (status, errnum, message, va_alist)
111      int status;
112      int errnum;
113      char *message;
114      va_dcl
115 #endif
116 {
117 #ifdef VA_START
118   va_list args;
119 #endif
120
121   if (error_print_progname)
122     (*error_print_progname) ();
123   else
124     {
125       fflush (stdout);
126       fprintf (stderr, "%s: ", program_name);
127     }
128
129 #ifdef VA_START
130   VA_START (args, message);
131 # if HAVE_VPRINTF || _LIBC
132   vfprintf (stderr, message, args);
133 # else
134   _doprnt (message, args, stderr);
135 # endif
136   va_end (args);
137 #else
138   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
139 #endif
140
141   ++error_message_count;
142   if (errnum)
143     fprintf (stderr, ": %s", strerror (errnum));
144   putc ('\n', stderr);
145   fflush (stderr);
146   if (status)
147     exit (status);
148 }
149 \f
150 /* Sometimes we want to have at most one error per line.  This
151    variable controls whether this mode is selected or not.  */
152 int error_one_per_line;
153
154 void
155 #if defined(VA_START) && __STDC__
156 error_at_line (int status, int errnum, const char *file_name,
157                unsigned int line_number, const char *message, ...)
158 #else
159 error_at_line (status, errnum, file_name, line_number, message, va_alist)
160      int status;
161      int errnum;
162      const char *file_name;
163      unsigned int line_number;
164      char *message;
165      va_dcl
166 #endif
167 {
168 #ifdef VA_START
169   va_list args;
170 #endif
171
172   if (error_one_per_line)
173     {
174       static const char *old_file_name;
175       static unsigned int old_line_number;
176
177       if (old_line_number == line_number &&
178           (file_name == old_file_name || !strcmp (old_file_name, file_name)))
179         /* Simply return and print nothing.  */
180         return;
181
182       old_file_name = file_name;
183       old_line_number = line_number;
184     }
185
186   if (error_print_progname)
187     (*error_print_progname) ();
188   else
189     {
190       fflush (stdout);
191       fprintf (stderr, "%s:", program_name);
192     }
193
194   if (file_name != NULL)
195     fprintf (stderr, "%s:%d: ", file_name, line_number);
196
197 #ifdef VA_START
198   VA_START (args, message);
199 # if HAVE_VPRINTF || _LIBC
200   vfprintf (stderr, message, args);
201 # else
202   _doprnt (message, args, stderr);
203 # endif
204   va_end (args);
205 #else
206   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
207 #endif
208
209   ++error_message_count;
210   if (errnum)
211     fprintf (stderr, ": %s", strerror (errnum));
212   putc ('\n', stderr);
213   fflush (stderr);
214   if (status)
215     exit (status);
216 }