/** * lseek test * * Copyright (C) 2014 NAKAMURA Minoru * * gcc -Wall -D_GNU_SOURCE -g seek_hole.c -o seek_hoke */ #include #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { int fd; off_t off; struct stat statinfo; if (argc != 2) { fprintf(stderr, "Syntax: %s filename\n", argv[0]); exit(EXIT_FAILURE); } if ((fd = open(argv[1], O_RDONLY)) < 0) { fprintf(stderr, "Cannot open %s\n", argv[1]); exit(EXIT_FAILURE); } if (fstat(fd, &statinfo) < 0) { fprintf(stderr, "Cannot stat %s\n", argv[1]); close(fd); exit(EXIT_FAILURE); } off = 0; while (off < statinfo.st_size) { off_t offset1, offset2; offset1 = lseek(fd, off, SEEK_HOLE); if (offset1 == (off_t) -1) goto error; offset2 = lseek(fd, offset1, SEEK_DATA); if (off == (off_t) -1) goto error; off = offset2; } error: close(fd); exit(EXIT_SUCCESS); }