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 "config.h"
     12 #include <stdio.h>
     13 #include <unistd.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 #include <sys/types.h>
     17 #include <fcntl.h>
     18 #include "../misc/nls-enable.h"
     19 
     20 static void usage(char *progname)
     21 {
     22 	fprintf(stderr, _("%s: %s filename nblocks blocksize\n"),
     23 		progname, progname);
     24 	exit(1);
     25 }
     26 
     27 
     28 int main(int argc, char **argv)
     29 {
     30 	char	*filename;
     31 	int	nblocks, blocksize;
     32 	int	fd;
     33 	char	*block;
     34 	int	ret;
     35 
     36 	if (argc != 4)
     37 		usage(argv[0]);
     38 
     39 	filename = argv[1];
     40 	nblocks = strtoul(argv[2], 0, 0) - 1;
     41 	blocksize = strtoul(argv[3], 0, 0);
     42 
     43 	if (nblocks < 0) {
     44 		fprintf(stderr, _("Illegal number of blocks!\n"));
     45 		exit(1);
     46 	}
     47 
     48 	block = malloc(blocksize);
     49 	if (block == 0) {
     50 		fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
     51 			blocksize);
     52 		exit(1);
     53 	}
     54 	memset(block, 0, blocksize);
     55 
     56 	fd = open(filename, O_RDWR);
     57 	if (fd < 0) {
     58 		perror(filename);
     59 		exit(1);
     60 	}
     61 	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
     62 	if (ret < 0) {
     63 		perror("lseek");
     64 		exit(1);
     65 	}
     66 	ret = read(fd, block, blocksize);
     67 	if (ret < 0) {
     68 		perror("read");
     69 		exit(1);
     70 	}
     71 	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
     72 	if (ret < 0) {
     73 		perror("lseek #2");
     74 		exit(1);
     75 	}
     76 	ret = write(fd, block, blocksize);
     77 	if (ret < 0) {
     78 		perror("read");
     79 		exit(1);
     80 	}
     81 	exit(0);
     82 }
     83