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 #elif defined(__APPLE__)
10 #include <machine/endian.h>
11 #include <libkern/OSByteOrder.h>
12 #include "compat/bsd-poll.h"
14 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
16 #define htobe16(x) OSSwapHostToBigInt16(x)
17 #define htole16(x) OSSwapHostToLittleInt16(x)
18 #define be16toh(x) OSSwapBigToHostInt16(x)
19 #define le16toh(x) OSSwapLittleToHostInt16(x)
21 #define htobe32(x) OSSwapHostToBigInt32(x)
22 #define htole32(x) OSSwapHostToLittleInt32(x)
23 #define be32toh(x) OSSwapBigToHostInt32(x)
24 #define le32toh(x) OSSwapLittleToHostInt32(x)
26 #define htobe64(x) OSSwapHostToBigInt64(x)
27 #define htole64(x) OSSwapHostToLittleInt64(x)
28 #define be64toh(x) OSSwapBigToHostInt64(x)
29 #define le64toh(x) OSSwapLittleToHostInt64(x)
31 #define st_atim st_atimespec
32 #define st_ctim st_ctimespec
33 #define st_mtim st_mtimespec
35 #else /* Linux, etc... */
36 #include <endian.h>
37 #endif
39 #include <fnmatch.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdint.h>
44 /*
45 #include <termios.h>
46 #include <wchar.h>
47 */
49 /* For flock. */
50 #ifndef O_EXLOCK
51 #define O_EXLOCK 0
52 #endif
54 #ifndef HAVE_FLOCK
55 #define LOCK_SH 0
56 #define LOCK_EX 0
57 #define LOCK_NB 0
58 #define flock(fd, op) (0)
59 #else
60 #include <sys/file.h>
61 #endif
63 #ifndef __dead
64 #define __dead __attribute__ ((__noreturn__))
65 #endif
67 #ifndef __OpenBSD__
68 #define pledge(s, p) (0)
69 #define unveil(s, p) (0)
70 #endif
72 #ifdef __FreeBSD__
73 #include <sys/capsicum.h>
74 #else
75 #define cap_enter() (0)
76 #endif
78 #ifndef HAVE_LINUX_LANDLOCK_H
79 #define landlock_no_fs() (0)
80 #else
81 int landlock_no_fs(void);
82 #endif
84 #ifndef INFTIM
85 #define INFTIM -1
86 #endif
88 #ifndef HAVE_BSD_UUID
89 #include <uuid/uuid.h>
90 #define uuid_s_ok 0
91 #define uuid_s_bad_version 1
92 #define uuid_s_invalid_string_uuid 2
93 #define uuid_s_no_memory 3
95 /* Length of a node address (an IEEE 802 address). */
96 #define _UUID_NODE_LEN 6
98 struct uuid {
99 uint32_t time_low;
100 uint16_t time_mid;
101 uint16_t time_hi_and_version;
102 uint8_t clock_seq_hi_and_reserved;
103 uint8_t clock_seq_low;
104 uint8_t node[_UUID_NODE_LEN];
105 };
107 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
108 int32_t uuid_is_nil(struct uuid *, uint32_t *);
109 void uuid_create(uuid_t *, uint32_t *);
110 void uuid_create_nil(struct uuid *, uint32_t *);
111 void uuid_from_string(const char *, uuid_t *, uint32_t *);
112 void uuid_to_string(uuid_t *, char **, uint32_t *);
113 #else
114 #include <uuid.h>
115 #endif
117 #ifdef HAVE_STDINT_H
118 #include <stdint.h>
119 #else
120 #include <inttypes.h>
121 #endif
123 #ifdef HAVE_QUEUE_H
124 #include <sys/queue.h>
125 #else
126 #include "compat/queue.h"
127 #endif
129 #ifdef HAVE_TREE_H
130 #include <sys/tree.h>
131 #else
132 #include "compat/tree.h"
133 #endif
135 #ifdef HAVE_UTIL_H
136 #include <util.h>
137 #endif
139 #ifdef HAVE_LIBUTIL_H
140 #include <libutil.h>
141 #endif
143 #ifdef HAVE_IMSG
144 #else
145 #include "compat/imsg.h"
146 #endif
148 #ifdef HAVE_SIPHASH
149 #include <siphash.h>
150 #else
151 #include "compat/siphash.h"
152 #endif
154 /* Include Apple-specific headers. Mostly for crypto.*/
155 #if defined(__APPLE__)
156 #define COMMON_DIGEST_FOR_OPENSSL
157 #include <CommonCrypto/CommonDigest.h>
158 #endif
160 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
161 #include <sha1.h>
162 #elif !defined(__APPLE__) && !defined(__DragonFly__)
163 #include <sha.h>
164 #elif defined(__DragonFly__)
165 #include <openssl/sha.h>
166 #endif
168 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
169 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
170 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
172 #define SHA1_CTX SHA_CTX
173 #define SHA1Init SHA1_Init
174 #define SHA1Update SHA1_Update
175 #define SHA1Final SHA1_Final
176 #endif
178 #ifndef HAVE_ASPRINTF
179 /* asprintf.c */
180 int asprintf(char **, const char *, ...);
181 int vasprintf(char **, const char *, va_list);
182 #endif
184 #ifndef HAVE_EXPLICIT_BZERO
185 /* explicit_bzero.c */
186 void explicit_bzero(void *, size_t);
187 #endif
189 #ifndef HAVE_GETDTABLECOUNT
190 /* getdtablecount.c */
191 int getdtablecount(void);
192 #endif
194 #ifndef HAVE_CLOSEFROM
195 /* closefrom.c */
196 void closefrom(int);
197 #endif
199 #if defined (__FreeBSD__)
200 #define closefrom(fd) (closefrom(fd), 0)
201 #endif
203 #ifndef HAVE_STRSEP
204 /* strsep.c */
205 char *strsep(char **, const char *);
206 #endif
208 #ifndef HAVE_STRTONUM
209 /* strtonum.c */
210 long long strtonum(const char *, long long, long long, const char **);
211 #endif
213 #ifndef HAVE_STRLCPY
214 /* strlcpy.c */
215 size_t strlcpy(char *, const char *, size_t);
216 #endif
218 #ifndef HAVE_STRLCAT
219 /* strlcat.c */
220 size_t strlcat(char *, const char *, size_t);
221 #endif
223 #ifndef HAVE_STRNLEN
224 /* strnlen.c */
225 size_t strnlen(const char *, size_t);
226 #endif
228 #ifndef HAVE_STRNDUP
229 /* strndup.c */
230 char *strndup(const char *, size_t);
231 #endif
233 #ifndef HAVE_GETPROGNAME
234 /* getprogname.c */
235 const char *getprogname(void);
236 #endif
238 #ifndef HAVE_GETLINE
239 /* getline.c */
240 ssize_t getline(char **, size_t *, FILE *);
241 #endif
243 #ifndef HAVE_FREEZERO
244 /* freezero.c */
245 void freezero(void *, size_t);
246 #endif
248 #ifndef HAVE_GETDTABLECOUNT
249 /* getdtablecount.c */
250 int getdtablecount(void);
251 #endif
253 #ifndef HAVE_REALLOCARRAY
254 /* reallocarray.c */
255 void *reallocarray(void *, size_t, size_t);
256 #endif
258 #ifndef HAVE_RECALLOCARRAY
259 /* recallocarray.c */
260 void *recallocarray(void *, size_t, size_t, size_t);
261 #endif
263 #ifndef HAVE_FMT_SCALED
264 /* fmt_scaled.c */
265 int fmt_scaled(long long, char *);
266 int scan_scaled(char *, long long *);
267 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
268 #endif
270 #ifndef HAVE_LIBBSD
271 /* getopt.c */
272 extern int BSDopterr;
273 extern int BSDoptind;
274 extern int BSDoptopt;
275 extern int BSDoptreset;
276 extern char *BSDoptarg;
277 int BSDgetopt(int, char *const *, const char *);
278 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
279 #define opterr BSDopterr
280 #define optind BSDoptind
281 #define optopt BSDoptopt
282 #define optreset BSDoptreset
283 #define optarg BSDoptarg
284 #endif
285 #endif
287 /* Check for some of the non-portable timespec*() functions.
288 * This should largely come from libbsd for systems which
289 * aren't BSD, but this will depend on how old the library
290 * is.
291 */
292 #ifndef timespecisset
293 #define timespecisset(tsp) \
294 ((tsp)->tv_sec || (tsp)->tv_nsec)
295 #endif
297 #ifndef timespecsub
298 #define timespecsub(tsp, usp, vsp) \
299 do { \
300 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
301 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
302 if ((vsp)->tv_nsec < 0) { \
303 (vsp)->tv_sec--; \
304 (vsp)->tv_nsec += 1000000000L; \
305 } \
306 } while (0)
307 #endif
309 #ifndef timespeccmp
310 #define timespeccmp(tvp, uvp, cmp) \
311 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
312 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
313 ((tvp)->tv_sec cmp (uvp)->tv_sec))
314 #endif
316 #ifndef HAVE_BSD_MERGESORT
317 /* mergesort.c */
318 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
319 #endif