]> mj.ucw.cz Git - misc.git/blob - bmap.c
Added xclipcat.
[misc.git] / bmap.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <linux/fs.h>
8
9 int main(void)
10 {
11   int bs;
12   if (ioctl(0, FIGETBSZ, &bs) < 0)
13     {
14       perror("FIGETBSZ");
15       return 1;
16     }
17   printf("Block size is %d\n", bs);
18   long long len = lseek64(0, 0, SEEK_END);
19   printf("File size is %Ld\n", len);
20   int blx = (len+bs-1)/bs;
21   printf("... that is %d blocks\n", blx);
22   int blk, pos;
23   int small_gaps = 0;
24   int large_gaps = 0;
25   int last_pos = -1;
26   for (blk=0; blk<blx; blk++)
27     {
28       pos = blk;
29       if (ioctl(0, FIBMAP, &pos) < 0)
30         {
31           perror("FIBMAP");
32           return 1;
33         }
34       printf("%d\t%d\n", blk, pos);
35       if (last_pos >= 0)
36         {
37           if (pos == last_pos+1)
38             ;
39           else if (pos >= last_pos && pos < last_pos + 1024)
40             small_gaps++;
41           else
42             large_gaps++;
43         }
44       last_pos = pos;
45     }
46   printf("Gaps: %d small, %d large\n", small_gaps, large_gaps);
47   return 0;
48 }