machine.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00029
00030
00031 #include <cxxtls/machine.h>
00032 #ifdef _MSC_VER
00033 #include <io.h>
00034 #include <portable_io.h>
00035 static std::string hostname_cache;
00036 static std::string uname_cache;
00037 static bool cached=false;
00038 #else
00039 #include <sys/utsname.h>
00040 #endif
00041
00042 namespace cxxtls
00043 {
00044
00045 MachineInfo
00046 MachineInfo::get()
00047 {
00048 MachineInfo rv;
00049
00050 #if _MSC_VER
00051
00052 if(!cached)
00053 {
00054 cached=true;
00055
00056 FILE *hostname_command=_popen("hostname", "r");
00057
00058 char buffer[2000];
00059
00060 if(hostname_command)
00061 {
00062 strcpy(buffer, "unknown\r\n");
00063
00064 fgets(buffer, 2000, hostname_command);
00065
00066 hostname_cache = buffer;
00067
00068 _pclose(hostname_command);
00069
00070 }
00071
00072 FILE *uname_command=_popen("cmd /c ver", "r");
00073
00074 if(uname_command)
00075 {
00076 strcpy(buffer, "Windows NT Version 4\r\n");
00077
00078 fgets(buffer, 2000, uname_command);
00079 fgets(buffer, 2000, uname_command);
00080
00081 char osname[2000];
00082
00083 sscanf(buffer, "%s", osname);
00084
00085 uname_cache = osname;
00086
00087 _pclose(uname_command);
00088
00089 }
00090
00091 }
00092
00093 rv.operating_system_ = uname_cache;
00094 rv.hostname_ = hostname_cache;
00095
00096
00097 return rv;
00098
00099 #else
00100 struct utsname info;
00101
00102 if(uname(&info) < 0)
00103 return rv;
00104
00105 rv.operating_system_ = info.sysname;
00106 rv.hostname_ = info.nodename;
00107
00108 return rv;
00109 #endif
00110
00111
00112 }
00113
00114 }