Merge branch 'obsd-master'
[tmux.git] / compat.h
blobcabdf3ad530890fa90185b2fc5b3b77376f6714d
1 /*
2 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
17 #ifndef COMPAT_H
18 #define COMPAT_H
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/uio.h>
24 #include <fnmatch.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <termios.h>
28 #include <wchar.h>
30 #ifdef HAVE_EVENT2_EVENT_H
31 #include <event2/event.h>
32 #include <event2/event_compat.h>
33 #include <event2/event_struct.h>
34 #include <event2/buffer.h>
35 #include <event2/buffer_compat.h>
36 #include <event2/bufferevent.h>
37 #include <event2/bufferevent_struct.h>
38 #include <event2/bufferevent_compat.h>
39 #else
40 #include <event.h>
41 #endif
43 #ifdef HAVE_MALLOC_TRIM
44 #include <malloc.h>
45 #endif
47 #ifdef HAVE_UTF8PROC
48 #include <utf8proc.h>
49 #endif
51 #ifndef __GNUC__
52 #define __attribute__(a)
53 #endif
55 #ifdef BROKEN___DEAD
56 #undef __dead
57 #endif
59 #ifndef __unused
60 #define __unused __attribute__ ((__unused__))
61 #endif
62 #ifndef __dead
63 #define __dead __attribute__ ((__noreturn__))
64 #endif
65 #ifndef __packed
66 #define __packed __attribute__ ((__packed__))
67 #endif
68 #ifndef __weak
69 #define __weak __attribute__ ((__weak__))
70 #endif
72 #ifndef ECHOPRT
73 #define ECHOPRT 0
74 #endif
76 #ifndef ACCESSPERMS
77 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
78 #endif
80 #if !defined(FIONREAD) && defined(__sun)
81 #include <sys/filio.h>
82 #endif
84 #ifdef HAVE_ERR_H
85 #include <err.h>
86 #else
87 void err(int, const char *, ...);
88 void errx(int, const char *, ...);
89 void warn(const char *, ...);
90 void warnx(const char *, ...);
91 #endif
93 #ifdef HAVE_PATHS_H
94 #include <paths.h>
95 #endif
97 #ifndef _PATH_BSHELL
98 #define _PATH_BSHELL "/bin/sh"
99 #endif
101 #ifndef _PATH_TMP
102 #define _PATH_TMP "/tmp/"
103 #endif
105 #ifndef _PATH_DEVNULL
106 #define _PATH_DEVNULL "/dev/null"
107 #endif
109 #ifndef _PATH_TTY
110 #define _PATH_TTY "/dev/tty"
111 #endif
113 #ifndef _PATH_DEV
114 #define _PATH_DEV "/dev/"
115 #endif
117 #ifndef _PATH_DEFPATH
118 #define _PATH_DEFPATH "/usr/bin:/bin"
119 #endif
121 #ifndef _PATH_VI
122 #define _PATH_VI "/usr/bin/vi"
123 #endif
125 #ifndef __OpenBSD__
126 #define pledge(s, p) (0)
127 #endif
129 #ifndef IMAXBEL
130 #define IMAXBEL 0
131 #endif
133 #ifdef HAVE_STDINT_H
134 #include <stdint.h>
135 #else
136 #include <inttypes.h>
137 #endif
139 #ifdef HAVE_QUEUE_H
140 #include <sys/queue.h>
141 #else
142 #include "compat/queue.h"
143 #endif
145 #ifdef HAVE_TREE_H
146 #include <sys/tree.h>
147 #else
148 #include "compat/tree.h"
149 #endif
151 #ifdef HAVE_BITSTRING_H
152 #include <bitstring.h>
153 #else
154 #include "compat/bitstring.h"
155 #endif
157 #ifdef HAVE_LIBUTIL_H
158 #include <libutil.h>
159 #endif
161 #ifdef HAVE_PTY_H
162 #include <pty.h>
163 #endif
165 #ifdef HAVE_UTIL_H
166 #include <util.h>
167 #endif
169 #ifdef HAVE_VIS
170 #include <vis.h>
171 #else
172 #include "compat/vis.h"
173 #endif
175 #ifdef HAVE_IMSG
176 #include <imsg.h>
177 #else
178 #include "compat/imsg.h"
179 #endif
181 #ifdef BROKEN_CMSG_FIRSTHDR
182 #undef CMSG_FIRSTHDR
183 #define CMSG_FIRSTHDR(mhdr) \
184 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
185 (struct cmsghdr *)(mhdr)->msg_control : \
186 (struct cmsghdr *)NULL)
187 #endif
189 #ifndef CMSG_ALIGN
190 #ifdef _CMSG_DATA_ALIGN
191 #define CMSG_ALIGN _CMSG_DATA_ALIGN
192 #else
193 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
194 #endif
195 #endif
197 #ifndef CMSG_SPACE
198 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
199 #endif
201 #ifndef CMSG_LEN
202 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
203 #endif
205 #ifndef O_DIRECTORY
206 #define O_DIRECTORY 0
207 #endif
209 #ifndef FNM_CASEFOLD
210 #ifdef FNM_IGNORECASE
211 #define FNM_CASEFOLD FNM_IGNORECASE
212 #else
213 #define FNM_CASEFOLD 0
214 #endif
215 #endif
217 #ifndef INFTIM
218 #define INFTIM -1
219 #endif
221 #ifndef WAIT_ANY
222 #define WAIT_ANY -1
223 #endif
225 #ifndef SUN_LEN
226 #define SUN_LEN(sun) (sizeof (sun)->sun_path)
227 #endif
229 #ifndef timercmp
230 #define timercmp(tvp, uvp, cmp) \
231 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
232 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
233 ((tvp)->tv_sec cmp (uvp)->tv_sec))
234 #endif
236 #ifndef timeradd
237 #define timeradd(tvp, uvp, vvp) \
238 do { \
239 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
240 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
241 if ((vvp)->tv_usec >= 1000000) { \
242 (vvp)->tv_sec++; \
243 (vvp)->tv_usec -= 1000000; \
245 } while (0)
246 #endif
248 #ifndef timersub
249 #define timersub(tvp, uvp, vvp) \
250 do { \
251 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
252 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
253 if ((vvp)->tv_usec < 0) { \
254 (vvp)->tv_sec--; \
255 (vvp)->tv_usec += 1000000; \
257 } while (0)
258 #endif
260 #ifndef TTY_NAME_MAX
261 #define TTY_NAME_MAX 32
262 #endif
264 #ifndef HOST_NAME_MAX
265 #define HOST_NAME_MAX 255
266 #endif
268 #ifndef CLOCK_REALTIME
269 #define CLOCK_REALTIME 0
270 #endif
271 #ifndef CLOCK_MONOTONIC
272 #define CLOCK_MONOTONIC CLOCK_REALTIME
273 #endif
275 #ifndef HAVE_FLOCK
276 #define LOCK_SH 0
277 #define LOCK_EX 0
278 #define LOCK_NB 0
279 #define flock(fd, op) (0)
280 #endif
282 #ifndef HAVE_EXPLICIT_BZERO
283 /* explicit_bzero.c */
284 void explicit_bzero(void *, size_t);
285 #endif
287 #ifndef HAVE_GETDTABLECOUNT
288 /* getdtablecount.c */
289 int getdtablecount(void);
290 #endif
292 #ifndef HAVE_CLOSEFROM
293 /* closefrom.c */
294 void closefrom(int);
295 #endif
297 #ifndef HAVE_STRCASESTR
298 /* strcasestr.c */
299 char *strcasestr(const char *, const char *);
300 #endif
302 #ifndef HAVE_STRSEP
303 /* strsep.c */
304 char *strsep(char **, const char *);
305 #endif
307 #ifndef HAVE_STRTONUM
308 /* strtonum.c */
309 long long strtonum(const char *, long long, long long, const char **);
310 #endif
312 #ifndef HAVE_STRLCPY
313 /* strlcpy.c */
314 size_t strlcpy(char *, const char *, size_t);
315 #endif
317 #ifndef HAVE_STRLCAT
318 /* strlcat.c */
319 size_t strlcat(char *, const char *, size_t);
320 #endif
322 #ifndef HAVE_STRNLEN
323 /* strnlen.c */
324 size_t strnlen(const char *, size_t);
325 #endif
327 #ifndef HAVE_STRNDUP
328 /* strndup.c */
329 char *strndup(const char *, size_t);
330 #endif
332 #ifndef HAVE_MEMMEM
333 /* memmem.c */
334 void *memmem(const void *, size_t, const void *, size_t);
335 #endif
337 #ifndef HAVE_GETPEEREID
338 /* getpeereid.c */
339 int getpeereid(int, uid_t *, gid_t *);
340 #endif
342 #ifndef HAVE_DAEMON
343 /* daemon.c */
344 int daemon(int, int);
345 #endif
347 #ifndef HAVE_GETPROGNAME
348 /* getprogname.c */
349 const char *getprogname(void);
350 #endif
352 #ifndef HAVE_SETPROCTITLE
353 /* setproctitle.c */
354 void setproctitle(const char *, ...);
355 #endif
357 #ifndef HAVE_CLOCK_GETTIME
358 /* clock_gettime.c */
359 int clock_gettime(int, struct timespec *);
360 #endif
362 #ifndef HAVE_B64_NTOP
363 /* base64.c */
364 #undef b64_ntop
365 #undef b64_pton
366 int b64_ntop(const char *, size_t, char *, size_t);
367 int b64_pton(const char *, u_char *, size_t);
368 #endif
370 #ifndef HAVE_FDFORKPTY
371 /* fdforkpty.c */
372 int getptmfd(void);
373 pid_t fdforkpty(int, int *, char *, struct termios *,
374 struct winsize *);
375 #endif
377 #ifndef HAVE_FORKPTY
378 /* forkpty.c */
379 pid_t forkpty(int *, char *, struct termios *, struct winsize *);
380 #endif
382 #ifndef HAVE_ASPRINTF
383 /* asprintf.c */
384 int asprintf(char **, const char *, ...);
385 int vasprintf(char **, const char *, va_list);
386 #endif
388 #ifndef HAVE_FGETLN
389 /* fgetln.c */
390 char *fgetln(FILE *, size_t *);
391 #endif
393 #ifndef HAVE_GETLINE
394 /* getline.c */
395 ssize_t getline(char **, size_t *, FILE *);
396 #endif
398 #ifndef HAVE_SETENV
399 /* setenv.c */
400 int setenv(const char *, const char *, int);
401 int unsetenv(const char *);
402 #endif
404 #ifndef HAVE_CFMAKERAW
405 /* cfmakeraw.c */
406 void cfmakeraw(struct termios *);
407 #endif
409 #ifndef HAVE_FREEZERO
410 /* freezero.c */
411 void freezero(void *, size_t);
412 #endif
414 #ifndef HAVE_REALLOCARRAY
415 /* reallocarray.c */
416 void *reallocarray(void *, size_t, size_t);
417 #endif
419 #ifndef HAVE_RECALLOCARRAY
420 /* recallocarray.c */
421 void *recallocarray(void *, size_t, size_t, size_t);
422 #endif
424 #ifdef HAVE_SYSTEMD
425 /* systemd.c */
426 int systemd_activated(void);
427 int systemd_create_socket(int, char **);
428 int systemd_move_pid_to_new_cgroup(pid_t, char **);
429 #endif
431 #ifdef HAVE_UTF8PROC
432 /* utf8proc.c */
433 int utf8proc_wcwidth(wchar_t);
434 int utf8proc_mbtowc(wchar_t *, const char *, size_t);
435 int utf8proc_wctomb(char *, wchar_t);
436 #endif
438 #ifdef NEED_FUZZING
439 /* tmux.c */
440 #define main __weak main
441 #endif
443 /* getopt.c */
444 extern int BSDopterr;
445 extern int BSDoptind;
446 extern int BSDoptopt;
447 extern int BSDoptreset;
448 extern char *BSDoptarg;
449 int BSDgetopt(int, char *const *, const char *);
450 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
451 #define opterr BSDopterr
452 #define optind BSDoptind
453 #define optopt BSDoptopt
454 #define optreset BSDoptreset
455 #define optarg BSDoptarg
457 #endif /* COMPAT_H */