Annotation of cci/usr/src/ucb/ftp/cmds.c, revision 1.1

1.1     ! root        1: #ifdef lint
        !             2: static char sccsid[] = "@(#)cmds.c     4.9 (Berkeley) 7/26/83";
        !             3: #endif
        !             4: 
        !             5: /*
        !             6:  * FTP User Program -- Command Routines.
        !             7:  */
        !             8: /*
        !             9:  * Changes
        !            10:  *     . fix shell() so that arguments of shell commands are passed to
        !            11:  *       to the commands.
        !            12:  *
        !            13:  *     . fix mget() so that files are copied to current local directory
        !            14:  *       (instead of using the remote file name as destination file).
        !            15:  *
        !            16:  *     . fix shell() so that to give commands to shell (instead of
        !            17:  *       running the commands directly) for expanding of metacharacters.
        !            18:  *
        !            19:  *     . fix mls() so that it aborts the command if globulize() returns
        !            20:  *       w/ error and reverse the logics of confirm(), i.e. if user answers
        !            21:  *       'y' the command is carried out. Otherwise, it is ignored.
        !            22:  *
        !            23:  *     . fix remglob(),  so that it can distinguish the first time it is
        !            24:  *       called within a calling routine and the subsequent calls, by
        !            25:  *       introducing firstime flag.  The old logic doesn't work if the calling
        !            26:  *       routine -  mdelete(), mget(), mls() - is interrupted. 
        !            27:  *
        !            28:  */
        !            29: #include <sys/param.h>
        !            30: #include <sys/stat.h>
        !            31: #include <sys/socket.h>
        !            32: 
        !            33: #include <arpa/ftp.h>
        !            34: 
        !            35: #include <signal.h>
        !            36: #include <stdio.h>
        !            37: #include <errno.h>
        !            38: #include <netdb.h>
        !            39: 
        !            40: #include "ftp_var.h"
        !            41: 
        !            42: extern char *globerr;
        !            43: extern char **glob();
        !            44: extern char *home;
        !            45: extern short gflag;
        !            46: extern char *remglob();
        !            47: extern char *getenv();
        !            48: extern char *index();
        !            49: extern char *rindex();
        !            50: extern char *malloc();
        !            51: 
        !            52: /*
        !            53:  * Connect to peer server and
        !            54:  * auto-login, if possible.
        !            55:  */
        !            56: 
        !            57: setpeer(argc, argv)
        !            58:        int argc;
        !            59:        char *argv[];
        !            60: {
        !            61:        struct hostent *host, *hookup();
        !            62:        int port;
        !            63: 
        !            64:        if (connected) {
        !            65:                printf("Already connected to %s, use close first.\n",
        !            66:                        hostname);
        !            67:                return;
        !            68:        }
        !            69:        if (argc < 2) {
        !            70:                strcat(line, " ");
        !            71:                printf("(to) ");
        !            72:                gets(&line[strlen(line)]);
        !            73:                makeargv();
        !            74:                argc = margc;
        !            75:                argv = margv;
        !            76:        }
        !            77:        if (argc > 3) {
        !            78:                printf("usage: %s host-name [port]\n", argv[0]);
        !            79:                return;
        !            80:        }
        !            81:        port = sp->s_port;
        !            82:        if (argc > 2) {
        !            83:                port = atoi(argv[2]);
        !            84:                if (port <= 0) {
        !            85:                        printf("%s: bad port number-- %s\n", argv[1], argv[2]);
        !            86:                        printf ("usage: %s host-name [port]\n", argv[0]);
        !            87:                        return;
        !            88:                }
        !            89:                port = htons(port);
        !            90:        }
        !            91:        host = hookup(argv[1], port);
        !            92:        if (host) {
        !            93:                connected = 1;
        !            94:                if (autologin)
        !            95:                        login(host);
        !            96:        }
        !            97: }
        !            98: 
        !            99: struct types {
        !           100:        char    *t_name;
        !           101:        char    *t_mode;
        !           102:        int     t_type;
        !           103:        char    *t_arg;
        !           104: } types[] = {
        !           105:        { "ascii",      "A",    TYPE_A, 0 },
        !           106:        { "binary",     "I",    TYPE_I, 0 },
        !           107:        { "image",      "I",    TYPE_I, 0 },
        !           108:        { "ebcdic",     "E",    TYPE_E, 0 },
        !           109:        { "tenex",      "L",    TYPE_L, bytename },
        !           110:        0
        !           111: };
        !           112: 
        !           113: /*
        !           114:  * Set transfer type.
        !           115:  */
        !           116: settype(argc, argv)
        !           117:        char *argv[];
        !           118: {
        !           119:        register struct types *p;
        !           120:        int comret;
        !           121: 
        !           122:        if (argc > 2) {
        !           123:                char *sep;
        !           124: 
        !           125:                printf("usage: %s [", argv[0]);
        !           126:                sep = " ";
        !           127:                for (p = types; p->t_name; p++) {
        !           128:                        printf("%s%s", sep, p->t_name);
        !           129:                        if (*sep == ' ')
        !           130:                                sep = " | ";
        !           131:                }
        !           132:                printf(" ]\n");
        !           133:                return;
        !           134:        }
        !           135:        if (argc < 2) {
        !           136:                printf("Using %s mode to transfer files.\n", typename);
        !           137:                return;
        !           138:        }
        !           139:        for (p = types; p->t_name; p++)
        !           140:                if (strcmp(argv[1], p->t_name) == 0)
        !           141:                        break;
        !           142:        if (p->t_name == 0) {
        !           143:                printf("%s: unknown mode\n", argv[1]);
        !           144:                return;
        !           145:        }
        !           146:        if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
        !           147:                comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
        !           148:        else
        !           149:                comret = command("TYPE %s", p->t_mode);
        !           150:        if (comret == COMPLETE) {
        !           151:                strcpy(typename, p->t_name);
        !           152:                type = p->t_type;
        !           153:        }
        !           154: }
        !           155: 
        !           156: /*
        !           157:  * Set binary transfer type.
        !           158:  */
        !           159: /*VARARGS*/
        !           160: setbinary()
        !           161: {
        !           162: 
        !           163:        call(settype, "type", "binary", 0);
        !           164: }
        !           165: 
        !           166: /*
        !           167:  * Set ascii transfer type.
        !           168:  */
        !           169: /*VARARGS*/
        !           170: setascii()
        !           171: {
        !           172: 
        !           173:        call(settype, "type", "ascii", 0);
        !           174: }
        !           175: 
        !           176: /*
        !           177:  * Set tenex transfer type.
        !           178:  */
        !           179: /*VARARGS*/
        !           180: settenex()
        !           181: {
        !           182: 
        !           183:        call(settype, "type", "tenex", 0);
        !           184: }
        !           185: 
        !           186: /*
        !           187:  * Set ebcdic transfer type.
        !           188:  */
        !           189: /*VARARGS*/
        !           190: setebcdic()
        !           191: {
        !           192: 
        !           193:        call(settype, "type", "ebcdic", 0);
        !           194: }
        !           195: 
        !           196: /*
        !           197:  * Set file transfer mode.
        !           198:  */
        !           199: setmode(argc, argv)
        !           200:        char *argv[];
        !           201: {
        !           202: 
        !           203:        printf("We only support %s mode, sorry.\n", modename);
        !           204: }
        !           205: 
        !           206: /*
        !           207:  * Set file transfer format.
        !           208:  */
        !           209: setform(argc, argv)
        !           210:        char *argv[];
        !           211: {
        !           212: 
        !           213:        printf("We only support %s format, sorry.\n", formname);
        !           214: }
        !           215: 
        !           216: /*
        !           217:  * Set file transfer structure.
        !           218:  */
        !           219: setstruct(argc, argv)
        !           220:        char *argv[];
        !           221: {
        !           222: 
        !           223:        printf("We only support %s structure, sorry.\n", structname);
        !           224: }
        !           225: 
        !           226: put(argc, argv)
        !           227:        int argc;
        !           228:        char *argv[];
        !           229: {
        !           230:        char *cmd;
        !           231: 
        !           232:        if (argc == 2)
        !           233:                argc++, argv[2] = argv[1];
        !           234:        if (argc < 2) {
        !           235:                strcat(line, " ");
        !           236:                printf("(local-file) ");
        !           237:                gets(&line[strlen(line)]);
        !           238:                makeargv();
        !           239:                argc = margc;
        !           240:                argv = margv;
        !           241:        }
        !           242:        if (argc < 2) {
        !           243: usage:
        !           244:                printf("%s local-file remote-file\n", argv[0]);
        !           245:                return;
        !           246:        }
        !           247:        if (argc < 3) {
        !           248:                strcat(line, " ");
        !           249:                printf("(remote-file) ");
        !           250:                gets(&line[strlen(line)]);
        !           251:                makeargv();
        !           252:                argc = margc;
        !           253:                argv = margv;
        !           254:        }
        !           255:        if (argc < 3) 
        !           256:                goto usage;
        !           257:        if (!globulize(&argv[1]))
        !           258:                return;
        !           259:        cmd = (argv[0][0] == 'a') ? "APPE" : "STOR";
        !           260:        sendrequest(cmd, argv[1], argv[2]);
        !           261: }
        !           262: 
        !           263: /*
        !           264:  * Send multiple files.
        !           265:  */
        !           266: mput(argc, argv)
        !           267:        char *argv[];
        !           268: {
        !           269:        register int i;
        !           270: 
        !           271:        if (argc < 2) {
        !           272:                strcat(line, " ");
        !           273:                printf("(local-files) ");
        !           274:                gets(&line[strlen(line)]);
        !           275:                makeargv();
        !           276:                argc = margc;
        !           277:                argv = margv;
        !           278:        }
        !           279:        if (argc < 2) {
        !           280:                printf("%s local-files\n", argv[0]);
        !           281:                return;
        !           282:        }
        !           283:        for (i = 1; i < argc; i++) {
        !           284:                register char **cpp, **gargs;
        !           285: 
        !           286:                if (!doglob) {
        !           287:                        if (confirm(argv[0], argv[i])) 
        !           288:                                sendrequest("STOR", argv[i], argv[i]);
        !           289:                        continue;
        !           290:                }
        !           291:                gargs = glob(argv[i]);
        !           292:                if (globerr != NULL) {
        !           293:                        printf("%s\n", globerr);
        !           294:                        if (gargs)
        !           295:                                blkfree(gargs);
        !           296:                        continue;
        !           297:                }
        !           298:                for (cpp = gargs; cpp && *cpp != NULL; cpp++)
        !           299:                        if (confirm(argv[0], *cpp))
        !           300:                                sendrequest("STOR", *cpp, *cpp);
        !           301:                if (gargs != NULL)
        !           302:                        blkfree(gargs);
        !           303:        }
        !           304: }
        !           305: 
        !           306: /*
        !           307:  * Receive one file.
        !           308:  */
        !           309: get(argc, argv)
        !           310:        char *argv[];
        !           311: {
        !           312: 
        !           313:        if (argc == 2)
        !           314:                argc++, argv[2] = argv[1];
        !           315:        if (argc < 2) {
        !           316:                strcat(line, " ");
        !           317:                printf("(remote-file) ");
        !           318:                gets(&line[strlen(line)]);
        !           319:                makeargv();
        !           320:                argc = margc;
        !           321:                argv = margv;
        !           322:        }
        !           323:        if (argc < 2) {
        !           324: usage:
        !           325:                printf("%s remote-file [ local-file ]\n", argv[0]);
        !           326:                return;
        !           327:        }
        !           328:        if (argc < 3) {
        !           329:                strcat(line, " ");
        !           330:                printf("(local-file) ");
        !           331:                gets(&line[strlen(line)]);
        !           332:                makeargv();
        !           333:                argc = margc;
        !           334:                argv = margv;
        !           335:        }
        !           336:        if (argc < 3) 
        !           337:                goto usage;
        !           338:        if (!globulize(&argv[2]))
        !           339:                return;
        !           340:        recvrequest("RETR", argv[2], argv[1], "w");
        !           341: }
        !           342: 
        !           343: /*
        !           344:  * Get multiple files.
        !           345:  */
        !           346: mget(argc, argv)
        !           347:        char *argv[];
        !           348: {
        !           349:        char *cp;
        !           350: 
        !           351:        if (argc < 2) {
        !           352:                strcat(line, " ");
        !           353:                printf("(remote-files) ");
        !           354:                gets(&line[strlen(line)]);
        !           355:                makeargv();
        !           356:                argc = margc;
        !           357:                argv = margv;
        !           358:        }
        !           359:        if (argc < 2) {
        !           360:                printf("%s remote-files\n", argv[0]);
        !           361:                return;
        !           362:        }
        !           363:        while ((cp = remglob(argc, argv)) != NULL)
        !           364:                if (confirm(argv[0], cp)) {
        !           365:                        char    *local;
        !           366: 
        !           367:                        local = malloc((unsigned)(strlen(cp)+1));
        !           368:                        if (!local) {
        !           369:                                fprintf(stderr,"ftp: %s.\n",
        !           370:                                        "Cannot allocate memory");
        !           371:                                exit(1);
        !           372:                        }
        !           373:                        if (rindex(cp,'/'))
        !           374:                                strcpy(local, rindex(cp,'/')+1);
        !           375:                        else
        !           376:                                strcpy(local, cp);
        !           377:                        recvrequest("RETR", local, cp, "w");
        !           378:                }
        !           379: }
        !           380: 
        !           381: extern int firstime;
        !           382: 
        !           383: char *
        !           384: remglob(argc, argv)
        !           385:        char *argv[];
        !           386: {
        !           387:        char temp[16];
        !           388:        static char buf[MAXPATHLEN];
        !           389:        static FILE *ftemp = NULL;
        !           390:        static char **args;
        !           391:        int oldverbose, oldhash;
        !           392:        char *cp, *mode;
        !           393: 
        !           394:        if (!doglob) {
        !           395:                if (args == NULL)
        !           396:                        args = argv;
        !           397:                if ((cp = *++args) == NULL)
        !           398:                        args = NULL;
        !           399:                return (cp);
        !           400:        }
        !           401:        if (firstime == YES) {
        !           402:                strcpy(temp, "/tmp/ftpXXXXXX");
        !           403:                mktemp(temp);
        !           404:                oldverbose = verbose, verbose = 0;
        !           405:                oldhash = hash, hash = 0;
        !           406:                for (mode = "w"; *++argv != NULL; mode = "a")
        !           407:                        recvrequest ("NLST", temp, *argv, mode);
        !           408:                verbose = oldverbose; hash = oldhash;
        !           409:                ftemp = fopen(temp, "r");
        !           410:                unlink(temp);
        !           411:                if (ftemp == NULL) {
        !           412:                        printf("can't find list of remote files, oops\n");
        !           413:                        return (NULL);
        !           414:                }
        !           415:        }
        !           416:        if (fgets(buf, sizeof (buf), ftemp) == NULL) {
        !           417:                fclose(ftemp), ftemp = NULL;
        !           418:                return (NULL);
        !           419:        }
        !           420:        if ((cp = index(buf, '\n')) != NULL)
        !           421:                *cp = '\0';
        !           422:        firstime = NO;
        !           423:        return (buf);
        !           424: }
        !           425: 
        !           426: char *
        !           427: onoff(bool)
        !           428:        int bool;
        !           429: {
        !           430: 
        !           431:        return (bool ? "on" : "off");
        !           432: }
        !           433: 
        !           434: /*
        !           435:  * Show status.
        !           436:  */
        !           437: status(argc, argv)
        !           438:        char *argv[];
        !           439: {
        !           440: 
        !           441:        if (connected)
        !           442:                printf("Connected to %s.\n", hostname);
        !           443:        else
        !           444:                printf("Not connected.\n");
        !           445:        printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
        !           446:                modename, typename, formname, structname);
        !           447:        printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n", 
        !           448:                onoff(verbose), onoff(bell), onoff(interactive),
        !           449:                onoff(doglob));
        !           450:        printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
        !           451:                onoff(hash), onoff(sendport));
        !           452: }
        !           453: 
        !           454: /*
        !           455:  * Set beep on cmd completed mode.
        !           456:  */
        !           457: /*VARARGS*/
        !           458: setbell()
        !           459: {
        !           460: 
        !           461:        bell = !bell;
        !           462:        printf("Bell mode %s.\n", onoff(bell));
        !           463: }
        !           464: 
        !           465: /*
        !           466:  * Turn on packet tracing.
        !           467:  */
        !           468: /*VARARGS*/
        !           469: settrace()
        !           470: {
        !           471: 
        !           472:        trace = !trace;
        !           473:        printf("Packet tracing %s.\n", onoff(trace));
        !           474: }
        !           475: 
        !           476: /*
        !           477:  * Toggle hash mark printing during transfers.
        !           478:  */
        !           479: /*VARARGS*/
        !           480: sethash()
        !           481: {
        !           482: 
        !           483:        hash = !hash;
        !           484:        printf("Hash mark printing %s", onoff(hash));
        !           485:        if (hash)
        !           486:                printf(" (%d bytes/hash mark)", BUFSIZ);
        !           487:        printf(".\n");
        !           488: }
        !           489: 
        !           490: /*
        !           491:  * Turn on printing of server echo's.
        !           492:  */
        !           493: /*VARARGS*/
        !           494: setverbose()
        !           495: {
        !           496: 
        !           497:        verbose = !verbose;
        !           498:        printf("Verbose mode %s.\n", onoff(verbose));
        !           499: }
        !           500: 
        !           501: /*
        !           502:  * Toggle PORT cmd use before each data connection.
        !           503:  */
        !           504: /*VARARGS*/
        !           505: setport()
        !           506: {
        !           507: 
        !           508:        sendport = !sendport;
        !           509:        printf("Use of PORT cmds %s.\n", onoff(sendport));
        !           510: }
        !           511: 
        !           512: /*
        !           513:  * Turn on interactive prompting
        !           514:  * during mget, mput, and mdelete.
        !           515:  */
        !           516: /*VARARGS*/
        !           517: setprompt()
        !           518: {
        !           519: 
        !           520:        interactive = !interactive;
        !           521:        printf("Interactive mode %s.\n", onoff(interactive));
        !           522: }
        !           523: 
        !           524: /*
        !           525:  * Toggle metacharacter interpretation
        !           526:  * on local file names.
        !           527:  */
        !           528: /*VARARGS*/
        !           529: setglob()
        !           530: {
        !           531:        
        !           532:        doglob = !doglob;
        !           533:        printf("Globbing %s.\n", onoff(doglob));
        !           534: }
        !           535: 
        !           536: /*
        !           537:  * Set debugging mode on/off and/or
        !           538:  * set level of debugging.
        !           539:  */
        !           540: /*VARARGS*/
        !           541: setdebug(argc, argv)
        !           542:        char *argv[];
        !           543: {
        !           544:        int val;
        !           545: 
        !           546:        if (argc > 1) {
        !           547:                val = atoi(argv[1]);
        !           548:                if (val < 0) {
        !           549:                        printf("%s: bad debugging value.\n", argv[1]);
        !           550:                        return;
        !           551:                }
        !           552:        } else
        !           553:                val = !debug;
        !           554:        debug = val;
        !           555:        if (debug)
        !           556:                options |= SO_DEBUG;
        !           557:        else
        !           558:                options &= ~SO_DEBUG;
        !           559:        printf("Debugging %s (debug=%d).\n", onoff(debug), debug);
        !           560: }
        !           561: 
        !           562: /*
        !           563:  * Set current working directory
        !           564:  * on remote machine.
        !           565:  */
        !           566: cd(argc, argv)
        !           567:        char *argv[];
        !           568: {
        !           569: 
        !           570:        if (argc < 2) {
        !           571:                strcat(line, " ");
        !           572:                printf("(remote-directory) ");
        !           573:                gets(&line[strlen(line)]);
        !           574:                makeargv();
        !           575:                argc = margc;
        !           576:                argv = margv;
        !           577:        }
        !           578:        if (argc < 2) {
        !           579:                printf("%s remote-directory\n", argv[0]);
        !           580:                return;
        !           581:        }
        !           582:        (void) command("CWD %s", argv[1]);
        !           583: }
        !           584: 
        !           585: /*
        !           586:  * Set current working directory
        !           587:  * on local machine.
        !           588:  */
        !           589: lcd(argc, argv)
        !           590:        char *argv[];
        !           591: {
        !           592:        char buf[MAXPATHLEN];
        !           593: 
        !           594:        if (argc < 2)
        !           595:                argc++, argv[1] = home;
        !           596:        if (argc != 2) {
        !           597:                printf("%s local-directory\n", argv[0]);
        !           598:                return;
        !           599:        }
        !           600:        if (!globulize(&argv[1]))
        !           601:                return;
        !           602:        if (chdir(argv[1]) < 0) {
        !           603:                perror(argv[1]);
        !           604:                return;
        !           605:        }
        !           606:        printf("Local directory now %s\n", getwd(buf));
        !           607: }
        !           608: 
        !           609: /*
        !           610:  * Delete a single file.
        !           611:  */
        !           612: delete(argc, argv)
        !           613:        char *argv[];
        !           614: {
        !           615: 
        !           616:        if (argc < 2) {
        !           617:                strcat(line, " ");
        !           618:                printf("(remote-file) ");
        !           619:                gets(&line[strlen(line)]);
        !           620:                makeargv();
        !           621:                argc = margc;
        !           622:                argv = margv;
        !           623:        }
        !           624:        if (argc < 2) {
        !           625:                printf("%s remote-file\n", argv[0]);
        !           626:                return;
        !           627:        }
        !           628:        (void) command("DELE %s", argv[1]);
        !           629: }
        !           630: 
        !           631: /*
        !           632:  * Delete multiple files.
        !           633:  */
        !           634: mdelete(argc, argv)
        !           635:        char *argv[];
        !           636: {
        !           637:        char *cp;
        !           638: 
        !           639:        if (argc < 2) {
        !           640:                strcat(line, " ");
        !           641:                printf("(remote-files) ");
        !           642:                gets(&line[strlen(line)]);
        !           643:                makeargv();
        !           644:                argc = margc;
        !           645:                argv = margv;
        !           646:        }
        !           647:        if (argc < 2) {
        !           648:                printf("%s remote-files\n", argv[0]);
        !           649:                return;
        !           650:        }
        !           651:        while ((cp = remglob(argc, argv)) != NULL)
        !           652:                if (confirm(argv[0], cp))
        !           653:                        (void) command("DELE %s", cp);
        !           654: }
        !           655: 
        !           656: /*
        !           657:  * Rename a remote file.
        !           658:  */
        !           659: renamefile(argc, argv)
        !           660:        char *argv[];
        !           661: {
        !           662: 
        !           663:        if (argc < 2) {
        !           664:                strcat(line, " ");
        !           665:                printf("(from-name) ");
        !           666:                gets(&line[strlen(line)]);
        !           667:                makeargv();
        !           668:                argc = margc;
        !           669:                argv = margv;
        !           670:        }
        !           671:        if (argc < 2) {
        !           672: usage:
        !           673:                printf("%s from-name to-name\n", argv[0]);
        !           674:                return;
        !           675:        }
        !           676:        if (argc < 3) {
        !           677:                strcat(line, " ");
        !           678:                printf("(to-name) ");
        !           679:                gets(&line[strlen(line)]);
        !           680:                makeargv();
        !           681:                argc = margc;
        !           682:                argv = margv;
        !           683:        }
        !           684:        if (argc < 3) 
        !           685:                goto usage;
        !           686:        if (command("RNFR %s", argv[1]) == CONTINUE)
        !           687:                (void) command("RNTO %s", argv[2]);
        !           688: }
        !           689: 
        !           690: /*
        !           691:  * Get a directory listing
        !           692:  * of remote files.
        !           693:  */
        !           694: ls(argc, argv)
        !           695:        char *argv[];
        !           696: {
        !           697:        char *cmd;
        !           698: 
        !           699:        if (argc < 2)
        !           700:                argc++, argv[1] = NULL;
        !           701:        if (argc < 3)
        !           702:                argc++, argv[2] = "-";
        !           703:        if (argc > 3) {
        !           704:                printf("usage: %s remote-directory local-file\n", argv[0]);
        !           705:                return;
        !           706:        }
        !           707:        cmd = argv[0][0] == 'l' ? "NLST" : "LIST";
        !           708:        if (strcmp(argv[2], "-") && !globulize(&argv[2]))
        !           709:                return;
        !           710:        recvrequest(cmd, argv[2], argv[1], "w");
        !           711: }
        !           712: 
        !           713: /*
        !           714:  * Get a directory listing
        !           715:  * of multiple remote files.
        !           716:  */
        !           717: mls(argc, argv)
        !           718:        char *argv[];
        !           719: {
        !           720:        char *cmd, *mode, *cp, *dest;
        !           721: 
        !           722:        if (argc < 2) {
        !           723:                strcat(line, " ");
        !           724:                printf("(remote-files) ");
        !           725:                gets(&line[strlen(line)]);
        !           726:                makeargv();
        !           727:                argc = margc;
        !           728:                argv = margv;
        !           729:        }
        !           730:        if (argc < 3) {
        !           731:                strcat(line, " ");
        !           732:                printf("(local-file) ");
        !           733:                gets(&line[strlen(line)]);
        !           734:                makeargv();
        !           735:                argc = margc;
        !           736:                argv = margv;
        !           737:        }
        !           738:        if (argc < 3) {
        !           739:                printf("%s remote-files local-file\n", argv[0]);
        !           740:                return;
        !           741:        }
        !           742:        dest = argv[argc - 1];
        !           743:        argv[argc - 1] = NULL;
        !           744:        if (strcmp(dest, "-"))
        !           745:             if (!globulize(&dest) || !confirm("local-file", dest))
        !           746:                        return; /* not this local file. Cancel action */
        !           747:        cmd = argv[0][1] == 'l' ? "NLST" : "LIST";
        !           748:        for (mode = "w"; cp = remglob(argc, argv); mode = "a")
        !           749:                if (confirm(argv[0], cp))
        !           750:                        recvrequest(cmd, dest, cp, mode);
        !           751: }
        !           752: 
        !           753: /*
        !           754:  * Do a shell escape
        !           755:  */
        !           756: shell(argc, argv)
        !           757:        char *argv[];
        !           758: {
        !           759:        int pid, status, (*old1)(), (*old2)();
        !           760:        char shellnam[40], *shell, *namep;
        !           761:        char **cpp, **gargs;
        !           762: 
        !           763:        if (debug) {
        !           764:                register char **zip = argv;
        !           765: 
        !           766:                printf("argc: %d\n", argc);
        !           767:                printf("%s", *zip);
        !           768:                while (*++zip != NULL)
        !           769:                        printf(" %s", *zip);
        !           770:                printf("\n");
        !           771:                fflush(stdout);
        !           772:        }
        !           773:        old1 = signal (SIGINT, SIG_IGN);
        !           774:        old2 = signal (SIGQUIT, SIG_IGN);
        !           775:        if ((pid = fork()) == 0) {
        !           776: 
        !           777:                char    *cmd;
        !           778: 
        !           779:                for (pid = 3; pid < 20; pid++)
        !           780:                        close(pid);
        !           781:                signal(SIGINT, SIG_DFL);
        !           782:                signal(SIGQUIT, SIG_DFL);
        !           783:                shell = getenv("SHELL");
        !           784:                if (shell == NULL)
        !           785:                        shell = "/bin/sh";
        !           786:                if (argc <= 1) {
        !           787:                        namep = rindex(shell,'/');
        !           788:                        if (namep == NULL)
        !           789:                                namep = shell;
        !           790:                        strcpy(shellnam,"-");
        !           791:                        strcat(shellnam, ++namep);
        !           792:                        if (debug)
        !           793:                                printf("namep: %s\n", namep);
        !           794:                        if (strcmp(namep, "sh") != 0)
        !           795:                                shellnam[0] = '+';
        !           796:                        if (debug)
        !           797:                                printf("shellname: %s\n", shellnam);
        !           798:                        execl(shell, shellnam, (char *)0);
        !           799:                        perror(shell);
        !           800:                        exit(1);
        !           801:                }
        !           802:                if (debug)
        !           803:                        printf("run %s %s %s %s\n",shell,
        !           804:                                shellnam,"-c",argv[1]), fflush(stdout);
        !           805:                execl(shell, shellnam, "-c", argv[1], 0);
        !           806:                perror(shell);          
        !           807:                exit(1);
        !           808:        }
        !           809:        if (pid > 0)
        !           810:                while (wait(&status) != pid)
        !           811:                        ;
        !           812:        signal(SIGINT, old1);
        !           813:        signal(SIGQUIT, old2);
        !           814:        if (pid == -1)
        !           815:                perror("Try again later");
        !           816:        return (0);
        !           817: }
        !           818: 
        !           819: /*
        !           820:  * Send new user information (re-login)
        !           821:  */
        !           822: user(argc, argv)
        !           823:        int argc;
        !           824:        char **argv;
        !           825: {
        !           826:        char acct[80], *getpass();
        !           827:        int n;
        !           828: 
        !           829:        if (argc < 2) {
        !           830:                strcat(line, " ");
        !           831:                printf("(username) ");
        !           832:                gets(&line[strlen(line)]);
        !           833:                makeargv();
        !           834:                argc = margc;
        !           835:                argv = margv;
        !           836:        }
        !           837:        if (argc > 4) {
        !           838:                printf("usage: %s username [password] [account]\n", argv[0]);
        !           839:                return (0);
        !           840:        }
        !           841:        n = command("USER %s", argv[1]);
        !           842:        if (n == CONTINUE) {
        !           843:                if (argc < 3 )
        !           844:                        argv[2] = getpass("Password: "), argc++;
        !           845:                n = command("PASS %s", argv[2]);
        !           846:        }
        !           847:        if (n == CONTINUE) {
        !           848:                if (argc < 4) {
        !           849:                        printf("Account: "); (void) fflush(stdout);
        !           850:                        (void) fgets(acct, sizeof(acct) - 1, stdin);
        !           851:                        acct[strlen(acct) - 1] = '\0';
        !           852:                        argv[3] = acct; argc++;
        !           853:                }
        !           854:                n = command("ACCT %s", acct);
        !           855:        }
        !           856:        if (n != COMPLETE) {
        !           857:                fprintf(stderr, "Login failed.\n");
        !           858:                return (0);
        !           859:        }
        !           860:        return (1);
        !           861: }
        !           862: 
        !           863: /*
        !           864:  * Print working directory.
        !           865:  */
        !           866: /*VARARGS*/
        !           867: pwd()
        !           868: {
        !           869: 
        !           870:        (void) command("XPWD");
        !           871: }
        !           872: 
        !           873: /*
        !           874:  * Make a directory.
        !           875:  */
        !           876: makedir(argc, argv)
        !           877:        char *argv[];
        !           878: {
        !           879: 
        !           880:        if (argc < 2) {
        !           881:                strcat(line, " ");
        !           882:                printf("(directory-name) ");
        !           883:                gets(&line[strlen(line)]);
        !           884:                makeargv();
        !           885:                argc = margc;
        !           886:                argv = margv;
        !           887:        }
        !           888:        if (argc < 2) {
        !           889:                printf("%s directory-name\n", argv[0]);
        !           890:                return;
        !           891:        }
        !           892:        (void) command("XMKD %s", argv[1]);
        !           893: }
        !           894: 
        !           895: /*
        !           896:  * Remove a directory.
        !           897:  */
        !           898: removedir(argc, argv)
        !           899:        char *argv[];
        !           900: {
        !           901: 
        !           902:        if (argc < 2) {
        !           903:                strcat(line, " ");
        !           904:                printf("(directory-name) ");
        !           905:                gets(&line[strlen(line)]);
        !           906:                makeargv();
        !           907:                argc = margc;
        !           908:                argv = margv;
        !           909:        }
        !           910:        if (argc < 2) {
        !           911:                printf("%s directory-name\n", argv[0]);
        !           912:                return;
        !           913:        }
        !           914:        (void) command("XRMD %s", argv[1]);
        !           915: }
        !           916: 
        !           917: /*
        !           918:  * Send a line, verbatim, to the remote machine.
        !           919:  */
        !           920: quote(argc, argv)
        !           921:        char *argv[];
        !           922: {
        !           923:        int i;
        !           924:        char buf[BUFSIZ];
        !           925: 
        !           926:        if (argc < 2) {
        !           927:                strcat(line, " ");
        !           928:                printf("(command line to send) ");
        !           929:                gets(&line[strlen(line)]);
        !           930:                makeargv();
        !           931:                argc = margc;
        !           932:                argv = margv;
        !           933:        }
        !           934:        if (argc < 2) {
        !           935:                printf("usage: %s line-to-send\n", argv[0]);
        !           936:                return;
        !           937:        }
        !           938:        strcpy(buf, argv[1]);
        !           939:        for (i = 2; i < argc; i++) {
        !           940:                strcat(buf, " ");
        !           941:                strcat(buf, argv[i]);
        !           942:        }
        !           943:        (void) command(buf);
        !           944: }
        !           945: 
        !           946: /*
        !           947:  * Ask the other side for help.
        !           948:  */
        !           949: rmthelp(argc, argv)
        !           950:        char *argv[];
        !           951: {
        !           952:        int oldverbose = verbose;
        !           953: 
        !           954:        verbose = 1;
        !           955:        (void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
        !           956:        verbose = oldverbose;
        !           957: }
        !           958: 
        !           959: /*
        !           960:  * Terminate session and exit.
        !           961:  */
        !           962: /*VARARGS*/
        !           963: quit()
        !           964: {
        !           965: 
        !           966:        disconnect();
        !           967:        exit(0);
        !           968: }
        !           969: 
        !           970: /*
        !           971:  * Terminate session, but don't exit.
        !           972:  */
        !           973: disconnect()
        !           974: {
        !           975:        extern FILE *cout;
        !           976:        extern int data;
        !           977: 
        !           978:        if (!connected)
        !           979:                return;
        !           980:        (void) command("QUIT");
        !           981:        (void) fclose(cout);
        !           982:        cout = NULL;
        !           983:        connected = 0;
        !           984:        data = -1;
        !           985: }
        !           986: 
        !           987: confirm(cmd, file)
        !           988:        char *cmd, *file;
        !           989: {
        !           990:        char line[BUFSIZ];
        !           991: 
        !           992:        if (!interactive)
        !           993:                return (1);
        !           994:        printf("%s %s? ", cmd, file);
        !           995:        fflush(stdout);
        !           996:        gets(line);
        !           997:        return (*line != 'n' && *line != 'N');
        !           998: }
        !           999: 
        !          1000: fatal(msg)
        !          1001:        char *msg;
        !          1002: {
        !          1003: 
        !          1004:        fprintf(stderr, "ftp: %s\n");
        !          1005:        exit(1);
        !          1006: }
        !          1007: 
        !          1008: /*
        !          1009:  * Glob a local file name specification with
        !          1010:  * the expectation of a single return value.
        !          1011:  * Can't control multiple values being expanded
        !          1012:  * from the expression, we return only the first.
        !          1013:  */
        !          1014: globulize(cpp)
        !          1015:        char **cpp;
        !          1016: {
        !          1017:        char **globbed;
        !          1018: 
        !          1019:        if (!doglob)
        !          1020:                return (1);
        !          1021:        globbed = glob(*cpp);
        !          1022:        if (globerr != NULL) {
        !          1023:                printf("%s: %s\n", *cpp, globerr);
        !          1024:                if (globbed)
        !          1025:                        blkfree(globbed);
        !          1026:                return (0);
        !          1027:        }
        !          1028:        if (globbed) {
        !          1029:                *cpp = *globbed++;
        !          1030:                /* don't waste too much memory */
        !          1031:                if (*globbed)
        !          1032:                        blkfree(globbed);
        !          1033:        }
        !          1034:        return (1);
        !          1035: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.