A solution for searching PATH environ variables
A solution for searching PATH environ variables
- Subject: A solution for searching PATH environ variables
- From: Leigh Smith <email@hidden>
- Date: Tue, 4 Feb 2003 16:07:11 -0500
Since I wasn't exactly overwhelmed with responses when I asked for PATH
searching for use with NSTask, I took my own advice and wrote the
required function :-)
/*!
@function Math_FindExecutableInSearchPath
@param executableName The name of the executable to find.
@discussion Performs PATH environment variable searching
that execve() provides, similar to the use of "which".
While NSBundle would seem to do the job if the tool resided within an
application, this routine searches for unix binaries that may sit in
/usr/bin,
/usr/local/bin, /sw/bin (fink), /fink/bin (for perverse fink
installations), or /just/about/anywhere.
Beware that the idea behind this function is that the user can control
the binary that is retrieved and can therefore potentially Trojan (i.e
substitute) an executable, perhaps creating security breaches.
Therefore only use this to retrieve executables that are not Apple
supplied, do not perform a security function and are of an
non-destructive nature. Be careful!
*/
NSString *Math_FindExecutableInSearchPath(NSString *executableName)
{
// Obtain PATH from the environment NSDictionary obtained from
NSProcessInfo
NSDictionary *environ = [[NSProcessInfo processInfo] environment];
NSString *searchPath = [environ objectForKey: @"PATH"];
NSFileManager *fm = [NSFileManager defaultManager];
NSScanner *searchPathScanner = [NSScanner scannerWithString:
searchPath];
NSCharacterSet *pathSeparator = [NSCharacterSet
characterSetWithCharactersInString: @":"];
while(![searchPathScanner isAtEnd]) {
NSString *candidateDirectoryComponent;
NSString *candidateFileName;
// Split searchPath apart at each ':' character
[searchPathScanner scanUpToCharactersFromSet: pathSeparator
intoString:
&candidateDirectoryComponent];
// Eat the separator
[searchPathScanner scanCharactersFromSet: pathSeparator
intoString: nil];
// Use NSFileManager to test the location of each concatenation
of candidate directory
// and executable name for executability.
candidateFileName = [candidateDirectoryComponent
stringByAppendingPathComponent: executableName];
if([fm isExecutableFileAtPath: candidateFileName])
return candidateFileName;
}
return nil;
}
You'll also find a copy of the function within the MathKit
(
http://mathkit.sourceforge.net) which is where it was first needed (to
fire up gnuplot), hence it NSBundle couldn't help. Now, next question,
how do we get some version of this into Foundation? :-) I mean, even
DOS has the concept of a search path for executables, it's quite
foundational in nature...
--
Leigh Smith
mailto:email@hidden
http://www.leighsmith.com
[demime 0.98b removed an attachment of type text/directory which had a name of Leigh Smith.vcf]
_______________________________________________
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.