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