morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

fallocate.c (733B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 
      4 #include <fcntl.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <unistd.h>
      8 
      9 #include "util.h"
     10 
     11 static void
     12 usage(void)
     13 {
     14 	eprintf("usage: %s [-o offset] -l length file\n", argv0);
     15 }
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20 	int fd;
     21 	off_t size = 0, offset = 0;
     22 
     23 	ARGBEGIN {
     24 	case 'l':
     25 		size = estrtol(EARGF(usage()), 10);
     26 		break;
     27 	case 'o':
     28 		offset = estrtol(EARGF(usage()), 10);
     29 		break;
     30 	default:
     31 		usage();
     32 	} ARGEND;
     33 
     34 	if (argc != 1 || !size)
     35 		usage();
     36 
     37 	fd = open(argv[0], O_RDWR | O_CREAT, 0644);
     38 	if (fd < 0)
     39 		eprintf("open %s:", argv[0]);
     40 
     41 	if (posix_fallocate(fd, offset, size) < 0)
     42 		eprintf("posix_fallocate:");
     43 
     44 	close(fd);
     45 	return 0;
     46 }