close input as soon as possible
use stat() instead of fopen() to check if file exists already
removed temp file usage for optimization and added stdin/stdout support