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