#include #include #include #include #include #include #include #include #include #include #include #include /*****************************************************************************/ /* */ /* Front ends to various library calls */ /* */ /*****************************************************************************/ static int syspagesize = 0, pagemask, offmask; caddr_t femmap(int fd, off_t foff, size_t syz, int prot, u_char *msg) { /* femmap() and fem/unmap() take care of the need to allocate on page boundaries and in multiples of pages. */ off_t pfoff; /* foff on a page boundary */ size_t psyz; /* syz rounded up to full pages */ int chopped; caddr_t rslt; if (syspagesize == 0) { syspagesize = getpagesize(); offmask = syspagesize - 1; pagemask = ~offmask; if (0) printf("--- Syspagesize %d, pagemask %X, offmask %X\n", syspagesize, pagemask, offmask); } pfoff = foff & pagemask; chopped = foff & offmask; psyz = (syz + chopped + syspagesize - 1) & pagemask; if ((rslt = mmap(0, psyz, prot, MAP_SHARED, fd, pfoff)) ==MAP_FAILED) { fprintf(stderr,"Error: %s\n", msg); perror("Mmap"); exit(201); } printf("Just Mmaped %d which is now at address %X\n", psyz, (int)rslt); if (0) printf(" Mapping %d at %X <---> %d (%s\n", psyz, (int)rslt, (int)pfoff, msg); return (rslt + chopped); } void femunmap(caddr_t cad, size_t syz, u_char *msg) { /* Unmap the specified block of memory. Cad should be the result returned by a call to femmap() and syz should be the size passed to femmap() */ size_t psyz; /* syz rounded up to full pages */ int chopped; caddr_t pcad; chopped = (int) cad & offmask; pcad = (caddr_t) ((int) cad & pagemask); psyz = (syz + chopped + syspagesize - 1) & pagemask; printf("About to UnMapp %d at %X\n", psyz, (int)pcad); if (munmap(pcad, psyz)) { perror(msg); exit(205); } if (0) printf(" UnMapping %d at %X - (%s)\n", psyz, (int)pcad, msg); } static off_t bnsyz; static u_char *bnams; int main(void) { int tfd; u_char filename[100]; struct stat finfo; strcpy(filename, "cygcheck.out"); tfd = open(filename, O_RDONLY, 0); fstat(tfd, &finfo); bnsyz = finfo.st_size; bnams = femmap(tfd, 0, bnsyz, PROT_READ, "bnams"); if (bnams != NULL) { femunmap(bnams, bnsyz, "bnams"); bnams = NULL; } return(0); }