views:

81

answers:

6

I need to check the platform I'm on, meaning: processor, operating system, and operating system version. An example I currently have is i686-apple-darwin10. How can I obtain the similar string on Linux and Windows? I know that the autotools provide a detection script that produces that string, but I don't recall its name now, and since it's in bash, it won't work on windows.

A related question is: I've never really got to understand the techniques to detect a platform. Are there standards for this ? For example, "i686-apple-darwin10" is one, but also I saw the uname-uname -p style: "Linux-i386". What are the best-practices, standard strings and preferred context for each platform string?

+1  A: 

There is no standard, cross-platform way to do this, and there is no "standard" string.

uname on Linux/UNIX gets you some information, but generally only about the running kernel and not about the platform (when you're talking Linux, what do you mean by "operating system version"? Kernel version? OS version e.g. RHEL 5.3?). Windows you can interrogate WMI but there's no commandline to give you the info.

Joe
A: 

Running dxdiag in silent mode will give you all kinds of information on windows.

dxdiag /x outfile.xml

Wait for existance of .xml file. All kinds of information.

Stefan Kendall
Why the heck was I downvoted? I've used this trick before to pull out video display modes, among other things.system("dxdiag flags") where flags is the quiet option. It works.
Stefan Kendall
+2  A: 

As the starting point, you need to specify what language you want this detection in.

If you specify "I want a single program that can run on any computer and identify it", then forget it - this won't work. No program file can run on any computer - let alone identifying it. One option would be "use a program that is already known to be on the system", but no single such program is universally available.

Wrt. programming languages, you have the choice between "scripting" and "compiled" (perhaps with "byte-code compiled" in-between). In scripting languages, you need to chose one where the interpreter is already on the system, or where you can accept that the user installs the interpreter first before running your script. That's why Unix shell scripts are so widely used for system identification. They are, of course, restricted to systems that provide /bin/sh.

For compiled languages, you have the choice between compile-time and run-time system identification. Compile time is really common in C, and various compilers have predefined macros that you can use to identify the compiler, or the operating system, or the microprocessor.

For run-time, you need to have system calls/library functions that you can use. Which ones you can use depends on the programming language.

Martin v. Löwis
At least browsers are universally available.
J.F. Sebastian
A: 

I can only help for Linux and Windows. I dont know for other o/s, but probably *nixes is same as Linux.

Version: Windows: ver, Linux: uname -a.

CPU: In Linux use /proc/cpuinfo file, Windows check registery (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ACPI\ search for the one with Class=Processor)

Cem Kalyoncu
+2  A: 

On a GNU/Linux system, you might need a combination of uname and lsb_release.

Below what you can get with uname:

$ uname --help
Usage: uname [OPTION]...
Print certain system information.  With no OPTION, same as -s.

  -a, --all                print all information, in the following order,
                             except omit -p and -i if unknown:
  -s, --kernel-name        print the kernel name
  -n, --nodename           print the network node hostname
  -r, --kernel-release     print the kernel release
  -v, --kernel-version     print the kernel version
  -m, --machine            print the machine hardware name
  -p, --processor          print the processor type or "unknown"
  -i, --hardware-platform  print the hardware platform or "unknown"
  -o, --operating-system   print the operating system
      --help     display this help and exit
      --version  output version information and exit

And now with lsb_release:

$ lsb_release --help
Usage: lsb_release [options]

Options:
  -h, --help         show this help message and exit
  -v, --version      show LSB modules this system supports
  -i, --id           show distributor ID
  -d, --description  show description of this distribution
  -r, --release      show release number of this distribution
  -c, --codename     show code name of this distribution
  -a, --all          show all of the above information
  -s, --short        show requested information in short format
Pascal Thivent
+1  A: 

If python present on the system (it is available for all major platforms) then:

import platform
print platform.platform()

Output:

Linux-2.6.28-15-generic-i686-with-Ubuntu-9.04-jaunty

J.F. Sebastian