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