C++ get linux distribution name\version

后端 未结 6 1640
悲哀的现实
悲哀的现实 2021-01-12 06:10

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

lsb_release -a
         


        
6条回答
  •  遥遥无期
    2021-01-12 07:00

    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

提交回复
热议问题