Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef COMPAT_H
18 #define COMPAT_H
20 #include "config.h"
22 #include <sys/types.h>
23 #include <sys/uio.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdint.h>
29 #if HAVE_ENDIAN_H
30 # include <endian.h>
31 #elif HAVE_SYS_ENDIAN_H
32 # include <sys/endian.h>
33 #elif HAVE_LIBKERN_OSBYTEORDER_H
34 # include <machine/endian.h>
35 # include <libkern/OSByteOrder.h>
37 # define htobe16(x) OSSwapHostToBigInt16(x)
38 # define htole16(x) OSSwapHostToLittleInt16(x)
39 # define be16toh(x) OSSwapBigToHostInt16(x)
40 # define le16toh(x) OSSwapLittleToHostInt16(x)
42 # define htobe32(x) OSSwapHostToBigInt32(x)
43 # define htole32(x) OSSwapHostToLittleInt32(x)
44 # define be32toh(x) OSSwapBigToHostInt32(x)
45 # define le32toh(x) OSSwapLittleToHostInt32(x)
47 # define htobe64(x) OSSwapHostToBigInt64(x)
48 # define htole64(x) OSSwapHostToLittleInt64(x)
49 # define be64toh(x) OSSwapBigToHostInt64(x)
50 # define le64toh(x) OSSwapLittleToHostInt64(x)
51 #else
52 # error no compatible endian.h header found
53 #endif
55 #ifdef HAVE_QUEUE_H
56 # include <sys/queue.h>
57 #else
58 # include "compat/queue.h"
59 #endif
61 #ifdef HAVE_IMSG
62 # include <imsg.h>
63 #else
64 # include "compat/imsg.h"
65 #endif
67 #ifdef HAVE_LIBUTIL
68 # include <ohash.h>
69 # include <util.h>
70 #else
71 # include "compat/ohash.h"
72 # define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
73 int scan_scaled(char *, long long *);
74 int fmt_scaled(long long, char *);
75 #endif
77 #ifndef HAVE_ASPRINTF
78 int asprintf(char**, const char*, ...);
79 int vasprintf(char**, const char*, va_list);
80 #endif
82 #ifndef HAVE_ERR
83 void err(int, const char*, ...);
84 void errx(int, const char*, ...);
85 void warn(int, const char*, ...);
86 void warnx(int, const char*, ...);
87 #else
88 # include <err.h>
89 #endif
91 #ifndef HAVE_EXPLICIT_BZERO
92 void explicit_bzero(void *, size_t);
93 #endif
95 #ifndef HAVE_FREEZERO
96 void freezero(void*, size_t);
97 #endif
99 #ifndef HAVE_GETDTABLECOUNT
100 int getdtablecount(void);
101 #endif
103 #ifndef HAVE_GETDTABLESIZE
104 int getdtablesize(void);
105 #endif
107 #ifndef HAVE_GETPROGNAME
108 const char *getprogname(void);
109 #endif
111 #ifndef HAVE_MEMMEM
112 void *memmem(const void*, size_t, const void*, size_t);
113 #endif
115 #ifndef HAVE_REALLOCARRAY
116 void *reallocarray(void*, size_t, size_t);
117 #endif
119 #ifndef HAVE_RECALLOCARRAY
120 void *recallocarray(void*, size_t, size_t, size_t);
121 #endif
123 #ifndef HAVE_STRCASESTR
124 char *strcasestr(const char *, const char *);
125 #endif
127 #ifndef HAVE_STRLCPY
128 size_t strlcpy(char*, const char*, size_t);
129 #endif
131 #ifndef HAVE_STRLCAT
132 size_t strlcat(char*, const char*, size_t);
133 #endif
135 #ifndef HAVE_STRSEP
136 char *strsep(char**, const char*);
137 #endif
139 #ifndef HAVE_STRTONUM
140 long long strtonum(const char*, long long, long long, const char**);
141 #endif
143 #ifndef HAVE_SETPROCTITLE
144 void setproctitle(const char*, ...);
145 #endif
147 #ifndef __dead
148 #define __dead __attribute__((__noreturn__))
149 #endif
151 /*
152 * The following time-related macros from sys/time.h are subject to:
154 * Copyright (c) 1982, 1986, 1993
155 * The Regents of the University of California. All rights reserved.
157 * Redistribution and use in source and binary forms, with or without
158 * modification, are permitted provided that the following conditions
159 * are met:
160 * 1. Redistributions of source code must retain the above copyright
161 * notice, this list of conditions and the following disclaimer.
162 * 2. Redistributions in binary form must reproduce the above copyright
163 * notice, this list of conditions and the following disclaimer in the
164 * documentation and/or other materials provided with the distribution.
165 * 3. Neither the name of the University nor the names of its contributors
166 * may be used to endorse or promote products derived from this software
167 * without specific prior written permission.
169 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
170 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
173 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
174 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
175 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
176 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
177 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
178 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
179 * SUCH DAMAGE.
181 * @(#)time.h 8.2 (Berkeley) 7/10/94
182 */
184 #ifndef HAVE_TIMESPECSUB
185 #define timespecsub(tsp, usp, vsp) \
186 do { \
187 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
188 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
189 if ((vsp)->tv_nsec < 0) { \
190 (vsp)->tv_sec--; \
191 (vsp)->tv_nsec += 1000000000L; \
192 } \
193 } while (0)
194 #endif
196 #ifndef HAVE_TIMERCMP
197 #define timercmp(tvp, uvp, cmp) \
198 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
199 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
200 ((tvp)->tv_sec cmp (uvp)->tv_sec))
201 #endif
203 #ifndef HAVE_TIMEVAL_TO_TIMESPEC
204 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
205 (ts)->tv_sec = (tv)->tv_sec; \
206 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
207 } while (0)
208 #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
209 (tv)->tv_sec = (ts)->tv_sec; \
210 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
211 } while (0)
212 #endif
214 #endif /* COMPAT_H */