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