5

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

Brian Brown
  • 3,873
  • 16
  • 48
  • 79
  • Why exactly do you ask? Can't you just use `uname` syscall? Why? (in other words, why is it so important to guess from a *C* program if your Linux is Debian or Mint or Gentoo) – Basile Starynkevitch Sep 12 '13 at 17:20
  • Also see [Best way to find os name and version in unix/linux platform](http://stackoverflow.com/q/26988262/608639). – jww Mar 10 '17 at 15:04

4 Answers4

3

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;
}
Megharaj
  • 1,589
  • 2
  • 20
  • 32
3

LSB in name lsb_release states for "Linux Standard Base", but doesn't so standard.

I came to the following code: https://github.com/myaut/tsload/blob/master/agent/lib/libhostinfo/plat/linux/uname.c

It checks:

  • /etc/oracle-release for Oracle Enterprise Linux
  • /etc/redhat-release for RHEL other derivatives
  • /etc/SuSE-release for SuSE derivatives
  • lsb_release output for any other Linux

It also cuts out irrelevant words i.e. CentOS release 6.3 (Final) -> CentOS 6.3

myaut
  • 11,174
  • 2
  • 30
  • 62
2

You could try to use popen(3) to run and read the output of command /usr/bin/lsb_release -ds. If that fails, you might read and parse /etc/issue

Notice that clever or paranoid sysadmins might edit and configure that /etc/issue file at will.

But I don't understand why you want to know all that (from inside a C program), in other words, why the uname syscall is not enough.

In particular, there are a lot of Debian (or Redhat) derived specialized distributions, and I don't understand why the name of the particular distribution would matter to a C program. It matters much more for packaging purposes....

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • yup, I used `popen` for this but Im concered about oses that dont have `lsb_release` :) Is `/etc/issue` availible on all OSes I asked about? – Brian Brown Sep 12 '13 at 17:20
  • Ok, I should choose an easier way, I will cat `/etc/issue` ;) But is it availible on EVERY OS I asked about? – Brian Brown Sep 12 '13 at 17:22
  • maybe the c program doesn't care at all... its the user that's being catered to. – James Morris Sep 12 '13 at 17:34
  • 1
    I have recently seen a system where /etc/issue was indeed heavily edited, and didn't contain any OS information any more. Not sure if /etc/issue.net was edited as well; it might be a viable fallback for you. Also, Debian/Ubuntu systems appear to have a /etc/debian_version file which you could consult. – oliver Sep 12 '13 at 21:13
1

The uname system call gives you the generic system type (Linux in all your cases) in the sysname field, but it also gives you additional data in the release, version, and machine fields. The release field will give you the kernel version, and the version field will give you the general system version, which will be different for all the various linux variants you mention.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226