1 /* Id */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #ifndef COMPAT_H 20 #define COMPAT_H 21 22 #ifndef __GNUC__ 23 #define __attribute__(a) 24 #endif 25 26 #ifndef __dead 27 #define __dead __attribute__ ((__noreturn__)) 28 #endif 29 #ifndef __packed 30 #define __packed __attribute__ ((__packed__)) 31 #endif 32 33 #ifndef ECHOPRT 34 #define ECHOPRT 0 35 #endif 36 37 #ifndef HAVE_BSD_TYPES 38 typedef uint8_t u_int8_t; 39 typedef uint16_t u_int16_t; 40 typedef uint32_t u_int32_t; 41 typedef uint64_t u_int64_t; 42 #endif 43 44 #ifndef HAVE_PATHS_H 45 #define _PATH_BSHELL "/bin/sh" 46 #define _PATH_TMP "/tmp/" 47 #define _PATH_DEVNULL "/dev/null" 48 #define _PATH_TTY "/dev/tty" 49 #define _PATH_DEV "/dev/" 50 #endif 51 52 #ifdef HAVE_QUEUE_H 53 #include <sys/queue.h> 54 #else 55 #include "compat/queue.h" 56 #endif 57 58 #ifdef HAVE_TREE_H 59 #include <sys/tree.h> 60 #else 61 #include "compat/tree.h" 62 #endif 63 64 #ifdef HAVE_BITSTRING_H 65 #include <bitstring.h> 66 #else 67 #include "compat/bitstring.h" 68 #endif 69 70 #ifdef HAVE_PATHS_H 71 #include <paths.h> 72 #endif 73 74 #ifdef HAVE_FORKPTY 75 #ifdef HAVE_LIBUTIL_H 76 #include <libutil.h> 77 #endif 78 #ifdef HAVE_PTY_H 79 #include <pty.h> 80 #endif 81 #ifdef HAVE_UTIL_H 82 #include <util.h> 83 #endif 84 #endif 85 86 #ifdef HAVE_VIS 87 #include <vis.h> 88 #else 89 #include "compat/vis.h" 90 #endif 91 92 #ifdef HAVE_IMSG 93 #include <imsg.h> 94 #else 95 #include "compat/imsg.h" 96 #endif 97 98 #ifdef HAVE_STDINT_H 99 #include <stdint.h> 100 #else 101 #include <inttypes.h> 102 #endif 103 104 #ifdef BROKEN_CMSG_FIRSTHDR 105 #undef CMSG_FIRSTHDR 106 #define CMSG_FIRSTHDR(mhdr) \ 107 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 108 (struct cmsghdr *)(mhdr)->msg_control : \ 109 (struct cmsghdr *)NULL) 110 #endif 111 112 #ifndef CMSG_ALIGN 113 #ifdef _CMSG_DATA_ALIGN 114 #define CMSG_ALIGN _CMSG_DATA_ALIGN 115 #else 116 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1)) 117 #endif 118 #endif 119 120 #ifndef CMSG_SPACE 121 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) 122 #endif 123 124 #ifndef CMSG_LEN 125 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) 126 #endif 127 128 #ifndef O_DIRECTORY 129 #define O_DIRECTORY 0 130 #endif 131 132 #ifndef INFTIM 133 #define INFTIM -1 134 #endif 135 136 #ifndef WAIT_ANY 137 #define WAIT_ANY -1 138 #endif 139 140 #ifndef SUN_LEN 141 #define SUN_LEN(sun) (sizeof (sun)->sun_path) 142 #endif 143 144 #ifndef timercmp 145 #define timercmp(tvp, uvp, cmp) \ 146 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 147 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 148 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 149 #endif 150 151 #ifndef timeradd 152 #define timeradd(tvp, uvp, vvp) \ 153 do { \ 154 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 155 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 156 if ((vvp)->tv_usec >= 1000000) { \ 157 (vvp)->tv_sec++; \ 158 (vvp)->tv_usec -= 1000000; \ 159 } \ 160 } while (0) 161 #endif 162 163 #ifndef timersub 164 #define timersub(tvp, uvp, vvp) \ 165 do { \ 166 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 167 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 168 if ((vvp)->tv_usec < 0) { \ 169 (vvp)->tv_sec--; \ 170 (vvp)->tv_usec += 1000000; \ 171 } \ 172 } while (0) 173 #endif 174 175 #ifndef TTY_NAME_MAX 176 #define TTY_NAME_MAX 32 177 #endif 178 179 #ifndef HAVE_BZERO 180 #undef bzero 181 #define bzero(buf, len) memset(buf, 0, len); 182 #endif 183 184 #ifndef HAVE_CLOSEFROM 185 /* closefrom.c */ 186 void closefrom(int); 187 #endif 188 189 #ifndef HAVE_STRCASESTR 190 /* strcasestr.c */ 191 char *strcasestr(const char *, const char *); 192 #endif 193 194 #ifndef HAVE_STRSEP 195 /* strsep.c */ 196 char *strsep(char **, const char *); 197 #endif 198 199 #ifndef HAVE_STRTONUM 200 /* strtonum.c */ 201 long long strtonum(const char *, long long, long long, const char **); 202 #endif 203 204 #ifndef HAVE_STRLCPY 205 /* strlcpy.c */ 206 size_t strlcpy(char *, const char *, size_t); 207 #endif 208 209 #ifndef HAVE_STRLCAT 210 /* strlcat.c */ 211 size_t strlcat(char *, const char *, size_t); 212 #endif 213 214 #ifndef HAVE_DAEMON 215 /* daemon.c */ 216 int daemon(int, int); 217 #endif 218 219 #ifndef HAVE_B64_NTOP 220 /* b64_ntop.c */ 221 #undef b64_ntop /* for Cygwin */ 222 int b64_ntop(const char *, size_t, char *, size_t); 223 #endif 224 225 #ifndef HAVE_FORKPTY 226 /* forkpty.c */ 227 #include <sys/ioctl.h> 228 pid_t forkpty(int *, char *, struct termios *, struct winsize *); 229 #endif 230 231 #ifndef HAVE_ASPRINTF 232 /* asprintf.c */ 233 int asprintf(char **, const char *, ...); 234 int vasprintf(char **, const char *, va_list); 235 #endif 236 237 #ifndef HAVE_FGETLN 238 /* fgetln.c */ 239 char *fgetln(FILE *, size_t *); 240 #endif 241 242 #ifndef HAVE_SETENV 243 /* setenv.c */ 244 int setenv(const char *, const char *, int); 245 int unsetenv(const char *); 246 #endif 247 248 #ifndef HAVE_CFMAKERAW 249 /* cfmakeraw.c */ 250 void cfmakeraw(struct termios *); 251 #endif 252 253 #ifndef HAVE_OPENAT 254 /* openat.c */ 255 #define AT_FDCWD -100 256 int openat(int, const char *, int, ...); 257 #endif 258 259 #ifdef HAVE_GETOPT 260 #include <getopt.h> 261 #else 262 /* getopt.c */ 263 extern int BSDopterr; 264 extern int BSDoptind; 265 extern int BSDoptopt; 266 extern int BSDoptreset; 267 extern char *BSDoptarg; 268 int BSDgetopt(int, char *const *, const char *); 269 #define getopt(ac, av, o) BSDgetopt(ac, av, o) 270 #define opterr BSDopterr 271 #define optind BSDoptind 272 #define optopt BSDoptopt 273 #define optreset BSDoptreset 274 #define optarg BSDoptarg 275 #endif 276 277 #endif /* COMPAT_H */ 278