]>
Commit | Line | Data |
---|---|---|
1 | #ifndef _WARNP_H_ | |
2 | #define _WARNP_H_ | |
3 | ||
4 | #include <errno.h> | |
5 | #include <stddef.h> | |
6 | ||
7 | /* Avoid namespace collisions with BSD <err.h>. */ | |
8 | #define warn libcperciva_warn | |
9 | #define warnx libcperciva_warnx | |
10 | ||
11 | /** | |
12 | * warnp_setprogname(progname): | |
13 | * Set the program name to be used by warn() and warnx() to ${progname}. | |
14 | */ | |
15 | void warnp_setprogname(const char *); | |
16 | #define WARNP_INIT do { \ | |
17 | if (argv[0] != NULL) \ | |
18 | warnp_setprogname(argv[0]); \ | |
19 | } while (0) | |
20 | ||
21 | /* As in BSD <err.h>. */ | |
22 | void warn(const char *, ...); | |
23 | void warnx(const char *, ...); | |
24 | ||
25 | /* | |
26 | * If compiled with DEBUG defined, print __FILE__ and __LINE__. | |
27 | */ | |
28 | #ifdef DEBUG | |
29 | #define warnline do { \ | |
30 | warnx("%s, %d", __FILE__, __LINE__); \ | |
31 | } while (0) | |
32 | #else | |
33 | #define warnline | |
34 | #endif | |
35 | ||
36 | /* | |
37 | * Call warn(3) or warnx(3) depending upon whether errno == 0; and clear | |
38 | * errno (so that the standard error message isn't repeated later). | |
39 | */ | |
40 | #define warnp(...) do { \ | |
41 | warnline; \ | |
42 | if (errno != 0) { \ | |
43 | warn(__VA_ARGS__); \ | |
44 | errno = 0; \ | |
45 | } else \ | |
46 | warnx(__VA_ARGS__); \ | |
47 | } while (0) | |
48 | ||
49 | /* | |
50 | * Call warnx(3) and set errno == 0. Unlike warnp, this should be used | |
51 | * in cases where we're reporting a problem which we discover ourselves | |
52 | * rather than one which is reported to us from a library or the kernel. | |
53 | */ | |
54 | #define warn0(...) do { \ | |
55 | warnline; \ | |
56 | warnx(__VA_ARGS__); \ | |
57 | errno = 0; \ | |
58 | } while (0) | |
59 | ||
60 | #endif /* !_WARNP_H_ */ |