#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#define BUFSIZE 4096
#define COPYMODE 0644
void do_cp(char*,char*);
int main(int ac,char* argv[])
{
char *SRC,*DST;
if(ac==1){
printf("cp: missing file operand\n");
exit(0);
}
if(ac==2){
printf("cp: missing destination file operand after '%s'\n",argv[1]);
exit(0);
}
if(ac==3){
SRC=argv[1];
DST=argv[2];
}
do_cp(SRC,DST);
return 0;
}
void do_cp(char* src,char* dst)
{
int fd_RD,fd_WR,nchars;
char buf[BUFSIZE];
fd_RD=open(src,O_RDONLY);
fd_WR=creat(dst,COPYMODE);
while((nchars=read(fd_RD,buf,BUFSIZE))>0){
write(fd_WR,buf,nchars);
}
}