added DEBUG_COMPLETION --debug flag, and some debug statements to go with it
use ferror() instead of (incorrectly) errno to catch stdio error