blob: 2a849b21bb8bc2a7cc0baaeea2d0f7de81c7beb4 [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * extend.c --- extend a file so that it has at least a specified
3 * number of blocks.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04004 *
Theodore Ts'o50e1e101997-04-26 13:58:21 +00005 * 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>
Theodore Ts'oadfca562000-09-12 21:24:36 +000017#include "../misc/nls-enable.h"
Theodore Ts'o50e1e101997-04-26 13:58:21 +000018
19static void usage(char *progname)
20{
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000021 fprintf(stderr, _("%s: %s filename nblocks blocksize\n"),
Theodore Ts'o50e1e101997-04-26 13:58:21 +000022 progname, progname);
23 exit(1);
24}
25
26
27int 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) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000043 fprintf(stderr, _("Illegal number of blocks!\n"));
Theodore Ts'o50e1e101997-04-26 13:58:21 +000044 exit(1);
45 }
46
47 block = malloc(blocksize);
48 if (block == 0) {
Theodore Ts'o0c4a0722000-02-07 03:11:03 +000049 fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
Theodore Ts'o50e1e101997-04-26 13:58:21 +000050 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}