1/* See LICENSE file for copyright and license details. */
2
3
4#include <sys/stat.h>
5
6#include <fcntl.h>
7#include <limits.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12
13#include "util.h"
14
15static void
16usage(void)
17{
18 eprintf("usage: %s [-o num] -l num file ...\n", argv0);
19}
20
21// ?man fallocate: preallocate file space
22// ?man arguments: -l num file ...
23// ?man preallocate or deallocate space to a file
24int
25main(int argc, char *argv[])
26{
27 int fd, ret = 0;
28 off_t size = 0, offset = 0;
29
30 ARGBEGIN {
31 // ?man -l:num: list in long format
32 case 'l':
33 size = estrtonum(EARGF(usage()), 1, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX));
34 break;
35 // ?man -o:num: specify output file
36 case 'o':
37 offset = estrtonum(EARGF(usage()), 0, MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX));
38 break;
39 default:
40 usage();
41 } ARGEND;
42
43 if (!argc || !size)
44 usage();
45
46 for (; *argv; argc--, argv++) {
47 if ((fd = open(*argv, O_RDWR | O_CREAT, 0644)) < 0) {
48 weprintf("open %s:", *argv);
49 ret = 1;
50 } else if (posix_fallocate(fd, offset, size) < 0) {
51 weprintf("posix_fallocate %s:", *argv);
52 ret = 1;
53 }
54
55 if (fd >= 0 && close(fd) < 0) {
56 weprintf("close %s:", *argv);
57 ret = 1;
58 }
59 }
60
61 return ret;
62}