]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/extend.c
libext2fs: fix potential divide by zero bug caused by a lxcfs bug
[thirdparty/e2fsprogs.git] / e2fsck / extend.c
CommitLineData
50e1e10f
TT
1/*
2 * extend.c --- extend a file so that it has at least a specified
3 * number of blocks.
efc6f628 4 *
50e1e10f
TT
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
d1154eb4 11#include "config.h"
50e1e10f
TT
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>
adfca56f 18#include "../misc/nls-enable.h"
50e1e10f
TT
19
20static void usage(char *progname)
21{
0c4a0726 22 fprintf(stderr, _("%s: %s filename nblocks blocksize\n"),
50e1e10f
TT
23 progname, progname);
24 exit(1);
25}
26
27
28int main(int argc, char **argv)
29{
30 char *filename;
31 int nblocks, blocksize;
32 int fd;
33 char *block;
70303df1 34 errcode_t retval;
50e1e10f
TT
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) {
0c4a0726 45 fprintf(stderr, _("Illegal number of blocks!\n"));
50e1e10f
TT
46 exit(1);
47 }
48
70303df1
AD
49 retval = ext2fs_get_memzero(blocksize, &block);
50 if (retval) {
0c4a0726 51 fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
50e1e10f
TT
52 blocksize);
53 exit(1);
54 }
50e1e10f
TT
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 }
70303df1
AD
81 ext2fs_free_mem(&block);
82 return(0);
50e1e10f 83}