1 #ifndef PORT_H_
2 #define PORT_H_
3 
4 #include "config.h"
5 
6 #ifdef __MINGW32__
7 // Microsoft Windows headers expect this to be set. The MSVC compiler sets
8 // it, but MinGW doesn't.
9 #	if defined(_X86_)
10 #		define _M_IX86
11 #	elif defined(_IA64_)
12 #		define _M_IA64
13 #	elif defined(__amd64__)
14 #		define _M_AMD64
15 #		define _M_X64
16 #	elif defined(__m68k__)
17 #		define _68K_
18 #	elif defined(__ppc__)
19 #		define _M_PPC
20 #	endif
21 #endif
22 
23 
24 // Compilation related
25 #ifdef _MSC_VER
26 #	define inline __inline
27 #elif defined(__SYMBIAN32__)
28 #else
29 #	define inline __inline__
30 #	ifdef __MINGW32__
31 		// For when including Microsoft Windows header files.
32 #		define _inline inline
33 #	endif
34 #endif
35 
36 
37 // Compilation warnings:
38 #ifdef _MSC_VER
39 	// UQM uses a lot of functions that can be used unsafely, but it uses them
40 	// in a safe way. The warnings about these functions however may drown out
41 	// serious warnings, so we turn them off.
42 #	define _CRT_SECURE_NO_DEPRECATE
43 
44 	// Escalate some warnings we consider important
45 	// "'operator' : 'identifier1' indirection to slightly different base
46 	//   types from 'identifier2'
47 #	pragma warning( 3 : 4057 )
48 	// "unreferenced formal parameter"
49 #	pragma warning( 3 : 4100 )
50 	// "'function' : unreferenced local function has been removed"
51 #	pragma warning( 3 : 4505 )
52 	// "local variable 'name' may be used without having been initialized"
53 #	pragma warning( 3 : 4701 )
54 
55 	// Downgrade some warnings we consider unimportant
56 	// "'operator' conversion from 'type1' to 'type2', possible loss of data"
57 #	pragma warning( 4 : 4244)
58 #endif
59 
60 
61 #ifdef _MSC_VER
62 #	include <io.h>
63 #else
64 #	include <unistd.h>
65 #endif
66 
67 // Using "HAVE_STRCASECMP_UQM" instead of "HAVE_STRCASECMP" as the latter
68 // conflicts with SDL.
69 #if !defined(HAVE_STRICMP) && !defined(HAVE_STRCASECMP_UQM)
70 #	error Neither stricmp() nor strcasecmp() is available.
71 #elif !defined(HAVE_STRICMP)
72 #	define stricmp strcasecmp
73 #elif !defined(HAVE_STRCASECMP_UQM)
74 #	define strcasecmp stricmp
75 #else
76 	// We should take care not to define anything if both strcasecmp() and
77 	// stricmp() are defined, as one might exist as a macro to the other.
78 #endif
79 
80 
81 #ifndef HAVE_STRUPR
82 #if defined(__cplusplus)
83 extern "C" {
84 #endif
85 char *strupr (char *str);
86 #if defined(__cplusplus)
87 }
88 #endif
89 #endif
90 
91 #if !defined (_MSC_VER) && !defined (HAVE_READDIR_R)
92 #	include <dirent.h>
93 #if defined(__cplusplus)
94 extern "C" {
95 #endif
96 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
97 #if defined(__cplusplus)
98 }
99 #endif
100 #endif
101 
102 // Directories
103 #ifdef WIN32
104 #	include <stdlib.h>
105 #	define PATH_MAX  _MAX_PATH
106 #	define NAME_MAX  _MAX_FNAME
107 		// _MAX_DIR and FILENAME_MAX could also be candidates.
108 		// If anyone can tell me which one matches NAME_MAX, please
109 		// let me know. - SvdB
110 #elif defined(_WIN32_WCE)
111 #	include <sys/syslimits.h>
112 #else
113 #	include <limits.h>
114 		/* PATH_MAX is per POSIX defined in <limits.h>, but:
115 		 * "A definition of one of the values from Table 2.6 shall bea
116 		 * omitted from <limits.h> on specific implementations where the
117 		 * corresponding value is equal to or greater than the
118 		 * stated minimum, but where the value can vary depending
119 		 * on the file to which it is applied. The actual value supported
120 		 * for a specific pathname shall be provided by the pathconf()
121 		 * function."
122 		 * _POSIX_NAME_MAX will provide a minimum (14).
123 		 * This is relevant (at least) for Solaris.
124 		 */
125 #	ifndef NAME_MAX
126 #		define NAME_MAX _POSIX_NAME_MAX
127 #	endif
128 #endif
129 
130 // Some types
131 #ifdef _MSC_VER
132 typedef int ssize_t;
133 typedef unsigned short mode_t;
134 #endif
135 
136 // Directories
137 #include <sys/stat.h>
138 #ifdef _MSC_VER
139 #	define MKDIR(name, mode) ((void) mode, _mkdir(name))
140 #elif defined(__MINGW32__)
141 #	define MKDIR(name, mode) ((void) mode, mkdir(name))
142 #else
143 #	define MKDIR mkdir
144 #endif
145 #ifdef _MSC_VER
146 #	include <direct.h>
147 #	define chdir _chdir
148 #	define getcwd _getcwd
149 #	define access _access
150 #	define F_OK 0
151 #	define W_OK 2
152 #	define R_OK 4
153 #	define open _open
154 #	define read _read
155 //#	define fstat _fstat
156 #	define write _write
157 //#	define stat _stat
158 #	define unlink _unlink
159 #endif
160 
161 // Memory
162 #ifdef WIN32
163 #	ifdef __MINGW32__
164 #		include <malloc.h>
165 #	elif defined (_MSC_VER)
166 #		define alloca _alloca
167 #	endif
168 #elif defined(__linux__) || defined(__svr4__)
169 #	include <alloca.h>
170 #endif
171 
172 // String formatting
173 #ifdef _MSC_VER
174 #	include <stdarg.h>
175 // Defined in port.c
176 #if defined(__cplusplus)
177 extern "C" {
178 #endif
179 int snprintf(char *str, size_t size, const char *format, ...);
180 int vsnprintf(char *str, size_t size, const char *format, va_list args);
181 #if defined(__cplusplus)
182 }
183 #endif
184 
185 #endif  /* _MSC_VER */
186 
187 #if defined(__cplusplus)
188 extern "C" {
189 #endif
190 
191 // setenv()
192 #ifndef HAVE_SETENV
193 int setenv (const char *name, const char *value, int overwrite);
194 #endif
195 
196 #ifndef HAVE_WCHAR_T
197 typedef unsigned short wchar_t;
198 #endif
199 
200 #ifndef HAVE_WINT_T
201 typedef unsigned int wint_t;
202 #endif
203 
204 #if defined(__cplusplus)
205 }
206 #endif
207 
208 #if defined (_MSC_VER) || defined(__MINGW32__)
209 #	define USE_WINSOCK
210 #endif
211 
212 // errno error numbers. The values used don't matter, as long as they
213 // don't conflict with existing errno error numbers.
214 #ifdef PORT_WANT_ERRNO
215 #	ifdef USE_WINSOCK
216 #		include <errno.h>
217 #		ifndef E2BIG
218 #			define E2BIG            0x01000001
219 #		endif
220 #		ifndef EACCES
221 #			define EACCES           0x01000002
222 #		endif
223 #		ifndef EADDRINUSE
224 #			define EADDRINUSE       0x01000003
225 #		endif
226 #		ifndef EADDRNOTAVAIL
227 #			define EADDRNOTAVAIL    0x01000004
228 #		endif
229 #		ifndef EAFNOSUPPORT
230 #			define EAFNOSUPPORT     0x01000005
231 #		endif
232 #		ifndef EAGAIN
233 #			ifdef EWOULDBLOCK
234 #				define EAGAIN       EWOULDBLOCK
235 #			else
236 #				define EAGAIN       0x01000006
237 #			endif
238 #		endif
239 #		ifndef EALREADY
240 #			define EALREADY         0x01000007
241 #		endif
242 #		ifndef EBADF
243 #			define EBADF            0x01000008
244 #		endif
245 #		ifndef EBADMSG
246 #			define EBADMSG          0x01000009
247 #		endif
248 #		ifndef EBUSY
249 #			define EBUSY            0x0100000a
250 #		endif
251 #		ifndef ECANCELED
252 #			define ECANCELED        0x0100000b
253 #		endif
254 #		ifndef ECHILD
255 #			define ECHILD           0x0100000c
256 #		endif
257 #		ifndef ECONNABORTED
258 #			define ECONNABORTED     0x0100000d
259 #		endif
260 #		ifndef ECONNREFUSED
261 #			define ECONNREFUSED     0x0100000e
262 #		endif
263 #		ifndef ECONNRESET
264 #			define ECONNRESET       0x0100000f
265 #		endif
266 #		ifndef EDEADLK
267 #			define EDEADLK          0x01000010
268 #		endif
269 #		ifndef EDESTADDRREQ
270 #			define EDESTADDRREQ     0x01000011
271 #		endif
272 #		ifndef EDOM
273 #			define EDOM             0x01000012
274 #		endif
275 // Reserved in POSIX
276 //#		ifndef //EDQUOT
277 //#			define //EDQUOT         0x01000013
278 //#		endif
279 #		ifndef EEXIST
280 #			define EEXIST           0x01000014
281 #		endif
282 #		ifndef EFAULT
283 #			define EFAULT           0x01000015
284 #		endif
285 #		ifndef EFBIG
286 #			define EFBIG            0x01000016
287 #		endif
288 #		ifndef EHOSTUNREACH
289 #			define EHOSTUNREACH     0x01000017
290 #		endif
291 #		ifndef EIDRM
292 #			define EIDRM            0x01000018
293 #		endif
294 #		ifndef EILSEQ
295 #			define EILSEQ           0x01000019
296 #		endif
297 #		ifndef EINPROGRESS
298 #			define EINPROGRESS      0x0100001a
299 #		endif
300 #		ifndef EINTR
301 #			define EINTR            0x0100001b
302 #		endif
303 #		ifndef EINVAL
304 #			define EINVAL           0x0100001c
305 #		endif
306 #		ifndef EIO
307 #			define EIO              0x0100001d
308 #		endif
309 #		ifndef EISCONN
310 #			define EISCONN          0x0100001e
311 #		endif
312 #		ifndef EISDIR
313 #			define EISDIR           0x0100001f
314 #		endif
315 #		ifndef ELOOP
316 #			define ELOOP            0x01000020
317 #		endif
318 #		ifndef EMFILE
319 #			define EMFILE           0x01000021
320 #		endif
321 #		ifndef EMLINK
322 #			define EMLINK           0x01000022
323 #		endif
324 #		ifndef EMSGSIZE
325 #			define EMSGSIZE         0x01000023
326 #		endif
327 // Reserved in POSIX
328 //#		ifndef //EMULTIHOP
329 //#			define //EMULTIHOP      0x01000024
330 //#		endif
331 #		ifndef ENAMETOOLONG
332 #			define ENAMETOOLONG     0x01000025
333 #		endif
334 #		ifndef ENETDOWN
335 #			define ENETDOWN         0x01000026
336 #		endif
337 #		ifndef ENETRESET
338 #			define ENETRESET        0x01000027
339 #		endif
340 #		ifndef ENETUNREACH
341 #			define ENETUNREACH      0x01000028
342 #		endif
343 #		ifndef ENFILE
344 #			define ENFILE           0x01000029
345 #		endif
346 #		ifndef ENOBUFS
347 #			define ENOBUFS          0x0100002a
348 #		endif
349 #		ifndef ENODATA
350 #			define ENODATA          0x0100002b
351 #		endif
352 #		ifndef ENODEV
353 #			define ENODEV           0x0100002c
354 #		endif
355 #		ifndef ENOENT
356 #			define ENOENT           0x0100002d
357 #		endif
358 #		ifndef ENOEXEC
359 #			define ENOEXEC          0x0100002e
360 #		endif
361 #		ifndef ENOLCK
362 #			define ENOLCK           0x0100002f
363 #		endif
364 // Reserved in POSIX
365 //#		ifndef ENOLINK
366 //#			define ENOLINK          0x01000030
367 //#		endif
368 #		ifndef ENOMEM
369 #			define ENOMEM           0x01000031
370 #		endif
371 #		ifndef ENOMSG
372 #			define ENOMSG           0x01000032
373 #		endif
374 #		ifndef ENOPROTOOPT
375 #			define ENOPROTOOPT      0x01000033
376 #		endif
377 #		ifndef ENOSPC
378 #			define ENOSPC           0x01000034
379 #		endif
380 #		ifndef ENOSR
381 #			define ENOSR            0x01000035
382 #		endif
383 #		ifndef ENOSTR
384 #			define ENOSTR           0x01000036
385 #		endif
386 #		ifndef ENOSYS
387 #			define ENOSYS           0x01000037
388 #		endif
389 #		ifndef ENOTCONN
390 #			define ENOTCONN         0x01000038
391 #		endif
392 #		ifndef ENOTDIR
393 #			define ENOTDIR          0x01000039
394 #		endif
395 #		ifndef ENOTEMPTY
396 #			define ENOTEMPTY        0x0100003a
397 #		endif
398 #		ifndef ENOTSOCK
399 #			define ENOTSOCK         0x0100003b
400 #		endif
401 #		ifndef ENOTSUP
402 #			define ENOTSUP          0x0100003c
403 #		endif
404 #		ifndef ENOTTY
405 #			define ENOTTY           0x0100003d
406 #		endif
407 #		ifndef ENXIO
408 #			define ENXIO            0x0100003e
409 #		endif
410 #		ifndef EOPNOTSUPP
411 #			define EOPNOTSUPP       0x0100003f
412 #		endif
413 #		ifndef EOVERFLOW
414 #			define EOVERFLOW        0x01000040
415 #		endif
416 #		ifndef EPERM
417 #			define EPERM            0x01000041
418 #		endif
419 #		ifndef EPIPE
420 #			define EPIPE            0x01000042
421 #		endif
422 #		ifndef EPROTO
423 #			define EPROTO           0x01000043
424 #		endif
425 #		ifndef EPROTONOSUPPORT
426 #			define EPROTONOSUPPORT  0x01000044
427 #		endif
428 #		ifndef EPROTOTYPE
429 #			define EPROTOTYPE       0x01000045
430 #		endif
431 #		ifndef ERANGE
432 #			define ERANGE           0x01000046
433 #		endif
434 #		ifndef EROFS
435 #			define EROFS            0x01000047
436 #		endif
437 #		ifndef ESPIPE
438 #			define ESPIPE           0x01000048
439 #		endif
440 #		ifndef ESRCH
441 #			define ESRCH            0x01000049
442 #		endif
443 // Reserved in POSIX
444 //#		ifndef //ESTALE
445 //#			define //ESTALE         0x0100004a
446 //#		endif
447 #		ifndef ETIME
448 #			define ETIME            0x0100004b
449 #		endif
450 #		ifndef ETIMEDOUT
451 #			define ETIMEDOUT        0x0100004c
452 #		endif
453 #		ifndef ETXTBSY
454 #			define ETXTBSY          0x0100004d
455 #		endif
456 #		ifndef EWOULDBLOCK
457 #			ifdef EAGAIN
458 #				define EWOULDBLOCK  EAGAIN
459 #			else
460 #				define EWOULDBLOCK  0x0100004e
461 #			endif
462 #		endif
463 #		ifndef EXDEV
464 #			define EXDEV            0x0100004f
465 #		endif
466 
467 // Non-POSIX:
468 #		ifndef EHOSTDOWN
469 #			define EHOSTDOWN        0x01100001
470 #		endif
471 #		ifndef EPFNOSUPPORT
472 #			define EPFNOSUPPORT     0x01100002
473 #		endif
474 #		ifndef EPROCLIM
475 #			define EPROCLIM         0x01100003
476 #		endif
477 #		ifndef ESHUTDOWN
478 #			define ESHUTDOWN        0x01100004
479 #		endif
480 #		ifndef ESOCKTNOSUPPORT
481 #			define ESOCKTNOSUPPORT  0x01100005
482 #		endif
483 #	elif  defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__DragonFly__)
484 #		ifndef EBADMSG
485 #			define EBADMSG EIO
486 #		endif
487 #	endif  /* defined (__FreeBSD__) || defined (__OpenBSD__) */
488 #endif  /* defined (PORT_WANT_ERRNO) */
489 
490 // Use SDL_INCLUDE to portably include the SDL files from the right location.
491 // The SDL_DIR definition is provided by the build configuration.
492 #define SDL_INCLUDE(file) #file
493 
494 // Mark a function as using printf-style function arguments, so that
495 // extra consistency checks can be made by the compiler.
496 // The first argument to PRINTF_FUNCTION and VPRINTF_FUNCTION is the
497 // index of the format string argument, the second is the index of
498 // the first argument which is specified in the format string (in the
499 // case of PRINTF_FUNCTION) or of the va_list argument (in the case of
500 // VPRINTF_FUNCTION).
501 #ifdef __GNUC__
502 #	define PRINTF_FUNCTION(formatArg, firstArg) \
503 			__attribute__((format(printf, formatArg, firstArg)))
504 #	define VPRINTF_FUNCTION(formatArg) \
505 			__attribute__((format(printf, formatArg, 0)))
506 #else
507 #	define PRINTF_FUNCTION(formatArg, firstArg)
508 #	define VPRINTF_FUNCTION(formatArg)
509 #endif
510 
511 #if defined(__GNUC__)
512 #	define _NORETURN __attribute__((noreturn))
513 #else
514 #	define _NORETURN
515 #endif
516 
517 // Buffered vs. unbuffered logfile
518 // stderr is normally unbuffered when connected to a terminal, but it
519 // will be buffered when connected to a file, when a --logfile argument
520 // is passed to uqm.
521 // Unbuffered output is slower, which can be significant if much debug output
522 // is requested, but after a crash occurs the logfile will still be up to
523 // date.
524 // On platforms where there is no console, having up-to-date log files
525 // after a crash is valuable enough to make the logfile unbuffered by
526 // default there.
527 #if defined(_WIN32_WCE)
528 #	define UNBUFFERED_LOGFILE
529 #endif
530 
531 // Variations in path handling
532 #if defined(WIN32) || defined(__SYMBIAN32__)
533 	// HAVE_DRIVE_LETTERS is defined to signify that DOS/Windows style drive
534 	// letters are to be recognised on this platform.
535 #	define HAVE_DRIVE_LETTERS
536 	// BACKSLASH_IS_PATH_SEPARATOR is defined to signify that the backslash
537 	// character is to be recognised as a path separator on this platform.
538 	// This does not affect the acceptance of forward slashes as path
539 	// separators.
540 #	define BACKSLASH_IS_PATH_SEPARATOR
541 #endif
542 #if defined(WIN32)
543 	// HAVE_UNC_PATHS is defined to signify that Universal Naming Convention
544 	// style paths are to be recognised on this platform.
545 #	define HAVE_UNC_PATHS
546 	// HAVE_CWD_PER_DRIVE is defined to signify that every drive has its own
547 	// current working directory.
548 #	define HAVE_CWD_PER_DRIVE
549 #endif
550 // REJECT_DRIVE_PATH_WITHOUT_SLASH can also be defined, if paths of the form
551 // "d:foo/bar" (without a slash after the drive letter) are to be rejected.
552 
553 #endif  /* PORT_H_ */
554 
555