• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
usage(char * progname)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 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30 	char	*filename;
31 	int	nblocks, blocksize;
32 	int	fd;
33 	char	*block;
34 	errcode_t retval;
35 	int	ret;
36 
37 	if (argc != 4)
38 		usage(argv[0]);
39 
40 	filename = argv[1];
41 	nblocks = strtoul(argv[2], 0, 0) - 1;
42 	blocksize = strtoul(argv[3], 0, 0);
43 
44 	if (nblocks < 0) {
45 		fprintf(stderr, _("Illegal number of blocks!\n"));
46 		exit(1);
47 	}
48 
49 	retval = ext2fs_get_memzero(blocksize, &block);
50 	if (retval) {
51 		fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
52 			blocksize);
53 		exit(1);
54 	}
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 	ext2fs_free_mem(&block);
82 	return(0);
83 }
84