Small improvement to platform detection.
[slunkcrypt.git] / frontend / src / platform.h
blob7999fe82e33cc707fdc0150e1c34e14216ff155f
1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
6 #ifndef INC_PLATFORM_H
7 #define INC_PLATFORM_H
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #ifdef _WIN32
13 # include <share.h>
14 # ifndef _SH_SECURE
15 # define _SH_SECURE 0x80
16 # endif
17 #endif
19 /* Detect operating system type */
20 #if defined(__MINGW32__)
21 # define OS_TYPE_NAME "MinGW32"
22 #elif defined(_WIN32)
23 # define OS_TYPE_NAME "Windows"
24 #elif defined(__CYGWIN__)
25 # define OS_TYPE_NAME "Cygwin"
26 #elif defined(__linux__)
27 # define OS_TYPE_NAME "Linux"
28 #elif defined(__FreeBSD__)
29 # define OS_TYPE_NAME "FreeBSD"
30 #elif defined(__DragonFly__)
31 # define OS_TYPE_NAME "DragonFly"
32 #elif defined(__NetBSD__)
33 # define OS_TYPE_NAME "NetBSD"
34 #elif defined(__OpenBSD__)
35 # define OS_TYPE_NAME "OpenBSD"
36 #elif defined(__sun) && defined(__SVR4)
37 # define OS_TYPE_NAME "Solaris"
38 #elif defined(__QNX__)
39 # define OS_TYPE_NAME "QNX"
40 #elif defined(__APPLE__) && defined(__MACH__)
41 # define OS_TYPE_NAME "macOS"
42 #elif defined(__unix__)
43 # define OS_TYPE_NAME "Unix"
44 #else
45 # error Unknown operating system!
46 #endif
48 /* Detect CPU architecture */
49 #if defined(__x86_64__) || defined(_M_X64)
50 # define CPU_ARCH "x64"
51 #elif defined(__i386__) || defined(_M_IX86)
52 # define CPU_ARCH "x86"
53 #elif defined(__aarch64__) || defined(_M_ARM64)
54 # define CPU_ARCH "arm64"
55 #elif defined(__arm__) || defined(_M_ARM)
56 # define CPU_ARCH "arm"
57 #else
58 # error Unknown CPU architecture!
59 #endif
61 /* Platform-specific quirks */
62 #ifdef _WIN32
63 # define MAIN wmain
64 # define CHR wchar_t
65 # define _T(X) L##X
66 # define GETENV(X) _wgetenv((X))
67 # define STRLEN(X) wcslen((X))
68 # define STRICMP(X,Y) _wcsicmp((X),(Y))
69 # define STRNICMP(X,Y,Z) _wcsnicmp((X),(Y),(Z))
70 # define STRRCHR(X,Y) wcsrchr((X),(Y))
71 # define STRTOUL(X) wcstoul((X), NULL, 0)
72 # define STRDUP(X) _wcsdup((X))
73 # define FPUTS(X,Y) fputws((X),(Y))
74 # define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__)
75 # define REMOVE(X) _wremove((X))
76 # define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE)
77 # define STRERROR(X) _wcserror((X))
78 # ifdef __USE_MINGW_ANSI_STDIO
79 # define PRISTR "ls"
80 # define PRIstr "hs"
81 # define PRIwcs "ls"
82 # else
83 # define PRISTR "s"
84 # define PRIstr "S"
85 # define PRIwcs "s"
86 # endif
87 #else
88 # define MAIN main
89 # define CHR char
90 # define _T(X) X
91 # define GETENV(X) getenv((X))
92 # define STRLEN(X) strlen((X))
93 # define STRICMP(X,Y) strcasecmp((X),(Y))
94 # define STRNICMP(X,Y,Z) strncasecmp((X),(Y),(Z))
95 # define STRRCHR(X,Y) strrchr((X),(Y))
96 # define STRTOUL(X) strtoul((X), NULL, 0)
97 # define STRDUP(X) strdup((X))
98 # define FPUTS(X,Y) fputs((X),(Y))
99 # define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__)
100 # define REMOVE(X) remove((X))
101 # define FOPEN(X,Y) fopen((X),(Y))
102 # define STRERROR(X) strerror((X))
103 # define PRISTR "s"
104 # define PRIstr "s"
105 # define PRIwcs "ls"
106 #endif
108 #define T(X) _T(X)
110 #ifdef _MSC_VER
111 # define strdup(X) _strdup((X))
112 #endif
114 #endif