1*0afa8e06SEd Maste /*
2*0afa8e06SEd Maste  * Public domain
3*0afa8e06SEd Maste  * err.h compatibility shim
4*0afa8e06SEd Maste  */
5*0afa8e06SEd Maste 
6*0afa8e06SEd Maste #ifndef _COMPAT_ERR_H
7*0afa8e06SEd Maste #define _COMPAT_ERR_H
8*0afa8e06SEd Maste 
9*0afa8e06SEd Maste #if !defined(HAVE_ERR_H)
10*0afa8e06SEd Maste 
11*0afa8e06SEd Maste #include <errno.h>
12*0afa8e06SEd Maste #include <stdarg.h>
13*0afa8e06SEd Maste #include <stdlib.h>
14*0afa8e06SEd Maste #include <stdio.h>
15*0afa8e06SEd Maste #include <string.h>
16*0afa8e06SEd Maste 
17*0afa8e06SEd Maste #if defined(_MSC_VER)
18*0afa8e06SEd Maste __declspec(noreturn)
19*0afa8e06SEd Maste #else
20*0afa8e06SEd Maste __attribute__((noreturn))
21*0afa8e06SEd Maste #endif
22*0afa8e06SEd Maste static inline void
err(int eval,const char * fmt,...)23*0afa8e06SEd Maste err(int eval, const char *fmt, ...)
24*0afa8e06SEd Maste {
25*0afa8e06SEd Maste 	int sverrno = errno;
26*0afa8e06SEd Maste 	va_list ap;
27*0afa8e06SEd Maste 
28*0afa8e06SEd Maste 	va_start(ap, fmt);
29*0afa8e06SEd Maste 	if (fmt != NULL) {
30*0afa8e06SEd Maste 		vfprintf(stderr, fmt, ap);
31*0afa8e06SEd Maste 		fprintf(stderr, ": ");
32*0afa8e06SEd Maste 	}
33*0afa8e06SEd Maste 	va_end(ap);
34*0afa8e06SEd Maste 	fprintf(stderr, "%s\n", strerror(sverrno));
35*0afa8e06SEd Maste 	exit(eval);
36*0afa8e06SEd Maste }
37*0afa8e06SEd Maste 
38*0afa8e06SEd Maste #if defined(_MSC_VER)
39*0afa8e06SEd Maste __declspec(noreturn)
40*0afa8e06SEd Maste #else
41*0afa8e06SEd Maste __attribute__((noreturn))
42*0afa8e06SEd Maste #endif
43*0afa8e06SEd Maste static inline void
errx(int eval,const char * fmt,...)44*0afa8e06SEd Maste errx(int eval, const char *fmt, ...)
45*0afa8e06SEd Maste {
46*0afa8e06SEd Maste 	va_list ap;
47*0afa8e06SEd Maste 
48*0afa8e06SEd Maste 	va_start(ap, fmt);
49*0afa8e06SEd Maste 	if (fmt != NULL)
50*0afa8e06SEd Maste 		vfprintf(stderr, fmt, ap);
51*0afa8e06SEd Maste 	va_end(ap);
52*0afa8e06SEd Maste 	fprintf(stderr, "\n");
53*0afa8e06SEd Maste 	exit(eval);
54*0afa8e06SEd Maste }
55*0afa8e06SEd Maste 
56*0afa8e06SEd Maste static inline void
warn(const char * fmt,...)57*0afa8e06SEd Maste warn(const char *fmt, ...)
58*0afa8e06SEd Maste {
59*0afa8e06SEd Maste 	int sverrno = errno;
60*0afa8e06SEd Maste 	va_list ap;
61*0afa8e06SEd Maste 
62*0afa8e06SEd Maste 	va_start(ap, fmt);
63*0afa8e06SEd Maste 	if (fmt != NULL) {
64*0afa8e06SEd Maste 		vfprintf(stderr, fmt, ap);
65*0afa8e06SEd Maste 		fprintf(stderr, ": ");
66*0afa8e06SEd Maste 	}
67*0afa8e06SEd Maste 	va_end(ap);
68*0afa8e06SEd Maste 	fprintf(stderr, "%s\n", strerror(sverrno));
69*0afa8e06SEd Maste }
70*0afa8e06SEd Maste 
71*0afa8e06SEd Maste static inline void
warnx(const char * fmt,...)72*0afa8e06SEd Maste warnx(const char *fmt, ...)
73*0afa8e06SEd Maste {
74*0afa8e06SEd Maste 	va_list ap;
75*0afa8e06SEd Maste 
76*0afa8e06SEd Maste 	va_start(ap, fmt);
77*0afa8e06SEd Maste 	if (fmt != NULL)
78*0afa8e06SEd Maste 		vfprintf(stderr, fmt, ap);
79*0afa8e06SEd Maste 	va_end(ap);
80*0afa8e06SEd Maste 	fprintf(stderr, "\n");
81*0afa8e06SEd Maste }
82*0afa8e06SEd Maste 
83*0afa8e06SEd Maste #endif /* !defined(HAVE_ERR_H) */
84*0afa8e06SEd Maste 
85*0afa8e06SEd Maste #endif /* _COMPAT_ERR_H */
86