b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
57c28b61 |
15 | #ifndef _WIN32 |
7fd59977 |
16 | |
42cf5bc1 |
17 | #include <OSD_Host.hxx> |
18 | #include <OSD_OSDError.hxx> |
7fd59977 |
19 | #include <OSD_WhoAmI.hxx> |
42cf5bc1 |
20 | #include <Standard_ConstructionError.hxx> |
21 | #include <Standard_NullObject.hxx> |
22 | #include <TCollection_AsciiString.hxx> |
7fd59977 |
23 | |
24 | const OSD_WhoAmI Iam = OSD_WHost; |
25 | |
26 | #include <errno.h> |
27 | |
03155c18 |
28 | #include <sys/utsname.h> // For 'uname' |
29 | #include <netdb.h> // This is for 'gethostbyname' |
30 | #include <unistd.h> |
7fd59977 |
31 | #include <stdio.h> |
32 | |
7fd59977 |
33 | #if defined(__osf__) || defined(DECOSF1) |
34 | #include <sys/types.h> |
35 | #include <sys/sysinfo.h> // For 'getsysinfo' |
36 | #include <sys/socket.h> // To get ethernet address |
37 | #include <sys/ioctl.h> |
38 | #include <net/if.h> |
39 | extern "C" { |
40 | int gethostname(char* address, int len); |
41 | } |
42 | #endif |
43 | |
7fd59977 |
44 | extern "C" {int sysinfo(int, char *, long);} |
45 | |
46 | |
47 | // ========================================================================= |
48 | |
49 | OSD_Host::OSD_Host(){} |
50 | |
51 | // ========================================================================= |
52 | |
53 | TCollection_AsciiString OSD_Host::SystemVersion(){ |
54 | struct utsname info; |
55 | TCollection_AsciiString result; |
7fd59977 |
56 | |
57 | uname (&info); |
58 | result = info.sysname; |
59 | result += " "; |
60 | result += info.release; |
7fd59977 |
61 | return(result); |
62 | } |
63 | |
64 | // ========================================================================= |
65 | |
66 | OSD_SysType OSD_Host::SystemId()const{ |
67 | struct utsname info; |
68 | |
69 | uname (&info); |
70 | |
71 | if (!strcmp(info.sysname,"SunOS")) return (OSD_UnixBSD); |
72 | if (!strcmp(info.sysname,"ULTRIX")) return (OSD_UnixBSD); |
73 | if (!strcmp(info.sysname,"FreeBSD")) return (OSD_UnixBSD); |
74 | if (!strncmp(info.sysname,"Linux",5)) return (OSD_LinuxREDHAT); |
75 | if (!strncmp(info.sysname,"IRIX", 4)) return (OSD_UnixSystemV); |
76 | if (!strncmp(info.sysname,"OSF", 3)) return (OSD_OSF); |
77 | if (!strcmp(info.sysname,"AIX")) return (OSD_Aix); |
78 | if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_UnixSystemV); |
79 | if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VMS); |
80 | if (!strcmp(info.sysname,"Darwin")) return (OSD_MacOs); |
81 | return (OSD_Unknown); |
82 | } |
83 | |
84 | // ========================================================================= |
85 | |
86 | TCollection_AsciiString OSD_Host::HostName(){ |
87 | TCollection_AsciiString result; |
88 | char value[65]; |
89 | int status; |
90 | |
91 | status = gethostname(value, 64); |
92 | if (status == -1) myError.SetValue(errno, Iam, "Host Name"); |
93 | |
94 | result = value; |
95 | return(result); |
96 | } |
97 | |
98 | |
99 | // ========================================================================= |
100 | |
101 | |
102 | Standard_Integer OSD_Host::AvailableMemory(){ |
103 | Standard_Integer result; |
104 | |
105 | #if defined(__osf__) || defined(DECOSF1) |
106 | char buffer[16]; |
107 | //// result = getsysinfo(GSI_PHYSMEM,buffer, 16,0,NULL); |
108 | if (result != -1) |
109 | result *= 1024; |
110 | #else |
111 | result = 0; |
112 | //@@ A faire |
113 | #endif |
114 | return (result); |
115 | } |
116 | |
117 | // ========================================================================= |
118 | |
119 | TCollection_AsciiString OSD_Host::InternetAddress(){ |
120 | struct hostent internet_address; |
121 | int a,b,c,d; |
122 | char buffer[16]; |
123 | TCollection_AsciiString result,host; |
124 | |
125 | host = HostName(); |
126 | memcpy(&internet_address, |
127 | gethostbyname(host.ToCString()), |
128 | sizeof(struct hostent)); |
129 | |
130 | // Gets each bytes into integers |
131 | a = (unsigned char)internet_address.h_addr_list[0][0]; |
132 | b = (unsigned char)internet_address.h_addr_list[0][1]; |
133 | c = (unsigned char)internet_address.h_addr_list[0][2]; |
134 | d = (unsigned char)internet_address.h_addr_list[0][3]; |
135 | sprintf(buffer,"%d.%d.%d.%d",a,b,c,d); |
136 | result = buffer; |
137 | return(result); |
138 | } |
139 | |
7fd59977 |
140 | // ========================================================================= |
141 | OSD_OEMType OSD_Host::MachineType(){ |
142 | struct utsname info; |
143 | |
144 | uname (&info); |
145 | |
146 | if (!strcmp(info.sysname,"SunOS")) return (OSD_SUN); |
147 | if (!strcmp(info.sysname,"ULTRIX")) return (OSD_DEC); |
148 | if (!strncmp(info.sysname,"IRIX",4)) return (OSD_SGI); |
149 | if (!strcmp(info.sysname,"HP-UX")) return (OSD_HP); |
150 | if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_NEC); |
151 | if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VAX); |
152 | if (!strncmp(info.sysname,"OSF",3)) return (OSD_DEC); |
153 | if (!strncmp(info.sysname,"Linux",5)) return (OSD_LIN); |
154 | if (!strcmp(info.sysname,"FreeBSD")) return (OSD_LIN); |
155 | if (!strncmp(info.sysname,"AIX",3)) return (OSD_AIX); |
156 | if (!strcmp(info.sysname,"Darwin")) return (OSD_MAC); |
157 | return (OSD_Unavailable); |
158 | |
159 | } |
160 | |
161 | void OSD_Host::Reset(){ |
162 | myError.Reset(); |
163 | } |
164 | |
165 | Standard_Boolean OSD_Host::Failed()const{ |
166 | return( myError.Failed()); |
167 | } |
168 | |
169 | void OSD_Host::Perror() { |
170 | myError.Perror(); |
171 | } |
172 | |
173 | |
174 | Standard_Integer OSD_Host::Error()const{ |
175 | return( myError.Error()); |
176 | } |
177 | |
178 | #else |
179 | |
180 | //------------------------------------------------------------------------ |
181 | //------------------- WNT Sources of OSD_Host --------------------------- |
182 | //------------------------------------------------------------------------ |
183 | |
7fd59977 |
184 | #include <windows.h> |
185 | |
186 | #include <OSD_Host.hxx> |
187 | |
7c65581d |
188 | #if defined(_MSC_VER) |
189 | #pragma comment( lib, "WSOCK32.LIB" ) |
190 | #endif |
7fd59977 |
191 | |
192 | void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... ); |
193 | |
194 | static BOOL fInit = FALSE; |
195 | static TCollection_AsciiString hostName; |
196 | static TCollection_AsciiString version; |
197 | static TCollection_AsciiString interAddr; |
7fd59977 |
198 | static Standard_Integer memSize; |
199 | |
200 | OSD_Host :: OSD_Host () { |
742cc8b0 |
201 | #ifndef OCCT_UWP |
7fd59977 |
202 | DWORD nSize; |
ad03c234 |
203 | char szHostName[MAX_COMPUTERNAME_LENGTH + 1]; |
302f96fb |
204 | char* hostAddr = 0; |
7fd59977 |
205 | MEMORYSTATUS ms; |
206 | WSADATA wd; |
207 | PHOSTENT phe; |
208 | IN_ADDR inAddr; |
ad03c234 |
209 | OSVERSIONINFOW osVerInfo; |
7fd59977 |
210 | |
211 | if ( !fInit ) { |
212 | |
213 | nSize = MAX_COMPUTERNAME_LENGTH + 1; |
c85a994a |
214 | ZeroMemory (&osVerInfo, sizeof(OSVERSIONINFOW)); |
215 | osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); |
7fd59977 |
216 | |
302f96fb |
217 | ZeroMemory (&ms, sizeof(ms)); |
ad03c234 |
218 | ZeroMemory (szHostName, sizeof(char) * (MAX_COMPUTERNAME_LENGTH + 1)); |
7fd59977 |
219 | |
fe9b8ff2 |
220 | #ifdef _MSC_VER |
221 | // suppress GetVersionEx() deprecation warning |
222 | #pragma warning(disable : 4996) |
223 | #endif |
ad03c234 |
224 | if (!GetVersionExW (&osVerInfo)) |
225 | { |
226 | _osd_wnt_set_error (myError, OSD_WHost); |
227 | } |
228 | else if (!GetComputerNameA (szHostName, &nSize)) |
229 | { |
230 | _osd_wnt_set_error (myError, OSD_WHost); |
231 | } |
232 | else |
233 | { |
234 | ms.dwLength = sizeof(MEMORYSTATUS); |
235 | GlobalMemoryStatus (&ms); |
7fd59977 |
236 | } // end else |
fe9b8ff2 |
237 | #ifdef _MSC_VER |
238 | #pragma warning(default : 4996) |
239 | #endif |
7fd59977 |
240 | |
241 | if ( !Failed () ) { |
242 | |
60be1f9b |
243 | memSize = (Standard_Integer) ms.dwAvailPageFile; |
7fd59977 |
244 | |
245 | if ( WSAStartup ( MAKEWORD( 1, 1 ), &wd ) ) { |
246 | |
247 | _osd_wnt_set_error ( myError, OSD_WHost ); |
248 | |
ad03c234 |
249 | } else if ( ( phe = gethostbyname (szHostName) ) == NULL ) { |
7fd59977 |
250 | |
251 | _osd_wnt_set_error ( myError, OSD_WHost ); |
252 | |
253 | } else { |
254 | |
255 | CopyMemory ( &inAddr, *phe -> h_addr_list, sizeof ( IN_ADDR ) ); |
256 | hostAddr = inet_ntoa ( inAddr ); |
257 | |
258 | } // end else |
259 | |
260 | } // end if |
261 | |
262 | if ( !Failed () ) { |
263 | |
264 | hostName = szHostName; |
265 | interAddr = Standard_CString ( hostAddr ); |
c85a994a |
266 | TCollection_AsciiString aVersion = TCollection_AsciiString("Windows NT Version ") + (int )osVerInfo.dwMajorVersion + "." + (int )osVerInfo.dwMinorVersion; |
267 | if (*osVerInfo.szCSDVersion != L'\0') |
268 | { |
269 | aVersion += TCollection_AsciiString(" ") + TCollection_AsciiString (osVerInfo.szCSDVersion); |
270 | } |
271 | version = aVersion; |
7fd59977 |
272 | |
273 | fInit = TRUE; |
274 | |
275 | } // end if |
276 | |
277 | } // end if |
278 | |
279 | if ( fInit ) |
280 | |
281 | myName = hostName; |
742cc8b0 |
282 | #endif |
7fd59977 |
283 | } // end constructor |
284 | |
285 | TCollection_AsciiString OSD_Host :: SystemVersion () { |
286 | |
287 | return version; |
288 | |
289 | } // end OSD_Host :: SystemVersion |
290 | |
291 | OSD_SysType OSD_Host :: SystemId () const { |
292 | |
293 | return OSD_WindowsNT; |
294 | |
295 | } // end OSD_Host :: SystemId |
296 | |
297 | TCollection_AsciiString OSD_Host :: HostName () { |
298 | |
299 | return hostName; |
300 | |
301 | } // end OSD_Host :: HostName |
302 | |
303 | Standard_Integer OSD_Host :: AvailableMemory () { |
304 | |
305 | return memSize; |
306 | |
307 | } // end OSD_Host :: AvailableMemory |
308 | |
309 | TCollection_AsciiString OSD_Host :: InternetAddress () { |
310 | |
311 | return interAddr; |
312 | |
313 | } // end OSD_Host :: InternetAddress |
314 | |
7fd59977 |
315 | OSD_OEMType OSD_Host :: MachineType () { |
316 | |
317 | return OSD_PC; |
318 | |
319 | } // end OSD_Host :: MachineTYpe |
320 | |
321 | Standard_Boolean OSD_Host :: Failed () const { |
322 | |
323 | return myError.Failed (); |
324 | |
325 | } // end OSD_Host :: Failed |
326 | |
327 | void OSD_Host :: Reset () { |
328 | |
329 | myError.Reset (); |
330 | |
331 | } // end OSD_Host :: Reset |
332 | |
333 | void OSD_Host :: Perror () { |
334 | |
335 | myError.Perror (); |
336 | |
337 | } // end OSD_Host :: Perror |
338 | |
339 | Standard_Integer OSD_Host :: Error () const { |
340 | |
341 | return myError.Error (); |
342 | |
343 | } //end OSD_Host :: Error |
344 | |
345 | #endif |
346 | |
347 | |
348 | |
349 | |
350 | |