1 /* Error handler for noninteractive utilities
2    Copyright (C) 1990-1998, 2000-2004, 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
20 /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
21 
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <error.h>
27 
28 
29 /* This variable is incremented each time `error' is called.  */
30 unsigned int error_message_count = 0;
31 /* Sometimes we want to have at most one error per line.  This
32    variable controls whether this mode is selected or not.  */
33 int error_one_per_line;
34 /* If NULL, error will flush stdout, then print on stderr the program
35    name, a colon and a space.  Otherwise, error will call this
36    function without parameters instead.  */
37 void (*error_print_progname) (void) = NULL;
38 
error(int status,int errnum,const char * message,...)39 void error (int status, int errnum, const char *message, ...)
40 {
41     va_list args;
42 
43     fflush (stdout);
44 
45     if (error_print_progname)
46 	(*error_print_progname) ();
47     else
48 	fprintf (stderr, "%s: ", __uclibc_progname);
49 
50     va_start (args, message);
51     vfprintf (stderr, message, args);
52     va_end (args);
53     ++error_message_count;
54     if (errnum) {
55 	fprintf (stderr, ": %s", strerror (errnum));
56     }
57     putc ('\n', stderr);
58     if (status)
59 	exit (status);
60 }
61 
error_at_line(int status,int errnum,const char * file_name,unsigned int line_number,const char * message,...)62 void error_at_line (int status, int errnum, const char *file_name,
63 		    unsigned int line_number, const char *message, ...)
64 {
65     va_list args;
66 
67     if (error_one_per_line) {
68 	static const char *old_file_name;
69 	static unsigned int old_line_number;
70 
71 	if (old_line_number == line_number &&
72 		(file_name == old_file_name || !strcmp (old_file_name, file_name)))
73 	    /* Simply return and print nothing.  */
74 	    return;
75 
76 	old_file_name = file_name;
77 	old_line_number = line_number;
78     }
79 
80     fflush (stdout);
81 
82     if (error_print_progname)
83 	(*error_print_progname) ();
84     else
85 	fprintf (stderr, "%s:", __uclibc_progname);
86 
87     if (file_name != NULL)
88 	fprintf (stderr, "%s:%d: ", file_name, line_number);
89 
90     va_start (args, message);
91     vfprintf (stderr, message, args);
92     va_end (args);
93 
94     ++error_message_count;
95     if (errnum) {
96 	fprintf (stderr, ": %s", strerror (errnum));
97     }
98     putc ('\n', stderr);
99     if (status)
100 	exit (status);
101 }
102