Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- static int do_mincore(char *path)
- {
- unsigned long nr_pages, nr_present, i;
- struct stat statbuf;
- unsigned char *bits;
- int ret = 1;
- void *map;
- int fd;
- fd = open(path, O_RDONLY);
- if (fd == -1)
- goto out;
- if (fstat(fd, &statbuf) == -1)
- goto out_fd;
- nr_pages = (statbuf.st_size + 4095) / 4096;
- bits = malloc(nr_pages);
- if (bits == NULL)
- goto out_fd;
- map = mmap(NULL, statbuf.st_size, PROT_NONE, MAP_SHARED, fd, 0);
- if (map == MAP_FAILED)
- goto out_bits;
- if (mincore(map, statbuf.st_size, bits) == -1)
- goto out_map;
- nr_present = 0;
- for (i = 0; i < nr_pages; i++)
- if (bits[i])
- nr_present++;
- printf("%lu/%lu %s\n", nr_present, nr_pages, path);
- ret = 0;
- out_map:
- munmap(map, statbuf.st_size);
- out_bits:
- free(bits);
- out_fd:
- close(fd);
- out:
- return ret;
- }
- int main(int ac, char **av)
- {
- int ret = 0;
- int i;
- if (ac == 1) {
- printf("usage: mincore FILE...\n");
- return 1;
- }
- for (i = 1; i < ac; i++)
- ret |= do_mincore(av[i]);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement