home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / lib / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-14  |  4.1 KB  |  175 lines

  1. /* error.c -- error handler for noninteractive utilities
  2.    Copyright (C) 1990, 91, 92, 93, 94, 1995
  3.    Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  20.  
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24.  
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27.  
  28. #if HAVE_VPRINTF || HAVE_DOPRNT
  29. # if __STDC__
  30. #  include <stdarg.h>
  31. #  define VA_START(args, lastarg) va_start(args, lastarg)
  32. # else
  33. #  include <varargs.h>
  34. #  define VA_START(args, lastarg) va_start(args)
  35. # endif
  36. #else
  37. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  38. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  39. #endif
  40.  
  41. #if STDC_HEADERS
  42. # include <stdlib.h>
  43. # include <string.h>
  44. #else
  45. void exit ();
  46. #endif
  47.  
  48. #include <libintl.h>
  49.  
  50. #define _(str) (str)
  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. /* The calling program should define program_name and set it to the
  58.    name of the executing program.  */
  59. extern char *program_name;
  60.  
  61. #if HAVE_STRERROR
  62. char *strerror ();
  63. #else
  64. static char *
  65. private_strerror (errnum)
  66.      int errnum;
  67. {
  68.   extern char *sys_errlist[];
  69.   extern int sys_nerr;
  70.  
  71.   if (errnum > 0 && errnum <= sys_nerr)
  72.     return sys_errlist[errnum];
  73.   return _("Unknown system error");
  74. }
  75. #define strerror private_strerror
  76. #endif
  77.  
  78. /* Print the program name and error message MESSAGE, which is a printf-style
  79.    format string with optional args.
  80.    If ERRNUM is nonzero, print its corresponding system error message.
  81.    Exit with status STATUS if it is nonzero.  */
  82. /* VARARGS */
  83.  
  84. void
  85. #if defined(VA_START) && __STDC__
  86. error (int status, int errnum, const char *message, ...)
  87. #else
  88. error (status, errnum, message, va_alist)
  89.      int status;
  90.      int errnum;
  91.      char *message;
  92.      va_dcl
  93. #endif
  94. {
  95. #ifdef VA_START
  96.   va_list args;
  97. #endif
  98.  
  99.   if (error_print_progname)
  100.     (*error_print_progname) ();
  101.   else
  102.     {
  103.       fflush (stdout);
  104.       fprintf (stderr, "%s: ", program_name);
  105.     }
  106.  
  107. #ifdef VA_START
  108.   VA_START (args, message);
  109. # if HAVE_VPRINTF
  110.   vfprintf (stderr, message, args);
  111. # else
  112.   _doprnt (message, args, stderr);
  113. # endif
  114.   va_end (args);
  115. #else
  116.   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  117. #endif
  118.  
  119.   if (errnum)
  120.     fprintf (stderr, ": %s", strerror (errnum));
  121.   putc ('\n', stderr);
  122.   fflush (stderr);
  123.   if (status)
  124.     exit (status);
  125. }
  126.  
  127. void
  128. #if defined(VA_START) && __STDC__
  129. error_with_loc (int status, int errnum, const char *file_name,
  130.         size_t line_number, const char *message, ...)
  131. #else
  132. error_with_loc (status, errnum, file_name, line_number, message, va_alist)
  133.      int status;
  134.      int errnum;
  135.      char *file_name;
  136.      size_t line_number;
  137.      char *message;
  138.      va_dcl
  139. #endif
  140. {
  141. #ifdef VA_START
  142.   va_list args;
  143. #endif
  144.  
  145.   if (error_print_progname)
  146.     (*error_print_progname) ();
  147.   else
  148.     {
  149.       fflush (stdout);
  150.       fprintf (stderr, "%s:", program_name);
  151.     }
  152.  
  153.   if (file_name != NULL)
  154.     fprintf (stderr, "%s:%d: ", file_name, line_number);
  155.  
  156. #ifdef VA_START
  157.   VA_START (args, message);
  158. # if HAVE_VPRINTF
  159.   vfprintf (stderr, message, args);
  160. # else
  161.   _doprnt (message, args, stderr);
  162. # endif
  163.   va_end (args);
  164. #else
  165.   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  166. #endif
  167.  
  168.   if (errnum)
  169.     fprintf (stderr, ": %s", strerror (errnum));
  170.   putc ('\n', stderr);
  171.   fflush (stderr);
  172.   if (status)
  173.     exit (status);
  174. }
  175.