Line data Source code
1 : #include <errno.h>
2 : #include <stdbool.h>
3 : #include <signal.h>
4 : #include <stdio.h>
5 : #include <stdarg.h>
6 : #include <string.h>
7 : #include <stdlib.h>
8 :
9 : #include "error.h"
10 :
11 : /* error can be called in the same way as printf. */
12 0 : void (error)(const char* file, const char* func, int line, const char *fmt, ...)
13 : {
14 : va_list ap;
15 : static char abuf[BUFSIZ];
16 : static char bbuf[BUFSIZ];
17 :
18 0 : fflush(NULL);
19 :
20 0 : va_start(ap, fmt);
21 :
22 0 : snprintf(abuf, BUFSIZ, "%s: \"%s\" %s line %d: ", progname, file, func, line);
23 0 : snprintf(bbuf, BUFSIZ, "error: ");
24 0 : strncat(abuf, bbuf, BUFSIZ-100);
25 :
26 0 : vsnprintf(bbuf, BUFSIZ, fmt, ap);
27 0 : strncat(abuf, bbuf, BUFSIZ-100);
28 :
29 0 : if (errno != 0) {
30 0 : strncat(abuf, ": ", BUFSIZ-3);
31 0 : strncat(abuf, strerror(errno), BUFSIZ-100);
32 0 : errno = 0;
33 : }
34 :
35 0 : fprintf(stderr, "%s\n", abuf);
36 :
37 0 : va_end(ap);
38 :
39 0 : exit(1);
40 : }
|