Use _dl_pagesize instead of calling getpagesize.
[glibc/history.git] / misc / error.c
blobf49e4a7983fdddc888d43f5116fb2cb2dc6d9499
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990,91,92,93,94,95,96,97 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 not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 #include "error.h"
52 #ifndef _
53 #define _(String) String
54 #endif
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
65 /* This variable is incremented each time `error' is called. */
66 unsigned int error_message_count;
68 #ifdef _LIBC
69 /* In the GNU C library, there is a predefined variable for this. */
71 #define program_name program_invocation_name
72 #include <errno.h>
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
79 #else
81 /* The calling program should define program_name and set it to the
82 name of the executing program. */
83 extern char *program_name;
85 #ifndef HAVE_STRERROR_R
86 # if HAVE_STRERROR
87 # ifndef strerror /* On some systems, strerror is a macro */
88 char *strerror ();
89 # endif
90 # else
91 static char *
92 private_strerror (errnum)
93 int errnum;
95 extern char *sys_errlist[];
96 extern int sys_nerr;
98 if (errnum > 0 && errnum <= sys_nerr)
99 return _(sys_errlist[errnum]);
100 return _("Unknown system error");
102 # define strerror private_strerror
103 # endif /* HAVE_STRERROR */
104 #endif /* HAVE_STRERROR_R */
105 #endif /* _LIBC */
107 /* Print the program name and error message MESSAGE, which is a printf-style
108 format string with optional args.
109 If ERRNUM is nonzero, print its corresponding system error message.
110 Exit with status STATUS if it is nonzero. */
111 /* VARARGS */
113 void
114 #if defined(VA_START) && __STDC__
115 error (int status, int errnum, const char *message, ...)
116 #else
117 error (status, errnum, message, va_alist)
118 int status;
119 int errnum;
120 char *message;
121 va_dcl
122 #endif
124 #ifdef VA_START
125 va_list args;
126 #endif
128 if (error_print_progname)
129 (*error_print_progname) ();
130 else
132 fflush (stdout);
133 fprintf (stderr, "%s: ", program_name);
136 #ifdef VA_START
137 VA_START (args, message);
138 # if HAVE_VPRINTF || _LIBC
139 vfprintf (stderr, message, args);
140 # else
141 _doprnt (message, args, stderr);
142 # endif
143 va_end (args);
144 #else
145 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
146 #endif
148 ++error_message_count;
149 if (errnum)
151 #if defined HAVE_STRERROR_R || defined _LIBC
152 char errbuf[1024];
153 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
154 #else
155 fprintf (stderr, ": %s", strerror (errnum));
156 #endif
158 putc ('\n', stderr);
159 fflush (stderr);
160 if (status)
161 exit (status);
164 /* Sometimes we want to have at most one error per line. This
165 variable controls whether this mode is selected or not. */
166 int error_one_per_line;
168 void
169 #if defined(VA_START) && __STDC__
170 error_at_line (int status, int errnum, const char *file_name,
171 unsigned int line_number, const char *message, ...)
172 #else
173 error_at_line (status, errnum, file_name, line_number, message, va_alist)
174 int status;
175 int errnum;
176 const char *file_name;
177 unsigned int line_number;
178 char *message;
179 va_dcl
180 #endif
182 #ifdef VA_START
183 va_list args;
184 #endif
186 if (error_one_per_line)
188 static const char *old_file_name;
189 static unsigned int old_line_number;
191 if (old_line_number == line_number &&
192 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
193 /* Simply return and print nothing. */
194 return;
196 old_file_name = file_name;
197 old_line_number = line_number;
200 if (error_print_progname)
201 (*error_print_progname) ();
202 else
204 fflush (stdout);
205 fprintf (stderr, "%s:", program_name);
208 if (file_name != NULL)
209 fprintf (stderr, "%s:%d: ", file_name, line_number);
211 #ifdef VA_START
212 VA_START (args, message);
213 # if HAVE_VPRINTF || _LIBC
214 vfprintf (stderr, message, args);
215 # else
216 _doprnt (message, args, stderr);
217 # endif
218 va_end (args);
219 #else
220 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
221 #endif
223 ++error_message_count;
224 if (errnum)
226 #if defined HAVE_STRERROR_R || defined _LIBC
227 char errbuf[1024];
228 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
229 #else
230 fprintf (stderr, ": %s", strerror (errnum));
231 #endif
233 putc ('\n', stderr);
234 fflush (stderr);
235 if (status)
236 exit (status);
239 #ifdef _LIBC
240 /* Make the weak alias. */
241 #undef error
242 #undef error_at_line
243 weak_alias (__error, error)
244 weak_alias (__error_at_line, error_at_line)
245 #endif