/*To implement the CP command:*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<fcntl.h>#include<string.h>intMainintargcChar*argv[]) { intRD_FD, WR_FD;//read file descriptors and write file descriptors Charbuf[ -]={0}; intRd_ret =0; if(ARGC <3) {printf ("Please input src file and des file!\n"); return-1; } //Open source FileRD_FD = open (argv[1], o_rdonly); if(Rd_fd <0) {printf ("open src file%s failed!\n", argv[1]); return-2; } printf ("open src file%s succ, rd_fd =%d\n", argv[1], RD_FD); //Open the destination fileWR_FD = open (argv[2], o_wronly|o_creat); if(Wr_fd <0) {printf ("Open des file%s failed!\n", argv[2]); return-3; } while(1) {Rd_ret= Read (RD_FD, buf, -); if(Rd_ret < -)//determine if the data has been read { Break; } write (WR_FD, buf, Rd_ret); memset (BUF,0, -);//clean up the cache} write (WR_FD, buf, Rd_ret);//do one last write//Close File DescriptorClose (WR_FD); Close (RD_FD)return 0;}
Simple implementation of the Linux CP command