Use echo() instead of fwrite() to support output buffering

This really simple patch changes the `write()` and `writeln()` function to use `echo` instead of `fwrite()`. This is done in order to be able to intercept the output using output buffering and for fetching during unit tests.