Total Pageviews

Saturday, April 7, 2012

mmap to implement copy command

/*
Program to Implement copy using mmap

*/

#include
#include
#include
#include
#include
#include
#include
#include
struct stat statbuf;

int main(int argc, char *argv[]) {

int fdin, fdout, ret;
char *src, *dest;
if(argc < 3) {
printf("\n Check number of arguments\n");
exit(EXIT_FAILURE);
}
fdin = open(argv[1], O_RDONLY);
if(fdin < 0) {
printf("\n Cant open source file \n");
exit(EXIT_FAILURE);
}

ret=fstat(fdin,&statbuf);
if(ret < 0 ) {
printf("\nfstat system call failed ");
exit(EXIT_FAILURE);
}
printf("\nInpute File size is - %lu ",statbuf.st_size);
src=(char*)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin,0);
fdout = open(argv[2],O_RDWR | O_CREAT | O_TRUNC,S_IRWXU | S_IRWXO);
if(fdout < 0) {
printf("\n Cant open source file");
exit(EXIT_FAILURE);
}
ret=lseek(fdout,statbuf.st_size-1,SEEK_SET);
if(ret < 0)
printf("\n lseek failed ");
ret=write(fdout,"",1);
if(ret < 0)
printf("\n write failed ");
dest=(char *)mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout,0);
dest=memcpy(dest,src,statbuf.st_size);
if(!dest)
printf("\n memcpy failed ");
// msync((void *)dest,statbuf.st_size,MS_ASYNC);
printf("\nFile copied succefully\n");
printf("\n");
}

No comments:

Post a Comment