add prototypes
replace fputs with fprintf
fix quote escape bug and general improvements