C ++获取linux发行版名称\版本

时间:2011-06-11 11:37:42

标签: c++ linux distribution

根据问题“How to get Linux distribution name and version?”,要获得linux发行版的名称和版本,这可行:

lsb_release -a

在我的系统上,它显示了所需的输出:

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

现在,要在C ++中获取此信息,Qt4的QProcess将是一个很好的选择,但由于我使用std c ++开发没有Qt,我需要知道如何在标准C ++中获取此信息,即进程的标准输出,还有一种解析信息的方法。

Uptil现在我正在尝试使用here中的代码,但我仍然停留在函数read()上。

6 个答案:

答案 0 :(得分:19)

您只需使用以下功能:

int uname(struct utsname *buf);

包含标题

#include <sys/utsname.h>

它已经返回名称&amp;版本作为结构的一部分:

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

我错过了什么吗?

答案 1 :(得分:1)

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]
}

它对我有用

答案 2 :(得分:0)

有些文件名为/ etc / version 和/ etc / release ,其中包含您是否使用Ubuntu或Fedora等信息(这是什么OP澄清了他的问题。)

答案 3 :(得分:0)

从cplusplus.com论坛得到它,一个简单的电话GetSystemOutput("/usr/bin/lsb_release -a")就可以了。

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;
}

答案 4 :(得分:0)

对于最近的Linux发行版,您可以使用以下内容获取操作系统信息。输出非常标准,可以使用以下规范进行解析:

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

cat /etc/os-release

示例输出:

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"

答案 5 :(得分:0)

我个人喜欢@Alok Slav发布的一致解决方案,但是如果它可以帮助需要使用命令行实用程序来获取信息的人,请考虑使用popen

相关问题