summaryrefslogtreecommitdiff
path: root/lib/mlibc/options/glibc/generic/error.cpp
blob: 45203b212bd1a3bd8eb683cf05cb50f15ec9f174 (plain)
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
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <error.h>

unsigned int error_message_count = 0;
int error_one_per_line = 0;
void (*error_print_progname)(void) = NULL;

void error(int status, int errnum, const char *format, ...) {
	va_list args;
	va_start(args, format);

	error_message_count++;
	
	fflush(stdout);
	if(error_print_progname) {
		error_print_progname();
	} else {
		fprintf(stderr, "%s: ", program_invocation_name);
	}
	vfprintf(stderr, format, args);
	va_end(args);

	if(errnum) {
		fprintf(stderr, ": %s\n", strerror(errnum));
	}

	if(status) {
		exit(status);
	}
}

void error_at_line(int status, int errnum, const char *filename, unsigned int linenum, const char *format, ...) {
	va_list args;
	va_start(args, format);

	static bool first_call = true;
	static unsigned int last_line = 0;
	if(!(last_line == linenum && error_one_per_line && !first_call)) {
		first_call = false;
		last_line = linenum;
		error_message_count++;

		fflush(stdout);
		if(error_print_progname) {
			error_print_progname();
		} else {
			fprintf(stderr, "%s:", program_invocation_name);
		}
		fprintf(stderr, "%s:%u: ", filename, linenum);
		vfprintf(stderr, format, args);

		if(errnum) {
			fprintf(stderr, ": %s\n", strerror(errnum));
		}
	}
	va_end(args);

	if(status) {
		exit(status);
	}
}