#include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "usage: %s from_file to_file", argv[0]); exit(0); } /* from */ int from = open(argv[1], O_RDONLY, 0644); assert(from >= 0); struct stat st_buf; assert(fstat(from, &st_buf) >= 0); size_t size = st_buf.st_size; void *from_mmap = mmap(NULL, size, PROT_READ, MAP_SHARED, from, 0); assert(from_mmap >= 0); #if USE_MADVISE assert(madvise(from_mmap, size, MADV_SEQUENTIAL) >= 0); #endif /* to */ int to = open(argv[2], O_CREAT | O_WRONLY, 0666); assert(to >= 0); /* copy */ char *p = from_mmap; const char * const endp = from_mmap + size; while (p < endp) { int num_bytes = write(to, p, endp - p); p += num_bytes; } assert(p == endp); fsync(to); close(to); close(from); return 0; }