Anope IRC Services  Version 2.0
windows.cpp
Go to the documentation of this file.
1  /* POSIX emulation layer for Windows.
2  *
3  * Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
4  * Copyright (C) 2008-2014 Anope Team <info@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  const char *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  {NULL, 0}
40 };
41 
42 static WSADATA wsa;
43 
44 void OnStartup()
45 {
46  if (WSAStartup(MAKEWORD(2, 0), &wsa))
47  throw CoreException("Failed to initialize WinSock library");
48 }
49 
50 void OnShutdown()
51 {
52  WSACleanup();
53 }
54 
55 USHORT WindowsGetLanguage(const char *lang)
56 {
57  for (int i = 0; WindowsLanguages[i].languageName; ++i)
58  if (!strcmp(lang, WindowsLanguages[i].languageName))
59  return WindowsLanguages[i].windowsLanguageName;
60  return LANG_NEUTRAL;
61 }
62 
68 int gettimeofday(timeval *tv, void *)
69 {
70  SYSTEMTIME st;
71  GetSystemTime(&st);
72 
73  tv->tv_sec = Anope::CurTime;
74  tv->tv_usec = st.wMilliseconds;
75 
76  return 0;
77 }
78 
79 Anope::string GetWindowsVersion()
80 {
81  OSVERSIONINFOEX osvi;
82  SYSTEM_INFO si;
83 
84  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
85  ZeroMemory(&si, sizeof(SYSTEM_INFO));
86  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
87 
88  BOOL bOsVersionInfoEx = GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi));
89  if (!bOsVersionInfoEx)
90  {
91  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
92  if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi)))
93  return "";
94  }
95  GetSystemInfo(&si);
96 
97  Anope::string buf, extra, cputype;
98  /* Determine CPU type 32 or 64 */
99  if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
100  cputype = " 64-bit";
101  else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
102  cputype = " 32-bit";
103  else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
104  cputype = " Itanium 64-bit";
105 
106  switch (osvi.dwPlatformId)
107  {
108  /* test for the Windows NT product family. */
109  case VER_PLATFORM_WIN32_NT:
110  /* Windows Vista or Windows Server 2008 */
111  if (osvi.dwMajorVersion == 6 && !osvi.dwMinorVersion)
112  {
113  if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
114  extra = " Enterprise Edition";
115  else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
116  extra = " Datacenter Edition";
117  else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
118  extra = " Home Premium/Basic";
119  if (osvi.dwMinorVersion == 0)
120  {
121  if (osvi.wProductType & VER_NT_WORKSTATION)
122  buf = "Microsoft Windows Vista" + cputype + extra;
123  else
124  buf = "Microsoft Windows Server 2008" + cputype + extra;
125  }
126  else if (osvi.dwMinorVersion == 1)
127  {
128  if (osvi.wProductType & VER_NT_WORKSTATION)
129  buf = "Microsoft Windows 7" + cputype + extra;
130  else
131  buf = "Microsoft Windows Server 2008 R2" + cputype + extra;
132  }
133  }
134  /* Windows 2003 or Windows XP Pro 64 */
135  if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
136  {
137  if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
138  extra = " Datacenter Edition";
139  else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
140  extra = " Enterprise Edition";
141 #ifdef VER_SUITE_COMPUTE_SERVER
142  else if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
143  extra = " Compute Cluster Edition";
144 #endif
145  else if (osvi.wSuiteMask == VER_SUITE_BLADE)
146  extra = " Web Edition";
147  else
148  extra = " Standard Edition";
149  if (osvi.wProductType & VER_NT_WORKSTATION && si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
150  buf = "Microsoft Windows XP Professional x64 Edition" + extra;
151  else
152  buf = "Microsoft Windows Server 2003 Family" + cputype + extra;
153  }
154  if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
155  {
156  if (osvi.wSuiteMask & VER_SUITE_EMBEDDEDNT)
157  extra = " Embedded";
158  else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
159  extra = " Home Edition";
160  buf = "Microsoft Windows XP" + extra;
161  }
162  if (osvi.dwMajorVersion == 5 && !osvi.dwMinorVersion)
163  {
164  if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
165  extra = " Datacenter Server";
166  else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
167  extra = " Advanced Server";
168  else
169  extra = " Server";
170  buf = "Microsoft Windows 2000" + extra;
171  }
172  if (osvi.dwMajorVersion <= 4)
173  {
174  if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
175  extra = " Enterprise Edition";
176  buf = "Microsoft Windows NT Server 4.0" + extra;
177  }
178  break;
179  case VER_PLATFORM_WIN32_WINDOWS:
180  if (osvi.dwMajorVersion == 4 && !osvi.dwMinorVersion)
181  {
182  if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
183  extra = " OSR2";
184  buf = "Microsoft Windows 95" + extra;
185  }
186  if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
187  {
188  if (osvi.szCSDVersion[1] == 'A')
189  extra = "SE";
190  buf = "Microsoft Windows 98" + extra;
191  }
192  if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
193  buf = "Microsoft Windows Millenium Edition";
194  }
195  return buf;
196 }
197 
198 bool SupportedWindowsVersion()
199 {
200  OSVERSIONINFOEX osvi;
201 
202  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
203  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
204 
205  BOOL bOsVersionInfoEx = GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi));
206  if (!bOsVersionInfoEx)
207  {
208  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
209  if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi)))
210  return false;
211  }
212 
213  switch (osvi.dwPlatformId)
214  {
215  /* test for the Windows NT product family. */
216  case VER_PLATFORM_WIN32_NT:
217  /* win nt4 */
218  if (osvi.dwMajorVersion <= 4)
219  return false;
220  /* the rest */
221  return true;
222  /* win95 win98 winME */
223  case VER_PLATFORM_WIN32_WINDOWS:
224  return false;
225  }
226  return false;
227 }
228 
229 int setenv(const char *name, const char *value, int overwrite)
230 {
231  return SetEnvironmentVariable(name, value);
232 }
233 
234 int unsetenv(const char *name)
235 {
236  return SetEnvironmentVariable(name, NULL);
237 }
238 
239 int mkstemp(char *input)
240 {
241  input = _mktemp(input);
242  if (input == NULL)
243  {
244  errno = EEXIST;
245  return -1;
246  }
247 
248  int fd = open(input, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
249  return fd;
250 }
251 
252 void getcwd(char *buf, size_t sz)
253 {
254  GetCurrentDirectory(sz, buf);
255 }
256 
257 #endif
CoreExport time_t CurTime
Definition: main.cpp:41
Anope::string name
Definition: access.cpp:22