Blob


1 #ifndef _GOT_COMPAT_H
2 #define _GOT_COMPAT_H
4 #include <sys/types.h>
5 #include <sys/ioctl.h>
6 #include <sys/uio.h>
7 #if defined(__FreeBSD__)
8 #include <sys/endian.h>
9 #include <sys/capsicum.h>
10 #elif defined(__APPLE__)
11 #include <machine/endian.h>
12 #include <libkern/OSByteOrder.h>
13 #include "compat/bsd-poll.h"
15 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
17 #define htobe16(x) OSSwapHostToBigInt16(x)
18 #define htole16(x) OSSwapHostToLittleInt16(x)
19 #define be16toh(x) OSSwapBigToHostInt16(x)
20 #define le16toh(x) OSSwapLittleToHostInt16(x)
22 #define htobe32(x) OSSwapHostToBigInt32(x)
23 #define htole32(x) OSSwapHostToLittleInt32(x)
24 #define be32toh(x) OSSwapBigToHostInt32(x)
25 #define le32toh(x) OSSwapLittleToHostInt32(x)
27 #define htobe64(x) OSSwapHostToBigInt64(x)
28 #define htole64(x) OSSwapHostToLittleInt64(x)
29 #define be64toh(x) OSSwapBigToHostInt64(x)
30 #define le64toh(x) OSSwapLittleToHostInt64(x)
32 #define st_atim st_atimespec
33 #define st_ctim st_ctimespec
34 #define st_mtim st_mtimespec
36 #else /* Linux, etc... */
37 #include <endian.h>
38 #endif
40 #include <fnmatch.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdint.h>
45 /* For flock. */
46 #ifndef O_EXLOCK
47 #define O_EXLOCK 0
48 #endif
50 #ifndef HAVE_FLOCK
51 #define LOCK_SH 0
52 #define LOCK_EX 0
53 #define LOCK_NB 0
54 #define flock(fd, op) (0)
55 #else
56 #include <sys/file.h>
57 #endif
59 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
60 #ifndef WAIT_ANY
61 #define WAIT_ANY (-1)
62 #endif
64 /* SOCK_NONBLOCK isn't available across BSDs... */
65 #ifndef SOCK_NONBLOCK
66 #define SOCK_NONBLOCK 00004000
67 #endif
69 #ifndef __dead
70 #define __dead __attribute__ ((__noreturn__))
71 #endif
73 #ifndef __unused
74 #define __unused __attribute__ ((__unused__))
75 #endif
77 #ifndef __OpenBSD__
78 #define pledge(s, p) (0)
79 #define unveil(s, p) (0)
80 #endif
82 #ifndef __FreeBSD__
83 #define cap_enter() (0)
84 #endif
86 #ifndef HAVE_LINUX_LANDLOCK_H
87 #define landlock_no_fs() (0)
88 #else
89 int landlock_no_fs(void);
90 #endif
92 #ifndef INFTIM
93 #define INFTIM -1
94 #endif
96 #ifndef HAVE_BSD_UUID
97 #include <uuid/uuid.h>
98 #define uuid_s_ok 0
99 #define uuid_s_bad_version 1
100 #define uuid_s_invalid_string_uuid 2
101 #define uuid_s_no_memory 3
103 /* Length of a node address (an IEEE 802 address). */
104 #define _UUID_NODE_LEN 6
106 struct uuid {
107 uint32_t time_low;
108 uint16_t time_mid;
109 uint16_t time_hi_and_version;
110 uint8_t clock_seq_hi_and_reserved;
111 uint8_t clock_seq_low;
112 uint8_t node[_UUID_NODE_LEN];
113 };
115 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
116 int32_t uuid_is_nil(struct uuid *, uint32_t *);
117 void uuid_create(uuid_t *, uint32_t *);
118 void uuid_create_nil(struct uuid *, uint32_t *);
119 void uuid_from_string(const char *, uuid_t *, uint32_t *);
120 void uuid_to_string(uuid_t *, char **, uint32_t *);
121 #else
122 #include <uuid.h>
123 #endif
125 #ifdef HAVE_STDINT_H
126 #include <stdint.h>
127 #else
128 #include <inttypes.h>
129 #endif
131 #ifdef HAVE_QUEUE_H
132 #include <sys/queue.h>
133 #else
134 #include "compat/queue.h"
135 #endif
137 #ifdef HAVE_TREE_H
138 #include <sys/tree.h>
139 #else
140 #include "compat/tree.h"
141 #endif
143 #ifdef HAVE_UTIL_H
144 #include <util.h>
145 #endif
147 #ifdef HAVE_LIBUTIL_H
148 #include <libutil.h>
149 #endif
151 #ifdef HAVE_IMSG
152 #else
153 #include "compat/imsg.h"
154 #endif
156 #ifdef HAVE_SIPHASH
157 #include <siphash.h>
158 #else
159 #include "compat/siphash.h"
160 #endif
162 /* Include Apple-specific headers. Mostly for crypto.*/
163 #if defined(__APPLE__)
164 #define COMMON_DIGEST_FOR_OPENSSL
165 #include <CommonCrypto/CommonDigest.h>
166 #endif
168 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
169 #include <sha1.h>
170 #elif !defined(__APPLE__) && !defined(__DragonFly__)
171 #include <sha.h>
172 #elif defined(__DragonFly__)
173 #include <openssl/sha.h>
174 #endif
176 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
177 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
178 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
180 #define SHA1_CTX SHA_CTX
181 #define SHA1Init SHA1_Init
182 #define SHA1Update SHA1_Update
183 #define SHA1Final SHA1_Final
184 #endif
186 #ifndef HAVE_ASPRINTF
187 /* asprintf.c */
188 int asprintf(char **, const char *, ...);
189 int vasprintf(char **, const char *, va_list);
190 #endif
192 #ifndef HAVE_EXPLICIT_BZERO
193 /* explicit_bzero.c */
194 void explicit_bzero(void *, size_t);
195 #endif
197 #ifndef HAVE_GETDTABLECOUNT
198 /* getdtablecount.c */
199 int getdtablecount(void);
200 #endif
202 #ifndef HAVE_CLOSEFROM
203 /* closefrom.c */
204 void closefrom(int);
205 #endif
207 #if defined (__FreeBSD__)
208 #define closefrom(fd) (closefrom(fd), 0)
209 #endif
211 #ifndef HAVE_STRSEP
212 /* strsep.c */
213 char *strsep(char **, const char *);
214 #endif
216 #ifndef HAVE_STRTONUM
217 /* strtonum.c */
218 long long strtonum(const char *, long long, long long, const char **);
219 #endif
221 #ifndef HAVE_STRLCPY
222 /* strlcpy.c */
223 size_t strlcpy(char *, const char *, size_t);
224 #endif
226 #ifndef HAVE_STRLCAT
227 /* strlcat.c */
228 size_t strlcat(char *, const char *, size_t);
229 #endif
231 #ifndef HAVE_STRNLEN
232 /* strnlen.c */
233 size_t strnlen(const char *, size_t);
234 #endif
236 #ifndef HAVE_STRNDUP
237 /* strndup.c */
238 char *strndup(const char *, size_t);
239 #endif
241 #ifndef HAVE_GETPROGNAME
242 /* getprogname.c */
243 const char *getprogname(void);
244 #endif
246 #ifndef HAVE_GETLINE
247 /* getline.c */
248 ssize_t getline(char **, size_t *, FILE *);
249 #endif
251 #ifndef HAVE_FREEZERO
252 /* freezero.c */
253 void freezero(void *, size_t);
254 #endif
256 #ifndef HAVE_GETDTABLECOUNT
257 /* getdtablecount.c */
258 int getdtablecount(void);
259 #endif
261 #ifndef HAVE_REALLOCARRAY
262 /* reallocarray.c */
263 void *reallocarray(void *, size_t, size_t);
264 #endif
266 #ifndef HAVE_RECALLOCARRAY
267 /* recallocarray.c */
268 void *recallocarray(void *, size_t, size_t, size_t);
269 #endif
271 #ifndef HAVE_SETPROCTITLE
272 /* setproctitle.c */
273 void setproctitle(const char *, ...);
274 #endif
276 #ifndef HAVE_FMT_SCALED
277 /* fmt_scaled.c */
278 int fmt_scaled(long long, char *);
279 int scan_scaled(char *, long long *);
280 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
281 #endif
283 #ifndef HAVE_LIBBSD
284 /* getopt.c */
285 extern int BSDopterr;
286 extern int BSDoptind;
287 extern int BSDoptopt;
288 extern int BSDoptreset;
289 extern char *BSDoptarg;
290 int BSDgetopt(int, char *const *, const char *);
291 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
292 #define opterr BSDopterr
293 #define optind BSDoptind
294 #define optopt BSDoptopt
295 #define optreset BSDoptreset
296 #define optarg BSDoptarg
297 #endif
298 #endif
300 /* Check for some of the non-portable timespec*() functions.
301 * This should largely come from libbsd for systems which
302 * aren't BSD, but this will depend on how old the library
303 * is.
304 */
305 #ifndef timespecisset
306 #define timespecisset(tsp) \
307 ((tsp)->tv_sec || (tsp)->tv_nsec)
308 #endif
310 #ifndef timespecsub
311 #define timespecsub(tsp, usp, vsp) \
312 do { \
313 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
314 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
315 if ((vsp)->tv_nsec < 0) { \
316 (vsp)->tv_sec--; \
317 (vsp)->tv_nsec += 1000000000L; \
318 } \
319 } while (0)
320 #endif
322 #ifndef timespeccmp
323 #define timespeccmp(tvp, uvp, cmp) \
324 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
325 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
326 ((tvp)->tv_sec cmp (uvp)->tv_sec))
327 #endif
329 #ifndef HAVE_BSD_MERGESORT
330 /* mergesort.c */
331 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
332 #endif