Fix bugs in file output, including buffer overflow

The file output routine was bugged and failed when no parameter was
passed to the program. In addition, a glaring buffer overflow
vulnerability was present in the code used to determine the file name.
This commit fixes these bugs.