Add error checking to patch with stderr display on errors

Each of three points where errors can be detected will now generate an
appropriate error message on stderr.

Also changed to using the asprintf method for dynamically generated strings.