OSDN Git Service

Disabled NULL error_print_progname, useless
[uclinux-h8/uClibc.git] / libc / misc / error / error.c
1 /* Error handler for noninteractive utilities
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
7 /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
8
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <error.h>
14
15 libc_hidden_proto(strcmp)
16 libc_hidden_proto(strerror)
17 libc_hidden_proto(fprintf)
18 libc_hidden_proto(exit)
19 libc_hidden_proto(putc)
20 libc_hidden_proto(vfprintf)
21 libc_hidden_proto(fflush)
22 libc_hidden_proto(fputc)
23 libc_hidden_proto(__fputc_unlocked)
24 libc_hidden_proto(stdout)
25 libc_hidden_proto(stderr)
26
27 /* This variable is incremented each time `error' is called.  */
28 unsigned int error_message_count = 0;
29 /* Sometimes we want to have at most one error per line.  This
30    variable controls whether this mode is selected or not.  */
31 int error_one_per_line;
32 /* If NULL, error will flush stdout, then print on stderr the program
33    name, a colon and a space.  Otherwise, error will call this
34    function without parameters instead.  */
35 /* void (*error_print_progname) (void) = NULL; */
36
37 extern __typeof(error) __error attribute_hidden;
38 void __error (int status, int errnum, const char *message, ...)
39 {
40     va_list args;
41
42     fflush (stdout);
43
44     va_start (args, message);
45     vfprintf (stderr, message, args);
46     va_end (args);
47     ++error_message_count;
48     if (errnum) {
49         fprintf (stderr, ": %s", strerror (errnum));
50     }
51     putc ('\n', stderr);
52     if (status)
53         exit (status);
54 }
55
56 extern __typeof(error_at_line) __error_at_line attribute_hidden;
57 void __error_at_line (int status, int errnum, const char *file_name,
58                unsigned int line_number, const char *message, ...)
59 {
60     va_list args;
61
62     if (error_one_per_line) {
63         static const char *old_file_name;
64         static unsigned int old_line_number;
65
66         if (old_line_number == line_number &&
67                 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
68             /* Simply return and print nothing.  */
69             return;
70
71         old_file_name = file_name;
72         old_line_number = line_number;
73     }
74
75     fflush (stdout);
76
77     if (file_name != NULL)
78         fprintf (stderr, "%s:%d: ", file_name, line_number);
79
80     va_start (args, message);
81     vfprintf (stderr, message, args);
82     va_end (args);
83
84     ++error_message_count;
85     if (errnum) {
86         fprintf (stderr, ": %s", strerror (errnum));
87     }
88     putc ('\n', stderr);
89     if (status)
90         exit (status);
91 }
92
93 /* psm: keep this weak, too many use this in common code */
94 weak_alias(__error,error)
95 strong_alias(__error_at_line,error_at_line)