ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

ftp.c
Go to the documentation of this file.
00001 #define L_SET SEEK_SET
00002 #define L_INCR SEEK_CUR
00003 #define caddr_t void *
00004 /*
00005  * Copyright (c) 1985, 1989 Regents of the University of California.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms are permitted
00009  * provided that the above copyright notice and this paragraph are
00010  * duplicated in all such forms and that any documentation,
00011  * advertising materials, and other materials related to such
00012  * distribution and use acknowledge that the software was developed
00013  * by the University of California, Berkeley.  The name of the
00014  * University may not be used to endorse or promote products derived
00015  * from this software without specific prior written permission.
00016  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00019  */
00020 
00021 #ifndef lint
00022 static char sccsid[] = "@(#)ftp.c   5.28 (Berkeley) 4/20/89";
00023 #endif /* not lint */
00024 
00025 #include "precomp.h"
00026 
00027 #ifndef MAXHOSTNAMELEN
00028 #define MAXHOSTNAMELEN 64
00029 #endif
00030 
00031 #ifdef NOVFPRINTF
00032 #define vfprintf(a,b,c) _doprnt(b,c,a)
00033 #endif
00034 
00035 #ifdef sun
00036 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t  presence */
00037 #ifndef FD_SET
00038 #define NBBY    8       /* number of bits in a byte */
00039 /*
00040  * Select uses bit masks of file descriptors in longs.
00041  * These macros manipulate such bit fields (the filesystem macros use chars).
00042  * FD_SETSIZE may be defined by the user, but the default here
00043  * should be >= NOFILE (param.h).
00044  */
00045 #ifndef FD_SETSIZE
00046 #define FD_SETSIZE  256
00047 #endif
00048 
00049 typedef long    fd_mask;
00050 #define NFDBITS (sizeof(fd_mask) * NBBY)    /* bits per mask */
00051 #ifndef howmany
00052 #define howmany(x, y)   (((x)+((y)-1))/(y))
00053 #endif
00054 
00055 #define FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
00056 #define FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
00057 #define FD_ISSET(n, p)  ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
00058 #define FD_ZERO(p)  bzero((char *)(p), sizeof(*(p)))
00059 
00060 typedef int uid_t;
00061 typedef int gid_t;
00062 #endif
00063 #endif
00064 
00065 struct  sockaddr_in hisctladdr;
00066 struct  sockaddr_in data_addr;
00067 int data = -1;
00068 int abrtflag = 0;
00069 int ptflag = 0;
00070 int allbinary;
00071 struct  sockaddr_in myctladdr;
00072 uid_t   getuid();
00073 sig_t   lostpeer();
00074 off_t   restart_point = 0;
00075 
00076 SOCKET cin, cout;
00077 int dataconn(const char *mode);
00078 
00079 int command(const char *fmt, ...);
00080 
00081 char *hostname;
00082 
00083 typedef void (*Sig_t)(int);
00084 
00085 // Signal Handlers
00086 
00087 void psabort(int sig);
00088 
00089 char *hookup(const char *host, int port)
00090 {
00091     register struct hostent *hp = 0;
00092     int len;
00093     SOCKET s;
00094     static char hostnamebuf[80];
00095 
00096     bzero((char *)&hisctladdr, sizeof (hisctladdr));
00097     hisctladdr.sin_addr.s_addr = inet_addr(host);
00098     if (hisctladdr.sin_addr.s_addr != (unsigned long)-1) {
00099         hisctladdr.sin_family = AF_INET;
00100         (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
00101     } else {
00102         hp = gethostbyname(host);
00103         if (hp == NULL) {
00104             fprintf(stderr, "ftp: %s: ", host);
00105             herror((char *)NULL);
00106             code = -1;
00107             return((char *) 0);
00108         }
00109         hisctladdr.sin_family = hp->h_addrtype;
00110         bcopy(hp->h_addr_list[0],
00111              (caddr_t)&hisctladdr.sin_addr, hp->h_length);
00112         (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
00113     }
00114     hostname = hostnamebuf;
00115     s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
00116     if (s == INVALID_SOCKET) {
00117         perror("ftp: socket");
00118         code = -1;
00119         return (0);
00120     }
00121     hisctladdr.sin_port = port;
00122     while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
00123         if (hp && hp->h_addr_list[1]) {
00124             int oerrno = errno;
00125 
00126             fprintf(stderr, "ftp: connect to address %s: ",
00127                 inet_ntoa(hisctladdr.sin_addr));
00128             errno = oerrno;
00129             perror((char *) 0);
00130             hp->h_addr_list++;
00131             bcopy(hp->h_addr_list[0],
00132                  (caddr_t)&hisctladdr.sin_addr, hp->h_length);
00133             fprintf(stdout, "Trying %s...\n",
00134                 inet_ntoa(hisctladdr.sin_addr));
00135             (void) fflush(stdout);
00136             (void) close(s);
00137             s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
00138             if (s == INVALID_SOCKET) {
00139                 perror("ftp: socket");
00140                 code = -1;
00141                 return (0);
00142             }
00143             continue;
00144         }
00145         perror("ftp: connect");
00146         code = -1;
00147         goto bad;
00148     }
00149     len = sizeof (myctladdr);
00150     if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
00151         perror("ftp: getsockname");
00152         code = -1;
00153         goto bad;
00154     }
00155     cin = cout = s;
00156     if (verbose) {
00157         printf("Connected to %s.\n", hostname);
00158         (void) fflush(stdout);
00159     }
00160     if (getreply(0) > 2) {  /* read startup message from server */
00161         closesocket(cin);
00162         code = -1;
00163         goto bad;
00164     }
00165 #ifdef SO_OOBINLINE
00166     {
00167     int on = 1;
00168 
00169     if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (const char *) &on, sizeof(on))
00170         < 0 && debug) {
00171             perror("ftp: setsockopt");
00172         }
00173     }
00174 #endif //SO_OOBINLINE
00175 
00176     return (hostname);
00177 bad:
00178     (void) close(s);
00179     return ((char *)0);
00180 }
00181 
00182 int login(const char *host)
00183 {
00184     char tmp[80];
00185     char *puser, *ppass, *pacct;
00186     const char *user, *pass, *acct;
00187     int n, aflag = 0;
00188 
00189     user = pass = acct = 0;
00190     n = ruserpass(host, &puser, &ppass, &pacct);
00191     if (n < 0) {
00192         code = -1;
00193         return(0);
00194     }
00195     if (0 != n) {
00196         user = puser;
00197         pass = ppass;
00198         acct = pacct;
00199     }
00200     while (user == NULL) {
00201         const char *myname = "none"; // This needs to become the usename env
00202 
00203         if (myname)
00204             printf("Name (%s:%s): ", host, myname);
00205         else
00206             printf("Name (%s): ", host);
00207         (void) fflush(stdout);
00208         (void) fgets(tmp, sizeof(tmp) - 1, stdin);
00209         tmp[strlen(tmp) - 1] = '\0';
00210         if (*tmp == '\0')
00211             user = myname;
00212         else
00213             user = tmp;
00214     }
00215     n = command("USER %s", user);
00216     if (n == CONTINUE) {
00217         if (pass == NULL)
00218             pass = getpass("Password:");
00219         n = command("PASS %s", pass);
00220         fflush(stdin);
00221     }
00222     if (n == CONTINUE) {
00223         aflag++;
00224         acct = getpass("Account:");
00225         n = command("ACCT %s", acct);
00226     }
00227     if (n != COMPLETE) {
00228         fprintf(stderr, "Login failed.\n");
00229         return (0);
00230     }
00231     if (!aflag && acct != NULL)
00232         (void) command("ACCT %s", acct);
00233     if (proxy)
00234         return(1);
00235     for (n = 0; n < macnum; ++n) {
00236         if (!strcmp("init", macros[n].mac_name)) {
00237             (void) strcpy(line, "$init");
00238             makeargv();
00239             domacro(margc, margv);
00240             break;
00241         }
00242     }
00243     return (1);
00244 }
00245 
00246 static void
00247 cmdabort(int sig)
00248 {
00249     extern jmp_buf ptabort;
00250 
00251     printf("\n");
00252     (void) fflush(stdout);
00253     abrtflag++;
00254     if (ptflag)
00255         longjmp(ptabort,1);
00256 }
00257 
00258 /*VARARGS1*/
00259 int command(const char *fmt, ...)
00260 {
00261     va_list ap;
00262     int r;
00263     void (*oldintr)(int);
00264 
00265     abrtflag = 0;
00266     if (debug) {
00267         printf("---> ");
00268         va_start(ap, fmt);
00269         vfprintf(stdout, fmt, ap);
00270         va_end(ap);
00271         printf("\n");
00272         (void) fflush(stdout);
00273     }
00274     if (cout == 0) {
00275         perror ("No control connection for command");
00276         code = -1;
00277         return (0);
00278     }
00279     oldintr = signal(SIGINT,cmdabort);
00280     {
00281         char buffer[1024];
00282 
00283         va_start(ap, fmt);
00284         vsprintf(buffer, fmt, ap);
00285         va_end(ap);
00286 //DLJ: to work through  firewalls - send the command as a single message
00287         strcat(buffer,"\r\n");
00288         fprintfSocket(cout, buffer);
00289     }
00290 //DLJ: the following two lines are replaced by the strcat above - seems to
00291 // make it work through firewalls.
00292 //  fprintfSocket(cout, "\r\n");
00293 //  (void) fflush(cout);
00294     cpend = 1;
00295     r = getreply(!strcmp(fmt, "QUIT"));
00296     if (abrtflag && oldintr != SIG_IGN)
00297         (*oldintr)(SIGINT);
00298 //  (void) signal(SIGINT, oldintr);
00299     return(r);
00300 }
00301 
00302 char reply_string[BUFSIZ];      /* last line of previous reply */
00303 
00304 #include <ctype.h>
00305 
00306 int
00307 getreply(expecteof)
00308     int expecteof;
00309 {
00310     register int c, n;
00311     register int dig;
00312     register char *cp;
00313     int originalcode = 0, continuation = 0;
00314     void (*oldintr)(int);
00315     int pflag = 0;
00316     char *pt = pasv;
00317 
00318     oldintr = signal(SIGINT,cmdabort);
00319     for (;;) {
00320         dig = n = code = 0;
00321         cp = reply_string;
00322         while ((c = fgetcSocket(cin)) != '\n') {
00323             if (c == IAC) {     /* handle telnet commands */
00324                 switch (fgetcSocket(cin)) {
00325                 case WILL:
00326                 case WONT:
00327                     c = fgetcSocket(cin);
00328                     fprintfSocket(cout, "%c%c%c",IAC,DONT,c);
00329                     break;
00330                 case DO:
00331                 case DONT:
00332                     c = fgetcSocket(cin);
00333                     fprintfSocket(cout, "%c%c%c",IAC,WONT,c);
00334                     break;
00335                 default:
00336                     break;
00337                 }
00338                 continue;
00339             }
00340             dig++;
00341             if (c == EOF) {
00342                 if (expecteof) {
00343 //                  (void) signal(SIGINT,oldintr);
00344                     code = 221;
00345                     return (0);
00346                 }
00347                 lostpeer();
00348                 if (verbose) {
00349                     printf("421 Service not available, remote server has closed connection\n");
00350                     (void) fflush(stdout);
00351                 }
00352                 code = 421;
00353                 return(4);
00354             }
00355             if (c != '\r' && (verbose > 0 ||
00356                 (verbose > -1 && n == '5' && dig > 4))) {
00357                 if (proxflag &&
00358                    ((dig == 1 || dig == 5) && verbose == 0))
00359                     printf("%s:",hostname);
00360                 (void) putchar(c);
00361                 (void) fflush(stdout);
00362             }
00363             if (dig < 4 && isdigit(c))
00364                 code = code * 10 + (c - '0');
00365             if (!pflag && code == 227)
00366                 pflag = 1;
00367             if (dig > 4 && pflag == 1 && isdigit(c))
00368                 pflag = 2;
00369             if (pflag == 2) {
00370                 if (c != '\r' && c != ')')
00371                     *pt++ = c;
00372                 else {
00373                     *pt = '\0';
00374                     pflag = 3;
00375                 }
00376             }
00377             if (dig == 4 && c == '-') {
00378                 if (continuation)
00379                     code = 0;
00380                 continuation++;
00381             }
00382             if (n == 0)
00383                 n = c;
00384             if (cp < &reply_string[sizeof(reply_string) - 1])
00385                 *cp++ = c;
00386         }
00387         if (verbose > 0 || (verbose > -1 && n == '5')) {
00388             (void) putchar(c);
00389             (void) fflush (stdout);
00390         }
00391         if (continuation && code != originalcode) {
00392             if (originalcode == 0)
00393                 originalcode = code;
00394             continue;
00395         }
00396         *cp = '\0';
00397         if (n != '1')
00398             cpend = 0;
00399         (void) signal(SIGINT,oldintr);
00400         if (code == 421 || originalcode == 421)
00401             lostpeer();
00402         if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
00403             (*oldintr)(SIGINT);
00404         return (n - '0');
00405     }
00406 }
00407 
00408 static int
00409 empty(mask, sec)
00410     struct fd_set *mask;
00411     int sec;
00412 {
00413     struct timeval t;
00414 
00415     t.tv_sec = (long) sec;
00416     t.tv_usec = 0;
00417     return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
00418 }
00419 
00420 jmp_buf sendabort;
00421 
00422 #if 0
00423 void abortsend()
00424 {
00425 
00426     mflag = 0;
00427     abrtflag = 0;
00428     printf("\nsend aborted\n");
00429     (void) fflush(stdout);
00430     longjmp(sendabort, 1);
00431 }
00432 #endif
00433 
00434 #define HASHBYTES 1024
00435 
00436 void sendrequest(const char *cmd, const char *local, const char *remote, int printnames)
00437 {
00438     FILE *fin;
00439     int dout = 0;
00440     int (*closefunc)();
00441     sig_t (*oldintr)(), (*oldintp)();
00442     char buf[BUFSIZ], *bufp;
00443     long bytes = 0, hashbytes = HASHBYTES;
00444     register int c, d;
00445     struct stat st;
00446     struct timeval start, stop;
00447     const char *mode;
00448 
00449     if (verbose && printnames) {
00450         if (local && *local != '-')
00451             printf("local: %s ", local);
00452         if (remote)
00453             printf("remote: %s\n", remote);
00454         (void) fflush(stdout);
00455     }
00456     if (proxy) {
00457         proxtrans(cmd, local, remote);
00458         return;
00459     }
00460     closefunc = NULL;
00461     oldintr = NULL;
00462     oldintp = NULL;
00463     mode = "w";
00464     if (setjmp(sendabort)) {
00465         while (cpend) {
00466             (void) getreply(0);
00467         }
00468         if (data >= 0) {
00469             (void) close(data);
00470             data = -1;
00471         }
00472         if (oldintr)
00473 null();//           (void) signal(SIGINT,oldintr);
00474         if (oldintp)
00475 null();//           (void) signal(SIGPIPE,oldintp);
00476         code = -1;
00477         return;
00478     }
00479 null();//   oldintr = signal(SIGINT, abortsend);
00480     if (strcmp(local, "-") == 0)
00481         fin = stdin;
00482     else if (*local == '|') {
00483 null();//       oldintp = signal(SIGPIPE,SIG_IGN);
00484         fin = _popen(local + 1, "r");
00485         if (fin == NULL) {
00486             perror(local + 1);
00487 null();//           (void) signal(SIGINT, oldintr);
00488 null();//           (void) signal(SIGPIPE, oldintp);
00489             code = -1;
00490             return;
00491         }
00492         closefunc = _pclose;
00493     } else {
00494         fin = fopen(local, "r");
00495         if (fin == NULL) {
00496             perror(local);
00497 null();//           (void) signal(SIGINT, oldintr);
00498             code = -1;
00499             return;
00500         }
00501         closefunc = fclose;
00502         if (fstat(fileno(fin), &st) < 0 ||
00503             (st.st_mode&S_IFMT) != S_IFREG) {
00504             fprintf(stdout, "%s: not a plain file.\n", local);
00505             (void) fflush(stdout);
00506 null();//           (void) signal(SIGINT, oldintr);
00507             fclose(fin);
00508             code = -1;
00509             return;
00510         }
00511     }
00512     if (initconn()) {
00513 null();//       (void) signal(SIGINT, oldintr);
00514         if (oldintp)
00515 null();//           (void) signal(SIGPIPE, oldintp);
00516         code = -1;
00517         if (closefunc != NULL)
00518             (*closefunc)(fin);
00519         return;
00520     }
00521     if (setjmp(sendabort))
00522         goto abort;
00523 
00524     if (restart_point &&
00525         (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
00526         if (fseek(fin, (long) restart_point, 0) < 0) {
00527             perror(local);
00528             restart_point = 0;
00529             if (closefunc != NULL)
00530                 (*closefunc)(fin);
00531             return;
00532         }
00533         if (command("REST %ld", (long) restart_point)
00534             != CONTINUE) {
00535             restart_point = 0;
00536             if (closefunc != NULL)
00537                 (*closefunc)(fin);
00538             return;
00539         }
00540         restart_point = 0;
00541         mode = "r+w";
00542     }
00543     if (remote) {
00544         if (command("%s %s", cmd, remote) != PRELIM) {
00545 null();//           (void) signal(SIGINT, oldintr);
00546             if (oldintp)
00547 null();//               (void) signal(SIGPIPE, oldintp);
00548             if (closefunc != NULL)
00549                 (*closefunc)(fin);
00550             return;
00551         }
00552     } else
00553         if (command("%s", cmd) != PRELIM) {
00554 null();//           (void) signal(SIGINT, oldintr);
00555             if (oldintp)
00556 null();//               (void) signal(SIGPIPE, oldintp);
00557             if (closefunc != NULL)
00558                 (*closefunc)(fin);
00559             return;
00560         }
00561     dout = dataconn(mode);
00562     if (!dout)
00563         goto abort;
00564     (void) gettimeofday(&start, (struct timezone *)0);
00565 null();//   oldintp = signal(SIGPIPE, SIG_IGN);
00566     switch (type) {
00567 
00568     case TYPE_I:
00569     case TYPE_L:
00570         errno = d = 0;
00571         while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
00572             bytes += c;
00573             for (bufp = buf; c > 0; c -= d, bufp += d)
00574                 if ((d = send(dout, bufp, c, 0)) <= 0)
00575                     break;
00576             if (hash) {
00577                 while (bytes >= hashbytes) {
00578                     (void) putchar('#');
00579                     hashbytes += HASHBYTES;
00580                 }
00581                 (void) fflush(stdout);
00582             }
00583         }
00584         if (hash && bytes > 0) {
00585             if (bytes < HASHBYTES)
00586                 (void) putchar('#');
00587             (void) putchar('\n');
00588             (void) fflush(stdout);
00589         }
00590         if (c < 0)
00591             perror(local);
00592         if (d <= 0) {
00593             if (d == 0)
00594                 fprintf(stderr, "netout: write returned 0?\n");
00595             else if (errno != EPIPE)
00596                 perror("netout");
00597             bytes = -1;
00598         }
00599         break;
00600 
00601     case TYPE_A:
00602         {
00603         char buf[1024];
00604         static int bufsize = 1024;
00605         int ipos=0;
00606 
00607         while ((c = getc(fin)) != EOF) {
00608             if (c == '\n') {
00609                 while (hash && (bytes >= hashbytes)) {
00610                     (void) putchar('#');
00611                     (void) fflush(stdout);
00612                     hashbytes += HASHBYTES;
00613                 }
00614 // Szurgot: The following code is unncessary on Win32.
00615 //              (void) fputcSocket(dout, '\r');
00616 //              bytes++;
00617             }
00618 
00619             if (ipos >= bufsize) {
00620                 fputSocket(dout,buf,ipos);
00621                 if(!hash) (void) putchar('.');
00622                 ipos=0;
00623             }
00624             buf[ipos]=c; ++ipos;
00625             bytes++;
00626         }
00627         if (ipos) {
00628             fputSocket(dout,buf,ipos);
00629             ipos=0;
00630         }
00631         if (hash) {
00632             if (bytes < hashbytes)
00633             (void) putchar('#');
00634             (void) putchar('\n');
00635             (void) fflush(stdout);
00636         }
00637         else {
00638             (void) putchar('.');
00639             (void) putchar('\n');
00640             (void) fflush(stdout);
00641         }
00642         if (ferror(fin))
00643             perror(local);
00644 //      if (ferror(dout)) {
00645 //          if (errno != EPIPE)
00646 //              perror("netout");
00647 //          bytes = -1;
00648 //      }
00649         break;
00650         }
00651     }
00652     (void) gettimeofday(&stop, (struct timezone *)0);
00653     if (closefunc != NULL)
00654         (*closefunc)(fin);
00655     if(closesocket(dout)) {
00656         int iret=WSAGetLastError ();
00657         fprintf(stdout,"Error closing socket(%d)\n",iret);
00658         (void) fflush(stdout);
00659     }
00660     (void) getreply(0);
00661 null();//   (void) signal(SIGINT, oldintr);
00662     if (oldintp)
00663 null();//       (void) signal(SIGPIPE, oldintp);
00664     if (bytes > 0)
00665         ptransfer("sent", bytes, &start, &stop);
00666     return;
00667 abort:
00668     (void) gettimeofday(&stop, (struct timezone *)0);
00669 null();//   (void) signal(SIGINT, oldintr);
00670     if (oldintp)
00671 null();//       (void) signal(SIGPIPE, oldintp);
00672     if (!cpend) {
00673         code = -1;
00674         return;
00675     }
00676     if (data >= 0) {
00677         (void) close(data);
00678         data = -1;
00679     }
00680     if (dout)
00681         if(closesocket(dout)) {
00682             int iret=WSAGetLastError ();
00683             fprintf(stdout,"Error closing socket(%d)\n",iret);
00684             (void) fflush(stdout);
00685         }
00686 
00687     (void) getreply(0);
00688     code = -1;
00689     if (closefunc != NULL && fin != NULL)
00690         (*closefunc)(fin);
00691     if (bytes > 0)
00692         ptransfer("sent", bytes, &start, &stop);
00693 }
00694 
00695 jmp_buf recvabort;
00696 
00697 #if 0
00698 void abortrecv()
00699 {
00700 
00701     mflag = 0;
00702     abrtflag = 0;
00703     printf("\n");
00704     (void) fflush(stdout);
00705     longjmp(recvabort, 1);
00706 }
00707 #endif
00708 
00709 void recvrequest(const char *cmd, const char *local, const char *remote, const char *mode,
00710                 int printnames)
00711 {
00712     FILE *fout = stdout;
00713     int din = 0;
00714     int (*closefunc)();
00715     void (*oldintr)(int), (*oldintp)(int);
00716     int oldverbose = 0, oldtype = 0, is_retr, tcrflag, nfnd, bare_lfs = 0;
00717     char msg;
00718 //  static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
00719     char buf[1024];
00720     static int bufsize = 1024;
00721     long bytes = 0, hashbytes = HASHBYTES;
00722 //  struct
00723         fd_set mask;
00724     register int c;
00725     struct timeval start, stop;
00726 //  struct stat st;
00727 
00728     is_retr = strcmp(cmd, "RETR") == 0;
00729     if (is_retr && verbose && printnames) {
00730         if (local && *local != '-')
00731             printf("local: %s ", local);
00732         if (remote)
00733             printf("remote: %s\n", remote);
00734         (void) fflush(stdout);
00735     }
00736     if (proxy && is_retr) {
00737         proxtrans(cmd, local, remote);
00738         return;
00739     }
00740     closefunc = NULL;
00741     oldintr = NULL;
00742     oldintp = NULL;
00743     tcrflag = !crflag && is_retr;
00744     if (setjmp(recvabort)) {
00745         while (cpend) {
00746             (void) getreply(0);
00747         }
00748         if (data >= 0) {
00749             (void) close(data);
00750             data = -1;
00751         }
00752         if (oldintr)
00753 null();//           (void) signal(SIGINT, oldintr);
00754         code = -1;
00755         return;
00756     }
00757 null();//   oldintr = signal(SIGINT, abortrecv);
00758     if (strcmp(local, "-") && *local != '|') {
00759 #ifndef _WIN32
00760     register int d;
00761 // This whole thing is a problem... access Won't work on non-existent files
00762         if (access(local, 2) < 0) {
00763             char *dir = rindex(local, '/');
00764 
00765             if (errno != ENOENT && errno != EACCES) {
00766                 perror(local);
00767                 (void) signal(SIGINT, oldintr);
00768                 code = -1;
00769                 return;
00770             }
00771             if (dir != NULL)
00772                 *dir = 0;
00773             d = access(dir ? local : ".", 2);
00774             if (dir != NULL)
00775                 *dir = '/';
00776             if (d < 0) {
00777                 perror(local);
00778                 (void) signal(SIGINT, oldintr);
00779                 code = -1;
00780                 return;
00781             }
00782             if (!runique && errno == EACCES &&
00783                 chmod(local, 0600) < 0) {
00784                 perror(local);
00785                 (void) signal(SIGINT, oldintr);
00786                 code = -1;
00787                 return;
00788             }
00789             if (runique && errno == EACCES &&
00790                 (local = gunique(local)) == NULL) {
00791                 (void) signal(SIGINT, oldintr);
00792                 code = -1;
00793                 return;
00794             }
00795         }
00796         else if (runique && (local = gunique(local)) == NULL) {
00797             (void) signal(SIGINT, oldintr);
00798             code = -1;
00799             return;
00800         }
00801 #endif
00802     }
00803     if (initconn()) {
00804 null();//       (void) signal(SIGINT, oldintr);
00805         code = -1;
00806         return;
00807     }
00808     if (setjmp(recvabort))
00809         goto abort;
00810     if (!is_retr) {
00811         if (type != TYPE_A && (allbinary == 0 || type != TYPE_I)) {
00812             oldtype = type;
00813             oldverbose = verbose;
00814             if (!debug)
00815                 verbose = 0;
00816             setascii(0, NULL);
00817             verbose = oldverbose;
00818         }
00819     } else if (restart_point) {
00820         if (command("REST %ld", (long) restart_point) != CONTINUE)
00821             return;
00822     }
00823     if (remote) {
00824         if (command("%s %s", cmd, remote) != PRELIM) {
00825 null();//           (void) signal(SIGINT, oldintr);
00826             if (oldtype) {
00827                 if (!debug)
00828                     verbose = 0;
00829                 switch (oldtype) {
00830                     case TYPE_I:
00831                         setbinary(0, NULL);
00832                         break;
00833                     case TYPE_E:
00834                         setebcdic();
00835                         break;
00836                     case TYPE_L:
00837                         settenex(0, NULL);
00838                         break;
00839                 }
00840                 verbose = oldverbose;
00841             }
00842             return;
00843         }
00844     } else {
00845         if (command("%s", cmd) != PRELIM) {
00846 null();//           (void) signal(SIGINT, oldintr);
00847             if (oldtype) {
00848                 if (!debug)
00849                     verbose = 0;
00850                 switch (oldtype) {
00851                     case TYPE_I:
00852                         setbinary(0, NULL);
00853                         break;
00854                     case TYPE_E:
00855                         setebcdic();
00856                         break;
00857                     case TYPE_L:
00858                         settenex(0, NULL);
00859                         break;
00860                 }
00861                 verbose = oldverbose;
00862             }
00863             return;
00864         }
00865     }
00866     din = dataconn("r");
00867     if (!din)
00868         goto abort;
00869     if (strcmp(local, "-") == 0)
00870         fout = stdout;
00871     else if (*local == '|') {
00872 null();//       oldintp = signal(SIGPIPE, SIG_IGN);
00873         fout = _popen(local + 1, "w");
00874         if (fout == NULL) {
00875             perror(local+1);
00876             goto abort;
00877         }
00878         closefunc = _pclose;
00879     } else {
00880         fout = fopen(local, mode);
00881         if (fout == NULL) {
00882             perror(local);
00883             goto abort;
00884         }
00885         closefunc = fclose;
00886     }
00887     (void) gettimeofday(&start, (struct timezone *)0);
00888     switch (type) {
00889 
00890     case TYPE_I:
00891     case TYPE_L:
00892         if (restart_point &&
00893             lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
00894             perror(local);
00895             if (closefunc != NULL)
00896                 (*closefunc)(fout);
00897             return;
00898         }
00899         errno = 0;
00900 //      while ((c = recv(din, buf, bufsize, 1)) > 0) {
00901 //          if ((d = write(fileno(fout), buf, c)) != c)
00902 //          if ((d = write(fileno(fout), buf, c)) != c)
00903 //              break;
00904         while ((c = recv(din, buf, bufsize, 0)) > 0) {
00905             write(fileno(fout), buf, c);
00906             bytes += c;
00907             if (hash) {
00908                 while (bytes >= hashbytes) {
00909                     (void) putchar('#');
00910                     hashbytes += HASHBYTES;
00911                 }
00912                 (void) fflush(stdout);
00913             }
00914         }
00915         if (hash && bytes > 0) {
00916             if (bytes < HASHBYTES)
00917                 (void) putchar('#');
00918             (void) putchar('\n');
00919             (void) fflush(stdout);
00920         }
00921 //      if (c < 0) {
00922 //          if (errno != EPIPE)
00923 //              perror("netin");
00924 //          bytes = -1;
00925 //      }
00926 //      if (d < c) {
00927 //          if (d < 0)
00928 //              perror(local);
00929 //          else
00930 //              fprintf(stderr, "%s: short write\n", local);
00931 //      }
00932         break;
00933 
00934     case TYPE_A:
00935         if (restart_point) {
00936             register int i, n, c;
00937 
00938             if (fseek(fout, 0L, L_SET) < 0)
00939                 goto done;
00940             n = restart_point;
00941             i = 0;
00942             while (i++ < n) {
00943                 if ((c=getc(fout)) == EOF)
00944                     goto done;
00945                 if (c == '\n')
00946                     i++;
00947             }
00948             if (fseek(fout, 0L, L_INCR) < 0) {
00949 done:
00950                 perror(local);
00951                 if (closefunc != NULL)
00952                     (*closefunc)(fout);
00953                 return;
00954             }
00955         }
00956         while ((c = fgetcSocket(din)) != EOF) {
00957             if (c == '\n')
00958                 bare_lfs++;
00959             while (c == '\r') {
00960                 while (hash && (bytes >= hashbytes)) {
00961                     (void) putchar('#');
00962                     (void) fflush(stdout);
00963                     hashbytes += HASHBYTES;
00964                 }
00965                 bytes++;
00966                 if ((c = fgetcSocket(din)) != '\n' || tcrflag) {
00967                     if (ferror(fout))
00968                     goto break2;
00969                     (void) putc('\r', fout);
00970                     if (c == '\0') {
00971                         bytes++;
00972                         goto contin2;
00973                     }
00974                     if (c == EOF)
00975                         goto contin2;
00976                 }
00977             }
00978             (void) putc(c, fout);
00979             bytes++;
00980     contin2:    ;
00981         }
00982 break2:
00983         if (bare_lfs) {
00984             printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
00985             printf("File may not have transferred correctly.\n");
00986             (void) fflush(stdout);
00987         }
00988         if (hash) {
00989             if (bytes < hashbytes)
00990                 (void) putchar('#');
00991             (void) putchar('\n');
00992             (void) fflush(stdout);
00993         }
00994 //      if (ferror(din)) {
00995 //          if (errno != EPIPE)
00996 //              perror("netin");
00997 //          bytes = -1;
00998 //      }
00999         if (ferror(fout))
01000             perror(local);
01001         break;
01002     }
01003     if (closefunc != NULL)
01004         (*closefunc)(fout);
01005 null();//   (void) signal(SIGINT, oldintr);
01006     if (oldintp)
01007 null();//       (void) signal(SIGPIPE, oldintp);
01008     (void) gettimeofday(&stop, (struct timezone *)0);
01009     if(closesocket(din)) {
01010         int iret=WSAGetLastError ();
01011         fprintf(stdout,"Error closing socket(%d)\n",iret);
01012         (void) fflush(stdout);
01013     }
01014 
01015     (void) getreply(0);
01016     if (bytes > 0 && is_retr)
01017         ptransfer("received", bytes, &start, &stop);
01018     if (oldtype) {
01019         if (!debug)
01020             verbose = 0;
01021         switch (oldtype) {
01022             case TYPE_I:
01023                 setbinary(0, NULL);
01024                 break;
01025             case TYPE_E:
01026                 setebcdic();
01027                 break;
01028             case TYPE_L:
01029                 settenex(0, NULL);
01030                 break;
01031         }
01032         verbose = oldverbose;
01033     }
01034     return;
01035 abort:
01036 
01037 /* abort using RFC959 recommended IP,SYNC sequence  */
01038 
01039     (void) gettimeofday(&stop, (struct timezone *)0);
01040     if (oldintp)
01041 null();//       (void) signal(SIGPIPE, oldintr);
01042 null();//   (void) signal(SIGINT,SIG_IGN);
01043     if (oldtype) {
01044         if (!debug)
01045             verbose = 0;
01046         switch (oldtype) {
01047             case TYPE_I:
01048                 setbinary(0, NULL);
01049                 break;
01050             case TYPE_E:
01051                 setebcdic();
01052                 break;
01053             case TYPE_L:
01054                 settenex(0, NULL);
01055                 break;
01056         }
01057         verbose = oldverbose;
01058     }
01059     if (!cpend) {
01060         code = -1;
01061 null();//       (void) signal(SIGINT,oldintr);
01062         return;
01063     }
01064 
01065     fprintfSocket(cout,"%c%c",IAC,IP);
01066     msg = (char)IAC;
01067 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
01068 /* after urgent byte rather than before as now is protocol */
01069     if (send(cout,&msg,1,MSG_OOB) != 1) {
01070         perror("abort");
01071     }
01072     fprintfSocket(cout,"%cABOR\r\n",DM);
01073     FD_ZERO(&mask);
01074     FD_SET(cin, &mask); // Need to correct this
01075     if (din) {
01076         FD_SET(din, &mask); // Need to correct this
01077     }
01078     if ((nfnd = empty(&mask,10)) <= 0) {
01079         if (nfnd < 0) {
01080             perror("abort");
01081         }
01082         code = -1;
01083         lostpeer();
01084     }
01085     if (din && FD_ISSET(din, &mask)) {
01086         while (recv(din, buf, bufsize, 0) > 0)
01087             ;
01088     }
01089     if (getreply(0) == ERROR && code == 552) { /* needed for nic style abort */
01090         if (data >= 0) {
01091             (void) close(data);
01092             data = -1;
01093         }
01094         (void) getreply(0);
01095     }
01096     (void) getreply(0);
01097     code = -1;
01098     if (data >= 0) {
01099         (void) close(data);
01100         data = -1;
01101     }
01102     if (closefunc != NULL && fout != NULL)
01103         (*closefunc)(fout);
01104     if (din)
01105         if(closesocket(din)) {
01106             int iret=WSAGetLastError ();
01107             fprintf(stdout,"Error closing socket(%d)\n",iret);
01108             (void) fflush(stdout);
01109         }
01110 
01111     if (bytes > 0)
01112         ptransfer("received", bytes, &start, &stop);
01113 null();//   (void) signal(SIGINT,oldintr);
01114 }
01115 
01116 int
01117 initconn()
01118 {
01119     register char *p, *a;
01120     int result, len, tmpno = 0;
01121     int on = 1;
01122     int a0, a1, a2, a3, p0, p1;
01123 
01124 
01125     if (passivemode) {
01126         data = socket(AF_INET, SOCK_STREAM, 0);
01127         if (data < 0) {
01128             perror("ftp: socket");
01129             return(1);
01130         }
01131         if ((options & SO_DEBUG) &&
01132             setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
01133             sizeof (on)) < 0)
01134             perror("ftp: setsockopt (ignored)");
01135         if (command("PASV") != COMPLETE) {
01136             printf("Passive mode refused.\n");
01137             goto bad;
01138         }
01139 
01140         /*
01141          * What we've got at this point is a string of comma
01142          * separated one-byte unsigned integer values.
01143          * The first four are the an IP address. The fifth is
01144          * the MSB of the port number, the sixth is the LSB.
01145          * From that we'll prepare a sockaddr_in.
01146          */
01147 
01148         if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",
01149             &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
01150             printf("Passive mode address scan failure. Shouldn't happen!\n");
01151             goto bad;
01152         }
01153 
01154         bzero(&data_addr, sizeof(data_addr));
01155         data_addr.sin_family = AF_INET;
01156         a = (char *)&data_addr.sin_addr.s_addr;
01157         a[0] = a0 & 0xff;
01158         a[1] = a1 & 0xff;
01159         a[2] = a2 & 0xff;
01160         a[3] = a3 & 0xff;
01161         p = (char *)&data_addr.sin_port;
01162         p[0] = p0 & 0xff;
01163         p[1] = p1 & 0xff;
01164 
01165         if (connect(data, (struct sockaddr *)&data_addr,
01166             sizeof(data_addr)) < 0) {
01167             perror("ftp: connect");
01168             goto bad;
01169         }
01170         return(0);
01171     }
01172 
01173 
01174 noport:
01175     data_addr = myctladdr;
01176     if (sendport)
01177         data_addr.sin_port = 0; /* let system pick one */
01178     if (data != -1)
01179         (void) close (data);
01180     data = socket(AF_INET, SOCK_STREAM, 0);
01181     if (data < 0) {
01182         perror("ftp: socket");
01183         if (tmpno)
01184             sendport = 1;
01185         return (1);
01186     }
01187     if (!sendport)
01188         if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
01189             perror("ftp: setsockopt (reuse address)");
01190             goto bad;
01191         }
01192     if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
01193         perror("ftp: bind");
01194         goto bad;
01195     }
01196     if (options & SO_DEBUG &&
01197         setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
01198         perror("ftp: setsockopt (ignored)");
01199     len = sizeof (data_addr);
01200     if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
01201         perror("ftp: getsockname");
01202         goto bad;
01203     }
01204     if (listen(data, 1) < 0)
01205         perror("ftp: listen");
01206     if (sendport) {
01207         a = (char *)&data_addr.sin_addr;
01208         p = (char *)&data_addr.sin_port;
01209 #define UC(b)   (((int)b)&0xff)
01210         result =
01211             command("PORT %d,%d,%d,%d,%d,%d",
01212               UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
01213               UC(p[0]), UC(p[1]));
01214         if (result == ERROR && sendport == -1) {
01215             sendport = 0;
01216             tmpno = 1;
01217             goto noport;
01218         }
01219         return (result != COMPLETE);
01220     }
01221     if (tmpno)
01222         sendport = 1;
01223     return (0);
01224 bad:
01225     (void) fflush(stdout);
01226     (void) close(data), data = -1;
01227     if (tmpno)
01228         sendport = 1;
01229     return (1);
01230 }
01231 
01232 int dataconn(const char *mode)
01233 {
01234     struct sockaddr_in from;
01235     int s, fromlen = sizeof (from);
01236 
01237     if (passivemode)
01238         return (data);
01239 
01240     s = accept(data, (struct sockaddr *) &from, &fromlen);
01241     if (s < 0) {
01242         perror("ftp: accept");
01243         (void) closesocket(data), data = -1;
01244         return 0;
01245     }
01246     if(closesocket(data)) {
01247         int iret=WSAGetLastError ();
01248         fprintf(stdout,"Error closing socket(%d)\n",iret);
01249         (void) fflush(stdout);
01250     }
01251 
01252     data = s;
01253     return (data);
01254 }
01255 
01256 void ptransfer(direction, bytes, t0, t1)
01257     const char *direction;
01258     long bytes;
01259     struct timeval *t0, *t1;
01260 {
01261     struct timeval td;
01262     double s, bs;
01263 
01264     if (verbose) {
01265         tvsub(&td, t1, t0);
01266         s = td.tv_sec + (td.tv_usec / 1000000.);
01267 #define nz(x)   ((x) == 0 ? 1 : (x))
01268         bs = bytes / nz(s);
01269         printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
01270             bytes, direction, s, bs / 1024.);
01271         (void) fflush(stdout);
01272     }
01273 }
01274 
01275 /*tvadd(tsum, t0)
01276     struct timeval *tsum, *t0;
01277 {
01278 
01279     tsum->tv_sec += t0->tv_sec;
01280     tsum->tv_usec += t0->tv_usec;
01281     if (tsum->tv_usec > 1000000)
01282         tsum->tv_sec++, tsum->tv_usec -= 1000000;
01283 } */
01284 
01285 void tvsub(tdiff, t1, t0)
01286     struct timeval *tdiff, *t1, *t0;
01287 {
01288 
01289     tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
01290     tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
01291     if (tdiff->tv_usec < 0)
01292         tdiff->tv_sec--, tdiff->tv_usec += 1000000;
01293 }
01294 
01295 void psabort(int flag)
01296 {
01297     extern int abrtflag;
01298 
01299     abrtflag++;
01300 }
01301 
01302 void pswitch(int flag)
01303 {
01304     extern int proxy, abrtflag;
01305     Sig_t oldintr;
01306     static struct comvars {
01307         int connect;
01308         char name[MAXHOSTNAMELEN];
01309         struct sockaddr_in mctl;
01310         struct sockaddr_in hctl;
01311         SOCKET in;
01312         SOCKET out;
01313         int tpe;
01314         int cpnd;
01315         int sunqe;
01316         int runqe;
01317         int mcse;
01318         int ntflg;
01319         char nti[17];
01320         char nto[17];
01321         int mapflg;
01322         char mi[MAXPATHLEN];
01323         char mo[MAXPATHLEN];
01324         } proxstruct, tmpstruct;
01325     struct comvars *ip, *op;
01326 
01327     abrtflag = 0;
01328     oldintr = signal(SIGINT, psabort);
01329     if (flag) {
01330         if (proxy)
01331             return;
01332         ip = &tmpstruct;
01333         op = &proxstruct;
01334         proxy++;
01335     }
01336     else {
01337         if (!proxy)
01338             return;
01339         ip = &proxstruct;
01340         op = &tmpstruct;
01341         proxy = 0;
01342     }
01343     ip->connect = connected;
01344     connected = op->connect;
01345     if (hostname) {
01346         (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
01347         ip->name[strlen(ip->name)] = '\0';
01348     } else
01349         ip->name[0] = 0;
01350     hostname = op->name;
01351     ip->hctl = hisctladdr;
01352     hisctladdr = op->hctl;
01353     ip->mctl = myctladdr;
01354     myctladdr = op->mctl;
01355     ip->in = cin;
01356     cin = op->in;
01357     ip->out = cout;
01358     cout = op->out;
01359     ip->tpe = type;
01360     type = op->tpe;
01361     if (!type)
01362         type = 1;
01363     ip->cpnd = cpend;
01364     cpend = op->cpnd;
01365     ip->sunqe = sunique;
01366     sunique = op->sunqe;
01367     ip->runqe = runique;
01368     runique = op->runqe;
01369     ip->mcse = mcase;
01370     mcase = op->mcse;
01371     ip->ntflg = ntflag;
01372     ntflag = op->ntflg;
01373     (void) strncpy(ip->nti, ntin, 16);
01374     (ip->nti)[strlen(ip->nti)] = '\0';
01375     (void) strcpy(ntin, op->nti);
01376     (void) strncpy(ip->nto, ntout, 16);
01377     (ip->nto)[strlen(ip->nto)] = '\0';
01378     (void) strcpy(ntout, op->nto);
01379     ip->mapflg = mapflag;
01380     mapflag = op->mapflg;
01381     (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
01382     (ip->mi)[strlen(ip->mi)] = '\0';
01383     (void) strcpy(mapin, op->mi);
01384     (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
01385     (ip->mo)[strlen(ip->mo)] = '\0';
01386     (void) strcpy(mapout, op->mo);
01387 //  (void) signal(SIGINT, oldintr);
01388     if (abrtflag) {
01389         abrtflag = 0;
01390         (*oldintr)(1);
01391     }
01392 }
01393 
01394 jmp_buf ptabort;
01395 int ptabflg;
01396 
01397 #if 0
01398 void
01399 abortpt()
01400 {
01401     printf("\n");
01402     (void) fflush(stdout);
01403     ptabflg++;
01404     mflag = 0;
01405     abrtflag = 0;
01406     longjmp(ptabort, 1);
01407 }
01408 #endif
01409 
01410 void proxtrans(cmd, local, remote)
01411     const char *cmd, *local, *remote;
01412 {
01413 //  void (*oldintr)(int);
01414     int tmptype, oldtype = 0, secndflag = 0, nfnd;
01415     extern jmp_buf ptabort;
01416     const char *cmd2;
01417 //  struct
01418     fd_set mask;
01419 
01420     if (strcmp(cmd, "RETR"))
01421         cmd2 = "RETR";
01422     else
01423         cmd2 = runique ? "STOU" : "STOR";
01424     if (command("PASV") != COMPLETE) {
01425         printf("proxy server does not support third part transfers.\n");
01426         (void) fflush(stdout);
01427         return;
01428     }
01429     tmptype = type;
01430     pswitch(0);
01431     if (!connected) {
01432         printf("No primary connection\n");
01433         (void) fflush(stdout);
01434         pswitch(1);
01435         code = -1;
01436         return;
01437     }
01438     if (type != tmptype) {
01439         oldtype = type;
01440         switch (tmptype) {
01441             case TYPE_A:
01442                 setascii(0, NULL);
01443                 break;
01444             case TYPE_I:
01445                 setbinary(0, NULL);
01446                 break;
01447             case TYPE_E:
01448                 setebcdic();
01449                 break;
01450             case TYPE_L:
01451                 settenex(0, NULL);
01452                 break;
01453         }
01454     }
01455     if (command("PORT %s", pasv) != COMPLETE) {
01456         switch (oldtype) {
01457             case 0:
01458                 break;
01459             case TYPE_A:
01460                 setascii(0, NULL);
01461                 break;
01462             case TYPE_I:
01463                 setbinary(0, NULL);
01464                 break;
01465             case TYPE_E:
01466                 setebcdic();
01467                 break;
01468             case TYPE_L:
01469                 settenex(0, NULL);
01470                 break;
01471         }
01472         pswitch(1);
01473         return;
01474     }
01475     if (setjmp(ptabort))
01476         goto abort;
01477 null();//   oldintr = signal(SIGINT, abortpt);
01478     if (command("%s %s", cmd, remote) != PRELIM) {
01479 null();//       (void) signal(SIGINT, oldintr);
01480         switch (oldtype) {
01481             case 0:
01482                 break;
01483             case TYPE_A:
01484                 setascii(0, NULL);
01485                 break;
01486             case TYPE_I:
01487                 setbinary(0, NULL);
01488                 break;
01489             case TYPE_E:
01490                 setebcdic();
01491                 break;
01492             case TYPE_L:
01493                 settenex(0, NULL);
01494                 break;
01495         }
01496         pswitch(1);
01497         return;
01498     }
01499     sleep(2);
01500     pswitch(1);
01501     secndflag++;
01502     if (command("%s %s", cmd2, local) != PRELIM)
01503         goto abort;
01504     ptflag++;
01505     (void) getreply(0);
01506     pswitch(0);
01507     (void) getreply(0);
01508 null();//   (void) signal(SIGINT, oldintr);
01509     switch (oldtype) {
01510         case 0:
01511             break;
01512         case TYPE_A:
01513             setascii(0, NULL);
01514             break;
01515         case TYPE_I:
01516             setbinary(0, NULL);
01517             break;
01518         case TYPE_E:
01519             setebcdic();
01520             break;
01521         case TYPE_L:
01522             settenex(0, NULL);
01523             break;
01524     }
01525     pswitch(1);
01526     ptflag = 0;
01527     printf("local: %s remote: %s\n", local, remote);
01528     (void) fflush(stdout);
01529     return;
01530 abort:
01531 null();//   (void) signal(SIGINT, SIG_IGN);
01532     ptflag = 0;
01533     if (strcmp(cmd, "RETR") && !proxy)
01534         pswitch(1);
01535     else if (!strcmp(cmd, "RETR") && proxy)
01536         pswitch(0);
01537     if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
01538         if (command("%s %s", cmd2, local) != PRELIM) {
01539             pswitch(0);
01540             switch (oldtype) {
01541                 case 0:
01542                     break;
01543                 case TYPE_A:
01544                     setascii(0, NULL);
01545                     break;
01546                 case TYPE_I:
01547                     setbinary(0, NULL);
01548                     break;
01549                 case TYPE_E:
01550                     setebcdic();
01551                     break;
01552                 case TYPE_L:
01553                     settenex(0, NULL);
01554                     break;
01555             }
01556             if (cpend) {
01557                 char msg[2];
01558 
01559                 fprintfSocket(cout,"%c%c",IAC,IP);
01560                 *msg = (char) IAC;
01561                 *(msg+1) = (char) DM;
01562                 if (send(cout,msg,2,MSG_OOB) != 2)
01563                     perror("abort");
01564                 fprintfSocket(cout,"ABOR\r\n");
01565                 FD_ZERO(&mask);
01566 //              FD_SET(fileno(cin), &mask); // Chris: Need to correct this
01567                 if ((nfnd = empty(&mask,10)) <= 0) {
01568                     if (nfnd < 0) {
01569                         perror("abort");
01570                     }
01571                     if (ptabflg)
01572                         code = -1;
01573                     lostpeer();
01574                 }
01575                 (void) getreply(0);
01576                 (void) getreply(0);
01577             }
01578         }
01579         pswitch(1);
01580         if (ptabflg)
01581             code = -1;
01582 null();//       (void) signal(SIGINT, oldintr);
01583         return;
01584     }
01585     if (cpend) {
01586         char msg[2];
01587 
01588         fprintfSocket(cout,"%c%c",IAC,IP);
01589         *msg = (char)IAC;
01590         *(msg+1) = (char)DM;
01591         if (send(cout,msg,2,MSG_OOB) != 2)
01592             perror("abort");
01593         fprintfSocket(cout,"ABOR\r\n");
01594         FD_ZERO(&mask);
01595 //      FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
01596         if ((nfnd = empty(&mask,10)) <= 0) {
01597             if (nfnd < 0) {
01598                 perror("abort");
01599             }
01600             if (ptabflg)
01601                 code = -1;
01602             lostpeer();
01603         }
01604         (void) getreply(0);
01605         (void) getreply(0);
01606     }
01607     pswitch(!proxy);
01608     if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
01609         if (command("%s %s", cmd2, local) != PRELIM) {
01610             pswitch(0);
01611             switch (oldtype) {
01612                 case 0:
01613                     break;
01614                 case TYPE_A:
01615                     setascii(0, NULL);
01616                     break;
01617                 case TYPE_I:
01618                     setbinary(0, NULL);
01619                     break;
01620                 case TYPE_E:
01621                     setebcdic();
01622                     break;
01623                 case TYPE_L:
01624                     settenex(0, NULL);
01625                     break;
01626             }
01627             if (cpend) {
01628                 char msg[2];
01629 
01630                 fprintfSocket(cout,"%c%c",IAC,IP);
01631                 *msg = (char)IAC;
01632                 *(msg+1) = (char)DM;
01633                 if (send(cout,msg,2,MSG_OOB) != 2)
01634                     perror("abort");
01635                 fprintfSocket(cout,"ABOR\r\n");
01636                 FD_ZERO(&mask);
01637 //              FD_SET(fileno(cin), &mask); // Chris:
01638                 if ((nfnd = empty(&mask,10)) <= 0) {
01639                     if (nfnd < 0) {
01640                         perror("abort");
01641                     }
01642                     if (ptabflg)
01643                         code = -1;
01644                     lostpeer();
01645                 }
01646                 (void) getreply(0);
01647                 (void) getreply(0);
01648             }
01649             pswitch(1);
01650             if (ptabflg)
01651                 code = -1;
01652 null();//           (void) signal(SIGINT, oldintr);
01653             return;
01654         }
01655     }
01656     if (cpend) {
01657         char msg[2];
01658 
01659         fprintfSocket(cout,"%c%c",IAC,IP);
01660         *msg = (char)IAC;
01661         *(msg+1) = (char)DM;
01662         if (send(cout,msg,2,MSG_OOB) != 2)
01663             perror("abort");
01664         fprintfSocket(cout,"ABOR\r\n");
01665         FD_ZERO(&mask);
01666 //      FD_SET(fileno(cin), &mask); // Chris:
01667         if ((nfnd = empty(&mask,10)) <= 0) {
01668             if (nfnd < 0) {
01669                 perror("abort");
01670             }
01671             if (ptabflg)
01672                 code = -1;
01673             lostpeer();
01674         }
01675         (void) getreply(0);
01676         (void) getreply(0);
01677     }
01678     pswitch(!proxy);
01679     if (cpend) {
01680         FD_ZERO(&mask);
01681 //      FD_SET(fileno(cin), &mask); // Chris:
01682         if ((nfnd = empty(&mask,10)) <= 0) {
01683             if (nfnd < 0) {
01684                 perror("abort");
01685             }
01686             if (ptabflg)
01687                 code = -1;
01688             lostpeer();
01689         }
01690         (void) getreply(0);
01691         (void) getreply(0);
01692     }
01693     if (proxy)
01694         pswitch(0);
01695     switch (oldtype) {
01696         case 0:
01697             break;
01698         case TYPE_A:
01699             setascii(0, NULL);
01700             break;
01701         case TYPE_I:
01702             setbinary(0, NULL);
01703             break;
01704         case TYPE_E:
01705             setebcdic();
01706             break;
01707         case TYPE_L:
01708             settenex(0, NULL);
01709             break;
01710     }
01711     pswitch(1);
01712     if (ptabflg)
01713         code = -1;
01714 null();//   (void) signal(SIGINT, oldintr);
01715 }
01716 
01717 void reset(int argc, const char *argv[])
01718 {
01719 //  struct
01720     fd_set mask;
01721     int nfnd = 1;
01722 
01723     FD_ZERO(&mask);
01724     while (nfnd > 0) {
01725 //      FD_SET(fileno(cin), &mask); // Chris
01726         if ((nfnd = empty(&mask,0)) < 0) {
01727             perror("reset");
01728             code = -1;
01729             lostpeer();
01730         }
01731         else if (nfnd) {
01732             (void) getreply(0);
01733         }
01734     }
01735 }
01736 
01737 #if 0
01738 char *
01739 gunique(local)
01740     char *local;
01741 {
01742     static char new[MAXPATHLEN];
01743     char *cp = rindex(local, '/');
01744     int d, count=0;
01745     char ext = '1';
01746 
01747     if (cp)
01748         *cp = '\0';
01749     d = access(cp ? local : ".", 2);
01750     if (cp)
01751         *cp = '/';
01752     if (d < 0) {
01753         perror(local);
01754         return((char *) 0);
01755     }
01756     (void) strcpy(new, local);
01757     cp = new + strlen(new);
01758     *cp++ = '.';
01759     while (!d) {
01760         if (++count == 100) {
01761             printf("runique: can't find unique file name.\n");
01762             (void) fflush(stdout);
01763             return((char *) 0);
01764         }
01765         *cp++ = ext;
01766         *cp = '\0';
01767         if (ext == '9')
01768             ext = '0';
01769         else
01770             ext++;
01771         if ((d = access(new, 0)) < 0)
01772             break;
01773         if (ext != '0')
01774             cp--;
01775         else if (*(cp - 2) == '.')
01776             *(cp - 1) = '1';
01777         else {
01778             *(cp - 2) = *(cp - 2) + 1;
01779             cp--;
01780         }
01781     }
01782     return(new);
01783 }
01784 #endif
01785 
01786 int null(void)
01787 {
01788     return 0;
01789 }

Generated on Fri May 25 2012 04:15:30 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.