add common function readn and writen