Implementation of output-files.
This also implements the programmatic interface output-files-prog. The flag progp, passed to some of the implementation functions below, says whether the programmatic interface has been called.