retire nonsymbolic rootdev, dev2name
[minix.git] / commands / ftp101 / xfer.c
blob7edfd1dad4d5943e6c857730ab769aa2dcab6b5c
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>
8 */
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <time.h>
19 #include <utime.h>
20 #include <net/hton.h>
22 #include "ftp.h"
23 #include "xfer.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];
33 #else
34 static char buffer[2048];
35 static char bufout[2048];
36 #endif
38 static int asciisend(fd, fdout)
39 int fd;
40 int fdout;
42 int s, len;
43 char c;
44 char *p;
45 char *op, *ope;
46 unsigned long total=0L;
47 char block[3];
49 if(atty) {
50 printf("Sent ");
51 fflush(stdout);
54 op = bufout;
55 ope = bufout + sizeof(bufout) - 3;
57 while((s = read(fd, buffer, sizeof(buffer))) > 0) {
58 total += (long)s;
59 p = buffer;
60 while(s-- > 0) {
61 c = *p++;
62 if(c == '\n') {
63 *op++ = '\r';
64 total++;
66 *op++ = c;
67 if(op >= ope) {
68 if(mode == MODE_B) {
69 block[0] = '\0';
70 *(u16_t *)&block[1] = htons(op - bufout);
71 write(fdout, block, sizeof(block));
73 write(fdout, bufout, op - bufout);
74 op = bufout;
77 if(atty) {
78 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total);
79 fflush(stdout);
82 if(op > bufout) {
83 if(mode == MODE_B) {
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));
93 s = 0;
95 if(atty) {
96 printf("\n");
97 fflush(stdout);
100 return(s);
103 static int binarysend(fd, fdout)
104 int fd;
105 int fdout;
107 int s;
108 unsigned long total=0L;
109 char block[3];
111 if(atty) {
112 printf("Sent ");
113 fflush(stdout);
116 while((s = read(fd, buffer, sizeof(buffer))) > 0) {
117 if(mode == MODE_B) {
118 block[0] = MODE_B_EOF;
119 *(u16_t *)&block[1] = htons(s);
120 write(fdout, block, sizeof(block));
122 write(fdout, buffer, s);
123 total += (long)s;
124 if(atty) {
125 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total);
126 fflush(stdout);
129 if(mode == MODE_B) {
130 block[0] = MODE_B_EOF;
131 *(u16_t *)&block[1] = htons(0);
132 write(fdout, block, sizeof(block));
133 s = 0;
135 if(atty) {
136 printf("\n");
137 fflush(stdout);
140 return(s);
143 int sendfile(fd, fdout)
144 int fd;
145 int fdout;
147 int s;
149 switch(type) {
150 case TYPE_A:
151 s = asciisend(fd, fdout);
152 break;
153 default:
154 s = binarysend(fd, fdout);
157 if(s < 0)
158 return(-1);
159 else
160 return(0);
163 static int asciirecv(fd, fdin)
164 int fd;
165 int fdin;
167 int s, len;
168 int gotcr;
169 char c;
170 char *p;
171 char *op, *ope;
172 unsigned long total=0L;
173 char block[3];
174 unsigned short cnt;
176 if(isatty && fd > 2) {
177 printf("Received ");
178 fflush(stdout);
180 gotcr = 0;
181 op = bufout; ope = bufout + sizeof(bufout) - 3;
182 cnt = 0;
183 while(1) {
184 if(mode != MODE_B)
185 cnt = sizeof(buffer);
186 else
187 if(cnt == 0) {
188 s = read(fdin, block, sizeof(block));
189 cnt = ntohs(*(u16_t *)&block[1]);
190 s = 0;
191 if(cnt == 0 && block[0] & MODE_B_EOF)
192 break;
194 s = read(fdin, buffer, cnt > sizeof(buffer) ? sizeof(buffer) : cnt);
195 if(s <= 0) break;
196 cnt -= s;
197 total += (long)s;
198 p = buffer;
199 while(s-- > 0) {
200 c = *p++;
201 if(gotcr) {
202 gotcr = 0;
203 if(c != '\n')
204 *op++ = '\r';
206 if(c == '\r')
207 gotcr = 1;
208 else
209 *op++ = c;
210 if(op >= ope) {
211 write(fd, bufout, op - bufout);
212 op = bufout;
215 if(atty && fd > 2) {
216 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total);
217 fflush(stdout);
219 if(cnt == 0 && mode == MODE_B && block[0] & MODE_B_EOF) {
220 s = 0;
221 break;
224 if(gotcr)
225 *op++ = '\r';
226 if(op > bufout)
227 write(fd, bufout, op - bufout);
228 if(atty && fd > 2) {
229 printf("\n");
230 fflush(stdout);
232 if((mode == MODE_B && cnt != 0) || s != 0)
233 return(-1);
234 else
235 return(0);
238 static binaryrecv(fd, fdin)
239 int fd;
240 int fdin;
242 int s;
243 unsigned long total=0L;
244 char block[3];
245 unsigned short cnt;
247 if(atty && fd > 2) {
248 printf("Received ");
249 fflush(stdout);
251 cnt = 0;
252 while(1) {
253 if(mode != MODE_B)
254 cnt = sizeof(buffer);
255 else
256 if(cnt == 0) {
257 s = read(fdin, block, sizeof(block));
258 cnt = ntohs(*(u16_t *)&block[1]);
259 s = 0;
260 if(cnt == 0 && block[0] & MODE_B_EOF)
261 break;
263 s = read(fdin, buffer, cnt > sizeof(buffer) ? sizeof(buffer) : cnt);
264 if(s <= 0) break;
265 cnt -= s;
266 total += (long)s;
267 write(fd, buffer, s);
268 if(atty && fd > 2) {
269 printf("%8lu bytes\b\b\b\b\b\b\b\b\b\b\b\b\b\b", total);
270 fflush(stdout);
272 if(cnt == 0 && mode == MODE_B && block[0] & MODE_B_EOF) {
273 s = 0;
274 break;
277 if(atty && fd > 2) {
278 printf("\n");
279 fflush(stdout);
281 if((mode == MODE_B && cnt != 0) || s != 0)
282 return(-1);
283 else
284 return(0);
287 int recvfile(fd, fdin)
288 int fd;
289 int fdin;
291 int s;
293 switch(type) {
294 case TYPE_A:
295 s = asciirecv(fd, fdin);
296 break;
297 default:
298 s = binaryrecv(fd, fdin);
301 if(s < 0)
302 return(-1);
303 else
304 return(0);