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 #ifndef __GNUC__ 21 #define __attribute__(a) 22 #endif 23 24 #ifndef __unused 25 #define __unused __attribute__ ((__unused__)) 26 #endif 27 #ifndef __dead 28 #define __dead __attribute__ ((__noreturn__)) 29 #endif 30 #ifndef __packed 31 #define __packed __attribute__ ((__packed__)) 32 #endif 33 34 #ifndef ECHOPRT 35 #define ECHOPRT 0 36 #endif 37 38 #ifndef HAVE_BSD_TYPES 39 typedef uint8_t u_int8_t; 40 typedef uint16_t u_int16_t; 41 typedef uint32_t u_int32_t; 42 typedef uint64_t u_int64_t; 43 #endif 44 45 #ifndef HAVE_PATHS_H 46 #define _PATH_BSHELL "/bin/sh" 47 #define _PATH_TMP "/tmp/" 48 #define _PATH_DEVNULL "/dev/null" 49 #define _PATH_TTY "/dev/tty" 50 #define _PATH_DEV "/dev/" 51 #endif 52 53 #ifdef HAVE_QUEUE_H 54 #include <sys/queue.h> 55 #else 56 #include "compat/queue.h" 57 #endif 58 59 #ifdef HAVE_TREE_H 60 #include <sys/tree.h> 61 #else 62 #include "compat/tree.h" 63 #endif 64 65 #ifdef HAVE_BITSTRING_H 66 #include <bitstring.h> 67 #else 68 #include "compat/bitstring.h" 69 #endif 70 71 #ifdef HAVE_PATHS_H 72 #include <paths.h> 73 #endif 74 75 #ifdef HAVE_FORKPTY 76 #ifdef HAVE_LIBUTIL_H 77 #include <libutil.h> 78 #endif 79 #ifdef HAVE_PTY_H 80 #include <pty.h> 81 #endif 82 #ifdef HAVE_UTIL_H 83 #include <util.h> 84 #endif 85 #endif 86 87 #ifdef HAVE_VIS 88 #include <vis.h> 89 #else 90 #include "compat/vis.h" 91 #endif 92 93 #ifdef HAVE_IMSG 94 #include <imsg.h> 95 #else 96 #include "compat/imsg.h" 97 #endif 98 99 #ifdef HAVE_STDINT_H 100 #include <stdint.h> 101 #else 102 #include <inttypes.h> 103 #endif 104 105 #ifdef BROKEN_CMSG_FIRSTHDR 106 #undef CMSG_FIRSTHDR 107 #define CMSG_FIRSTHDR(mhdr) \ 108 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 109 (struct cmsghdr *)(mhdr)->msg_control : \ 110 (struct cmsghdr *)NULL) 111 #endif 112 113 #ifndef CMSG_ALIGN 114 #ifdef _CMSG_DATA_ALIGN 115 #define CMSG_ALIGN _CMSG_DATA_ALIGN 116 #else 117 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1)) 118 #endif 119 #endif 120 121 #ifndef CMSG_SPACE 122 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) 123 #endif 124 125 #ifndef CMSG_LEN 126 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) 127 #endif 128 129 #ifndef O_DIRECTORY 130 #define O_DIRECTORY 0 131 #endif 132 133 #ifndef INFTIM 134 #define INFTIM -1 135 #endif 136 137 #ifndef WAIT_ANY 138 #define WAIT_ANY -1 139 #endif 140 141 #ifndef SUN_LEN 142 #define SUN_LEN(sun) (sizeof (sun)->sun_path) 143 #endif 144 145 #ifndef timercmp 146 #define timercmp(tvp, uvp, cmp) \ 147 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 148 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 149 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 150 #endif 151 152 #ifndef timeradd 153 #define timeradd(tvp, uvp, vvp) \ 154 do { \ 155 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 156 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 157 if ((vvp)->tv_usec >= 1000000) { \ 158 (vvp)->tv_sec++; \ 159 (vvp)->tv_usec -= 1000000; \ 160 } \ 161 } while (0) 162 #endif 163 164 #ifndef timersub 165 #define timersub(tvp, uvp, vvp) \ 166 do { \ 167 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 168 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 169 if ((vvp)->tv_usec < 0) { \ 170 (vvp)->tv_sec--; \ 171 (vvp)->tv_usec += 1000000; \ 172 } \ 173 } while (0) 174 #endif 175 176 #ifndef TTY_NAME_MAX 177 #define TTY_NAME_MAX 32 178 #endif 179 180 #ifndef HOST_NAME_MAX 181 #define HOST_NAME_MAX 255 182 #endif 183 184 #ifndef HAVE_FLOCK 185 #define LOCK_SH 0 186 #define LOCK_EX 0 187 #define LOCK_NB 0 188 #define flock(fd, op) (0) 189 #endif 190 191 #ifndef HAVE_CLOSEFROM 192 /* closefrom.c */ 193 void closefrom(int); 194 #endif 195 196 #ifndef HAVE_STRCASESTR 197 /* strcasestr.c */ 198 char *strcasestr(const char *, const char *); 199 #endif 200 201 #ifndef HAVE_STRSEP 202 /* strsep.c */ 203 char *strsep(char **, const char *); 204 #endif 205 206 #ifndef HAVE_STRTONUM 207 /* strtonum.c */ 208 long long strtonum(const char *, long long, long long, const char **); 209 #endif 210 211 #ifndef HAVE_STRLCPY 212 /* strlcpy.c */ 213 size_t strlcpy(char *, const char *, size_t); 214 #endif 215 216 #ifndef HAVE_STRLCAT 217 /* strlcat.c */ 218 size_t strlcat(char *, const char *, size_t); 219 #endif 220 221 #ifndef HAVE_DAEMON 222 /* daemon.c */ 223 int daemon(int, int); 224 #endif 225 226 #ifndef HAVE_B64_NTOP 227 /* b64_ntop.c */ 228 #undef b64_ntop /* for Cygwin */ 229 int b64_ntop(const char *, size_t, char *, size_t); 230 #endif 231 232 #ifndef HAVE_FORKPTY 233 /* forkpty.c */ 234 #include <sys/ioctl.h> 235 pid_t forkpty(int *, char *, struct termios *, struct winsize *); 236 #endif 237 238 #ifndef HAVE_ASPRINTF 239 /* asprintf.c */ 240 int asprintf(char **, const char *, ...); 241 int vasprintf(char **, const char *, va_list); 242 #endif 243 244 #ifndef HAVE_FGETLN 245 /* fgetln.c */ 246 char *fgetln(FILE *, size_t *); 247 #endif 248 249 #ifndef HAVE_FPARSELN 250 char *fparseln(FILE *, size_t *, size_t *, const char *, int); 251 #endif 252 253 #ifndef HAVE_SETENV 254 /* setenv.c */ 255 int setenv(const char *, const char *, int); 256 int unsetenv(const char *); 257 #endif 258 259 #ifndef HAVE_CFMAKERAW 260 /* cfmakeraw.c */ 261 void cfmakeraw(struct termios *); 262 #endif 263 264 #ifndef HAVE_OPENAT 265 /* openat.c */ 266 #define AT_FDCWD -100 267 int openat(int, const char *, int, ...); 268 #endif 269 270 #ifndef HAVE_REALLOCARRAY 271 /* reallocarray.c */ 272 void *reallocarray(void *, size_t, size_t size); 273 #endif 274 275 #ifdef HAVE_GETOPT 276 #include <getopt.h> 277 #else 278 /* getopt.c */ 279 extern int BSDopterr; 280 extern int BSDoptind; 281 extern int BSDoptopt; 282 extern int BSDoptreset; 283 extern char *BSDoptarg; 284 int BSDgetopt(int, char *const *, const char *); 285 #define getopt(ac, av, o) BSDgetopt(ac, av, o) 286 #define opterr BSDopterr 287 #define optind BSDoptind 288 #define optopt BSDoptopt 289 #define optreset BSDoptreset 290 #define optarg BSDoptarg 291 #endif 292 293 #endif /* COMPAT_H */ 294