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