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

📄 ntpq.c

📁 网络时间协议NTP 源码 版本v4.2.0b 该源码用于linux平台下
💻 C
📖 第 1 页 / 共 5 页
字号:
	    return hextolfp(str+2, lfp);	/*	 * If it starts with a '"', try it as an RT-11 date.	 */	if (*str == '"') {		register char *cp = str+1;		register char *bp;		char buf[30];		bp = buf;		while (*cp != '"' && *cp != '\0' && bp < &buf[29])		    *bp++ = *cp++;		*bp = '\0';		return rtdatetolfp(buf, lfp);	}	/*	 * Might still be hex.  Check out the first character.  Talk	 * about heuristics!	 */	if ((*str >= 'A' && *str <= 'F') || (*str >= 'a' && *str <= 'f'))	    return hextolfp(str, lfp);	/*	 * Try it as a decimal.  If this fails, try as an unquoted	 * RT-11 date.  This code should go away eventually.	 */	if (atolfp(str, lfp))	    return 1;	return rtdatetolfp(str, lfp);}/* * decodetime - decode a time value.  It should be in milliseconds */intdecodetime(	char *str,	l_fp *lfp	){	return mstolfp(str, lfp);}/* * decodeint - decode an integer */intdecodeint(	char *str,	long *val	){	if (*str == '0') {		if (*(str+1) == 'x' || *(str+1) == 'X')		    return hextoint(str+2, (u_long *)&val);		return octtoint(str, (u_long *)&val);	}	return atoint(str, val);}/* * decodeuint - decode an unsigned integer */intdecodeuint(	char *str,	u_long *val	){	if (*str == '0') {		if (*(str + 1) == 'x' || *(str + 1) == 'X')			return (hextoint(str + 2, val));		return (octtoint(str, val));	}	return (atouint(str, val));}/* * decodearr - decode an array of time values */static intdecodearr(	char *str,	int *narr,	l_fp *lfparr	){	register char *cp, *bp;	register l_fp *lfp;	char buf[60];	lfp = lfparr;	cp = str;	*narr = 0;	while (*narr < 8) {		while (isspace((int)*cp))		    cp++;		if (*cp == '\0')		    break;		bp = buf;		while (!isspace((int)*cp) && *cp != '\0')		    *bp++ = *cp++;		*bp++ = '\0';		if (!decodetime(buf, lfp))		    return 0;		(*narr)++;		lfp++;	}	return 1;}/* * Finally, the built in command handlers *//* * help - tell about commands, or details of a particular command */static voidhelp(	struct parse *pcmd,	FILE *fp	){	struct xcmd *xcp;	char *cmd;	const char *list[100];        int word, words;        int row, rows;        int col, cols;	if (pcmd->nargs == 0) {		words = 0;		for (xcp = builtins; xcp->keyword != 0; xcp++) {			if (*(xcp->keyword) != '?')			    list[words++] = xcp->keyword;		}		for (xcp = opcmds; xcp->keyword != 0; xcp++)		    list[words++] = xcp->keyword;		qsort(#ifdef QSORT_USES_VOID_P		    (void *)#else		    (char *)#endif			(list), (size_t)(words), sizeof(char *), helpsort);		col = 0;		for (word = 0; word < words; word++) {		 	int length = strlen(list[word]);			if (col < length) {			    col = length;                        }		}		cols = SCREENWIDTH / ++col;                rows = (words + cols - 1) / cols;		(void) fprintf(fp, "ntpq commands:\n");		for (row = 0; row < rows; row++) {                        for (word = row; word < words; word += rows) {			        (void) fprintf(fp, "%-*.*s", col, col-1, list[word]);                        }                        (void) fprintf(fp, "\n");                }	} else {		cmd = pcmd->argval[0].string;		words = findcmd(cmd, builtins, opcmds, &xcp);		if (words == 0) {			(void) fprintf(stderr,				       "Command `%s' is unknown\n", cmd);			return;		} else if (words >= 2) {			(void) fprintf(stderr,				       "Command `%s' is ambiguous\n", cmd);			return;		}		(void) fprintf(fp, "function: %s\n", xcp->comment);		printusage(xcp, fp);	}}/* * helpsort - do hostname qsort comparisons */#ifdef QSORT_USES_VOID_Pstatic inthelpsort(	const void *t1,	const void *t2	){	char const * const * name1 = (char const * const *)t1;	char const * const * name2 = (char const * const *)t2;	return strcmp(*name1, *name2);}#elsestatic inthelpsort(	char **name1,	char **name2	){	return strcmp(*name1, *name2);}#endif/* * printusage - print usage information for a command */static voidprintusage(	struct xcmd *xcp,	FILE *fp	){	register int i;	(void) fprintf(fp, "usage: %s", xcp->keyword);	for (i = 0; i < MAXARGS && xcp->arg[i] != NO; i++) {		if (xcp->arg[i] & OPT)		    (void) fprintf(fp, " [ %s ]", xcp->desc[i]);		else		    (void) fprintf(fp, " %s", xcp->desc[i]);	}	(void) fprintf(fp, "\n");}/* * timeout - set time out time */static voidtimeout(	struct parse *pcmd,	FILE *fp	){	int val;	if (pcmd->nargs == 0) {		val = tvout.tv_sec * 1000 + tvout.tv_usec / 1000;		(void) fprintf(fp, "primary timeout %d ms\n", val);	} else {		tvout.tv_sec = pcmd->argval[0].uval / 1000;		tvout.tv_usec = (pcmd->argval[0].uval - (tvout.tv_sec * 1000))			* 1000;	}}/* * auth_delay - set delay for auth requests */static voidauth_delay(	struct parse *pcmd,	FILE *fp	){	int isneg;	u_long val;	if (pcmd->nargs == 0) {		val = delay_time.l_ui * 1000 + delay_time.l_uf / 4294967;		(void) fprintf(fp, "delay %lu ms\n", val);	} else {		if (pcmd->argval[0].ival < 0) {			isneg = 1;			val = (u_long)(-pcmd->argval[0].ival);		} else {			isneg = 0;			val = (u_long)pcmd->argval[0].ival;		}		delay_time.l_ui = val / 1000;		val %= 1000;		delay_time.l_uf = val * 4294967;	/* 2**32/1000 */		if (isneg)		    L_NEG(&delay_time);	}}/* * host - set the host we are dealing with. */static voidhost(	struct parse *pcmd,	FILE *fp	){	int i;	if (pcmd->nargs == 0) {		if (havehost)		    (void) fprintf(fp, "current host is %s\n", currenthost);		else		    (void) fprintf(fp, "no current host\n");		return;	}	i = 0;	ai_fam_templ = ai_fam_default;	if (pcmd->nargs == 2) {		if (!strcmp("-4", pcmd->argval[i].string))			ai_fam_templ = AF_INET;		else if (!strcmp("-6", pcmd->argval[i].string))			ai_fam_templ = AF_INET6;		else {			if (havehost)				(void) fprintf(fp,				    "current host remains %s\n", currenthost);			else				(void) fprintf(fp, "still no current host\n");			return;		}		i = 1;	}	if (openhost(pcmd->argval[i].string)) {		(void) fprintf(fp, "current host set to %s\n", currenthost);		numassoc = 0;	} else {		if (havehost)		    (void) fprintf(fp,				   "current host remains %s\n", currenthost);		else		    (void) fprintf(fp, "still no current host\n");	}}/* * poll - do one (or more) polls of the host via NTP *//*ARGSUSED*/static voidntp_poll(	struct parse *pcmd,	FILE *fp	){	(void) fprintf(fp, "poll not implemented yet\n");}/* * keyid - get a keyid to use for authenticating requests */static voidkeyid(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0) {		if (info_auth_keyid == 0)		    (void) fprintf(fp, "no keyid defined\n");		else		    (void) fprintf(fp, "keyid is %lu\n", (u_long)info_auth_keyid);	} else {		/* allow zero so that keyid can be cleared. */		if(pcmd->argval[0].uval > NTP_MAXKEY)		    (void) fprintf(fp, "Invalid key identifier\n");		info_auth_keyid = pcmd->argval[0].uval;	}}/* * keytype - get type of key to use for authenticating requests */static voidkeytype(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0)	    fprintf(fp, "keytype is %s\n",		    (info_auth_keytype == KEY_TYPE_MD5) ? "MD5" : "???");	else	    switch (*(pcmd->argval[0].string)) {		case 'm':		case 'M':		    info_auth_keytype = KEY_TYPE_MD5;		    break;		default:		    fprintf(fp, "keytype must be 'md5'\n");	    }}/* * passwd - get an authentication key *//*ARGSUSED*/static voidpasswd(	struct parse *pcmd,	FILE *fp	){	char *pass;	if (info_auth_keyid == 0) {		int u_keyid = getkeyid("Keyid: ");		if (u_keyid == 0 || u_keyid > NTP_MAXKEY) {			(void)fprintf(fp, "Invalid key identifier\n");			return;		}		info_auth_keyid = u_keyid;	}	pass = getpass("MD5 Password: ");	if (*pass == '\0')	    (void) fprintf(fp, "Password unchanged\n");	else {	    authusekey(info_auth_keyid, info_auth_keytype, (u_char *)pass);	    authtrust(info_auth_keyid, 1);	}}/* * hostnames - set the showhostnames flag */static voidhostnames(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0) {		if (showhostnames)		    (void) fprintf(fp, "hostnames being shown\n");		else		    (void) fprintf(fp, "hostnames not being shown\n");	} else {		if (STREQ(pcmd->argval[0].string, "yes"))		    showhostnames = 1;		else if (STREQ(pcmd->argval[0].string, "no"))		    showhostnames = 0;		else		    (void)fprintf(stderr, "What?\n");	}}/* * setdebug - set/change debugging level */static voidsetdebug(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0) {		(void) fprintf(fp, "debug level is %d\n", debug);		return;	} else if (STREQ(pcmd->argval[0].string, "no")) {		debug = 0;	} else if (STREQ(pcmd->argval[0].string, "more")) {		debug++;	} else if (STREQ(pcmd->argval[0].string, "less")) {		debug--;	} else {		(void) fprintf(fp, "What?\n");		return;	}	(void) fprintf(fp, "debug level set to %d\n", debug);}/* * quit - stop this nonsense *//*ARGSUSED*/static voidquit(	struct parse *pcmd,	FILE *fp	){	if (havehost)	    closesocket(sockfd);	/* cleanliness next to godliness */	exit(0);}/* * version - print the current version number *//*ARGSUSED*/static voidversion(	struct parse *pcmd,	FILE *fp	){	(void) fprintf(fp, "%s\n", Version);	return;}/* * raw - set raw mode output *//*ARGSUSED*/static voidraw(	struct parse *pcmd,	FILE *fp	){	rawmode = 1;	(void) fprintf(fp, "Output set to raw\n");}/* * cooked - set cooked mode output *//*ARGSUSED*/static voidcooked(	struct parse *pcmd,	FILE *fp	){	rawmode = 0;	(void) fprintf(fp, "Output set to cooked\n");	return;}/* * authenticate - always authenticate requests to this host */static voidauthenticate(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0) {		if (always_auth) {			(void) fprintf(fp,				       "authenticated requests being sent\n");		} else		    (void) fprintf(fp,				   "unauthenticated requests being sent\n");	} else {		if (STREQ(pcmd->argval[0].string, "yes")) {			always_auth = 1;		} else if (STREQ(pcmd->argval[0].string, "no")) {			always_auth = 0;		} else		    (void)fprintf(stderr, "What?\n");	}}/* * ntpversion - choose the NTP version to use */static voidntpversion(	struct parse *pcmd,	FILE *fp	){	if (pcmd->nargs == 0) {		(void) fprintf(fp,			       "NTP version being claimed is %d\n", pktversion);	} else {		if (pcmd->argval[0].uval < NTP_OLDVERSION		    || pcmd->argval[0].uval > NTP_VERSION) {			(void) fprintf(stderr, "versions %d to %d, please\n",				       NTP_OLDVERSION, NTP_VERSION);		} else {			pktversion = (u_char) pcmd->argval[0].uval;		}	}}/* * warning - print a warning message */static voidwarning(	const char *fmt,	const char *st1,	const char *st2	){	(void) fprintf(stderr, "%s: ", progname);	(void) fprintf(stderr, fmt, st1, st2);	(void) fprintf(stderr, ": ");	perror("");}

⌨️ 快捷键说明

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