Implementation of CP Command Using UNIX System Call


Implementation of CP Command Using UNIX System Call

Implementation of CP Command Using UNIX System Call

#include <stdio.h>
main(int argc,char *argv[])
{
int fd1,fd2,n;
char a;
if(argc<3)
{
printf("Operation is not possible");
}
fd1=open(argv[1],0);
if (fd1==-1)
{
printf("Source is not present");
}
fd2=open(argv[2],1);
if(fd2==-1)
fd2=creat(argv[2],0700);
n=read(fd1,&a,1);
while(n>0)
{
write(fd2,&a,1);
n=read(fd1,&a,1);
}
close(fd1);
close(fd2);
}

Output

-bash-3.2$ cc cp.c
-bash-3.2$ cat>sam.txt
^Z
[4]+ Stopped
cat > sam.txt
-bash-3.2$ ./a.out ex.txt sam.txt
-bash-3.2$ cat sam.txt
hello world!