Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef COMPAT_H
18 #define COMPAT_H
20 #include "config.h"
22 #include <sys/types.h>
23 #include <sys/uio.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdint.h>
29 #ifndef __dead
30 #define __dead __attribute__((noreturn))
31 #endif
33 #ifndef INFTIM
34 #define INFTIM -1
35 #endif
37 #if HAVE_ENDIAN_H
38 # include <endian.h>
39 #elif HAVE_SYS_ENDIAN_H
40 # include <sys/endian.h>
41 #elif HAVE_LIBKERN_OSBYTEORDER_H
42 # include <machine/endian.h>
43 # include <libkern/OSByteOrder.h>
45 # define htobe16(x) OSSwapHostToBigInt16(x)
46 # define htole16(x) OSSwapHostToLittleInt16(x)
47 # define be16toh(x) OSSwapBigToHostInt16(x)
48 # define le16toh(x) OSSwapLittleToHostInt16(x)
50 # define htobe32(x) OSSwapHostToBigInt32(x)
51 # define htole32(x) OSSwapHostToLittleInt32(x)
52 # define be32toh(x) OSSwapBigToHostInt32(x)
53 # define le32toh(x) OSSwapLittleToHostInt32(x)
55 # define htobe64(x) OSSwapHostToBigInt64(x)
56 # define htole64(x) OSSwapHostToLittleInt64(x)
57 # define be64toh(x) OSSwapBigToHostInt64(x)
58 # define le64toh(x) OSSwapLittleToHostInt64(x)
59 #else
60 # error no compatible endian.h header found
61 #endif
63 #if HAVE_EVENT2
64 # include <event2/event.h>
65 # include <event2/event_compat.h>
66 # include <event2/event_struct.h>
67 # include <event2/buffer.h>
68 # include <event2/buffer_compat.h>
69 # include <event2/bufferevent.h>
70 # include <event2/bufferevent_struct.h>
71 # include <event2/bufferevent_compat.h>
72 #else
73 # include <event.h>
74 #endif
76 #ifdef HAVE_QUEUE_H
77 # include <sys/queue.h>
78 #else
79 # include "compat/queue.h"
80 #endif
82 #ifdef HAVE_SYS_TREE_H
83 # include <sys/tree.h>
84 #else
85 # include "compat/tree.h"
86 #endif
88 #ifdef HAVE_LIBUTIL
89 # include <imsg.h>
90 # include <ohash.h>
91 # include <util.h>
92 #else
93 # include "compat/imsg.h"
94 # include "compat/ohash.h"
95 # define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, NUL */
96 int fmt_scaled(long long, char *);
97 #endif
99 #ifndef HAVE_ARC4RANDOM
100 # include <stdint.h>
101 uint32_t arc4random(void);
102 void arc4random_buf(void *, size_t);
103 uint32_t arc4random_uniform(uint32_t);
104 #endif
106 #ifndef HAVE_ASPRINTF
107 int asprintf(char **, const char *, ...);
108 int vasprintf(char **, const char *, ...);
109 #endif
111 #ifndef HAVE_ERRC
112 __dead void err(int, const char *, ...);
113 __dead void errc(int, int, const char *, ...);
114 __dead void errx(int, const char *, ...);
115 void warn(const char *, ...);
116 void warnc(int, const char *, ...);
117 void warnx(const char *, ...);
118 #else
119 #include <err.h>
120 #endif
122 #ifndef FREEZERO
123 void freezero(void *, size_t);
124 #endif
126 #ifndef HAVE_GETDTABLECOUNT
127 int getdtablecount(void);
128 #endif
130 #ifndef HAVE_GETDTABLESIZE
131 int getdtablesize(void);
132 #endif
134 #ifndef HAVE_GETPROGNAME
135 const char *getprogname(void);
136 #endif
138 #ifndef HAVE_MEMMEM
139 void *memmem(const void *, size_t, const void *, size_t);
140 #endif
142 #ifndef HAVE_REALLOCARRAY
143 void *reallocarray(void *, size_t, size_t);
144 #endif
146 #ifndef HAVE_RECALLOCARRAY
147 void *recallocarray(void *, size_t, size_t, size_t);
148 #endif
150 #ifndef HAVE_SETPROCTITLE
151 void setproctitle(const char *, ...);
152 #endif
154 #ifndef HAVE_SETPROGNAME
155 void setprogname(const char *);
156 #endif
158 #ifndef HAVE_STRLCAT
159 size_t strlcat(char *, const char *, size_t);
160 #endif
162 #ifndef HAVE_STRLCPY
163 size_t strlcpy(char *, const char *, size_t);
164 #endif
166 #ifndef HAVE_STRSEP
167 char *strsep(char **, const char *);
168 #endif
170 #ifndef HAVE_STRTONUM
171 long long strtonum(const char *, long long, long long, const char **);
172 #endif
174 #ifdef HAVE_VIS
175 # include <vis.h>
176 #else
177 # include "compat/vis.h"
178 #endif
180 #endif /* COMPAT_H */