Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get OS name with C [Linux, portable for distros: Centos, Debian, Fedora, OpenSUSE, RedHat, Ubuntu]

I know I can check my OS name with this simple command: lsb_release -ds. But I also know, that its not portable on all platforms where I need it. I tried struct utsname info; and uname(&info) and it works great but gives me only "base" name - "Linux".

Is there any portable (C) way of getting full OS name? Portable between Centos, Debian, Fedora, OpenSUSE, RedHat, Ubuntu at least? Cheers

like image 746
Brian Brown Avatar asked Sep 12 '13 17:09

Brian Brown


People also ask

How do I know if I have CentOS or Ubuntu?

CentOS vs Ubuntu – Main Differences The biggest difference between the two Linux distributions is that Ubuntu is based on the Debian architecture while CentOS is forked from Red Hat Enterprise Linux. In Ubuntu, you can download DEB packages using the apt-get package manager.


1 Answers

Here is the C code that says the name of the OS. You can also edit the code for other various purpose, by using the same logic.

#include<stdio.h>

int main()
{
    FILE *fp;
    char buffer[50] = " ";
    fp = popen("lsb_release -ds", "r");
    fgets(buffer, 50, fp);
    pclose(fp);
    printf("Name of the OS is : %s",buffer);
    return 0;
}
like image 135
Megharaj Avatar answered Oct 08 '22 11:10

Megharaj