Question

When a C program written in the conventional int main(int argc, char* argv[]) style is launched from the Finder, it starts in the home directory of the user who launched it. (e.g. /Users/Fork).

Is there any fancy hook by which such a terminal-based program can know what directory it was launched in?

Was it helpful?

Solution

You can obtain the full path for the program's binary image via _NSGetExecutablePath. Canonicalise that using e.g. realpath and then strip off the filename using e.g. dirname and you have the directory the program was started from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top