📄 cmds.c
字号:
/* Copyright (c) 1985, 1989 Regents of the University of California. *//* All rights reserved. *//* *//* Redistribution and use in source and binary forms, with or without *//* modification, are permitted provided that the following conditions *//* are met: *//* *//* 1. Redistributions of source code must retain the above copyright *//* notice, this list of conditions and the following disclaimer. *//* 2. Redistributions in binary form must reproduce the above copyright *//* notice, this list of conditions and the following disclaimer in the *//* documentation and/or other materials provided with the distribution. *//* 3. All advertising materials mentioning features or use of this software *//* must display the following acknowledgement: *//* This product includes software developed by the University of *//* California, Berkeley and its contributors. *//* 4. Neither the name of the University nor the names of its contributors *//* may be used to endorse or promote products derived from this software *//* without specific prior written permission. *//* *//* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND *//* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE*//* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE *//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL*//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *//* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *//* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT*//* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *//* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *//* SUCH DAMAGE. *//* FTP User Program -- Command Routines. */#include "ftp_var.h"#define DONT_NEED_SIGBLOCK#define DONT_NEED_SIGPAUSE#define DONT_NEED_SIGUNBLOCK#define DONT_NEED_SIGPENDING#include "sigfix.h"#include <arpa/ftp.h>#include <ctype.h>#include "pathnames.h"#ifndef HAVE_GETCWDextern char *getwd();#endifextern int command();extern char *home;extern char *globerr;extern char **ftpglob();extern char *remglob();extern int errno;extern off_t restart_point;extern char reply_string[];char *mname;jmp_buf jabort;char *dotrans(), *domap();/* * Options and other state info. */int trace; /* trace packets exchanged */int hash; /* print # for each buffer transferred */int sendport; /* use PORT cmd for each data connection */int verbose; /* print messages coming back from server */int connected; /* connected to server */int fromatty; /* input is from a terminal */int interactive; /* interactively prompt on m* cmds */int debug; /* debugging level */int bell; /* ring bell on cmd completion */int doglob; /* glob local file names */int autologin; /* establish user account on connection */int proxy; /* proxy server connection active */int proxflag; /* proxy connection exists */int sunique; /* store files on server with unique name */int runique; /* store local files with unique name */int mcase; /* map upper to lower case for mget names */int ntflag; /* use ntin ntout tables for name translation */int mapflag; /* use mapin mapout templates on file names */int code; /* return/reply code for ftp command */int crflag; /* if 1, strip car. rets. on ascii gets */char pasv[64]; /* passive port for proxy data connection */char *altarg; /* argv[1] with no shell-like preprocessing */char ntin[17]; /* input translation table */char ntout[17]; /* output translation table */char mapin[MAXPATHLEN]; /* input map template */char mapout[MAXPATHLEN]; /* output map template */char typename[32]; /* name of file transfer type */int type; /* requested file transfer type */int curtype; /* current file transfer type */char structname[32]; /* name of file transfer structure */int stru; /* file transfer structure */char formname[32]; /* name of file transfer format */int form; /* file transfer format */char modename[32]; /* name of file transfer mode */int mode; /* file transfer mode */char bytename[32]; /* local byte size in ascii */int bytesize; /* local byte size in binary */char *hostname; /* name of host connected to */int unix_server; /* server is unix, can use binary for ascii */int unix_proxy; /* proxy is unix, can use binary for ascii */struct servent *sp; /* service spec for tcp/ftp */int ftp_s_port; /* save ftp's sp->s_port value here */char line[200]; /* input line buffer */char *stringbase; /* current scan point in line buffer */char argbuf[200]; /* argument storage buffer */char *argbase; /* current storage point in arg buffer */int margc; /* count of arguments on input line */char *margv[20]; /* args parsed from input line */int cpend; /* flag: if != 0, then pending server reply */int mflag; /* flag: if != 0, then active multi command */int options; /* used during socket creation */jmp_buf toplevel; /* non-local goto stuff for cmd scanner */int macnum; /* number of defined macros */struct macel macros[16];char macbuf[4096];/* `Another' gets another argument, and stores the new argc and argv. *//* It reverts to the top level (via main.c's intr()) on EOF/error. *//* Returns false if no new arguments have been added. */int another(int *pargc, char ***pargv, char *prompt) { int len = strlen(line), ret; extern sig_t intr(); if (len >= sizeof(line) - 3) { printf("sorry, arguments too long\n"); intr(); } printf("(%s) ", prompt); line[len++] = ' '; if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) intr(); len += strlen(&line[len]); if (len > 0 && line[len - 1] == '\n') line[len - 1] = '\0'; makeargv(); ret = margc > *pargc; *pargc = margc; *pargv = margv; return ret;}/* Connect to peer server and auto-login, if possible. */void setpeer(int argc, char *argv[]) { char *host, *hookup(); short port; if (connected) { printf("Already connected to %s, use close first.\n", hostname); code = -1; return; } if (argc < 2) another(&argc, &argv, "to"); if (argc < 2 || argc > 3) { printf("usage: %s host-name [port]\n", argv[0]); code = -1; return; } /* port = sp->s_port; */ port = ftp_s_port; if (argc > 2) { port = atoi(argv[2]); if (port <= 0) { printf("%s: bad port number-- %s\n", argv[1], argv[2]); printf ("usage: %s host-name [port]\n", argv[0]); code = -1; return; } port = htons(port); } host = hookup(argv[1], port); if (host) {#if NBBY == 8 int overbose;#endif connected = 1; /* Set up defaults for FTP. */ strcpy(typename, "ascii"), type = TYPE_A; curtype = TYPE_A; strcpy(formname, "non-print"), form = FORM_N; strcpy(modename, "stream"), mode = MODE_S; strcpy(structname, "file"), stru = STRU_F; strcpy(bytename, "8"), bytesize = 8; if (autologin) ftplogin(argv[1]); #if NBBY == 8 /* this ifdef is to keep someone form "porting" this to an */ /* incompatible system and not checking this out. This way they have */ /* to think about it. */ overbose = verbose; if (debug == 0) verbose = -1; if (command("SYST\r\n") == COMPLETE && overbose) { register char *cp, c; cp = index(reply_string+4, ' '); if (cp == NULL) cp = index(reply_string+4, '\r'); if (cp) { if (cp[-1] == '.') cp--; c = *cp; *cp = '\0'; } printf("Remote system type is %s.\n", reply_string+4); if (cp) *cp = c; } if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) { if (proxy) unix_proxy = 1; else unix_server = 1; /* Set type to 0 (not specified by user), meaning binary by */ /* default, but don't bother telling server. We can use binary */ /* for text files unless changed by the user. */ type = 0; strcpy(typename, "binary"); if (overbose) printf("Using %s mode to transfer files.\n", typename); } else { if (proxy) unix_proxy = 0; else unix_server = 0; if (overbose && !strncmp(reply_string, "215 TOPS20", 10)) printf("Remember to set tenex mode when transfering binary files from this machine.\n"); } verbose = overbose;#endif }}struct types { char *t_name; char *t_mode; int t_type; char *t_arg;} types[] = { { "ascii", "A", TYPE_A, 0 }, { "binary", "I", TYPE_I, 0 }, { "image", "I", TYPE_I, 0 }, { "ebcdic", "E", TYPE_E, 0 }, { "tenex", "L", TYPE_L, bytename }, { NULL, NULL, 0, NULL }};/* Set transfer type. */void settype(int argc, char *argv[]) { register struct types *p; int comret; if (argc > 2) { char *sep; printf("usage: %s [", argv[0]); sep = " "; for (p = types; p->t_name; p++) { printf("%s%s", sep, p->t_name); sep = " | "; } printf(" ]\n"); code = -1; return; } if (argc < 2) { printf("Using %s mode to transfer files.\n", typename); code = 0; return; } for (p = types; p->t_name; p++) if (strcmp(argv[1], p->t_name) == 0) break; if (p->t_name == 0) { printf("%s: unknown mode\n", argv[1]); code = -1; return; } if ((p->t_arg != NULL) && (*(p->t_arg) != '\0')) comret = command ("TYPE %s %s\r\n", p->t_mode, p->t_arg); else comret = command("TYPE %s\r\n", p->t_mode); if (comret == COMPLETE) { strcpy(typename, p->t_name); curtype = type = p->t_type; }}/* Internal form of settype; changes current type in use with server *//* without changing our notion of the type for data transfers. *//* Used to change to and from ascii for listings. */void changetype(int newtype, int show) { register struct types *p; int comret, oldverbose = verbose; if (newtype == 0) newtype = TYPE_I; if (newtype == curtype) return; if (debug == 0 && show == 0) verbose = 0; for (p = types; p->t_name; p++) if (newtype == p->t_type) break; if (p->t_name == 0) { printf("ftp: internal error: unknown type %d\n", newtype); return; } if (newtype == TYPE_L && bytename[0] != '\0') comret = command("TYPE %s %s\r\n", p->t_mode, bytename); else comret = command("TYPE %s\r\n", p->t_mode); if (comret == COMPLETE) curtype = newtype; verbose = oldverbose;}char *stype[] = { "type", "", 0};/* Set binary transfer type. */void setbinary() { stype[1] = "binary"; settype(2, stype);}/* Set ascii transfer type. */void setascii() { stype[1] = "ascii"; settype(2, stype);}/* Set tenex transfer type. */void settenex() { stype[1] = "tenex"; settype(2, stype);}/* Set file transfer mode. */void setftpmode(int argc, char *argv[]) { printf("We only support %s mode, sorry.\n", modename); code = -1;}/* Set file transfer format. */void setform(int argc, char *argv[]) { printf("We only support %s format, sorry.\n", formname); code = -1;}/* Set file transfer structure. */void setstruct(int argc, char *argv[]) { printf("We only support %s structure, sorry.\n", structname); code = -1;}/* Glob a local file name specification with the expectation of a single *//* return value. Can't control multiple values being expanded from the *//* expression, we return only the first. */int globulize(char **cpp) { char **globbed; if (!doglob) return (1); globbed = ftpglob(*cpp); if (globerr != NULL) { printf("%s: %s\n", *cpp, globerr); if (globbed) { blkfree(globbed); free((char *)globbed); } return 0; } if (globbed) { *cpp = *globbed++; /* don't waste too much memory */ if (*globbed) { blkfree(globbed); free((char *)globbed); } } return 1;}/* Send a single file. */void put(int argc, char *argv[]) { char *cmd; int loc = 0; char *oldargv1, *oldargv2; if (argc == 2) { argc++; argv[2] = argv[1]; loc++; } if (argc < 2 && !another(&argc, &argv, "local-file")) goto usage; if (argc < 3 && !another(&argc, &argv, "remote-file")) { usage: printf("usage: %s local-file remote-file\n", argv[0]); code = -1; return; } oldargv1 = argv[1]; oldargv2 = argv[2]; if (!globulize(&argv[1])) { code = -1; return; } /* If "globulize" modifies argv[1], and argv[2] is a copy of */ /* the old argv[1], make it a copy of the new argv[1]. */ if (argv[1] != oldargv1 && argv[2] == oldargv1) argv[2] = argv[1]; cmd = (argv[0][0] == 'a') ? "APPE" : ((sunique) ? "STOU" : "STOR"); if (loc && ntflag) argv[2] = dotrans(argv[2]); if (loc && mapflag) argv[2] = domap(argv[2]); sendrequest(cmd, argv[1], argv[2], argv[1] != oldargv1 || argv[2] != oldargv2);}int confirm(char *cmd, char *file) { char bline[BUFSIZ]; if (!interactive) return (1); printf("%s %s? ", cmd, file); fflush(stdout); if (fgets(bline, sizeof(bline), stdin) == NULL) return (0); return (*bline != 'n' && *bline != 'N');}/* Send multiple files. */void mabort() { int ointer; extern jmp_buf jabort; printf("\n"); fflush(stdout); if (mflag && fromatty) { ointer = interactive; interactive = 1; if (confirm("Continue with", mname)) { interactive = ointer; longjmp(jabort,0); } interactive = ointer; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -