A bug fix for the previous select bug fix.
[glibc.git] / misc / error.c
blobc48c0f3c46e6833f530ab5cc543705a8eb8aa431
1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96 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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, 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 /* If NULL, error will flush stdout, then print on stderr the program
51 name, a colon and a space. Otherwise, error will call this
52 function without parameters instead. */
53 void (*error_print_progname) (
54 #if __STDC__
55 void
56 #endif
59 /* This variable is incremented each time `error' is called. */
60 unsigned int error_message_count;
62 #ifdef _LIBC
63 /* In the GNU C library, there is a predefined variable for this. */
65 #define program_name program_invocation_name
66 #include <errno.h>
68 #else
70 /* The calling program should define program_name and set it to the
71 name of the executing program. */
72 extern char *program_name;
74 #if HAVE_STRERROR
75 # ifndef strerror /* On some systems, strerror is a macro */
76 char *strerror ();
77 # endif
78 #else
79 static char *
80 private_strerror (errnum)
81 int errnum;
83 extern char *sys_errlist[];
84 extern int sys_nerr;
86 if (errnum > 0 && errnum <= sys_nerr)
87 return sys_errlist[errnum];
88 return "Unknown system error";
90 #define strerror private_strerror
91 #endif /* HAVE_STRERROR */
92 #endif /* _LIBC */
94 /* Print the program name and error message MESSAGE, which is a printf-style
95 format string with optional args.
96 If ERRNUM is nonzero, print its corresponding system error message.
97 Exit with status STATUS if it is nonzero. */
98 /* VARARGS */
100 void
101 #if defined(VA_START) && __STDC__
102 error (int status, int errnum, const char *message, ...)
103 #else
104 error (status, errnum, message, va_alist)
105 int status;
106 int errnum;
107 char *message;
108 va_dcl
109 #endif
111 #ifdef VA_START
112 va_list args;
113 #endif
115 if (error_print_progname)
116 (*error_print_progname) ();
117 else
119 fflush (stdout);
120 fprintf (stderr, "%s: ", program_name);
123 #ifdef VA_START
124 VA_START (args, message);
125 # if HAVE_VPRINTF || _LIBC
126 vfprintf (stderr, message, args);
127 # else
128 _doprnt (message, args, stderr);
129 # endif
130 va_end (args);
131 #else
132 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
133 #endif
135 ++error_message_count;
137 if (errnum)
138 fprintf (stderr, ": %s", strerror (errnum));
139 putc ('\n', stderr);
140 fflush (stderr);
141 if (status)
142 exit (status);