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