copy function using system calls and error checking