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 #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 #ifdef HAVE_QUEUE_H
118 #include <sys/queue.h>
119 #else
120 #include "compat/queue.h"
121 #endif
123 #ifdef HAVE_TREE_H
124 #include <sys/tree.h>
125 #else
126 #include "compat/tree.h"
127 #endif
129 #ifdef HAVE_UTIL_H
130 #include <util.h>
131 #endif
133 #ifdef HAVE_LIBUTIL_H
134 #include <libutil.h>
135 #endif
137 #ifdef HAVE_IMSG
138 #else
139 #include "compat/imsg.h"
140 #endif
142 /* Include Apple-specific headers. Mostly for crypto.*/
143 #if defined(__APPLE__)
144 #define COMMON_DIGEST_FOR_OPENSSL
145 #include <CommonCrypto/CommonDigest.h>
146 #endif
148 #if defined(HAVE_LIBCRYPTO) && !defined(__APPLE__) && !defined(__DragonFly__)
149 #include <sha1.h>
150 #elif !defined(__APPLE__) && !defined(__DragonFly__)
151 #include <sha.h>
152 #elif defined(__DragonFly__)
153 #include <openssl/sha.h>
154 #endif
156 #if !defined(HAVE_LIBCRYPTO) || defined(__APPLE__) || defined(__DragonFly__)
157 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
158 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
160 #define SHA1_CTX SHA_CTX
161 #define SHA1Init SHA1_Init
162 #define SHA1Update SHA1_Update
163 #define SHA1Final SHA1_Final
164 #endif
166 #ifndef HAVE_ASPRINTF
167 /* asprintf.c */
168 int asprintf(char **, const char *, ...);
169 int vasprintf(char **, const char *, va_list);
170 #endif
172 #ifndef HAVE_EXPLICIT_BZERO
173 /* explicit_bzero.c */
174 void explicit_bzero(void *, size_t);
175 #endif
177 #ifndef HAVE_GETDTABLECOUNT
178 /* getdtablecount.c */
179 int getdtablecount(void);
180 #endif
182 #ifndef HAVE_CLOSEFROM
183 /* closefrom.c */
184 void closefrom(int);
185 #endif
187 #if defined (__FreeBSD__)
188 #define closefrom(fd) (closefrom(fd), 0)
189 #endif
191 #ifndef HAVE_STRSEP
192 /* strsep.c */
193 char *strsep(char **, const char *);
194 #endif
196 #ifndef HAVE_STRTONUM
197 /* strtonum.c */
198 long long strtonum(const char *, long long, long long, const char **);
199 #endif
201 #ifndef HAVE_STRLCPY
202 /* strlcpy.c */
203 size_t strlcpy(char *, const char *, size_t);
204 #endif
206 #ifndef HAVE_STRLCAT
207 /* strlcat.c */
208 size_t strlcat(char *, const char *, size_t);
209 #endif
211 #ifndef HAVE_STRNLEN
212 /* strnlen.c */
213 size_t strnlen(const char *, size_t);
214 #endif
216 #ifndef HAVE_STRNDUP
217 /* strndup.c */
218 char *strndup(const char *, size_t);
219 #endif
221 #ifndef HAVE_GETPROGNAME
222 /* getprogname.c */
223 const char *getprogname(void);
224 #endif
226 #ifndef HAVE_GETLINE
227 /* getline.c */
228 ssize_t getline(char **, size_t *, FILE *);
229 #endif
231 #ifndef HAVE_FREEZERO
232 /* freezero.c */
233 void freezero(void *, size_t);
234 #endif
236 #ifndef HAVE_GETDTABLECOUNT
237 /* getdtablecount.c */
238 int getdtablecount(void);
239 #endif
241 #ifndef HAVE_REALLOCARRAY
242 /* reallocarray.c */
243 void *reallocarray(void *, size_t, size_t);
244 #endif
246 #ifndef HAVE_RECALLOCARRAY
247 /* recallocarray.c */
248 void *recallocarray(void *, size_t, size_t, size_t);
249 #endif
251 #ifndef HAVE_FMT_SCALED
252 /* fmt_scaled.c */
253 int fmt_scaled(long long, char *);
254 int scan_scaled(char *, long long *);
255 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
256 #endif
258 #ifndef HAVE_LIBBSD
259 /* getopt.c */
260 extern int BSDopterr;
261 extern int BSDoptind;
262 extern int BSDoptopt;
263 extern int BSDoptreset;
264 extern char *BSDoptarg;
265 int BSDgetopt(int, char *const *, const char *);
266 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
267 #define opterr BSDopterr
268 #define optind BSDoptind
269 #define optopt BSDoptopt
270 #define optreset BSDoptreset
271 #define optarg BSDoptarg
272 #endif
273 #endif
275 /* Check for some of the non-portable timespec*() functions.
276 * This should largely come from libbsd for systems which
277 * aren't BSD, but this will depend on how old the library
278 * is.
279 */
280 #ifndef timespecisset
281 #define timespecisset(tsp) \
282 ((tsp)->tv_sec || (tsp)->tv_nsec)
283 #endif
285 #ifndef timespecsub
286 #define timespecsub(tsp, usp, vsp) \
287 do { \
288 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
289 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
290 if ((vsp)->tv_nsec < 0) { \
291 (vsp)->tv_sec--; \
292 (vsp)->tv_nsec += 1000000000L; \
293 } \
294 } while (0)
295 #endif
297 #ifndef timespeccmp
298 #define timespeccmp(tvp, uvp, cmp) \
299 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
300 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
301 ((tvp)->tv_sec cmp (uvp)->tv_sec))
302 #endif
304 #ifndef HAVE_BSD_MERGESORT
305 /* mergesort.c */
306 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
307 #endif