On Saturday, April 17, 2004, at 02:17 , Gordon Henriksen wrote:

On Thursday, April 15, 2004, at 02:25 , Jeff Clites wrote:

For Unix platforms at least, you should be able to do this:

executablePath = isAbsolute($0) ? dirname($0) : cwd().dirname($0)

That absolutely does not work, as already pointed out. Ths looks like a reasonable reference implementation (LGPL), though:


        http://www.opensource.apple.com/darwinsource/10.3/libiconv-9/libiconv/srclib/
progreloc.c

On Windows and Linux, it uses Win32 and /proc to provide a robust implementation. Otherwise, it guesses by looking at $0 and $ENV{PATH}.

My guess is that there's a more reliable (and non-portable) way to do this on Mac OS X, since Carbon applications need to reliably open the resource fork of the executable.

Ah! Indeed there is.


http://developer.apple.com/documentation/Carbon/Reference/Process_Manager/
index.html

And, indeed, it is witheringly non-portable.

CFDictionaryRef dict = ProcessInformationCopyDictionary(
kCurrentProcess, kProcessDictionaryIncludeAllInformationMask);
CFString cfPath = (CFString *) CFDictionaryGetValue(dict, kIOBundleExecutableKey);
CFIndex length = CFStringGetMaximumSizeForEncoding(cfPath, kCFEncodingUTF8);
char *path = (char *) malloc(length + 1);
CFStringGetCString(cfPath, path, length + 1, kCFEncodingUTF8);
CFRelease(dict);


Ahem.

I'm sure the ProcessInformationCopyDictionary API is implemented in terms of something sane at the Darwin level, but God only knows what it is.



Gordon Henriksen
[EMAIL PROTECTED]

Reply via email to