home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / ftp / cmds.c,v next >
Encoding:
Text File  |  1994-05-23  |  39.6 KB  |  2,162 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     rzsfl:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.05.23.09.03.41;    author rzsfl;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @Original
  17. @
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1985, 1989 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms, with or without
  30.  * modification, are permitted provided that the following conditions
  31.  * are met:
  32.  * 1. Redistributions of source code must retain the above copyright
  33.  *    notice, this list of conditions and the following disclaimer.
  34.  * 2. Redistributions in binary form must reproduce the above copyright
  35.  *    notice, this list of conditions and the following disclaimer in the
  36.  *    documentation and/or other materials provided with the distribution.
  37.  * 3. All advertising materials mentioning features or use of this software
  38.  *    must display the following acknowledgement:
  39.  *    This product includes software developed by the University of
  40.  *    California, Berkeley and its contributors.
  41.  * 4. Neither the name of the University nor the names of its contributors
  42.  *    may be used to endorse or promote products derived from this software
  43.  *    without specific prior written permission.
  44.  *
  45.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  46.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  49.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55.  * SUCH DAMAGE.
  56.  */
  57.  
  58. #ifndef lint
  59. /*static char sccsid[] = "from: @@(#)cmds.c    5.26 (Berkeley) 3/5/91";*/
  60. static char rcsid[] = "$Id: cmds.c,v 1.2 1993/08/01 18:15:31 mycroft Exp $";
  61. #endif /* not lint */
  62.  
  63. /*
  64.  * FTP User Program -- Command Routines.
  65.  */
  66. #include <sys/param.h>
  67. #include <sys/wait.h>
  68. #include <sys/stat.h>
  69. #include <sys/socket.h>
  70.  
  71. #include <arpa/ftp.h>
  72.  
  73. #include <signal.h>
  74. #include <stdio.h>
  75. #include <errno.h>
  76. #include <netdb.h>
  77. #include <ctype.h>
  78. #include <time.h>
  79. #include <netinet/in.h>
  80.  
  81. #include "ftp_var.h"
  82. #include "pathnames.h"
  83.  
  84. extern    char *globerr;
  85. extern    char **ftpglob();
  86. extern    char *home;
  87. extern    char *remglob();
  88. extern    char *getenv();
  89. extern    char *index();
  90. extern    char *rindex();
  91. extern    char *strerror();
  92. extern    int  errno;
  93. extern off_t restart_point;
  94. extern char reply_string[];
  95.  
  96. char *mname;
  97. jmp_buf jabort;
  98. char *dotrans(), *domap();
  99.  
  100. /*
  101.  * `Another' gets another argument, and stores the new argc and argv.
  102.  * It reverts to the top level (via main.c's intr()) on EOF/error.
  103.  *
  104.  * Returns false if no new arguments have been added.
  105.  */
  106. another(pargc, pargv, prompt)
  107.     int *pargc;
  108.     char ***pargv;
  109.     char *prompt;
  110. {
  111.     int len = strlen(line), ret;
  112.     extern sig_t intr();
  113.  
  114.     if (len >= sizeof(line) - 3) {
  115.         printf("sorry, arguments too long\n");
  116.         intr();
  117.     }
  118.     printf("(%s) ", prompt);
  119.     line[len++] = ' ';
  120.     if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
  121.         intr();
  122.     len += strlen(&line[len]);
  123.     if (len > 0 && line[len - 1] == '\n')
  124.         line[len - 1] = '\0';
  125.     makeargv();
  126.     ret = margc > *pargc;
  127.     *pargc = margc;
  128.     *pargv = margv;
  129.     return (ret);
  130. }
  131.  
  132. /*
  133.  * Connect to peer server and
  134.  * auto-login, if possible.
  135.  */
  136. setpeer(argc, argv)
  137.     int argc;
  138.     char *argv[];
  139. {
  140.     char *host, *hookup();
  141.     short port;
  142.  
  143.     if (connected) {
  144.         printf("Already connected to %s, use close first.\n",
  145.             hostname);
  146.         code = -1;
  147.         return;
  148.     }
  149.     if (argc < 2)
  150.         (void) another(&argc, &argv, "to");
  151.     if (argc < 2 || argc > 3) {
  152.         printf("usage: %s host-name [port]\n", argv[0]);
  153.         code = -1;
  154.         return;
  155.     }
  156.     port = sp->s_port;
  157.     if (argc > 2) {
  158.         port = atoi(argv[2]);
  159.         if (port <= 0) {
  160.             printf("%s: bad port number-- %s\n", argv[1], argv[2]);
  161.             printf ("usage: %s host-name [port]\n", argv[0]);
  162.             code = -1;
  163.             return;
  164.         }
  165.         port = htons(port);
  166.     }
  167.     host = hookup(argv[1], port);
  168.     if (host) {
  169.         int overbose;
  170.  
  171.         connected = 1;
  172.         /*
  173.          * Set up defaults for FTP.
  174.          */
  175.         (void) strcpy(typename, "ascii"), type = TYPE_A;
  176.         curtype = TYPE_A;
  177.         (void) strcpy(formname, "non-print"), form = FORM_N;
  178.         (void) strcpy(modename, "stream"), mode = MODE_S;
  179.         (void) strcpy(structname, "file"), stru = STRU_F;
  180.         (void) strcpy(bytename, "8"), bytesize = 8;
  181.         if (autologin)
  182.             (void) login(argv[1]);
  183.  
  184. #if defined(unix) && NBBY == 8
  185. /*
  186.  * this ifdef is to keep someone form "porting" this to an incompatible
  187.  * system and not checking this out. This way they have to think about it.
  188.  */
  189.         overbose = verbose;
  190.         if (debug == 0)
  191.             verbose = -1;
  192.         if (command("SYST") == COMPLETE && overbose) {
  193.             register char *cp, c;
  194.             cp = index(reply_string+4, ' ');
  195.             if (cp == NULL)
  196.                 cp = index(reply_string+4, '\r');
  197.             if (cp) {
  198.                 if (cp[-1] == '.')
  199.                     cp--;
  200.                 c = *cp;
  201.                 *cp = '\0';
  202.             }
  203.  
  204.             printf("Remote system type is %s.\n",
  205.                 reply_string+4);
  206.             if (cp)
  207.                 *cp = c;
  208.         }
  209.         if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
  210.             if (proxy)
  211.                 unix_proxy = 1;
  212.             else
  213.                 unix_server = 1;
  214.             /*
  215.              * Set type to 0 (not specified by user),
  216.              * meaning binary by default, but don't bother
  217.              * telling server.  We can use binary
  218.              * for text files unless changed by the user.
  219.              */
  220.             type = 0;
  221.             (void) strcpy(typename, "binary");
  222.             if (overbose)
  223.                 printf("Using %s mode to transfer files.\n",
  224.                 typename);
  225.         } else {
  226.             if (proxy)
  227.                 unix_proxy = 0;
  228.             else
  229.                 unix_server = 0;
  230.             if (overbose && 
  231.                 !strncmp(reply_string, "215 TOPS20", 10))
  232.                 printf(
  233. "Remember to set tenex mode when transfering binary files from this machine.\n");
  234.         }
  235.         verbose = overbose;
  236. #endif /* unix */
  237.     }
  238. }
  239.  
  240. struct    types {
  241.     char    *t_name;
  242.     char    *t_mode;
  243.     int    t_type;
  244.     char    *t_arg;
  245. } types[] = {
  246.     { "ascii",    "A",    TYPE_A,    0 },
  247.     { "binary",    "I",    TYPE_I,    0 },
  248.     { "image",    "I",    TYPE_I,    0 },
  249.     { "ebcdic",    "E",    TYPE_E,    0 },
  250.     { "tenex",    "L",    TYPE_L,    bytename },
  251.     0
  252. };
  253.  
  254. /*
  255.  * Set transfer type.
  256.  */
  257. settype(argc, argv)
  258.     int argc;
  259.     char *argv[];
  260. {
  261.     register struct types *p;
  262.     int comret;
  263.  
  264.     if (argc > 2) {
  265.         char *sep;
  266.  
  267.         printf("usage: %s [", argv[0]);
  268.         sep = " ";
  269.         for (p = types; p->t_name; p++) {
  270.             printf("%s%s", sep, p->t_name);
  271.             sep = " | ";
  272.         }
  273.         printf(" ]\n");
  274.         code = -1;
  275.         return;
  276.     }
  277.     if (argc < 2) {
  278.         printf("Using %s mode to transfer files.\n", typename);
  279.         code = 0;
  280.         return;
  281.     }
  282.     for (p = types; p->t_name; p++)
  283.         if (strcmp(argv[1], p->t_name) == 0)
  284.             break;
  285.     if (p->t_name == 0) {
  286.         printf("%s: unknown mode\n", argv[1]);
  287.         code = -1;
  288.         return;
  289.     }
  290.     if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
  291.         comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
  292.     else
  293.         comret = command("TYPE %s", p->t_mode);
  294.     if (comret == COMPLETE) {
  295.         (void) strcpy(typename, p->t_name);
  296.         curtype = type = p->t_type;
  297.     }
  298. }
  299.  
  300. /*
  301.  * Internal form of settype; changes current type in use with server
  302.  * without changing our notion of the type for data transfers.
  303.  * Used to change to and from ascii for listings.
  304.  */
  305. changetype(newtype, show)
  306.     int newtype, show;
  307. {
  308.     register struct types *p;
  309.     int comret, oldverbose = verbose;
  310.  
  311.     if (newtype == 0)
  312.         newtype = TYPE_I;
  313.     if (newtype == curtype)
  314.         return;
  315.     if (debug == 0 && show == 0)
  316.         verbose = 0;
  317.     for (p = types; p->t_name; p++)
  318.         if (newtype == p->t_type)
  319.             break;
  320.     if (p->t_name == 0) {
  321.         printf("ftp: internal error: unknown type %d\n", newtype);
  322.         return;
  323.     }
  324.     if (newtype == TYPE_L && bytename[0] != '\0')
  325.         comret = command("TYPE %s %s", p->t_mode, bytename);
  326.     else
  327.         comret = command("TYPE %s", p->t_mode);
  328.     if (comret == COMPLETE)
  329.         curtype = newtype;
  330.     verbose = oldverbose;
  331. }
  332.  
  333. char *stype[] = {
  334.     "type",
  335.     "",
  336.     0
  337. };
  338.  
  339. /*
  340.  * Set binary transfer type.
  341.  */
  342. /*VARARGS*/
  343. setbinary()
  344. {
  345.     stype[1] = "binary";
  346.     settype(2, stype);
  347. }
  348.  
  349. /*
  350.  * Set ascii transfer type.
  351.  */
  352. /*VARARGS*/
  353. setascii()
  354. {
  355.     stype[1] = "ascii";
  356.     settype(2, stype);
  357. }
  358.  
  359. /*
  360.  * Set tenex transfer type.
  361.  */
  362. /*VARARGS*/
  363. settenex()
  364. {
  365.     stype[1] = "tenex";
  366.     settype(2, stype);
  367. }
  368.  
  369. /*
  370.  * Set file transfer mode.
  371.  */
  372. /*ARGSUSED*/
  373. setmode(argc, argv)
  374.     int argc;
  375.     char *argv[];
  376. {
  377.  
  378.     printf("We only support %s mode, sorry.\n", modename);
  379.     code = -1;
  380. }
  381.  
  382. /*
  383.  * Set file transfer format.
  384.  */
  385. /*ARGSUSED*/
  386. setform(argc, argv)
  387.     int argc;
  388.     char *argv[];
  389. {
  390.  
  391.     printf("We only support %s format, sorry.\n", formname);
  392.     code = -1;
  393. }
  394.  
  395. /*
  396.  * Set file transfer structure.
  397.  */
  398. /*ARGSUSED*/
  399. setstruct(argc, argv)
  400.     int argc;
  401.     char *argv[];
  402. {
  403.  
  404.     printf("We only support %s structure, sorry.\n", structname);
  405.     code = -1;
  406. }
  407.  
  408. /*
  409.  * Send a single file.
  410.  */
  411. put(argc, argv)
  412.     int argc;
  413.     char *argv[];
  414. {
  415.     char *cmd;
  416.     int loc = 0;
  417.     char *oldargv1, *oldargv2;
  418.  
  419.     if (argc == 2) {
  420.         argc++;
  421.         argv[2] = argv[1];
  422.         loc++;
  423.     }
  424.     if (argc < 2 && !another(&argc, &argv, "local-file"))
  425.         goto usage;
  426.     if (argc < 3 && !another(&argc, &argv, "remote-file")) {
  427. usage:
  428.         printf("usage: %s local-file remote-file\n", argv[0]);
  429.         code = -1;
  430.         return;
  431.     }
  432.     oldargv1 = argv[1];
  433.     oldargv2 = argv[2];
  434.     if (!globulize(&argv[1])) {
  435.         code = -1;
  436.         return;
  437.     }
  438.     /*
  439.      * If "globulize" modifies argv[1], and argv[2] is a copy of
  440.      * the old argv[1], make it a copy of the new argv[1].
  441.      */
  442.     if (argv[1] != oldargv1 && argv[2] == oldargv1) {
  443.         argv[2] = argv[1];
  444.     }
  445.     cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR");
  446.     if (loc && ntflag) {
  447.         argv[2] = dotrans(argv[2]);
  448.     }
  449.     if (loc && mapflag) {
  450.         argv[2] = domap(argv[2]);
  451.     }
  452.     sendrequest(cmd, argv[1], argv[2],
  453.         argv[1] != oldargv1 || argv[2] != oldargv2);
  454. }
  455.  
  456. /*
  457.  * Send multiple files.
  458.  */
  459. mput(argc, argv)
  460.     int argc;
  461.     char **argv;
  462. {
  463.     extern jmp_buf jabort;
  464.     register int i;
  465.     sig_t oldintr;
  466.     int ointer;
  467.     char *tp;
  468.     void mabort();
  469.  
  470.     if (argc < 2 && !another(&argc, &argv, "local-files")) {
  471.         printf("usage: %s local-files\n", argv[0]);
  472.         code = -1;
  473.         return;
  474.     }
  475.     mname = argv[0];
  476.     mflag = 1;
  477.     oldintr = signal(SIGINT, mabort);
  478.     (void) setjmp(jabort);
  479.     if (proxy) {
  480.         char *cp, *tp2, tmpbuf[MAXPATHLEN];
  481.  
  482.         while ((cp = remglob(argv,0)) != NULL) {
  483.             if (*cp == 0) {
  484.                 mflag = 0;
  485.                 continue;
  486.             }
  487.             if (mflag && confirm(argv[0], cp)) {
  488.                 tp = cp;
  489.                 if (mcase) {
  490.                     while (*tp && !islower(*tp)) {
  491.                         tp++;
  492.                     }
  493.                     if (!*tp) {
  494.                         tp = cp;
  495.                         tp2 = tmpbuf;
  496.                         while ((*tp2 = *tp) != NULL) {
  497.                              if (isupper(*tp2)) {
  498.                                 *tp2 = 'a' + *tp2 - 'A';
  499.                              }
  500.                              tp++;
  501.                              tp2++;
  502.                         }
  503.                     }
  504.                     tp = tmpbuf;
  505.                 }
  506.                 if (ntflag) {
  507.                     tp = dotrans(tp);
  508.                 }
  509.                 if (mapflag) {
  510.                     tp = domap(tp);
  511.                 }
  512.                 sendrequest((sunique) ? "STOU" : "STOR",
  513.                     cp, tp, cp != tp || !interactive);
  514.                 if (!mflag && fromatty) {
  515.                     ointer = interactive;
  516.                     interactive = 1;
  517.                     if (confirm("Continue with","mput")) {
  518.                         mflag++;
  519.                     }
  520.                     interactive = ointer;
  521.                 }
  522.             }
  523.         }
  524.         (void) signal(SIGINT, oldintr);
  525.         mflag = 0;
  526.         return;
  527.     }
  528.     for (i = 1; i < argc; i++) {
  529.         register char **cpp, **gargs;
  530.  
  531.         if (!doglob) {
  532.             if (mflag && confirm(argv[0], argv[i])) {
  533.                 tp = (ntflag) ? dotrans(argv[i]) : argv[i];
  534.                 tp = (mapflag) ? domap(tp) : tp;
  535.                 sendrequest((sunique) ? "STOU" : "STOR",
  536.                     argv[i], tp, tp != argv[i] || !interactive);
  537.                 if (!mflag && fromatty) {
  538.                     ointer = interactive;
  539.                     interactive = 1;
  540.                     if (confirm("Continue with","mput")) {
  541.                         mflag++;
  542.                     }
  543.                     interactive = ointer;
  544.                 }
  545.             }
  546.             continue;
  547.         }
  548.         gargs = ftpglob(argv[i]);
  549.         if (globerr != NULL) {
  550.             printf("%s\n", globerr);
  551.             if (gargs) {
  552.                 blkfree(gargs);
  553.                 free((char *)gargs);
  554.             }
  555.             continue;
  556.         }
  557.         for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
  558.             if (mflag && confirm(argv[0], *cpp)) {
  559.                 tp = (ntflag) ? dotrans(*cpp) : *cpp;
  560.                 tp = (mapflag) ? domap(tp) : tp;
  561.                 sendrequest((sunique) ? "STOU" : "STOR",
  562.                     *cpp, tp, *cpp != tp || !interactive);
  563.                 if (!mflag && fromatty) {
  564.                     ointer = interactive;
  565.                     interactive = 1;
  566.                     if (confirm("Continue with","mput")) {
  567.                         mflag++;
  568.                     }
  569.                     interactive = ointer;
  570.                 }
  571.             }
  572.         }
  573.         if (gargs != NULL) {
  574.             blkfree(gargs);
  575.             free((char *)gargs);
  576.         }
  577.     }
  578.     (void) signal(SIGINT, oldintr);
  579.     mflag = 0;
  580. }
  581.  
  582. reget(argc, argv)
  583.     int argc;
  584.     char *argv[];
  585. {
  586.     (void) getit(argc, argv, 1, "r+w");
  587. }
  588.  
  589. get(argc, argv)
  590.     int argc;
  591.     char *argv[];
  592. {
  593.     (void) getit(argc, argv, 0, restart_point ? "r+w" : "w" );
  594. }
  595.  
  596. /*
  597.  * Receive one file.
  598.  */
  599. getit(argc, argv, restartit, mode)
  600.     int argc;
  601.     char *argv[];
  602.     char *mode;
  603. {
  604.     int loc = 0;
  605.     char *oldargv1, *oldargv2;
  606.  
  607.     if (argc == 2) {
  608.         argc++;
  609.         argv[2] = argv[1];
  610.         loc++;
  611.     }
  612.     if (argc < 2 && !another(&argc, &argv, "remote-file"))
  613.         goto usage;
  614.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  615. usage:
  616.         printf("usage: %s remote-file [ local-file ]\n", argv[0]);
  617.         code = -1;
  618.         return (0);
  619.     }
  620.     oldargv1 = argv[1];
  621.     oldargv2 = argv[2];
  622.     if (!globulize(&argv[2])) {
  623.         code = -1;
  624.         return (0);
  625.     }
  626.     if (loc && mcase) {
  627.         char *tp = argv[1], *tp2, tmpbuf[MAXPATHLEN];
  628.  
  629.         while (*tp && !islower(*tp)) {
  630.             tp++;
  631.         }
  632.         if (!*tp) {
  633.             tp = argv[2];
  634.             tp2 = tmpbuf;
  635.             while ((*tp2 = *tp) != NULL) {
  636.                 if (isupper(*tp2)) {
  637.                     *tp2 = 'a' + *tp2 - 'A';
  638.                 }
  639.                 tp++;
  640.                 tp2++;
  641.             }
  642.             argv[2] = tmpbuf;
  643.         }
  644.     }
  645.     if (loc && ntflag)
  646.         argv[2] = dotrans(argv[2]);
  647.     if (loc && mapflag)
  648.         argv[2] = domap(argv[2]);
  649.     if (restartit) {
  650.         struct stat stbuf;
  651.         int ret;
  652.  
  653.         ret = stat(argv[2], &stbuf);
  654.         if (restartit == 1) {
  655.             if (ret < 0) {
  656.                 fprintf(stderr, "local: %s: %s\n", argv[2],
  657.                     strerror(errno));
  658.                 return (0);
  659.             }
  660.             restart_point = stbuf.st_size;
  661.         } else {
  662.             if (ret == 0) {
  663.                 int overbose;
  664.  
  665.                 overbose = verbose;
  666.                 if (debug == 0)
  667.                     verbose = -1;
  668.                 if (command("MDTM %s", argv[1]) == COMPLETE) {
  669.                     int yy, mo, day, hour, min, sec;
  670.                     struct tm *tm;
  671.                     verbose = overbose;
  672.                     sscanf(reply_string,
  673.                         "%*s %04d%02d%02d%02d%02d%02d",
  674.                         &yy, &mo, &day, &hour, &min, &sec);
  675.                     tm = gmtime(&stbuf.st_mtime);
  676.                     tm->tm_mon++;
  677.                     if (tm->tm_year > yy%100)
  678.                         return (1);
  679.                     else if (tm->tm_year == yy%100) {
  680.                         if (tm->tm_mon > mo)
  681.                             return (1);
  682.                     } else if (tm->tm_mon == mo) {
  683.                         if (tm->tm_mday > day)
  684.                             return (1);
  685.                     } else if (tm->tm_mday == day) {
  686.                         if (tm->tm_hour > hour)
  687.                             return (1);
  688.                     } else if (tm->tm_hour == hour) {
  689.                         if (tm->tm_min > min)
  690.                             return (1);
  691.                     } else if (tm->tm_min == min) {
  692.                         if (tm->tm_sec > sec)
  693.                             return (1);
  694.                     }
  695.                 } else {
  696.                     printf("%s\n", reply_string);
  697.                     verbose = overbose;
  698.                     return (0);
  699.                 }
  700.             }
  701.         }
  702.     }
  703.  
  704.     recvrequest("RETR", argv[2], argv[1], mode,
  705.         argv[1] != oldargv1 || argv[2] != oldargv2);
  706.     restart_point = 0;
  707.     return (0);
  708. }
  709.  
  710. void
  711. mabort()
  712. {
  713.     int ointer;
  714.     extern jmp_buf jabort;
  715.  
  716.     printf("\n");
  717.     (void) fflush(stdout);
  718.     if (mflag && fromatty) {
  719.         ointer = interactive;
  720.         interactive = 1;
  721.         if (confirm("Continue with", mname)) {
  722.             interactive = ointer;
  723.             longjmp(jabort,0);
  724.         }
  725.         interactive = ointer;
  726.     }
  727.     mflag = 0;
  728.     longjmp(jabort,0);
  729. }
  730.  
  731. /*
  732.  * Get multiple files.
  733.  */
  734. mget(argc, argv)
  735.     int argc;
  736.     char **argv;
  737. {
  738.     extern jmp_buf jabort;
  739.     sig_t oldintr;
  740.     int ointer;
  741.     char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
  742.     void mabort();
  743.  
  744.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  745.         printf("usage: %s remote-files\n", argv[0]);
  746.         code = -1;
  747.         return;
  748.     }
  749.     mname = argv[0];
  750.     mflag = 1;
  751.     oldintr = signal(SIGINT,mabort);
  752.     (void) setjmp(jabort);
  753.     while ((cp = remglob(argv,proxy)) != NULL) {
  754.         if (*cp == '\0') {
  755.             mflag = 0;
  756.             continue;
  757.         }
  758.         if (mflag && confirm(argv[0], cp)) {
  759.             tp = cp;
  760.             if (mcase) {
  761.                 while (*tp && !islower(*tp)) {
  762.                     tp++;
  763.                 }
  764.                 if (!*tp) {
  765.                     tp = cp;
  766.                     tp2 = tmpbuf;
  767.                     while ((*tp2 = *tp) != NULL) {
  768.                         if (isupper(*tp2)) {
  769.                             *tp2 = 'a' + *tp2 - 'A';
  770.                         }
  771.                         tp++;
  772.                         tp2++;
  773.                     }
  774.                 }
  775.                 tp = tmpbuf;
  776.             }
  777.             if (ntflag) {
  778.                 tp = dotrans(tp);
  779.             }
  780.             if (mapflag) {
  781.                 tp = domap(tp);
  782.             }
  783.             recvrequest("RETR", tp, cp, "w",
  784.                 tp != cp || !interactive);
  785.             if (!mflag && fromatty) {
  786.                 ointer = interactive;
  787.                 interactive = 1;
  788.                 if (confirm("Continue with","mget")) {
  789.                     mflag++;
  790.                 }
  791.                 interactive = ointer;
  792.             }
  793.         }
  794.     }
  795.     (void) signal(SIGINT,oldintr);
  796.     mflag = 0;
  797. }
  798.  
  799. char *
  800. remglob(argv,doswitch)
  801.     char *argv[];
  802.     int doswitch;
  803. {
  804.     char temp[16];
  805.     static char buf[MAXPATHLEN];
  806.     static FILE *ftemp = NULL;
  807.     static char **args;
  808.     int oldverbose, oldhash;
  809.     char *cp, *mode;
  810.  
  811.     if (!mflag) {
  812.         if (!doglob) {
  813.             args = NULL;
  814.         }
  815.         else {
  816.             if (ftemp) {
  817.                 (void) fclose(ftemp);
  818.                 ftemp = NULL;
  819.             }
  820.         }
  821.         return(NULL);
  822.     }
  823.     if (!doglob) {
  824.         if (args == NULL)
  825.             args = argv;
  826.         if ((cp = *++args) == NULL)
  827.             args = NULL;
  828.         return (cp);
  829.     }
  830.     if (ftemp == NULL) {
  831.         (void) strcpy(temp, _PATH_TMP);
  832.         (void) mktemp(temp);
  833.         oldverbose = verbose, verbose = 0;
  834.         oldhash = hash, hash = 0;
  835.         if (doswitch) {
  836.             pswitch(!proxy);
  837.         }
  838.         for (mode = "w"; *++argv != NULL; mode = "a")
  839.             recvrequest ("NLST", temp, *argv, mode, 0);
  840.         if (doswitch) {
  841.             pswitch(!proxy);
  842.         }
  843.         verbose = oldverbose; hash = oldhash;
  844.         ftemp = fopen(temp, "r");
  845.         (void) unlink(temp);
  846.         if (ftemp == NULL) {
  847.             printf("can't find list of remote files, oops\n");
  848.             return (NULL);
  849.         }
  850.     }
  851.     if (fgets(buf, sizeof (buf), ftemp) == NULL) {
  852.         (void) fclose(ftemp), ftemp = NULL;
  853.         return (NULL);
  854.     }
  855.     if ((cp = index(buf, '\n')) != NULL)
  856.         *cp = '\0';
  857.     return (buf);
  858. }
  859.  
  860. char *
  861. onoff(bool)
  862.     int bool;
  863. {
  864.  
  865.     return (bool ? "on" : "off");
  866. }
  867.  
  868. /*
  869.  * Show status.
  870.  */
  871. /*ARGSUSED*/
  872. status(argc, argv)
  873.     int argc;
  874.     char *argv[];
  875. {
  876.     int i;
  877.  
  878.     if (connected)
  879.         printf("Connected to %s.\n", hostname);
  880.     else
  881.         printf("Not connected.\n");
  882.     if (!proxy) {
  883.         pswitch(1);
  884.         if (connected) {
  885.             printf("Connected for proxy commands to %s.\n", hostname);
  886.         }
  887.         else {
  888.             printf("No proxy connection.\n");
  889.         }
  890.         pswitch(0);
  891.     }
  892.     printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
  893.         modename, typename, formname, structname);
  894.     printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n", 
  895.         onoff(verbose), onoff(bell), onoff(interactive),
  896.         onoff(doglob));
  897.     printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),
  898.         onoff(runique));
  899.     printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));
  900.     if (ntflag) {
  901.         printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);
  902.     }
  903.     else {
  904.         printf("Ntrans: off\n");
  905.     }
  906.     if (mapflag) {
  907.         printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
  908.     }
  909.     else {
  910.         printf("Nmap: off\n");
  911.     }
  912.     printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
  913.         onoff(hash), onoff(sendport));
  914.     if (macnum > 0) {
  915.         printf("Macros:\n");
  916.         for (i=0; i<macnum; i++) {
  917.             printf("\t%s\n",macros[i].mac_name);
  918.         }
  919.     }
  920.     code = 0;
  921. }
  922.  
  923. /*
  924.  * Set beep on cmd completed mode.
  925.  */
  926. /*VARARGS*/
  927. setbell()
  928. {
  929.  
  930.     bell = !bell;
  931.     printf("Bell mode %s.\n", onoff(bell));
  932.     code = bell;
  933. }
  934.  
  935. /*
  936.  * Turn on packet tracing.
  937.  */
  938. /*VARARGS*/
  939. settrace()
  940. {
  941.  
  942.     trace = !trace;
  943.     printf("Packet tracing %s.\n", onoff(trace));
  944.     code = trace;
  945. }
  946.  
  947. /*
  948.  * Toggle hash mark printing during transfers.
  949.  */
  950. /*VARARGS*/
  951. sethash()
  952. {
  953.  
  954.     hash = !hash;
  955.     printf("Hash mark printing %s", onoff(hash));
  956.     code = hash;
  957.     if (hash)
  958.         printf(" (%d bytes/hash mark)", 1024);
  959.     printf(".\n");
  960. }
  961.  
  962. /*
  963.  * Turn on printing of server echo's.
  964.  */
  965. /*VARARGS*/
  966. setverbose()
  967. {
  968.  
  969.     verbose = !verbose;
  970.     printf("Verbose mode %s.\n", onoff(verbose));
  971.     code = verbose;
  972. }
  973.  
  974. /*
  975.  * Toggle PORT cmd use before each data connection.
  976.  */
  977. /*VARARGS*/
  978. setport()
  979. {
  980.  
  981.     sendport = !sendport;
  982.     printf("Use of PORT cmds %s.\n", onoff(sendport));
  983.     code = sendport;
  984. }
  985.  
  986. /*
  987.  * Turn on interactive prompting
  988.  * during mget, mput, and mdelete.
  989.  */
  990. /*VARARGS*/
  991. setprompt()
  992. {
  993.  
  994.     interactive = !interactive;
  995.     printf("Interactive mode %s.\n", onoff(interactive));
  996.     code = interactive;
  997. }
  998.  
  999. /*
  1000.  * Toggle metacharacter interpretation
  1001.  * on local file names.
  1002.  */
  1003. /*VARARGS*/
  1004. setglob()
  1005. {
  1006.     
  1007.     doglob = !doglob;
  1008.     printf("Globbing %s.\n", onoff(doglob));
  1009.     code = doglob;
  1010. }
  1011.  
  1012. /*
  1013.  * Set debugging mode on/off and/or
  1014.  * set level of debugging.
  1015.  */
  1016. /*VARARGS*/
  1017. setdebug(argc, argv)
  1018.     int argc;
  1019.     char *argv[];
  1020. {
  1021.     int val;
  1022.  
  1023.     if (argc > 1) {
  1024.         val = atoi(argv[1]);
  1025.         if (val < 0) {
  1026.             printf("%s: bad debugging value.\n", argv[1]);
  1027.             code = -1;
  1028.             return;
  1029.         }
  1030.     } else
  1031.         val = !debug;
  1032.     debug = val;
  1033.     if (debug)
  1034.         options |= SO_DEBUG;
  1035.     else
  1036.         options &= ~SO_DEBUG;
  1037.     printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
  1038.     code = debug > 0;
  1039. }
  1040.  
  1041. /*
  1042.  * Set current working directory
  1043.  * on remote machine.
  1044.  */
  1045. cd(argc, argv)
  1046.     int argc;
  1047.     char *argv[];
  1048. {
  1049.  
  1050.     if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
  1051.         printf("usage: %s remote-directory\n", argv[0]);
  1052.         code = -1;
  1053.         return;
  1054.     }
  1055.     if (command("CWD %s", argv[1]) == ERROR && code == 500) {
  1056.         if (verbose)
  1057.             printf("CWD command not recognized, trying XCWD\n");
  1058.         (void) command("XCWD %s", argv[1]);
  1059.     }
  1060. }
  1061.  
  1062. /*
  1063.  * Set current working directory
  1064.  * on local machine.
  1065.  */
  1066. lcd(argc, argv)
  1067.     int argc;
  1068.     char *argv[];
  1069. {
  1070.     char buf[MAXPATHLEN];
  1071.     extern char *getwd();
  1072.  
  1073.     if (argc < 2)
  1074.         argc++, argv[1] = home;
  1075.     if (argc != 2) {
  1076.         printf("usage: %s local-directory\n", argv[0]);
  1077.         code = -1;
  1078.         return;
  1079.     }
  1080.     if (!globulize(&argv[1])) {
  1081.         code = -1;
  1082.         return;
  1083.     }
  1084.     if (chdir(argv[1]) < 0) {
  1085.         fprintf(stderr, "local: %s: %s\n", argv[1], strerror(errno));
  1086.         code = -1;
  1087.         return;
  1088.     }
  1089.     printf("Local directory now %s\n", getwd(buf));
  1090.     code = 0;
  1091. }
  1092.  
  1093. /*
  1094.  * Delete a single file.
  1095.  */
  1096. delete(argc, argv)
  1097.     int argc;
  1098.     char *argv[];
  1099. {
  1100.  
  1101.     if (argc < 2 && !another(&argc, &argv, "remote-file")) {
  1102.         printf("usage: %s remote-file\n", argv[0]);
  1103.         code = -1;
  1104.         return;
  1105.     }
  1106.     (void) command("DELE %s", argv[1]);
  1107. }
  1108.  
  1109. /*
  1110.  * Delete multiple files.
  1111.  */
  1112. mdelete(argc, argv)
  1113.     int argc;
  1114.     char **argv;
  1115. {
  1116.     extern jmp_buf jabort;
  1117.     sig_t oldintr;
  1118.     int ointer;
  1119.     char *cp;
  1120.     void mabort();
  1121.  
  1122.     if (argc < 2 && !another(&argc, &argv, "remote-files")) {
  1123.         printf("usage: %s remote-files\n", argv[0]);
  1124.         code = -1;
  1125.         return;
  1126.     }
  1127.     mname = argv[0];
  1128.     mflag = 1;
  1129.     oldintr = signal(SIGINT, mabort);
  1130.     (void) setjmp(jabort);
  1131.     while ((cp = remglob(argv,0)) != NULL) {
  1132.         if (*cp == '\0') {
  1133.             mflag = 0;
  1134.             continue;
  1135.         }
  1136.         if (mflag && confirm(argv[0], cp)) {
  1137.             (void) command("DELE %s", cp);
  1138.             if (!mflag && fromatty) {
  1139.                 ointer = interactive;
  1140.                 interactive = 1;
  1141.                 if (confirm("Continue with", "mdelete")) {
  1142.                     mflag++;
  1143.                 }
  1144.                 interactive = ointer;
  1145.             }
  1146.         }
  1147.     }
  1148.     (void) signal(SIGINT, oldintr);
  1149.     mflag = 0;
  1150. }
  1151.  
  1152. /*
  1153.  * Rename a remote file.
  1154.  */
  1155. renamefile(argc, argv)
  1156.     int argc;
  1157.     char *argv[];
  1158. {
  1159.  
  1160.     if (argc < 2 && !another(&argc, &argv, "from-name"))
  1161.         goto usage;
  1162.     if (argc < 3 && !another(&argc, &argv, "to-name")) {
  1163. usage:
  1164.         printf("%s from-name to-name\n", argv[0]);
  1165.         code = -1;
  1166.         return;
  1167.     }
  1168.     if (command("RNFR %s", argv[1]) == CONTINUE)
  1169.         (void) command("RNTO %s", argv[2]);
  1170. }
  1171.  
  1172. /*
  1173.  * Get a directory listing
  1174.  * of remote files.
  1175.  */
  1176. ls(argc, argv)
  1177.     int argc;
  1178.     char *argv[];
  1179. {
  1180.     char *cmd;
  1181.  
  1182.     if (argc < 2)
  1183.         argc++, argv[1] = NULL;
  1184.     if (argc < 3)
  1185.         argc++, argv[2] = "-";
  1186.     if (argc > 3) {
  1187.         printf("usage: %s remote-directory local-file\n", argv[0]);
  1188.         code = -1;
  1189.         return;
  1190.     }
  1191.     cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
  1192.     if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
  1193.         code = -1;
  1194.         return;
  1195.     }
  1196.     if (strcmp(argv[2], "-") && *argv[2] != '|')
  1197.         if (!globulize(&argv[2]) || !confirm("output to local-file:", argv[2])) {
  1198.             code = -1;
  1199.             return;
  1200.     }
  1201.     recvrequest(cmd, argv[2], argv[1], "w", 0);
  1202. }
  1203.  
  1204. /*
  1205.  * Get a directory listing
  1206.  * of multiple remote files.
  1207.  */
  1208. mls(argc, argv)
  1209.     int argc;
  1210.     char **argv;
  1211. {
  1212.     extern jmp_buf jabort;
  1213.     sig_t oldintr;
  1214.     int ointer, i;
  1215.     char *cmd, mode[1], *dest;
  1216.     void mabort();
  1217.  
  1218.     if (argc < 2 && !another(&argc, &argv, "remote-files"))
  1219.         goto usage;
  1220.     if (argc < 3 && !another(&argc, &argv, "local-file")) {
  1221. usage:
  1222.         printf("usage: %s remote-files local-file\n", argv[0]);
  1223.         code = -1;
  1224.         return;
  1225.     }
  1226.     dest = argv[argc - 1];
  1227.     argv[argc - 1] = NULL;
  1228.     if (strcmp(dest, "-") && *dest != '|')
  1229.         if (!globulize(&dest) ||
  1230.             !confirm("output to local-file:", dest)) {
  1231.             code = -1;
  1232.             return;
  1233.     }
  1234.     cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
  1235.     mname = argv[0];
  1236.     mflag = 1;
  1237.     oldintr = signal(SIGINT, mabort);
  1238.     (void) setjmp(jabort);
  1239.     for (i = 1; mflag && i < argc-1; ++i) {
  1240.         *mode = (i == 1) ? 'w' : 'a';
  1241.         recvrequest(cmd, dest, argv[i], mode, 0);
  1242.         if (!mflag && fromatty) {
  1243.             ointer = interactive;
  1244.             interactive = 1;
  1245.             if (confirm("Continue with", argv[0])) {
  1246.                 mflag ++;
  1247.             }
  1248.             interactive = ointer;
  1249.         }
  1250.     }
  1251.     (void) signal(SIGINT, oldintr);
  1252.     mflag = 0;
  1253. }
  1254.  
  1255. /*
  1256.  * Do a shell escape
  1257.  */
  1258. /*ARGSUSED*/
  1259. shell(argc, argv)
  1260.     int argc;
  1261.     char **argv;
  1262. {
  1263.     int pid;
  1264.     sig_t old1, old2;
  1265.     char shellnam[40], *shell, *namep; 
  1266.     union wait status;
  1267.  
  1268.     old1 = signal (SIGINT, SIG_IGN);
  1269.     old2 = signal (SIGQUIT, SIG_IGN);
  1270.     if ((pid = fork()) == 0) {
  1271.         for (pid = 3; pid < 20; pid++)
  1272.             (void) close(pid);
  1273.         (void) signal(SIGINT, SIG_DFL);
  1274.         (void) signal(SIGQUIT, SIG_DFL);
  1275.         shell = getenv("SHELL");
  1276.         if (shell == NULL)
  1277.             shell = _PATH_BSHELL;
  1278.         namep = rindex(shell,'/');
  1279.         if (namep == NULL)
  1280.             namep = shell;
  1281.         (void) strcpy(shellnam,"-");
  1282.         (void) strcat(shellnam, ++namep);
  1283.         if (strcmp(namep, "sh") != 0)
  1284.             shellnam[0] = '+';
  1285.         if (debug) {
  1286.             printf ("%s\n", shell);
  1287.             (void) fflush (stdout);
  1288.         }
  1289.         if (argc > 1) {
  1290.             execl(shell,shellnam,"-c",altarg,(char *)0);
  1291.         }
  1292.         else {
  1293.             execl(shell,shellnam,(char *)0);
  1294.         }
  1295.         perror(shell);
  1296.         code = -1;
  1297.         exit(1);
  1298.         }
  1299.     if (pid > 0)
  1300.         while (wait((int *)&status) != pid)
  1301.             ;
  1302.     (void) signal(SIGINT, old1);
  1303.     (void) signal(SIGQUIT, old2);
  1304.     if (pid == -1) {
  1305.         perror("Try again later");
  1306.         code = -1;
  1307.     }
  1308.     else {
  1309.         code = 0;
  1310.     }
  1311.     return (0);
  1312. }
  1313.  
  1314. /*
  1315.  * Send new user information (re-login)
  1316.  */
  1317. user(argc, argv)
  1318.     int argc;
  1319.     char **argv;
  1320. {
  1321.     char acct[80], *getpass();
  1322.     int n, aflag = 0;
  1323.  
  1324.     if (argc < 2)
  1325.         (void) another(&argc, &argv, "username");
  1326.     if (argc < 2 || argc > 4) {
  1327.         printf("usage: %s username [password] [account]\n", argv[0]);
  1328.         code = -1;
  1329.         return (0);
  1330.     }
  1331.     n = command("USER %s", argv[1]);
  1332.     if (n == CONTINUE) {
  1333.         if (argc < 3 )
  1334.             argv[2] = getpass("Password: "), argc++;
  1335.         n = command("PASS %s", argv[2]);
  1336.     }
  1337.     if (n == CONTINUE) {
  1338.         if (argc < 4) {
  1339.             printf("Account: "); (void) fflush(stdout);
  1340.             (void) fgets(acct, sizeof(acct) - 1, stdin);
  1341.             acct[strlen(acct) - 1] = '\0';
  1342.             argv[3] = acct; argc++;
  1343.         }
  1344.         n = command("ACCT %s", argv[3]);
  1345.         aflag++;
  1346.     }
  1347.     if (n != COMPLETE) {
  1348.         fprintf(stdout, "Login failed.\n");
  1349.         return (0);
  1350.     }
  1351.     if (!aflag && argc == 4) {
  1352.         (void) command("ACCT %s", argv[3]);
  1353.     }
  1354.     return (1);
  1355. }
  1356.  
  1357. /*
  1358.  * Print working directory.
  1359.  */
  1360. /*VARARGS*/
  1361. pwd()
  1362. {
  1363.     int oldverbose = verbose;
  1364.  
  1365.     /*
  1366.      * If we aren't verbose, this doesn't do anything!
  1367.      */
  1368.     verbose = 1;
  1369.     if (command("PWD") == ERROR && code == 500) {
  1370.         printf("PWD command not recognized, trying XPWD\n");
  1371.         (void) command("XPWD");
  1372.     }
  1373.     verbose = oldverbose;
  1374. }
  1375.  
  1376. /*
  1377.  * Make a directory.
  1378.  */
  1379. makedir(argc, argv)
  1380.     int argc;
  1381.     char *argv[];
  1382. {
  1383.  
  1384.     if (argc < 2 && !another(&argc, &argv, "directory-name")) {
  1385.         printf("usage: %s directory-name\n", argv[0]);
  1386.         code = -1;
  1387.         return;
  1388.     }
  1389.     if (command("MKD %s", argv[1]) == ERROR && code == 500) {
  1390.         if (verbose)
  1391.             printf("MKD command not recognized, trying XMKD\n");
  1392.         (void) command("XMKD %s", argv[1]);
  1393.     }
  1394. }
  1395.  
  1396. /*
  1397.  * Remove a directory.
  1398.  */
  1399. removedir(argc, argv)
  1400.     int argc;
  1401.     char *argv[];
  1402. {
  1403.  
  1404.     if (argc < 2 && !another(&argc, &argv, "directory-name")) {
  1405.         printf("usage: %s directory-name\n", argv[0]);
  1406.         code = -1;
  1407.         return;
  1408.     }
  1409.     if (command("RMD %s", argv[1]) == ERROR && code == 500) {
  1410.         if (verbose)
  1411.             printf("RMD command not recognized, trying XRMD\n");
  1412.         (void) command("XRMD %s", argv[1]);
  1413.     }
  1414. }
  1415.  
  1416. /*
  1417.  * Send a line, verbatim, to the remote machine.
  1418.  */
  1419. quote(argc, argv)
  1420.     int argc;
  1421.     char *argv[];
  1422. {
  1423.  
  1424.     if (argc < 2 && !another(&argc, &argv, "command line to send")) {
  1425.         printf("usage: %s line-to-send\n", argv[0]);
  1426.         code = -1;
  1427.         return;
  1428.     }
  1429.     quote1("", argc, argv);
  1430. }
  1431.  
  1432. /*
  1433.  * Send a SITE command to the remote machine.  The line
  1434.  * is sent verbatim to the remote machine, except that the
  1435.  * word "SITE" is added at the front.
  1436.  */
  1437. site(argc, argv)
  1438.     int argc;
  1439.     char *argv[];
  1440. {
  1441.  
  1442.     if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {
  1443.         printf("usage: %s line-to-send\n", argv[0]);
  1444.         code = -1;
  1445.         return;
  1446.     }
  1447.     quote1("SITE ", argc, argv);
  1448. }
  1449.  
  1450. /*
  1451.  * Turn argv[1..argc) into a space-separated string, then prepend initial text.
  1452.  * Send the result as a one-line command and get response.
  1453.  */
  1454. quote1(initial, argc, argv)
  1455.     char *initial;
  1456.     int argc;
  1457.     char **argv;
  1458. {
  1459.     register int i, len;
  1460.     char buf[BUFSIZ];        /* must be >= sizeof(line) */
  1461.  
  1462.     (void) strcpy(buf, initial);
  1463.     if (argc > 1) {
  1464.         len = strlen(buf);
  1465.         len += strlen(strcpy(&buf[len], argv[1]));
  1466.         for (i = 2; i < argc; i++) {
  1467.             buf[len++] = ' ';
  1468.             len += strlen(strcpy(&buf[len], argv[i]));
  1469.         }
  1470.     }
  1471.     if (command(buf) == PRELIM) {
  1472.         while (getreply(0) == PRELIM);
  1473.     }
  1474. }
  1475.  
  1476. do_chmod(argc, argv)
  1477.     int argc;
  1478.     char *argv[];
  1479. {
  1480.  
  1481.     if (argc < 2 && !another(&argc, &argv, "mode"))
  1482.         goto usage;
  1483.     if (argc < 3 && !another(&argc, &argv, "file-name")) {
  1484. usage:
  1485.         printf("usage: %s mode file-name\n", argv[0]);
  1486.         code = -1;
  1487.         return;
  1488.     }
  1489.     (void) command("SITE CHMOD %s %s", argv[1], argv[2]);
  1490. }
  1491.  
  1492. do_umask(argc, argv)
  1493.     int argc;
  1494.     char *argv[];
  1495. {
  1496.     int oldverbose = verbose;
  1497.  
  1498.     verbose = 1;
  1499.     (void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
  1500.     verbose = oldverbose;
  1501. }
  1502.  
  1503. idle(argc, argv)
  1504.     int argc;
  1505.     char *argv[];
  1506. {
  1507.     int oldverbose = verbose;
  1508.  
  1509.     verbose = 1;
  1510.     (void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
  1511.     verbose = oldverbose;
  1512. }
  1513.  
  1514. /*
  1515.  * Ask the other side for help.
  1516.  */
  1517. rmthelp(argc, argv)
  1518.     int argc;
  1519.     char *argv[];
  1520. {
  1521.     int oldverbose = verbose;
  1522.  
  1523.     verbose = 1;
  1524.     (void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
  1525.     verbose = oldverbose;
  1526. }
  1527.  
  1528. /*
  1529.  * Terminate session and exit.
  1530.  */
  1531. /*VARARGS*/
  1532. quit()
  1533. {
  1534.  
  1535.     if (connected)
  1536.         disconnect();
  1537.     pswitch(1);
  1538.     if (connected) {
  1539.         disconnect();
  1540.     }
  1541.     exit(0);
  1542. }
  1543.  
  1544. /*
  1545.  * Terminate session, but don't exit.
  1546.  */
  1547. disconnect()
  1548. {
  1549.     extern FILE *cout;
  1550.     extern int data;
  1551.  
  1552.     if (!connected)
  1553.         return;
  1554.     (void) command("QUIT");
  1555.     if (cout) {
  1556.         (void) fclose(cout);
  1557.     }
  1558.     cout = NULL;
  1559.     connected = 0;
  1560.     data = -1;
  1561.     if (!proxy) {
  1562.         macnum = 0;
  1563.     }
  1564. }
  1565.  
  1566. confirm(cmd, file)
  1567.     char *cmd, *file;
  1568. {
  1569.     char line[BUFSIZ];
  1570.  
  1571.     if (!interactive)
  1572.         return (1);
  1573.     printf("%s %s? ", cmd, file);
  1574.     (void) fflush(stdout);
  1575.     if (fgets(line, sizeof line, stdin) == NULL)
  1576.         return (0);
  1577.     return (*line != 'n' && *line != 'N');
  1578. }
  1579.  
  1580. fatal(msg)
  1581.     char *msg;
  1582. {
  1583.  
  1584.     fprintf(stderr, "ftp: %s\n", msg);
  1585.     exit(1);
  1586. }
  1587.  
  1588. /*
  1589.  * Glob a local file name specification with
  1590.  * the expectation of a single return value.
  1591.  * Can't control multiple values being expanded
  1592.  * from the expression, we return only the first.
  1593.  */
  1594. globulize(cpp)
  1595.     char **cpp;
  1596. {
  1597.     char **globbed;
  1598.  
  1599.     if (!doglob)
  1600.         return (1);
  1601.     globbed = ftpglob(*cpp);
  1602.     if (globerr != NULL) {
  1603.         printf("%s: %s\n", *cpp, globerr);
  1604.         if (globbed) {
  1605.             blkfree(globbed);
  1606.             free((char *)globbed);
  1607.         }
  1608.         return (0);
  1609.     }
  1610.     if (globbed) {
  1611.         *cpp = *globbed++;
  1612.         /* don't waste too much memory */
  1613.         if (*globbed) {
  1614.             blkfree(globbed);
  1615.             free((char *)globbed);
  1616.         }
  1617.     }
  1618.     return (1);
  1619. }
  1620.  
  1621. account(argc,argv)
  1622.     int argc;
  1623.     char **argv;
  1624. {
  1625.     char acct[50], *getpass(), *ap;
  1626.  
  1627.     if (argc > 1) {
  1628.         ++argv;
  1629.         --argc;
  1630.         (void) strncpy(acct,*argv,49);
  1631.         acct[49] = '\0';
  1632.         while (argc > 1) {
  1633.             --argc;
  1634.             ++argv;
  1635.             (void) strncat(acct,*argv, 49-strlen(acct));
  1636.         }
  1637.         ap = acct;
  1638.     }
  1639.     else {
  1640.         ap = getpass("Account:");
  1641.     }
  1642.     (void) command("ACCT %s", ap);
  1643. }
  1644.  
  1645. jmp_buf abortprox;
  1646.  
  1647. void
  1648. proxabort()
  1649. {
  1650.     extern int proxy;
  1651.  
  1652.     if (!proxy) {
  1653.         pswitch(1);
  1654.     }
  1655.     if (connected) {
  1656.         proxflag = 1;
  1657.     }
  1658.     else {
  1659.         proxflag = 0;
  1660.     }
  1661.     pswitch(0);
  1662.     longjmp(abortprox,1);
  1663. }
  1664.  
  1665. doproxy(argc,argv)
  1666.     int argc;
  1667.     char *argv[];
  1668. {
  1669.     extern struct cmd cmdtab[];
  1670.     extern jmp_buf abortprox;
  1671.     register struct cmd *c;
  1672.     struct cmd *getcmd();
  1673.     sig_t oldintr;
  1674.     void proxabort();
  1675.  
  1676.     if (argc < 2 && !another(&argc, &argv, "command")) {
  1677.         printf("usage: %s command\n", argv[0]);
  1678.         code = -1;
  1679.         return;
  1680.     }
  1681.     c = getcmd(argv[1]);
  1682.     if (c == (struct cmd *) -1) {
  1683.         printf("?Ambiguous command\n");
  1684.         (void) fflush(stdout);
  1685.         code = -1;
  1686.         return;
  1687.     }
  1688.     if (c == 0) {
  1689.         printf("?Invalid command\n");
  1690.         (void) fflush(stdout);
  1691.         code = -1;
  1692.         return;
  1693.     }
  1694.     if (!c->c_proxy) {
  1695.         printf("?Invalid proxy command\n");
  1696.         (void) fflush(stdout);
  1697.         code = -1;
  1698.         return;
  1699.     }
  1700.     if (setjmp(abortprox)) {
  1701.         code = -1;
  1702.         return;
  1703.     }
  1704.     oldintr = signal(SIGINT, proxabort);
  1705.     pswitch(1);
  1706.     if (c->c_conn && !connected) {
  1707.         printf("Not connected\n");
  1708.         (void) fflush(stdout);
  1709.         pswitch(0);
  1710.         (void) signal(SIGINT, oldintr);
  1711.         code = -1;
  1712.         return;
  1713.     }
  1714.     (*c->c_handler)(argc-1, argv+1);
  1715.     if (connected) {
  1716.         proxflag = 1;
  1717.     }
  1718.     else {
  1719.         proxflag = 0;
  1720.     }
  1721.     pswitch(0);
  1722.     (void) signal(SIGINT, oldintr);
  1723. }
  1724.  
  1725. setcase()
  1726. {
  1727.     mcase = !mcase;
  1728.     printf("Case mapping %s.\n", onoff(mcase));
  1729.     code = mcase;
  1730. }
  1731.  
  1732. setcr()
  1733. {
  1734.     crflag = !crflag;
  1735.     printf("Carriage Return stripping %s.\n", onoff(crflag));
  1736.     code = crflag;
  1737. }
  1738.  
  1739. setntrans(argc,argv)
  1740.     int argc;
  1741.     char *argv[];
  1742. {
  1743.     if (argc == 1) {
  1744.         ntflag = 0;
  1745.         printf("Ntrans off.\n");
  1746.         code = ntflag;
  1747.         return;
  1748.     }
  1749.     ntflag++;
  1750.     code = ntflag;
  1751.     (void) strncpy(ntin, argv[1], 16);
  1752.     ntin[16] = '\0';
  1753.     if (argc == 2) {
  1754.         ntout[0] = '\0';
  1755.         return;
  1756.     }
  1757.     (void) strncpy(ntout, argv[2], 16);
  1758.     ntout[16] = '\0';
  1759. }
  1760.  
  1761. char *
  1762. dotrans(name)
  1763.     char *name;
  1764. {
  1765.     static char new[MAXPATHLEN];
  1766.     char *cp1, *cp2 = new;
  1767.     register int i, ostop, found;
  1768.  
  1769.     for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++);
  1770.     for (cp1 = name; *cp1; cp1++) {
  1771.         found = 0;
  1772.         for (i = 0; *(ntin + i) && i < 16; i++) {
  1773.             if (*cp1 == *(ntin + i)) {
  1774.                 found++;
  1775.                 if (i < ostop) {
  1776.                     *cp2++ = *(ntout + i);
  1777.                 }
  1778.                 break;
  1779.             }
  1780.         }
  1781.         if (!found) {
  1782.             *cp2++ = *cp1;
  1783.         }
  1784.     }
  1785.     *cp2 = '\0';
  1786.     return(new);
  1787. }
  1788.  
  1789. setnmap(argc, argv)
  1790.     int argc;
  1791.     char *argv[];
  1792. {
  1793.     char *cp;
  1794.  
  1795.     if (argc == 1) {
  1796.         mapflag = 0;
  1797.         printf("Nmap off.\n");
  1798.         code = mapflag;
  1799.         return;
  1800.     }
  1801.     if (argc < 3 && !another(&argc, &argv, "mapout")) {
  1802.         printf("Usage: %s [mapin mapout]\n",argv[0]);
  1803.         code = -1;
  1804.         return;
  1805.     }
  1806.     mapflag = 1;
  1807.     code = 1;
  1808.     cp = index(altarg, ' ');
  1809.     if (proxy) {
  1810.         while(*++cp == ' ');
  1811.         altarg = cp;
  1812.         cp = index(altarg, ' ');
  1813.     }
  1814.     *cp = '\0';
  1815.     (void) strncpy(mapin, altarg, MAXPATHLEN - 1);
  1816.     while (*++cp == ' ');
  1817.     (void) strncpy(mapout, cp, MAXPATHLEN - 1);
  1818. }
  1819.  
  1820. char *
  1821. domap(name)
  1822.     char *name;
  1823. {
  1824.     static char new[MAXPATHLEN];
  1825.     register char *cp1 = name, *cp2 = mapin;
  1826.     char *tp[9], *te[9];
  1827.     int i, toks[9], toknum = 0, match = 1;
  1828.  
  1829.     for (i=0; i < 9; ++i) {
  1830.         toks[i] = 0;
  1831.     }
  1832.     while (match && *cp1 && *cp2) {
  1833.         switch (*cp2) {
  1834.             case '\\':
  1835.                 if (*++cp2 != *cp1) {
  1836.                     match = 0;
  1837.                 }
  1838.                 break;
  1839.             case '$':
  1840.                 if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {
  1841.                     if (*cp1 != *(++cp2+1)) {
  1842.                         toks[toknum = *cp2 - '1']++;
  1843.                         tp[toknum] = cp1;
  1844.                         while (*++cp1 && *(cp2+1)
  1845.                             != *cp1);
  1846.                         te[toknum] = cp1;
  1847.                     }
  1848.                     cp2++;
  1849.                     break;
  1850.                 }
  1851.                 /* FALLTHROUGH */
  1852.             default:
  1853.                 if (*cp2 != *cp1) {
  1854.                     match = 0;
  1855.                 }
  1856.                 break;
  1857.         }
  1858.         if (match && *cp1) {
  1859.             cp1++;
  1860.         }
  1861.         if (match && *cp2) {
  1862.             cp2++;
  1863.         }
  1864.     }
  1865.     if (!match && *cp1) /* last token mismatch */
  1866.     {
  1867.         toks[toknum] = 0;
  1868.     }
  1869.     cp1 = new;
  1870.     *cp1 = '\0';
  1871.     cp2 = mapout;
  1872.     while (*cp2) {
  1873.         match = 0;
  1874.         switch (*cp2) {
  1875.             case '\\':
  1876.                 if (*(cp2 + 1)) {
  1877.                     *cp1++ = *++cp2;
  1878.                 }
  1879.                 break;
  1880.             case '[':
  1881. LOOP:
  1882.                 if (*++cp2 == '$' && isdigit(*(cp2+1))) { 
  1883.                     if (*++cp2 == '0') {
  1884.                         char *cp3 = name;
  1885.  
  1886.                         while (*cp3) {
  1887.                             *cp1++ = *cp3++;
  1888.                         }
  1889.                         match = 1;
  1890.                     }
  1891.                     else if (toks[toknum = *cp2 - '1']) {
  1892.                         char *cp3 = tp[toknum];
  1893.  
  1894.                         while (cp3 != te[toknum]) {
  1895.                             *cp1++ = *cp3++;
  1896.                         }
  1897.                         match = 1;
  1898.                     }
  1899.                 }
  1900.                 else {
  1901.                     while (*cp2 && *cp2 != ',' && 
  1902.                         *cp2 != ']') {
  1903.                         if (*cp2 == '\\') {
  1904.                             cp2++;
  1905.                         }
  1906.                         else if (*cp2 == '$' &&
  1907.                                    isdigit(*(cp2+1))) {
  1908.                             if (*++cp2 == '0') {
  1909.                                char *cp3 = name;
  1910.  
  1911.                                while (*cp3) {
  1912.                                 *cp1++ = *cp3++;
  1913.                                }
  1914.                             }
  1915.                             else if (toks[toknum =
  1916.                                 *cp2 - '1']) {
  1917.                                char *cp3=tp[toknum];
  1918.  
  1919.                                while (cp3 !=
  1920.                                   te[toknum]) {
  1921.                                 *cp1++ = *cp3++;
  1922.                                }
  1923.                             }
  1924.                         }
  1925.                         else if (*cp2) {
  1926.                             *cp1++ = *cp2++;
  1927.                         }
  1928.                     }
  1929.                     if (!*cp2) {
  1930.                         printf("nmap: unbalanced brackets\n");
  1931.                         return(name);
  1932.                     }
  1933.                     match = 1;
  1934.                     cp2--;
  1935.                 }
  1936.                 if (match) {
  1937.                     while (*++cp2 && *cp2 != ']') {
  1938.                           if (*cp2 == '\\' && *(cp2 + 1)) {
  1939.                             cp2++;
  1940.                           }
  1941.                     }
  1942.                     if (!*cp2) {
  1943.                         printf("nmap: unbalanced brackets\n");
  1944.                         return(name);
  1945.                     }
  1946.                     break;
  1947.                 }
  1948.                 switch (*++cp2) {
  1949.                     case ',':
  1950.                         goto LOOP;
  1951.                     case ']':
  1952.                         break;
  1953.                     default:
  1954.                         cp2--;
  1955.                         goto LOOP;
  1956.                 }
  1957.                 break;
  1958.             case '$':
  1959.                 if (isdigit(*(cp2 + 1))) {
  1960.                     if (*++cp2 == '0') {
  1961.                         char *cp3 = name;
  1962.  
  1963.                         while (*cp3) {
  1964.                             *cp1++ = *cp3++;
  1965.                         }
  1966.                     }
  1967.                     else if (toks[toknum = *cp2 - '1']) {
  1968.                         char *cp3 = tp[toknum];
  1969.  
  1970.                         while (cp3 != te[toknum]) {
  1971.                             *cp1++ = *cp3++;
  1972.                         }
  1973.                     }
  1974.                     break;
  1975.                 }
  1976.                 /* intentional drop through */
  1977.             default:
  1978.                 *cp1++ = *cp2;
  1979.                 break;
  1980.         }
  1981.         cp2++;
  1982.     }
  1983.     *cp1 = '\0';
  1984.     if (!*new) {
  1985.         return(name);
  1986.     }
  1987.     return(new);
  1988. }
  1989.  
  1990. setsunique()
  1991. {
  1992.     sunique = !sunique;
  1993.     printf("Store unique %s.\n", onoff(sunique));
  1994.     code = sunique;
  1995. }
  1996.  
  1997. setrunique()
  1998. {
  1999.     runique = !runique;
  2000.     printf("Receive unique %s.\n", onoff(runique));
  2001.     code = runique;
  2002. }
  2003.  
  2004. /* change directory to perent directory */
  2005. cdup()
  2006. {
  2007.     if (command("CDUP") == ERROR && code == 500) {
  2008.         if (verbose)
  2009.             printf("CDUP command not recognized, trying XCUP\n");
  2010.         (void) command("XCUP");
  2011.     }
  2012. }
  2013.  
  2014. /* restart transfer at specific point */
  2015. restart(argc, argv)
  2016.     int argc;
  2017.     char *argv[];
  2018. {
  2019.     extern long atol();
  2020.     if (argc != 2)
  2021.         printf("restart: offset not specified\n");
  2022.     else {
  2023.         restart_point = atol(argv[1]);
  2024.         printf("restarting at %ld. %s\n", restart_point,
  2025.             "execute get, put or append to initiate transfer");
  2026.     }
  2027. }
  2028.  
  2029. /* show remote system type */
  2030. syst()
  2031. {
  2032.     (void) command("SYST");
  2033. }
  2034.  
  2035. macdef(argc, argv)
  2036.     int argc;
  2037.     char *argv[];
  2038. {
  2039.     char *tmp;
  2040.     int c;
  2041.  
  2042.     if (macnum == 16) {
  2043.         printf("Limit of 16 macros have already been defined\n");
  2044.         code = -1;
  2045.         return;
  2046.     }
  2047.     if (argc < 2 && !another(&argc, &argv, "macro name")) {
  2048.         printf("Usage: %s macro_name\n",argv[0]);
  2049.         code = -1;
  2050.         return;
  2051.     }
  2052.     if (interactive) {
  2053.         printf("Enter macro line by line, terminating it with a null line\n");
  2054.     }
  2055.     (void) strncpy(macros[macnum].mac_name, argv[1], 8);
  2056.     if (macnum == 0) {
  2057.         macros[macnum].mac_start = macbuf;
  2058.     }
  2059.     else {
  2060.         macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
  2061.     }
  2062.     tmp = macros[macnum].mac_start;
  2063.     while (tmp != macbuf+4096) {
  2064.         if ((c = getchar()) == EOF) {
  2065.             printf("macdef:end of file encountered\n");
  2066.             code = -1;
  2067.             return;
  2068.         }
  2069.         if ((*tmp = c) == '\n') {
  2070.             if (tmp == macros[macnum].mac_start) {
  2071.                 macros[macnum++].mac_end = tmp;
  2072.                 code = 0;
  2073.                 return;
  2074.             }
  2075.             if (*(tmp-1) == '\0') {
  2076.                 macros[macnum++].mac_end = tmp - 1;
  2077.                 code = 0;
  2078.                 return;
  2079.             }
  2080.             *tmp = '\0';
  2081.         }
  2082.         tmp++;
  2083.     }
  2084.     while (1) {
  2085.         while ((c = getchar()) != '\n' && c != EOF)
  2086.             /* LOOP */;
  2087.         if (c == EOF || getchar() == '\n') {
  2088.             printf("Macro not defined - 4k buffer exceeded\n");
  2089.             code = -1;
  2090.             return;
  2091.         }
  2092.     }
  2093. }
  2094.  
  2095. /*
  2096.  * get size of file on remote machine
  2097.  */
  2098. sizecmd(argc, argv)
  2099.     int argc;
  2100.     char *argv[];
  2101. {
  2102.  
  2103.     if (argc < 2 && !another(&argc, &argv, "filename")) {
  2104.         printf("usage: %s filename\n", argv[0]);
  2105.         code = -1;
  2106.         return;
  2107.     }
  2108.     (void) command("SIZE %s", argv[1]);
  2109. }
  2110.  
  2111. /*
  2112.  * get last modification time of file on remote machine
  2113.  */
  2114. modtime(argc, argv)
  2115.     int argc;
  2116.     char *argv[];
  2117. {
  2118.     int overbose;
  2119.  
  2120.     if (argc < 2 && !another(&argc, &argv, "filename")) {
  2121.         printf("usage: %s filename\n", argv[0]);
  2122.         code = -1;
  2123.         return;
  2124.     }
  2125.     overbose = verbose;
  2126.     if (debug == 0)
  2127.         verbose = -1;
  2128.     if (command("MDTM %s", argv[1]) == COMPLETE) {
  2129.         int yy, mo, day, hour, min, sec;
  2130.         sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
  2131.             &day, &hour, &min, &sec);
  2132.         /* might want to print this in local time */
  2133.         printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],
  2134.             mo, day, yy, hour, min, sec);
  2135.     } else
  2136.         printf("%s\n", reply_string);
  2137.     verbose = overbose;
  2138. }
  2139.  
  2140. /*
  2141.  * show status on reomte machine
  2142.  */
  2143. rmtstatus(argc, argv)
  2144.     int argc;
  2145.     char *argv[];
  2146. {
  2147.     (void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
  2148. }
  2149.  
  2150. /*
  2151.  * get file if modtime is more recent than current file
  2152.  */
  2153. newer(argc, argv)
  2154.     int argc;
  2155.     char *argv[];
  2156. {
  2157.     if (getit(argc, argv, -1, "w"))
  2158.         printf("Local file \"%s\" is newer than remote file \"%s\"\n",
  2159.             argv[1], argv[2]);
  2160. }
  2161. @
  2162.