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
| | #include <error.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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);
flockfile(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);
funlockfile(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);
}
|