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 <limits.h> 25 #include <stdio.h> 26 #include <termios.h> 27 #include <wchar.h> 28 29 #ifndef __GNUC__ 30 #define __attribute__(a) 31 #endif 32 33 #ifndef __unused 34 #define __unused __attribute__ ((__unused__)) 35 #endif 36 #ifndef __dead 37 #define __dead __attribute__ ((__noreturn__)) 38 #endif 39 #ifndef __packed 40 #define __packed __attribute__ ((__packed__)) 41 #endif 42 43 #ifndef ECHOPRT 44 #define ECHOPRT 0 45 #endif 46 47 #ifndef ACCESSPERMS 48 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) 49 #endif 50 51 #if !defined(FIONREAD) && defined(__sun) 52 #include <sys/filio.h> 53 #endif 54 55 #ifdef HAVE_ERR_H 56 #include <err.h> 57 #else 58 void err(int, const char *, ...); 59 void errx(int, const char *, ...); 60 void warn(const char *, ...); 61 void warnx(const char *, ...); 62 #endif 63 64 #ifndef HAVE_PATHS_H 65 #define _PATH_BSHELL "/bin/sh" 66 #define _PATH_TMP "/tmp/" 67 #define _PATH_DEVNULL "/dev/null" 68 #define _PATH_TTY "/dev/tty" 69 #define _PATH_DEV "/dev/" 70 #endif 71 72 #ifndef __OpenBSD__ 73 #define pledge(s, p) (0) 74 #endif 75 76 #ifdef HAVE_STDINT_H 77 #include <stdint.h> 78 #else 79 #include <inttypes.h> 80 #endif 81 82 #ifdef HAVE_QUEUE_H 83 #include <sys/queue.h> 84 #else 85 #include "compat/queue.h" 86 #endif 87 88 #ifdef HAVE_TREE_H 89 #include <sys/tree.h> 90 #else 91 #include "compat/tree.h" 92 #endif 93 94 #ifdef HAVE_BITSTRING_H 95 #include <bitstring.h> 96 #else 97 #include "compat/bitstring.h" 98 #endif 99 100 #ifdef HAVE_PATHS_H 101 #include <paths.h> 102 #endif 103 104 #ifdef HAVE_FORKPTY 105 #ifdef HAVE_LIBUTIL_H 106 #include <libutil.h> 107 #endif 108 #ifdef HAVE_PTY_H 109 #include <pty.h> 110 #endif 111 #ifdef HAVE_UTIL_H 112 #include <util.h> 113 #endif 114 #endif 115 116 #ifdef HAVE_VIS 117 #include <vis.h> 118 #else 119 #include "compat/vis.h" 120 #endif 121 122 #ifdef HAVE_IMSG 123 #include <imsg.h> 124 #else 125 #include "compat/imsg.h" 126 #endif 127 128 #ifdef BROKEN_CMSG_FIRSTHDR 129 #undef CMSG_FIRSTHDR 130 #define CMSG_FIRSTHDR(mhdr) \ 131 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 132 (struct cmsghdr *)(mhdr)->msg_control : \ 133 (struct cmsghdr *)NULL) 134 #endif 135 136 #ifndef CMSG_ALIGN 137 #ifdef _CMSG_DATA_ALIGN 138 #define CMSG_ALIGN _CMSG_DATA_ALIGN 139 #else 140 #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1)) 141 #endif 142 #endif 143 144 #ifndef CMSG_SPACE 145 #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) 146 #endif 147 148 #ifndef CMSG_LEN 149 #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) 150 #endif 151 152 #ifndef O_DIRECTORY 153 #define O_DIRECTORY 0 154 #endif 155 156 #ifndef INFTIM 157 #define INFTIM -1 158 #endif 159 160 #ifndef WAIT_ANY 161 #define WAIT_ANY -1 162 #endif 163 164 #ifndef SUN_LEN 165 #define SUN_LEN(sun) (sizeof (sun)->sun_path) 166 #endif 167 168 #ifndef timercmp 169 #define timercmp(tvp, uvp, cmp) \ 170 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 171 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 172 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 173 #endif 174 175 #ifndef timeradd 176 #define timeradd(tvp, uvp, vvp) \ 177 do { \ 178 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 179 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 180 if ((vvp)->tv_usec >= 1000000) { \ 181 (vvp)->tv_sec++; \ 182 (vvp)->tv_usec -= 1000000; \ 183 } \ 184 } while (0) 185 #endif 186 187 #ifndef timersub 188 #define timersub(tvp, uvp, vvp) \ 189 do { \ 190 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 191 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 192 if ((vvp)->tv_usec < 0) { \ 193 (vvp)->tv_sec--; \ 194 (vvp)->tv_usec += 1000000; \ 195 } \ 196 } while (0) 197 #endif 198 199 #ifndef TTY_NAME_MAX 200 #define TTY_NAME_MAX 32 201 #endif 202 203 #ifndef HOST_NAME_MAX 204 #define HOST_NAME_MAX 255 205 #endif 206 207 #ifndef HAVE_FLOCK 208 #define LOCK_SH 0 209 #define LOCK_EX 0 210 #define LOCK_NB 0 211 #define flock(fd, op) (0) 212 #endif 213 214 #ifndef HAVE_EXPLICIT_BZERO 215 /* explicit_bzero.c */ 216 void explicit_bzero(void *, size_t); 217 #endif 218 219 #ifndef HAVE_GETDTABLECOUNT 220 /* getdtablecount.c */ 221 int getdtablecount(void); 222 #endif 223 224 #ifndef HAVE_CLOSEFROM 225 /* closefrom.c */ 226 void closefrom(int); 227 #endif 228 229 #ifndef HAVE_STRCASESTR 230 /* strcasestr.c */ 231 char *strcasestr(const char *, const char *); 232 #endif 233 234 #ifndef HAVE_STRSEP 235 /* strsep.c */ 236 char *strsep(char **, const char *); 237 #endif 238 239 #ifndef HAVE_STRTONUM 240 /* strtonum.c */ 241 long long strtonum(const char *, long long, long long, const char **); 242 #endif 243 244 #ifndef HAVE_STRLCPY 245 /* strlcpy.c */ 246 size_t strlcpy(char *, const char *, size_t); 247 #endif 248 249 #ifndef HAVE_STRLCAT 250 /* strlcat.c */ 251 size_t strlcat(char *, const char *, size_t); 252 #endif 253 254 #ifndef HAVE_STRNLEN 255 /* strnlen.c */ 256 size_t strnlen(const char *, size_t); 257 #endif 258 259 #ifndef HAVE_STRNDUP 260 /* strndup.c */ 261 char *strndup(const char *, size_t); 262 #endif 263 264 #ifndef HAVE_MEMMEM 265 /* memmem.c */ 266 void *memmem(const void *, size_t, const void *, size_t); 267 #endif 268 269 #ifndef HAVE_DAEMON 270 /* daemon.c */ 271 int daemon(int, int); 272 #endif 273 274 #ifndef HAVE_GETPROGNAME 275 /* getprogname.c */ 276 const char *getprogname(void); 277 #endif 278 279 #ifndef HAVE_SETPROCTITLE 280 /* setproctitle.c */ 281 void setproctitle(const char *, ...); 282 #endif 283 284 #ifndef HAVE_B64_NTOP 285 /* base64.c */ 286 #undef b64_ntop 287 #undef b64_pton 288 int b64_ntop(const char *, size_t, char *, size_t); 289 int b64_pton(const char *, u_char *, size_t); 290 #endif 291 292 #ifndef HAVE_FDFORKPTY 293 /* fdforkpty.c */ 294 int getptmfd(void); 295 pid_t fdforkpty(int, int *, char *, struct termios *, 296 struct winsize *); 297 #endif 298 299 #ifndef HAVE_FORKPTY 300 /* forkpty.c */ 301 pid_t forkpty(int *, char *, struct termios *, struct winsize *); 302 #endif 303 304 #ifndef HAVE_ASPRINTF 305 /* asprintf.c */ 306 int asprintf(char **, const char *, ...); 307 int vasprintf(char **, const char *, va_list); 308 #endif 309 310 #ifndef HAVE_FGETLN 311 /* fgetln.c */ 312 char *fgetln(FILE *, size_t *); 313 #endif 314 315 #ifndef HAVE_FPARSELN 316 char *fparseln(FILE *, size_t *, size_t *, const char *, int); 317 #endif 318 319 #ifndef HAVE_SETENV 320 /* setenv.c */ 321 int setenv(const char *, const char *, int); 322 int unsetenv(const char *); 323 #endif 324 325 #ifndef HAVE_CFMAKERAW 326 /* cfmakeraw.c */ 327 void cfmakeraw(struct termios *); 328 #endif 329 330 #ifndef HAVE_FREEZERO 331 /* freezero.c */ 332 void freezero(void *, size_t); 333 #endif 334 335 #ifndef HAVE_REALLOCARRAY 336 /* reallocarray.c */ 337 void *reallocarray(void *, size_t, size_t); 338 #endif 339 340 #ifndef HAVE_RECALLOCARRAY 341 /* recallocarray.c */ 342 void *recallocarray(void *, size_t, size_t, size_t); 343 #endif 344 345 #ifdef HAVE_UTF8PROC 346 /* utf8proc.c */ 347 int utf8proc_wcwidth(wchar_t); 348 int utf8proc_mbtowc(wchar_t *, const char *, size_t); 349 int utf8proc_wctomb(char *, wchar_t); 350 #endif 351 352 #ifndef HAVE_GETOPT 353 /* getopt.c */ 354 extern int BSDopterr; 355 extern int BSDoptind; 356 extern int BSDoptopt; 357 extern int BSDoptreset; 358 extern char *BSDoptarg; 359 int BSDgetopt(int, char *const *, const char *); 360 #define getopt(ac, av, o) BSDgetopt(ac, av, o) 361 #define opterr BSDopterr 362 #define optind BSDoptind 363 #define optopt BSDoptopt 364 #define optreset BSDoptreset 365 #define optarg BSDoptarg 366 #endif 367 368 #endif /* COMPAT_H */ 369