/* * reader.c */ #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *file_data; struct stat sb; int fd; char buf[1]; /* open the file */ if((fd = open("test-file", O_RDONLY)) < 0) { perror("open"); exit(1); } /* get file metadata, most especially its size */ if(fstat(fd, &sb) < 0) { perror("fstat"); exit(2); } /* set up the mapping */ file_data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); /* pause */ write(1, "hit enter to output file contents\n", 34); read(0, buf, 1); /* write the data to stdout */ write(1, file_data, sb.st_size); /* pause */ write(1, "hit enter to output file contents again\n", 40); read(0, buf, 1); /* write the data to stdout */ write(1, file_data, sb.st_size); /* clean up after ourselves */ munmap(file_data, sb.st_size); close(fd); }