Linux distro name

This is a discussion on Linux distro name within the Linux Programming forums, part of the Platform Specific Boards category; Hi everybody, I want to get linux distribution name. I googled and found lsb_release Does it a standard way? Does ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    61

    Question Linux distro name

    Hi everybody,
    I want to get linux distribution name. I googled and found lsb_release
    Does it a standard way? Does it installed on linux distributions as default?
    Is there any library in C or C++ to use that or I have to use system(); ?

    And is there any other way?

    Thanks for your consideration

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    Maybe I had to mention this:
    There are /etc/debian_version and /etc/issue files in debian based linux.

    But any standard way to do this in all linux?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    uname is the command.

    But whether there is a system call behind that, I've no idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    /etc/issue is probably on most systems.

    The distro is not that relevant to C programming since all distos are GNU based.

    If you are doing something a certain way and are not sure if it will work on all distros, rather than check the distro just test your method (eg, are the files you presume present, present?). In other words, aim to make it work where ever possible, and fail gracefully where it will not.

    The other issue is that just because a system is based on a given distro does not mean you can rely on it having certain characteristics. A lot can be modified; if you presume that the system is a certain way just because it is "debian", you will easily get caught with your pants down. You must test for the characteristics themselves.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    Thank you MK27
    My program is a service and I should add it to system services and it is distro dependant!!!
    For debian based, redhat based and openSuse !!!!
    I thought that the best way to do that is to get distro name and do the compatible commands for each of them!!!!

    Is there any other way (or standard way to do this on all Linux)?

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    Then test the presence of, say, /etc/init.d /etc/rcx.d, etc.

    I believe that's what MK27 meant by testing for characteristics.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    61

    Smile

    Hi,
    I wrote this function It's not beauty!!! but it can work on most conditions, of course not all. Add it or improve it please. Hope that we can write something useful, other can use and enjoy

    ( I categorized Linux distributions (as my need) in: REDHAT, SUSE and DEBIAN based )

    Code:
    int DistroName()
    {
        //0 redhat, 1 suse, 2 debian, 3 NONE
        system("cat /etc/issue > .distro");
        FILE* fp=NULL;
        fp=fopen(".distro","r");
        char distro[100];
        fscanf(fp,"%s",distro);
        int i=0;
        for(i=0;distro[i]!='\0';i++)
            distro[i]=tolower(distro[i]);
        fclose(fp);
    
        if( access("/etc/redhat-release",F_OK)==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"centos")==0
                            || strcmp(distro,"fedora")==0 )     return 0;
    
        else if( access("/etc/suse_release",F_OK)==0 || strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
            return 1;
    
        else if( access("/etc/debian_version",F_OK)==0 || strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
            return 2;
    
        system("lsb_release -si > .distro");
        fp=fopen(".distro","r");
        fscanf(fp,"%s",distro);
        for(i=0;distro[i]!='\0';i++)
            distro[i]=tolower(distro[i]);
        fclose(fp);
    
        unlink(".distro");
    
        if( strcmp(distro,"centos")==0 || strcmp(distro,"redhat")==0 || strcmp(distro,"fedora")==0 )
            return 0;
    
        else if( strcmp(distro,"opensuse")==0 || strcmp(distro,"suse")==0 )
            return 1;
    
        else if( strcmp(distro,"ubuntu")==0 || strcmp(distro,"debian")==0 )
            return 2;
    
        return 3;
    }
    Last edited by hosseinyounesi; 07-27-2009 at 08:52 AM. Reason: Forget something

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    There are many similar distros that probably have similar directory structures. On the Redhat side, there's SuSE, Mandriva, CentOS, Fedora... and on the Debian side, there's Ubuntu (and it's various variants - kubuntu, xubuntu, edubuntu, etc), knoppix, Xandros, Linspire (or whatever it's called now).... Those are just the ones I can name. There are MANY MANY others. That's why we have been suggesting that it is impractical to test for distributions, and it would be a better idea to test for specific features you need.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    I said that this is according to my need. I just need to 3 sections I mentioned, everyone can add to this function and use more.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Post

    Quote Originally Posted by hosseinyounesi View Post
    I said that this is according to my need. I just need to 3 sections I mentioned, everyone can add to this function and use more.
    Yes, but as I warned you these things can be modified. So if you test for the distro and someone has modified the filesystem structure, which could easily be the case -- for example, some available server set-ups may be different than the desktop set-up and these differences can be more or less extensive -- then what you *think* is supposed to be true just because a system is "redhat" is NOT NECESSARILY SO.

    YOU MUST TEST SPECIFICALLY FOR THE FILES AND OTHER RESOURCES YOU WANT TO WORK WITH. JUST SAYING "OH, LOOK, IT'S DEBIAN" IS A VERY BAD IDEA AND BOUND TO LEAD TO PROBLEMS.

    If you intention is to distribute this or have it used on other computers, anyone with half a brain will take one look at that method, know it for what it is (a very poor hack) and simply not use the software. If you are just writing something for use by yourself, or in an environment like an office where you will always be the one doing the maintenance, *you* might get away with it, but as I said it will not fly further than that this way.

    Plus, you are then saying "oh if another distro could look like this we could add more functions". Again, you are obviously trying to find ways to compensate for *your* laziness by proposing work-arounds for the consequences.

    Learn to use the system properly. It may take you some more time, but it will mean the difference between something that works (properly) and something that works via a series of fortuitous coincidences based on "well it worked for me that one time" etc. Which the last one is quite likely to not work at all when production tested.
    Last edited by MK27; 07-27-2009 at 10:03 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > My program is a service and I should add it to system services and it is distro dependant!!!
    I would suggest you find the source / installer for other common services, and see how they manage it.

    As MK27 says, some half-baked detection of the current OS is going to come unstuck, if there is a more generally accepted way of solving the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jul 2009
    Posts
    61
    Thank you MK27 You are right, but I thought that at least "lsb_release" will work!!!

    This program works in my office, but now I'm going to search more and read some services code to detect the way!!!!

    But I have to say this too:
    most of programs and distributions are in .deb, .rpm and tar.gz files. I think with these extensions the distribution is chosen by the user itself?!

    Thanks again & again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which Linux Distro?
    By CreatedByShadow in forum Tech Board
    Replies: 9
    Last Post: 07-30-2007, 10:28 AM
  2. Linux distro advice
    By Decrypt in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 08-16-2006, 10:42 AM
  3. Looking for a new distro (Linux) to use, any suggestions?
    By Maragato in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 11-29-2004, 12:44 PM
  4. Linux distro
    By c++.prog.newbie in forum Linux Programming
    Replies: 18
    Last Post: 01-22-2004, 12:58 AM
  5. Which Linux distro is better ?
    By pritesh in forum Linux Programming
    Replies: 15
    Last Post: 07-10-2002, 07:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21