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

📄 commands.c

📁 经典的unix下telnet的c代码
💻 C
📖 第 1 页 / 共 5 页
字号:
	}	/*	 * Special case for DISPLAY variable.  If it is ":0.0" or	 * "unix:0.0", we have to get rid of "unix" and insert our	 * hostname.	 */	if ((ep = env_find("DISPLAY"))	    && ((*ep->value == ':')		|| (strncmp((char *)ep->value, "unix:", 5) == 0))) {		char hbuf[MAXHOSTNAMELEN];		char *cp2 = strchr((char *)ep->value, ':');		gethostname(hbuf, sizeof hbuf);		/* If this is not the full name, try to get it via DNS */		if (strchr(hbuf, '.') == 0) {			struct hostent *he = gethostbyname(hbuf);			if (he != 0)				strncpy(hbuf, he->h_name, sizeof hbuf-1);			hbuf[sizeof hbuf-1] = '\0';		}		if (asprintf (&cp, "%s%s", hbuf, cp2) == -1)			err(1, "asprintf");		free(ep->value);		ep->value = (unsigned char *)cp;	}	/*	 * If USER is not defined, but LOGNAME is, then add	 * USER with the value from LOGNAME.  By default, we	 * don't export the USER variable.	 */	if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {		env_define((unsigned char *)"USER", ep->value);		env_unexport((unsigned char *)"USER");	}	env_export((unsigned char *)"DISPLAY");	env_export((unsigned char *)"PRINTER");	env_export((unsigned char *)"XAUTHORITY");}	struct env_lst *env_define(var, value)	unsigned char *var, *value;{	struct env_lst *ep;	if ((ep = env_find(var))) {		if (ep->var)			free(ep->var);		if (ep->value)			free(ep->value);	} else {		if ((ep = malloc(sizeof(struct env_lst))) == NULL)			err(1, "malloc");		ep->next = envlisthead.next;		envlisthead.next = ep;		ep->prev = &envlisthead;		if (ep->next)			ep->next->prev = ep;	}	ep->welldefined = opt_welldefined((char *)var);	ep->export = 1;	if ((ep->var = strdup((char *)var)) == NULL)		err(1, "strdup");	if ((ep->value = strdup((char *)value)) == NULL)		err(1, "strdup");	return(ep);}	voidenv_undefine(var)	unsigned char *var;{	struct env_lst *ep;	if ((ep = env_find(var))) {		ep->prev->next = ep->next;		if (ep->next)			ep->next->prev = ep->prev;		if (ep->var)			free(ep->var);		if (ep->value)			free(ep->value);		free(ep);	}}	voidenv_export(var)	unsigned char *var;{	struct env_lst *ep;	if ((ep = env_find(var)))		ep->export = 1;}	voidenv_unexport(var)	unsigned char *var;{	struct env_lst *ep;	if ((ep = env_find(var)) != NULL)		ep->export = 0;}	voidenv_send(var)	unsigned char *var;{	struct env_lst *ep;	if (my_state_is_wont(TELOPT_NEW_ENVIRON)#ifdef	OLD_ENVIRON	    && my_state_is_wont(TELOPT_OLD_ENVIRON)#endif		) {		fprintf(stderr,		    "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",									var);		return;	}	ep = env_find(var);	if (ep == 0) {		fprintf(stderr, "Cannot send '%s': variable not defined\r\n",									var);		return;	}	env_opt_start_info();	env_opt_add(ep->var);	env_opt_end(0);}	voidenv_list(){	struct env_lst *ep;	for (ep = envlisthead.next; ep; ep = ep->next) {		printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',					ep->var, ep->value);	}}	unsigned char *env_default(init, welldefined)	int init;{	static struct env_lst *nep = NULL;	if (init) {		nep = &envlisthead;		return NULL;	}	if (nep) {		while ((nep = nep->next)) {			if (nep->export && (nep->welldefined == welldefined))				return(nep->var);		}	}	return(NULL);}	unsigned char *env_getvalue(var)	unsigned char *var;{	struct env_lst *ep;	if ((ep = env_find(var)))		return(ep->value);	return(NULL);}#if defined(OLD_ENVIRON) && defined(ENV_HACK)	voidenv_varval(what)	unsigned char *what;{	extern int old_env_var, old_env_value, env_auto;	int len = strlen((char *)what);	if (len == 0)		goto unknown;	if (strncasecmp((char *)what, "status", len) == 0) {		if (env_auto)			printf("%s%s", "VAR and VALUE are/will be ",					"determined automatically\r\n");		if (old_env_var == OLD_ENV_VAR)			printf("VAR and VALUE set to correct definitions\r\n");		else			printf("VAR and VALUE definitions are reversed\r\n");	} else if (strncasecmp((char *)what, "auto", len) == 0) {		env_auto = 1;		old_env_var = OLD_ENV_VALUE;		old_env_value = OLD_ENV_VAR;	} else if (strncasecmp((char *)what, "right", len) == 0) {		env_auto = 0;		old_env_var = OLD_ENV_VAR;		old_env_value = OLD_ENV_VALUE;	} else if (strncasecmp((char *)what, "wrong", len) == 0) {		env_auto = 0;		old_env_var = OLD_ENV_VALUE;		old_env_value = OLD_ENV_VAR;	} else {unknown:		printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\r\n");	}}#endif#if	defined(AUTHENTICATION)/* * The AUTHENTICATE command. */struct authlist {	char	*name;	char	*help;	int	(*handler)();	int	narg;};static int	auth_help(void);struct authlist AuthList[] = {    { "status",	"Display current status of authentication information",						auth_status,	0 },    { "disable", "Disable an authentication type ('auth disable ?' for more)",						auth_disable,	1 },    { "enable", "Enable an authentication type ('auth enable ?' for more)",						auth_enable,	1 },    { "help",	0,				auth_help,		0 },    { "?",	"Print help information",	auth_help,		0 },    { 0 },};    static intauth_help(){    struct authlist *c;    for (c = AuthList; c->name; c++) {	if (c->help) {	    if (*c->help)		printf("%-15s %s\r\n", c->name, c->help);	    else		printf("\r\n");	}    }    return 0;}    intauth_cmd(argc, argv)    int  argc;    char *argv[];{    struct authlist *c;    if (argc < 2) {	fprintf(stderr,	    "Need an argument to 'auth' command.  'auth ?' for help.\r\n");	return 0;    }    c = (struct authlist *)		genget(argv[1], (char **) AuthList, sizeof(struct authlist));    if (c == 0) {	fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",    				argv[1]);	return 0;    }    if (Ambiguous(c)) {	fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",    				argv[1]);	return 0;    }    if (c->narg + 2 != argc) {	fprintf(stderr,	    "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\r\n",		c->narg < argc + 2 ? "only " : "",		c->narg, c->narg == 1 ? "" : "s", c->name);	return 0;    }    return((*c->handler)(argv[2], argv[3]));}#endif#if    defined(ENCRYPTION)/* * The ENCRYPT command. */struct encryptlist {       char    *name;       char    *help;       int     (*handler)();       int     needconnect;       int     minarg;       int     maxarg;}; static int       EncryptHelp (void);struct encryptlist EncryptList[] = {    { "enable", "Enable encryption. ('encrypt enable ?' for more)",                                               EncryptEnable, 1, 1, 2 },    { "disable", "Disable encryption. ('encrypt enable ?' for more)",                                               EncryptDisable, 0, 1, 2 },    { "type", "Set encryption type. ('encrypt type ?' for more)",                                               EncryptType, 0, 1, 1 },    { "start", "Start encryption. ('encrypt start ?' for more)",                                               EncryptStart, 1, 0, 1 },    { "stop", "Stop encryption. ('encrypt stop ?' for more)",                                               EncryptStop, 1, 0, 1 },    { "input", "Start encrypting the input stream",                                               EncryptStartInput, 1, 0, 0 },    { "-input", "Stop encrypting the input stream",                                               EncryptStopInput, 1, 0, 0 },    { "output", "Start encrypting the output stream",                                               EncryptStartOutput, 1, 0, 0 },    { "-output", "Stop encrypting the output stream",                                               EncryptStopOutput, 1, 0, 0 },    { "status",        "Display current status of authentication information",                                               EncryptStatus,  0, 0, 0 },    { "help",  0,                              EncryptHelp,    0, 0, 0 },    { "?",     "Print help information",       EncryptHelp,    0, 0, 0 },    { 0 },};static intEncryptHelp(){    struct encryptlist *c;    for (c = EncryptList; c->name; c++) {       if (c->help) {           if (*c->help)               printf("%-15s %s\r\n", c->name, c->help);           else               printf("\r\n");       }    }    return 0;}static intencrypt_cmd(int argc, char **argv){    struct encryptlist *c;    if (argc < 2) {	fprintf(stderr, "Need at least one argument for 'encrypt' command.\n");	fprintf(stderr, "('encrypt ?' for help)\n");	return 0;    }    c = (struct encryptlist *)               genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));    if (c == 0) {        fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",                               argv[1]);        return 0;    }    if (Ambiguous(c)) {        fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",                               argv[1]);        return 0;    }    argc -= 2;    if (argc < c->minarg || argc > c->maxarg) {       if (c->minarg == c->maxarg) {           fprintf(stderr, "Need %s%d argument%s ",               c->minarg < argc ? "only " : "", c->minarg,               c->minarg == 1 ? "" : "s");       } else {           fprintf(stderr, "Need %s%d-%d arguments ",               c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);       }       fprintf(stderr, "to 'encrypt %s' command.  'encrypt ?' for help.\r\n",               c->name);       return 0;    }    if (c->needconnect && !connected) {       if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {           printf("?Need to be connected first.\r\n");           return 0;       }    }    return ((*c->handler)(argc > 0 ? argv[2] : 0,                       argc > 1 ? argv[3] : 0,                       argc > 2 ? argv[4] : 0));}#endif#if	defined(unix) && defined(TN3270)    static voidfilestuff(fd)    int fd;{    int res;#ifdef	F_GETOWN    setconnmode(0);    res = fcntl(fd, F_GETOWN, 0);    setcommandmode();    if (res == -1) {	perror("fcntl");	return;    }    printf("\tOwner is %d.\r\n", res);#endif    setconnmode(0);    res = fcntl(fd, F_GETFL, 0);    setcommandmode();    if (res == -1) {	perror("fcntl");	return;    }#ifdef notdef    printf("\tFlags are 0x%x: %s\r\n", res, decodeflags(res));#endif}#endif /* defined(unix) && defined(TN3270) *//* * Print status about the connection. */    /*ARGSUSED*/    static intstatus(argc, argv)    int	 argc;    char *argv[];{    if (connected) {	printf("Connected to %s.\r\n", hostname);	if ((argc < 2) || strcmp(argv[1], "notmuch")) {	    int mode = getconnmode();	    if (my_want_state_is_will(TELOPT_LINEMODE)) {		printf("Operating with LINEMODE option\r\n");		printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");		printf("%s catching of signals\r\n",					(mode&MODE_TRAPSIG) ? "Local" : "No");		slcstate();#ifdef	KLUDGELINEMODE	    } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {		printf("Operating in obsolete linemode\r\n");#endif	    } else {		printf("Operating in single character mode\r\n");		if (localchars)		    printf("Catching signals locally\r\n");	    }	    printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");	    if (my_want_state_is_will(TELOPT_LFLOW))		printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");#if    defined(ENCRYPTION)           encrypt_display();#endif	}    } else {	printf("No connection.\r\n");    }#   if !defined(TN3270)    printf("Escape character is '%s'.\r\n", control(escape));    (void) fflush(stdout);#   else /* !defined(TN3270) */    if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {	printf("Escape character is '%s'.\r\n", control(escape));    }#   if defined(unix)    if ((argc >= 2) && !strcmp(argv[1], "everything")) {	printf("SIGIO received %d time%s.\r\n",				sigiocount, (sigiocount == 1)? "":"s");	if (In3270) {	    printf("Process ID %ld, process group %ld.\r\n",					    (long)getpid(), (long)getpgrp());	    printf("Terminal input:\r\n");	    filestuff(tin);	    printf("Terminal output:\r\n");	    filestuff(tout);	    printf("Network socket:\r\n");	    filestuff(net);	}    }    if (In3270 && transcom) {       printf("Transparent mode command is '%s'.\r\n", transcom);    }#   endif /* defined(unix) */    (void) fflush(stdout);    if (In3270) {	return 0;    }#   endif /* defined(TN3270) */    fflush(stdout);    return 1;}#ifdef	SIGINFO/* * Function that gets called when SIGINFO is received. */voidayt_status(){    (void) call(status, "status", "notmuch", 0);}#endifstatic Command *getcmd(char *name);static voidcmdrc(char *m1, char *m2){    static char rcname[128];    Command *c;    FILE *rcfile;    int gotmachine = 0;    int l1 = strlen(m1);    int l2 = strlen(m2);    char m1save[MAXHOSTNAMELEN];    if (skiprc)	return;    strlcpy(m1save, m1, sizeof(m1save));    m1 = m1save;    if (rcname[0] == 0) {	char *home = getenv("HOME");	if (home == NULL || *home == '\0')	    return;	snprintf (rcname, sizeof(rcname), "%s/.telnetrc",		  home ? home : "");    }    if ((rcfile = fopen(rcname, "r")) == 0) {	return;    }

⌨️ 快捷键说明

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