On Jan 15, 2010, at 7:23 AM, Gary McGill wrote: I am trying to pass a variable to system(). The final code would look like this: system(“lp –d Internal_Modem –o phone=555-5555 ~/Desktop/fileToFax.pdf”)
If I simply put this line in my code, it works fine. The problem is I have to get the phone number from the database. When I build the string using AppendString [...] Earlier replies have addressed your immediate problem, but I want to recommend that you not call system().
(1) Assembling command-line strings is messy, because you have to deal with issues of shell quoting. In your case, can a phone number contain a "*" or "#"? Either one will confuse the shell and make the command fail. In general this can be a nightmare to deal with. In the worst case, if you don't trust the data source, this can allow hackers to exploit your app to run arbitrary code (e.g. passing in a phone number like "0; rm -rf ~;") — this has been the cause of many, many, many attacks on websites and other systems.
(2) You don't know which shell is going to run the command — probably bash, but some people (like me) use tcsh, and there's also zsh. All have subtle differences in the way they parse commands.
(3) Launching a shell has overhead. Probably not an issue in your case, but if you call this a lot it can be noticeable.
(4) Why use a Unix system call when there are friendly Cocoa classes that do the same thing?
What you should be using instead is NSTask. It will let you pass the arguments as individual NSStrings (so no C-string conversion or concatenation to do), they don't get run through a shell or otherwise parsed (so no worries about quoting), and it directly launches the binary (so better performance.)
—Jens |