cgi: remove charwrite code duplication