Define macros to enable 64-bit file support on 32-bit
[pcu.git] / mincore.c
blob3309bd9c9c299a90c7373dbabedb57dfa310a2be
1 #include "compat-util.h"
3 static void mincore_stats(const char *path)
5 struct stat sb;
6 char *map;
7 unsigned char *vec;
8 size_t vec_len;
9 size_t map_len;
10 int fd;
11 size_t i;
13 if ((fd = open(path, O_RDONLY)) < 0) {
14 fprintf(stderr, "%s: open(): %s\n", path, strerror(errno));
15 return;
18 if (fstat(fd, &sb) < 0) {
19 fprintf(stderr, "%s: fstat(%d): %s\n",
20 path, fd, strerror(errno));
21 goto err_close;
24 vec_len = (sb.st_size + page_size() - 1) / page_size();
25 if (!(vec = malloc(vec_len))) {
26 fprintf(stderr, "%s: malloc(%lu): %s\n",
27 path, (unsigned long)vec_len, strerror(errno));
28 goto err_close;
31 map_len = PAGE_ALIGN(sb.st_size);
32 if (!(map = mmap(NULL, map_len, PROT_READ, MAP_SHARED, fd, 0))) {
33 fprintf(stderr, "%s: mmap(%lu): %s\n",
34 path, (unsigned long)vec_len, strerror(errno));
35 goto err_free;
38 if (mincore(map, map_len, vec) < 0) {
39 fprintf(stderr, "%s: mincore(%lu): %s\n",
40 path, (unsigned long)vec_len, strerror(errno));
41 goto err_munmap;
44 for (i = 0; i < vec_len; ++i)
45 printf("%s: [%08lu] %x\n",
46 path, (unsigned long)i,
47 (vec[i]));
48 err_munmap:
49 munmap(map, map_len);
50 err_free:
51 free(vec);
52 err_close:
53 close(fd);
56 int main(int argc, char * const argv[])
58 int i;
60 for (i = 1; i < argc; ++i)
61 mincore_stats(argv[i]);
62 return 0;