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 /*
46 #include <termios.h>
47 #include <wchar.h>
48 */
50 /* For flock. */
51 #ifndef O_EXLOCK
52 #define O_EXLOCK 0
53 #endif
55 #ifndef HAVE_FLOCK
56 #define LOCK_SH 0
57 #define LOCK_EX 0
58 #define LOCK_NB 0
59 #define flock(fd, op) (0)
60 #else
61 #include <sys/file.h>
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 #ifdef HAVE_QUEUE_H
123 #include <sys/queue.h>
124 #else
125 #include "compat/queue.h"
126 #endif
128 #ifdef HAVE_TREE_H
129 #include <sys/tree.h>
130 #else
131 #include "compat/tree.h"
132 #endif
134 #ifdef HAVE_UTIL_H
135 #include <util.h>
136 #endif
138 #ifdef HAVE_LIBUTIL_H
139 #include <libutil.h>
140 #endif
142 #ifdef HAVE_IMSG
143 #else
144 #include "compat/imsg.h"
145 #endif
147 #ifdef HAVE_SIPHASH
148 #include <siphash.h>
149 #else
150 #include "compat/siphash.h"
151 #endif
153 /* Include Apple-specific headers. Mostly for crypto.*/
154 #if defined(__APPLE__)
155 #define COMMON_DIGEST_FOR_OPENSSL
156 #include <CommonCrypto/CommonDigest.h>
157 #endif
159 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
160 #include <sha1.h>
161 #elif !defined(__APPLE__) && !defined(__DragonFly__)
162 #include <sha.h>
163 #elif defined(__DragonFly__)
164 #include <openssl/sha.h>
165 #endif
167 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
168 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
169 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
171 #define SHA1_CTX SHA_CTX
172 #define SHA1Init SHA1_Init
173 #define SHA1Update SHA1_Update
174 #define SHA1Final SHA1_Final
175 #endif
177 #ifndef HAVE_ASPRINTF
178 /* asprintf.c */
179 int asprintf(char **, const char *, ...);
180 int vasprintf(char **, const char *, va_list);
181 #endif
183 #ifndef HAVE_EXPLICIT_BZERO
184 /* explicit_bzero.c */
185 void explicit_bzero(void *, size_t);
186 #endif
188 #ifndef HAVE_GETDTABLECOUNT
189 /* getdtablecount.c */
190 int getdtablecount(void);
191 #endif
193 #ifndef HAVE_CLOSEFROM
194 /* closefrom.c */
195 void closefrom(int);
196 #endif
198 #if defined (__FreeBSD__)
199 #define closefrom(fd) (closefrom(fd), 0)
200 #endif
202 #ifndef HAVE_STRSEP
203 /* strsep.c */
204 char *strsep(char **, const char *);
205 #endif
207 #ifndef HAVE_STRTONUM
208 /* strtonum.c */
209 long long strtonum(const char *, long long, long long, const char **);
210 #endif
212 #ifndef HAVE_STRLCPY
213 /* strlcpy.c */
214 size_t strlcpy(char *, const char *, size_t);
215 #endif
217 #ifndef HAVE_STRLCAT
218 /* strlcat.c */
219 size_t strlcat(char *, const char *, size_t);
220 #endif
222 #ifndef HAVE_STRNLEN
223 /* strnlen.c */
224 size_t strnlen(const char *, size_t);
225 #endif
227 #ifndef HAVE_STRNDUP
228 /* strndup.c */
229 char *strndup(const char *, size_t);
230 #endif
232 #ifndef HAVE_GETPROGNAME
233 /* getprogname.c */
234 const char *getprogname(void);
235 #endif
237 #ifndef HAVE_GETLINE
238 /* getline.c */
239 ssize_t getline(char **, size_t *, FILE *);
240 #endif
242 #ifndef HAVE_FREEZERO
243 /* freezero.c */
244 void freezero(void *, size_t);
245 #endif
247 #ifndef HAVE_GETDTABLECOUNT
248 /* getdtablecount.c */
249 int getdtablecount(void);
250 #endif
252 #ifndef HAVE_REALLOCARRAY
253 /* reallocarray.c */
254 void *reallocarray(void *, size_t, size_t);
255 #endif
257 #ifndef HAVE_RECALLOCARRAY
258 /* recallocarray.c */
259 void *recallocarray(void *, size_t, size_t, size_t);
260 #endif
262 #ifndef HAVE_FMT_SCALED
263 /* fmt_scaled.c */
264 int fmt_scaled(long long, char *);
265 int scan_scaled(char *, long long *);
266 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
267 #endif
269 #ifndef HAVE_LIBBSD
270 /* getopt.c */
271 extern int BSDopterr;
272 extern int BSDoptind;
273 extern int BSDoptopt;
274 extern int BSDoptreset;
275 extern char *BSDoptarg;
276 int BSDgetopt(int, char *const *, const char *);
277 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
278 #define opterr BSDopterr
279 #define optind BSDoptind
280 #define optopt BSDoptopt
281 #define optreset BSDoptreset
282 #define optarg BSDoptarg
283 #endif
284 #endif
286 /* Check for some of the non-portable timespec*() functions.
287 * This should largely come from libbsd for systems which
288 * aren't BSD, but this will depend on how old the library
289 * is.
290 */
291 #ifndef timespecisset
292 #define timespecisset(tsp) \
293 ((tsp)->tv_sec || (tsp)->tv_nsec)
294 #endif
296 #ifndef timespecsub
297 #define timespecsub(tsp, usp, vsp) \
298 do { \
299 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
300 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
301 if ((vsp)->tv_nsec < 0) { \
302 (vsp)->tv_sec--; \
303 (vsp)->tv_nsec += 1000000000L; \
304 } \
305 } while (0)
306 #endif
308 #ifndef timespeccmp
309 #define timespeccmp(tvp, uvp, cmp) \
310 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
311 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
312 ((tvp)->tv_sec cmp (uvp)->tv_sec))
313 #endif
315 #ifndef HAVE_BSD_MERGESORT
316 /* mergesort.c */
317 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
318 #endif