1/*
2 * Copyright (c) 2007 Nicholas Marriott <[email protected]>
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 MIND, USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef COMPAT_H
18#define COMPAT_H
19
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include <sys/uio.h>
23
24#include <fnmatch.h>
25#include <limits.h>
26#include <stdio.h>
27#include <termios.h>
28#include <wchar.h>
29
30#ifndef __GNUC__
31#define __attribute__(a)
32#endif
33
34#ifndef __unused
35#define __unused __attribute__ ((__unused__))
36#endif
37#ifndef __dead
38#define __dead __attribute__ ((__noreturn__))
39#endif
40#ifndef __packed
41#define __packed __attribute__ ((__packed__))
42#endif
43
44#ifndef ECHOPRT
45#define ECHOPRT 0
46#endif
47
48#ifndef ACCESSPERMS
49#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
50#endif
51
52#if !defined(FIONREAD) && defined(__sun)
53#include <sys/filio.h>
54#endif
55
56#ifdef HAVE_ERR_H
57#include <err.h>
58#else
59void err(int, const char *, ...);
60void errx(int, const char *, ...);
61void warn(const char *, ...);
62void warnx(const char *, ...);
63#endif
64
65#ifdef HAVE_PATHS_H
66#include <paths.h>
67#endif
68
69#ifndef _PATH_BSHELL
70#define _PATH_BSHELL "/bin/sh"
71#endif
72
73#ifndef _PATH_TMP
74#define _PATH_TMP "/tmp/"
75#endif
76
77#ifndef _PATH_DEVNULL
78#define _PATH_DEVNULL "/dev/null"
79#endif
80
81#ifndef _PATH_TTY
82#define _PATH_TTY "/dev/tty"
83#endif
84
85#ifndef _PATH_DEV
86#define _PATH_DEV "/dev/"
87#endif
88
89#ifndef _PATH_DEFPATH
90#define _PATH_DEFPATH "/usr/bin:/bin"
91#endif
92
93#ifndef __OpenBSD__
94#define pledge(s, p) (0)
95#endif
96
97#ifdef HAVE_STDINT_H
98#include <stdint.h>
99#else
100#include <inttypes.h>
101#endif
102
103#ifdef HAVE_QUEUE_H
104#include <sys/queue.h>
105#else
106#include "compat/queue.h"
107#endif
108
109#ifdef HAVE_TREE_H
110#include <sys/tree.h>
111#else
112#include "compat/tree.h"
113#endif
114
115#ifdef HAVE_BITSTRING_H
116#include <bitstring.h>
117#else
118#include "compat/bitstring.h"
119#endif
120
121#ifdef HAVE_LIBUTIL_H
122#include <libutil.h>
123#endif
124
125#ifdef HAVE_PTY_H
126#include <pty.h>
127#endif
128
129#ifdef HAVE_UTIL_H
130#include <util.h>
131#endif
132
133#ifdef HAVE_VIS
134#include <vis.h>
135#else
136#include "compat/vis.h"
137#endif
138
139#ifdef HAVE_IMSG
140#include <imsg.h>
141#else
142#include "compat/imsg.h"
143#endif
144
145#ifdef BROKEN_CMSG_FIRSTHDR
146#undef CMSG_FIRSTHDR
147#define CMSG_FIRSTHDR(mhdr) \
148 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
149 (struct cmsghdr *)(mhdr)->msg_control : \
150 (struct cmsghdr *)NULL)
151#endif
152
153#ifndef CMSG_ALIGN
154#ifdef _CMSG_DATA_ALIGN
155#define CMSG_ALIGN _CMSG_DATA_ALIGN
156#else
157#define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
158#endif
159#endif
160
161#ifndef CMSG_SPACE
162#define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
163#endif
164
165#ifndef CMSG_LEN
166#define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
167#endif
168
169#ifndef O_DIRECTORY
170#define O_DIRECTORY 0
171#endif
172
173#ifndef FNM_CASEFOLD
174#ifdef FNM_IGNORECASE
175#define FNM_CASEFOLD FNM_IGNORECASE
176#else
177#define FNM_CASEFOLD 0
178#endif
179#endif
180
181#ifndef INFTIM
182#define INFTIM -1
183#endif
184
185#ifndef WAIT_ANY
186#define WAIT_ANY -1
187#endif
188
189#ifndef SUN_LEN
190#define SUN_LEN(sun) (sizeof (sun)->sun_path)
191#endif
192
193#ifndef timercmp
194#define timercmp(tvp, uvp, cmp) \
195 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
196 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
197 ((tvp)->tv_sec cmp (uvp)->tv_sec))
198#endif
199
200#ifndef timeradd
201#define timeradd(tvp, uvp, vvp) \
202 do { \
203 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
204 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
205 if ((vvp)->tv_usec >= 1000000) { \
206 (vvp)->tv_sec++; \
207 (vvp)->tv_usec -= 1000000; \
208 } \
209 } while (0)
210#endif
211
212#ifndef timersub
213#define timersub(tvp, uvp, vvp) \
214 do { \
215 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
216 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
217 if ((vvp)->tv_usec < 0) { \
218 (vvp)->tv_sec--; \
219 (vvp)->tv_usec += 1000000; \
220 } \
221 } while (0)
222#endif
223
224#ifndef TTY_NAME_MAX
225#define TTY_NAME_MAX 32
226#endif
227
228#ifndef HOST_NAME_MAX
229#define HOST_NAME_MAX 255
230#endif
231
232#ifndef HAVE_FLOCK
233#define LOCK_SH 0
234#define LOCK_EX 0
235#define LOCK_NB 0
236#define flock(fd, op) (0)
237#endif
238
239#ifndef HAVE_EXPLICIT_BZERO
240/* explicit_bzero.c */
241void explicit_bzero(void *, size_t);
242#endif
243
244#ifndef HAVE_GETDTABLECOUNT
245/* getdtablecount.c */
246int getdtablecount(void);
247#endif
248
249#ifndef HAVE_CLOSEFROM
250/* closefrom.c */
251void closefrom(int);
252#endif
253
254#ifndef HAVE_STRCASESTR
255/* strcasestr.c */
256char *strcasestr(const char *, const char *);
257#endif
258
259#ifndef HAVE_STRSEP
260/* strsep.c */
261char *strsep(char **, const char *);
262#endif
263
264#ifndef HAVE_STRTONUM
265/* strtonum.c */
266long long strtonum(const char *, long long, long long, const char **);
267#endif
268
269#ifndef HAVE_STRLCPY
270/* strlcpy.c */
271size_t strlcpy(char *, const char *, size_t);
272#endif
273
274#ifndef HAVE_STRLCAT
275/* strlcat.c */
276size_t strlcat(char *, const char *, size_t);
277#endif
278
279#ifndef HAVE_STRNLEN
280/* strnlen.c */
281size_t strnlen(const char *, size_t);
282#endif
283
284#ifndef HAVE_STRNDUP
285/* strndup.c */
286char *strndup(const char *, size_t);
287#endif
288
289#ifndef HAVE_MEMMEM
290/* memmem.c */
291void *memmem(const void *, size_t, const void *, size_t);
292#endif
293
294#ifndef HAVE_DAEMON
295/* daemon.c */
296int daemon(int, int);
297#endif
298
299#ifndef HAVE_GETPROGNAME
300/* getprogname.c */
301const char *getprogname(void);
302#endif
303
304#ifndef HAVE_SETPROCTITLE
305/* setproctitle.c */
306void setproctitle(const char *, ...);
307#endif
308
309#ifndef HAVE_B64_NTOP
310/* base64.c */
311#undef b64_ntop
312#undef b64_pton
313int b64_ntop(const char *, size_t, char *, size_t);
314int b64_pton(const char *, u_char *, size_t);
315#endif
316
317#ifndef HAVE_FDFORKPTY
318/* fdforkpty.c */
319int getptmfd(void);
320pid_t fdforkpty(int, int *, char *, struct termios *,
321 struct winsize *);
322#endif
323
324#ifndef HAVE_FORKPTY
325/* forkpty.c */
326pid_t forkpty(int *, char *, struct termios *, struct winsize *);
327#endif
328
329#ifndef HAVE_ASPRINTF
330/* asprintf.c */
331int asprintf(char **, const char *, ...);
332int vasprintf(char **, const char *, va_list);
333#endif
334
335#ifndef HAVE_FGETLN
336/* fgetln.c */
337char *fgetln(FILE *, size_t *);
338#endif
339
340#ifndef HAVE_SETENV
341/* setenv.c */
342int setenv(const char *, const char *, int);
343int unsetenv(const char *);
344#endif
345
346#ifndef HAVE_CFMAKERAW
347/* cfmakeraw.c */
348void cfmakeraw(struct termios *);
349#endif
350
351#ifndef HAVE_FREEZERO
352/* freezero.c */
353void freezero(void *, size_t);
354#endif
355
356#ifndef HAVE_REALLOCARRAY
357/* reallocarray.c */
358void *reallocarray(void *, size_t, size_t);
359#endif
360
361#ifndef HAVE_RECALLOCARRAY
362/* recallocarray.c */
363void *recallocarray(void *, size_t, size_t, size_t);
364#endif
365
366#ifdef HAVE_UTF8PROC
367/* utf8proc.c */
368int utf8proc_wcwidth(wchar_t);
369int utf8proc_mbtowc(wchar_t *, const char *, size_t);
370int utf8proc_wctomb(char *, wchar_t);
371#endif
372
373/* getopt.c */
374extern int BSDopterr;
375extern int BSDoptind;
376extern int BSDoptopt;
377extern int BSDoptreset;
378extern char *BSDoptarg;
379int BSDgetopt(int, char *const *, const char *);
380#define getopt(ac, av, o) BSDgetopt(ac, av, o)
381#define opterr BSDopterr
382#define optind BSDoptind
383#define optopt BSDoptopt
384#define optreset BSDoptreset
385#define optarg BSDoptarg
386
387#endif /* COMPAT_H */
388