Re: Finding the executable path in a foundation tool.
Re: Finding the executable path in a foundation tool.
- Subject: Re: Finding the executable path in a foundation tool.
- From: Malte Tancred <email@hidden>
- Date: Wed, 9 Jul 2003 12:04:30 +0200
James Quick wrote:
On Tuesday, July 8, 2003, at 06:21 PM, Prachi Gauriar wrote:
How about the old-fashioned C way of just looking at argv[0]?
Contents of main.m:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *executablePath = [NSString
stringWithUTF8String:argv[0]];
NSLog(@"%@\n", executablePath);
[pool release];
return 0;
}
This will only work if the excutable was execed using a full path. A
partial pathname
may not work. You also need to examine each element in the environment
search
path. That, too, may fail in certain instances if the process has
changes its current
working directory.
Just made a quick test:
[prune:~/Code/Temporary/findpath] malte% cat findpath.m
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"CWD: %@", [[NSFileManager defaultManager]
currentDirectoryPath]);
NSLog(@"Process name: %@", [[NSProcessInfo processInfo]
processName]);
NSLog(@"First argument: %@", [[[NSProcessInfo processInfo]
arguments] objectAtIndex:0]);
NSLog(@"First argument: %@", [NSString
stringWithUTF8String:argv[0]]);
[pool release];
exit(0);
return 0;
}
[prune:~/Code/Temporary/findpath] malte% make
cc -o findpath findpath.m -framework Foundation
[prune:~/Code/Temporary/findpath] malte% ./findpath
2003-07-09 11:55:13.336 findpath[546] CWD:
/Users/malte/Code/Temporary/findpath
2003-07-09 11:55:13.346 findpath[546] Process name: findpath
2003-07-09 11:55:13.349 findpath[546] First argument:
/Users/malte/Code/Temporary/findpath/findpath
2003-07-09 11:55:13.350 findpath[546] First argument: ./findpath
It seems NSProcessInfo is doing the thing for you. Anyone know if
NSProcessInfo's behaviour is reliable? The documentation doesn't say.
If NSProcessInfo's behaviour is not reliable, wouldn't it be easier to
simply use the CWD, append the first argument (argv[0]) and then use
NSString's -stringByStandardizingPath ?
Just a few thoughts.
Cheerio,
Malte
--
Malte Tancred
Computer Programmer
Oops AB,
http://oops.se/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.