Nicholas Marriott | f71b305 | 2010-12-31 22:12:33 +0000 | [diff] [blame] | 1 | /* $Id: compat.h,v 1.32 2010-12-31 22:12:33 nicm Exp $ */ |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 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 | */ |
Nicholas Marriott | 58684ea | 2010-10-27 21:34:59 +0000 | [diff] [blame] | 18 | |
| 19 | #ifndef COMPAT_H |
| 20 | #define COMPAT_H |
Nicholas Marriott | 914f858 | 2010-10-27 21:33:15 +0000 | [diff] [blame] | 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 |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 32 | |
Nicholas Marriott | f71b305 | 2010-12-31 22:12:33 +0000 | [diff] [blame] | 33 | #ifndef HAVE_BSD_TYPES |
Nicholas Marriott | 2f51711 | 2009-08-20 12:49:18 +0000 | [diff] [blame] | 34 | typedef uint8_t u_int8_t; |
| 35 | typedef uint16_t u_int16_t; |
| 36 | typedef uint32_t u_int32_t; |
| 37 | typedef uint64_t u_int64_t; |
| 38 | #endif |
| 39 | |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 40 | #ifndef HAVE_PATHS_H |
| 41 | #define _PATH_BSHELL "/bin/sh" |
| 42 | #define _PATH_TMP "/tmp/" |
| 43 | #define _PATH_DEVNULL "/dev/null" |
| 44 | #define _PATH_TTY "/dev/tty" |
Nicholas Marriott | 48c36d6 | 2009-07-21 13:07:50 +0000 | [diff] [blame] | 45 | #define _PATH_DEV "/dev/" |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #ifdef HAVE_QUEUE_H |
Nicholas Marriott | 802e129 | 2009-06-25 17:02:59 +0000 | [diff] [blame] | 49 | #include <sys/queue.h> |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 50 | #else |
| 51 | #include "compat/queue.h" |
| 52 | #endif |
| 53 | |
| 54 | #ifdef HAVE_TREE_H |
Nicholas Marriott | 802e129 | 2009-06-25 17:02:59 +0000 | [diff] [blame] | 55 | #include <sys/tree.h> |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 56 | #else |
| 57 | #include "compat/tree.h" |
| 58 | #endif |
Nicholas Marriott | 9e7a5fa | 2010-05-19 21:49:57 +0000 | [diff] [blame] | 59 | |
Nicholas Marriott | 802e129 | 2009-06-25 17:02:59 +0000 | [diff] [blame] | 60 | #ifdef HAVE_BITSTRING_H |
| 61 | #include <bitstring.h> |
| 62 | #else |
| 63 | #include "compat/bitstring.h" |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | #ifdef HAVE_PATHS_H |
| 67 | #include <paths.h> |
| 68 | #endif |
| 69 | |
| 70 | #ifdef HAVE_FORKPTY |
| 71 | #ifdef HAVE_LIBUTIL_H |
| 72 | #include <libutil.h> |
| 73 | #endif |
| 74 | #ifdef HAVE_PTY_H |
| 75 | #include <pty.h> |
| 76 | #endif |
| 77 | #ifdef HAVE_UTIL_H |
| 78 | #include <util.h> |
| 79 | #endif |
| 80 | #endif |
| 81 | |
Nicholas Marriott | 32312e0 | 2009-06-25 20:27:32 +0000 | [diff] [blame] | 82 | #ifdef HAVE_VIS |
| 83 | #include <vis.h> |
| 84 | #else |
| 85 | #include "compat/vis.h" |
Nicholas Marriott | 09cc530 | 2009-08-20 05:34:58 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
Nicholas Marriott | 914f858 | 2010-10-27 21:33:15 +0000 | [diff] [blame] | 88 | #ifdef HAVE_IMSG |
Tiago Cunha | 60134ce | 2010-06-06 13:00:47 +0000 | [diff] [blame] | 89 | #include <imsg.h> |
Nicholas Marriott | 914f858 | 2010-10-27 21:33:15 +0000 | [diff] [blame] | 90 | #else |
| 91 | #include "compat/imsg.h" |
Tiago Cunha | c274551 | 2009-09-03 20:54:39 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Nicholas Marriott | f54482a | 2010-09-07 19:32:58 +0000 | [diff] [blame] | 94 | #ifdef HAVE_STDINT_H |
| 95 | #include <stdint.h> |
| 96 | #else |
| 97 | #include <inttypes.h> |
| 98 | #endif |
| 99 | |
Nicholas Marriott | f71b305 | 2010-12-31 22:12:33 +0000 | [diff] [blame] | 100 | #ifdef BROKEN_CMSG_FIRSTHDR |
Nicholas Marriott | 2668225 | 2009-09-02 12:30:56 +0000 | [diff] [blame] | 101 | #undef CMSG_FIRSTHDR |
| 102 | #define CMSG_FIRSTHDR(mhdr) \ |
Nicholas Marriott | 9c01a3d | 2010-05-19 21:40:49 +0000 | [diff] [blame] | 103 | ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ |
| 104 | (struct cmsghdr *)(mhdr)->msg_control : \ |
Nicholas Marriott | 2668225 | 2009-09-02 12:30:56 +0000 | [diff] [blame] | 105 | (struct cmsghdr *)NULL) |
Nicholas Marriott | 9c01a3d | 2010-05-19 21:40:49 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Nicholas Marriott | 9c01a3d | 2010-05-19 21:40:49 +0000 | [diff] [blame] | 108 | #ifndef CMSG_ALIGN |
Nicholas Marriott | f71b305 | 2010-12-31 22:12:33 +0000 | [diff] [blame] | 109 | #ifdef _CMSG_DATA_ALIGN |
Nicholas Marriott | 9c01a3d | 2010-05-19 21:40:49 +0000 | [diff] [blame] | 110 | #define CMSG_ALIGN _CMSG_DATA_ALIGN |
| 111 | #else |
| 112 | #define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1)) |
| 113 | #endif |
| 114 | #endif |
| 115 | |
| 116 | #ifndef CMSG_SPACE |
| 117 | #define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) |
| 118 | #endif |
| 119 | |
| 120 | #ifndef CMSG_LEN |
| 121 | #define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) |
Nicholas Marriott | 2668225 | 2009-09-02 12:30:56 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Nicholas Marriott | ec818b8 | 2009-07-01 22:46:13 +0000 | [diff] [blame] | 124 | #ifndef INFTIM |
| 125 | #define INFTIM -1 |
| 126 | #endif |
| 127 | |
| 128 | #ifndef WAIT_ANY |
| 129 | #define WAIT_ANY -1 |
| 130 | #endif |
| 131 | |
| 132 | #ifndef SUN_LEN |
| 133 | #define SUN_LEN(sun) (sizeof (sun)->sun_path) |
Nicholas Marriott | ec818b8 | 2009-07-01 22:46:13 +0000 | [diff] [blame] | 134 | #endif |
| 135 | |
| 136 | #ifndef timercmp |
| 137 | #define timercmp(tvp, uvp, cmp) \ |
| 138 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
| 139 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ |
| 140 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
| 141 | #endif |
| 142 | |
| 143 | #ifndef timeradd |
| 144 | #define timeradd(tvp, uvp, vvp) \ |
| 145 | do { \ |
| 146 | (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ |
| 147 | (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ |
| 148 | if ((vvp)->tv_usec >= 1000000) { \ |
| 149 | (vvp)->tv_sec++; \ |
| 150 | (vvp)->tv_usec -= 1000000; \ |
| 151 | } \ |
| 152 | } while (0) |
| 153 | #endif |
| 154 | |
| 155 | #ifndef TTY_NAME_MAX |
| 156 | #define TTY_NAME_MAX 32 |
Tiago Cunha | c274551 | 2009-09-03 20:54:39 +0000 | [diff] [blame] | 157 | #endif |
| 158 | |
Nicholas Marriott | 698e003 | 2009-08-20 12:25:17 +0000 | [diff] [blame] | 159 | #ifndef HAVE_BZERO |
Nicholas Marriott | a6fc49a | 2010-11-11 20:45:49 +0000 | [diff] [blame] | 160 | #undef bzero |
Nicholas Marriott | 9c01a3d | 2010-05-19 21:40:49 +0000 | [diff] [blame] | 161 | #define bzero(buf, len) memset(buf, 0, len); |
Nicholas Marriott | 698e003 | 2009-08-20 12:25:17 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |
Nicholas Marriott | 2287ec7 | 2010-10-27 20:21:01 +0000 | [diff] [blame] | 164 | #ifndef HAVE_CLOSEFROM |
| 165 | /* closefrom.c */ |
Nicholas Marriott | 2287ec7 | 2010-10-27 20:21:01 +0000 | [diff] [blame] | 166 | void closefrom(int); |
| 167 | #endif |
| 168 | |
Nicholas Marriott | 1f1623e | 2009-07-02 07:31:02 +0000 | [diff] [blame] | 169 | #ifndef HAVE_STRCASESTR |
| 170 | /* strcasestr.c */ |
| 171 | char *strcasestr(const char *, const char *); |
| 172 | #endif |
| 173 | |
Nicholas Marriott | 8f9858b | 2009-08-16 16:15:53 +0000 | [diff] [blame] | 174 | #ifndef HAVE_STRSEP |
| 175 | /* strsep.c */ |
| 176 | char *strsep(char **, const char *); |
| 177 | #endif |
| 178 | |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 179 | #ifndef HAVE_STRTONUM |
| 180 | /* strtonum.c */ |
| 181 | long long strtonum(const char *, long long, long long, const char **); |
| 182 | #endif |
| 183 | |
| 184 | #ifndef HAVE_STRLCPY |
| 185 | /* strlcpy.c */ |
| 186 | size_t strlcpy(char *, const char *, size_t); |
| 187 | #endif |
| 188 | |
| 189 | #ifndef HAVE_STRLCAT |
| 190 | /* strlcat.c */ |
| 191 | size_t strlcat(char *, const char *, size_t); |
| 192 | #endif |
| 193 | |
| 194 | #ifndef HAVE_DAEMON |
| 195 | /* daemon.c */ |
| 196 | int daemon(int, int); |
| 197 | #endif |
| 198 | |
| 199 | #ifndef HAVE_FORKPTY |
| 200 | /* forkpty.c */ |
Nicholas Marriott | a6fc49a | 2010-11-11 20:45:49 +0000 | [diff] [blame] | 201 | #include <sys/ioctl.h> |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 202 | pid_t forkpty(int *, char *, struct termios *, struct winsize *); |
| 203 | #endif |
| 204 | |
| 205 | #ifndef HAVE_ASPRINTF |
| 206 | /* asprintf.c */ |
Nicholas Marriott | 278effd | 2010-05-19 21:31:39 +0000 | [diff] [blame] | 207 | int asprintf(char **, const char *, ...); |
| 208 | int vasprintf(char **, const char *, va_list); |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 209 | #endif |
| 210 | |
| 211 | #ifndef HAVE_FGETLN |
| 212 | /* fgetln.c */ |
Nicholas Marriott | 278effd | 2010-05-19 21:31:39 +0000 | [diff] [blame] | 213 | char *fgetln(FILE *, size_t *); |
| 214 | #endif |
| 215 | |
| 216 | #ifndef HAVE_SETENV |
| 217 | /* setenv.c */ |
Nicholas Marriott | a9c6976 | 2010-06-05 16:29:40 +0000 | [diff] [blame] | 218 | int setenv(const char *, const char *, int); |
| 219 | int unsetenv(const char *); |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 220 | #endif |
| 221 | |
Nicholas Marriott | 58684ea | 2010-10-27 21:34:59 +0000 | [diff] [blame] | 222 | #ifdef HAVE_GETOPT |
| 223 | #include <getopt.h> |
| 224 | #else |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 225 | /* getopt.c */ |
| 226 | extern int BSDopterr; |
| 227 | extern int BSDoptind; |
| 228 | extern int BSDoptopt; |
| 229 | extern int BSDoptreset; |
| 230 | extern char *BSDoptarg; |
| 231 | int BSDgetopt(int, char *const *, const char *); |
| 232 | #define getopt(ac, av, o) BSDgetopt(ac, av, o) |
| 233 | #define opterr BSDopterr |
| 234 | #define optind BSDoptind |
| 235 | #define optopt BSDoptopt |
| 236 | #define optreset BSDoptreset |
| 237 | #define optarg BSDoptarg |
| 238 | #endif |
Nicholas Marriott | 58684ea | 2010-10-27 21:34:59 +0000 | [diff] [blame] | 239 | |
| 240 | #endif /* COMPAT_H */ |