#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int fd1 = -1;
int fd2 = -1;
unsigned char tmp = 0;
if(argc != 3)
{
printf("The command should be ./mycp file1 file2\n");
return 1;
}
fd1 = open(argv[1],O_RDONLY);
if(fd1 < 0)
{
printf("Open file %s failed\n",argv[1]);
return 2;
}
fd2 = open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666);
if(fd2 < 0)
{
printf("Open file %s failed\n",argv[2]);
close(fd1);
return 3;
}
while(1 == read(fd1,&tmp,1))
{
write(fd2,&tmp,1);
}
close(fd1);
close(fd2);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int fd1 = -1;
int fd2 = -1;
unsigned char tmp = 0;
if(argc != 3)
{
printf("The command should be ./mycp file1 file2\n");
return 1;
}
fd1 = open(argv[1],O_RDONLY);
if(fd1 < 0)
{
printf("Open file %s failed\n",argv[1]);
return 2;
}
fd2 = open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666);
if(fd2 < 0)
{
printf("Open file %s failed\n",argv[2]);
close(fd1);
return 3;
}
while(1 == read(fd1,&tmp,1))
{
write(fd2,&tmp,1);
}
close(fd1);
close(fd2);
return 0;
}