[SHELL/EXPERIMENTS]
[reactos.git] / base / applications / network / ftp / ftp.c
1 /*
2 * Copyright (c) 1985, 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18 #include "precomp.h"
19
20 #include <signal.h>
21
22 #define L_SET SEEK_SET
23 #define L_INCR SEEK_CUR
24 #define caddr_t void *
25
26 #ifndef lint
27 static char sccsid[] = "@(#)ftp.c 5.28 (Berkeley) 4/20/89";
28 #endif /* not lint */
29
30 #ifndef MAXHOSTNAMELEN
31 #define MAXHOSTNAMELEN 64
32 #endif
33
34 #ifdef NOVFPRINTF
35 #define vfprintf(a,b,c) _doprnt(b,c,a)
36 #endif
37
38 #ifdef sun
39 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t presence */
40 #ifndef FD_SET
41 #define NBBY 8 /* number of bits in a byte */
42 /*
43 * Select uses bit masks of file descriptors in longs.
44 * These macros manipulate such bit fields (the filesystem macros use chars).
45 * FD_SETSIZE may be defined by the user, but the default here
46 * should be >= NOFILE (param.h).
47 */
48 #ifndef FD_SETSIZE
49 #define FD_SETSIZE 256
50 #endif
51
52 typedef long fd_mask;
53 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
54 #ifndef howmany
55 #define howmany(x, y) (((x)+((y)-1))/(y))
56 #endif
57
58 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
59 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
60 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
61 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
62
63 typedef int uid_t;
64 typedef int gid_t;
65 #endif
66 #endif
67
68 struct sockaddr_in hisctladdr;
69 struct sockaddr_in data_addr;
70 int data = -1;
71 int abrtflag = 0;
72 int ptflag = 0;
73 int allbinary;
74 struct sockaddr_in myctladdr;
75 uid_t getuid();
76 sig_t lostpeer();
77 off_t restart_point = 0;
78
79 SOCKET cin, cout;
80 int dataconn(const char *mode);
81
82 int command(const char *fmt, ...);
83
84 char *hostname;
85
86 typedef void (*Sig_t)(int);
87
88 // Signal Handlers
89
90 void psabort(int sig);
91
92 char *hookup(const char *host, int port)
93 {
94 register struct hostent *hp = 0;
95 int len;
96 SOCKET s;
97 static char hostnamebuf[80];
98
99 bzero((char *)&hisctladdr, sizeof (hisctladdr));
100 hisctladdr.sin_addr.s_addr = inet_addr(host);
101 if (hisctladdr.sin_addr.s_addr != (unsigned long)-1) {
102 hisctladdr.sin_family = AF_INET;
103 (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
104 } else {
105 hp = gethostbyname(host);
106 if (hp == NULL) {
107 fprintf(stderr, "ftp: %s: ", host);
108 herror((char *)NULL);
109 code = -1;
110 return((char *) 0);
111 }
112 hisctladdr.sin_family = hp->h_addrtype;
113 bcopy(hp->h_addr_list[0],
114 (caddr_t)&hisctladdr.sin_addr, hp->h_length);
115 (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
116 }
117 hostname = hostnamebuf;
118 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
119 if (s == INVALID_SOCKET) {
120 perror("ftp: socket");
121 code = -1;
122 return (0);
123 }
124 hisctladdr.sin_port = port;
125 while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
126 if (hp && hp->h_addr_list[1]) {
127 int oerrno = errno;
128
129 fprintf(stderr, "ftp: connect to address %s: ",
130 inet_ntoa(hisctladdr.sin_addr));
131 errno = oerrno;
132 perror((char *) 0);
133 hp->h_addr_list++;
134 bcopy(hp->h_addr_list[0],
135 (caddr_t)&hisctladdr.sin_addr, hp->h_length);
136 fprintf(stdout, "Trying %s...\n",
137 inet_ntoa(hisctladdr.sin_addr));
138 (void) fflush(stdout);
139 (void) close(s);
140 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
141 if (s == INVALID_SOCKET) {
142 perror("ftp: socket");
143 code = -1;
144 return (0);
145 }
146 continue;
147 }
148 perror("ftp: connect");
149 code = -1;
150 goto bad;
151 }
152 len = sizeof (myctladdr);
153 if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
154 perror("ftp: getsockname");
155 code = -1;
156 goto bad;
157 }
158 cin = cout = s;
159 if (verbose) {
160 printf("Connected to %s.\n", hostname);
161 (void) fflush(stdout);
162 }
163 if (getreply(0) > 2) { /* read startup message from server */
164 closesocket(cin);
165 code = -1;
166 goto bad;
167 }
168 #ifdef SO_OOBINLINE
169 {
170 int on = 1;
171
172 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (const char *) &on, sizeof(on))
173 < 0 && debug) {
174 perror("ftp: setsockopt");
175 }
176 }
177 #endif //SO_OOBINLINE
178
179 return (hostname);
180 bad:
181 (void) close(s);
182 return ((char *)0);
183 }
184
185 int login(const char *host)
186 {
187 char tmp[80];
188 char *puser, *ppass, *pacct;
189 const char *user, *pass, *acct;
190 int n, aflag = 0;
191
192 user = pass = acct = 0;
193 n = ruserpass(host, &puser, &ppass, &pacct);
194 if (n < 0) {
195 code = -1;
196 return(0);
197 }
198 if (0 != n) {
199 user = puser;
200 pass = ppass;
201 acct = pacct;
202 }
203 while (user == NULL) {
204 const char *myname = "none"; // This needs to become the usename env
205
206 if (myname)
207 printf("Name (%s:%s): ", host, myname);
208 else
209 printf("Name (%s): ", host);
210 (void) fflush(stdout);
211 (void) fgets(tmp, sizeof(tmp) - 1, stdin);
212 tmp[strlen(tmp) - 1] = '\0';
213 if (*tmp == '\0')
214 user = myname;
215 else
216 user = tmp;
217 }
218 n = command("USER %s", user);
219 if (n == CONTINUE) {
220 if (pass == NULL)
221 pass = getpass("Password:");
222 n = command("PASS %s", pass);
223 fflush(stdin);
224 }
225 if (n == CONTINUE) {
226 aflag++;
227 acct = getpass("Account:");
228 n = command("ACCT %s", acct);
229 }
230 if (n != COMPLETE) {
231 fprintf(stderr, "Login failed.\n");
232 return (0);
233 }
234 if (!aflag && acct != NULL)
235 (void) command("ACCT %s", acct);
236 if (proxy)
237 return(1);
238 for (n = 0; n < macnum; ++n) {
239 if (!strcmp("init", macros[n].mac_name)) {
240 (void) strcpy(line, "$init");
241 makeargv();
242 domacro(margc, margv);
243 break;
244 }
245 }
246 return (1);
247 }
248
249 static void
250 cmdabort(int sig)
251 {
252 extern jmp_buf ptabort;
253
254 printf("\n");
255 (void) fflush(stdout);
256 abrtflag++;
257 if (ptflag)
258 longjmp(ptabort,1);
259 }
260
261 /*VARARGS1*/
262 int command(const char *fmt, ...)
263 {
264 va_list ap;
265 int r;
266 void (*oldintr)(int);
267
268 abrtflag = 0;
269 if (debug) {
270 printf("---> ");
271 va_start(ap, fmt);
272 vfprintf(stdout, fmt, ap);
273 va_end(ap);
274 printf("\n");
275 (void) fflush(stdout);
276 }
277 if (cout == 0) {
278 perror ("No control connection for command");
279 code = -1;
280 return (0);
281 }
282 oldintr = signal(SIGINT,cmdabort);
283 {
284 char buffer[1024];
285
286 va_start(ap, fmt);
287 vsprintf(buffer, fmt, ap);
288 va_end(ap);
289 //DLJ: to work through firewalls - send the command as a single message
290 strcat(buffer,"\r\n");
291 fprintfSocket(cout, buffer);
292 }
293 //DLJ: the following two lines are replaced by the strcat above - seems to
294 // make it work through firewalls.
295 // fprintfSocket(cout, "\r\n");
296 // (void) fflush(cout);
297 cpend = 1;
298 r = getreply(!strcmp(fmt, "QUIT"));
299 if (abrtflag && oldintr != SIG_IGN)
300 (*oldintr)(SIGINT);
301 // (void) signal(SIGINT, oldintr);
302 return(r);
303 }
304
305 char reply_string[BUFSIZ]; /* last line of previous reply */
306
307 #include <ctype.h>
308
309 int
310 getreply(expecteof)
311 int expecteof;
312 {
313 register int c, n;
314 register int dig;
315 register char *cp;
316 int originalcode = 0, continuation = 0;
317 void (*oldintr)(int);
318 int pflag = 0;
319 char *pt = pasv;
320
321 oldintr = signal(SIGINT,cmdabort);
322 for (;;) {
323 dig = n = code = 0;
324 cp = reply_string;
325 while ((c = fgetcSocket(cin)) != '\n') {
326 if (c == IAC) { /* handle telnet commands */
327 switch (fgetcSocket(cin)) {
328 case WILL:
329 case WONT:
330 c = fgetcSocket(cin);
331 fprintfSocket(cout, "%c%c%c",IAC,DONT,c);
332 break;
333 case DO:
334 case DONT:
335 c = fgetcSocket(cin);
336 fprintfSocket(cout, "%c%c%c",IAC,WONT,c);
337 break;
338 default:
339 break;
340 }
341 continue;
342 }
343 dig++;
344 if (c == EOF) {
345 if (expecteof) {
346 // (void) signal(SIGINT,oldintr);
347 code = 221;
348 return (0);
349 }
350 lostpeer();
351 if (verbose) {
352 printf("421 Service not available, remote server has closed connection\n");
353 (void) fflush(stdout);
354 }
355 code = 421;
356 return(4);
357 }
358 if (c != '\r' && (verbose > 0 ||
359 (verbose > -1 && n == '5' && dig > 4))) {
360 if (proxflag &&
361 ((dig == 1 || dig == 5) && verbose == 0))
362 printf("%s:",hostname);
363 (void) putchar(c);
364 (void) fflush(stdout);
365 }
366 if (dig < 4 && isdigit(c))
367 code = code * 10 + (c - '0');
368 if (!pflag && code == 227)
369 pflag = 1;
370 if (dig > 4 && pflag == 1 && isdigit(c))
371 pflag = 2;
372 if (pflag == 2) {
373 if (c != '\r' && c != ')')
374 *pt++ = c;
375 else {
376 *pt = '\0';
377 pflag = 3;
378 }
379 }
380 if (dig == 4 && c == '-') {
381 if (continuation)
382 code = 0;
383 continuation++;
384 }
385 if (n == 0)
386 n = c;
387 if (cp < &reply_string[sizeof(reply_string) - 1])
388 *cp++ = c;
389 }
390 if (verbose > 0 || (verbose > -1 && n == '5')) {
391 (void) putchar(c);
392 (void) fflush (stdout);
393 }
394 if (continuation && code != originalcode) {
395 if (originalcode == 0)
396 originalcode = code;
397 continue;
398 }
399 *cp = '\0';
400 if (n != '1')
401 cpend = 0;
402 (void) signal(SIGINT,oldintr);
403 if (code == 421 || originalcode == 421)
404 lostpeer();
405 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
406 (*oldintr)(SIGINT);
407 return (n - '0');
408 }
409 }
410
411 static int
412 empty(mask, sec)
413 struct fd_set *mask;
414 int sec;
415 {
416 struct timeval t;
417
418 t.tv_sec = (long) sec;
419 t.tv_usec = 0;
420 return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
421 }
422
423 jmp_buf sendabort;
424
425 #if 0
426 void abortsend()
427 {
428
429 mflag = 0;
430 abrtflag = 0;
431 printf("\nsend aborted\n");
432 (void) fflush(stdout);
433 longjmp(sendabort, 1);
434 }
435 #endif
436
437 #define HASHBYTES 1024
438
439 void sendrequest(const char *cmd, const char *local, const char *remote, int printnames)
440 {
441 FILE *fin;
442 int dout = 0;
443 int (*closefunc)();
444 sig_t (*oldintr)(), (*oldintp)();
445 char buf[BUFSIZ], *bufp;
446 long bytes = 0, hashbytes = HASHBYTES;
447 register int c, d;
448 struct stat st;
449 struct timeval start, stop;
450 const char *mode;
451
452 if (verbose && printnames) {
453 if (local && *local != '-')
454 printf("local: %s ", local);
455 if (remote)
456 printf("remote: %s\n", remote);
457 (void) fflush(stdout);
458 }
459 if (proxy) {
460 proxtrans(cmd, local, remote);
461 return;
462 }
463 closefunc = NULL;
464 oldintr = NULL;
465 oldintp = NULL;
466 mode = "w";
467 if (setjmp(sendabort)) {
468 while (cpend) {
469 (void) getreply(0);
470 }
471 if (data >= 0) {
472 (void) close(data);
473 data = -1;
474 }
475 if (oldintr)
476 null();// (void) signal(SIGINT,oldintr);
477 if (oldintp)
478 null();// (void) signal(SIGPIPE,oldintp);
479 code = -1;
480 return;
481 }
482 null();// oldintr = signal(SIGINT, abortsend);
483 if (strcmp(local, "-") == 0)
484 fin = stdin;
485 else if (*local == '|') {
486 null();// oldintp = signal(SIGPIPE,SIG_IGN);
487 fin = _popen(local + 1, "r");
488 if (fin == NULL) {
489 perror(local + 1);
490 null();// (void) signal(SIGINT, oldintr);
491 null();// (void) signal(SIGPIPE, oldintp);
492 code = -1;
493 return;
494 }
495 closefunc = _pclose;
496 } else {
497 fin = fopen(local, "r");
498 if (fin == NULL) {
499 perror(local);
500 null();// (void) signal(SIGINT, oldintr);
501 code = -1;
502 return;
503 }
504 closefunc = fclose;
505 if (fstat(fileno(fin), &st) < 0 ||
506 (st.st_mode&S_IFMT) != S_IFREG) {
507 fprintf(stdout, "%s: not a plain file.\n", local);
508 (void) fflush(stdout);
509 null();// (void) signal(SIGINT, oldintr);
510 fclose(fin);
511 code = -1;
512 return;
513 }
514 }
515 if (initconn()) {
516 null();// (void) signal(SIGINT, oldintr);
517 if (oldintp)
518 null();// (void) signal(SIGPIPE, oldintp);
519 code = -1;
520 if (closefunc != NULL)
521 (*closefunc)(fin);
522 return;
523 }
524 if (setjmp(sendabort))
525 goto abort;
526
527 if (restart_point &&
528 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
529 if (fseek(fin, (long) restart_point, 0) < 0) {
530 perror(local);
531 restart_point = 0;
532 if (closefunc != NULL)
533 (*closefunc)(fin);
534 return;
535 }
536 if (command("REST %ld", (long) restart_point)
537 != CONTINUE) {
538 restart_point = 0;
539 if (closefunc != NULL)
540 (*closefunc)(fin);
541 return;
542 }
543 restart_point = 0;
544 mode = "r+w";
545 }
546 if (remote) {
547 if (command("%s %s", cmd, remote) != PRELIM) {
548 null();// (void) signal(SIGINT, oldintr);
549 if (oldintp)
550 null();// (void) signal(SIGPIPE, oldintp);
551 if (closefunc != NULL)
552 (*closefunc)(fin);
553 return;
554 }
555 } else
556 if (command("%s", cmd) != PRELIM) {
557 null();// (void) signal(SIGINT, oldintr);
558 if (oldintp)
559 null();// (void) signal(SIGPIPE, oldintp);
560 if (closefunc != NULL)
561 (*closefunc)(fin);
562 return;
563 }
564 dout = dataconn(mode);
565 if (!dout)
566 goto abort;
567 (void) gettimeofday(&start, (struct timezone *)0);
568 null();// oldintp = signal(SIGPIPE, SIG_IGN);
569 switch (type) {
570
571 case TYPE_I:
572 case TYPE_L:
573 errno = d = 0;
574 while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
575 bytes += c;
576 for (bufp = buf; c > 0; c -= d, bufp += d)
577 if ((d = send(dout, bufp, c, 0)) <= 0)
578 break;
579 if (hash) {
580 while (bytes >= hashbytes) {
581 (void) putchar('#');
582 hashbytes += HASHBYTES;
583 }
584 (void) fflush(stdout);
585 }
586 }
587 if (hash && bytes > 0) {
588 if (bytes < HASHBYTES)
589 (void) putchar('#');
590 (void) putchar('\n');
591 (void) fflush(stdout);
592 }
593 if (c < 0)
594 perror(local);
595 if (d <= 0) {
596 if (d == 0)
597 fprintf(stderr, "netout: write returned 0?\n");
598 else if (errno != EPIPE)
599 perror("netout");
600 bytes = -1;
601 }
602 break;
603
604 case TYPE_A:
605 {
606 char buf[1024];
607 static int bufsize = 1024;
608 int ipos=0;
609
610 while ((c = getc(fin)) != EOF) {
611 if (c == '\n') {
612 while (hash && (bytes >= hashbytes)) {
613 (void) putchar('#');
614 (void) fflush(stdout);
615 hashbytes += HASHBYTES;
616 }
617 // Szurgot: The following code is unncessary on Win32.
618 // (void) fputcSocket(dout, '\r');
619 // bytes++;
620 }
621
622 if (ipos >= bufsize) {
623 fputSocket(dout,buf,ipos);
624 if(!hash) (void) putchar('.');
625 ipos=0;
626 }
627 buf[ipos]=c; ++ipos;
628 bytes++;
629 }
630 if (ipos) {
631 fputSocket(dout,buf,ipos);
632 ipos=0;
633 }
634 if (hash) {
635 if (bytes < hashbytes)
636 (void) putchar('#');
637 (void) putchar('\n');
638 (void) fflush(stdout);
639 }
640 else {
641 (void) putchar('.');
642 (void) putchar('\n');
643 (void) fflush(stdout);
644 }
645 if (ferror(fin))
646 perror(local);
647 // if (ferror(dout)) {
648 // if (errno != EPIPE)
649 // perror("netout");
650 // bytes = -1;
651 // }
652 break;
653 }
654 }
655 (void) gettimeofday(&stop, (struct timezone *)0);
656 if (closefunc != NULL)
657 (*closefunc)(fin);
658 if(closesocket(dout)) {
659 int iret=WSAGetLastError ();
660 fprintf(stdout,"Error closing socket(%d)\n",iret);
661 (void) fflush(stdout);
662 }
663 (void) getreply(0);
664 null();// (void) signal(SIGINT, oldintr);
665 if (oldintp)
666 null();// (void) signal(SIGPIPE, oldintp);
667 if (bytes > 0)
668 ptransfer("sent", bytes, &start, &stop);
669 return;
670 abort:
671 (void) gettimeofday(&stop, (struct timezone *)0);
672 null();// (void) signal(SIGINT, oldintr);
673 if (oldintp)
674 null();// (void) signal(SIGPIPE, oldintp);
675 if (!cpend) {
676 code = -1;
677 return;
678 }
679 if (data >= 0) {
680 (void) close(data);
681 data = -1;
682 }
683 if (dout)
684 if(closesocket(dout)) {
685 int iret=WSAGetLastError ();
686 fprintf(stdout,"Error closing socket(%d)\n",iret);
687 (void) fflush(stdout);
688 }
689
690 (void) getreply(0);
691 code = -1;
692 if (closefunc != NULL && fin != NULL)
693 (*closefunc)(fin);
694 if (bytes > 0)
695 ptransfer("sent", bytes, &start, &stop);
696 }
697
698 jmp_buf recvabort;
699
700 #if 0
701 void abortrecv()
702 {
703
704 mflag = 0;
705 abrtflag = 0;
706 printf("\n");
707 (void) fflush(stdout);
708 longjmp(recvabort, 1);
709 }
710 #endif
711
712 void recvrequest(const char *cmd, const char *local, const char *remote, const char *mode,
713 int printnames)
714 {
715 FILE *fout = stdout;
716 int din = 0;
717 int (*closefunc)();
718 void (*oldintr)(int), (*oldintp)(int);
719 int oldverbose = 0, oldtype = 0, is_retr, tcrflag, nfnd, bare_lfs = 0;
720 char msg;
721 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
722 char buf[1024];
723 static int bufsize = 1024;
724 long bytes = 0, hashbytes = HASHBYTES;
725 // struct
726 fd_set mask;
727 register int c;
728 struct timeval start, stop;
729 // struct stat st;
730
731 is_retr = strcmp(cmd, "RETR") == 0;
732 if (is_retr && verbose && printnames) {
733 if (local && *local != '-')
734 printf("local: %s ", local);
735 if (remote)
736 printf("remote: %s\n", remote);
737 (void) fflush(stdout);
738 }
739 if (proxy && is_retr) {
740 proxtrans(cmd, local, remote);
741 return;
742 }
743 closefunc = NULL;
744 oldintr = NULL;
745 oldintp = NULL;
746 tcrflag = !crflag && is_retr;
747 if (setjmp(recvabort)) {
748 while (cpend) {
749 (void) getreply(0);
750 }
751 if (data >= 0) {
752 (void) close(data);
753 data = -1;
754 }
755 if (oldintr)
756 null();// (void) signal(SIGINT, oldintr);
757 code = -1;
758 return;
759 }
760 null();// oldintr = signal(SIGINT, abortrecv);
761 if (strcmp(local, "-") && *local != '|') {
762 #ifndef _WIN32
763 register int d;
764 // This whole thing is a problem... access Won't work on non-existent files
765 if (access(local, 2) < 0) {
766 char *dir = rindex(local, '/');
767
768 if (errno != ENOENT && errno != EACCES) {
769 perror(local);
770 (void) signal(SIGINT, oldintr);
771 code = -1;
772 return;
773 }
774 if (dir != NULL)
775 *dir = 0;
776 d = access(dir ? local : ".", 2);
777 if (dir != NULL)
778 *dir = '/';
779 if (d < 0) {
780 perror(local);
781 (void) signal(SIGINT, oldintr);
782 code = -1;
783 return;
784 }
785 if (!runique && errno == EACCES &&
786 chmod(local, 0600) < 0) {
787 perror(local);
788 (void) signal(SIGINT, oldintr);
789 code = -1;
790 return;
791 }
792 if (runique && errno == EACCES &&
793 (local = gunique(local)) == NULL) {
794 (void) signal(SIGINT, oldintr);
795 code = -1;
796 return;
797 }
798 }
799 else if (runique && (local = gunique(local)) == NULL) {
800 (void) signal(SIGINT, oldintr);
801 code = -1;
802 return;
803 }
804 #endif
805 }
806 if (initconn()) {
807 null();// (void) signal(SIGINT, oldintr);
808 code = -1;
809 return;
810 }
811 if (setjmp(recvabort))
812 goto abort;
813 if (!is_retr) {
814 if (type != TYPE_A && (allbinary == 0 || type != TYPE_I)) {
815 oldtype = type;
816 oldverbose = verbose;
817 if (!debug)
818 verbose = 0;
819 setascii(0, NULL);
820 verbose = oldverbose;
821 }
822 } else if (restart_point) {
823 if (command("REST %ld", (long) restart_point) != CONTINUE)
824 return;
825 }
826 if (remote) {
827 if (command("%s %s", cmd, remote) != PRELIM) {
828 null();// (void) signal(SIGINT, oldintr);
829 if (oldtype) {
830 if (!debug)
831 verbose = 0;
832 switch (oldtype) {
833 case TYPE_I:
834 setbinary(0, NULL);
835 break;
836 case TYPE_E:
837 setebcdic();
838 break;
839 case TYPE_L:
840 settenex(0, NULL);
841 break;
842 }
843 verbose = oldverbose;
844 }
845 return;
846 }
847 } else {
848 if (command("%s", cmd) != PRELIM) {
849 null();// (void) signal(SIGINT, oldintr);
850 if (oldtype) {
851 if (!debug)
852 verbose = 0;
853 switch (oldtype) {
854 case TYPE_I:
855 setbinary(0, NULL);
856 break;
857 case TYPE_E:
858 setebcdic();
859 break;
860 case TYPE_L:
861 settenex(0, NULL);
862 break;
863 }
864 verbose = oldverbose;
865 }
866 return;
867 }
868 }
869 din = dataconn("r");
870 if (!din)
871 goto abort;
872 if (strcmp(local, "-") == 0)
873 fout = stdout;
874 else if (*local == '|') {
875 null();// oldintp = signal(SIGPIPE, SIG_IGN);
876 fout = _popen(local + 1, "w");
877 if (fout == NULL) {
878 perror(local+1);
879 goto abort;
880 }
881 closefunc = _pclose;
882 } else {
883 fout = fopen(local, mode);
884 if (fout == NULL) {
885 perror(local);
886 goto abort;
887 }
888 closefunc = fclose;
889 }
890 (void) gettimeofday(&start, (struct timezone *)0);
891 switch (type) {
892
893 case TYPE_I:
894 case TYPE_L:
895 if (restart_point &&
896 lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
897 perror(local);
898 if (closefunc != NULL)
899 (*closefunc)(fout);
900 return;
901 }
902 errno = 0;
903 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
904 // if ((d = write(fileno(fout), buf, c)) != c)
905 // if ((d = write(fileno(fout), buf, c)) != c)
906 // break;
907 while ((c = recv(din, buf, bufsize, 0)) > 0) {
908 write(fileno(fout), buf, c);
909 bytes += c;
910 if (hash) {
911 while (bytes >= hashbytes) {
912 (void) putchar('#');
913 hashbytes += HASHBYTES;
914 }
915 (void) fflush(stdout);
916 }
917 }
918 if (hash && bytes > 0) {
919 if (bytes < HASHBYTES)
920 (void) putchar('#');
921 (void) putchar('\n');
922 (void) fflush(stdout);
923 }
924 // if (c < 0) {
925 // if (errno != EPIPE)
926 // perror("netin");
927 // bytes = -1;
928 // }
929 // if (d < c) {
930 // if (d < 0)
931 // perror(local);
932 // else
933 // fprintf(stderr, "%s: short write\n", local);
934 // }
935 break;
936
937 case TYPE_A:
938 if (restart_point) {
939 register int i, n, c;
940
941 if (fseek(fout, 0L, L_SET) < 0)
942 goto done;
943 n = restart_point;
944 i = 0;
945 while (i++ < n) {
946 if ((c=getc(fout)) == EOF)
947 goto done;
948 if (c == '\n')
949 i++;
950 }
951 if (fseek(fout, 0L, L_INCR) < 0) {
952 done:
953 perror(local);
954 if (closefunc != NULL)
955 (*closefunc)(fout);
956 return;
957 }
958 }
959 while ((c = fgetcSocket(din)) != EOF) {
960 if (c == '\n')
961 bare_lfs++;
962 while (c == '\r') {
963 while (hash && (bytes >= hashbytes)) {
964 (void) putchar('#');
965 (void) fflush(stdout);
966 hashbytes += HASHBYTES;
967 }
968 bytes++;
969 if ((c = fgetcSocket(din)) != '\n' || tcrflag) {
970 if (ferror(fout))
971 goto break2;
972 (void) putc('\r', fout);
973 if (c == '\0') {
974 bytes++;
975 goto contin2;
976 }
977 if (c == EOF)
978 goto contin2;
979 }
980 }
981 (void) putc(c, fout);
982 bytes++;
983 contin2: ;
984 }
985 break2:
986 if (bare_lfs) {
987 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
988 printf("File may not have transferred correctly.\n");
989 (void) fflush(stdout);
990 }
991 if (hash) {
992 if (bytes < hashbytes)
993 (void) putchar('#');
994 (void) putchar('\n');
995 (void) fflush(stdout);
996 }
997 // if (ferror(din)) {
998 // if (errno != EPIPE)
999 // perror("netin");
1000 // bytes = -1;
1001 // }
1002 if (ferror(fout))
1003 perror(local);
1004 break;
1005 }
1006 if (closefunc != NULL)
1007 (*closefunc)(fout);
1008 null();// (void) signal(SIGINT, oldintr);
1009 if (oldintp)
1010 null();// (void) signal(SIGPIPE, oldintp);
1011 (void) gettimeofday(&stop, (struct timezone *)0);
1012 if(closesocket(din)) {
1013 int iret=WSAGetLastError ();
1014 fprintf(stdout,"Error closing socket(%d)\n",iret);
1015 (void) fflush(stdout);
1016 }
1017
1018 (void) getreply(0);
1019 if (bytes > 0 && is_retr)
1020 ptransfer("received", bytes, &start, &stop);
1021 if (oldtype) {
1022 if (!debug)
1023 verbose = 0;
1024 switch (oldtype) {
1025 case TYPE_I:
1026 setbinary(0, NULL);
1027 break;
1028 case TYPE_E:
1029 setebcdic();
1030 break;
1031 case TYPE_L:
1032 settenex(0, NULL);
1033 break;
1034 }
1035 verbose = oldverbose;
1036 }
1037 return;
1038 abort:
1039
1040 /* abort using RFC959 recommended IP,SYNC sequence */
1041
1042 (void) gettimeofday(&stop, (struct timezone *)0);
1043 if (oldintp)
1044 null();// (void) signal(SIGPIPE, oldintr);
1045 null();// (void) signal(SIGINT,SIG_IGN);
1046 if (oldtype) {
1047 if (!debug)
1048 verbose = 0;
1049 switch (oldtype) {
1050 case TYPE_I:
1051 setbinary(0, NULL);
1052 break;
1053 case TYPE_E:
1054 setebcdic();
1055 break;
1056 case TYPE_L:
1057 settenex(0, NULL);
1058 break;
1059 }
1060 verbose = oldverbose;
1061 }
1062 if (!cpend) {
1063 code = -1;
1064 null();// (void) signal(SIGINT,oldintr);
1065 return;
1066 }
1067
1068 fprintfSocket(cout,"%c%c",IAC,IP);
1069 msg = (char)IAC;
1070 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1071 /* after urgent byte rather than before as now is protocol */
1072 if (send(cout,&msg,1,MSG_OOB) != 1) {
1073 perror("abort");
1074 }
1075 fprintfSocket(cout,"%cABOR\r\n",DM);
1076 FD_ZERO(&mask);
1077 FD_SET(cin, &mask); // Need to correct this
1078 if (din) {
1079 FD_SET(din, &mask); // Need to correct this
1080 }
1081 if ((nfnd = empty(&mask,10)) <= 0) {
1082 if (nfnd < 0) {
1083 perror("abort");
1084 }
1085 code = -1;
1086 lostpeer();
1087 }
1088 if (din && FD_ISSET(din, &mask)) {
1089 while (recv(din, buf, bufsize, 0) > 0)
1090 ;
1091 }
1092 if (getreply(0) == ERROR && code == 552) { /* needed for nic style abort */
1093 if (data >= 0) {
1094 (void) close(data);
1095 data = -1;
1096 }
1097 (void) getreply(0);
1098 }
1099 (void) getreply(0);
1100 code = -1;
1101 if (data >= 0) {
1102 (void) close(data);
1103 data = -1;
1104 }
1105 if (closefunc != NULL && fout != NULL)
1106 (*closefunc)(fout);
1107 if (din)
1108 if(closesocket(din)) {
1109 int iret=WSAGetLastError ();
1110 fprintf(stdout,"Error closing socket(%d)\n",iret);
1111 (void) fflush(stdout);
1112 }
1113
1114 if (bytes > 0)
1115 ptransfer("received", bytes, &start, &stop);
1116 null();// (void) signal(SIGINT,oldintr);
1117 }
1118
1119 int
1120 initconn()
1121 {
1122 register char *p, *a;
1123 int result, len, tmpno = 0;
1124 int on = 1;
1125 int a0, a1, a2, a3, p0, p1;
1126
1127
1128 if (passivemode) {
1129 data = socket(AF_INET, SOCK_STREAM, 0);
1130 if (data < 0) {
1131 perror("ftp: socket");
1132 return(1);
1133 }
1134 if ((options & SO_DEBUG) &&
1135 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1136 sizeof (on)) < 0)
1137 perror("ftp: setsockopt (ignored)");
1138 if (command("PASV") != COMPLETE) {
1139 printf("Passive mode refused.\n");
1140 goto bad;
1141 }
1142
1143 /*
1144 * What we've got at this point is a string of comma
1145 * separated one-byte unsigned integer values.
1146 * The first four are the an IP address. The fifth is
1147 * the MSB of the port number, the sixth is the LSB.
1148 * From that we'll prepare a sockaddr_in.
1149 */
1150
1151 if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",
1152 &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1153 printf("Passive mode address scan failure. Shouldn't happen!\n");
1154 goto bad;
1155 }
1156
1157 bzero(&data_addr, sizeof(data_addr));
1158 data_addr.sin_family = AF_INET;
1159 a = (char *)&data_addr.sin_addr.s_addr;
1160 a[0] = a0 & 0xff;
1161 a[1] = a1 & 0xff;
1162 a[2] = a2 & 0xff;
1163 a[3] = a3 & 0xff;
1164 p = (char *)&data_addr.sin_port;
1165 p[0] = p0 & 0xff;
1166 p[1] = p1 & 0xff;
1167
1168 if (connect(data, (struct sockaddr *)&data_addr,
1169 sizeof(data_addr)) < 0) {
1170 perror("ftp: connect");
1171 goto bad;
1172 }
1173 return(0);
1174 }
1175
1176
1177 noport:
1178 data_addr = myctladdr;
1179 if (sendport)
1180 data_addr.sin_port = 0; /* let system pick one */
1181 if (data != -1)
1182 (void) close (data);
1183 data = socket(AF_INET, SOCK_STREAM, 0);
1184 if (data < 0) {
1185 perror("ftp: socket");
1186 if (tmpno)
1187 sendport = 1;
1188 return (1);
1189 }
1190 if (!sendport)
1191 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
1192 perror("ftp: setsockopt (reuse address)");
1193 goto bad;
1194 }
1195 if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
1196 perror("ftp: bind");
1197 goto bad;
1198 }
1199 if (options & SO_DEBUG &&
1200 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
1201 perror("ftp: setsockopt (ignored)");
1202 len = sizeof (data_addr);
1203 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1204 perror("ftp: getsockname");
1205 goto bad;
1206 }
1207 if (listen(data, 1) < 0)
1208 perror("ftp: listen");
1209 if (sendport) {
1210 a = (char *)&data_addr.sin_addr;
1211 p = (char *)&data_addr.sin_port;
1212 #define UC(b) (((int)b)&0xff)
1213 result =
1214 command("PORT %d,%d,%d,%d,%d,%d",
1215 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1216 UC(p[0]), UC(p[1]));
1217 if (result == ERROR && sendport == -1) {
1218 sendport = 0;
1219 tmpno = 1;
1220 goto noport;
1221 }
1222 return (result != COMPLETE);
1223 }
1224 if (tmpno)
1225 sendport = 1;
1226 return (0);
1227 bad:
1228 (void) fflush(stdout);
1229 (void) close(data), data = -1;
1230 if (tmpno)
1231 sendport = 1;
1232 return (1);
1233 }
1234
1235 int dataconn(const char *mode)
1236 {
1237 struct sockaddr_in from;
1238 int s, fromlen = sizeof (from);
1239
1240 if (passivemode)
1241 return (data);
1242
1243 s = accept(data, (struct sockaddr *) &from, &fromlen);
1244 if (s < 0) {
1245 perror("ftp: accept");
1246 (void) closesocket(data), data = -1;
1247 return 0;
1248 }
1249 if(closesocket(data)) {
1250 int iret=WSAGetLastError ();
1251 fprintf(stdout,"Error closing socket(%d)\n",iret);
1252 (void) fflush(stdout);
1253 }
1254
1255 data = s;
1256 return (data);
1257 }
1258
1259 void ptransfer(direction, bytes, t0, t1)
1260 const char *direction;
1261 long bytes;
1262 struct timeval *t0, *t1;
1263 {
1264 struct timeval td;
1265 double s, bs;
1266
1267 if (verbose) {
1268 tvsub(&td, t1, t0);
1269 s = td.tv_sec + (td.tv_usec / 1000000.);
1270 #define nz(x) ((x) == 0 ? 1 : (x))
1271 bs = bytes / nz(s);
1272 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1273 bytes, direction, s, bs / 1024.);
1274 (void) fflush(stdout);
1275 }
1276 }
1277
1278 /*tvadd(tsum, t0)
1279 struct timeval *tsum, *t0;
1280 {
1281
1282 tsum->tv_sec += t0->tv_sec;
1283 tsum->tv_usec += t0->tv_usec;
1284 if (tsum->tv_usec > 1000000)
1285 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1286 } */
1287
1288 void tvsub(tdiff, t1, t0)
1289 struct timeval *tdiff, *t1, *t0;
1290 {
1291
1292 tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
1293 tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
1294 if (tdiff->tv_usec < 0)
1295 tdiff->tv_sec--, tdiff->tv_usec += 1000000;
1296 }
1297
1298 void psabort(int flag)
1299 {
1300 extern int abrtflag;
1301
1302 abrtflag++;
1303 }
1304
1305 void pswitch(int flag)
1306 {
1307 extern int proxy, abrtflag;
1308 Sig_t oldintr;
1309 static struct comvars {
1310 int connect;
1311 char name[MAXHOSTNAMELEN];
1312 struct sockaddr_in mctl;
1313 struct sockaddr_in hctl;
1314 SOCKET in;
1315 SOCKET out;
1316 int tpe;
1317 int cpnd;
1318 int sunqe;
1319 int runqe;
1320 int mcse;
1321 int ntflg;
1322 char nti[17];
1323 char nto[17];
1324 int mapflg;
1325 char mi[MAXPATHLEN];
1326 char mo[MAXPATHLEN];
1327 } proxstruct, tmpstruct;
1328 struct comvars *ip, *op;
1329
1330 abrtflag = 0;
1331 oldintr = signal(SIGINT, psabort);
1332 if (flag) {
1333 if (proxy)
1334 return;
1335 ip = &tmpstruct;
1336 op = &proxstruct;
1337 proxy++;
1338 }
1339 else {
1340 if (!proxy)
1341 return;
1342 ip = &proxstruct;
1343 op = &tmpstruct;
1344 proxy = 0;
1345 }
1346 ip->connect = connected;
1347 connected = op->connect;
1348 if (hostname) {
1349 (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1350 ip->name[strlen(ip->name)] = '\0';
1351 } else
1352 ip->name[0] = 0;
1353 hostname = op->name;
1354 ip->hctl = hisctladdr;
1355 hisctladdr = op->hctl;
1356 ip->mctl = myctladdr;
1357 myctladdr = op->mctl;
1358 ip->in = cin;
1359 cin = op->in;
1360 ip->out = cout;
1361 cout = op->out;
1362 ip->tpe = type;
1363 type = op->tpe;
1364 if (!type)
1365 type = 1;
1366 ip->cpnd = cpend;
1367 cpend = op->cpnd;
1368 ip->sunqe = sunique;
1369 sunique = op->sunqe;
1370 ip->runqe = runique;
1371 runique = op->runqe;
1372 ip->mcse = mcase;
1373 mcase = op->mcse;
1374 ip->ntflg = ntflag;
1375 ntflag = op->ntflg;
1376 (void) strncpy(ip->nti, ntin, 16);
1377 (ip->nti)[strlen(ip->nti)] = '\0';
1378 (void) strcpy(ntin, op->nti);
1379 (void) strncpy(ip->nto, ntout, 16);
1380 (ip->nto)[strlen(ip->nto)] = '\0';
1381 (void) strcpy(ntout, op->nto);
1382 ip->mapflg = mapflag;
1383 mapflag = op->mapflg;
1384 (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
1385 (ip->mi)[strlen(ip->mi)] = '\0';
1386 (void) strcpy(mapin, op->mi);
1387 (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
1388 (ip->mo)[strlen(ip->mo)] = '\0';
1389 (void) strcpy(mapout, op->mo);
1390 // (void) signal(SIGINT, oldintr);
1391 if (abrtflag) {
1392 abrtflag = 0;
1393 (*oldintr)(1);
1394 }
1395 }
1396
1397 jmp_buf ptabort;
1398 int ptabflg;
1399
1400 #if 0
1401 void
1402 abortpt()
1403 {
1404 printf("\n");
1405 (void) fflush(stdout);
1406 ptabflg++;
1407 mflag = 0;
1408 abrtflag = 0;
1409 longjmp(ptabort, 1);
1410 }
1411 #endif
1412
1413 void proxtrans(cmd, local, remote)
1414 const char *cmd, *local, *remote;
1415 {
1416 // void (*oldintr)(int);
1417 int tmptype, oldtype = 0, secndflag = 0, nfnd;
1418 extern jmp_buf ptabort;
1419 const char *cmd2;
1420 // struct
1421 fd_set mask;
1422
1423 if (strcmp(cmd, "RETR"))
1424 cmd2 = "RETR";
1425 else
1426 cmd2 = runique ? "STOU" : "STOR";
1427 if (command("PASV") != COMPLETE) {
1428 printf("proxy server does not support third part transfers.\n");
1429 (void) fflush(stdout);
1430 return;
1431 }
1432 tmptype = type;
1433 pswitch(0);
1434 if (!connected) {
1435 printf("No primary connection\n");
1436 (void) fflush(stdout);
1437 pswitch(1);
1438 code = -1;
1439 return;
1440 }
1441 if (type != tmptype) {
1442 oldtype = type;
1443 switch (tmptype) {
1444 case TYPE_A:
1445 setascii(0, NULL);
1446 break;
1447 case TYPE_I:
1448 setbinary(0, NULL);
1449 break;
1450 case TYPE_E:
1451 setebcdic();
1452 break;
1453 case TYPE_L:
1454 settenex(0, NULL);
1455 break;
1456 }
1457 }
1458 if (command("PORT %s", pasv) != COMPLETE) {
1459 switch (oldtype) {
1460 case 0:
1461 break;
1462 case TYPE_A:
1463 setascii(0, NULL);
1464 break;
1465 case TYPE_I:
1466 setbinary(0, NULL);
1467 break;
1468 case TYPE_E:
1469 setebcdic();
1470 break;
1471 case TYPE_L:
1472 settenex(0, NULL);
1473 break;
1474 }
1475 pswitch(1);
1476 return;
1477 }
1478 if (setjmp(ptabort))
1479 goto abort;
1480 null();// oldintr = signal(SIGINT, abortpt);
1481 if (command("%s %s", cmd, remote) != PRELIM) {
1482 null();// (void) signal(SIGINT, oldintr);
1483 switch (oldtype) {
1484 case 0:
1485 break;
1486 case TYPE_A:
1487 setascii(0, NULL);
1488 break;
1489 case TYPE_I:
1490 setbinary(0, NULL);
1491 break;
1492 case TYPE_E:
1493 setebcdic();
1494 break;
1495 case TYPE_L:
1496 settenex(0, NULL);
1497 break;
1498 }
1499 pswitch(1);
1500 return;
1501 }
1502 sleep(2);
1503 pswitch(1);
1504 secndflag++;
1505 if (command("%s %s", cmd2, local) != PRELIM)
1506 goto abort;
1507 ptflag++;
1508 (void) getreply(0);
1509 pswitch(0);
1510 (void) getreply(0);
1511 null();// (void) signal(SIGINT, oldintr);
1512 switch (oldtype) {
1513 case 0:
1514 break;
1515 case TYPE_A:
1516 setascii(0, NULL);
1517 break;
1518 case TYPE_I:
1519 setbinary(0, NULL);
1520 break;
1521 case TYPE_E:
1522 setebcdic();
1523 break;
1524 case TYPE_L:
1525 settenex(0, NULL);
1526 break;
1527 }
1528 pswitch(1);
1529 ptflag = 0;
1530 printf("local: %s remote: %s\n", local, remote);
1531 (void) fflush(stdout);
1532 return;
1533 abort:
1534 null();// (void) signal(SIGINT, SIG_IGN);
1535 ptflag = 0;
1536 if (strcmp(cmd, "RETR") && !proxy)
1537 pswitch(1);
1538 else if (!strcmp(cmd, "RETR") && proxy)
1539 pswitch(0);
1540 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1541 if (command("%s %s", cmd2, local) != PRELIM) {
1542 pswitch(0);
1543 switch (oldtype) {
1544 case 0:
1545 break;
1546 case TYPE_A:
1547 setascii(0, NULL);
1548 break;
1549 case TYPE_I:
1550 setbinary(0, NULL);
1551 break;
1552 case TYPE_E:
1553 setebcdic();
1554 break;
1555 case TYPE_L:
1556 settenex(0, NULL);
1557 break;
1558 }
1559 if (cpend) {
1560 char msg[2];
1561
1562 fprintfSocket(cout,"%c%c",IAC,IP);
1563 *msg = (char) IAC;
1564 *(msg+1) = (char) DM;
1565 if (send(cout,msg,2,MSG_OOB) != 2)
1566 perror("abort");
1567 fprintfSocket(cout,"ABOR\r\n");
1568 FD_ZERO(&mask);
1569 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1570 if ((nfnd = empty(&mask,10)) <= 0) {
1571 if (nfnd < 0) {
1572 perror("abort");
1573 }
1574 if (ptabflg)
1575 code = -1;
1576 lostpeer();
1577 }
1578 (void) getreply(0);
1579 (void) getreply(0);
1580 }
1581 }
1582 pswitch(1);
1583 if (ptabflg)
1584 code = -1;
1585 null();// (void) signal(SIGINT, oldintr);
1586 return;
1587 }
1588 if (cpend) {
1589 char msg[2];
1590
1591 fprintfSocket(cout,"%c%c",IAC,IP);
1592 *msg = (char)IAC;
1593 *(msg+1) = (char)DM;
1594 if (send(cout,msg,2,MSG_OOB) != 2)
1595 perror("abort");
1596 fprintfSocket(cout,"ABOR\r\n");
1597 FD_ZERO(&mask);
1598 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1599 if ((nfnd = empty(&mask,10)) <= 0) {
1600 if (nfnd < 0) {
1601 perror("abort");
1602 }
1603 if (ptabflg)
1604 code = -1;
1605 lostpeer();
1606 }
1607 (void) getreply(0);
1608 (void) getreply(0);
1609 }
1610 pswitch(!proxy);
1611 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1612 if (command("%s %s", cmd2, local) != PRELIM) {
1613 pswitch(0);
1614 switch (oldtype) {
1615 case 0:
1616 break;
1617 case TYPE_A:
1618 setascii(0, NULL);
1619 break;
1620 case TYPE_I:
1621 setbinary(0, NULL);
1622 break;
1623 case TYPE_E:
1624 setebcdic();
1625 break;
1626 case TYPE_L:
1627 settenex(0, NULL);
1628 break;
1629 }
1630 if (cpend) {
1631 char msg[2];
1632
1633 fprintfSocket(cout,"%c%c",IAC,IP);
1634 *msg = (char)IAC;
1635 *(msg+1) = (char)DM;
1636 if (send(cout,msg,2,MSG_OOB) != 2)
1637 perror("abort");
1638 fprintfSocket(cout,"ABOR\r\n");
1639 FD_ZERO(&mask);
1640 // FD_SET(fileno(cin), &mask); // Chris:
1641 if ((nfnd = empty(&mask,10)) <= 0) {
1642 if (nfnd < 0) {
1643 perror("abort");
1644 }
1645 if (ptabflg)
1646 code = -1;
1647 lostpeer();
1648 }
1649 (void) getreply(0);
1650 (void) getreply(0);
1651 }
1652 pswitch(1);
1653 if (ptabflg)
1654 code = -1;
1655 null();// (void) signal(SIGINT, oldintr);
1656 return;
1657 }
1658 }
1659 if (cpend) {
1660 char msg[2];
1661
1662 fprintfSocket(cout,"%c%c",IAC,IP);
1663 *msg = (char)IAC;
1664 *(msg+1) = (char)DM;
1665 if (send(cout,msg,2,MSG_OOB) != 2)
1666 perror("abort");
1667 fprintfSocket(cout,"ABOR\r\n");
1668 FD_ZERO(&mask);
1669 // FD_SET(fileno(cin), &mask); // Chris:
1670 if ((nfnd = empty(&mask,10)) <= 0) {
1671 if (nfnd < 0) {
1672 perror("abort");
1673 }
1674 if (ptabflg)
1675 code = -1;
1676 lostpeer();
1677 }
1678 (void) getreply(0);
1679 (void) getreply(0);
1680 }
1681 pswitch(!proxy);
1682 if (cpend) {
1683 FD_ZERO(&mask);
1684 // FD_SET(fileno(cin), &mask); // Chris:
1685 if ((nfnd = empty(&mask,10)) <= 0) {
1686 if (nfnd < 0) {
1687 perror("abort");
1688 }
1689 if (ptabflg)
1690 code = -1;
1691 lostpeer();
1692 }
1693 (void) getreply(0);
1694 (void) getreply(0);
1695 }
1696 if (proxy)
1697 pswitch(0);
1698 switch (oldtype) {
1699 case 0:
1700 break;
1701 case TYPE_A:
1702 setascii(0, NULL);
1703 break;
1704 case TYPE_I:
1705 setbinary(0, NULL);
1706 break;
1707 case TYPE_E:
1708 setebcdic();
1709 break;
1710 case TYPE_L:
1711 settenex(0, NULL);
1712 break;
1713 }
1714 pswitch(1);
1715 if (ptabflg)
1716 code = -1;
1717 null();// (void) signal(SIGINT, oldintr);
1718 }
1719
1720 void reset(int argc, const char *argv[])
1721 {
1722 // struct
1723 fd_set mask;
1724 int nfnd = 1;
1725
1726 FD_ZERO(&mask);
1727 while (nfnd > 0) {
1728 // FD_SET(fileno(cin), &mask); // Chris
1729 if ((nfnd = empty(&mask,0)) < 0) {
1730 perror("reset");
1731 code = -1;
1732 lostpeer();
1733 }
1734 else if (nfnd) {
1735 (void) getreply(0);
1736 }
1737 }
1738 }
1739
1740 #if 0
1741 char *
1742 gunique(local)
1743 char *local;
1744 {
1745 static char new[MAXPATHLEN];
1746 char *cp = rindex(local, '/');
1747 int d, count=0;
1748 char ext = '1';
1749
1750 if (cp)
1751 *cp = '\0';
1752 d = access(cp ? local : ".", 2);
1753 if (cp)
1754 *cp = '/';
1755 if (d < 0) {
1756 perror(local);
1757 return((char *) 0);
1758 }
1759 (void) strcpy(new, local);
1760 cp = new + strlen(new);
1761 *cp++ = '.';
1762 while (!d) {
1763 if (++count == 100) {
1764 printf("runique: can't find unique file name.\n");
1765 (void) fflush(stdout);
1766 return((char *) 0);
1767 }
1768 *cp++ = ext;
1769 *cp = '\0';
1770 if (ext == '9')
1771 ext = '0';
1772 else
1773 ext++;
1774 if ((d = access(new, 0)) < 0)
1775 break;
1776 if (ext != '0')
1777 cp--;
1778 else if (*(cp - 2) == '.')
1779 *(cp - 1) = '1';
1780 else {
1781 *(cp - 2) = *(cp - 2) + 1;
1782 cp--;
1783 }
1784 }
1785 return(new);
1786 }
1787 #endif
1788
1789 int null(void)
1790 {
1791 return 0;
1792 }