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

📄 cmds.c

📁 linux下ftp client程序的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
 * 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. * Send the result as a one-line command and get response. */voidquote1(initial, argc, argv)	char *initial;	int argc;	char **argv;{	int i, len;	char buf[BUFSIZ];		/* must be >= sizeof(line) */	(void) strcpy(buf, initial);	if (argc > 1) {		len = strlen(buf);		len += strlen(strcpy(&buf[len], argv[1]));		for (i = 2; i < argc; i++) {			buf[len++] = ' ';			len += strlen(strcpy(&buf[len], argv[i]));		}	}	if (command(buf) == PRELIM) {		while (getreply(0) == PRELIM)			continue;	}}voiddo_chmod(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "mode"))		goto usage;	if (argc < 3 && !another(&argc, &argv, "file-name")) {usage:		printf("usage: %s mode file-name\n", argv[0]);		code = -1;		return;	}	(void) command("SITE CHMOD %s %s", argv[1], argv[2]);}voiddo_umask(argc, argv)	int argc;	char *argv[];{	int oldverbose = verbose;	verbose = 1;	(void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);	verbose = oldverbose;}voidsite_idle(argc, argv)	int argc;	char *argv[];{	int oldverbose = verbose;	verbose = 1;	(void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);	verbose = oldverbose;}/* * Ask the other side for help. */voidrmthelp(argc, argv)	int argc;	char *argv[];{	int oldverbose = verbose;	verbose = 1;	(void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);	verbose = oldverbose;}/* * Terminate session and exit. *//*VARARGS*/voidquit(argc, argv)	int argc;	char *argv[];{	if (connected)		disconnect(0, 0);	pswitch(1);	if (connected) {		disconnect(0, 0);	}	exit(0);}/* * Terminate session, but don't exit. */voiddisconnect(argc, argv)	int argc;	char *argv[];{	if (!connected)		return;	(void) command("QUIT");	if (cout) {		(void) fclose(cout);	}	cout = NULL;	connected = 0;	data = -1;	if (!proxy) {		macnum = 0;	}}intconfirm(cmd, file)	char *cmd, *file;{	char line[BUFSIZ];	if (!interactive)		return (1);	printf("%s %s? ", cmd, file);	(void) fflush(stdout);	if (fgets(line, sizeof line, stdin) == NULL)		return (0);	return (*line != 'n' && *line != 'N');}voidfatal(msg)	char *msg;{	error(1, 0, "%s", msg);}/* * 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. */char *globulize(cp)	char *cp;{	glob_t gl;	int flags;	if (!doglob)		return strdup (cp);	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;#ifdef GLOB_QUOTE	flags |= GLOB_QUOTE;#endif	memset(&gl, 0, sizeof(gl));	if (glob(cp, flags, NULL, &gl) ||	    gl.gl_pathc == 0) {		error (0, 0, "%s: not found", cp);		globfree(&gl);		return (0);	}	cp = strdup(gl.gl_pathv[0]);	globfree(&gl);	return cp;}voidaccount(argc,argv)	int argc;	char **argv;{	char acct[50], *ap;	if (argc > 1) {		++argv;		--argc;		(void) strncpy(acct,*argv, sizeof(acct) - 1);		acct[sizeof(acct) - 1] = '\0';		while (argc > 1) {			--argc;			++argv;			(void) strncat(acct,*argv,				(sizeof(acct) - 1) - strlen(acct));		}		ap = acct;	}	else {		ap = getpass("Account:");	}	(void) command("ACCT %s", ap);}jmp_buf abortprox;voidproxabort(sig)  int sig;{	if (!proxy) {		pswitch(1);	}	if (connected) {		proxflag = 1;	}	else {		proxflag = 0;	}	pswitch(0);	longjmp(abortprox,1);}voiddoproxy(argc, argv)	int argc;	char *argv[];{	struct cmd *c;	sig_t oldintr;	if (argc < 2 && !another(&argc, &argv, "command")) {		printf("usage: %s command\n", argv[0]);		code = -1;		return;	}	c = getcmd(argv[1]);	if (c == (struct cmd *) -1) {		printf("?Ambiguous command\n");		(void) fflush(stdout);		code = -1;		return;	}	if (c == 0) {		printf("?Invalid command\n");		(void) fflush(stdout);		code = -1;		return;	}	if (!c->c_proxy) {		printf("?Invalid proxy command\n");		(void) fflush(stdout);		code = -1;		return;	}	if (setjmp(abortprox)) {		code = -1;		return;	}	oldintr = signal(SIGINT, proxabort);	pswitch(1);	if (c->c_conn && !connected) {		printf("Not connected\n");		(void) fflush(stdout);		pswitch(0);		(void) signal(SIGINT, oldintr);		code = -1;		return;	}	(*c->c_handler)(argc-1, argv+1);	if (connected) {		proxflag = 1;	}	else {		proxflag = 0;	}	pswitch(0);	(void) signal(SIGINT, oldintr);}voidsetcase(argc, argv)	int argc;	char *argv[];{	mcase = !mcase;	printf("Case mapping %s.\n", onoff(mcase));	code = mcase;}voidsetcr(argc, argv)	int argc;	char *argv[];{	crflag = !crflag;	printf("Carriage Return stripping %s.\n", onoff(crflag));	code = crflag;}voidsetntrans(argc,argv)	int argc;	char *argv[];{	if (argc == 1) {		ntflag = 0;		printf("Ntrans off.\n");		code = ntflag;		return;	}	ntflag++;	code = ntflag;	(void) strncpy(ntin, argv[1], sizeof(ntin) - 1);	ntin[sizeof(ntin) - 1] = '\0';	if (argc == 2) {		ntout[0] = '\0';		return;	}	(void) strncpy(ntout, argv[2], sizeof(ntout) - 1);	ntout[sizeof(ntout) - 1] = '\0';}char *dotrans(name)	char *name;{	char *new = malloc (strlen (name) + 1);	char *cp1, *cp2 = new;	int i, ostop, found;	for (ostop = 0; *(ntout + ostop) && ostop < sizeof(ntout) - 1; ostop++)		continue;	for (cp1 = name; *cp1; cp1++) {		found = 0;		for (i = 0; *(ntin + i) && i < sizeof(ntin) - 1; i++) {			if (*cp1 == *(ntin + i)) {				found++;				if (i < ostop) {					*cp2++ = *(ntout + i);				}				break;			}		}		if (!found) {			*cp2++ = *cp1;		}	}	*cp2 = '\0';	return (new);}voidsetpassive(argc, argv)	int argc;	char *argv[];{	passivemode = !passivemode;	printf("Passive mode %s.\n", onoff(passivemode));	code = passivemode;}voidsetnmap(argc, argv)	int argc;	char *argv[];{	char *cp;	if (argc == 1) {		mapflag = 0;		printf("Nmap off.\n");		code = mapflag;		return;	}	if (argc < 3 && !another(&argc, &argv, "mapout")) {		printf("Usage: %s [mapin mapout]\n",argv[0]);		code = -1;		return;	}	mapflag = 1;	code = 1;	cp = strchr(altarg, ' ');	if (proxy) {		while(*++cp == ' ')			continue;		altarg = cp;		cp = strchr(altarg, ' ');	}	*cp = '\0';	if (mapin)		free (mapin);	mapin = strdup (altarg);	while (*++cp == ' ')		continue;	if (mapout)		free (mapout);	mapout = strdup (cp);}static intcp_subst (from_p, to_p, toks, tp, te, tok0, buf_p, buf_len_p)char **from_p, **to_p;char *tp[9], *te[9];int toks[9];char *tok0;char **buf_p;int *buf_len_p;{	int toknum;	char *src;	int src_len;	if (*++(*from_p) == '0') {		src = tok0;		src_len = strlen (tok0);	}	else if (toks[toknum = **from_p - '1']) {		src = tp[toknum];		src_len = te[toknum] - src;	}	else		return 0;	if (src_len > 2) {		/* This subst will be longer than the original, so make room		   for it.  */		*buf_len_p += src_len - 2;		*buf_p = realloc (*buf_p, *buf_len_p);	}	while (src_len--)		*(*to_p)++ = *src++;	return 1;}char *domap(name)	char *name;{	int buf_len = strlen (name) + 1;	char *buf = malloc (buf_len);	char *cp1 = name, *cp2 = mapin;	char *tp[9], *te[9];	int i, toks[9], toknum = 0, match = 1;	for (i=0; i < 9; ++i) {		toks[i] = 0;	}	while (match && *cp1 && *cp2) {		switch (*cp2) {			case '\\':				if (*++cp2 != *cp1) {					match = 0;				}				break;			case '$':				if (*(cp2+1) >= '1' && (*cp2+1) <= '9') {					if (*cp1 != *(++cp2+1)) {						toks[toknum = *cp2 - '1']++;						tp[toknum] = cp1;						while (*++cp1 && *(cp2+1)							!= *cp1);						te[toknum] = cp1;					}					cp2++;					break;				}				/* FALLTHROUGH */			default:				if (*cp2 != *cp1) {					match = 0;				}				break;		}		if (match && *cp1) {			cp1++;		}		if (match && *cp2) {			cp2++;		}	}	if (!match && *cp1) /* last token mismatch */	{		toks[toknum] = 0;	}	cp1 = buf;	*cp1 = '\0';	cp2 = mapout;	while (*cp2) {		match = 0;		switch (*cp2) {			case '\\':				if (*(cp2 + 1)) {					*cp1++ = *++cp2;				}				break;			case '[':LOOP:				if (*++cp2 == '$' && isdigit(*(cp2+1)))					cp_subst (&cp2, &cp1,						  toks, tp, te, name,						  &buf, &buf_len);				else {					while (*cp2 && *cp2 != ',' &&					    *cp2 != ']') {						if (*cp2 == '\\') {							cp2++;						}						else if (*cp2 == '$' &&   						        isdigit(*(cp2+1)))							if (cp_subst (&cp2,								      &cp1,								      toks,								      tp, te,								      name,								      &buf,								      &buf_len))								match = 1;						else if (*cp2) {							*cp1++ = *cp2++;						}					}					if (!*cp2) {						printf("nmap: unbalanced brackets\n");						return (name);					}					match = 1;					cp2--;				}				if (match) {					while (*++cp2 && *cp2 != ']') {					      if (*cp2 == '\\' && *(cp2 + 1)) {							cp2++;					      }					}					if (!*cp2) {						printf("nmap: unbalanced brackets\n");						return (name);					}					break;				}				switch (*++cp2) {					case ',':						goto LOOP;					case ']':						break;					default:						cp2--;						goto LOOP;				}				break;			case '$':				if (isdigit(*(cp2 + 1))) {					if (cp_subst (&cp2, &cp1,						      toks, tp, te, name,						      &buf, &buf_len))						match = 1;					break;				}				/* intentional drop through */			default:				*cp1++ = *cp2;				break;		}		cp2++;	}	*cp1 = '\0';	if (! *buf)		strcpy (buf, name);	return buf;}voidsetsunique(argc, argv)	int argc;	char *argv[];{	sunique = !sunique;	printf("Store unique %s.\n", onoff(sunique));	code = sunique;}voidsetrunique(argc, argv)	int argc;	char *argv[];{	runique = !runique;	printf("Receive unique %s.\n", onoff(runique));	code = runique;}/* change directory to parent directory */voidcdup(argc, argv)	int argc;	char *argv[];{	if (command("CDUP") == ERROR && code == 500) {		if (verbose)			printf("CDUP command not recognized, trying XCUP\n");		(void) command("XCUP");	}}/* restart transfer at specific point */voidrestart(argc, argv)	int argc;	char *argv[];{	if (argc != 2)		printf("restart: offset not specified\n");	else {		restart_point = atol(argv[1]);		printf((sizeof(restart_point) > sizeof(long)			? "restarting at %qd. %s\n"			: "restarting at %ld. %s\n"), restart_point,		    "execute get, put or append to initiate transfer");	}}/* show remote system type */voidsyst(argc, argv)	int argc;	char *argv[];{	(void) command("SYST");}voidmacdef(argc, argv)	int argc;	char *argv[];{	char *tmp;	int c;	if (macnum == 16) {		printf("Limit of 16 macros have already been defined\n");		code = -1;		return;	}	if (argc < 2 && !another(&argc, &argv, "macro name")) {		printf("Usage: %s macro_name\n",argv[0]);		code = -1;		return;	}	if (interactive) {		printf("Enter macro line by line, terminating it with a null line\n");	}	(void) strncpy(macros[macnum].mac_name, argv[1], 8);	if (macnum == 0) {		macros[macnum].mac_start = macbuf;	}	else {		macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;	}	tmp = macros[macnum].mac_start;	while (tmp != macbuf+4096) {		if ((c = getchar()) == EOF) {			printf("macdef:end of file encountered\n");			code = -1;			return;		}		if ((*tmp = c) == '\n') {			if (tmp == macros[macnum].mac_start) {				macros[macnum++].mac_end = tmp;				code = 0;				return;			}			if (*(tmp-1) == '\0') {				macros[macnum++].mac_end = tmp - 1;				code = 0;				return;			}			*tmp = '\0';		}		tmp++;	}	while (1) {		while ((c = getchar()) != '\n' && c != EOF)			/* LOOP */;		if (c == EOF || getchar() == '\n') {			printf("Macro not defined - 4k buffer exceeded\n");			code = -1;			return;		}	}}/* * get size of file on remote machine */voidsizecmd(argc, argv)	int argc;	char *argv[];{	if (argc < 2 && !another(&argc, &argv, "filename")) {		printf("usage: %s filename\n", argv[0]);		code = -1;		return;	}	(void) command("SIZE %s", argv[1]);}/* * get last modification time of file on remote machine */voidmodtime(argc, argv)	int argc;	char *argv[];{	int overbose;	if (argc < 2 && !another(&argc, &argv, "filename")) {		printf("usage: %s filename\n", argv[0]);		code = -1;		return;	}	overbose = verbose;	if (debug == 0)		verbose = -1;	if (command("MDTM %s", argv[1]) == COMPLETE) {		int yy, mo, day, hour, min, sec;		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,			&day, &hour, &min, &sec);		/* might want to print this in local time */		printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1],			mo, day, yy, hour, min, sec);	} else		printf("%s\n", reply_string);	verbose = overbose;}/* * show status on remote machine */voidrmtstatus(argc, argv)	int argc;	char *argv[];{	(void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);}/* * get file if modtime is more recent than current file */voidnewer(argc, argv)	int argc;	char *argv[];{	if (getit(argc, argv, -1, "w"))		printf("Local file \"%s\" is newer than remote file \"%s\"\n",			argv[2], argv[1]);}

⌨️ 快捷键说明

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