diff options
Diffstat (limited to 'fallocate.c')
-rw-r--r-- | fallocate.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/fallocate.c b/fallocate.c new file mode 100644 index 0000000..910d191 --- /dev/null +++ b/fallocate.c @@ -0,0 +1,59 @@ +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +#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); + } +} |