Finding OS

This is a discussion on Finding OS within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a C program that outputs the OS of the system. Does C have any ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    10

    Finding OS

    Hi,

    I am trying to write a C program that outputs the OS of the system. Does C have any functions that outputs the system properties. I didnot find any function which does this for me. Can anyone help me with this?

    Chandana.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Nope - C and C++ are written to be OS neutral, so there is no standard way to find out which OS you're actually on.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For Windows (all versions), the command is "ver".

    Code:
    system("ver");
    Your C program must be somewhere in the path to the OS, for this to work. That would include the root of the boot drive, the Documents and Settings, and the Windows folders.

    Linux has a similar command which I can't recall right off the bat.

    Note that these are just OS commands, and not a part of C, itself.

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    For linux

    system ("uname -a");

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    this should work on all unix and unix-likes. i dont know if windows has something similar to this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h> 
    #include <sys/utsname.h>
    
    int main(int argc, char** argv)
    {
    	int SIZE=256;
         char Host[SIZE];
         struct utsname uts;
    
           
      if(gethostname(Host, SIZE) != 0 || uname(&uts) < 0)
      {
         fprintf(stderr, "%s\n",strerror(EINVAL));
                 exit(1);
      }
      else
      {
            
    			 fprintf(stdout,"%s\n%s\n", uts.sysname, uts.machine);
                 fprintf(stdout,"%s\n%s\n",Host, uts.nodename);
                 fprintf(stdout,"%s\n%s\n", uts.release, uts.version);
                 
    			 exit(0); 
            
       }
    	return 0;
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Of course, that means you would have to already know what the OS is in order to use the right command.

    You could check for the presence of certain "system specific commands" (such as ver or uname).

    Also, I think there is some kind of #ifdef that can be used to detect windows, eg I have seen this used:
    Code:
    #ifdef WIN32
    [....]
    #else //assume linux
    [...]
    #endif
    I'm pretty sure that is actually set by the compiler automatically on a win32 machine, but I could be wrong -- you'll have to check yerself.
    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

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by MK27 View Post
    Of course, that means you would have to already know what the OS is in order to use the right command.

    You could check for the presence of certain "system specific commands" (such as ver or uname).

    Also, I think there is some kind of #ifdef that can be used to detect windows, eg I have seen this used:
    Code:
    #ifdef WIN32
    [....]
    #else //assume linux
    [...]
    #endif
    I'm pretty sure that is actually set by the compiler automatically on a win32 machine, but I could be wrong -- you'll have to check yerself.
    each compiler differs in the OS Macro ...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Nevertheless, each platform requires a specific binary and has different APIs and/or commands to find out what OS/version it is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    if i did not have the flu i think i might have waited with posting until i was sure i understood the question right. this works on linux. i think there is a mentioning of this is the k&r book. all compilers should have some sort of macro for this purpose if i understand the book correct. i never use system and if i do its just dummy functions i use it for. i been tolld to use popen instead. thats why i posted this example using this function.

    updated:
    Code:
    #ifdef _WIN32
       #define OS "windows\n"
    #else
        #define OS "unix\n"
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h> 
    #include <sys/utsname.h>
    
    int main(int argc, char** argv)
    {
    	int SIZE=256;
         char Host[SIZE];
         struct utsname uts;
    
          
      if(gethostname(Host, SIZE) == 0 || uname(&uts) < 0)
      {
    	 printf("error this pc is running OS =  %s \n",OS);
             fprintf(stderr, "%s\n",strerror(EINVAL));
                 exit(1);
      }
      else
      {
            
    	     fprintf(stdout,"%s\n%s\n", uts.sysname, uts.machine);
                 fprintf(stdout,"%s\n%s\n",Host, uts.nodename);
                 fprintf(stdout,"%s\n%s\n", uts.release, uts.version);
                 
    			 exit(0); 
            
       }
    	return 0;
    }
    i go back to bed now. i think
    ps sorry if indention fails.
    Last edited by cmay; 08-08-2009 at 11:52 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Not until you fixed the indentation!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    60
    Code:
    #include <errno.h>
    #include <unistd.h> 
    #include <sys/utsname.h>
    ... I wonder if this works under Windows

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by bvkim View Post
    Code:
    #include <errno.h>
    #include <unistd.h> 
    #include <sys/utsname.h>
    ... I wonder if this works under Windows
    it does not i guess. hence the attempt to get back to bed to avoid explaining why i did not read the tread before i posted properly .
    Code:
    #ifdef _WIN32
       #define OS "windows\n"
    #include <windows.h>
    #else
        #define OS "unix\n"
      #include <unistd.h> 
    #include <sys/utsname.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    i ment to say something like this . (i think )
    bedtime now ;=)

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    If you want to know what macros to use...
    Pre-defined C/C++ Compiler Macros
    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.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    10

    Thankyou

    Thanks to all. Your suggestions have helped me a lot.

    Chandana.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting if OS is Windows???
    By Ktulu in forum Windows Programming
    Replies: 2
    Last Post: 11-19-2006, 02:49 AM
  2. a simple OS
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-06-2004, 11:47 PM
  3. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 10:06 AM
  4. Linux OS to Windows OS code
    By sw9830 in forum C Programming
    Replies: 2
    Last Post: 02-28-2003, 03:11 PM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM

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