1 /* $Id: compat.h,v 1.1.1.1 2011/03/10 09:15:38 jmmv Exp $ */ 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 HAVE_U_INT 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 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" 45 #define _PATH_DEV "/dev/" 46 #endif 47 48 #ifdef HAVE_QUEUE_H 49 #include <sys/queue.h> 50 #else 51 #include "compat/queue.h" 52 #endif 53 54 #ifdef HAVE_TREE_H 55 #include <sys/tree.h> 56 #else 57 #include "compat/tree.h" 58 #endif 59 60 #ifdef HAVE_BITSTRING_H 61 #include <bitstring.h> 62 #else 63 #include "compat/bitstring.h" 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 82 #ifdef HAVE_VIS 83 #include <vis.h> 84 #else 85 #include "compat/vis.h" 86 #endif 87 88 #ifdef HAVE_IMSG 89 #include <imsg.h> 90 #else 91 #include "compat/imsg.h" 92 #endif 93 94 #ifdef HAVE_STDINT_H 95 #include <stdint.h> 96 #else 97 #include <inttypes.h> 98 #endif 99 100 #ifdef HAVE_BROKEN_CMSG_FIRSTHDR 101 /* CMSG_FIRSTHDR broken on OS X. */ 102 #undef CMSG_FIRSTHDR 103 #define CMSG_FIRSTHDR(mhdr) \ 104 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 105 (struct cmsghdr *)(mhdr)->msg_control : \ 106 (struct cmsghdr *)NULL) 107 #endif 108 109 /* CMSG_ALIGN, CMSG_SPACE, CMSG_LEN missing from Solaris 9. */ 110 #ifndef CMSG_ALIGN 111 #ifdef __sun 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 INFTIM 127 #define INFTIM -1 128 #endif 129 130 #ifndef WAIT_ANY 131 #define WAIT_ANY -1 132 #endif 133 134 #ifndef SUN_LEN 135 #define SUN_LEN(sun) (sizeof (sun)->sun_path) 136 #endif 137 138 #ifndef timercmp 139 #define timercmp(tvp, uvp, cmp) \ 140 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 141 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 142 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 143 #endif 144 145 #ifndef timeradd 146 #define timeradd(tvp, uvp, vvp) \ 147 do { \ 148 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 149 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 150 if ((vvp)->tv_usec >= 1000000) { \ 151 (vvp)->tv_sec++; \ 152 (vvp)->tv_usec -= 1000000; \ 153 } \ 154 } while (0) 155 #endif 156 157 #ifndef TTY_NAME_MAX 158 #define TTY_NAME_MAX 32 159 #endif 160 161 #ifndef HAVE_BZERO 162 #undef bzero 163 #define bzero(buf, len) memset(buf, 0, len); 164 #endif 165 166 #ifndef HAVE_CLOSEFROM 167 /* closefrom.c */ 168 #define HAVE_FCNTL_H 169 #define HAVE_DIRENT_H 170 #define HAVE_SYSCONF 171 void closefrom(int); 172 #endif 173 174 #ifndef HAVE_STRCASESTR 175 /* strcasestr.c */ 176 char *strcasestr(const char *, const char *); 177 #endif 178 179 #ifndef HAVE_STRSEP 180 /* strsep.c */ 181 char *strsep(char **, const char *); 182 #endif 183 184 #ifndef HAVE_STRTONUM 185 /* strtonum.c */ 186 long long strtonum(const char *, long long, long long, const char **); 187 #endif 188 189 #ifndef HAVE_STRLCPY 190 /* strlcpy.c */ 191 size_t strlcpy(char *, const char *, size_t); 192 #endif 193 194 #ifndef HAVE_STRLCAT 195 /* strlcat.c */ 196 size_t strlcat(char *, const char *, size_t); 197 #endif 198 199 #ifndef HAVE_DAEMON 200 /* daemon.c */ 201 int daemon(int, int); 202 #endif 203 204 #ifndef HAVE_FORKPTY 205 /* forkpty.c */ 206 #include <sys/ioctl.h> 207 pid_t forkpty(int *, char *, struct termios *, struct winsize *); 208 #endif 209 210 #ifndef HAVE_ASPRINTF 211 /* asprintf.c */ 212 int asprintf(char **, const char *, ...); 213 int vasprintf(char **, const char *, va_list); 214 #endif 215 216 #ifndef HAVE_FGETLN 217 /* fgetln.c */ 218 char *fgetln(FILE *, size_t *); 219 #endif 220 221 #ifndef HAVE_SETENV 222 /* setenv.c */ 223 int setenv(const char *, const char *, int); 224 int unsetenv(const char *); 225 #endif 226 227 #ifdef HAVE_GETOPT 228 #include <getopt.h> 229 #else 230 /* getopt.c */ 231 extern int BSDopterr; 232 extern int BSDoptind; 233 extern int BSDoptopt; 234 extern int BSDoptreset; 235 extern char *BSDoptarg; 236 int BSDgetopt(int, char *const *, const char *); 237 #define getopt(ac, av, o) BSDgetopt(ac, av, o) 238 #define opterr BSDopterr 239 #define optind BSDoptind 240 #define optopt BSDoptopt 241 #define optreset BSDoptreset 242 #define optarg BSDoptarg 243 #endif 244 245 #endif /* COMPAT_H */ 246