LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is there any C++ api available to know the OS description? (https://www.linuxquestions.org/questions/programming-9/is-there-any-c-api-available-to-know-the-os-description-738051/)

matrixdipu 07-06-2009 08:05 AM

Is there any C++ api available to know the OS description?
 
I am working tools that will be used on the multiple OS and platform. This tools will provide the details of the OS on which it is running, like 32 bit or 64 bit OS, exact version of Linux,Solaris,or other OS.

One way I am thinking of using the "uname -a" command to extract the OS information on the Linus/Unix based OS. Please suggest me that if it efficient to call the command from the program.

Please let me know if there is any api available or there is workaround I can implement.

TIA
dipu

dwhitney67 07-06-2009 08:40 AM

You can consider using popen(), and then parse the results. Or under Linux, you can examine /proc/version and/or perhaps /proc/cpuinfo, to get information. Or just use "uname -a". I'm not sure if Solaris uses a similar /proc to that of Linux.

Here's a simple program:
Code:

#include <cstdio>
#include <iostream>
#include <string>
#include <fstream>

int main()
{
  // here, executing a command to get results via a pipe
  //
  FILE* fp = popen("cat /proc/cpuinfo", "r");

  if (fp)
  {
      char buf[1024] = {0};

      while (fgets(buf, sizeof(buf), fp))
      {
        std::cout << buf;
      }
      pclose(fp);
  }


  // here, just reading a file the ol' fashioned way
  //
  std::fstream file("/proc/version", std::ios::in);

  if (file)
  {
      std::string line;

      while (getline(file, line))
      {
        std::cout << line << std::endl;
      }
  }
}


GazL 07-06-2009 09:04 AM

As well as the command there's also a uname() function call.

'man 2 uname' for details.


All times are GMT -5. The time now is 08:32 AM.