Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ get linux distribution name\version

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

On my system, it shows the needed output:

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

Now, to get this info in C++, Qt4's QProcess would be a great option but since I am developing without Qt using std c++, I need to know how to get this info in standard C++, i.e. the stdout of the process, and also a way to parse the info.

Uptil now I am trying to use code from here but am stuck on function read().

like image 882
yolo Avatar asked Jun 11 '11 11:06

yolo


2 Answers

You can simply use the function:

int uname(struct utsname *buf);

by including the header

#include <sys/utsname.h>

It already returns the name & version as a part of the structure:

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

Am I missing something?

like image 66
Alok Save Avatar answered Sep 20 '22 16:09

Alok Save


For recent linux distros you can use following to get the OS info. The output is pretty standard and can be parsed using following spec:

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

cat /etc/os-release

Sample 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"
like image 40
adnan kamili Avatar answered Sep 20 '22 16:09

adnan kamili