MOHAMED
MOHAMED

Reputation: 43518

in the code (.c file) how I can find the linux distribution name version

I'd like to know in the code (.c file) how I can find the linux distribution name version (like ubuntu 10.0.4, or centOS 5.5...)? The c function that I'm looking for should be like the uname() system call used in (.c files) to get kernel version. it will be appreciated that the function is working for all linux distribution (standard)
I 'm not looking to get distribution name and version by the use of command line linux from code (.c file) (like the use of system("cat /etc/release");).

Any suggestion will be appreciated!

Regards

Upvotes: 0

Views: 3259

Answers (7)

Simone Lazzaris
Simone Lazzaris

Reputation: 1

I usually inspect /etc/issue; while (as others pointed out) it is not guaranteed, I've fount in the field that's quite reliable. As far as I've experienced, it works on ubuntu, debian, redhat, centos, slackware and archlinux.

Upvotes: 0

Offirmo
Offirmo

Reputation: 19840

There is no portable way to do that, you'll have to use some OS detection tool/library.

Fortunately, there are a few out there. I know those 2 :

(I used facter via puppet and it is very good.)

With a little additional scripting, you can use one of those program's output to generate a .h that you can then use in your code.

You can even integrate this generation as a step in your makefile.

Upvotes: 0

Arun
Arun

Reputation: 20383

Is it acceptable to run some shell commands?

$ /usr/bin/lsb_release -r
Release:        11.04

$ /usr/bin/lsb_release -d
Description:    Ubuntu 11.04

$ /usr/bin/lsb_release -rd
Description:    Ubuntu 11.04
Release:        11.04

Upvotes: 0

NPE
NPE

Reputation: 500167

You could fopen("/etc/lsb-release") and parse its contents. It looks like this:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.3 LTS"

This method is not universal. You'll need to make sure that it works on all distros that you care about (if it doesn't, I suggest you go with @ott--'s answer).

Upvotes: 1

ott--
ott--

Reputation: 5702

There is no standard for this yet. You can query following files or check for existence:

/etc/lsb-release
/etc/issue
/etc/*release
/etc/*version

Upvotes: 3

Martin Beckett
Martin Beckett

Reputation: 96119

AFAIK there isn't a standard system call to get this if uname(2) doesn't give you enough info.
Safest approach is probably to check for "/proc/version" and read that

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283624

Well, you can (and should) use fopen and fgets instead of system("cat"), for reading /etc/release.

There's no universal method though, I can even build a linux image which has no filesystem at all (except initramfs) and definitely no distribution name.

Upvotes: 1

Related Questions