119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/*
219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * extend.c --- extend a file so that it has at least a specified
319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * 	number of blocks.
419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project *
519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project *
719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * This file may be redistributed under the terms of the GNU Public
819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * License.
919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */
1019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdio.h>
1219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <unistd.h>
1319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdlib.h>
1419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <string.h>
1519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <sys/types.h>
1619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <fcntl.h>
1719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "../misc/nls-enable.h"
1819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
1919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectstatic void usage(char *progname)
2019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{
2119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	fprintf(stderr, _("%s: %s filename nblocks blocksize\n"),
2219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		progname, progname);
2319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	exit(1);
2419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project}
2519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
2619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
2719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectint main(int argc, char **argv)
2819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{
2919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	char	*filename;
3019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	int	nblocks, blocksize;
3119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	int	fd;
3219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	char	*block;
3319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	int	ret;
3419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
3519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (argc != 4)
3619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		usage(argv[0]);
3719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
3819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	filename = argv[1];
3919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	nblocks = strtoul(argv[2], 0, 0) - 1;
4019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	blocksize = strtoul(argv[3], 0, 0);
4119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
4219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (nblocks < 0) {
4319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		fprintf(stderr, _("Illegal number of blocks!\n"));
4419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
4519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
4619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
4719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	block = malloc(blocksize);
4819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (block == 0) {
4919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		fprintf(stderr, _("Couldn't allocate block buffer (size=%d)\n"),
5019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project			blocksize);
5119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
5219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
5319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	memset(block, 0, blocksize);
5419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project
5519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	fd = open(filename, O_RDWR);
5619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (fd < 0) {
5719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		perror(filename);
5819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
5919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
6019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
6119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (ret < 0) {
6219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		perror("lseek");
6319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
6419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
6519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	ret = read(fd, block, blocksize);
6619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (ret < 0) {
6719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		perror("read");
6819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
6919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
7019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	ret = lseek(fd, nblocks*blocksize, SEEK_SET);
7119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (ret < 0) {
7219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		perror("lseek #2");
7319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
7419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
7519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	ret = write(fd, block, blocksize);
7619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	if (ret < 0) {
7719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		perror("read");
7819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project		exit(1);
7919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	}
8019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project	exit(0);
8119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project}
82