tags:

views:

66

answers:

4

Hi, I was wondering how you could detect what platform a program is running on, I have an application and need it to go to a certain folder depending on whether it is on a Linux or Mac machine.

Thanks!

A: 

If you know you're Unix, running 'uname' will tell you quite a bit about the system. if you need to know more. Of course, that's relying on an external executable.

There should be environmental variables you can check, but I'm not sure which ones.

CWF
+1  A: 

You will probably need to compile the program specifically for Mac or Linux, so you can use some sort of preprocessor directive. This list of macros may be helpful.

Justin Ethier
+1  A: 

The uname() system call will tell you about the version of the OS

Martin Beckett
uname is a syscall now?
Mustapha Isyaku-Rabiu
A lot of unix commands are also system calls, do "man 3 blah" to lookup command "blah" in the system library
Martin Beckett
+1  A: 

It may break in the future, but for now, you can play on a large number of filesystem differences between both.

  • /Applications, /Developer, /Library and others are mac-specific. If you have them, it looks like a mac.
  • /proc, /home, /srv and others are linux-specific. If they are there, it looks like a linux machine. See full list.

If you combine several of these path-checking tricks into a function, you can insulate yourself against any one of the tricks failing independently of the others.

You can check for folder existence by using stat.

struct stat st;
if(stat("/proc",&st) == 0) {
    printf(" /proc is present: this may be Linux\n");
}
tucuxi