ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

fallocate.c (987B)


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