C ++ get linux distribution name \ version

According to the question " How to get the name and version of the Linux distribution? To get the name and version of the linux distribution, this works:

lsb_release -a 

My system displays the desired output:

 No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 9.10 Release: 9.10 Codename: karmic 

Now, to get this information in C ++, Qt4 QProcess would be a great option, but since I develop without Qt using std C ++, I need to know how to get this information in standard C ++, i.e. at the beginning of the process , as well as a method of analyzing information.

Uptil now I'm trying to use the code here , but stuck on the read () function.

+8
source share
6 answers

Got it from the cplusplus.com forums, a simple call to GetSystemOutput("/usr/bin/lsb_release -a") works.

 char* GetSystemOutput(char* cmd){ int buff_size = 32; char* buff = new char[buff_size]; char* ret = NULL; string str = ""; int fd[2]; int old_fd[3]; pipe(fd); old_fd[0] = dup(STDIN_FILENO); old_fd[1] = dup(STDOUT_FILENO); old_fd[2] = dup(STDERR_FILENO); int pid = fork(); switch(pid){ case 0: close(fd[0]); close(STDOUT_FILENO); close(STDERR_FILENO); dup2(fd[1], STDOUT_FILENO); dup2(fd[1], STDERR_FILENO); system(cmd); //execlp((const char*)cmd, cmd,0); close (fd[1]); exit(0); break; case -1: cerr << "GetSystemOutput/fork() error\n" << endl; exit(1); default: close(fd[1]); dup2(fd[0], STDIN_FILENO); int rc = 1; while (rc > 0){ rc = read(fd[0], buff, buff_size); str.append(buff, rc); //memset(buff, 0, buff_size); } ret = new char [strlen((char*)str.c_str())]; strcpy(ret, (char*)str.c_str()); waitpid(pid, NULL, 0); close(fd[0]); } dup2(STDIN_FILENO, old_fd[0]); dup2(STDOUT_FILENO, old_fd[1]); dup2(STDERR_FILENO, old_fd[2]); return ret; } 
0
source

You can simply use the function:

 int uname(struct utsname *buf); 

including title

 #include <sys/utsname.h> 

It already returns the name and version as part of the structure:

  struct utsname { char sysname[]; /* Operating system name (eg, "Linux") */ char nodename[]; /* Name within "some implementation-defined network" */ char release[]; /* OS release (eg, "2.6.28") */ char version[]; /* OS version */ char machine[]; /* Hardware identifier */ #ifdef _GNU_SOURCE char domainname[]; /* NIS or YP domain name */ #endif }; 

Did I miss something?

+22
source
 int writepipe[2]; if (pipe(writepipe) < 0) { perror("pipe"); return 1; } int ret = fork(); if (ret < 0) { perror("fork"); return 1; } else if (ret == 0) // child process { dup2(writepipe[1],1); // set writepipe[1] as stdout // close fds close(writepipe[0]); close(writepipe[1]); execlp("lsb_release","lsb_release","-a",NULL); //TODO: Error checking } else // parent process { int status; waitpid(ret,&status,0); //TODO: Error checking //do what you need //read output of lsb_release from writepipe[0] } 

It works for me

+1
source

For recent Linux distributions, you can use the following to get OS information. The output is pretty standard and can be analyzed using the following specification:

https://www.freedesktop.org/software/systemd/man/os-release.html

 cat /etc/os-release 

Selective Outputs:

 NAME=Fedora VERSION="27 (Twenty Seven)" ID=fedora VERSION_ID=27 PRETTY_NAME="Fedora 27 (Twenty Seven)" NAME="Ubuntu" VERSION="16.04.4 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.4 LTS" VERSION_ID="16.04" NAME="Arch Linux" PRETTY_NAME="Arch Linux" ID=arch ID_LIKE=archlinux ANSI_COLOR="0;36" 
+1
source

There are files called / etc / version and / etc / release that contain information, for example, whether you use Ubuntu or Fedora, etc. (this is what the OP clarified that his question will be).

0
source

Personally, I like the uname solution posted by @Alok Slav, but if it helps someone who needs to use the command line utility to get information, consider using popen .

0
source

All Articles