1 /* xfer.c Copyright 1992-2000 by Michael Temari All Rights Reserved
3 * This file is part of ftp.
6 * 03/14/00 Initial Release Michael Temari, <Michael@TemWare.Com>
10 #include <sys/types.h>
25 static int asciisend(int fd
, int fdout
);
26 static int binarysend(int fd
, int fdout
);
27 static int asciirecv(int fd
, int fdin
);
28 static int binaryrecv(int fd
, int fdin
);
30 #if (__WORD_SIZE == 4)
31 static char buffer
[8192];
32 static char bufout
[8192];
34 static char buffer
[2048];
35 static char bufout
[2048];
38 static int asciisend(fd
, fdout
)
46 unsigned long total
=0L;
55 ope
= bufout
+ sizeof(bufout
) - 3;
57 while((s
= read(fd
, buffer
, sizeof(buffer
))) > 0) {
70 *(u16_t
*)&block
[1] = htons(op
- bufout
);
71 write(fdout
, block
, sizeof(block
));
73 write(fdout
, bufout
, op
- bufout
);
78 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total
);
84 block
[0] = MODE_B_EOF
;
85 *(u16_t
*)&block
[1] = htons(op
- bufout
);
86 write(fdout
, block
, sizeof(block
));
88 write(fdout
, bufout
, op
- bufout
);
89 } else if(mode
== MODE_B
) {
90 block
[0] = MODE_B_EOF
;
91 *(u16_t
*)&block
[1] = htons(0);
92 write(fdout
, block
, sizeof(block
));
103 static int binarysend(fd
, fdout
)
108 unsigned long total
=0L;
116 while((s
= read(fd
, buffer
, sizeof(buffer
))) > 0) {
118 block
[0] = MODE_B_EOF
;
119 *(u16_t
*)&block
[1] = htons(s
);
120 write(fdout
, block
, sizeof(block
));
122 write(fdout
, buffer
, s
);
125 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total
);
130 block
[0] = MODE_B_EOF
;
131 *(u16_t
*)&block
[1] = htons(0);
132 write(fdout
, block
, sizeof(block
));
143 int sendfile(fd
, fdout
)
151 s
= asciisend(fd
, fdout
);
154 s
= binarysend(fd
, fdout
);
163 static int asciirecv(fd
, fdin
)
172 unsigned long total
=0L;
176 if(isatty
&& fd
> 2) {
181 op
= bufout
; ope
= bufout
+ sizeof(bufout
) - 3;
185 cnt
= sizeof(buffer
);
188 s
= read(fdin
, block
, sizeof(block
));
189 cnt
= ntohs(*(u16_t
*)&block
[1]);
191 if(cnt
== 0 && block
[0] & MODE_B_EOF
)
194 s
= read(fdin
, buffer
, cnt
> sizeof(buffer
) ? sizeof(buffer
) : cnt
);
211 write(fd
, bufout
, op
- bufout
);
216 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total
);
219 if(cnt
== 0 && mode
== MODE_B
&& block
[0] & MODE_B_EOF
) {
227 write(fd
, bufout
, op
- bufout
);
232 if((mode
== MODE_B
&& cnt
!= 0) || s
!= 0)
238 static binaryrecv(fd
, fdin
)
243 unsigned long total
=0L;
254 cnt
= sizeof(buffer
);
257 s
= read(fdin
, block
, sizeof(block
));
258 cnt
= ntohs(*(u16_t
*)&block
[1]);
260 if(cnt
== 0 && block
[0] & MODE_B_EOF
)
263 s
= read(fdin
, buffer
, cnt
> sizeof(buffer
) ? sizeof(buffer
) : cnt
);
267 write(fd
, buffer
, s
);
269 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total
);
272 if(cnt
== 0 && mode
== MODE_B
&& block
[0] & MODE_B_EOF
) {
281 if((mode
== MODE_B
&& cnt
!= 0) || s
!= 0)
287 int recvfile(fd
, fdin
)
295 s
= asciirecv(fd
, fdin
);
298 s
= binaryrecv(fd
, fdin
);