1 /************************************************************************* 2 * TinyFugue - programmable mud client 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2005, 2006-2007 Ken Keys 4 * 5 * TinyFugue (aka "tf") is protected under the terms of the GNU 6 * General Public License. See the file "COPYING" for details. 7 ************************************************************************/ 8 9 #ifndef PORT_H 10 #define PORT_H 11 12 #ifdef __hpux__ 13 # ifndef _HPUX_SOURCE 14 # define _HPUX_SOURCE /* Enables some "extensions" on HPUX. */ 15 # endif 16 #endif 17 18 #if _AIX - 0 19 # ifndef _ALL_SOURCE 20 # define _ALL_SOURCE /* Enables some "extensions" on AIX. */ 21 # endif 22 /* # define _BSD 44 */ /* Needed for nonblocking connect on AIX. */ 23 #endif 24 25 #if 0 /* These cause a few problems, but little benefit, so forget it. */ 26 /* These aren't neccessary, but may improve optimization, etc. */ 27 # ifdef __GNUC__ 28 # define INLINE __inline__ 29 # if (__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || (__GNUC__ > 2) 30 # define GNUC_2_5_OR_LATER 31 # define PURE __attribute__ ((const)) 32 # define NORET __attribute__ ((noreturn)) 33 # endif 34 # endif 35 #endif 36 37 #ifndef INLINE 38 # define INLINE 39 #endif 40 #ifndef PURE 41 # define PURE 42 #endif 43 #ifndef NORET 44 # define NORET 45 #endif 46 47 #if 0 48 # ifdef __GNUC__ 49 # define format_printf(fmt, var) __attribute__((format(printf, fmt, var))) 50 # else 51 # define format_printf(fmt, var) /* empty */ 52 # endif 53 #else 54 # define format_printf(fmt, var) /* empty */ 55 #endif 56 57 /* Wide character support */ 58 #if WIDECHAR 59 #include <wctype.h> 60 #endif 61 62 /* standard stuff */ 63 64 #include <errno.h> 65 #ifndef errno 66 extern int errno; /* Some systems don't declare errno in errno.h. Duh. */ 67 #endif 68 69 #include <stdio.h> 70 71 #ifndef SEEK_SET 72 # define SEEK_SET 0 73 #endif 74 75 76 #if HAVE_UNISTD_H 77 # include <unistd.h> 78 #endif 79 80 #ifndef STDIN_FILENO 81 # define STDIN_FILENO 0 82 # define STDOUT_FILENO 1 83 # define STDERR_FILENO 2 84 #endif 85 86 #if STDC_HEADERS 87 # include <stdlib.h> 88 # include <string.h> 89 #else 90 extern void free(); 91 # if HAVE_MEMORY_H 92 # include <memory.h> 93 # endif 94 # if !HAVE_STRCHR 95 # if HAVE_INDEX 96 # define strchr index 97 # define strrchr rindex /* assumed */ 98 # endif 99 # endif 100 # if !HAVE_MEMCPY 101 # if HAVE_BCOPY 102 # define memcpy(dst, src, len) bcopy((src), (dst), (len)) 103 # endif 104 # endif 105 #endif 106 107 108 #if !HAVE_BZERO 109 /* We don't use the nonstandard bzero(), but some stupid sys/select.h do */ 110 # define bzero(ptr, size) memset((ptr), '\0', (size)) 111 #endif 112 113 /* Try the common case insensitive strcmp's before falling back to our own */ 114 #if HAVE_STRCASECMP 115 # define cstrcmp strcasecmp 116 #else 117 # if HAVE_STRICMP 118 # define cstrcmp stricmp 119 # else 120 # if HAVE_STRCMPI 121 # define cstrcmp strcmpi 122 # endif 123 # endif 124 #endif 125 #ifndef cstrcmp 126 extern int cstrcmp(const char *s, const char *t); 127 #endif 128 129 #if !HAVE_STRERROR 130 extern int sys_nerr; 131 extern char *sys_errlist[]; 132 # define strerror(n) (((n) > 0 && (n) < sys_nerr) ? sys_errlist[(n)] : \ 133 "unknown error") 134 #endif 135 136 #if !HAVE_FILENO /* occurs on at least one pre-POSIX SVr3-like platform */ 137 # ifdef PLATFORM_UNIX 138 # define fileno(p) ((p)->_file) 139 # else 140 /* Who knows what it should be elsewhere; it should already exist. */ 141 # endif 142 #endif 143 144 #ifdef PLATFORM_OS2 145 # define HAVE_GETCWD 146 # define getcwd _getcwd2 /* handles drive names */ 147 # define chdir _chdir2 /* handles drive names */ 148 #endif 149 150 151 #if !HAVE_LOCALE_H 152 # undef HAVE_SETLOCALE 153 #endif 154 #if !HAVE_SETLOCALE 155 # undef HAVE_LOCALE_H 156 #endif 157 158 159 #include <ctype.h> 160 #if STDC_HEADERS /* are tolower and toupper safe on non-letters, per ANSI? */ 161 /* more efficient */ 162 # define lcase(x) tolower((unsigned char)(x)) 163 # define ucase(x) toupper((unsigned char)(x)) 164 #else 165 /* will work even in nonstandard C */ 166 extern int lcase(int x); 167 extern int ucase(int x); 168 /* This expression evaluates its argument more than once: 169 * (is_upper(x) ? tolower(x) : (x)) 170 * This expression has no sequence points: 171 * (dummy=(x), (is_upper(dummy) ? tolower(dummy) : (dummy))) 172 */ 173 #endif 174 175 /* The standard ctype functions expect an int argument, containing either EOF 176 * or an unsigned char representation of a character. The functions below do 177 * not allow EOF, but do work on plain char values. (The signedness becomes 178 * important when using character sets other than 7-bit ASCII.) 179 */ 180 #if WIDECHAR 181 #define is_alnum(c) iswalnum((wint_t)c) 182 #define is_alpha(c) iswalpha((wint_t)c) 183 #define is_cntrl(c) iswcntrl((wint_t)c) 184 #define is_digit(c) iswdigit((wint_t)c) 185 #define is_graph(c) iswgraph((wint_t)c) 186 #define is_lower(c) iswlower((wint_t)c) 187 #define is_print(c) iswprint((wint_t)c) 188 #define is_punct(c) iswpunct((wint_t)c) 189 #define is_space(c) iswspace((wint_t)c) 190 #define is_upper(c) iswupper((wint_t)c) 191 #define is_xdigit(c) iswxdigit((wint_t)c) 192 #else 193 #define is_alnum(c) isalnum((unsigned char)(c)) 194 #define is_alpha(c) isalpha((unsigned char)(c)) 195 #define is_cntrl(c) iscntrl((unsigned char)(c)) 196 #define is_digit(c) isdigit((unsigned char)(c)) 197 #define is_graph(c) isgraph((unsigned char)(c)) 198 #define is_lower(c) islower((unsigned char)(c)) 199 #define is_print(c) isprint((unsigned char)(c)) 200 #define is_punct(c) ispunct((unsigned char)(c)) 201 #define is_space(c) isspace((unsigned char)(c)) 202 #define is_upper(c) isupper((unsigned char)(c)) 203 #define is_xdigit(c) isxdigit((unsigned char)(c)) 204 #endif 205 206 /* RRAND(lo,hi) returns a random integer in the range [lo,hi]. 207 * RAND() returns a random integer in the range [0,TF_RAND_MAX]. 208 * SRAND() seeds the generator. 209 * If random() exists, use it, because it is better than rand(). 210 * If not, we'll have to use rand(); if RAND_MAX isn't defined, 211 * we'll have to use the modulus method instead of the division method. 212 * Warning: RRAND() is undefined if hi < lo. 213 * Warning: on Solaris 2.x, libucb contains a nonstandard rand() that does 214 * not agree with RAND_MAX. We must not link with -lucb. 215 */ 216 217 #if HAVE_SRANDOM 218 # include <math.h> 219 # define RAND() (int)random() 220 # define SRAND(seed) srandom(seed) 221 # define RRAND(lo,hi) (RAND() % ((hi)-(lo)+1) + (lo)) 222 #else 223 # if HAVE_SRAND 224 # define RAND() rand() 225 # define SRAND(seed) srand(seed) 226 # if RAND_MAX 227 # define RRAND(lo,hi) ((hi)==(lo)) ? (hi) : \ 228 ((RAND() / (RAND_MAX / ((hi)-(lo)+1) + 1)) + (lo)) 229 # else 230 # define RRAND(lo,hi) (RAND() % ((hi)-(lo)+1) + (lo)) 231 # endif 232 # else 233 error "Don't have srand() or srandom()." 234 # endif 235 #endif 236 237 238 #if !HAVE_STRTOD 239 # define NO_FLOAT 240 #endif 241 242 243 #ifndef PATH_MAX 244 # ifdef MAXPATHLEN 245 # define PATH_MAX MAXPATHLEN 246 # else 247 # define PATH_MAX 1024 248 # endif 249 #endif 250 251 252 /* These just prevent warnings during development. They should not be 253 * used in production, since they might conflict with system headers. 254 */ 255 #ifdef TF_IRIX_DECLS 256 extern int kill(pid_t, int); 257 extern int ioctl(int, int, ...); 258 extern long random(void); 259 extern int srandom(unsigned); 260 #endif 261 #ifdef TF_AIX_DECLS 262 extern int strcasecmp(const char *, const char *); 263 extern time_t time(time_t *); 264 /* extern pid_t wait(int *); */ 265 extern int socket(int, int, int); 266 extern int getsockopt(int, int, int, char *, int *); 267 extern int send(int, const char *, int, int); 268 extern int recv(int, char *, int, int); 269 extern int ioctl(int, int, ...); 270 extern long random(void); 271 extern int srandom(unsigned); 272 #endif 273 274 #ifndef TRUE 275 #define TRUE 1 276 #endif 277 #ifndef FALSE 278 #define FALSE 0 279 #endif 280 281 #endif /* PORT_H */ 282