/* slightly modified to avoid external references */ #include #include #include /* mmap() */ #include #include #include #include #include #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /* default file access permissions for new files */ #ifndef MAP_FILE /* 44BSD defines this & requires it to mmap files */ #define MAP_FILE 0 /* to compile under systems other than 44BSD */ #endif int main(int argc, char *argv[]) { int fdin, fdout; char *src, *dst; struct stat statbuf; if (argc != 3) { fprintf(stderr, "usage: a.out \n"); exit(0); } if ( (fdin = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "can't open %s for reading\n", argv[1]); exit(1); } if ( (fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0) { fprintf(stderr, "usage: a.out \n"); exit(0); } if (fstat(fdin, &statbuf) < 0) { /* need size of input file */ fprintf(stderr, "fstat error\n"); exit(1); } /* set size of output file */ if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) { fprintf(stderr, "lseek error\n"); exit(1); } if (write(fdout, " ", 1) != 1) { fprintf(stderr, "write error\n"); exit(1); } if ( (src = mmap(0, statbuf.st_size, PROT_READ, MAP_FILE | MAP_SHARED, fdin, 0)) == (caddr_t) -1) { fprintf(stderr, "mmap error for input\n"); exit(1); } if ( (dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fdout, 0)) == (caddr_t) -1) { fprintf(stderr, "mmap error for output\n"); exit(1); } memcpy(dst, src, statbuf.st_size); /* does the file copy */ exit(0); }