xref: /original-bsd/sys/tahoe/stand/fastcopy.c (revision e2944021)
1 /*	fastcopy.c	1.1	86/01/12	*/
2 /*	fastcopy.c */
3 
4 #define	BSIZE	1024
5 #define FACTOR	32	/* 32k bytes in one i/o */
6 
7 char	buffer[BSIZE * FACTOR];
8 
9 main()
10 {
11 	char		in_dev[50];
12 	char		out_dev[50];
13 	char		blocks[50];
14 	register int	input;
15 	register int	output;
16 	register int	count;
17 	register int	firstread = 1;
18 	register int	blk_siz = BSIZE * FACTOR;
19 	register int	blk_num = 0;
20 	register int	read_count = 0;
21 	register int	write_count = 0;
22 	register int	transfers = 0;
23 
24 	in_dev[0] = 0;
25 	out_dev[0] = 0;
26 	blocks[0] = 0;
27 	for(;;) {
28 		/* get input and output devices */
29 		printf("Source device : ");
30 		gets(in_dev);
31 		if((input = open(in_dev, 0)) > 0)
32 			break;
33 		printf("Cannot open input file '%s'\n", in_dev);
34 	}
35 
36 	for(;;) {
37 		printf("Copy to device : ");
38 		gets(out_dev);
39 		if((output = open(out_dev, 1)) > 0)
40 			break;
41 		printf("Cannot open output file '%s'\n", out_dev);
42 	}
43 
44 	for(;;) {
45 		printf("Number of blocks : ");
46 		gets(blocks);
47 		count = number(blocks);
48 		if(count > 0)
49 			break;
50 	}
51 	printf("\nCopy %d blocks from %s to %s\n", count, in_dev, out_dev);
52 	do {
53 		if ((transfers > 0) && !(transfers % 25))
54 			printf("%d blocks\n", blk_num);
55 		transfers++;
56 		read_count = read(input, buffer,
57 		    ((count*BSIZE) > blk_siz) ? blk_siz : count*BSIZE);
58 		if (firstread) {
59 			if (read_count != blk_siz) {
60 				blk_siz = read_count;
61 			}
62 			firstread = 0;
63 			printf("Block size from input = %d bytes\n", blk_siz);
64 		}
65 		if (read_count > 0) {
66 			if (read_count != blk_siz)
67 				printf("Short read!  Block %d: %d read, %d bytes requested\n", blk_num, read_count, blk_siz);
68 			write_count = write(output, buffer, read_count);
69 			if (write_count != read_count)
70 				printf("Short write!  Block %d: %d bytes written of %d bytes possible\n", blk_num, write_count, read_count);
71 			count -= read_count / BSIZE;
72 			blk_num += read_count / BSIZE;
73 		}
74 	} while((read_count > 0) && (write_count > 0) && (count > 0));
75 	printf ("Total of %d blocks copied\n", blk_num);
76 	close(input);
77 	close(output);
78 }
79 
80 int number (response)
81 char *response;
82 {
83 	int	total;
84 
85 	total = 0;
86 	while (*response == ' ' || *response == '\t') response++;
87 	while (*response >= '0' && *response <= '9') {
88 		total = total * 10 + (*response - '0');
89 		response++;
90 	}
91 	return (total);
92 }
93