machine.cxx

Go to the documentation of this file.
00001 //
00002 // Copyright 2002, Lowell Boggs Jr.
00003 //
00004 // This file or directory, containing source code for a computer program,
00005 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00006 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00007 // source file without charge or obligation so long as you agree to
00008 // the following:
00009 //
00010 //  1.  You must indemnify Lowell Boggs against any and all financial
00011 //      obligations caused by its use, misuse, function, or malfunction.
00012 //      Further, you acknowledge that there is no warranty of any kind,
00013 //      whatsoever.
00014 //
00015 //  2.  You agree not to attempt to patent any portion of this original
00016 //      work -- though you may attempt to patent your own extensions to
00017 //      it if you so choose.
00018 //
00019 //  3.  You keep this copyright notice with the file and all copies
00020 //      of the file and do not change it anyway except language translation.
00021 //
00022 // You are responsible for enforcing your own compliance with these
00023 // conditions and may not use this source file if you cannot agree to the
00024 // above terms and conditions.
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);  // first line is blank!
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 } // namespace cxxtls
Generated on Wed Feb 29 22:50:05 2012 for CXXUtilities by  doxygen 1.6.3