Hello, if someone needs to use posix_fallocate(2) from userland, I have this simple C code
C:
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <string.h>
int fd, risultato, ch;
int sflag = 0;
extern char *__progname;
void
usage(void)
{
(void)fprintf(stderr, "Usage: %s -s SIZE -f FILENAME\n",__progname);
exit(1);
}
int main(int argc, char *argv[]) {
while ((ch = getopt(argc, argv, "s:f:")) != -1) {
switch (ch) {
case 's':
sflag = atoi(optarg);
break;
case 'f':
if ((fd = open(optarg, O_CREAT|O_RDWR, 0)) == -1)
err(1, "%s", optarg);
break;
default:
usage();
return 1;
}
}
argc -= optind;
argv += optind;
if (optind < 5) {
usage();
}
fchmod(fd,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
risultato=posix_fallocate(fd,0,sflag);
if ( risultato != 0 ) {
fprintf(stderr,"%s: \n",strerror(errno));
close(fd);
return 1;
} else {
close(fd);
return 0;
}
}