1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * The companion to ll_sparseness_write; walk all the bytes in the file.
5 * the bytes at the offsets specified on the command line must be '+', as
6 * previously written by ll_sparseness_write. All other bytes must be
14 #include <sys/types.h>
20 #define BUFSIZE (1024*1024)
22 void error(char *fmt, ...)
26 vfprintf(stderr, fmt, ap);
31 int compare_offsets(const void *a, const void *b)
33 off_t *A = (off_t *)a;
34 off_t *B = (off_t *)b;
38 int main(int argc, char **argv)
40 unsigned int num_offsets, cur_off = 0, i;
41 off_t *offsets, pos = 0, end_of_buf = 0;
48 error("Usage: %s <filename> <offset> [ offset ... ]\n",
51 fd = open(argv[1], O_RDONLY);
53 error("couldn't open %s: %s\n", argv[1], strerror(errno));
55 buf = malloc(BUFSIZE);
57 error("can't allocate buffer\n");
59 num_offsets = argc - 2;
60 offsets = calloc(sizeof(offsets[0]), num_offsets);
61 for (i = 0; i < num_offsets; i++) {
62 offsets[i] = strtoul(argv[i + 2], &end, 10);
64 error("couldn't parse offset '%s'\n", argv[i + 2]);
66 qsort(offsets, num_offsets, sizeof(offsets[0]), compare_offsets);
68 if (fstat(fd, &st) < 0)
69 error("stat: %s\n", strerror(errno));
71 for (i = 0; pos < st.st_size; i++, pos++) {
72 if (pos == end_of_buf) {
73 ret = read(fd, buf, BUFSIZE);
75 error("read(): %s\n", strerror(errno));
76 end_of_buf = pos + ret;
77 if (end_of_buf > st.st_size)
78 error("read %d bytes past file size?\n",
79 end_of_buf - st.st_size);
83 /* check for 0 when we aren't at a given offset */
84 if (cur_off >= num_offsets || pos != offsets[cur_off]) {
86 error("found char 0x%x at pos %lu instead of "
87 "0x0\n", buf[i], (long)pos);
91 /* the command line asks us to check for + at this offset */
93 error("found char 0x%x at pos %lu instead of "
94 "'.'\n", buf[i], (long)pos);
96 /* skip over duplicate offset arguments */
97 while (cur_off < num_offsets && offsets[cur_off] == pos)
100 /* don't bother freeing or closing.. */