Use sprintf
Fix a couple bugs

real_filename was sometimes clobbered by subsequent stack usage, and
there was no handling of readlink errors.