#include<myhead.h>
int copy_file(const char *file,const char *file1,int start,int len);
int get_len(const char *file)
{
int len;
int fd = open(file,O_RDONLY);
if(fd==-1){
perror("open");
return -1;
}
len = lseek(fd,0,SEEK_END);
close(fd);
return len;
}
void *fun1(void *para)
{
const char **argv=(const char **)para;
int len = get_len(argv[1]);
copy_file(argv[1],argv[2],0,len/2);
pthread_exit(NULL);
}
void *fun2(void *para)
{
const char **argv=(const char **)para;
int len = get_len(argv[1]);
copy_file(argv[1],argv[2],len/2,len-(len/2));
pthread_exit(NULL);
}
int copy_file(const char *file,const char *file1,int start,int len)
{
int fd = open(file,O_RDONLY);
if(fd==-1){
perror("open");
return -1;
}
int fd1 = open(file1,O_WRONLY);
if(fd1==-1){
perror("open");
return -1;
}
lseek(fd,start,SEEK_SET);
lseek(fd1,start,SEEK_SET);
char buff[1024];
int res;
int sum=0;
while((res=read(fd,buff,sizeof(buff)))!=0)
{
sum+=res;
if(sum>=len||res==0)
{
write(fd1,buff,res-(sum-len));
break;
}
write(fd1,buff,res);
}
close(fd);
close(fd1);
}
int main(int argc, const char *argv[])
{
if(argc!=3)
{
perror("extern argument ");
return -1;
}
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,fun1,(void*)argv)!=0)
{
perror("create");
return -1;
}
if(pthread_create(&tid2,NULL,fun2, (void*)argv)!=0)
{
perror("create");
return -1;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}