date and time created 88/12/14 15:30:10 by sklower
[unix-history] / usr / src / usr.bin / ftp / ftp.c
index 8e8d6eb..d50edea 100644 (file)
@@ -1,12 +1,23 @@
 /*
  * Copyright (c) 1985 Regents of the University of California.
 /*
  * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)ftp.c      5.6 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)ftp.c      5.22 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "ftp_var.h"
 
 
 #include "ftp_var.h"
 
@@ -14,6 +25,7 @@ static char sccsid[] = "@(#)ftp.c     5.6 (Berkeley) %G%";
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/time.h>
+#include <sys/param.h>
 
 #include <netinet/in.h>
 #include <arpa/ftp.h>
 
 #include <netinet/in.h>
 #include <arpa/ftp.h>
@@ -29,11 +41,11 @@ static char sccsid[] = "@(#)ftp.c   5.6 (Berkeley) %G%";
 struct sockaddr_in hisctladdr;
 struct sockaddr_in data_addr;
 int    data = -1;
 struct sockaddr_in hisctladdr;
 struct sockaddr_in data_addr;
 int    data = -1;
-int     telflag = 0;
 int    abrtflag = 0;
 int    ptflag = 0;
 int    connected;
 struct sockaddr_in myctladdr;
 int    abrtflag = 0;
 int    ptflag = 0;
 int    connected;
 struct sockaddr_in myctladdr;
+uid_t  getuid();
 
 FILE   *cin, *cout;
 FILE   *dataconn();
 
 FILE   *cin, *cout;
 FILE   *dataconn();
@@ -44,9 +56,8 @@ hookup(host, port)
        int port;
 {
        register struct hostent *hp = 0;
        int port;
 {
        register struct hostent *hp = 0;
-       int s,len,oldverbose;
+       int s,len;
        static char hostnamebuf[80];
        static char hostnamebuf[80];
-       char msg[2];
 
        bzero((char *)&hisctladdr, sizeof (hisctladdr));
        hisctladdr.sin_addr.s_addr = inet_addr(host);
 
        bzero((char *)&hisctladdr, sizeof (hisctladdr));
        hisctladdr.sin_addr.s_addr = inet_addr(host);
@@ -57,7 +68,8 @@ hookup(host, port)
        else {
                hp = gethostbyname(host);
                if (hp == NULL) {
        else {
                hp = gethostbyname(host);
                if (hp == NULL) {
-                       printf("%s: unknown host\n", host);
+                       fprintf(stderr, "ftp: %s: ", host);
+                       herror((char *)NULL);
                        code = -1;
                        return((char *) 0);
                }
                        code = -1;
                        return((char *) 0);
                }
@@ -74,19 +86,26 @@ hookup(host, port)
                return (0);
        }
        hisctladdr.sin_port = port;
                return (0);
        }
        hisctladdr.sin_port = port;
-       while (connect(s, (caddr_t)&hisctladdr, sizeof (hisctladdr)) < 0) {
+       while (connect(s, &hisctladdr, sizeof (hisctladdr)) < 0) {
                if (hp && hp->h_addr_list[1]) {
                        int oerrno = errno;
 
                        fprintf(stderr, "ftp: connect to address %s: ",
                                inet_ntoa(hisctladdr.sin_addr));
                        errno = oerrno;
                if (hp && hp->h_addr_list[1]) {
                        int oerrno = errno;
 
                        fprintf(stderr, "ftp: connect to address %s: ",
                                inet_ntoa(hisctladdr.sin_addr));
                        errno = oerrno;
-                       perror(0);
+                       perror((char *) 0);
                        hp->h_addr_list++;
                        bcopy(hp->h_addr_list[0],
                             (caddr_t)&hisctladdr.sin_addr, hp->h_length);
                        hp->h_addr_list++;
                        bcopy(hp->h_addr_list[0],
                             (caddr_t)&hisctladdr.sin_addr, hp->h_length);
-                       fprintf(stderr, "Trying %s...\n",
+                       fprintf(stdout, "Trying %s...\n",
                                inet_ntoa(hisctladdr.sin_addr));
                                inet_ntoa(hisctladdr.sin_addr));
+                       (void) close(s);
+                       s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
+                       if (s < 0) {
+                               perror("ftp: socket");
+                               code = -1;
+                               return (0);
+                       }
                        continue;
                }
                perror("ftp: connect");
                        continue;
                }
                perror("ftp: connect");
@@ -104,46 +123,36 @@ hookup(host, port)
        if (cin == NULL || cout == NULL) {
                fprintf(stderr, "ftp: fdopen failed.\n");
                if (cin)
        if (cin == NULL || cout == NULL) {
                fprintf(stderr, "ftp: fdopen failed.\n");
                if (cin)
-                       fclose(cin);
+                       (void) fclose(cin);
                if (cout)
                if (cout)
-                       fclose(cout);
+                       (void) fclose(cout);
                code = -1;
                goto bad;
        }
        if (verbose)
                code = -1;
                goto bad;
        }
        if (verbose)
-               printf("Connected to %s.\n", hp->h_name);
-       if (getreply(0) != 2) {         /* read startup message from server */
+               printf("Connected to %s.\n", hostname);
+       if (getreply(0) > 2) {  /* read startup message from server */
                if (cin)
                if (cin)
-                       fclose(cin);
+                       (void) fclose(cin);
                if (cout)
                if (cout)
-                       fclose(cout);
+                       (void) fclose(cout);
                code = -1;
                goto bad;
        }
                code = -1;
                goto bad;
        }
+#ifdef SO_OOBINLINE
+       {
+       int on = 1;
 
 
-/* test to see if server command parser understands TELNET SYNC command */
-
-       fprintf(cout,"%c%c",IAC,NOP);
-       (void) fflush(cout);
-       *msg = IAC;
-       *(msg+1) = DM;
-       if (send(s,msg,2,MSG_OOB) != 2) {
-               perror("sync");
-       }
-       oldverbose = verbose;
-       if (!debug) {
-               verbose = -1;
-       }
-       if (command("NOOP") == COMPLETE) {
-               telflag = 1;
-       }
-       else {
-               telflag = 0;
+       if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on))
+               < 0 && debug) {
+                       perror("ftp: setsockopt");
+               }
        }
        }
-       verbose = oldverbose;
+#endif SO_OOBINLINE
+
        return (hostname);
 bad:
        return (hostname);
 bad:
-       close(s);
+       (void) close(s);
        return ((char *)0);
 }
 
        return ((char *)0);
 }
 
@@ -166,25 +175,21 @@ login(host)
                if (myname == NULL) {
                        struct passwd *pp = getpwuid(getuid());
 
                if (myname == NULL) {
                        struct passwd *pp = getpwuid(getuid());
 
-                       if (pp != NULL) {
+                       if (pp != NULL)
                                myname = pp->pw_name;
                                myname = pp->pw_name;
-                       }
                }
                printf("Name (%s:%s): ", host, myname);
                (void) fgets(tmp, sizeof(tmp) - 1, stdin);
                tmp[strlen(tmp) - 1] = '\0';
                }
                printf("Name (%s:%s): ", host, myname);
                (void) fgets(tmp, sizeof(tmp) - 1, stdin);
                tmp[strlen(tmp) - 1] = '\0';
-               if (*tmp == '\0') {
+               if (*tmp == '\0')
                        user = myname;
                        user = myname;
-               }
-               else {
+               else
                        user = tmp;
                        user = tmp;
-               }
        }
        n = command("USER %s", user);
        if (n == CONTINUE) {
        }
        n = command("USER %s", user);
        if (n == CONTINUE) {
-               if (pass == NULL) {
+               if (pass == NULL)
                        pass = getpass("Password:");
                        pass = getpass("Password:");
-               }
                n = command("PASS %s", pass);
        }
        if (n == CONTINUE) {
                n = command("PASS %s", pass);
        }
        if (n == CONTINUE) {
@@ -196,15 +201,13 @@ login(host)
                fprintf(stderr, "Login failed.\n");
                return (0);
        }
                fprintf(stderr, "Login failed.\n");
                return (0);
        }
-       if (!aflag && acct != NULL) {
+       if (!aflag && acct != NULL)
                (void) command("ACCT %s", acct);
                (void) command("ACCT %s", acct);
-       }
-       if (proxy) {
+       if (proxy)
                return(1);
                return(1);
-       }
        for (n = 0; n < macnum; ++n) {
                if (!strcmp("init", macros[n].mac_name)) {
        for (n = 0; n < macnum; ++n) {
                if (!strcmp("init", macros[n].mac_name)) {
-                       strcpy(line, "$init");
+                       (void) strcpy(line, "$init");
                        makeargv();
                        domacro(margc, margv);
                        break;
                        makeargv();
                        domacro(margc, margv);
                        break;
@@ -220,12 +223,11 @@ cmdabort()
        printf("\n");
        (void) fflush(stdout);
        abrtflag++;
        printf("\n");
        (void) fflush(stdout);
        abrtflag++;
-       if (ptflag) {
+       if (ptflag)
                longjmp(ptabort,1);
                longjmp(ptabort,1);
-       }
 }
 
 }
 
-/*VARARGS 1*/
+/*VARARGS1*/
 command(fmt, args)
        char *fmt;
 {
 command(fmt, args)
        char *fmt;
 {
@@ -249,9 +251,8 @@ command(fmt, args)
        (void) fflush(cout);
        cpend = 1;
        r = getreply(!strcmp(fmt, "QUIT"));
        (void) fflush(cout);
        cpend = 1;
        r = getreply(!strcmp(fmt, "QUIT"));
-       if (abrtflag && oldintr != SIG_IGN) {
+       if (abrtflag && oldintr != SIG_IGN)
                (*oldintr)();
                (*oldintr)();
-       }
        (void) signal(SIGINT, oldintr);
        return(r);
 }
        (void) signal(SIGINT, oldintr);
        return(r);
 }
@@ -271,6 +272,25 @@ getreply(expecteof)
        for (;;) {
                dig = n = code = 0;
                while ((c = getc(cin)) != '\n') {
        for (;;) {
                dig = n = code = 0;
                while ((c = getc(cin)) != '\n') {
+                       if (c == IAC) {     /* handle telnet commands */
+                               switch (c = getc(cin)) {
+                               case WILL:
+                               case WONT:
+                                       c = getc(cin);
+                                       fprintf(cout, "%c%c%c",IAC,WONT,c);
+                                       (void) fflush(cout);
+                                       break;
+                               case DO:
+                               case DONT:
+                                       c = getc(cin);
+                                       fprintf(cout, "%c%c%c",IAC,DONT,c);
+                                       (void) fflush(cout);
+                                       break;
+                               default:
+                                       break;
+                               }
+                               continue;
+                       }
                        dig++;
                        if (c == EOF) {
                                if (expecteof) {
                        dig++;
                        if (c == EOF) {
                                if (expecteof) {
@@ -282,46 +302,41 @@ getreply(expecteof)
                                if (verbose) {
                                        printf("421 Service not available, remote server has closed connection\n");
                                        (void) fflush(stdout);
                                if (verbose) {
                                        printf("421 Service not available, remote server has closed connection\n");
                                        (void) fflush(stdout);
-                                       code = 421;
-                                       return(4);
                                }
                                }
+                               code = 421;
+                               return(4);
                        }
                        if (c != '\r' && (verbose > 0 ||
                            (verbose > -1 && n == '5' && dig > 4))) {
                        }
                        if (c != '\r' && (verbose > 0 ||
                            (verbose > -1 && n == '5' && dig > 4))) {
-                               if ( proxflag && 
-                                  (dig == 1 || dig == 5 && verbose == 0)) {
+                               if (proxflag &&
+                                  (dig == 1 || dig == 5 && verbose == 0))
                                        printf("%s:",hostname);
                                        printf("%s:",hostname);
-                               }
-                               putchar(c);
+                               (void) putchar(c);
                        }
                        if (dig < 4 && isdigit(c))
                                code = code * 10 + (c - '0');
                        }
                        if (dig < 4 && isdigit(c))
                                code = code * 10 + (c - '0');
-                       if (!pflag && code == 227) {
+                       if (!pflag && code == 227)
                                pflag = 1;
                                pflag = 1;
-                       }
-                       if (dig > 4 && pflag == 1 && isdigit(c)) {
+                       if (dig > 4 && pflag == 1 && isdigit(c))
                                pflag = 2;
                                pflag = 2;
-                       }
                        if (pflag == 2) {
                        if (pflag == 2) {
-                               if (c != '\r' && c != ')') {
+                               if (c != '\r' && c != ')')
                                        *pt++ = c;
                                        *pt++ = c;
-                               }
                                else {
                                        *pt = '\0';
                                        pflag = 3;
                                }
                        }
                        if (dig == 4 && c == '-') {
                                else {
                                        *pt = '\0';
                                        pflag = 3;
                                }
                        }
                        if (dig == 4 && c == '-') {
-                               if (continuation) {
+                               if (continuation)
                                        code = 0;
                                        code = 0;
-                               }
                                continuation++;
                        }
                        if (n == 0)
                                n = c;
                }
                if (verbose > 0 || verbose > -1 && n == '5') {
                                continuation++;
                        }
                        if (n == 0)
                                n = c;
                }
                if (verbose > 0 || verbose > -1 && n == '5') {
-                       putchar(c);
+                       (void) putchar(c);
                        (void) fflush (stdout);
                }
                if (continuation && code != originalcode) {
                        (void) fflush (stdout);
                }
                if (continuation && code != originalcode) {
@@ -329,32 +344,26 @@ getreply(expecteof)
                                originalcode = code;
                        continue;
                }
                                originalcode = code;
                        continue;
                }
-               if (n != '1') {
+               if (n != '1')
                        cpend = 0;
                        cpend = 0;
-               }
                (void) signal(SIGINT,oldintr);
                (void) signal(SIGINT,oldintr);
-               if (code == 421 || originalcode == 421) {
+               if (code == 421 || originalcode == 421)
                        lostpeer();
                        lostpeer();
-               }
-               if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN) {
+               if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
                        (*oldintr)();
                        (*oldintr)();
-               }
                return (n - '0');
        }
 }
 
 empty(mask, sec)
                return (n - '0');
        }
 }
 
 empty(mask, sec)
-       long mask;
+       struct fd_set *mask;
        int sec;
 {
        struct timeval t;
 
        t.tv_sec = (long) sec;
        t.tv_usec = 0;
        int sec;
 {
        struct timeval t;
 
        t.tv_sec = (long) sec;
        t.tv_usec = 0;
-       if (select(20, &mask, 0, 0, &t) < 0) {
-               return(-1);
-       }
-       return (mask);
+       return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
 }
 
 jmp_buf        sendabort;
 }
 
 jmp_buf        sendabort;
@@ -396,12 +405,10 @@ sendrequest(cmd, local, remote)
                        (void) close(data);
                        data = -1;
                }
                        (void) close(data);
                        data = -1;
                }
-               if (oldintr) {
+               if (oldintr)
                        (void) signal(SIGINT,oldintr);
                        (void) signal(SIGINT,oldintr);
-               }
-               if (oldintp) {
+               if (oldintp)
                        (void) signal(SIGPIPE,oldintp);
                        (void) signal(SIGPIPE,oldintp);
-               }
                code = -1;
                return;
        }
                code = -1;
                return;
        }
@@ -430,7 +437,7 @@ sendrequest(cmd, local, remote)
                closefunc = fclose;
                if (fstat(fileno(fin), &st) < 0 ||
                    (st.st_mode&S_IFMT) != S_IFREG) {
                closefunc = fclose;
                if (fstat(fileno(fin), &st) < 0 ||
                    (st.st_mode&S_IFMT) != S_IFREG) {
-                       fprintf(stderr, "%s: not a plain file.\n", local);
+                       fprintf(stdout, "%s: not a plain file.\n", local);
                        (void) signal(SIGINT, oldintr);
                        code = -1;
                        return;
                        (void) signal(SIGINT, oldintr);
                        code = -1;
                        return;
@@ -438,36 +445,31 @@ sendrequest(cmd, local, remote)
        }
        if (initconn()) {
                (void) signal(SIGINT, oldintr);
        }
        if (initconn()) {
                (void) signal(SIGINT, oldintr);
-               if (oldintp) {
+               if (oldintp)
                        (void) signal(SIGPIPE, oldintp);
                        (void) signal(SIGPIPE, oldintp);
-               }
                code = -1;
                return;
        }
                code = -1;
                return;
        }
-       if (setjmp(sendabort)) {
+       if (setjmp(sendabort))
                goto abort;
                goto abort;
-       }
        if (remote) {
                if (command("%s %s", cmd, remote) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
        if (remote) {
                if (command("%s %s", cmd, remote) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
-                       if (oldintp) {
+                       if (oldintp)
                                (void) signal(SIGPIPE, oldintp);
                                (void) signal(SIGPIPE, oldintp);
-                       }
                        return;
                }
        } else
                if (command("%s", cmd) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
                        return;
                }
        } else
                if (command("%s", cmd) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
-                       if (oldintp) {
+                       if (oldintp)
                                (void) signal(SIGPIPE, oldintp);
                                (void) signal(SIGPIPE, oldintp);
-                       }
                        return;
                }
        dout = dataconn("w");
                        return;
                }
        dout = dataconn("w");
-       if (dout == NULL) {
+       if (dout == NULL)
                goto abort;
                goto abort;
-       }
-       gettimeofday(&start, (struct timezone *)0);
+       (void) gettimeofday(&start, (struct timezone *)0);
        switch (type) {
 
        case TYPE_I:
        switch (type) {
 
        case TYPE_I:
@@ -478,13 +480,13 @@ sendrequest(cmd, local, remote)
                                break;
                        bytes += c;
                        if (hash) {
                                break;
                        bytes += c;
                        if (hash) {
-                               putchar('#');
-                               fflush(stdout);
+                               (void) putchar('#');
+                               (void) fflush(stdout);
                        }
                }
                if (hash && bytes > 0) {
                        }
                }
                if (hash && bytes > 0) {
-                       putchar('\n');
-                       fflush(stdout);
+                       (void) putchar('\n');
+                       (void) fflush(stdout);
                }
                if (c < 0)
                        perror(local);
                }
                if (c < 0)
                        perror(local);
@@ -496,27 +498,27 @@ sendrequest(cmd, local, remote)
                while ((c = getc(fin)) != EOF) {
                        if (c == '\n') {
                                while (hash && (bytes >= hashbytes)) {
                while ((c = getc(fin)) != EOF) {
                        if (c == '\n') {
                                while (hash && (bytes >= hashbytes)) {
-                                       putchar('#');
-                                       fflush(stdout);
+                                       (void) putchar('#');
+                                       (void) fflush(stdout);
                                        hashbytes += sizeof (buf);
                                }
                                if (ferror(dout))
                                        break;
                                        hashbytes += sizeof (buf);
                                }
                                if (ferror(dout))
                                        break;
-                               putc('\r', dout);
+                               (void) putc('\r', dout);
                                bytes++;
                        }
                                bytes++;
                        }
-                       putc(c, dout);
+                       (void) putc(c, dout);
                        bytes++;
        /*              if (c == '\r') {                                */
                        bytes++;
        /*              if (c == '\r') {                                */
-       /*                      putc('\0', dout);  /* this violates rfc */
+       /*              (void)  putc('\0', dout);  /* this violates rfc */
        /*                      bytes++;                                */
        /*              }                                               */      
                }
                if (hash) {
                        if (bytes < hashbytes)
        /*                      bytes++;                                */
        /*              }                                               */      
                }
                if (hash) {
                        if (bytes < hashbytes)
-                               putchar('#');
-                       putchar('\n');
-                       fflush(stdout);
+                               (void) putchar('#');
+                       (void) putchar('\n');
+                       (void) fflush(stdout);
                }
                if (ferror(fin))
                        perror(local);
                }
                if (ferror(fin))
                        perror(local);
@@ -524,21 +526,20 @@ sendrequest(cmd, local, remote)
                        perror("netout");
                break;
        }
                        perror("netout");
                break;
        }
-       gettimeofday(&stop, (struct timezone *)0);
+       (void) gettimeofday(&stop, (struct timezone *)0);
        if (closefunc != NULL)
                (*closefunc)(fin);
        (void) fclose(dout);
        (void) getreply(0);
        (void) signal(SIGINT, oldintr);
        if (closefunc != NULL)
                (*closefunc)(fin);
        (void) fclose(dout);
        (void) getreply(0);
        (void) signal(SIGINT, oldintr);
-       if (bytes > 0 && verbose)
+       if (bytes > 0)
                ptransfer("sent", bytes, &start, &stop, local, remote);
        return;
 abort:
                ptransfer("sent", bytes, &start, &stop, local, remote);
        return;
 abort:
-       gettimeofday(&stop, (struct timezone *)0);
+       (void) gettimeofday(&stop, (struct timezone *)0);
        (void) signal(SIGINT, oldintr);
        (void) signal(SIGINT, oldintr);
-       if (oldintp) {
+       if (oldintp)
                (void) signal(SIGPIPE, oldintp);
                (void) signal(SIGPIPE, oldintp);
-       }
        if (!cpend) {
                code = -1;
                return;
        if (!cpend) {
                code = -1;
                return;
@@ -547,14 +548,13 @@ abort:
                (void) close(data);
                data = -1;
        }
                (void) close(data);
                data = -1;
        }
-       if (dout) {
+       if (dout)
                (void) fclose(dout);
                (void) fclose(dout);
-       }
        (void) getreply(0);
        code = -1;
        if (closefunc != NULL && fin != NULL)
                (*closefunc)(fin);
        (void) getreply(0);
        code = -1;
        if (closefunc != NULL && fin != NULL)
                (*closefunc)(fin);
-       if (bytes > 0 && verbose)
+       if (bytes > 0)
                ptransfer("sent", bytes, &start, &stop, local, remote);
 }
 
                ptransfer("sent", bytes, &start, &stop, local, remote);
 }
 
@@ -575,9 +575,10 @@ recvrequest(cmd, local, remote, mode)
 {
        FILE *fout, *din = 0, *popen();
        int (*closefunc)(), pclose(), fclose(), (*oldintr)(), (*oldintp)(); 
 {
        FILE *fout, *din = 0, *popen();
        int (*closefunc)(), pclose(), fclose(), (*oldintr)(), (*oldintp)(); 
-       int abortrecv(), oldverbose, oldtype = 0, tcrflag;
-       char buf[BUFSIZ], *gunique();
-       long bytes = 0, hashbytes = sizeof (buf), mask;
+       int abortrecv(), oldverbose, oldtype = 0, tcrflag, nfnd;
+       char buf[BUFSIZ], *gunique(), msg;
+       long bytes = 0, hashbytes = sizeof (buf);
+       struct fd_set mask;
        register int c, d;
        struct timeval start, stop;
 
        register int c, d;
        struct timeval start, stop;
 
@@ -597,9 +598,8 @@ recvrequest(cmd, local, remote, mode)
                        (void) close(data);
                        data = -1;
                }
                        (void) close(data);
                        data = -1;
                }
-               if (oldintr) {
+               if (oldintr)
                        (void) signal(SIGINT, oldintr);
                        (void) signal(SIGINT, oldintr);
-               }
                code = -1;
                return;
        }
                code = -1;
                return;
        }
@@ -650,15 +650,13 @@ recvrequest(cmd, local, remote, mode)
                code = -1;
                return;
        }
                code = -1;
                return;
        }
-       if (setjmp(recvabort)) {
+       if (setjmp(recvabort))
                goto abort;
                goto abort;
-       }
        if (strcmp(cmd, "RETR") && type != TYPE_A) {
                oldtype = type;
                oldverbose = verbose;
        if (strcmp(cmd, "RETR") && type != TYPE_A) {
                oldtype = type;
                oldverbose = verbose;
-               if (!debug) {
+               if (!debug)
                        verbose = 0;
                        verbose = 0;
-               }
                setascii();
                verbose = oldverbose;
        }
                setascii();
                verbose = oldverbose;
        }
@@ -666,9 +664,8 @@ recvrequest(cmd, local, remote, mode)
                if (command("%s %s", cmd, remote) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
                        if (oldtype) {
                if (command("%s %s", cmd, remote) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
                        if (oldtype) {
-                               if (!debug) {
+                               if (!debug)
                                        verbose = 0;
                                        verbose = 0;
-                               }
                                switch (oldtype) {
                                        case TYPE_I:
                                                setbinary();
                                switch (oldtype) {
                                        case TYPE_I:
                                                setbinary();
@@ -688,9 +685,8 @@ recvrequest(cmd, local, remote, mode)
                if (command("%s", cmd) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
                        if (oldtype) {
                if (command("%s", cmd) != PRELIM) {
                        (void) signal(SIGINT, oldintr);
                        if (oldtype) {
-                               if (!debug) {
+                               if (!debug)
                                        verbose = 0;
                                        verbose = 0;
-                               }
                                switch (oldtype) {
                                        case TYPE_I:
                                                setbinary();
                                switch (oldtype) {
                                        case TYPE_I:
                                                setbinary();
@@ -710,9 +706,8 @@ recvrequest(cmd, local, remote, mode)
        din = dataconn("r");
        if (din == NULL)
                goto abort;
        din = dataconn("r");
        if (din == NULL)
                goto abort;
-       if (strcmp(local, "-") == 0) {
+       if (strcmp(local, "-") == 0)
                fout = stdout;
                fout = stdout;
-       }
        else if (*local == '|') {
                oldintp = signal(SIGPIPE, SIG_IGN);
                fout = popen(local + 1, "w");
        else if (*local == '|') {
                oldintp = signal(SIGPIPE, SIG_IGN);
                fout = popen(local + 1, "w");
@@ -730,7 +725,7 @@ recvrequest(cmd, local, remote, mode)
                }
                closefunc = fclose;
        }
                }
                closefunc = fclose;
        }
-       gettimeofday(&start, (struct timezone *)0);
+       (void) gettimeofday(&start, (struct timezone *)0);
        switch (type) {
 
        case TYPE_I:
        switch (type) {
 
        case TYPE_I:
@@ -741,13 +736,13 @@ recvrequest(cmd, local, remote, mode)
                                break;
                        bytes += c;
                        if (hash) {
                                break;
                        bytes += c;
                        if (hash) {
-                               putchar('#');
-                               fflush(stdout);
+                               (void) putchar('#');
+                               (void) fflush(stdout);
                        }
                }
                if (hash && bytes > 0) {
                        }
                }
                if (hash && bytes > 0) {
-                       putchar('\n');
-                       fflush(stdout);
+                       (void) putchar('\n');
+                       (void) fflush(stdout);
                }
                if (c < 0)
                        perror("netin");
                }
                if (c < 0)
                        perror("netin");
@@ -757,31 +752,31 @@ recvrequest(cmd, local, remote, mode)
 
        case TYPE_A:
                while ((c = getc(din)) != EOF) {
 
        case TYPE_A:
                while ((c = getc(din)) != EOF) {
-                       if (c == '\r') {
+                       while (c == '\r') {
                                while (hash && (bytes >= hashbytes)) {
                                while (hash && (bytes >= hashbytes)) {
-                                       putchar('#');
-                                       fflush(stdout);
+                                       (void) putchar('#');
+                                       (void) fflush(stdout);
                                        hashbytes += sizeof (buf);
                                }
                                bytes++;
                                if ((c = getc(din)) != '\n' || tcrflag) {
                                        if (ferror (fout))
                                                break;
                                        hashbytes += sizeof (buf);
                                }
                                bytes++;
                                if ((c = getc(din)) != '\n' || tcrflag) {
                                        if (ferror (fout))
                                                break;
-                                       putc ('\r', fout);
+                                       (void) putc ('\r', fout);
                                }
                                /*if (c == '\0') {
                                        bytes++;
                                        continue;
                                }*/
                        }
                                }
                                /*if (c == '\0') {
                                        bytes++;
                                        continue;
                                }*/
                        }
-                       putc (c, fout);
+                       (void) putc (c, fout);
                        bytes++;
                }
                if (hash) {
                        if (bytes < hashbytes)
                        bytes++;
                }
                if (hash) {
                        if (bytes < hashbytes)
-                               putchar('#');
-                       putchar('\n');
-                       fflush(stdout);
+                               (void) putchar('#');
+                       (void) putchar('\n');
+                       (void) fflush(stdout);
                }
                if (ferror (din))
                        perror ("netin");
                }
                if (ferror (din))
                        perror ("netin");
@@ -789,22 +784,19 @@ recvrequest(cmd, local, remote, mode)
                        perror (local);
                break;
        }
                        perror (local);
                break;
        }
-       if (closefunc != NULL) {
+       if (closefunc != NULL)
                (*closefunc)(fout);
                (*closefunc)(fout);
-       }
-       signal(SIGINT, oldintr);
-       if (oldintp) {
+       (void) signal(SIGINT, oldintr);
+       if (oldintp)
                (void) signal(SIGPIPE, oldintp);
                (void) signal(SIGPIPE, oldintp);
-       }
-       gettimeofday(&stop, (struct timezone *)0);
+       (void) gettimeofday(&stop, (struct timezone *)0);
        (void) fclose(din);
        (void) getreply(0);
        (void) fclose(din);
        (void) getreply(0);
-       if (bytes > 0 && verbose)
+       if (bytes > 0)
                ptransfer("received", bytes, &start, &stop, local, remote);
        if (oldtype) {
                ptransfer("received", bytes, &start, &stop, local, remote);
        if (oldtype) {
-               if (!debug) {
+               if (!debug)
                        verbose = 0;
                        verbose = 0;
-               }
                switch (oldtype) {
                        case TYPE_I:
                                setbinary();
                switch (oldtype) {
                        case TYPE_I:
                                setbinary();
@@ -821,18 +813,15 @@ recvrequest(cmd, local, remote, mode)
        return;
 abort:
 
        return;
 abort:
 
-/* if server command parser understands TELNET commands, abort using */
-/* recommended IP,SYNC sequence                                      */
+/* abort using RFC959 recommended IP,SYNC sequence  */
 
 
-       gettimeofday(&stop, (struct timezone *)0);
-       if (oldintp) {
+       (void) gettimeofday(&stop, (struct timezone *)0);
+       if (oldintp)
                (void) signal(SIGPIPE, oldintr);
                (void) signal(SIGPIPE, oldintr);
-       }
        (void) signal(SIGINT,SIG_IGN);
        if (oldtype) {
        (void) signal(SIGINT,SIG_IGN);
        if (oldtype) {
-               if (!debug) {
+               if (!debug)
                        verbose = 0;
                        verbose = 0;
-               }
                switch (oldtype) {
                        case TYPE_I:
                                setbinary();
                switch (oldtype) {
                        case TYPE_I:
                                setbinary();
@@ -851,31 +840,36 @@ abort:
                (void) signal(SIGINT,oldintr);
                return;
        }
                (void) signal(SIGINT,oldintr);
                return;
        }
-       if (telflag) {
-               char msg[2];
 
 
-               fprintf(cout,"%c%c",IAC,IP);
-               (void) fflush(cout); 
-               *msg = IAC;
-               *(msg+1) = DM;
-               if (send(fileno(cout),msg,2,MSG_OOB) != 2) {
-                       perror("abort");
-               }
+       fprintf(cout,"%c%c",IAC,IP);
+       (void) fflush(cout); 
+       msg = IAC;
+/* send IAC in urgent mode instead of DM because UNIX places oob mark */
+/* after urgent byte rather than before as now is protocol            */
+       if (send(fileno(cout),&msg,1,MSG_OOB) != 1) {
+               perror("abort");
        }
        }
-       fprintf(cout,"ABOR\r\n");
+       fprintf(cout,"%cABOR\r\n",DM);
        (void) fflush(cout);
        (void) fflush(cout);
-       mask = (1 << fileno(cin)) | (din ? (1 << fileno(din)) : 0);
-       if ((mask = empty(mask,10)) < 0) {
-               perror("abort");
+       FD_ZERO(&mask);
+       FD_SET(fileno(cin), &mask);
+       if (din) { 
+               FD_SET(fileno(din), &mask);
+       }
+       if ((nfnd = empty(&mask,10)) <= 0) {
+               if (nfnd < 0) {
+                       perror("abort");
+               }
                code = -1;
                lostpeer();
        }
                code = -1;
                lostpeer();
        }
-       if (din && mask & (1 << fileno(din))) {
-               while ((c = read(fileno(din), buf, sizeof (buf))) > 0);
+       if (din && FD_ISSET(fileno(din), &mask)) {
+               while ((c = read(fileno(din), buf, sizeof (buf))) > 0)
+                       ;
        }
        }
-       if ((c = getreply(0)) == ERROR) { /* needed for nic style abort */
+       if ((c = getreply(0)) == ERROR && code == 552) { /* needed for nic style abort */
                if (data >= 0) {
                if (data >= 0) {
-                       close(data);
+                       (void) close(data);
                        data = -1;
                }
                (void) getreply(0);
                        data = -1;
                }
                (void) getreply(0);
@@ -886,13 +880,11 @@ abort:
                (void) close(data);
                data = -1;
        }
                (void) close(data);
                data = -1;
        }
-       if (closefunc != NULL && fout != NULL) {
+       if (closefunc != NULL && fout != NULL)
                (*closefunc)(fout);
                (*closefunc)(fout);
-       }
-       if (din) {
+       if (din)
                (void) fclose(din);
                (void) fclose(din);
-       }
-       if (bytes > 0 && verbose)
+       if (bytes > 0)
                ptransfer("received", bytes, &start, &stop, local, remote);
        (void) signal(SIGINT,oldintr);
 }
                ptransfer("received", bytes, &start, &stop, local, remote);
        (void) signal(SIGINT,oldintr);
 }
@@ -902,7 +894,7 @@ abort:
  * before we send the command, otherwise the
  * server's connect may fail.
  */
  * before we send the command, otherwise the
  * server's connect may fail.
  */
-static int sendport = -1;
+int sendport = -1;
 
 initconn()
 {
 
 initconn()
 {
@@ -919,32 +911,29 @@ noport:
        data = socket(AF_INET, SOCK_STREAM, 0);
        if (data < 0) {
                perror("ftp: socket");
        data = socket(AF_INET, SOCK_STREAM, 0);
        if (data < 0) {
                perror("ftp: socket");
-               if (tmpno) {
+               if (tmpno)
                        sendport = 1;
                        sendport = 1;
-               }
                return (1);
        }
        if (!sendport)
                return (1);
        }
        if (!sendport)
-               if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) {
-                       perror("ftp: setsockopt (resuse address)");
+               if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
+                       perror("ftp: setsockopt (reuse address)");
                        goto bad;
                }
                        goto bad;
                }
-       if (bind(data, (char *)&data_addr, sizeof (data_addr), 0) < 0) {
+       if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
                perror("ftp: bind");
                goto bad;
        }
        if (options & SO_DEBUG &&
                perror("ftp: bind");
                goto bad;
        }
        if (options & SO_DEBUG &&
-           setsockopt(data, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)) < 0)
+           setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
                perror("ftp: setsockopt (ignored)");
        len = sizeof (data_addr);
        if (getsockname(data, (char *)&data_addr, &len) < 0) {
                perror("ftp: getsockname");
                goto bad;
        }
                perror("ftp: setsockopt (ignored)");
        len = sizeof (data_addr);
        if (getsockname(data, (char *)&data_addr, &len) < 0) {
                perror("ftp: getsockname");
                goto bad;
        }
-       if (listen(data, 1) < 0) {
+       if (listen(data, 1) < 0)
                perror("ftp: listen");
                perror("ftp: listen");
-               
-       }
        if (sendport) {
                a = (char *)&data_addr.sin_addr;
                p = (char *)&data_addr.sin_port;
        if (sendport) {
                a = (char *)&data_addr.sin_addr;
                p = (char *)&data_addr.sin_port;
@@ -960,15 +949,13 @@ noport:
                }
                return (result != COMPLETE);
        }
                }
                return (result != COMPLETE);
        }
-       if (tmpno) {
+       if (tmpno)
                sendport = 1;
                sendport = 1;
-       }
        return (0);
 bad:
        (void) close(data), data = -1;
        return (0);
 bad:
        (void) close(data), data = -1;
-       if (tmpno) {
+       if (tmpno)
                sendport = 1;
                sendport = 1;
-       }
        return (1);
 }
 
        return (1);
 }
 
@@ -979,7 +966,7 @@ dataconn(mode)
        struct sockaddr_in from;
        int s, fromlen = sizeof (from);
 
        struct sockaddr_in from;
        int s, fromlen = sizeof (from);
 
-       s = accept(data, &from, &fromlen, 0);
+       s = accept(data, (struct sockaddr *) &from, &fromlen);
        if (s < 0) {
                perror("ftp: accept");
                (void) close(data), data = -1;
        if (s < 0) {
                perror("ftp: accept");
                (void) close(data), data = -1;
@@ -998,21 +985,22 @@ ptransfer(direction, bytes, t0, t1, local, remote)
        struct timeval td;
        float s, bs;
 
        struct timeval td;
        float s, bs;
 
-       tvsub(&td, t1, t0);
-       s = td.tv_sec + (td.tv_usec / 1000000.);
+       if (verbose) {
+               tvsub(&td, t1, t0);
+               s = td.tv_sec + (td.tv_usec / 1000000.);
 #define        nz(x)   ((x) == 0 ? 1 : (x))
 #define        nz(x)   ((x) == 0 ? 1 : (x))
-       bs = bytes / nz(s);
-       if (local && *local != '-') {
-               printf("local: %s ", local);
-       }
-       if (remote) {
-               printf("remote: %s\n", remote);
+               bs = bytes / nz(s);
+               printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
+                   bytes, direction, s, bs / 1024.);
+       } else {
+               if (local && *local != '-')
+                       printf("local: %s ", local);
+               if (remote)
+                       printf("remote: %s\n", remote);
        }
        }
-       printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
-               bytes, direction, s, bs / 1024.);
 }
 
 }
 
-tvadd(tsum, t0)
+/*tvadd(tsum, t0)
        struct timeval *tsum, *t0;
 {
 
        struct timeval *tsum, *t0;
 {
 
@@ -1020,7 +1008,7 @@ tvadd(tsum, t0)
        tsum->tv_usec += t0->tv_usec;
        if (tsum->tv_usec > 1000000)
                tsum->tv_sec++, tsum->tv_usec -= 1000000;
        tsum->tv_usec += t0->tv_usec;
        if (tsum->tv_usec > 1000000)
                tsum->tv_sec++, tsum->tv_usec -= 1000000;
-}
+} */
 
 tvsub(tdiff, t1, t0)
        struct timeval *tdiff, *t1, *t0;
 
 tvsub(tdiff, t1, t0)
        struct timeval *tdiff, *t1, *t0;
@@ -1046,12 +1034,11 @@ pswitch(flag)
        int (*oldintr)();
        static struct comvars {
                int connect;
        int (*oldintr)();
        static struct comvars {
                int connect;
-               char name[32];
+               char name[MAXHOSTNAMELEN];
                struct sockaddr_in mctl;
                struct sockaddr_in hctl;
                FILE *in;
                FILE *out;
                struct sockaddr_in mctl;
                struct sockaddr_in hctl;
                FILE *in;
                FILE *out;
-               int tflag;
                int tpe;
                int cpnd;
                int sunqe;
                int tpe;
                int cpnd;
                int sunqe;
@@ -1069,25 +1056,26 @@ pswitch(flag)
        abrtflag = 0;
        oldintr = signal(SIGINT, psabort);
        if (flag) {
        abrtflag = 0;
        oldintr = signal(SIGINT, psabort);
        if (flag) {
-               if (proxy) {
+               if (proxy)
                        return;
                        return;
-               }
                ip = &tmpstruct;
                op = &proxstruct;
                proxy++;
        }
        else {
                ip = &tmpstruct;
                op = &proxstruct;
                proxy++;
        }
        else {
-               if (!proxy) {
+               if (!proxy)
                        return;
                        return;
-               }
                ip = &proxstruct;
                op = &tmpstruct;
                proxy = 0;
        }
        ip->connect = connected;
        connected = op->connect;
                ip = &proxstruct;
                op = &tmpstruct;
                proxy = 0;
        }
        ip->connect = connected;
        connected = op->connect;
-       strncpy(ip->name, hostname, 31);
-       (ip->name)[strlen(ip->name)] = '\0';
+       if (hostname) {
+               (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
+               ip->name[strlen(ip->name)] = '\0';
+       } else
+               ip->name[0] = 0;
        hostname = op->name;
        ip->hctl = hisctladdr;
        hisctladdr = op->hctl;
        hostname = op->name;
        ip->hctl = hisctladdr;
        hisctladdr = op->hctl;
@@ -1097,13 +1085,10 @@ pswitch(flag)
        cin = op->in;
        ip->out = cout;
        cout = op->out;
        cin = op->in;
        ip->out = cout;
        cout = op->out;
-       ip->tflag = telflag;
-       telflag = op->tflag;
        ip->tpe = type;
        type = op->tpe;
        ip->tpe = type;
        type = op->tpe;
-       if (!type) {
+       if (!type)
                type = 1;
                type = 1;
-       }
        ip->cpnd = cpend;
        cpend = op->cpnd;
        ip->sunqe = sunique;
        ip->cpnd = cpend;
        cpend = op->cpnd;
        ip->sunqe = sunique;
@@ -1114,25 +1099,25 @@ pswitch(flag)
        mcase = op->mcse;
        ip->ntflg = ntflag;
        ntflag = op->ntflg;
        mcase = op->mcse;
        ip->ntflg = ntflag;
        ntflag = op->ntflg;
-       strncpy(ip->nti, ntin, 16);
+       (void) strncpy(ip->nti, ntin, 16);
        (ip->nti)[strlen(ip->nti)] = '\0';
        (ip->nti)[strlen(ip->nti)] = '\0';
-       strcpy(ntin, op->nti);
-       strncpy(ip->nto, ntout, 16);
+       (void) strcpy(ntin, op->nti);
+       (void) strncpy(ip->nto, ntout, 16);
        (ip->nto)[strlen(ip->nto)] = '\0';
        (ip->nto)[strlen(ip->nto)] = '\0';
-       strcpy(ntout, op->nto);
+       (void) strcpy(ntout, op->nto);
        ip->mapflg = mapflag;
        mapflag = op->mapflg;
        ip->mapflg = mapflag;
        mapflag = op->mapflg;
-       strncpy(ip->mi, mapin, MAXPATHLEN - 1);
+       (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
        (ip->mi)[strlen(ip->mi)] = '\0';
        (ip->mi)[strlen(ip->mi)] = '\0';
-       strcpy(mapin, op->mi);
-       strncpy(ip->mo, mapout, MAXPATHLEN - 1);
+       (void) strcpy(mapin, op->mi);
+       (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
        (ip->mo)[strlen(ip->mo)] = '\0';
        (ip->mo)[strlen(ip->mo)] = '\0';
-       strcpy(mapout, op->mo);
+       (void) strcpy(mapout, op->mo);
        (void) signal(SIGINT, oldintr);
        if (abrtflag) {
                abrtflag = 0;
                (*oldintr)();
        (void) signal(SIGINT, oldintr);
        if (abrtflag) {
                abrtflag = 0;
                (*oldintr)();
-               }
+       }
 }
 
 jmp_buf ptabort;
 }
 
 jmp_buf ptabort;
@@ -1141,7 +1126,7 @@ int ptabflg;
 abortpt()
 {
        printf("\n");
 abortpt()
 {
        printf("\n");
-       fflush(stdout);
+       (void) fflush(stdout);
        ptabflg++;
        mflag = 0;
        abrtflag = 0;
        ptabflg++;
        mflag = 0;
        abrtflag = 0;
@@ -1151,17 +1136,15 @@ abortpt()
 proxtrans(cmd, local, remote)
        char *cmd, *local, *remote;
 {
 proxtrans(cmd, local, remote)
        char *cmd, *local, *remote;
 {
-       int (*oldintr)(), abortpt(), tmptype, oldtype = 0, secndflag = 0;
+       int (*oldintr)(), abortpt(), tmptype, oldtype = 0, secndflag = 0, nfnd;
        extern jmp_buf ptabort;
        char *cmd2;
        extern jmp_buf ptabort;
        char *cmd2;
-       long mask;
+       struct fd_set mask;
 
 
-       if (strcmp(cmd, "RETR")) {
+       if (strcmp(cmd, "RETR"))
                cmd2 = "RETR";
                cmd2 = "RETR";
-       }
-       else {
+       else
                cmd2 = runique ? "STOU" : "STOR";
                cmd2 = runique ? "STOU" : "STOR";
-       }
        if (command("PASV") != COMPLETE) {
                printf("proxy server does not support third part transfers.\n");
                return;
        if (command("PASV") != COMPLETE) {
                printf("proxy server does not support third part transfers.\n");
                return;
@@ -1211,9 +1194,8 @@ proxtrans(cmd, local, remote)
                pswitch(1);
                return;
        }
                pswitch(1);
                return;
        }
-       if (setjmp(ptabort)) {
+       if (setjmp(ptabort))
                goto abort;
                goto abort;
-       }
        oldintr = signal(SIGINT, abortpt);
        if (command("%s %s", cmd, remote) != PRELIM) {
                (void) signal(SIGINT, oldintr);
        oldintr = signal(SIGINT, abortpt);
        if (command("%s %s", cmd, remote) != PRELIM) {
                (void) signal(SIGINT, oldintr);
@@ -1239,9 +1221,8 @@ proxtrans(cmd, local, remote)
        sleep(2);
        pswitch(1);
        secndflag++;
        sleep(2);
        pswitch(1);
        secndflag++;
-       if (command("%s %s", cmd2, local) != PRELIM) {
+       if (command("%s %s", cmd2, local) != PRELIM)
                goto abort;
                goto abort;
-       }
        ptflag++;
        (void) getreply(0);
        pswitch(0);
        ptflag++;
        (void) getreply(0);
        pswitch(0);
@@ -1270,12 +1251,10 @@ proxtrans(cmd, local, remote)
 abort:
        (void) signal(SIGINT, SIG_IGN);
        ptflag = 0;
 abort:
        (void) signal(SIGINT, SIG_IGN);
        ptflag = 0;
-       if (strcmp(cmd, "RETR") && !proxy) {
+       if (strcmp(cmd, "RETR") && !proxy)
                pswitch(1);
                pswitch(1);
-       }
-       else if (!strcmp(cmd, "RETR") && proxy) {
+       else if (!strcmp(cmd, "RETR") && proxy)
                pswitch(0);
                pswitch(0);
-       }
        if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
                if (command("%s %s", cmd2, local) != PRELIM) {
                        pswitch(0);
        if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
                if (command("%s %s", cmd2, local) != PRELIM) {
                        pswitch(0);
@@ -1295,26 +1274,25 @@ abort:
                                        settenex();
                                        break;
                        }
                                        settenex();
                                        break;
                        }
-                       if (cpend && telflag) {
+                       if (cpend) {
                                char msg[2];
 
                                fprintf(cout,"%c%c",IAC,IP);
                                (void) fflush(cout); 
                                *msg = IAC;
                                *(msg+1) = DM;
                                char msg[2];
 
                                fprintf(cout,"%c%c",IAC,IP);
                                (void) fflush(cout); 
                                *msg = IAC;
                                *(msg+1) = DM;
-                               if (send(fileno(cout),msg,2,MSG_OOB) != 2) {
+                               if (send(fileno(cout),msg,2,MSG_OOB) != 2)
                                        perror("abort");
                                        perror("abort");
-                               }
-                       }
-                       if (cpend) {
                                fprintf(cout,"ABOR\r\n");
                                (void) fflush(cout);
                                fprintf(cout,"ABOR\r\n");
                                (void) fflush(cout);
-                               mask = 1 << fileno(cin);
-                               if ((mask = empty(mask,10)) < 0) {
-                                       perror("abort");
-                                       if (ptabflg) {
-                                               code = -1;
+                               FD_ZERO(&mask);
+                               FD_SET(fileno(cin), &mask);
+                               if ((nfnd = empty(&mask,10)) <= 0) {
+                                       if (nfnd < 0) {
+                                               perror("abort");
                                        }
                                        }
+                                       if (ptabflg)
+                                               code = -1;
                                        lostpeer();
                                }
                                (void) getreply(0);
                                        lostpeer();
                                }
                                (void) getreply(0);
@@ -1322,32 +1300,30 @@ abort:
                        }
                }
                pswitch(1);
                        }
                }
                pswitch(1);
-               if (ptabflg) {
+               if (ptabflg)
                        code = -1;
                        code = -1;
-               }
                (void) signal(SIGINT, oldintr);
                return;
        }
                (void) signal(SIGINT, oldintr);
                return;
        }
-       if (cpend && telflag) {
+       if (cpend) {
                char msg[2];
 
                fprintf(cout,"%c%c",IAC,IP);
                (void) fflush(cout); 
                *msg = IAC;
                *(msg+1) = DM;
                char msg[2];
 
                fprintf(cout,"%c%c",IAC,IP);
                (void) fflush(cout); 
                *msg = IAC;
                *(msg+1) = DM;
-               if (send(fileno(cout),msg,2,MSG_OOB) != 2) {
+               if (send(fileno(cout),msg,2,MSG_OOB) != 2)
                        perror("abort");
                        perror("abort");
-               }
-       }
-       if (cpend) {
                fprintf(cout,"ABOR\r\n");
                (void) fflush(cout);
                fprintf(cout,"ABOR\r\n");
                (void) fflush(cout);
-               mask = 1 << fileno(cin);
-               if ((mask = empty(mask,10)) < 0) {
-                       perror("abort");
-                       if (ptabflg) {
-                               code = -1;
+               FD_ZERO(&mask);
+               FD_SET(fileno(cin), &mask);
+               if ((nfnd = empty(&mask,10)) <= 0) {
+                       if (nfnd < 0) {
+                               perror("abort");
                        }
                        }
+                       if (ptabflg)
+                               code = -1;
                        lostpeer();
                }
                (void) getreply(0);
                        lostpeer();
                }
                (void) getreply(0);
@@ -1373,59 +1349,56 @@ abort:
                                        settenex();
                                        break;
                        }
                                        settenex();
                                        break;
                        }
-                       if (cpend && telflag) {
+                       if (cpend) {
                                char msg[2];
 
                                fprintf(cout,"%c%c",IAC,IP);
                                (void) fflush(cout); 
                                *msg = IAC;
                                *(msg+1) = DM;
                                char msg[2];
 
                                fprintf(cout,"%c%c",IAC,IP);
                                (void) fflush(cout); 
                                *msg = IAC;
                                *(msg+1) = DM;
-                               if (send(fileno(cout),msg,2,MSG_OOB) != 2) {
+                               if (send(fileno(cout),msg,2,MSG_OOB) != 2)
                                        perror("abort");
                                        perror("abort");
-                               }
-                       }
-                       if (cpend) {
                                fprintf(cout,"ABOR\r\n");
                                (void) fflush(cout);
                                fprintf(cout,"ABOR\r\n");
                                (void) fflush(cout);
-                               mask = 1 << fileno(cin);
-                               if ((mask = empty(mask,10)) < 0) {
-                                       perror("abort");
-                                       if (ptabflg) {
-                                               code = -1;
+                               FD_ZERO(&mask);
+                               FD_SET(fileno(cin), &mask);
+                               if ((nfnd = empty(&mask,10)) <= 0) {
+                                       if (nfnd < 0) {
+                                               perror("abort");
                                        }
                                        }
+                                       if (ptabflg)
+                                               code = -1;
                                        lostpeer();
                                }
                                (void) getreply(0);
                                (void) getreply(0);
                        }
                        pswitch(1);
                                        lostpeer();
                                }
                                (void) getreply(0);
                                (void) getreply(0);
                        }
                        pswitch(1);
-                       if (ptabflg) {
+                       if (ptabflg)
                                code = -1;
                                code = -1;
-                       }
                        (void) signal(SIGINT, oldintr);
                        return;
                }
        }
                        (void) signal(SIGINT, oldintr);
                        return;
                }
        }
-       if (cpend && telflag) {
+       if (cpend) {
                char msg[2];
 
                fprintf(cout,"%c%c",IAC,IP);
                (void) fflush(cout); 
                *msg = IAC;
                *(msg+1) = DM;
                char msg[2];
 
                fprintf(cout,"%c%c",IAC,IP);
                (void) fflush(cout); 
                *msg = IAC;
                *(msg+1) = DM;
-               if (send(fileno(cout),msg,2,MSG_OOB) != 2) {
+               if (send(fileno(cout),msg,2,MSG_OOB) != 2)
                        perror("abort");
                        perror("abort");
-               }
-       }
-       if (cpend) {
                fprintf(cout,"ABOR\r\n");
                (void) fflush(cout);
                fprintf(cout,"ABOR\r\n");
                (void) fflush(cout);
-               mask = 1 << fileno(cin);
-               if ((mask = empty(mask,10)) < 0) {
-                       perror("abort");
-                       if (ptabflg) {
-                               code = -1;
+               FD_ZERO(&mask);
+               FD_SET(fileno(cin), &mask);
+               if ((nfnd = empty(&mask,10)) <= 0) {
+                       if (nfnd < 0) {
+                               perror("abort");
                        }
                        }
+                       if (ptabflg)
+                               code = -1;
                        lostpeer();
                }
                (void) getreply(0);
                        lostpeer();
                }
                (void) getreply(0);
@@ -1433,20 +1406,21 @@ abort:
        }
        pswitch(!proxy);
        if (cpend) {
        }
        pswitch(!proxy);
        if (cpend) {
-               mask = 1 << fileno(cin);
-               if ((mask = empty(mask,10)) < 0) {
-                       perror("abort");
-                       if (ptabflg) {
-                               code = -1;
+               FD_ZERO(&mask);
+               FD_SET(fileno(cin), &mask);
+               if ((nfnd = empty(&mask,10)) <= 0) {
+                       if (nfnd < 0) {
+                               perror("abort");
                        }
                        }
+                       if (ptabflg)
+                               code = -1;
                        lostpeer();
                }
                (void) getreply(0);
                (void) getreply(0);
        }
                        lostpeer();
                }
                (void) getreply(0);
                (void) getreply(0);
        }
-       if (proxy) {
+       if (proxy)
                pswitch(0);
                pswitch(0);
-       }
        switch (oldtype) {
                case 0:
                        break;
        switch (oldtype) {
                case 0:
                        break;
@@ -1464,24 +1438,25 @@ abort:
                        break;
        }
        pswitch(1);
                        break;
        }
        pswitch(1);
-       if (ptabflg) {
+       if (ptabflg)
                code = -1;
                code = -1;
-       }
        (void) signal(SIGINT, oldintr);
 }
 
 reset()
 {
        (void) signal(SIGINT, oldintr);
 }
 
 reset()
 {
-       long mask;
+       struct fd_set mask;
+       int nfnd = 1;
 
 
-       mask = 1 << fileno(cin);
-       while (mask > 0) {
-               if ((mask = empty(mask,0)) < 0) {
+       FD_ZERO(&mask);
+       while (nfnd > 0) {
+               FD_SET(fileno(cin), &mask);
+               if ((nfnd = empty(&mask,0)) < 0) {
                        perror("reset");
                        code = -1;
                        lostpeer();
                }
                        perror("reset");
                        code = -1;
                        lostpeer();
                }
-               if (mask > 0) {
+               else if (nfnd) {
                        (void) getreply(0);
                }
        }
                        (void) getreply(0);
                }
        }
@@ -1496,13 +1471,11 @@ gunique(local)
        int d, count=0;
        char ext = '1';
 
        int d, count=0;
        char ext = '1';
 
-       if (cp) {
+       if (cp)
                *cp = '\0';
                *cp = '\0';
-       }
        d = access(cp ? local : ".", 2);
        d = access(cp ? local : ".", 2);
-       if (cp) {
+       if (cp)
                *cp = '/';
                *cp = '/';
-       }
        if (d < 0) {
                perror(local);
                return((char *) 0);
        if (d < 0) {
                perror(local);
                return((char *) 0);
@@ -1517,21 +1490,16 @@ gunique(local)
                }
                *cp++ = ext;
                *cp = '\0';
                }
                *cp++ = ext;
                *cp = '\0';
-               if (ext == '9') {
+               if (ext == '9')
                        ext = '0';
                        ext = '0';
-               }
-               else {
+               else
                        ext++;
                        ext++;
-               }
-               if ((d = access(new, 0)) < 0) {
+               if ((d = access(new, 0)) < 0)
                        break;
                        break;
-               }
-               if (ext != '0') {
+               if (ext != '0')
                        cp--;
                        cp--;
-               }
-               else if (*(cp - 2) == '.') {
+               else if (*(cp - 2) == '.')
                        *(cp - 1) = '1';
                        *(cp - 1) = '1';
-               }
                else {
                        *(cp - 2) = *(cp - 2) + 1;
                        cp--;
                else {
                        *(cp - 2) = *(cp - 2) + 1;
                        cp--;