Fix CFLAGS/LDFLAGS syntax
[platform/upstream/lrzsz.git] / lib / error.c
1 /* error.c -- error handler for noninteractive utilities
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96 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 #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 #ifndef _
47 # define _(String) String
48 #endif
49
50 /* If NULL, error will flush stdout, then print on stderr the program
51    name, a colon and a space.  Otherwise, error will call this
52    function without parameters instead.  */
53 void (*error_print_progname) (
54 #if __STDC__ - 0
55                               void
56 #endif
57                               );
58
59 /* This variable is incremented each time `error' is called.  */
60 unsigned int error_message_count;
61
62 #ifdef _LIBC
63 /* In the GNU C library, there is a predefined variable for this.  */
64
65 # define program_name program_invocation_name
66 # include <errno.h>
67
68 #else
69
70 /* The calling program should define program_name and set it to the
71    name of the executing program.  */
72 extern char *program_name;
73
74 # if HAVE_STRERROR
75 #  ifndef strerror              /* On some systems, strerror is a macro */
76 char *strerror ();
77 #  endif
78 # else
79 static char *
80 private_strerror (errnum)
81      int errnum;
82 {
83   extern char *sys_errlist[];
84   extern int sys_nerr;
85
86   if (errnum > 0 && errnum <= sys_nerr)
87     return sys_errlist[errnum];
88   return _("Unknown system error");
89 }
90 #  define strerror private_strerror
91 # endif /* HAVE_STRERROR */
92 #endif  /* _LIBC */
93
94 /* Print the program name and error message MESSAGE, which is a printf-style
95    format string with optional args.
96    If ERRNUM is nonzero, print its corresponding system error message.
97    Exit with status STATUS if it is nonzero.  */
98 /* VARARGS */
99
100 void
101 #if defined(VA_START) && __STDC__
102 error (int status, int errnum, const char *message, ...)
103 #else
104 error (status, errnum, message, va_alist)
105      int status;
106      int errnum;
107      char *message;
108      va_dcl
109 #endif
110 {
111 #ifdef VA_START
112   va_list args;
113 #endif
114
115   if (error_print_progname)
116     (*error_print_progname) ();
117   else
118     {
119       fflush (stdout);
120       fprintf (stderr, "%s: ", program_name);
121     }
122
123 #ifdef VA_START
124   VA_START (args, message);
125 # if HAVE_VPRINTF || _LIBC
126   vfprintf (stderr, message, args);
127 # else
128   _doprnt (message, args, stderr);
129 # endif
130   va_end (args);
131 #else
132   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
133 #endif
134
135   ++error_message_count;
136   if (errnum)
137     fprintf (stderr, ": %s", strerror (errnum));
138   putc ('\n', stderr);
139   fflush (stderr);
140   if (status)
141     exit (status);
142 }
143 \f
144 /* Sometimes we want to have at most one error per line.  This
145    variable controls whether this mode is selected or not.  */
146 int error_one_per_line;
147
148 void
149 #if defined(VA_START) && __STDC__
150 error_at_line (int status, int errnum, const char *file_name,
151                unsigned int line_number, const char *message, ...)
152 #else
153 error_at_line (status, errnum, file_name, line_number, message, va_alist)
154      int status;
155      int errnum;
156      const char *file_name;
157      unsigned int line_number;
158      char *message;
159      va_dcl
160 #endif
161 {
162 #ifdef VA_START
163   va_list args;
164 #endif
165
166   if (error_one_per_line)
167     {
168       static const char *old_file_name;
169       static unsigned int old_line_number;
170
171       if (old_line_number == line_number &&
172           (file_name == old_file_name || !strcmp (old_file_name, file_name)))
173         /* Simply return and print nothing.  */
174         return;
175
176       old_file_name = file_name;
177       old_line_number = line_number;
178     }
179
180   if (error_print_progname)
181     (*error_print_progname) ();
182   else
183     {
184       fflush (stdout);
185       fprintf (stderr, "%s:", program_name);
186     }
187
188   if (file_name != NULL)
189     fprintf (stderr, "%s:%d: ", file_name, line_number);
190
191 #ifdef VA_START
192   VA_START (args, message);
193 # if HAVE_VPRINTF || _LIBC
194   vfprintf (stderr, message, args);
195 # else
196   _doprnt (message, args, stderr);
197 # endif
198   va_end (args);
199 #else
200   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
201 #endif
202
203   ++error_message_count;
204   if (errnum)
205     fprintf (stderr, ": %s", strerror (errnum));
206   putc ('\n', stderr);
207   fflush (stderr);
208   if (status)
209     exit (status);
210 }