|
Packit |
15a96c |
/* Error handler for noninteractive utilities
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
Copyright (C) 1990,91,92,93,94,95,96,97,98 Free Software Foundation, Inc.
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
This file is part of the GNU C Library. Its master source is NOT part of
|
|
Packit |
15a96c |
the C library, however. The master source lives in /gd/gnu/lib.
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
15a96c |
modify it under the terms of the GNU Library General Public License as
|
|
Packit |
15a96c |
published by the Free Software Foundation; either version 2 of the
|
|
Packit |
15a96c |
License, or (at your option) any later version.
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
15a96c |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
15a96c |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
15a96c |
Library General Public License for more details.
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
You should have received a copy of the GNU Library General Public
|
|
Packit |
15a96c |
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
|
Packit |
15a96c |
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
Packit |
15a96c |
Boston, MA 02111-1307, USA. */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef HAVE_CONFIG_H
|
|
Packit |
15a96c |
# include <config.h>
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#include <stdio.h>
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
|
|
Packit |
15a96c |
# if __STDC__
|
|
Packit |
15a96c |
# include <stdarg.h>
|
|
Packit |
15a96c |
# define VA_START(args, lastarg) va_start(args, lastarg)
|
|
Packit |
15a96c |
# else
|
|
Packit |
15a96c |
# include <varargs.h>
|
|
Packit |
15a96c |
# define VA_START(args, lastarg) va_start(args)
|
|
Packit |
15a96c |
# endif
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
# define va_alist a1, a2, a3, a4, a5, a6, a7, a8
|
|
Packit |
15a96c |
# define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#if STDC_HEADERS || _LIBC
|
|
Packit |
15a96c |
# include <stdlib.h>
|
|
Packit |
15a96c |
# include <string.h>
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
void exit ();
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#include "error.h"
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifndef _
|
|
Packit |
15a96c |
# define _(String) String
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* If NULL, error will flush stdout, then print on stderr the program
|
|
Packit |
15a96c |
name, a colon and a space. Otherwise, error will call this
|
|
Packit |
15a96c |
function without parameters instead. */
|
|
Packit |
15a96c |
void (*error_print_progname) (
|
|
Packit |
15a96c |
#if __STDC__ - 0
|
|
Packit |
15a96c |
void
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* This variable is incremented each time `error' is called. */
|
|
Packit |
15a96c |
unsigned int error_message_count;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef _LIBC
|
|
Packit |
15a96c |
/* In the GNU C library, there is a predefined variable for this. */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
# define program_name program_invocation_name
|
|
Packit |
15a96c |
# include <errno.h>
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* In GNU libc we want do not want to use the common name `error' directly.
|
|
Packit |
15a96c |
Instead make it a weak alias. */
|
|
Packit |
15a96c |
# define error __error
|
|
Packit |
15a96c |
# define error_at_line __error_at_line
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
# ifdef USE_IN_LIBIO
|
|
Packit |
15a96c |
# include <libio/iolibio.h>
|
|
Packit |
15a96c |
# define fflush(s) _IO_fflush (s)
|
|
Packit |
15a96c |
# endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#else /* not _LIBC */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* The calling program should define program_name and set it to the
|
|
Packit |
15a96c |
name of the executing program. */
|
|
Packit |
15a96c |
extern char *program_name;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
# ifdef HAVE_STRERROR_R
|
|
Packit |
15a96c |
# define __strerror_r strerror_r
|
|
Packit |
15a96c |
# else
|
|
Packit |
15a96c |
# if HAVE_STRERROR
|
|
Packit |
15a96c |
# ifndef strerror /* On some systems, strerror is a macro */
|
|
Packit |
15a96c |
char *strerror ();
|
|
Packit |
15a96c |
# endif
|
|
Packit |
15a96c |
# else
|
|
Packit |
15a96c |
static char *
|
|
Packit |
15a96c |
private_strerror (errnum)
|
|
Packit |
15a96c |
int errnum;
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
extern char *sys_errlist[];
|
|
Packit |
15a96c |
extern int sys_nerr;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (errnum > 0 && errnum <= sys_nerr)
|
|
Packit |
15a96c |
return _(sys_errlist[errnum]);
|
|
Packit |
15a96c |
return _("Unknown system error");
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
# define strerror private_strerror
|
|
Packit |
15a96c |
# endif /* HAVE_STRERROR */
|
|
Packit |
15a96c |
# endif /* HAVE_STRERROR_R */
|
|
Packit |
15a96c |
#endif /* not _LIBC */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* Print the program name and error message MESSAGE, which is a printf-style
|
|
Packit |
15a96c |
format string with optional args.
|
|
Packit |
15a96c |
If ERRNUM is nonzero, print its corresponding system error message.
|
|
Packit |
15a96c |
Exit with status STATUS if it is nonzero. */
|
|
Packit |
15a96c |
/* VARARGS */
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
void
|
|
Packit |
15a96c |
#if defined VA_START && __STDC__
|
|
Packit |
15a96c |
error (int status, int errnum, const char *message, ...)
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
error (status, errnum, message, va_alist)
|
|
Packit |
15a96c |
int status;
|
|
Packit |
15a96c |
int errnum;
|
|
Packit |
15a96c |
char *message;
|
|
Packit |
15a96c |
va_dcl
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#ifdef VA_START
|
|
Packit |
15a96c |
va_list args;
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (error_print_progname)
|
|
Packit |
15a96c |
(*error_print_progname) ();
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
fflush (stdout);
|
|
Packit |
15a96c |
fprintf (stderr, "%s: ", program_name);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef VA_START
|
|
Packit |
15a96c |
VA_START (args, message);
|
|
Packit |
15a96c |
# if HAVE_VPRINTF || _LIBC
|
|
Packit |
15a96c |
vfprintf (stderr, message, args);
|
|
Packit |
15a96c |
# else
|
|
Packit |
15a96c |
_doprnt (message, args, stderr);
|
|
Packit |
15a96c |
# endif
|
|
Packit |
15a96c |
va_end (args);
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
++error_message_count;
|
|
Packit |
15a96c |
if (errnum)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#if defined HAVE_STRERROR_R || defined _LIBC
|
|
Packit |
15a96c |
char errbuf[1024];
|
|
Packit |
15a96c |
fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
fprintf (stderr, ": %s", strerror (errnum));
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
putc ('\n', stderr);
|
|
Packit |
15a96c |
fflush (stderr);
|
|
Packit |
15a96c |
if (status)
|
|
Packit |
15a96c |
exit (status);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
/* Sometimes we want to have at most one error per line. This
|
|
Packit |
15a96c |
variable controls whether this mode is selected or not. */
|
|
Packit |
15a96c |
int error_one_per_line;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
void
|
|
Packit |
15a96c |
#if defined VA_START && __STDC__
|
|
Packit |
15a96c |
error_at_line (int status, int errnum, const char *file_name,
|
|
Packit |
15a96c |
unsigned int line_number, const char *message, ...)
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
error_at_line (status, errnum, file_name, line_number, message, va_alist)
|
|
Packit |
15a96c |
int status;
|
|
Packit |
15a96c |
int errnum;
|
|
Packit |
15a96c |
const char *file_name;
|
|
Packit |
15a96c |
unsigned int line_number;
|
|
Packit |
15a96c |
char *message;
|
|
Packit |
15a96c |
va_dcl
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#ifdef VA_START
|
|
Packit |
15a96c |
va_list args;
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (error_one_per_line)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
static const char *old_file_name;
|
|
Packit |
15a96c |
static unsigned int old_line_number;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (old_line_number == line_number &&
|
|
Packit |
15a96c |
(file_name == old_file_name || !strcmp (old_file_name, file_name)))
|
|
Packit |
15a96c |
/* Simply return and print nothing. */
|
|
Packit |
15a96c |
return;
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
old_file_name = file_name;
|
|
Packit |
15a96c |
old_line_number = line_number;
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (error_print_progname)
|
|
Packit |
15a96c |
(*error_print_progname) ();
|
|
Packit |
15a96c |
else
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
fflush (stdout);
|
|
Packit |
15a96c |
fprintf (stderr, "%s:", program_name);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
if (file_name != NULL)
|
|
Packit |
15a96c |
fprintf (stderr, "%s:%d: ", file_name, line_number);
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef VA_START
|
|
Packit |
15a96c |
VA_START (args, message);
|
|
Packit |
15a96c |
# if HAVE_VPRINTF || _LIBC
|
|
Packit |
15a96c |
vfprintf (stderr, message, args);
|
|
Packit |
15a96c |
# else
|
|
Packit |
15a96c |
_doprnt (message, args, stderr);
|
|
Packit |
15a96c |
# endif
|
|
Packit |
15a96c |
va_end (args);
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
++error_message_count;
|
|
Packit |
15a96c |
if (errnum)
|
|
Packit |
15a96c |
{
|
|
Packit |
15a96c |
#if defined HAVE_STRERROR_R || defined _LIBC
|
|
Packit |
15a96c |
char errbuf[1024];
|
|
Packit |
15a96c |
fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
|
|
Packit |
15a96c |
#else
|
|
Packit |
15a96c |
fprintf (stderr, ": %s", strerror (errnum));
|
|
Packit |
15a96c |
#endif
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
putc ('\n', stderr);
|
|
Packit |
15a96c |
fflush (stderr);
|
|
Packit |
15a96c |
if (status)
|
|
Packit |
15a96c |
exit (status);
|
|
Packit |
15a96c |
}
|
|
Packit |
15a96c |
|
|
Packit |
15a96c |
#ifdef _LIBC
|
|
Packit |
15a96c |
/* Make the weak alias. */
|
|
Packit |
15a96c |
# undef error
|
|
Packit |
15a96c |
# undef error_at_line
|
|
Packit |
15a96c |
weak_alias (__error, error)
|
|
Packit |
15a96c |
weak_alias (__error_at_line, error_at_line)
|
|
Packit |
15a96c |
#endif
|