#include #include #include #include #include #include #include /* * mmcat.c - cat a file using mmap * * idea: no read or lseek, just mmap and *p * * system calls used: * open - to get a fd for a file * mmap - to map file to memory */ #define oops(m,x) { perror(m); exit(x); } main(int ac, char *av[]) { int fd; char *region; char *p; off_t filesize; struct stat info; int rv; off_t pos = 0; int not_printed; long pagesize; if ( ac != 2 ){ fprintf(stderr, "usage: mmcat filename\n"); exit(1); } if ( stat(av[1], &info) == -1 ) oops(av[1], 2); filesize = info.st_size; pagesize = sysconf(_SC_PAGESIZE); if ( (fd = open(av[1],O_RDONLY)) == -1 ) oops(av[1], 3); region = (char*)mmap(NULL,pagesize,PROT_READ,MAP_SHARED,fd,pos); p = region; not_printed = pagesize; if ( region == (char *)MAP_FAILED ) oops("mmap",4); for(pos=0; pos