Rev 4872 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | /* Error handler for noninteractive utilities |
2 | Copyright (C) 1990-1998, 2000-2005, 2006 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, write to the Free |
||
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
||
18 | 02111-1307 USA. */ |
||
19 | |||
20 | /* Written by David MacKenzie |
||
21 | |||
22 | #include |
||
23 | #include |
||
24 | #include |
||
25 | #include |
||
26 | #include |
||
27 | |||
28 | #include "error.h" |
||
29 | |||
30 | /* This variable is incremented each time `error' is called. */ |
||
31 | unsigned int error_message_count; |
||
32 | |||
33 | char *strerror_r (); |
||
34 | |||
35 | /* The calling program should define program_name and set it to the |
||
36 | name of the executing program. */ |
||
37 | //extern char *program_name; |
||
38 | |||
39 | static void |
||
40 | print_errno_message (int errnum) |
||
41 | { |
||
42 | char const *s; |
||
43 | |||
44 | s = strerror (errnum); |
||
45 | fprintf (stderr, ": %s", s); |
||
46 | } |
||
47 | |||
48 | static void |
||
49 | error_tail (int status, int errnum, const char *message, va_list args) |
||
50 | { |
||
51 | vfprintf (stderr, message, args); |
||
52 | va_end (args); |
||
53 | |||
54 | ++error_message_count; |
||
55 | if (errnum) |
||
56 | print_errno_message (errnum); |
||
57 | putc ('\n', stderr); |
||
58 | fflush (stderr); |
||
59 | if (status) |
||
60 | exit (status); |
||
61 | } |
||
62 | |||
63 | |||
64 | /* Print the program name and error message MESSAGE, which is a printf-style |
||
65 | format string with optional args. |
||
66 | If ERRNUM is nonzero, print its corresponding system error message. |
||
67 | Exit with status STATUS if it is nonzero. */ |
||
68 | void |
||
69 | error (int status, int errnum, const char *message, ...) |
||
70 | { |
||
71 | va_list args; |
||
72 | |||
73 | // fflush (stdout); |
||
74 | |||
75 | // fprintf (stderr, "%s: ", program_name); |
||
76 | |||
77 | va_start (args, message); |
||
78 | error_tail (status, errnum, message, args); |
||
79 | |||
80 | }; |
||
81 | |||
82 | /* Sometimes we want to have at most one error per line. This |
||
83 | variable controls whether this mode is selected or not. */ |
||
84 | |||
85 | int error_one_per_line; |
||
86 | |||
87 | void |
||
88 | error_at_line (int status, int errnum, const char *file_name, |
||
89 | unsigned int line_number, const char *message, ...) |
||
90 | { |
||
91 | va_list args; |
||
92 | |||
93 | if (error_one_per_line) |
||
94 | { |
||
95 | static const char *old_file_name; |
||
96 | static unsigned int old_line_number; |
||
97 | |||
98 | if (old_line_number == line_number |
||
99 | && (file_name == old_file_name |
||
100 | || strcmp (old_file_name, file_name) == 0)) |
||
101 | /* Simply return and print nothing. */ |
||
102 | return; |
||
103 | |||
104 | old_file_name = file_name; |
||
105 | old_line_number = line_number; |
||
106 | } |
||
107 | |||
108 | // fflush (stdout); |
||
109 | |||
110 | fprintf (stderr, file_name != NULL ? "%s:%d: " : " ", |
||
111 | file_name, line_number); |
||
112 | |||
113 | va_start (args, message); |
||
114 | error_tail (status, errnum, message, args); |
||
115 | |||
116 | } |
||
117 |