Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) 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 #ifndef EVBUFFER_EOL_LF
     42 /*
     43  * This doesn't really work because evbuffer_readline is broken, but gets us to
     44  * build with very old (older than 1.4.14) libevent.
     45  */
     46 #define EVBUFFER_EOL_LF
     47 #define evbuffer_readln(a, b, c) evbuffer_readline(a)
     48 #endif
     49 #endif
     50 
     51 #ifdef HAVE_MALLOC_TRIM
     52 #include <malloc.h>
     53 #endif
     54 
     55 #ifdef HAVE_UTF8PROC
     56 #include <utf8proc.h>
     57 #endif
     58 
     59 #ifndef __GNUC__
     60 #define __attribute__(a)
     61 #endif
     62 
     63 #ifdef BROKEN___DEAD
     64 #undef __dead
     65 #endif
     66 
     67 #ifndef __unused
     68 #define __unused __attribute__ ((__unused__))
     69 #endif
     70 #ifndef __dead
     71 #define __dead __attribute__ ((__noreturn__))
     72 #endif
     73 #ifndef __packed
     74 #define __packed __attribute__ ((__packed__))
     75 #endif
     76 #ifndef __weak
     77 #define __weak __attribute__ ((__weak__))
     78 #endif
     79 
     80 #ifndef ECHOPRT
     81 #define ECHOPRT 0
     82 #endif
     83 
     84 #ifndef ACCESSPERMS
     85 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
     86 #endif
     87 
     88 #if !defined(FIONREAD) && defined(__sun)
     89 #include <sys/filio.h>
     90 #endif
     91 
     92 #ifdef HAVE_ERR_H
     93 #include <err.h>
     94 #else
     95 void	err(int, const char *, ...);
     96 void	errx(int, const char *, ...);
     97 void	warn(const char *, ...);
     98 void	warnx(const char *, ...);
     99 #endif
    100 
    101 #ifdef HAVE_PATHS_H
    102 #include <paths.h>
    103 #endif
    104 
    105 #ifndef _PATH_BSHELL
    106 #define _PATH_BSHELL	"/bin/sh"
    107 #endif
    108 
    109 #ifndef _PATH_TMP
    110 #define _PATH_TMP	"/tmp/"
    111 #endif
    112 
    113 #ifndef _PATH_DEVNULL
    114 #define _PATH_DEVNULL	"/dev/null"
    115 #endif
    116 
    117 #ifndef _PATH_TTY
    118 #define _PATH_TTY	"/dev/tty"
    119 #endif
    120 
    121 #ifndef _PATH_DEV
    122 #define _PATH_DEV	"/dev/"
    123 #endif
    124 
    125 #ifndef _PATH_DEFPATH
    126 #define _PATH_DEFPATH	"/usr/bin:/bin"
    127 #endif
    128 
    129 #ifndef _PATH_VI
    130 #define _PATH_VI	"/usr/bin/vi"
    131 #endif
    132 
    133 #ifndef __OpenBSD__
    134 #define pledge(s, p) (0)
    135 #endif
    136 
    137 #ifndef IMAXBEL
    138 #define IMAXBEL 0
    139 #endif
    140 
    141 #ifdef HAVE_STDINT_H
    142 #include <stdint.h>
    143 #else
    144 #include <inttypes.h>
    145 #endif
    146 
    147 #ifdef HAVE_QUEUE_H
    148 #include <sys/queue.h>
    149 #else
    150 #include "compat/queue.h"
    151 #endif
    152 
    153 #ifdef HAVE_TREE_H
    154 #include <sys/tree.h>
    155 #else
    156 #include "compat/tree.h"
    157 #endif
    158 
    159 #ifdef HAVE_BITSTRING_H
    160 #include <bitstring.h>
    161 #else
    162 #include "compat/bitstring.h"
    163 #endif
    164 
    165 #ifdef HAVE_LIBUTIL_H
    166 #include <libutil.h>
    167 #endif
    168 
    169 #ifdef HAVE_PTY_H
    170 #include <pty.h>
    171 #endif
    172 
    173 #ifdef HAVE_UTIL_H
    174 #include <util.h>
    175 #endif
    176 
    177 #ifdef HAVE_VIS
    178 #include <vis.h>
    179 #else
    180 #include "compat/vis.h"
    181 #endif
    182 
    183 #ifdef HAVE_IMSG
    184 #include <imsg.h>
    185 #else
    186 #include "compat/imsg.h"
    187 #endif
    188 
    189 #ifdef BROKEN_CMSG_FIRSTHDR
    190 #undef CMSG_FIRSTHDR
    191 #define CMSG_FIRSTHDR(mhdr) \
    192 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
    193 	    (struct cmsghdr *)(mhdr)->msg_control :	    \
    194 	    (struct cmsghdr *)NULL)
    195 #endif
    196 
    197 #ifndef CMSG_ALIGN
    198 #ifdef _CMSG_DATA_ALIGN
    199 #define CMSG_ALIGN _CMSG_DATA_ALIGN
    200 #else
    201 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
    202 #endif
    203 #endif
    204 
    205 #ifndef CMSG_SPACE
    206 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
    207 #endif
    208 
    209 #ifndef CMSG_LEN
    210 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
    211 #endif
    212 
    213 #ifndef O_DIRECTORY
    214 #define O_DIRECTORY 0
    215 #endif
    216 
    217 #ifndef FNM_CASEFOLD
    218 #ifdef FNM_IGNORECASE
    219 #define FNM_CASEFOLD FNM_IGNORECASE
    220 #else
    221 #define FNM_CASEFOLD 0
    222 #endif
    223 #endif
    224 
    225 #ifndef INFTIM
    226 #define INFTIM -1
    227 #endif
    228 
    229 #ifndef WAIT_ANY
    230 #define WAIT_ANY -1
    231 #endif
    232 
    233 #ifndef SUN_LEN
    234 #define SUN_LEN(sun) (sizeof (sun)->sun_path)
    235 #endif
    236 
    237 #ifndef timercmp
    238 #define	timercmp(tvp, uvp, cmp)						\
    239 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    240 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    241 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    242 #endif
    243 
    244 #ifndef timeradd
    245 #define	timeradd(tvp, uvp, vvp)						\
    246 	do {								\
    247 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    248 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    249 		if ((vvp)->tv_usec >= 1000000) {			\
    250 			(vvp)->tv_sec++;				\
    251 			(vvp)->tv_usec -= 1000000;			\
    252 		}							\
    253 	} while (0)
    254 #endif
    255 
    256 #ifndef timersub
    257 #define timersub(tvp, uvp, vvp)                                         \
    258 	do {                                                            \
    259 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
    260 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
    261 		if ((vvp)->tv_usec < 0) {                               \
    262 			(vvp)->tv_sec--;                                \
    263 			(vvp)->tv_usec += 1000000;                      \
    264 		}                                                       \
    265 	} while (0)
    266 #endif
    267 
    268 #ifndef TTY_NAME_MAX
    269 #define TTY_NAME_MAX 32
    270 #endif
    271 
    272 #ifndef HOST_NAME_MAX
    273 #define HOST_NAME_MAX 255
    274 #endif
    275 
    276 #ifndef CLOCK_REALTIME
    277 #define CLOCK_REALTIME 0
    278 #endif
    279 #ifndef CLOCK_MONOTONIC
    280 #define CLOCK_MONOTONIC CLOCK_REALTIME
    281 #endif
    282 
    283 #ifndef HAVE_FLOCK
    284 #define LOCK_SH 0
    285 #define LOCK_EX 0
    286 #define LOCK_NB 0
    287 #define flock(fd, op) (0)
    288 #endif
    289 
    290 #ifndef HAVE_EXPLICIT_BZERO
    291 /* explicit_bzero.c */
    292 void		 explicit_bzero(void *, size_t);
    293 #endif
    294 
    295 #ifndef HAVE_GETDTABLECOUNT
    296 /* getdtablecount.c */
    297 int		 getdtablecount(void);
    298 #endif
    299 
    300 #ifndef HAVE_GETDTABLESIZE
    301 /* getdtablesize.c */
    302 int		 getdtablesize(void);
    303 #endif
    304 
    305 #ifndef HAVE_CLOSEFROM
    306 /* closefrom.c */
    307 void		 closefrom(int);
    308 #endif
    309 
    310 #ifndef HAVE_STRCASESTR
    311 /* strcasestr.c */
    312 char		*strcasestr(const char *, const char *);
    313 #endif
    314 
    315 #ifndef HAVE_STRSEP
    316 /* strsep.c */
    317 char		*strsep(char **, const char *);
    318 #endif
    319 
    320 #ifndef HAVE_STRTONUM
    321 /* strtonum.c */
    322 long long	 strtonum(const char *, long long, long long, const char **);
    323 #endif
    324 
    325 #ifndef HAVE_STRLCPY
    326 /* strlcpy.c */
    327 size_t	 	 strlcpy(char *, const char *, size_t);
    328 #endif
    329 
    330 #ifndef HAVE_STRLCAT
    331 /* strlcat.c */
    332 size_t	 	 strlcat(char *, const char *, size_t);
    333 #endif
    334 
    335 #ifndef HAVE_STRNLEN
    336 /* strnlen.c */
    337 size_t		 strnlen(const char *, size_t);
    338 #endif
    339 
    340 #ifndef HAVE_STRNDUP
    341 /* strndup.c */
    342 char		*strndup(const char *, size_t);
    343 #endif
    344 
    345 #ifndef HAVE_MEMMEM
    346 /* memmem.c */
    347 void		*memmem(const void *, size_t, const void *, size_t);
    348 #endif
    349 
    350 #ifndef HAVE_HTONLL
    351 /* htonll.c */
    352 #undef htonll
    353 uint64_t	 htonll(uint64_t);
    354 #endif
    355 
    356 #ifndef HAVE_NTOHLL
    357 /* ntohll.c */
    358 #undef ntohll
    359 uint64_t	 ntohll(uint64_t);
    360 #endif
    361 
    362 #ifndef HAVE_GETPEEREID
    363 /* getpeereid.c */
    364 int		getpeereid(int, uid_t *, gid_t *);
    365 #endif
    366 
    367 #ifndef HAVE_DAEMON
    368 /* daemon.c */
    369 int	 	 daemon(int, int);
    370 #endif
    371 
    372 #ifndef HAVE_GETPROGNAME
    373 /* getprogname.c */
    374 const char	*getprogname(void);
    375 #endif
    376 
    377 #ifndef HAVE_SETPROCTITLE
    378 /* setproctitle.c */
    379 void		 setproctitle(const char *, ...);
    380 #endif
    381 
    382 #ifndef HAVE_CLOCK_GETTIME
    383 /* clock_gettime.c */
    384 int		 clock_gettime(int, struct timespec *);
    385 #endif
    386 
    387 #ifndef HAVE_B64_NTOP
    388 /* base64.c */
    389 #undef b64_ntop
    390 #undef b64_pton
    391 int		 b64_ntop(const char *, size_t, char *, size_t);
    392 int		 b64_pton(const char *, u_char *, size_t);
    393 #endif
    394 
    395 #ifndef HAVE_FDFORKPTY
    396 /* fdforkpty.c */
    397 int		 getptmfd(void);
    398 pid_t		 fdforkpty(int, int *, char *, struct termios *,
    399 		     struct winsize *);
    400 #endif
    401 
    402 #ifndef HAVE_FORKPTY
    403 /* forkpty.c */
    404 pid_t		 forkpty(int *, char *, struct termios *, struct winsize *);
    405 #endif
    406 
    407 #ifndef HAVE_ASPRINTF
    408 /* asprintf.c */
    409 int		 asprintf(char **, const char *, ...);
    410 int		 vasprintf(char **, const char *, va_list);
    411 #endif
    412 
    413 #ifndef HAVE_FGETLN
    414 /* fgetln.c */
    415 char		*fgetln(FILE *, size_t *);
    416 #endif
    417 
    418 #ifndef HAVE_GETLINE
    419 /* getline.c */
    420 ssize_t		 getline(char **, size_t *, FILE *);
    421 #endif
    422 
    423 #ifndef HAVE_SETENV
    424 /* setenv.c */
    425 int		 setenv(const char *, const char *, int);
    426 int		 unsetenv(const char *);
    427 #endif
    428 
    429 #ifndef HAVE_CFMAKERAW
    430 /* cfmakeraw.c */
    431 void		 cfmakeraw(struct termios *);
    432 #endif
    433 
    434 #ifndef HAVE_FREEZERO
    435 /* freezero.c */
    436 void		 freezero(void *, size_t);
    437 #endif
    438 
    439 #ifndef HAVE_REALLOCARRAY
    440 /* reallocarray.c */
    441 void		*reallocarray(void *, size_t, size_t);
    442 #endif
    443 
    444 #ifndef HAVE_RECALLOCARRAY
    445 /* recallocarray.c */
    446 void		*recallocarray(void *, size_t, size_t, size_t);
    447 #endif
    448 
    449 #ifdef HAVE_SYSTEMD
    450 /* systemd.c */
    451 int		 systemd_activated(void);
    452 int		 systemd_create_socket(int, char **);
    453 int		 systemd_move_to_new_cgroup(char **);
    454 #endif
    455 
    456 #ifdef HAVE_UTF8PROC
    457 /* utf8proc.c */
    458 int		 utf8proc_wcwidth(wchar_t);
    459 int		 utf8proc_mbtowc(wchar_t *, const char *, size_t);
    460 int		 utf8proc_wctomb(char *, wchar_t);
    461 #endif
    462 
    463 #ifdef NEED_FUZZING
    464 /* tmux.c */
    465 #define main __weak main
    466 #endif
    467 
    468 /* getopt.c */
    469 #ifndef HAVE_BSD_GETOPT
    470 extern int	 BSDopterr;
    471 extern int	 BSDoptind;
    472 extern int	 BSDoptopt;
    473 extern int	 BSDoptreset;
    474 extern char	*BSDoptarg;
    475 int	BSDgetopt(int, char *const *, const char *);
    476 #define getopt(ac, av, o)  BSDgetopt(ac, av, o)
    477 #define opterr             BSDopterr
    478 #define optind             BSDoptind
    479 #define optopt             BSDoptopt
    480 #define optreset           BSDoptreset
    481 #define optarg             BSDoptarg
    482 #endif
    483 
    484 #endif /* COMPAT_H */
    485