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