1 /*
2  * This file is part of RGBDS.
3  *
4  * Copyright (c) 2020 RGBDS contributors.
5  *
6  * SPDX-License-Identifier: MIT
7  */
8 
9 /* platform-specific hacks */
10 
11 #ifndef RGBDS_PLATFORM_H
12 #define RGBDS_PLATFORM_H
13 
14 // MSVC doesn't have str(n)casecmp, use a suitable replacement
15 #ifdef _MSC_VER
16 # include <string.h>
17 # define strcasecmp _stricmp
18 # define strncasecmp _strnicmp
19 #else
20 # include <strings.h>
21 #endif
22 
23 /* MSVC has deprecated strdup in favor of _strdup */
24 #ifdef _MSC_VER
25 # define strdup _strdup
26 #endif
27 
28 /* MSVC prefixes the names of S_* macros with underscores,
29    and doesn't define any S_IS* macros. Define them ourselves */
30 #ifdef _MSC_VER
31 # define S_IFMT _S_IFMT
32 # define S_IFDIR _S_IFDIR
33 # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
34 #endif
35 
36 /* MSVC doesn't use POSIX types or defines for `read` */
37 #ifdef _MSC_VER
38 # include <io.h>
39 # define STDIN_FILENO 0
40 # define STDOUT_FILENO 1
41 # define STDERR_FILENO 2
42 # define ssize_t int
43 # define SSIZE_MAX INT_MAX
44 #else
45 # include <fcntl.h>
46 # include <unistd.h>
47 #endif
48 
49 /* MSVC doesn't support `[static N]` for array arguments from C99 */
50 #ifdef _MSC_VER
51 # define MIN_NB_ELMS(N)
52 # define NONNULL(ptr) *ptr
53 #else
54 # define MIN_NB_ELMS(N) static (N)
55 # define NONNULL(ptr) ptr[static 1]
56 #endif
57 
58 // MSVC uses a different name for O_RDWR, and needs an additional _O_BINARY flag
59 #ifdef _MSC_VER
60 # include <fcntl.h>
61 # define O_RDWR _O_RDWR
62 # define S_ISREG(field) ((field) & _S_IFREG)
63 # define O_BINARY _O_BINARY
64 #elif !defined(O_BINARY) // Cross-compilers define O_BINARY
65 # define O_BINARY 0 // POSIX says we shouldn't care!
66 #endif // _MSC_VER
67 
68 // Windows has stdin and stdout open as text by default, which we may not want
69 #if defined(_MSC_VER) || defined(__MINGW32__)
70 # include <io.h>
71 # define setmode(fd, mode) _setmode(fd, mode)
72 #else
73 # define setmode(fd, mode) ((void)0)
74 #endif
75 
76 #endif /* RGBDS_PLATFORM_H */
77