views:

354

answers:

5

I am working tools that will be used on the multiple OS and platform. This tools will provide the details of the OS on which it is running, like 32 bit or 64 bit OS, exact version of Linux,Solaris,or other OS.

One way I am thinking of using the "uname -a" command to extract the OS information on the Linus/Unix based OS. Please suggest me that if it is efficient to call the command from the program.

Please let me know if there is any api available or there is any workaround that I can implement.

TIA anil

A: 

I would think that uname -a would be a good way, but only for *nixes.

Do you need to check OS info in Windows?

sheepsimulator
yes, I need to do check for OS info on the Windows also.
anil
+3  A: 

Why don't you simply wrap the OS detection with #define and each time call the ideal function so as to return a meaningful string in every case?

Some examples of system function to get information about the current OS (such as the running version):

Edouard A.
Because you have to know all the OSs in advance in order to write the #defines.
anon
we can write OS info for OSes inside #define. But still we need some api to get OS information inside #define.
anil
Well of course you know all the OSes in advance, you're compiling for each one of them!Windows : GetVersionExBSD : uname(3) - #include <sys/utsname.h>No need to spawn a process. ;)
Edouard A.
In that case, why call the function to determine the OS you already know? I was assuming at least some of the binaries would be cross platform, or the question doesn't make sense.
anon
A cross platform binary in C++? The goal is in my opinion to have some sort of console get what os the binary is running on. Let's say you have a grid of computers and want to know what os is running on each one of them. That's the sort of problem you want to address. You want to know the OS and the version, hence you modify for each platform.
Edouard A.
Cross platform in sense of running on various Linux flavours, various Windows flavours etc.
anon
In which case the above approach works very well. One define for Windows, one define for Linux, one for BSD, one for solaris... Maybe you can even unify some of the nixes.
Edouard A.
+2  A: 

The C++ Standard Library provides no way of doing this, so you are at the mercy of operating system specific features. And even then, there is no general method, as utilities like "uname" are not supported on Windows and will give differently formatted results on different UNIX flavours.

anon
yeah, Neil you have the right concern but we can cover major OSes like linux, solaris, AIX and windows and can put all other OSes in undefined kind of category.
anil
+1  A: 

You can give a try to sysinfo() call (from sys/systeminfo.h on unix)

Also regarding "uname -a" efficiency , it depends where are you going to use it. In normal circumstances ( if your application is not time/cpu critical ) performance of "uname -a" should be acceptable.

There is also a function "uname()" present in sys/systeminfo.h file, which may help you.

nurxb01
Is there any similar api for windows?
anil
Well i don't think , same functions are there on windows but you can try "GetVersionEx()" windows api
nurxb01
A: 

There is no standard way to do this . But you can apply fingerprinting techniques to find out the target OS.

These are generally used to guess remote machine OS. You can look at nmap also, it does various types of fingerprintings.

Alien01
can you provide some links for it? It would be really helpful.
anil
nmap is opensource tool.You can download from net but will take some effort to figure out how its done. You can try searching fingerprinting techniques on net. There are lot of resources available. However you have to do some research on it.
Alien01