anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

windows.cpp (6739B)

      1 /* POSIX emulation layer for Windows.
      2  *
      3  * (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
      4  * (C) 2008-2022 Anope Team <team@anope.org>
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #ifdef _WIN32
     13 #include "services.h"
     14 #include "anope.h"
     15 
     16 #include <io.h>
     17 #include <sys/stat.h>
     18 #include <sys/types.h>
     19 #include <fcntl.h>
     20 
     21 static struct WindowsLanguage
     22 {
     23 	Anope::string languageName;
     24 	USHORT windowsLanguageName;
     25 } WindowsLanguages[] = {
     26 	{"ca_ES", LANG_CATALAN},
     27 	{"de_DE", LANG_GERMAN},
     28 	{"el_GR", LANG_GREEK},
     29 	{"en_US", LANG_ENGLISH},
     30 	{"es_ES", LANG_SPANISH},
     31 	{"fr_FR", LANG_FRENCH},
     32 	{"hu_HU", LANG_HUNGARIAN},
     33 	{"it_IT", LANG_ITALIAN},
     34 	{"nl_NL", LANG_DUTCH},
     35 	{"pl_PL", LANG_POLISH},
     36 	{"pt_PT", LANG_PORTUGUESE},
     37 	{"ru_RU", LANG_RUSSIAN},
     38 	{"tr_TR", LANG_TURKISH},
     39 };
     40 
     41 static WSADATA wsa;
     42 
     43 void OnStartup()
     44 {
     45 	if (WSAStartup(MAKEWORD(2, 0), &wsa))
     46 		throw CoreException("Failed to initialize WinSock library");
     47 }
     48 
     49 void OnShutdown()
     50 {
     51 	WSACleanup();
     52 }
     53 
     54 USHORT WindowsGetLanguage(const Anope::string &lang)
     55 {
     56 	for (int i = 0; i < sizeof(WindowsLanguages) / sizeof(WindowsLanguage); ++i)
     57 	{
     58 		WindowsLanguage &l = WindowsLanguages[i];
     59 
     60 		if (lang == l.languageName || !lang.find(l.languageName + "."))
     61 			return l.windowsLanguageName;
     62 	}
     63 
     64 	return LANG_NEUTRAL;
     65 }
     66 
     67 /** Like gettimeofday(), but it works on Windows.
     68  * @param tv A timeval struct
     69  * @param tz Should be NULL, it is not used
     70  * @return 0 on success
     71  */
     72 int gettimeofday(timeval *tv, void *)
     73 {
     74 	SYSTEMTIME st;
     75 	GetSystemTime(&st);
     76 
     77 	tv->tv_sec = Anope::CurTime;
     78 	tv->tv_usec = st.wMilliseconds;
     79 
     80 	return 0;
     81 }
     82 
     83 Anope::string GetWindowsVersion()
     84 {
     85 	OSVERSIONINFOEX osvi;
     86 	SYSTEM_INFO si;
     87 
     88 	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
     89 	ZeroMemory(&si, sizeof(SYSTEM_INFO));
     90 	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
     91 
     92 	BOOL bOsVersionInfoEx = GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi));
     93 	if (!bOsVersionInfoEx)
     94 	{
     95 		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
     96 		if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi)))
     97 			return "";
     98 	}
     99 	GetSystemInfo(&si);
    100 
    101 	Anope::string buf, extra, cputype;
    102 	/* Determine CPU type 32 or 64 */
    103 	if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
    104 		cputype = " 64-bit";
    105 	else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
    106 		cputype = " 32-bit";
    107 	else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
    108 		cputype = " Itanium 64-bit";
    109 
    110 	switch (osvi.dwPlatformId)
    111 	{
    112 		/* test for the Windows NT product family. */
    113 		case VER_PLATFORM_WIN32_NT:
    114 			/* Windows Vista or Windows Server 2008 */
    115 			if (osvi.dwMajorVersion == 6 && !osvi.dwMinorVersion)
    116 			{
    117 				if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
    118 					extra = " Enterprise Edition";
    119 				else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
    120 					extra = " Datacenter Edition";
    121 				else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
    122 					extra = " Home Premium/Basic";
    123 				if (osvi.dwMinorVersion == 0)
    124 				{
    125 					if (osvi.wProductType & VER_NT_WORKSTATION)
    126 						buf = "Microsoft Windows Vista" + cputype + extra;
    127 					else
    128 						buf = "Microsoft Windows Server 2008" + cputype + extra;
    129 				}
    130 				else if (osvi.dwMinorVersion == 1)
    131 				{
    132 					if (osvi.wProductType & VER_NT_WORKSTATION)
    133 						buf = "Microsoft Windows 7" + cputype + extra;
    134 					else
    135 						buf = "Microsoft Windows Server 2008 R2" + cputype + extra;
    136 				}
    137 			}
    138 			/* Windows 2003 or Windows XP Pro 64 */
    139 			if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
    140 			{
    141 				if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
    142 					extra = " Datacenter Edition";
    143 				else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
    144 					extra = " Enterprise Edition";
    145 #ifdef VER_SUITE_COMPUTE_SERVER
    146 				else if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
    147 					extra = " Compute Cluster Edition";
    148 #endif
    149 				else if (osvi.wSuiteMask == VER_SUITE_BLADE)
    150 					extra = " Web Edition";
    151 				else
    152 					extra = " Standard Edition";
    153 				if (osvi.wProductType & VER_NT_WORKSTATION && si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
    154 					buf = "Microsoft Windows XP Professional x64 Edition" + extra;
    155 				else
    156 					buf = "Microsoft Windows Server 2003 Family" + cputype + extra;
    157 			}
    158 			if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
    159 			{
    160 				if (osvi.wSuiteMask & VER_SUITE_EMBEDDEDNT)
    161 					extra = " Embedded";
    162 				else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
    163 					extra = " Home Edition";
    164 				buf = "Microsoft Windows XP" + extra;
    165 			}
    166 			if (osvi.dwMajorVersion == 5 && !osvi.dwMinorVersion)
    167 			{
    168 				if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
    169 					extra = " Datacenter Server";
    170 				else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
    171 					extra = " Advanced Server";
    172 				else
    173 					extra = " Server";
    174 				buf = "Microsoft Windows 2000" + extra;
    175 			}
    176 			if (osvi.dwMajorVersion <= 4)
    177 			{
    178 				if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
    179 					extra = " Enterprise Edition";
    180 				buf = "Microsoft Windows NT Server 4.0" + extra;
    181 			}
    182 			break;
    183 		case VER_PLATFORM_WIN32_WINDOWS:
    184 			if (osvi.dwMajorVersion == 4 && !osvi.dwMinorVersion)
    185 			{
    186 				if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
    187 					extra = " OSR2";
    188 				buf = "Microsoft Windows 95" + extra;
    189 			}
    190 			if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
    191 			{
    192 				if (osvi.szCSDVersion[1] == 'A')
    193 					extra = "SE";
    194 				buf = "Microsoft Windows 98" + extra;
    195 			}
    196 			if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
    197 				buf = "Microsoft Windows Millennium Edition";
    198 	}
    199 	return buf;
    200 }
    201 
    202 bool SupportedWindowsVersion()
    203 {
    204 	OSVERSIONINFOEX osvi;
    205 
    206 	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    207 	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    208 
    209 	BOOL bOsVersionInfoEx = GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi));
    210 	if (!bOsVersionInfoEx)
    211 	{
    212 		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    213 		if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi)))
    214 			return false;
    215 	}
    216 
    217 	switch (osvi.dwPlatformId)
    218 	{
    219 		/* test for the Windows NT product family. */
    220 		case VER_PLATFORM_WIN32_NT:
    221 			/* win nt4 */
    222 			if (osvi.dwMajorVersion <= 4)
    223 				return false;
    224 			/* the rest */
    225 			return true;
    226 		/* win95 win98 winME */
    227 		case VER_PLATFORM_WIN32_WINDOWS:
    228 			return false;
    229 	}
    230 	return false;
    231 }
    232 
    233 int setenv(const char *name, const char *value, int overwrite)
    234 {
    235 	return SetEnvironmentVariable(name, value);
    236 }
    237 
    238 int unsetenv(const char *name)
    239 {
    240 	return SetEnvironmentVariable(name, NULL);
    241 }
    242 
    243 int mkstemp(char *input)
    244 {
    245 	input = _mktemp(input);
    246 	if (input == NULL)
    247 	{
    248 		errno = EEXIST;
    249 		return -1;
    250 	}
    251 
    252 	int fd = open(input, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
    253 	return fd;
    254 }
    255 
    256 void getcwd(char *buf, size_t sz)
    257 {
    258 	GetCurrentDirectory(sz, buf);
    259 }
    260 
    261 #endif