⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmds.c

📁 UNIX FTP client
💻 C
📖 第 1 页 / 共 3 页
字号:
			mflag = 0;			continue;		}		if (mflag && confirm(argv[0], cp)) {			tp = cp;			if (mcase) {				while (*tp && !islower(*tp)) {					tp++;				}				if (!*tp) {					tp = cp;					tp2 = tmpbuf;					while ((*tp2 = *tp) != '\0') {						if (isupper(*tp2)) {							*tp2 = 'a' + *tp2 - 'A';						}						tp++;						tp2++;					}				}				tp = tmpbuf;			}			if (ntflag) {				tp = dotrans(tp);			}			if (mapflag) {				tp = domap(tp);			}			recvrequest("RETR", tp, cp, "w",			    tp != cp || !interactive);			if (!mflag && fromatty) {				ointer = interactive;				interactive = 1;				if (confirm("Continue with","mget")) {					mflag++;				}				interactive = ointer;			}		}	}	(void) signal(SIGINT,oldintr);	mflag = 0;}char *remglob(char *argv[], int doswitch){	char temp[16];	static char buf[MAXPATHLEN];	static FILE *ftemp = NULL;	static char **args;	int oldverbose, oldhash;	char *cp;	if (!mflag) {		if (!doglob) {			args = NULL;		}		else {			if (ftemp) {				(void) fclose(ftemp);				ftemp = NULL;			}		}		return(NULL);	}	if (!doglob) {		if (args == NULL)			args = argv;		if ((cp = *++args) == NULL)			args = NULL;		return (cp);	}	if (ftemp == NULL) {		int oldumask, fd;		(void) strcpy(temp, _PATH_TMP);		/* libc 5.2.18 creates with mode 0666, which is dumb */		oldumask = umask(077);		fd = mkstemp(temp);		umask(oldumask);		if (fd<0) {			printf("Error creating temporary file, oops\n");			return NULL;		}				oldverbose = verbose, verbose = 0;		oldhash = hash, hash = 0;		if (doswitch) {			pswitch(!proxy);		}		while (*++argv != NULL) {			recvrequest ("NLST", temp, *argv, "a", 0);		}		unlink(temp);		if (doswitch) {			pswitch(!proxy);		}		verbose = oldverbose; hash = oldhash;		ftemp = fdopen(fd, "r");		if (ftemp == NULL) {			printf("fdopen failed, oops\n");			return NULL;		}	}	if (fgets(buf, sizeof (buf), ftemp) == NULL) {		(void) fclose(ftemp), ftemp = NULL;		return (NULL);	}	if ((cp = index(buf, '\n')) != NULL)		*cp = '\0';	return (buf);}static const char *onoff(int bool){	return (bool ? "on" : "off");}/* * Show status. *//*ARGSUSED*/voidstatus(void){	int i;	if (connected)		printf("Connected to %s.\n", hostname);	else		printf("Not connected.\n");	if (!proxy) {		pswitch(1);		if (connected) {			printf("Connected for proxy commands to %s.\n", hostname);		}		else {			printf("No proxy connection.\n");		}		pswitch(0);	}	printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",		modename, typename, formname, structname);	printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n", 		onoff(verbose), onoff(bell), onoff(interactive),		onoff(doglob));	printf("Store unique: %s; Receive unique: %s\n", onoff(sunique),		onoff(runique));	printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag));	if (ntflag) {		printf("Ntrans: (in) %s (out) %s\n", ntin,ntout);	}	else {		printf("Ntrans: off\n");	}	if (mapflag) {		printf("Nmap: (in) %s (out) %s\n", mapin, mapout);	}	else {		printf("Nmap: off\n");	}	printf("Hash mark printing: %s; Use of PORT cmds: %s\n",		onoff(hash), onoff(sendport));	printf("Tick counter printing: %s\n", onoff(tick));	if (macnum > 0) {		printf("Macros:\n");		for (i=0; i<macnum; i++) {			printf("\t%s\n",macros[i].mac_name);		}	}	code = 0;}/* * Set beep on cmd completed mode. *//*VARARGS*/voidsetbell(void){	bell = !bell;	printf("Bell mode %s.\n", onoff(bell));	code = bell;}/* * Turn on packet tracing. *//*VARARGS*/voidsettrace(void){	traceflag = !traceflag;	printf("Packet tracing %s.\n", onoff(traceflag));	code = traceflag;}/* * Toggle hash mark printing during transfers. *//*VARARGS*/voidsethash(void){	hash = !hash;	if (hash && tick)		settick(); 	printf("Hash mark printing %s", onoff(hash));	code = hash;	if (hash)		printf(" (%d bytes/hash mark)", 1024);	printf(".\n");}/* * Toggle tick counter printing during transfers. *//*VARARGS*/voidsettick(void){	tick = !tick;	if (hash && tick)		sethash();	printf("Tick counter printing %s", onoff(tick));	code = tick;	if (tick)		printf(" (%d bytes/tick increment)", TICKBYTES);	printf(".\n");}/* * Turn on printing of server echo's. *//*VARARGS*/voidsetverbose(void){	verbose = !verbose;	printf("Verbose mode %s.\n", onoff(verbose));	code = verbose;}/* * Toggle PORT cmd use before each data connection. */voidsetport(void){	sendport = !sendport;	printf("Use of PORT cmds %s.\n", onoff(sendport));	code = sendport;}/* * Turn on interactive prompting * during mget, mput, and mdelete. */voidsetprompt(void){	interactive = !interactive;	printf("Interactive mode %s.\n", onoff(interactive));	code = interactive;}/* * Toggle metacharacter interpretation * on local file names. */voidsetglob(void){		doglob = !doglob;	printf("Globbing %s.\n", onoff(doglob));	code = doglob;}/* * Set debugging mode on/off and/or * set level of debugging. *//*VARARGS*/voidsetdebug(argc, argv)	int argc;	char *argv[];{	int val;	if (argc > 1) {		val = atoi(argv[1]);		if (val < 0) {			printf("%s: bad debugging value.\n", argv[1]);			code = -1;			return;		}	} else		val = !debug;	debug = val;	if (debug)		options |= SO_DEBUG;	else		options &= ~SO_DEBUG;	printf("Debugging %s (debug=%d).\n", onoff(debug), debug);	code = debug > 0;}/* * Set current working directory * on remote machine. */voidcd(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "remote-directory")) {		printf("usage: %s remote-directory\n", argv[0]);		code = -1;		return;	}	if (command("CWD %s", argv[1]) == ERROR && code == 500) {		if (verbose)			printf("CWD command not recognized, trying XCWD\n");		(void) command("XCWD %s", argv[1]);	}}/* * Set current working directory * on local machine. */voidlcd(int argc, char *argv[]){	char buf[MAXPATHLEN];	const char *dir = NULL;	if (argc == 1) {	    /*dir = home;*/	    dir = ".";	}	else if (argc != 2) {		printf("usage: %s local-directory\n", argv[0]);		code = -1;		return;	}	else {	    dir = globulize(argv[1]);	}	if (!dir) {		code = -1;		return;	}	if (chdir(dir) < 0) {		fprintf(stderr, "local: %s: %s\n", dir, strerror(errno));		code = -1;		return;	}	if (!getcwd(buf, sizeof(buf))) {	    if (errno==ERANGE) strcpy(buf, "<too long>");	    else strcpy(buf, "???");	}	printf("Local directory now %s\n", buf);	code = 0;}/* * Delete a single file. */voiddelete_cmd(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "remote-file")) {		printf("usage: %s remote-file\n", argv[0]);		code = -1;		return;	}	(void) command("DELE %s", argv[1]);}/* * Delete multiple files. */voidmdelete(int argc, char *argv[]){	void (*oldintr)(int);	int ointer;	char *cp;	if (argc < 2 && !another(&argc, &argv, "remote-files")) {		printf("usage: %s remote-files\n", argv[0]);		code = -1;		return;	}	mname = argv[0];	mflag = 1;	oldintr = signal(SIGINT, mabort);	(void) sigsetjmp(jabort, 1);	while ((cp = remglob(argv,0)) != NULL) {		if (*cp == '\0') {			mflag = 0;			continue;		}		if (mflag && confirm(argv[0], cp)) {			(void) command("DELE %s", cp);			if (!mflag && fromatty) {				ointer = interactive;				interactive = 1;				if (confirm("Continue with", "mdelete")) {					mflag++;				}				interactive = ointer;			}		}	}	(void) signal(SIGINT, oldintr);	mflag = 0;}/* * Rename a remote file. */voidrenamefile(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "from-name"))		goto usage;	if (argc < 3 && !another(&argc, &argv, "to-name")) {usage:		printf("%s from-name to-name\n", argv[0]);		code = -1;		return;	}	if (command("RNFR %s", argv[1]) == CONTINUE)		(void) command("RNTO %s", argv[2]);}/* * Get a directory listing * of remote files. */voidls(int argc, char *argv[]){	static char foo[2] = "-";	const char *cmd;	if (argc < 2) {		argc++, argv[1] = NULL;	}	if (argc < 3) {		argc++, argv[2] = foo;	}	if (argc > 3) {		printf("usage: %s remote-directory local-file\n", argv[0]);		code = -1;		return;	}	cmd = argv[0][0] == 'n' ? "NLST" : "LIST";	if (strcmp(argv[2], "-") && (argv[2] = globulize(argv[2]))==NULL) {		code = -1;		return;	}	if (strcmp(argv[2], "-") && *argv[2] != '|')		if ((argv[2] = globulize(argv[2]))==NULL || 		    !confirm("output to local-file:", argv[2])) {			code = -1;			return;	}	recvrequest(cmd, argv[2], argv[1], "w", 0);}/* * Get a directory listing * of multiple remote files. */voidmls(int argc, char *argv[]){	void (*oldintr)(int);	int ointer, i;	const char *volatile cmd;	char *volatile dest;	const char *modestr;	if (argc < 2 && !another(&argc, &argv, "remote-files"))		goto usage;	if (argc < 3 && !another(&argc, &argv, "local-file")) {usage:		printf("usage: %s remote-files local-file\n", argv[0]);		code = -1;		return;	}	dest = argv[argc - 1];	argv[argc - 1] = NULL;	if (strcmp(dest, "-") && *dest != '|')		if ((dest = globulize(dest))==NULL ||		    !confirm("output to local-file:", dest)) {			code = -1;			return;	}	cmd = argv[0][1] == 'l' ? "NLST" : "LIST";	mname = argv[0];	mflag = 1;	oldintr = signal(SIGINT, mabort);	/*	 * This just plain seems wrong.	 */	(void) sigsetjmp(jabort, 1);	for (i = 1; mflag && i < argc-1; ++i) {		modestr = (i == 1) ? "w" : "a";		recvrequest(cmd, dest, argv[i], modestr, 0);		if (!mflag && fromatty) {			ointer = interactive;			interactive = 1;			if (confirm("Continue with", argv[0])) {				mflag ++;			}			interactive = ointer;		}	}	(void) signal(SIGINT, oldintr);	mflag = 0;}/* * Do a shell escape */voidshell(const char *arg){	int pid;	void (*old1)(int);	void (*old2)(int);	char shellnam[40];	const char *theshell, *namep; 	old1 = signal (SIGINT, SIG_IGN);	old2 = signal (SIGQUIT, SIG_IGN);	if ((pid = fork()) == 0) {		for (pid = 3; pid < 20; pid++)			(void) close(pid);		(void) signal(SIGINT, SIG_DFL);		(void) signal(SIGQUIT, SIG_DFL);		theshell = getenv("SHELL");		if (theshell == NULL)			theshell = _PATH_BSHELL;		namep = strrchr(theshell, '/');		if (namep == NULL)			namep = theshell;		(void) strcpy(shellnam,"-");		(void) strcat(shellnam, ++namep);		if (strcmp(namep, "sh") != 0)			shellnam[0] = '+';		if (debug) {			printf("%s\n", theshell);			(void) fflush (stdout);		}		if (arg) {			execl(theshell, shellnam, "-c", arg, NULL);		}		else {			execl(theshell, shellnam, NULL);		}		perror(theshell);		code = -1;		exit(1);		}	if (pid > 0) while (wait(NULL) != pid);	(void) signal(SIGINT, old1);	(void) signal(SIGQUIT, old2);	if (pid == -1) {		perror("Try again later");		code = -1;	}	else {		code = 0;	}}/* * Send new user information (re-login) */voiduser(int argc, char *argv[]){	char theacct[80];	int n, aflag = 0;	if (argc < 2)		(void) another(&argc, &argv, "username");	if (argc < 2 || argc > 4) {		printf("usage: %s username [password] [account]\n", argv[0]);		code = -1;		return;	}	n = command("USER %s", argv[1]);	if (n == CONTINUE) {		if (argc < 3 )			argv[2] = getpass("Password: "), argc++;		n = command("PASS %s", argv[2]);	}	if (n == CONTINUE) {		if (argc < 4) {			printf("Account: "); (void) fflush(stdout);			fgets(theacct, sizeof(theacct), stdin);			argv[3] = theacct; argc++;		}		n = command("ACCT %s", argv[3]);		aflag++;	}	if (n != COMPLETE) {		fprintf(stdout, "Login failed.\n");		return;	}	if (!aflag && argc == 4) {		(void) command("ACCT %s", argv[3]);	}}/* * Print working directory. */voidpwd(void){	int oldverbose = verbose;	/*	 * If we aren't verbose, this doesn't do anything!	 */	verbose = 1;	if (command("PWD") == ERROR && code == 500) {		printf("PWD command not recognized, trying XPWD\n");		(void) command("XPWD");	}	verbose = oldverbose;}/* * Make a directory. */voidmakedir(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "directory-name")) {		printf("usage: %s directory-name\n", argv[0]);		code = -1;		return;	}	if (command("MKD %s", argv[1]) == ERROR && code == 500) {		if (verbose)			printf("MKD command not recognized, trying XMKD\n");		(void) command("XMKD %s", argv[1]);	}}/* * Remove a directory. */voidremovedir(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "directory-name")) {		printf("usage: %s directory-name\n", argv[0]);		code = -1;		return;	}	if (command("RMD %s", argv[1]) == ERROR && code == 500) {		if (verbose)			printf("RMD command not recognized, trying XRMD\n");		(void) command("XRMD %s", argv[1]);	}}/* * Send a line, verbatim, to the remote machine. */voidquote(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "command line to send")) {		printf("usage: %s line-to-send\n", argv[0]);		code = -1;		return;	}	quote1("", argc, argv);}/* * Send a SITE command to the remote machine.  The line * is sent verbatim to the remote machine, except that the * word "SITE" is added at the front. */voidsite(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {		printf("usage: %s line-to-send\n", argv[0]);		code = -1;		return;	}	quote1("SITE ", argc, argv);}/* * Turn argv[1..argc) into a space-separated string, then prepend initial text.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -