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.
Printable View
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.
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.
For Windows (all versions), the command is "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.Code:system("ver");
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.
For linux
Quote:
system ("uname -a");
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;
}
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:
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.Code:#ifdef WIN32
[....]
#else //assume linux
[...]
#endif
Nevertheless, each platform requires a specific binary and has different APIs and/or commands to find out what OS/version it is.
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:
i go back to bed now. i think :)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;
}
ps sorry if indention fails.
Not until you fixed the indentation!
... I wonder if this works under Windows :DCode:#include <errno.h>
#include <unistd.h>
#include <sys/utsname.h>
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 .
i ment to say something like this . (i think )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>
bedtime now ;=)
If you want to know what macros to use...
Pre-defined C/C++ Compiler Macros
Thanks to all. Your suggestions have helped me a lot.
Chandana.