1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2003
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci *  FILE        : create_datafile.c
22f08c3bdfSopenharmony_ci *  PURPOSE	: Creates a file of specified size.
23f08c3bdfSopenharmony_ci *  HISTORY	:
24f08c3bdfSopenharmony_ci *  	10/17/2003 Robbie Williamson
25f08c3bdfSopenharmony_ci *	  -Written
26f08c3bdfSopenharmony_ci */
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#include <stdio.h>
29f08c3bdfSopenharmony_ci#include <stdlib.h>
30f08c3bdfSopenharmony_ci#include <sys/types.h>
31f08c3bdfSopenharmony_ci#include <sys/stat.h>
32f08c3bdfSopenharmony_ci#include <unistd.h>
33f08c3bdfSopenharmony_ci#include <fcntl.h>
34f08c3bdfSopenharmony_ci#include <string.h>
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci/* set write buffer size to whatever floats your boat.  I usually use 1M */
37f08c3bdfSopenharmony_ci#define BSIZE 1048576L
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ciint main(int argc, char *argv[])
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	off_t i;
42f08c3bdfSopenharmony_ci	long bufnum;
43f08c3bdfSopenharmony_ci	char buf[BSIZE];
44f08c3bdfSopenharmony_ci	off_t fd;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	if (argc != 3 || atoi(argv[1]) < 1) {
47f08c3bdfSopenharmony_ci		printf
48f08c3bdfSopenharmony_ci		    ("usage:\n\tcreate_file <# of %ld buffers to write> <name of file to create>\n\t ex. # create_file 10 /tmp/testfile\n",
49f08c3bdfSopenharmony_ci		     BSIZE);
50f08c3bdfSopenharmony_ci		exit(3);
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci	bufnum = strtol(argv[1], NULL, 0);
53f08c3bdfSopenharmony_ci	printf("Started building a %lu megabyte file\n", bufnum);
54f08c3bdfSopenharmony_ci	buf[0] = 'A';
55f08c3bdfSopenharmony_ci	for (i = 1; i < BSIZE; i++)
56f08c3bdfSopenharmony_ci		buf[i] = buf[i - 1] + 1;
57f08c3bdfSopenharmony_ci	buf[BSIZE - 1] = 'Z';
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if ((fd = creat(argv[2], 0755)) == -1)
60f08c3bdfSopenharmony_ci		perror("lftest: ");
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	for (i = 0; i < bufnum; i++) {
63f08c3bdfSopenharmony_ci		if (write(fd, buf, BSIZE) == -1)
64f08c3bdfSopenharmony_ci			return -1;
65f08c3bdfSopenharmony_ci		else {
66f08c3bdfSopenharmony_ci			printf(".");
67f08c3bdfSopenharmony_ci			fflush(stdout);
68f08c3bdfSopenharmony_ci		}
69f08c3bdfSopenharmony_ci		fsync(fd);
70f08c3bdfSopenharmony_ci	}
71f08c3bdfSopenharmony_ci	close(fd);
72f08c3bdfSopenharmony_ci	printf("\nFinished building a %lu megabyte file\n", bufnum);
73f08c3bdfSopenharmony_ci	return (0);
74f08c3bdfSopenharmony_ci}
75