Pyrogenesis  trunk
posix.h
Go to the documentation of this file.
1 /* Copyright (C) 2020 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * definitions for a subset of POSIX.
25  */
26 
27 /*
28 
29 [KEEP IN SYNC WITH WIKI]
30 
31 this header makes available commonly used POSIX (Portable Operating System
32 Interface) definitions, e.g. thread, file I/O and socket APIs.
33 on Linux and OS X we just include the requisite headers; Win32 doesn't really
34 support POSIX (*), so we have to implement everything ourselves.
35 
36 rationale: this is preferable to a wrapper for several reasons:
37 - less code (implementation is only needed on Win32)
38 - no lock-in (the abstraction may prevent not-designed-for operations
39  that the POSIX interface would have allowed)
40 - familiarity (many coders already know POSIX)
41 
42 if a useful definition is missing, feel free to add it!
43 
44 implementation reference is the "Single Unix Specification v3"
45 (http://www.unix.org/online.html) - it's similar to the POSIX standard
46 (superset?) and freely available.
47 
48 
49 * Win32 does have a POSIX subsystem (mandated by a government contract),
50 but it is crippled. only apps with the PE header 'subsystem' field set to
51 "POSIX" can use the appendant DLL, and then they can't call the regular
52 Windows APIs. this is obviously unacceptable - GDI is needed to set up OpenGL.
53 
54 we therefore need to emulate POSIX functions using the Win32 API.
55 fortunately, many POSIX functions are already implemented in the VC CRT and
56 need only be renamed (e.g. _open, _stat).
57 
58 */
59 
60 #ifndef INCLUDED_POSIX
61 #define INCLUDED_POSIX
62 
63 #if OS_WIN
65 #endif
66 
67 #include "lib/posix/posix_types.h"
68 
69 // disabled to reduce dependencies. include them where needed.
70 //#include "lib/posix/posix_aio.h"
71 //#include "lib/posix/posix_dlfcn.h"
72 //#include "lib/posix/posix_filesystem.h"
73 //#include "lib/posix/posix_mman.h"
74 //#include "lib/posix/posix_time.h"
75 //#include "lib/posix/posix_utsname.h"
76 
77 
78 // note: the following need only be #defined (instead of defining a
79 // trampoline function) because the redefined functions are already
80 // declared by standard headers.
81 
82 // provide C99 *snprintf functions if compiler doesn't already
83 // (MinGW does, VC7.1 doesn't).
84 #if MSC_VERSION
85 # define swprintf _snwprintf
86 # define vsnprintf _vsnprintf
87 # define vswprintf _vsnwprintf
88 #endif
89 
90 // VC doesn't define str[n]casecmp
91 #if MSC_VERSION
92 #define strcasecmp _stricmp
93 #define strncasecmp _strnicmp
94 #define wcscasecmp _wcsicmp
95 #define wcsncasecmp _wcsnicmp
96 #endif
97 
98 #if OS_MACOSX
99 # define EMULATE_WCSDUP 1
100 # define EMULATE_WCSCASECMP 1
101 #else
102 # define EMULATE_WCSDUP 0
103 # define EMULATE_WCSCASECMP 0
104 #endif
105 
106 #if EMULATE_WCSDUP
107 extern wchar_t* wcsdup(const wchar_t* str);
108 #endif
109 
110 #if EMULATE_WCSCASECMP
111 extern int wcscasecmp(const wchar_t* s1, const wchar_t* s2);
112 #endif
113 
114 #endif // #ifndef INCLUDED_POSIX