Home | History | Annotate | Download | only in e2fsck
      1 /*
      2  * extend.c --- extend a file so that it has at least a specified
      3  * 	number of blocks.
      4  *
      5  * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
      6  *
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  */
     10 
     11 #include <stdio.h>
     12 #include <unistd.h>
     13 #include <stdlib.h>
     14 #include <string.h>
     15 #include <sys/types.h>
     16 #include <fcntl.h>
     17 #include "../misc/nls-enable.h"
     18 
     19 static void usage(char *progname)
     20 {
     21 	fprintf(stderr, _("%s: %s filename nblocks blocksize\n"),
     22 		progname, progname);
     23 	exit(1);
     24 }
     25 
     26 
     27 int main(int argc, char **argv)
     28 {
     29 	char	*filename;
     30 	int	nblocks, blocksize;
     31 	int	fd;
     32 	char	*block;
     33 	int	ret;
     34 
     35 	if (argc != 4)
     36 		usage(argv[0]);
     37 
     38 	filename = argv[1];
     39 	nblocks = strtoul(argv[2], 0, 0) - 1;
     40 	blocksize = strtoul(argv[3], 0, 0);
     41 
     42 	if (nblocks < 0) {
     43 		fprintf(stderr, _("Illegal number of blocks!\n"));
     44 		exit(1);
     45 	}
     46 
     47 	block = malloc(blocksize);
     48 	if (block == 0) {
     49 		fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
     50 			blocksize);
     51 		exit(1);
     52 	}
     53 	memset(block, 0, blocksize);
     54 
     55 	fd = open(filename, O_RDWR);
     56 	if (fd < 0) {
     57 		perror(filename);
     58 		exit(1);
     59 	}
     60 	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
     61 	if (ret < 0) {
     62 		perror("lseek");
     63 		exit(1);
     64 	}
     65 	ret = read(fd, block, blocksize);
     66 	if (ret < 0) {
     67 		perror("read");
     68 		exit(1);
     69 	}
     70 	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
     71 	if (ret < 0) {
     72 		perror("lseek #2");
     73 		exit(1);
     74 	}
     75 	ret = write(fd, block, blocksize);
     76 	if (ret < 0) {
     77 		perror("read");
     78 		exit(1);
     79 	}
     80 	exit(0);
     81 }
     82