Avoid reimplementing basename.

Problem spotted by Peter Kessler.
Use a temporary directory rather than possibly unsafe temporary files.