1 1.1 christos /* $NetBSD: error.c,v 1.1.1.1 2016/01/10 21:36:18 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* Error handler for noninteractive utilities 4 1.1 christos Copyright (C) 1990-1998, 2000 Free Software Foundation, Inc. 5 1.1 christos 6 1.1 christos This file is part of the GNU C Library. Its master source is NOT part of 7 1.1 christos the C library, however. The master source lives in /gd/gnu/lib. 8 1.1 christos 9 1.1 christos The GNU C Library is free software; you can redistribute it and/or 10 1.1 christos modify it under the terms of the GNU Library General Public License as 11 1.1 christos published by the Free Software Foundation; either version 2 of the 12 1.1 christos License, or (at your option) any later version. 13 1.1 christos 14 1.1 christos The GNU C Library is distributed in the hope that it will be useful, 15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 1.1 christos Library General Public License for more details. 18 1.1 christos 19 1.1 christos You should have received a copy of the GNU Library General Public 20 1.1 christos License along with the GNU C Library; see the file COPYING.LIB. If not, 21 1.1 christos write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 22 1.1 christos Boston, MA 02111-1307, USA. */ 23 1.1 christos 24 1.1 christos /* Written by David MacKenzie <djm (at) gnu.ai.mit.edu>. */ 25 1.1 christos 26 1.1 christos #ifdef HAVE_CONFIG_H 27 1.1 christos # include <config.h> 28 1.1 christos #endif 29 1.1 christos 30 1.1 christos #include <stdio.h> 31 1.1 christos #if HAVE_LIBINTL_H 32 1.1 christos # include <libintl.h> 33 1.1 christos #endif 34 1.1 christos 35 1.1 christos #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC 36 1.1 christos # if __STDC__ 37 1.1 christos # include <stdarg.h> 38 1.1 christos # define VA_START(args, lastarg) va_start(args, lastarg) 39 1.1 christos # else 40 1.1 christos # include <varargs.h> 41 1.1 christos # define VA_START(args, lastarg) va_start(args) 42 1.1 christos # endif 43 1.1 christos #else 44 1.1 christos # define va_alist a1, a2, a3, a4, a5, a6, a7, a8 45 1.1 christos # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; 46 1.1 christos #endif 47 1.1 christos 48 1.1 christos #if STDC_HEADERS || _LIBC 49 1.1 christos # include <stdlib.h> 50 1.1 christos # include <string.h> 51 1.1 christos #else 52 1.1 christos void exit (); 53 1.1 christos #endif 54 1.1 christos 55 1.1 christos #include "error.h" 56 1.1 christos 57 1.1 christos #ifndef HAVE_DECL_STRERROR_R 58 1.1 christos "this configure-time declaration test was not run" 59 1.1 christos #endif 60 1.1 christos #if !HAVE_DECL_STRERROR_R 61 1.1 christos char *strerror_r (); 62 1.1 christos #endif 63 1.1 christos 64 1.1 christos #ifndef _ 65 1.1 christos # define _(String) String 66 1.1 christos #endif 67 1.1 christos 68 1.1 christos /* If NULL, error will flush stdout, then print on stderr the program 69 1.1 christos name, a colon and a space. Otherwise, error will call this 70 1.1 christos function without parameters instead. */ 71 1.1 christos void (*error_print_progname) ( 72 1.1 christos #if __STDC__ - 0 73 1.1 christos void 74 1.1 christos #endif 75 1.1 christos ); 76 1.1 christos 77 1.1 christos /* This variable is incremented each time `error' is called. */ 78 1.1 christos unsigned int error_message_count; 79 1.1 christos 80 1.1 christos #ifdef _LIBC 81 1.1 christos /* In the GNU C library, there is a predefined variable for this. */ 82 1.1 christos 83 1.1 christos # define program_name program_invocation_name 84 1.1 christos # include <errno.h> 85 1.1 christos 86 1.1 christos /* In GNU libc we want do not want to use the common name `error' directly. 87 1.1 christos Instead make it a weak alias. */ 88 1.1 christos # define error __error 89 1.1 christos # define error_at_line __error_at_line 90 1.1 christos 91 1.1 christos # ifdef USE_IN_LIBIO 92 1.1 christos # include <libio/iolibio.h> 93 1.1 christos # define fflush(s) _IO_fflush (s) 94 1.1 christos # endif 95 1.1 christos 96 1.1 christos #else /* not _LIBC */ 97 1.1 christos 98 1.1 christos /* The calling program should define program_name and set it to the 99 1.1 christos name of the executing program. */ 100 1.1 christos extern char *program_name; 101 1.1 christos 102 1.1 christos # ifdef HAVE_STRERROR_R 103 1.1 christos # define __strerror_r strerror_r 104 1.1 christos # else 105 1.1 christos # if HAVE_STRERROR 106 1.1 christos # ifndef strerror /* On some systems, strerror is a macro */ 107 1.1 christos char *strerror (); 108 1.1 christos # endif 109 1.1 christos # else 110 1.1 christos static char * 111 1.1 christos private_strerror (errnum) 112 1.1 christos int errnum; 113 1.1 christos { 114 1.1 christos extern char *sys_errlist[]; 115 1.1 christos extern int sys_nerr; 116 1.1 christos 117 1.1 christos if (errnum > 0 && errnum <= sys_nerr) 118 1.1 christos return _(sys_errlist[errnum]); 119 1.1 christos return _("Unknown system error"); 120 1.1 christos } 121 1.1 christos # define strerror private_strerror 122 1.1 christos # endif /* HAVE_STRERROR */ 123 1.1 christos # endif /* HAVE_STRERROR_R */ 124 1.1 christos #endif /* not _LIBC */ 125 1.1 christos 126 1.1 christos /* Print the program name and error message MESSAGE, which is a printf-style 127 1.1 christos format string with optional args. 128 1.1 christos If ERRNUM is nonzero, print its corresponding system error message. 129 1.1 christos Exit with status STATUS if it is nonzero. */ 130 1.1 christos /* VARARGS */ 131 1.1 christos 132 1.1 christos void 133 1.1 christos #if defined VA_START && __STDC__ 134 1.1 christos error (int status, int errnum, const char *message, ...) 135 1.1 christos #else 136 1.1 christos error (status, errnum, message, va_alist) 137 1.1 christos int status; 138 1.1 christos int errnum; 139 1.1 christos char *message; 140 1.1 christos va_dcl 141 1.1 christos #endif 142 1.1 christos { 143 1.1 christos #ifdef VA_START 144 1.1 christos va_list args; 145 1.1 christos #endif 146 1.1 christos 147 1.1 christos if (error_print_progname) 148 1.1 christos (*error_print_progname) (); 149 1.1 christos else 150 1.1 christos { 151 1.1 christos fflush (stdout); 152 1.1 christos fprintf (stderr, "%s: ", program_name); 153 1.1 christos } 154 1.1 christos 155 1.1 christos #ifdef VA_START 156 1.1 christos VA_START (args, message); 157 1.1 christos # if HAVE_VPRINTF || _LIBC 158 1.1 christos vfprintf (stderr, message, args); 159 1.1 christos # else 160 1.1 christos _doprnt (message, args, stderr); 161 1.1 christos # endif 162 1.1 christos va_end (args); 163 1.1 christos #else 164 1.1 christos fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); 165 1.1 christos #endif 166 1.1 christos 167 1.1 christos ++error_message_count; 168 1.1 christos if (errnum) 169 1.1 christos { 170 1.1 christos #if defined HAVE_STRERROR_R || _LIBC 171 1.1 christos char errbuf[1024]; 172 1.1 christos # if HAVE_WORKING_STRERROR_R || _LIBC 173 1.1 christos fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); 174 1.1 christos # else 175 1.1 christos /* Don't use __strerror_r's return value because on some systems 176 1.1 christos (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */ 177 1.1 christos __strerror_r (errnum, errbuf, sizeof errbuf); 178 1.1 christos fprintf (stderr, ": %s", errbuf); 179 1.1 christos # endif 180 1.1 christos #else 181 1.1 christos fprintf (stderr, ": %s", strerror (errnum)); 182 1.1 christos #endif 183 1.1 christos } 184 1.1 christos putc ('\n', stderr); 185 1.1 christos fflush (stderr); 186 1.1 christos if (status) 187 1.1 christos exit (status); 188 1.1 christos } 189 1.1 christos 190 1.1 christos /* Sometimes we want to have at most one error per line. This 192 1.1 christos variable controls whether this mode is selected or not. */ 193 1.1 christos int error_one_per_line; 194 1.1 christos 195 1.1 christos void 196 1.1 christos #if defined VA_START && __STDC__ 197 1.1 christos error_at_line (int status, int errnum, const char *file_name, 198 1.1 christos unsigned int line_number, const char *message, ...) 199 1.1 christos #else 200 1.1 christos error_at_line (status, errnum, file_name, line_number, message, va_alist) 201 1.1 christos int status; 202 1.1 christos int errnum; 203 1.1 christos const char *file_name; 204 1.1 christos unsigned int line_number; 205 1.1 christos char *message; 206 1.1 christos va_dcl 207 1.1 christos #endif 208 1.1 christos { 209 1.1 christos #ifdef VA_START 210 1.1 christos va_list args; 211 1.1 christos #endif 212 1.1 christos 213 1.1 christos if (error_one_per_line) 214 1.1 christos { 215 1.1 christos static const char *old_file_name; 216 1.1 christos static unsigned int old_line_number; 217 1.1 christos 218 1.1 christos if (old_line_number == line_number && 219 1.1 christos (file_name == old_file_name || !strcmp (old_file_name, file_name))) 220 1.1 christos /* Simply return and print nothing. */ 221 1.1 christos return; 222 1.1 christos 223 1.1 christos old_file_name = file_name; 224 1.1 christos old_line_number = line_number; 225 1.1 christos } 226 1.1 christos 227 1.1 christos if (error_print_progname) 228 1.1 christos (*error_print_progname) (); 229 1.1 christos else 230 1.1 christos { 231 1.1 christos fflush (stdout); 232 1.1 christos fprintf (stderr, "%s:", program_name); 233 1.1 christos } 234 1.1 christos 235 1.1 christos if (file_name != NULL) 236 1.1 christos fprintf (stderr, "%s:%d: ", file_name, line_number); 237 1.1 christos 238 1.1 christos #ifdef VA_START 239 1.1 christos VA_START (args, message); 240 1.1 christos # if HAVE_VPRINTF || _LIBC 241 1.1 christos vfprintf (stderr, message, args); 242 1.1 christos # else 243 1.1 christos _doprnt (message, args, stderr); 244 1.1 christos # endif 245 1.1 christos va_end (args); 246 1.1 christos #else 247 1.1 christos fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); 248 1.1 christos #endif 249 1.1 christos 250 1.1 christos ++error_message_count; 251 1.1 christos if (errnum) 252 1.1 christos { 253 1.1 christos #if defined HAVE_STRERROR_R || _LIBC 254 1.1 christos char errbuf[1024]; 255 1.1 christos # if HAVE_WORKING_STRERROR_R || _LIBC 256 1.1 christos fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); 257 1.1 christos # else 258 1.1 christos /* Don't use __strerror_r's return value because on some systems 259 1.1 christos (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */ 260 1.1 christos __strerror_r (errnum, errbuf, sizeof errbuf); 261 1.1 christos fprintf (stderr, ": %s", errbuf); 262 1.1 christos # endif 263 1.1 christos #else 264 1.1 christos fprintf (stderr, ": %s", strerror (errnum)); 265 1.1 christos #endif 266 1.1 christos } 267 1.1 christos putc ('\n', stderr); 268 1.1 christos fflush (stderr); 269 1.1 christos if (status) 270 1.1 christos exit (status); 271 1.1 christos } 272 1.1 christos 273 1.1 christos #ifdef _LIBC 274 1.1 christos /* Make the weak alias. */ 275 1.1 christos # undef error 276 1.1 christos # undef error_at_line 277 1.1 christos weak_alias (__error, error) 278 1.1 christos weak_alias (__error_at_line, error_at_line) 279 #endif 280