-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
40 lines (30 loc) · 784 Bytes
/
error.c
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
#include <errno.h>
#include <stdbool.h>
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "error.h"
/* error can be called in the same way as printf. */
void (error)(const char* file, const char* func, int line, const char *fmt, ...)
{
va_list ap;
static char abuf[BUFSIZ];
static char bbuf[BUFSIZ];
fflush(NULL);
va_start(ap, fmt);
snprintf(abuf, BUFSIZ, "%s: \"%s\" %s line %d: ", progname, file, func, line);
snprintf(bbuf, BUFSIZ, "error: ");
strncat(abuf, bbuf, BUFSIZ);
vsnprintf(bbuf, BUFSIZ, fmt, ap);
strncat(abuf, bbuf, BUFSIZ);
if (errno != 0) {
strncat(abuf, ": ", BUFSIZ);
strncat(abuf, strerror(errno), BUFSIZ);
errno = 0;
}
fprintf(stderr, "%s\n", abuf);
va_end(ap);
exit(1);
}