• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Translating filenames for command line?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Translating filenames for command line?


  • Subject: Re: Translating filenames for command line?
  • From: Andreas Monitzer <email@hidden>
  • Date: Tue, 1 Jan 2002 14:47:51 +0100

On Tuesday, January 1, 2002, at 02:21 , Scott Anguish wrote:

On Monday, December 31, 2001, at 02:55 PM, Andreas Monitzer wrote:

On Monday, December 31, 2001, at 08:38 , Finlay Dobbie wrote:

That was just a bog-standard badly written shell script, nothing to do with system().

system("rm -rf /Volumes/Harddisk 1/Applications/iTunes.app");

yeah, right.

It would have been the same problem if they'd called it via NSTask (and that's more likely how that script was called...)

System() wasn't the culprit there... lack of testing was


I don't think you get the problem. I'm not talking about launching a shell script, I'm talking about running the actual tool using ObjC:

[NSTask launchedTaskWithLaunchPath:@"/bin/rm" arguments:[NSArray arrayWithObjects:@"-rf",@"/Volumes/Harddisk 1/Applications/iTunes.app",nil]];

does the expected thing. The problem is that spaces are used for splitting the argument string, but are also part of the filename when using a shell. NSTask (and exec(3)) don't use a shell.

This might be pretty clear to you, but consider the following code:

void doit(void) {
NSString *tool=[[NSBundle mainBundle] pathForResource:@"mytool" ofType:nil];
char command[512];

snprintf(command,sizeof(command),"%s DoWhatIWant",[tool fileSystemRepresentation]);

system(command);
}

This code works fine as long as there is no space in the filename of mytool. On the other hand, this code:

void doit(void) {
NSString *tool=[[NSBundle mainBundle] pathForResource:@"mytool" ofType:nil];

[NSTask launchedTaskWithLaunchPath:tool arguments:[NSArray arrayWithObject:@"DoWhatIWant"]];
}

works fine no matter where the tool is.


andy


  • Follow-Ups:
    • Re: Translating filenames for command line?
      • From: Scott Anguish <email@hidden>
References: 
 >Re: Translating filenames for command line? (From: Scott Anguish <email@hidden>)

  • Prev by Date: Re: Translating filenames for command line?
  • Next by Date: Re: Translating filenames for command line?
  • Previous by thread: Re: Translating filenames for command line?
  • Next by thread: Re: Translating filenames for command line?
  • Index(es):
    • Date
    • Thread