#include #include #include #include #include #include #define EXIT_BAD_OPTION 2 void usage(int fd, char * progname) { dprintf(fd, "Usage: %s [-o offset] -l length name\n", progname); } int try_fallocate(char * filename, int offset, int length) { int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644); if (-1 == fd) return -1; int res = posix_fallocate(fd, offset, length); if (-1 == res) return -1; res = close(fd); if (-1 == res) return -1; return 0; } int main(int argc, char * argv[]) { int opt; int offset = 0; int length = 0; char * filename; while (-1 != (opt = getopt(argc, argv, "o:l:"))) { switch (opt) { case 'o': offset = atoi(optarg); break; case 'l': length = atoi(optarg); break; default: usage(2, argv[0]); exit(EXIT_BAD_OPTION); } } if (0 >= length || argc <= optind) { usage(2, argv[0]); exit(EXIT_BAD_OPTION); } filename = argv[optind]; int res = try_fallocate(filename, offset, length); if (-1 == res) { dprintf(2, "%s: %s: %s\n", argv[0], filename, strerror(errno)); exit(EXIT_FAILURE); } }