2

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!

2
  • Probably related: stackoverflow.com/questions/157759/… Commented Mar 30, 2010 at 1:56
  • 2
    I would use #ifdef macros. #ifdef _WIN32 #include <windows.h> #endif Commented Mar 30, 2010 at 2:41

4 Answers 4

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

1 Comment

A lot of unix commands are also system calls, do "man 3 blah" to lookup command "blah" in the system library
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.