0023072: Eliminate compiler warnings (level 3) on Windows / MSVC++
[occt.git] / src / OSD / OSD_Host.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #ifndef WNT
24
25 #include <Standard_NullObject.hxx>
26 #include <Standard_ConstructionError.hxx>
27 #include <OSD_Host.ixx>
28 #include <OSD_WhoAmI.hxx>
29
30 const OSD_WhoAmI Iam = OSD_WHost;
31
32 #include <errno.h>
33
34 #ifdef HAVE_SYS_UTSNAME_H
35 # include <sys/utsname.h> // For 'uname'
36 #endif
37
38 #ifdef HAVE_NETDB_H
39 # include <netdb.h>       // This is for 'gethostbyname'
40 #endif
41
42 #include <stdio.h>
43
44 #ifdef HAVE_SYS_IOCTL_H
45 # include <sys/ioctl.h>
46 #endif
47
48 #if defined(__osf__) || defined(DECOSF1)
49 #include <sys/types.h>
50 #include <sys/sysinfo.h>  // For 'getsysinfo'
51 #include <sys/socket.h>   // To get ethernet address
52 #include <sys/ioctl.h>
53 #include <net/if.h>
54 extern "C" {
55   int gethostname(char* address, int len); 
56 }
57 #endif
58
59 #ifdef HAVE_SYSENT_H
60 # include <sysent.h>      // for 'gethostname'
61 #endif
62
63 #ifdef HAVE_SYS_SOCKET_H
64 # include <sys/socket.h>
65 #endif
66
67 #ifdef HAVE_SYS_UNISTD_H
68 # include <sys/unistd.h>
69 #endif
70
71 #ifdef HAVE_UNISTD_H
72 # include <unistd.h>
73 #endif
74
75 #ifdef HAVE_SYS_SYSTEMINFO_H
76 # include <sys/systeminfo.h>
77 #endif
78
79 extern "C" {int sysinfo(int, char *, long);}
80
81
82 // =========================================================================
83
84 OSD_Host::OSD_Host(){}
85
86 // =========================================================================
87
88 TCollection_AsciiString OSD_Host::SystemVersion(){
89 struct utsname info;
90 TCollection_AsciiString result;
91 #ifdef HAVE_SYS_SYSTEMINFO_H
92 char buf[100];
93 #endif
94
95  uname (&info);
96  result  = info.sysname;
97  result += " ";
98  result += info.release;
99 #ifdef HAVE_SYS_SYSTEMINFO_H
100  result += " ";
101  sysinfo(SI_ARCHITECTURE,buf,99);
102  result += buf;
103  result += " ";
104  sysinfo(SI_HW_PROVIDER,buf,99);
105  result += buf;
106 #endif
107  return(result);
108 }
109
110 // =========================================================================
111
112 OSD_SysType OSD_Host::SystemId()const{
113 struct utsname info; 
114  
115  uname (&info);
116
117  if (!strcmp(info.sysname,"SunOS"))          return (OSD_UnixBSD);
118  if (!strcmp(info.sysname,"ULTRIX"))         return (OSD_UnixBSD);
119  if (!strcmp(info.sysname,"FreeBSD"))        return (OSD_UnixBSD);
120  if (!strncmp(info.sysname,"Linux",5))       return (OSD_LinuxREDHAT);
121  if (!strncmp(info.sysname,"IRIX", 4))       return (OSD_UnixSystemV);
122  if (!strncmp(info.sysname,"OSF", 3))        return (OSD_OSF);
123  if (!strcmp(info.sysname,"AIX"))            return (OSD_Aix);
124  if (!strcmp(info.sysname,"UNIX_System_V"))  return (OSD_UnixSystemV);
125  if (!strcmp(info.sysname,"VMS_POSIX"))      return (OSD_VMS);
126  if (!strcmp(info.sysname,"Darwin"))         return (OSD_MacOs);
127  return (OSD_Unknown);
128 }
129
130 // =========================================================================
131
132 TCollection_AsciiString OSD_Host::HostName(){
133 TCollection_AsciiString result;
134 char value[65];
135 int status;
136
137 status = gethostname(value, 64);
138 if (status == -1) myError.SetValue(errno, Iam, "Host Name");
139
140  result = value;
141  return(result);
142 }
143
144
145 // =========================================================================
146
147
148 Standard_Integer OSD_Host::AvailableMemory(){
149  Standard_Integer result;
150
151 #if defined(__osf__) || defined(DECOSF1)
152  char buffer[16];
153  ////     result = getsysinfo(GSI_PHYSMEM,buffer, 16,0,NULL);
154  if (result != -1)
155   result *= 1024;
156 #else
157  result = 0;
158  //@@ A faire
159 #endif
160  return (result);
161 }
162
163 // =========================================================================
164
165 TCollection_AsciiString OSD_Host::InternetAddress(){
166  struct hostent internet_address;
167  int a,b,c,d;
168  char buffer[16];
169  TCollection_AsciiString result,host;
170
171  host = HostName();
172  memcpy(&internet_address,
173         gethostbyname(host.ToCString()),
174         sizeof(struct hostent));
175
176  // Gets each bytes into integers
177  a = (unsigned char)internet_address.h_addr_list[0][0];
178  b = (unsigned char)internet_address.h_addr_list[0][1];
179  c = (unsigned char)internet_address.h_addr_list[0][2];
180  d = (unsigned char)internet_address.h_addr_list[0][3];
181  sprintf(buffer,"%d.%d.%d.%d",a,b,c,d);
182  result = buffer;
183  return(result);
184 }
185
186 // =========================================================================
187 OSD_OEMType OSD_Host::MachineType(){
188 struct utsname info; 
189  
190  uname (&info);
191
192  if (!strcmp(info.sysname,"SunOS"))         return (OSD_SUN);
193  if (!strcmp(info.sysname,"ULTRIX"))        return (OSD_DEC);
194  if (!strncmp(info.sysname,"IRIX",4))       return (OSD_SGI);
195  if (!strcmp(info.sysname,"HP-UX"))         return (OSD_HP);
196  if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_NEC);
197  if (!strcmp(info.sysname,"VMS_POSIX"))     return (OSD_VAX);
198  if (!strncmp(info.sysname,"OSF",3))        return (OSD_DEC);
199  if (!strncmp(info.sysname,"Linux",5))      return (OSD_LIN);
200  if (!strcmp(info.sysname,"FreeBSD"))       return (OSD_LIN);
201  if (!strncmp(info.sysname,"AIX",3))        return (OSD_AIX);
202  if (!strcmp(info.sysname,"Darwin"))        return (OSD_MAC);
203  return (OSD_Unavailable);
204
205 }
206
207 void OSD_Host::Reset(){
208  myError.Reset();
209 }
210
211 Standard_Boolean OSD_Host::Failed()const{
212  return( myError.Failed());
213 }
214
215 void OSD_Host::Perror() {
216  myError.Perror();
217 }
218
219
220 Standard_Integer OSD_Host::Error()const{
221  return( myError.Error());
222 }
223
224 #else
225
226 //------------------------------------------------------------------------
227 //-------------------  WNT Sources of OSD_Host ---------------------------
228 //------------------------------------------------------------------------
229
230 #define STRICT
231 #include <windows.h>
232
233 #include <OSD_Host.hxx>
234
235 #pragma comment( lib, "WSOCK32.LIB" )
236
237 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
238
239 static BOOL                    fInit = FALSE;
240 static TCollection_AsciiString hostName;
241 static TCollection_AsciiString version;
242 static TCollection_AsciiString interAddr;
243 static Standard_Integer        memSize;
244
245 OSD_Host :: OSD_Host () {
246
247  DWORD              nSize;
248  Standard_Character szHostName[ MAX_COMPUTERNAME_LENGTH + 1 ];
249  char*              hostAddr;
250  MEMORYSTATUS       ms;
251  WSADATA            wd;
252  PHOSTENT           phe;
253  IN_ADDR            inAddr;
254  OSVERSIONINFO      osVerInfo;
255
256  if ( !fInit ) {
257
258   nSize                         = MAX_COMPUTERNAME_LENGTH + 1;
259   osVerInfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
260
261   ZeroMemory (  szHostName, sizeof ( hostName )  );
262
263   if (  !GetVersionEx ( &osVerInfo )  ) {
264
265    _osd_wnt_set_error ( myError, OSD_WHost );
266
267   } else if (  !GetComputerName ( szHostName, &nSize )  ) {
268   
269    _osd_wnt_set_error ( myError, OSD_WHost );
270   
271   } else {
272  
273    ms.dwLength = sizeof ( MEMORYSTATUS );
274    GlobalMemoryStatus ( &ms );
275
276   }  // end else
277
278   if (  !Failed ()  ) {
279   
280     memSize = (Standard_Integer) ms.dwAvailPageFile;
281
282    if (   WSAStartup (  MAKEWORD( 1, 1 ), &wd  )   ) {
283    
284     _osd_wnt_set_error ( myError, OSD_WHost );
285    
286    } else if (   (  phe = gethostbyname ( szHostName )  ) == NULL   ) {
287    
288     _osd_wnt_set_error ( myError, OSD_WHost );
289    
290    } else {
291
292     CopyMemory (  &inAddr, *phe -> h_addr_list, sizeof ( IN_ADDR )  );
293     hostAddr = inet_ntoa ( inAddr );
294
295    }  // end else
296   
297   }  // end if
298
299   if (  !Failed ()  ) {
300   
301    hostName  = szHostName;
302    interAddr = Standard_CString ( hostAddr );
303    wsprintf (
304     osVerInfo.szCSDVersion, TEXT( "Windows NT Version %d.%d" ),
305     osVerInfo.dwMajorVersion, osVerInfo.dwMinorVersion
306    );
307    version = osVerInfo.szCSDVersion;
308
309    fInit = TRUE;
310   
311   }  // end if
312  
313  }  // end if
314
315  if ( fInit )
316
317   myName = hostName;
318
319 }  // end constructor
320
321 TCollection_AsciiString OSD_Host :: SystemVersion () {
322
323  return version;
324
325 }  // end OSD_Host :: SystemVersion
326
327 OSD_SysType OSD_Host :: SystemId () const {
328
329  return OSD_WindowsNT;
330
331 }  // end OSD_Host :: SystemId
332
333 TCollection_AsciiString OSD_Host :: HostName () {
334
335  return hostName;
336
337 }  // end OSD_Host :: HostName
338
339 Standard_Integer OSD_Host :: AvailableMemory () {
340
341  return memSize;
342
343 }  // end OSD_Host :: AvailableMemory
344
345 TCollection_AsciiString OSD_Host :: InternetAddress () {
346
347  return interAddr;
348
349 }  // end OSD_Host :: InternetAddress
350
351 OSD_OEMType OSD_Host :: MachineType () {
352
353  return OSD_PC;
354
355 }  // end OSD_Host :: MachineTYpe
356
357 Standard_Boolean OSD_Host :: Failed () const {
358
359  return myError.Failed ();
360
361 }  // end OSD_Host :: Failed
362
363 void OSD_Host :: Reset () {
364
365  myError.Reset ();
366
367 }  // end OSD_Host :: Reset
368
369 void OSD_Host :: Perror () {
370
371  myError.Perror ();
372
373 }  // end OSD_Host :: Perror
374
375 Standard_Integer OSD_Host :: Error () const {
376
377  return myError.Error ();
378
379 }  //end OSD_Host :: Error
380
381 #endif
382
383
384
385
386