1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| | #include <error.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stdio_impl.h"
#include "libc.h"
void (*__error_print_progname) (void) = 0;
unsigned int __error_message_count = 0;
int __error_one_per_line = 0;
static unsigned int saved_linenum = 0;
static const char *saved_file = 0;
static void errorv(int status, int errnum,
const char *file, unsigned int linenum,
const char *fmt, va_list ap) {
++__error_message_count;
fflush(stdout);
FLOCK(stderr);
if (__error_print_progname)
__error_print_progname();
else {
fprintf(stderr, "%s:", __progname_full);
if (!file)
fputc(' ', stderr);
}
if (file)
fprintf(stderr, "%s:%u: ", file, linenum);
vfprintf(stderr, fmt, ap);
if (errnum)
fprintf(stderr, ": %s", strerror(errnum));
fputc('\n', stderr);
fflush(stderr);
FUNLOCK(stderr);
if (status)
exit(status);
}
void __error(int status, int errnum, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
errorv(status, errnum, NULL, 0, fmt, ap);
va_end(ap);
}
void __error_at_line(int status, int errnum,
const char *file, unsigned int linenum,
const char *fmt, ...) {
if (error_one_per_line) {
if(saved_linenum == linenum && file != NULL &&
saved_file != NULL && !strcmp(file, saved_file))
return;
saved_linenum = linenum;
// Assuming that the lifetime of the passed in file name extends
// until the next call is rather questionable, but appears to be
// the expected semantics.
saved_file = file;
}
va_list ap;
va_start(ap, fmt);
errorv(status, errnum, file, linenum, fmt, ap);
va_end(ap);
}
weak_alias(__error_print_progname, error_print_progname);
weak_alias(__error_message_count, error_message_count);
weak_alias(__error_one_per_line, error_one_per_line);
weak_alias(__error, error);
weak_alias(__error_at_line, error_at_line);
|