1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 #include <OSD_Host.hxx>
19 #include <OSD_OSDError.hxx>
20 #include <OSD_WhoAmI.hxx>
21 #include <Standard_ConstructionError.hxx>
22 #include <Standard_NullObject.hxx>
23 #include <TCollection_AsciiString.hxx>
25 const OSD_WhoAmI Iam = OSD_WHost;
29 #include <sys/utsname.h> // For 'uname'
30 #include <netdb.h> // This is for 'gethostbyname'
34 #if defined(__osf__) || defined(DECOSF1)
35 #include <sys/types.h>
36 #include <sys/sysinfo.h> // For 'getsysinfo'
37 #include <sys/socket.h> // To get ethernet address
38 #include <sys/ioctl.h>
41 int gethostname(char* address, int len);
45 extern "C" {int sysinfo(int, char *, long);}
48 // =========================================================================
50 OSD_Host::OSD_Host(){}
52 // =========================================================================
54 TCollection_AsciiString OSD_Host::SystemVersion(){
56 TCollection_AsciiString result;
59 result = info.sysname;
61 result += info.release;
65 // =========================================================================
67 OSD_SysType OSD_Host::SystemId()const{
72 if (!strcmp(info.sysname,"SunOS")) return (OSD_UnixBSD);
73 if (!strcmp(info.sysname,"ULTRIX")) return (OSD_UnixBSD);
74 if (!strcmp(info.sysname,"FreeBSD")) return (OSD_UnixBSD);
75 if (!strncmp(info.sysname,"Linux",5)) return (OSD_LinuxREDHAT);
76 if (!strncmp(info.sysname,"IRIX", 4)) return (OSD_UnixSystemV);
77 if (!strncmp(info.sysname,"OSF", 3)) return (OSD_OSF);
78 if (!strcmp(info.sysname,"AIX")) return (OSD_Aix);
79 if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_UnixSystemV);
80 if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VMS);
81 if (!strcmp(info.sysname,"Darwin")) return (OSD_MacOs);
85 // =========================================================================
87 TCollection_AsciiString OSD_Host::HostName(){
88 TCollection_AsciiString result;
92 status = gethostname(value, 64);
93 if (status == -1) myError.SetValue(errno, Iam, "Host Name");
100 // =========================================================================
103 Standard_Integer OSD_Host::AvailableMemory(){
104 Standard_Integer result;
106 #if defined(__osf__) || defined(DECOSF1)
108 //// result = getsysinfo(GSI_PHYSMEM,buffer, 16,0,NULL);
118 // =========================================================================
120 TCollection_AsciiString OSD_Host::InternetAddress(){
121 struct hostent internet_address;
124 TCollection_AsciiString result,host;
127 memcpy(&internet_address,
128 gethostbyname(host.ToCString()),
129 sizeof(struct hostent));
131 // Gets each bytes into integers
132 a = (unsigned char)internet_address.h_addr_list[0][0];
133 b = (unsigned char)internet_address.h_addr_list[0][1];
134 c = (unsigned char)internet_address.h_addr_list[0][2];
135 d = (unsigned char)internet_address.h_addr_list[0][3];
136 sprintf(buffer,"%d.%d.%d.%d",a,b,c,d);
141 // =========================================================================
142 OSD_OEMType OSD_Host::MachineType(){
147 if (!strcmp(info.sysname,"SunOS")) return (OSD_SUN);
148 if (!strcmp(info.sysname,"ULTRIX")) return (OSD_DEC);
149 if (!strncmp(info.sysname,"IRIX",4)) return (OSD_SGI);
150 if (!strcmp(info.sysname,"HP-UX")) return (OSD_HP);
151 if (!strcmp(info.sysname,"UNIX_System_V")) return (OSD_NEC);
152 if (!strcmp(info.sysname,"VMS_POSIX")) return (OSD_VAX);
153 if (!strncmp(info.sysname,"OSF",3)) return (OSD_DEC);
154 if (!strncmp(info.sysname,"Linux",5)) return (OSD_LIN);
155 if (!strcmp(info.sysname,"FreeBSD")) return (OSD_LIN);
156 if (!strncmp(info.sysname,"AIX",3)) return (OSD_AIX);
157 if (!strcmp(info.sysname,"Darwin")) return (OSD_MAC);
158 return (OSD_Unavailable);
162 void OSD_Host::Reset(){
166 Standard_Boolean OSD_Host::Failed()const{
167 return( myError.Failed());
170 void OSD_Host::Perror() {
175 Standard_Integer OSD_Host::Error()const{
176 return( myError.Error());
181 //------------------------------------------------------------------------
182 //------------------- WNT Sources of OSD_Host ---------------------------
183 //------------------------------------------------------------------------
188 #include <OSD_Host.hxx>
190 #pragma comment( lib, "WSOCK32.LIB" )
192 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
194 static BOOL fInit = FALSE;
195 static TCollection_AsciiString hostName;
196 static TCollection_AsciiString version;
197 static TCollection_AsciiString interAddr;
198 static Standard_Integer memSize;
200 OSD_Host :: OSD_Host () {
203 Standard_Character szHostName[ MAX_COMPUTERNAME_LENGTH + 1 ];
209 OSVERSIONINFO osVerInfo;
213 nSize = MAX_COMPUTERNAME_LENGTH + 1;
214 osVerInfo.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
216 ZeroMemory (&ms, sizeof(ms));
217 ZeroMemory ( szHostName, sizeof ( Standard_Character ) * (MAX_COMPUTERNAME_LENGTH + 1) );
220 // suppress GetVersionEx() deprecation warning
221 #pragma warning(disable : 4996)
223 if ( !GetVersionEx ( &osVerInfo ) ) {
225 _osd_wnt_set_error ( myError, OSD_WHost );
227 } else if ( !GetComputerName ( szHostName, &nSize ) ) {
229 _osd_wnt_set_error ( myError, OSD_WHost );
233 ms.dwLength = sizeof ( MEMORYSTATUS );
234 GlobalMemoryStatus ( &ms );
238 #pragma warning(default : 4996)
243 memSize = (Standard_Integer) ms.dwAvailPageFile;
245 if ( WSAStartup ( MAKEWORD( 1, 1 ), &wd ) ) {
247 _osd_wnt_set_error ( myError, OSD_WHost );
249 } else if ( ( phe = gethostbyname ( szHostName ) ) == NULL ) {
251 _osd_wnt_set_error ( myError, OSD_WHost );
255 CopyMemory ( &inAddr, *phe -> h_addr_list, sizeof ( IN_ADDR ) );
256 hostAddr = inet_ntoa ( inAddr );
264 hostName = szHostName;
265 interAddr = Standard_CString ( hostAddr );
267 osVerInfo.szCSDVersion, TEXT( "Windows NT Version %d.%d" ),
268 osVerInfo.dwMajorVersion, osVerInfo.dwMinorVersion
270 version = osVerInfo.szCSDVersion;
284 TCollection_AsciiString OSD_Host :: SystemVersion () {
288 } // end OSD_Host :: SystemVersion
290 OSD_SysType OSD_Host :: SystemId () const {
292 return OSD_WindowsNT;
294 } // end OSD_Host :: SystemId
296 TCollection_AsciiString OSD_Host :: HostName () {
300 } // end OSD_Host :: HostName
302 Standard_Integer OSD_Host :: AvailableMemory () {
306 } // end OSD_Host :: AvailableMemory
308 TCollection_AsciiString OSD_Host :: InternetAddress () {
312 } // end OSD_Host :: InternetAddress
314 OSD_OEMType OSD_Host :: MachineType () {
318 } // end OSD_Host :: MachineTYpe
320 Standard_Boolean OSD_Host :: Failed () const {
322 return myError.Failed ();
324 } // end OSD_Host :: Failed
326 void OSD_Host :: Reset () {
330 } // end OSD_Host :: Reset
332 void OSD_Host :: Perror () {
336 } // end OSD_Host :: Perror
338 Standard_Integer OSD_Host :: Error () const {
340 return myError.Error ();
342 } //end OSD_Host :: Error