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