• 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: How to pass filenames to system( ) ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to pass filenames to system( ) ?


  • Subject: Re: How to pass filenames to system( ) ?
  • From: Gregg Wonderly <email@hidden>
  • Date: Wed, 03 Mar 2010 13:01:32 -0600

Patrick Stadelmann wrote:
Quinn wrote:

If you have to use system(), you will run into various gotchas like the one you described. You will have to parse each path and apply quoting. AFAIK there's no C-language routine to do this, but it's not too hard to do. I know I've written this code before, but I wasn't able to unearth it, alas. The basic idea is:

1. go through every byte of the string

2. if the byte is a reasonable value, pass it through unquoted

3. otherwise quote the byte by prefixing it with slosh ('\')

The exact definition of reasonable is tricky but, to be on the safe side, you can just say that the alphanumerics and underscore are reasonable, and everything else is not.

Oh, cool, I finally managed to track down my code.

Thanks for your answer. In the meantime, I tried escaping every single character, and it seems to also work. Simpler, but less memory efficient.

I didn't choose to use system(), I'm trying to modify the cups-pdf
tool (a virtual printer that enable batch printing to PDF, without
going through the print dialog), which use only portable functions.
The call to system() is used to invoke the .ps to .pdf interpreter,
something that could probably also be done differently on Mac OS X.

Surely fork(2) and execl(2) would be practical because then each argument is an element of the array passed to execl(2) and no quoting is necessary?


if( !(pid = fork() ) ) {
	execl( pathToInterp, pathToInterp, arg1, arg2, arg3, NULL );
	perror( "execl of interpreter failed");
	exit( 3 );
}
int serrno = errno;
int result = -1;
if( pid > 0 ) {
	result = wait(pid);
}

If you need pipes, use pipe(2) and just connect all the processes together. Less "easy" than system(3), but a whole lot more controllable in terms of avoiding quote problems in file names.

Gregg Wonderly
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Filesystem-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How to pass filenames to system( ) ? (From: Patrick Stadelmann <email@hidden>)
 >Re: How to pass filenames to system( ) ? (From: Quinn <email@hidden>)
 >Re: How to pass filenames to system( ) ? (From: Patrick Stadelmann <email@hidden>)

  • Prev by Date: Re: How to pass filenames to system( ) ?
  • Next by Date: filtering access to Windows disks
  • Previous by thread: Re: How to pass filenames to system( ) ?
  • Next by thread: Re: How to pass filenames to system( ) ?
  • Index(es):
    • Date
    • Thread