1 /* 2 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.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 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 #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 42 43 #ifdef HAVE_MALLOC_TRIM 44 #include <malloc.h> 45 #endif 46 47 #ifdef HAVE_UTF8PROC 48 #include <utf8proc.h> 49 #endif 50 51 #ifndef __GNUC__ 52 #define __attribute__(a) 53 #endif 54 55 #ifdef BROKEN___DEAD 56 #undef __dead 57 #endif 58 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 71 72 #ifndef ECHOPRT 73 #define ECHOPRT 0 74 #endif 75 76 #ifndef ACCESSPERMS 77 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) 78 #endif 79 80 #if !defined(FIONREAD) && defined(__sun) 81 #include <sys/filio.h> 82 #endif 83 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 92 93 #ifdef HAVE_PATHS_H 94 #include <paths.h> 95 #endif 96 97 #ifndef _PATH_BSHELL 98 #define _PATH_BSHELL "/bin/sh" 99 #endif 100 101 #ifndef _PATH_TMP 102 #define _PATH_TMP "/tmp/" 103 #endif 104 105 #ifndef _PATH_DEVNULL 106 #define _PATH_DEVNULL "/dev/null" 107 #endif 108 109 #ifndef _PATH_TTY 110 #define _PATH_TTY "/dev/tty" 111 #endif 112 113 #ifndef _PATH_DEV 114 #define _PATH_DEV "/dev/" 115 #endif 116 117 #ifndef _PATH_DEFPATH 118 #define _PATH_DEFPATH "/usr/bin:/bin" 119 #endif 120 121 #ifndef _PATH_VI 122 #define _PATH_VI "/usr/bin/vi" 123 #endif 124 125 #ifndef __OpenBSD__ 126 #define pledge(s, p) (0) 127 #endif 128 129 #ifndef IMAXBEL 130 #define IMAXBEL 0 131 #endif 132 133 #ifdef HAVE_STDINT_H 134 #include <stdint.h> 135 #else 136 #include <inttypes.h> 137 #endif 138 139 #ifdef HAVE_QUEUE_H 140 #include <sys/queue.h> 141 #else 142 #include "compat/queue.h" 143 #endif 144 145 #ifdef HAVE_TREE_H 146 #include <sys/tree.h> 147 #else 148 #include "compat/tree.h" 149 #endif 150 151 #ifdef HAVE_BITSTRING_H 152 #include <bitstring.h> 153 #else 154 #include "compat/bitstring.h" 155 #endif 156 157 #ifdef HAVE_LIBUTIL_H 158 #include <libutil.h> 159 #endif 160 161 #ifdef HAVE_PTY_H 162 #include <pty.h> 163 #endif 164 165 #ifdef HAVE_UTIL_H 166 #include <util.h> 167 #endif 168 169 #ifdef HAVE_VIS 170 #include <vis.h> 171 #else 172 #include "compat/vis.h" 173 #endif 174 175 #ifdef HAVE_IMSG 176 #include <imsg.h> 177 #else 178 #include "compat/imsg.h" 179 #endif 180 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 188 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 196 197 #ifndef CMSG_SPACE 198 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) 199 #endif 200 201 #ifndef CMSG_LEN 202 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) 203 #endif 204 205 #ifndef O_DIRECTORY 206 #define O_DIRECTORY 0 207 #endif 208 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 216 217 #ifndef INFTIM 218 #define INFTIM -1 219 #endif 220 221 #ifndef WAIT_ANY 222 #define WAIT_ANY -1 223 #endif 224 225 #ifndef SUN_LEN 226 #define SUN_LEN(sun) (sizeof (sun)->sun_path) 227 #endif 228 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 235 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; \ 244 } \ 245 } while (0) 246 #endif 247 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; \ 256 } \ 257 } while (0) 258 #endif 259 260 #ifndef TTY_NAME_MAX 261 #define TTY_NAME_MAX 32 262 #endif 263 264 #ifndef HOST_NAME_MAX 265 #define HOST_NAME_MAX 255 266 #endif 267 268 #ifndef CLOCK_REALTIME 269 #define CLOCK_REALTIME 0 270 #endif 271 #ifndef CLOCK_MONOTONIC 272 #define CLOCK_MONOTONIC CLOCK_REALTIME 273 #endif 274 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 281 282 #ifndef HAVE_EXPLICIT_BZERO 283 /* explicit_bzero.c */ 284 void explicit_bzero(void *, size_t); 285 #endif 286 287 #ifndef HAVE_GETDTABLECOUNT 288 /* getdtablecount.c */ 289 int getdtablecount(void); 290 #endif 291 292 #ifndef HAVE_GETDTABLESIZE 293 /* getdtablesize.c */ 294 int getdtablesize(void); 295 #endif 296 297 #ifndef HAVE_CLOSEFROM 298 /* closefrom.c */ 299 void closefrom(int); 300 #endif 301 302 #ifndef HAVE_STRCASESTR 303 /* strcasestr.c */ 304 char *strcasestr(const char *, const char *); 305 #endif 306 307 #ifndef HAVE_STRSEP 308 /* strsep.c */ 309 char *strsep(char **, const char *); 310 #endif 311 312 #ifndef HAVE_STRTONUM 313 /* strtonum.c */ 314 long long strtonum(const char *, long long, long long, const char **); 315 #endif 316 317 #ifndef HAVE_STRLCPY 318 /* strlcpy.c */ 319 size_t strlcpy(char *, const char *, size_t); 320 #endif 321 322 #ifndef HAVE_STRLCAT 323 /* strlcat.c */ 324 size_t strlcat(char *, const char *, size_t); 325 #endif 326 327 #ifndef HAVE_STRNLEN 328 /* strnlen.c */ 329 size_t strnlen(const char *, size_t); 330 #endif 331 332 #ifndef HAVE_STRNDUP 333 /* strndup.c */ 334 char *strndup(const char *, size_t); 335 #endif 336 337 #ifndef HAVE_MEMMEM 338 /* memmem.c */ 339 void *memmem(const void *, size_t, const void *, size_t); 340 #endif 341 342 #ifndef HAVE_HTONLL 343 /* htonll.c */ 344 #undef htonll 345 uint64_t htonll(uint64_t); 346 #endif 347 348 #ifndef HAVE_NTOHLL 349 /* ntohll.c */ 350 #undef ntohll 351 uint64_t ntohll(uint64_t); 352 #endif 353 354 #ifndef HAVE_GETPEEREID 355 /* getpeereid.c */ 356 int getpeereid(int, uid_t *, gid_t *); 357 #endif 358 359 #ifndef HAVE_DAEMON 360 /* daemon.c */ 361 int daemon(int, int); 362 #endif 363 364 #ifndef HAVE_GETPROGNAME 365 /* getprogname.c */ 366 const char *getprogname(void); 367 #endif 368 369 #ifndef HAVE_SETPROCTITLE 370 /* setproctitle.c */ 371 void setproctitle(const char *, ...); 372 #endif 373 374 #ifndef HAVE_CLOCK_GETTIME 375 /* clock_gettime.c */ 376 int clock_gettime(int, struct timespec *); 377 #endif 378 379 #ifndef HAVE_B64_NTOP 380 /* base64.c */ 381 #undef b64_ntop 382 #undef b64_pton 383 int b64_ntop(const char *, size_t, char *, size_t); 384 int b64_pton(const char *, u_char *, size_t); 385 #endif 386 387 #ifndef HAVE_FDFORKPTY 388 /* fdforkpty.c */ 389 int getptmfd(void); 390 pid_t fdforkpty(int, int *, char *, struct termios *, 391 struct winsize *); 392 #endif 393 394 #ifndef HAVE_FORKPTY 395 /* forkpty.c */ 396 pid_t forkpty(int *, char *, struct termios *, struct winsize *); 397 #endif 398 399 #ifndef HAVE_ASPRINTF 400 /* asprintf.c */ 401 int asprintf(char **, const char *, ...); 402 int vasprintf(char **, const char *, va_list); 403 #endif 404 405 #ifndef HAVE_FGETLN 406 /* fgetln.c */ 407 char *fgetln(FILE *, size_t *); 408 #endif 409 410 #ifndef HAVE_GETLINE 411 /* getline.c */ 412 ssize_t getline(char **, size_t *, FILE *); 413 #endif 414 415 #ifndef HAVE_SETENV 416 /* setenv.c */ 417 int setenv(const char *, const char *, int); 418 int unsetenv(const char *); 419 #endif 420 421 #ifndef HAVE_CFMAKERAW 422 /* cfmakeraw.c */ 423 void cfmakeraw(struct termios *); 424 #endif 425 426 #ifndef HAVE_FREEZERO 427 /* freezero.c */ 428 void freezero(void *, size_t); 429 #endif 430 431 #ifndef HAVE_REALLOCARRAY 432 /* reallocarray.c */ 433 void *reallocarray(void *, size_t, size_t); 434 #endif 435 436 #ifndef HAVE_RECALLOCARRAY 437 /* recallocarray.c */ 438 void *recallocarray(void *, size_t, size_t, size_t); 439 #endif 440 441 #ifdef HAVE_SYSTEMD 442 /* systemd.c */ 443 int systemd_activated(void); 444 int systemd_create_socket(int, char **); 445 int systemd_move_pid_to_new_cgroup(pid_t, char **); 446 #endif 447 448 #ifdef HAVE_UTF8PROC 449 /* utf8proc.c */ 450 int utf8proc_wcwidth(wchar_t); 451 int utf8proc_mbtowc(wchar_t *, const char *, size_t); 452 int utf8proc_wctomb(char *, wchar_t); 453 #endif 454 455 #ifdef NEED_FUZZING 456 /* tmux.c */ 457 #define main __weak main 458 #endif 459 460 /* getopt.c */ 461 #ifndef HAVE_BSD_GETOPT 462 extern int BSDopterr; 463 extern int BSDoptind; 464 extern int BSDoptopt; 465 extern int BSDoptreset; 466 extern char *BSDoptarg; 467 int BSDgetopt(int, char *const *, const char *); 468 #define getopt(ac, av, o) BSDgetopt(ac, av, o) 469 #define opterr BSDopterr 470 #define optind BSDoptind 471 #define optopt BSDoptopt 472 #define optreset BSDoptreset 473 #define optarg BSDoptarg 474 #endif 475 476 #endif /* COMPAT_H */ 477