Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * extend.c --- extend a file so that it has at least a specified |
| 3 | * number of blocks. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 4 | * |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 11 | #include "config.h" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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> |
Theodore Ts'o | adfca56 | 2000-09-12 21:24:36 +0000 | [diff] [blame] | 18 | #include "../misc/nls-enable.h" |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | |
| 20 | static void usage(char *progname) |
| 21 | { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 22 | fprintf(stderr, _("%s: %s filename nblocks blocksize\n"), |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 44 | fprintf(stderr, _("Illegal number of blocks!\n")); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 45 | exit(1); |
| 46 | } |
| 47 | |
| 48 | block = malloc(blocksize); |
| 49 | if (block == 0) { |
Theodore Ts'o | 0c4a072 | 2000-02-07 03:11:03 +0000 | [diff] [blame] | 50 | fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"), |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 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 | } |