mincore: show byte offset instead of page offset
[pcu.git] / mincore.c
blob6b8b0e2876d8f1ceb0db696ee45be75f33025b4a
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;
12 static const char *fmt = sizeof(void *) == 8 ?
13 "%s: %016lu %x\n": "%s: %08lu %x\n";
15 if ((fd = open(path, O_RDONLY|O_NOATIME)) < 0) {
16 fprintf(stderr, "%s: open(): %s\n", path, strerror(errno));
17 return;
20 if (fstat(fd, &sb) < 0) {
21 fprintf(stderr, "%s: fstat(%d): %s\n",
22 path, fd, strerror(errno));
23 goto err_close;
26 vec_len = (sb.st_size + page_size() - 1) / page_size();
27 if (!(vec = malloc(vec_len))) {
28 fprintf(stderr, "%s: malloc(%lu): %s\n",
29 path, (unsigned long)vec_len, strerror(errno));
30 goto err_close;
33 map_len = PAGE_ALIGN(sb.st_size);
34 if (!(map = mmap(NULL, map_len, PROT_READ, MAP_SHARED, fd, 0))) {
35 fprintf(stderr, "%s: mmap(%lu): %s\n",
36 path, (unsigned long)vec_len, strerror(errno));
37 goto err_free;
40 if (mincore(map, map_len, vec) < 0) {
41 fprintf(stderr, "%s: mincore(%lu): %s\n",
42 path, (unsigned long)vec_len, strerror(errno));
43 goto err_munmap;
46 for (i = 0; i < vec_len; ++i)
47 printf(fmt, path, (unsigned long)(page_size() * i), 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;