Improve closing files using pclose() which are opened by popen().

Pavan Deolasee provided the fix.