1131554Stjr/* Error handler for noninteractive utilities 2131554Stjr Copyright (C) 1990-1998, 2000 Free Software Foundation, Inc. 3131554Stjr 4131554Stjr This file is part of the GNU C Library. Its master source is NOT part of 5131554Stjr the C library, however. The master source lives in /gd/gnu/lib. 6131554Stjr 7131554Stjr The GNU C Library is free software; you can redistribute it and/or 8131554Stjr modify it under the terms of the GNU Library General Public License as 9131554Stjr published by the Free Software Foundation; either version 2 of the 10131554Stjr License, or (at your option) any later version. 11131554Stjr 12131554Stjr The GNU C Library is distributed in the hope that it will be useful, 13131554Stjr but WITHOUT ANY WARRANTY; without even the implied warranty of 14131554Stjr MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15131554Stjr Library General Public License for more details. 16131554Stjr 17131554Stjr You should have received a copy of the GNU Library General Public 18131554Stjr License along with the GNU C Library; see the file COPYING.LIB. If not, 19131554Stjr write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20131554Stjr Boston, MA 02111-1307, USA. */ 21131554Stjr 22131554Stjr/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ 23131554Stjr 24131554Stjr#ifdef HAVE_CONFIG_H 25131554Stjr# include <config.h> 26131554Stjr#endif 27131554Stjr 28131554Stjr#include <stdio.h> 29131554Stjr#if HAVE_LIBINTL_H 30131554Stjr# include <libintl.h> 31131554Stjr#endif 32131554Stjr 33131554Stjr#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC 34131554Stjr# if __STDC__ 35131554Stjr# include <stdarg.h> 36131554Stjr# define VA_START(args, lastarg) va_start(args, lastarg) 37131554Stjr# else 38131554Stjr# include <varargs.h> 39131554Stjr# define VA_START(args, lastarg) va_start(args) 40131554Stjr# endif 41131554Stjr#else 42131554Stjr# define va_alist a1, a2, a3, a4, a5, a6, a7, a8 43131554Stjr# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; 44131554Stjr#endif 45131554Stjr 46131554Stjr#if STDC_HEADERS || _LIBC 47131554Stjr# include <stdlib.h> 48131554Stjr# include <string.h> 49131554Stjr#else 50131554Stjrvoid exit (); 51131554Stjr#endif 52131554Stjr 53131554Stjr#include "error.h" 54131554Stjr 55131554Stjr#ifndef HAVE_DECL_STRERROR_R 56131554Stjr"this configure-time declaration test was not run" 57131554Stjr#endif 58131554Stjr#if !HAVE_DECL_STRERROR_R 59131554Stjrchar *strerror_r (); 60131554Stjr#endif 61131554Stjr 62131554Stjr#ifndef _ 63131554Stjr# define _(String) String 64131554Stjr#endif 65131554Stjr 66131554Stjr/* If NULL, error will flush stdout, then print on stderr the program 67131554Stjr name, a colon and a space. Otherwise, error will call this 68131554Stjr function without parameters instead. */ 69131554Stjrvoid (*error_print_progname) ( 70131554Stjr#if __STDC__ - 0 71131554Stjr void 72131554Stjr#endif 73131554Stjr ); 74131554Stjr 75131554Stjr/* This variable is incremented each time `error' is called. */ 76131554Stjrunsigned int error_message_count; 77131554Stjr 78131554Stjr#ifdef _LIBC 79131554Stjr/* In the GNU C library, there is a predefined variable for this. */ 80131554Stjr 81131554Stjr# define program_name program_invocation_name 82131554Stjr# include <errno.h> 83131554Stjr 84131554Stjr/* In GNU libc we want do not want to use the common name `error' directly. 85131554Stjr Instead make it a weak alias. */ 86131554Stjr# define error __error 87131554Stjr# define error_at_line __error_at_line 88131554Stjr 89131554Stjr# ifdef USE_IN_LIBIO 90131554Stjr# include <libio/iolibio.h> 91131554Stjr# define fflush(s) _IO_fflush (s) 92131554Stjr# endif 93131554Stjr 94131554Stjr#else /* not _LIBC */ 95131554Stjr 96131554Stjr/* The calling program should define program_name and set it to the 97131554Stjr name of the executing program. */ 98131554Stjrextern char *program_name; 99131554Stjr 100131554Stjr# ifdef HAVE_STRERROR_R 101131554Stjr# define __strerror_r strerror_r 102131554Stjr# else 103131554Stjr# if HAVE_STRERROR 104131554Stjr# ifndef strerror /* On some systems, strerror is a macro */ 105131554Stjrchar *strerror (); 106131554Stjr# endif 107131554Stjr# else 108131554Stjrstatic char * 109131554Stjrprivate_strerror (errnum) 110131554Stjr int errnum; 111131554Stjr{ 112131554Stjr extern char *sys_errlist[]; 113131554Stjr extern int sys_nerr; 114131554Stjr 115131554Stjr if (errnum > 0 && errnum <= sys_nerr) 116131554Stjr return _(sys_errlist[errnum]); 117131554Stjr return _("Unknown system error"); 118131554Stjr} 119131554Stjr# define strerror private_strerror 120131554Stjr# endif /* HAVE_STRERROR */ 121131554Stjr# endif /* HAVE_STRERROR_R */ 122131554Stjr#endif /* not _LIBC */ 123131554Stjr 124131554Stjr/* Print the program name and error message MESSAGE, which is a printf-style 125131554Stjr format string with optional args. 126131554Stjr If ERRNUM is nonzero, print its corresponding system error message. 127131554Stjr Exit with status STATUS if it is nonzero. */ 128131554Stjr/* VARARGS */ 129131554Stjr 130131554Stjrvoid 131131554Stjr#if defined VA_START && __STDC__ 132131554Stjrerror (int status, int errnum, const char *message, ...) 133131554Stjr#else 134131554Stjrerror (status, errnum, message, va_alist) 135131554Stjr int status; 136131554Stjr int errnum; 137131554Stjr char *message; 138131554Stjr va_dcl 139131554Stjr#endif 140131554Stjr{ 141131554Stjr#ifdef VA_START 142131554Stjr va_list args; 143131554Stjr#endif 144131554Stjr 145131554Stjr if (error_print_progname) 146131554Stjr (*error_print_progname) (); 147131554Stjr else 148131554Stjr { 149131554Stjr fflush (stdout); 150131554Stjr fprintf (stderr, "%s: ", program_name); 151131554Stjr } 152131554Stjr 153131554Stjr#ifdef VA_START 154131554Stjr VA_START (args, message); 155131554Stjr# if HAVE_VPRINTF || _LIBC 156131554Stjr vfprintf (stderr, message, args); 157131554Stjr# else 158131554Stjr _doprnt (message, args, stderr); 159131554Stjr# endif 160131554Stjr va_end (args); 161131554Stjr#else 162131554Stjr fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); 163131554Stjr#endif 164131554Stjr 165131554Stjr ++error_message_count; 166131554Stjr if (errnum) 167131554Stjr { 168131554Stjr#if defined HAVE_STRERROR_R || _LIBC 169131554Stjr char errbuf[1024]; 170131554Stjr# if HAVE_WORKING_STRERROR_R || _LIBC 171131554Stjr fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); 172131554Stjr# else 173131554Stjr /* Don't use __strerror_r's return value because on some systems 174131554Stjr (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */ 175131554Stjr __strerror_r (errnum, errbuf, sizeof errbuf); 176131554Stjr fprintf (stderr, ": %s", errbuf); 177131554Stjr# endif 178131554Stjr#else 179131554Stjr fprintf (stderr, ": %s", strerror (errnum)); 180131554Stjr#endif 181131554Stjr } 182131554Stjr putc ('\n', stderr); 183131554Stjr fflush (stderr); 184131554Stjr if (status) 185131554Stjr exit (status); 186131554Stjr} 187131554Stjr 188131554Stjr/* Sometimes we want to have at most one error per line. This 189131554Stjr variable controls whether this mode is selected or not. */ 190131554Stjrint error_one_per_line; 191131554Stjr 192131554Stjrvoid 193131554Stjr#if defined VA_START && __STDC__ 194131554Stjrerror_at_line (int status, int errnum, const char *file_name, 195131554Stjr unsigned int line_number, const char *message, ...) 196131554Stjr#else 197131554Stjrerror_at_line (status, errnum, file_name, line_number, message, va_alist) 198131554Stjr int status; 199131554Stjr int errnum; 200131554Stjr const char *file_name; 201131554Stjr unsigned int line_number; 202131554Stjr char *message; 203131554Stjr va_dcl 204131554Stjr#endif 205131554Stjr{ 206131554Stjr#ifdef VA_START 207131554Stjr va_list args; 208131554Stjr#endif 209131554Stjr 210131554Stjr if (error_one_per_line) 211131554Stjr { 212131554Stjr static const char *old_file_name; 213131554Stjr static unsigned int old_line_number; 214131554Stjr 215131554Stjr if (old_line_number == line_number && 216131554Stjr (file_name == old_file_name || !strcmp (old_file_name, file_name))) 217131554Stjr /* Simply return and print nothing. */ 218131554Stjr return; 219131554Stjr 220131554Stjr old_file_name = file_name; 221131554Stjr old_line_number = line_number; 222131554Stjr } 223131554Stjr 224131554Stjr if (error_print_progname) 225131554Stjr (*error_print_progname) (); 226131554Stjr else 227131554Stjr { 228131554Stjr fflush (stdout); 229131554Stjr fprintf (stderr, "%s:", program_name); 230131554Stjr } 231131554Stjr 232131554Stjr if (file_name != NULL) 233131554Stjr fprintf (stderr, "%s:%d: ", file_name, line_number); 234131554Stjr 235131554Stjr#ifdef VA_START 236131554Stjr VA_START (args, message); 237131554Stjr# if HAVE_VPRINTF || _LIBC 238131554Stjr vfprintf (stderr, message, args); 239131554Stjr# else 240131554Stjr _doprnt (message, args, stderr); 241131554Stjr# endif 242131554Stjr va_end (args); 243131554Stjr#else 244131554Stjr fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); 245131554Stjr#endif 246131554Stjr 247131554Stjr ++error_message_count; 248131554Stjr if (errnum) 249131554Stjr { 250131554Stjr#if defined HAVE_STRERROR_R || _LIBC 251131554Stjr char errbuf[1024]; 252131554Stjr# if HAVE_WORKING_STRERROR_R || _LIBC 253131554Stjr fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); 254131554Stjr# else 255131554Stjr /* Don't use __strerror_r's return value because on some systems 256131554Stjr (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */ 257131554Stjr __strerror_r (errnum, errbuf, sizeof errbuf); 258131554Stjr fprintf (stderr, ": %s", errbuf); 259131554Stjr# endif 260131554Stjr#else 261131554Stjr fprintf (stderr, ": %s", strerror (errnum)); 262131554Stjr#endif 263131554Stjr } 264131554Stjr putc ('\n', stderr); 265131554Stjr fflush (stderr); 266131554Stjr if (status) 267131554Stjr exit (status); 268131554Stjr} 269131554Stjr 270131554Stjr#ifdef _LIBC 271131554Stjr/* Make the weak alias. */ 272131554Stjr# undef error 273131554Stjr# undef error_at_line 274131554Stjrweak_alias (__error, error) 275131554Stjrweak_alias (__error_at_line, error_at_line) 276131554Stjr#endif 277