home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / lib / error.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  135 lines

  1. /* error.c -- error handler for noninteractive utilities
  2.    Copyright (C) 1990, 91, 92, 93, 94, 95 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17.  
  18. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  19.  
  20. #define sys_errlist sidestep_sys_errlist_declaration
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <errno.h>
  28.  
  29. #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
  30. # if __STDC__
  31. #  include <stdarg.h>
  32. #  define VA_START(args, lastarg) va_start(args, lastarg)
  33. # else
  34. #  include <varargs.h>
  35. #  define VA_START(args, lastarg) va_start(args)
  36. # endif
  37. #else
  38. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  39. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  40. #endif
  41.  
  42. #if STDC_HEADERS || _LIBC
  43. # include <stdlib.h>
  44. # include <string.h>
  45. #else
  46. void exit ();
  47. #endif
  48.  
  49. /* This variable is incremented each time `error' is called.  */
  50. unsigned int error_message_count;
  51.  
  52. /* If NULL, error will flush stdout, then print on stderr the program
  53.    name, a colon and a space.  Otherwise, error will call this
  54.    function without parameters instead.  */
  55. void (*error_print_progname) () = NULL;
  56.  
  57. #ifdef _LIBC
  58. #define program_name program_invocation_name
  59. #endif
  60.  
  61. /* The calling program should define program_name and set it to the
  62.    name of the executing program.  */
  63. extern char *program_name;
  64.  
  65. #if HAVE_STRERROR || _LIBC
  66. # ifndef strerror        /* On some systems, strerror is a macro */
  67. char *strerror ();
  68. # endif
  69. #else
  70. static char *
  71. private_strerror (errnum)
  72.      int errnum;
  73. {
  74. #undef sys_errlist
  75.   extern char *sys_errlist[];
  76.   extern int sys_nerr;
  77.  
  78.   if (errnum > 0 && errnum <= sys_nerr)
  79.     return sys_errlist[errnum];
  80.   return "Unknown system error";
  81. }
  82. #define strerror private_strerror
  83. #endif
  84.  
  85. /* Print the program name and error message MESSAGE, which is a printf-style
  86.    format string with optional args.
  87.    If ERRNUM is nonzero, print its corresponding system error message.
  88.    Exit with status STATUS if it is nonzero.  */
  89. /* VARARGS */
  90.  
  91. void
  92. #if defined(VA_START) && __STDC__
  93. error (int status, int errnum, const char *message, ...)
  94. #else
  95. error (status, errnum, message, va_alist)
  96.      int status;
  97.      int errnum;
  98.      char *message;
  99.      va_dcl
  100. #endif
  101. {
  102. #ifdef VA_START
  103.   va_list args;
  104. #endif
  105.  
  106.   if (error_print_progname)
  107.     (*error_print_progname) ();
  108.   else
  109.     {
  110.       fflush (stdout);
  111.       fprintf (stderr, "%s: ", program_name);
  112.     }
  113.  
  114. #ifdef VA_START
  115.   VA_START (args, message);
  116. # if HAVE_VPRINTF || _LIBC
  117.   vfprintf (stderr, message, args);
  118. # else
  119.   _doprnt (message, args, stderr);
  120. # endif
  121.   va_end (args);
  122. #else
  123.   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  124. #endif
  125.  
  126.   ++error_message_count;
  127.  
  128.   if (errnum)
  129.     fprintf (stderr, ": %s", strerror (errnum));
  130.   putc ('\n', stderr);
  131.   fflush (stderr);
  132.   if (status)
  133.     exit (status);
  134. }
  135.