<p>I'm writing a simple program to browse the local network and pass on filenames to mplayer using "system". However, sometimes filenames contain spaces or quotes.
Obviously I could write my own function to escape those, but I'm not sure exactly what characters do or do not need escaping.</p>

<p>Is there a function available in the CRT or somewhere in the linux headers to safely escape a string to pass to the command line ?</p>

While i don't know a function that does this, you can surround each of your arguments with
```
'...'
```
, and replace any
```
'
```
in the original argument by
```
'"'"'
```
. like
```
system("mplayer 'foo'\"'\"' bar'");
```
will give a single argument to mplayer which is
```
foo
```
'
```
bar
```
and which is allowed to contain strange things like
```
"
```
or
```
\n
```
. Note the escaping before
```
"
```
above (
```
\"
```
) is only to make it valid C++.
You should consider using a function that accepts the arguments each separate, thus avoiding such issues. Wikipedia has a good article on this about the famous fork-and-exec pattern.
http://en.wikipedia.org/wiki/Fork-exec