Re: NSTask vs. special characters
Re: NSTask vs. special characters
- Subject: Re: NSTask vs. special characters
- From: Rosyna <email@hidden>
- Date: Sun, 29 Apr 2007 17:33:31 -0700
Ack, at 4/29/07, Pierre Bernard said:
Got it to work:
NSString *filePath = [[destPath shellSafeString]
fileSystemString];
How did you possibly get this to work? NSTask takes a list of
arguments. These arguments do not need to be shell escaped unless
your NSTask is a shell (ie, tsch, bash, et cetera). If your NSTask is
a shell, that's very, very bad. But you could just enclose the string
with quotes if you're doing such a thing.
Enclosing in quotes is only needed if there will be shell
substitution done, which is only done by the shell.
- (NSString*) shellSafeString
{
NSMutableString *string = [self mutableCopy];
// Unsafe
[string replaceOccurrencesOfString:@"\\" withString:@"\\\\"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"?" withString:@"\\?"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"*" withString:@"\\*"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"'" withString:@"\\'"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"`" withString:@"\\`"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"$" withString:@"\\$"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"&" withString:@"\\&"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"|" withString:@"\\|"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
// White space
[string replaceOccurrencesOfString:@" " withString:@"\\ "
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\a" withString:@"\\a"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\t" withString:@"\\t"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\n" withString:@"\\n"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\f" withString:@"\\f"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\r" withString:@"\\r"
options:NSLiteralSearch range:NSMakeRange(0, [string length])];
return string;
}
This above code leaks. And the replacements aren't necessary when not
using a shell or quoting things.
- (NSString*) fileSystemString
{
NSString *string = [NSString stringWithFormat:@"%s", [self
fileSystemRepresentation]];
return string;
}
Here's the big problem... IIRC, %s interprets the string as the
system encoding. (Which would be either MacFrench or MacRoman in your
case). But -fileSystemRepresentation returns a "UTF-8" string. So
anything that doesn't have the exact same value (such as French
accents) in both encodings are going to get changed and mangled and
you'll get a file not found error.
There's no reason at all to do string with format. You have the
string as an NSString, there's no reason to convert it to anything
else.
--
Sincerely,
Rosyna Keller
Technical Support/Carbon troll/Always needs a hug
Unsanity: Unsane Tools for Insanely Great People
It's either this, or imagining Phil Schiller in a thong.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden