Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / sort / error.c
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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
27 # if __STDC__
28 #  include <stdarg.h>
29 #  define VA_START(args, lastarg) va_start(args, lastarg)
30 # else
31 #  include <varargs.h>
32 #  define VA_START(args, lastarg) va_start(args)
33 # endif
34 #else
35 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
36 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
37 #endif
38
39 #if STDC_HEADERS || _LIBC
40 # include <stdlib.h>
41 # include <string.h>
42 #else
43 void exit ();
44 #endif
45
46 /* This variable is incremented each time `error' is called.  */
47 unsigned int error_message_count;
48
49 /* If NULL, error will flush stdout, then print on stderr the program
50    name, a colon and a space.  Otherwise, error will call this
51    function without parameters instead.  */
52 void (*error_print_progname) () = NULL;
53
54 #ifdef _LIBC
55 #define program_name program_invocation_name
56 #endif
57
58 /* The calling program should define program_name and set it to the
59    name of the executing program.  */
60 extern char *program_name;
61
62 #if HAVE_STRERROR || _LIBC
63 # ifndef strerror               /* On some systems, strerror is a macro */
64 char *strerror ();
65 # endif
66 #else
67 static char *
68 private_strerror (errnum)
69      int errnum;
70 {
71   extern int sys_nerr;
72
73   if (errnum > 0 && errnum <= sys_nerr)
74     return sys_errlist[errnum];
75   return "Unknown system error";
76 }
77 #define strerror private_strerror
78 #endif
79
80 /* Print the program name and error message MESSAGE, which is a printf-style
81    format string with optional args.
82    If ERRNUM is nonzero, print its corresponding system error message.
83    Exit with status STATUS if it is nonzero.  */
84 /* VARARGS */
85
86 void
87 #if defined(VA_START) && __STDC__
88 error (int status, int errnum, const char *message, ...)
89 #else
90 error (status, errnum, message, va_alist)
91      int status;
92      int errnum;
93      char *message;
94      va_dcl
95 #endif
96 {
97 #ifdef VA_START
98   va_list args;
99 #endif
100
101   if (error_print_progname)
102     (*error_print_progname) ();
103   else
104     {
105       fflush (stdout);
106       fprintf (stderr, "%s: ", program_name);
107     }
108
109 #ifdef VA_START
110   VA_START (args, message);
111 # if HAVE_VPRINTF || _LIBC
112   vfprintf (stderr, message, args);
113 # else
114   _doprnt (message, args, stderr);
115 # endif
116   va_end (args);
117 #else
118   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
119 #endif
120
121   ++error_message_count;
122
123   if (errnum)
124     fprintf (stderr, ": %s", strerror (errnum));
125   putc ('\n', stderr);
126   fflush (stderr);
127   if (status)
128     exit (status);
129 }