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 __OpenBSD__
74 #define pledge(s, p) (0)
75 #define unveil(s, p) (0)
76 #endif
78 #ifndef __FreeBSD__
79 #define cap_enter() (0)
80 #endif
82 #ifndef HAVE_LINUX_LANDLOCK_H
83 #define landlock_no_fs() (0)
84 #else
85 int landlock_no_fs(void);
86 #endif
88 #ifndef INFTIM
89 #define INFTIM -1
90 #endif
92 #ifndef HAVE_BSD_UUID
93 #include <uuid/uuid.h>
94 #define uuid_s_ok 0
95 #define uuid_s_bad_version 1
96 #define uuid_s_invalid_string_uuid 2
97 #define uuid_s_no_memory 3
99 /* Length of a node address (an IEEE 802 address). */
100 #define _UUID_NODE_LEN 6
102 struct uuid {
103 uint32_t time_low;
104 uint16_t time_mid;
105 uint16_t time_hi_and_version;
106 uint8_t clock_seq_hi_and_reserved;
107 uint8_t clock_seq_low;
108 uint8_t node[_UUID_NODE_LEN];
109 };
111 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
112 int32_t uuid_is_nil(struct uuid *, uint32_t *);
113 void uuid_create(uuid_t *, uint32_t *);
114 void uuid_create_nil(struct uuid *, uint32_t *);
115 void uuid_from_string(const char *, uuid_t *, uint32_t *);
116 void uuid_to_string(uuid_t *, char **, uint32_t *);
117 #else
118 #include <uuid.h>
119 #endif
121 #ifdef HAVE_STDINT_H
122 #include <stdint.h>
123 #else
124 #include <inttypes.h>
125 #endif
127 #ifdef HAVE_QUEUE_H
128 #include <sys/queue.h>
129 #else
130 #include "compat/queue.h"
131 #endif
133 #ifdef HAVE_TREE_H
134 #include <sys/tree.h>
135 #else
136 #include "compat/tree.h"
137 #endif
139 #ifdef HAVE_UTIL_H
140 #include <util.h>
141 #endif
143 #ifdef HAVE_LIBUTIL_H
144 #include <libutil.h>
145 #endif
147 #ifdef HAVE_IMSG
148 #else
149 #include "compat/imsg.h"
150 #endif
152 #ifdef HAVE_SIPHASH
153 #include <siphash.h>
154 #else
155 #include "compat/siphash.h"
156 #endif
158 /* Include Apple-specific headers. Mostly for crypto.*/
159 #if defined(__APPLE__)
160 #define COMMON_DIGEST_FOR_OPENSSL
161 #include <CommonCrypto/CommonDigest.h>
162 #endif
164 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
165 #include <sha1.h>
166 #elif !defined(__APPLE__) && !defined(__DragonFly__)
167 #include <sha.h>
168 #elif defined(__DragonFly__)
169 #include <openssl/sha.h>
170 #endif
172 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
173 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
174 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
176 #define SHA1_CTX SHA_CTX
177 #define SHA1Init SHA1_Init
178 #define SHA1Update SHA1_Update
179 #define SHA1Final SHA1_Final
180 #endif
182 #ifndef HAVE_ASPRINTF
183 /* asprintf.c */
184 int asprintf(char **, const char *, ...);
185 int vasprintf(char **, const char *, va_list);
186 #endif
188 #ifndef HAVE_EXPLICIT_BZERO
189 /* explicit_bzero.c */
190 void explicit_bzero(void *, size_t);
191 #endif
193 #ifndef HAVE_GETDTABLECOUNT
194 /* getdtablecount.c */
195 int getdtablecount(void);
196 #endif
198 #ifndef HAVE_CLOSEFROM
199 /* closefrom.c */
200 void closefrom(int);
201 #endif
203 #if defined (__FreeBSD__)
204 #define closefrom(fd) (closefrom(fd), 0)
205 #endif
207 #ifndef HAVE_STRSEP
208 /* strsep.c */
209 char *strsep(char **, const char *);
210 #endif
212 #ifndef HAVE_STRTONUM
213 /* strtonum.c */
214 long long strtonum(const char *, long long, long long, const char **);
215 #endif
217 #ifndef HAVE_STRLCPY
218 /* strlcpy.c */
219 size_t strlcpy(char *, const char *, size_t);
220 #endif
222 #ifndef HAVE_STRLCAT
223 /* strlcat.c */
224 size_t strlcat(char *, const char *, size_t);
225 #endif
227 #ifndef HAVE_STRNLEN
228 /* strnlen.c */
229 size_t strnlen(const char *, size_t);
230 #endif
232 #ifndef HAVE_STRNDUP
233 /* strndup.c */
234 char *strndup(const char *, size_t);
235 #endif
237 #ifndef HAVE_GETPROGNAME
238 /* getprogname.c */
239 const char *getprogname(void);
240 #endif
242 #ifndef HAVE_GETLINE
243 /* getline.c */
244 ssize_t getline(char **, size_t *, FILE *);
245 #endif
247 #ifndef HAVE_FREEZERO
248 /* freezero.c */
249 void freezero(void *, size_t);
250 #endif
252 #ifndef HAVE_GETDTABLECOUNT
253 /* getdtablecount.c */
254 int getdtablecount(void);
255 #endif
257 #ifndef HAVE_REALLOCARRAY
258 /* reallocarray.c */
259 void *reallocarray(void *, size_t, size_t);
260 #endif
262 #ifndef HAVE_RECALLOCARRAY
263 /* recallocarray.c */
264 void *recallocarray(void *, size_t, size_t, size_t);
265 #endif
267 #ifndef HAVE_FMT_SCALED
268 /* fmt_scaled.c */
269 int fmt_scaled(long long, char *);
270 int scan_scaled(char *, long long *);
271 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
272 #endif
274 #ifndef HAVE_LIBBSD
275 /* getopt.c */
276 extern int BSDopterr;
277 extern int BSDoptind;
278 extern int BSDoptopt;
279 extern int BSDoptreset;
280 extern char *BSDoptarg;
281 int BSDgetopt(int, char *const *, const char *);
282 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
283 #define opterr BSDopterr
284 #define optind BSDoptind
285 #define optopt BSDoptopt
286 #define optreset BSDoptreset
287 #define optarg BSDoptarg
288 #endif
289 #endif
291 /* Check for some of the non-portable timespec*() functions.
292 * This should largely come from libbsd for systems which
293 * aren't BSD, but this will depend on how old the library
294 * is.
295 */
296 #ifndef timespecisset
297 #define timespecisset(tsp) \
298 ((tsp)->tv_sec || (tsp)->tv_nsec)
299 #endif
301 #ifndef timespecsub
302 #define timespecsub(tsp, usp, vsp) \
303 do { \
304 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
305 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
306 if ((vsp)->tv_nsec < 0) { \
307 (vsp)->tv_sec--; \
308 (vsp)->tv_nsec += 1000000000L; \
309 } \
310 } while (0)
311 #endif
313 #ifndef timespeccmp
314 #define timespeccmp(tvp, uvp, cmp) \
315 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
316 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
317 ((tvp)->tv_sec cmp (uvp)->tv_sec))
318 #endif
320 #ifndef HAVE_BSD_MERGESORT
321 /* mergesort.c */
322 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
323 #endif