correctly handle write errors encountered by printf-family functions