Even better emulate POSIX file functions (reduced)

This is reduced version of #5694.
Only obvious fixes.
According to POSIX specifications, `int` should be casted to `unsigned char`. So this will change the behavior.
For clarity, I can change array to unsigned chars, but it doesn't matter (result will be the same).