fileutil: Close fd if passed NULL for output stream

So we don't leak it.