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

📄 commands.c

📁 linux下c语言编写的telnet客户端程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*	$OpenBSD: commands.c,v 1.34 2000/11/08 21:49:44 aaron Exp $	*//*	$NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $	*//* * Copyright (c) 1988, 1990, 1993 *	The 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. */#include "telnet_locl.h"#include <err.h>#if	defined(IPPROTO_IP) && defined(IP_TOS)int tos = -1;#endif	/* defined(IPPROTO_IP) && defined(IP_TOS) */char	*hostname;static char _hostname[MAXHOSTNAMELEN];typedef int (*intrtn_t) __P((int, char**));static int call __P((intrtn_t, ...));typedef	int (*cmd_handler_t) P((int, char **));typedef struct {	char	*name;		/* command name */	char	*help;		/* help string (NULL for no help) */	cmd_handler_t	handler;	/* routine which executes command */	int	needconnect;	/* Do we need to be connected to execute? */} Command;static char line[256];static char saveline[256];static int margc;static char *margv[20];#if	defined(SKEY)#include <sys/wait.h>#define PATH_SKEY	"/usr/bin/skey"    intskey_calc(int argc, char *argv[]){	int status;	if(argc != 3) {		printf("usage: %s sequence challenge\n", argv[0]);		return 0;	}	switch(fork()) {	case 0:		execv(PATH_SKEY, argv);		exit (1);	case -1:		err(1, "fork");		break;	default:		(void) wait(&status);		if (WIFEXITED(status))			return (WEXITSTATUS(status));		return (0);	}}#endif   	    static voidmakeargv(void){    char *cp, *cp2, c;    char **argp = margv;    margc = 0;    cp = line;    if (*cp == '!') {		/* Special case shell escape */	strcpy(saveline, line);	/* save for shell command */	*argp++ = "!";		/* No room in string to get this */	margc++;	cp++;    }    while ((c = *cp)) {	int inquote = 0;	while (isspace(c))	    c = *++cp;	if (c == '\0')	    break;	*argp++ = cp;	margc += 1;	for (cp2 = cp; c != '\0'; c = *++cp) {	    if (inquote) {		if (c == inquote) {		    inquote = 0;		    continue;		}	    } else {		if (c == '\\') {		    if ((c = *++cp) == '\0')			break;		} else if (c == '"') {		    inquote = '"';		    continue;		} else if (c == '\'') {		    inquote = '\'';		    continue;		} else if (isspace(c))		    break;	    }	    *cp2++ = c;	}	*cp2 = '\0';	if (c == '\0')	    break;	cp++;    }    *argp++ = 0;}/* * Make a character string into a number. * * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1). */	static charspecial(char *s){	char c;	char b;	switch (*s) {	case '^':		b = *++s;		if (b == '?') {		    c = b | 0x40;		/* DEL */		} else {		    c = b & 0x1f;		}		break;	default:		c = *s;		break;	}	return c;}/* * Construct a control character sequence * for a special character. */	static char *control(cc_t c){	static char buf[5];	/*	 * The only way I could get the Sun 3.5 compiler	 * to shut up about	 *	if ((unsigned int)c >= 0x80)	 * was to assign "c" to an unsigned int variable...	 * Arggg....	 */	unsigned int uic = (unsigned int)c;	if (uic == 0x7f)		return ("^?");	if (c == (cc_t)_POSIX_VDISABLE) {		return "off";	}	if (uic >= 0x80) {		buf[0] = '\\';		buf[1] = ((c>>6)&07) + '0';		buf[2] = ((c>>3)&07) + '0';		buf[3] = (c&07) + '0';		buf[4] = 0;	} else if (uic >= 0x20) {		buf[0] = c;		buf[1] = 0;	} else {		buf[0] = '^';		buf[1] = '@'+c;		buf[2] = 0;	}	return (buf);}/* *	The following are data structures and routines for *	the "send" command. * */typedef	int (*send_handler_t) P((const char *));struct sendlist {    const char	*name;		/* How user refers to it (case independent) */    const char	*help;		/* Help information (0 ==> no help) */    int		needconnect;	/* Need to be connected */    int		narg;		/* Number of arguments */    send_handler_t handler;	/* Routine to perform (for special ops) */    int		nbyte;		/* Number of bytes to send this command */    int		what;		/* Character to be sent (<0 ==> special) */};static int	send_esc P((const char *)),	send_help P((const char *)),	send_docmd P((const char *)),	send_dontcmd P((const char *)),	send_willcmd P((const char *)),	send_wontcmd P((const char *));static struct sendlist Sendlist[] = {    { "ao",	"Send Telnet Abort output",		1, 0, 0, 2, AO },    { "ayt",	"Send Telnet 'Are You There'",		1, 0, 0, 2, AYT },    { "brk",	"Send Telnet Break",			1, 0, 0, 2, BREAK },    { "break",	0,					1, 0, 0, 2, BREAK },    { "ec",	"Send Telnet Erase Character",		1, 0, 0, 2, EC },    { "el",	"Send Telnet Erase Line",		1, 0, 0, 2, EL },    { "escape",	"Send current escape character",	1, 0, send_esc, 1, 0 },    { "ga",	"Send Telnet 'Go Ahead' sequence",	1, 0, 0, 2, GA },    { "ip",	"Send Telnet Interrupt Process",	1, 0, 0, 2, IP },    { "intp",	0,					1, 0, 0, 2, IP },    { "interrupt", 0,					1, 0, 0, 2, IP },    { "intr",	0,					1, 0, 0, 2, IP },    { "nop",	"Send Telnet 'No operation'",		1, 0, 0, 2, NOP },    { "eor",	"Send Telnet 'End of Record'",		1, 0, 0, 2, EOR },    { "abort",	"Send Telnet 'Abort Process'",		1, 0, 0, 2, ABORT },    { "susp",	"Send Telnet 'Suspend Process'",	1, 0, 0, 2, SUSP },    { "eof",	"Send Telnet End of File Character",	1, 0, 0, 2, xEOF },    { "synch",	"Perform Telnet 'Synch operation'",	1, 0, (send_handler_t)dosynch, 2, 0 },    { "getstatus", "Send request for STATUS",		1, 0, (send_handler_t)get_status, 6, 0 },    { "?",	"Display send options",			0, 0, send_help, 0, 0 },    { "help",	0,					0, 0, send_help, 0, 0 },    { "do",	0,					0, 1, send_docmd, 3, 0 },    { "dont",	0,					0, 1, send_dontcmd, 3, 0 },    { "will",	0,					0, 1, send_willcmd, 3, 0 },    { "wont",	0,					0, 1, send_wontcmd, 3, 0 },    { NULL, NULL, 0, 0, NULL, 0, 0 }};#define	GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \				sizeof(struct sendlist)))    static intsendcmd(int argc, char *argv[]){    int count;		/* how many bytes we are going to need to send */    int i;    struct sendlist *s;	/* pointer to current command */    int success = 0;    int needconnect = 0;    if (argc < 2) {	printf("need at least one argument for 'send' command\r\n");	printf("'send ?' for help\r\n");	return 0;    }    /*     * First, validate all the send arguments.     * In addition, we see how much space we are going to need, and     * whether or not we will be doing a "SYNCH" operation (which     * flushes the network queue).     */    count = 0;    for (i = 1; i < argc; i++) {	s = GETSEND(argv[i]);	if (s == 0) {	    printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",			argv[i]);	    return 0;	} else if (Ambiguous(s)) {	    printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",			argv[i]);	    return 0;	}	if (i + s->narg >= argc) {	    fprintf(stderr,	    "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\r\n",		s->narg, s->narg == 1 ? "" : "s", s->name, s->name);	    return 0;	}	count += s->nbyte;	if (s->handler == send_help) {	    send_help(NULL);	    return 0;	}	i += s->narg;	needconnect += s->needconnect;

⌨️ 快捷键说明

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