FreeBSD/contrib/ntp/ntpq/ntpq-subs.c
/*
 * ifstats - ntpq -c ifstats modeled on ntpdc -c ifstats.
 */
c ifstats modeled on ntpdc -c ifstats.
c ifstats modeled on ntpdc - c ifstats .
static void 
ifstats(
	struct parse *	pcmd,
	FILE *		fp
	)
{
	const char	addr_fmt[] =	"addr.%u";
	const char	bcast_fmt[] =	"bcast.%u";
	const char	en_fmt[] =	"en.%u";	
	const char	flags_fmt[] =	"flags.%u";
	const char	mc_fmt[] =	"mc.%u";	
	const char	name_fmt[] =	"name.%u";
	const char	pc_fmt[] =	"pc.%u";	
	const char	rx_fmt[] =	"rx.%u";
	const char	tl_fmt[] =	"tl.%u";	
	const char	tx_fmt[] =	"tx.%u";
	const char	txerr_fmt[] =	"txerr.%u";
	const char	up_fmt[] =	"up.%u";	
	const char *	datap;
	int		qres;
	size_t		dsize;
	u_short		rstatus;
	char *		tag;
	char *		val;
	int		fields;
	u_int		ui;
	ifstats_row	row;
	int		comprende;
	size_t		len;
	qres = doquery(CTL_OP_READ_ORDLIST_A, 0, TRUE, 0, NULL, &rstatus,
		       &dsize, &datap);
	if (qres)	
		return;
	fprintf(fp,
		"    interface name                                        send\n"
		" #  address/broadcast     drop flag ttl mc received sent failed peers   uptime\n"
		"==============================================================================\n");
	ZERO(row);
	fields = 0;
	ui = 0;
	while (nextvar(&dsize, &datap, &tag, &val)) {
		if (debug > 1)
			fprintf(stderr, "nextvar gave: %s = %s\n", tag,
				(NULL == val)
				    ? ""
				    : val);
		comprende = FALSE;
		switch(tag[0]) {
		case 'a':
			if (1 == sscanf(tag, addr_fmt, &ui) &&
			    decodenetnum(val, &row.addr))
				comprende = TRUE;
			break;
		case 'b':
			if (1 == sscanf(tag, bcast_fmt, &ui) &&
			    (NULL == val ||
			     decodenetnum(val, &row.bcast)))
				comprende = TRUE;
			break;
		case 'e':
			if (1 == sscanf(tag, en_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.enabled))
				comprende = TRUE;
			break;
		case 'f':
			if (1 == sscanf(tag, flags_fmt, &ui) &&
			    1 == sscanf(val, "0x%x", &row.flags))
				comprende = TRUE;
			break;
		case 'm':
			if (1 == sscanf(tag, mc_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.mcast_count))
				comprende = TRUE;
			break;
		case 'n':
			if (1 == sscanf(tag, name_fmt, &ui)) {
				INSIST(val);
				len = strlen(val);
				if (len >= 2 &&
				    len - 2 < sizeof(row.name)) {
					len -= 2;
					memcpy(row.name, val + 1, len);
					row.name[len] = '\0';
					comprende = TRUE;
				}
			}
			break;
		case 'p':
			if (1 == sscanf(tag, pc_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.peer_count))
				comprende = TRUE;
			break;
		case 'r':
			if (1 == sscanf(tag, rx_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.received))
				comprende = TRUE;
			break;
		case 't':
			if (1 == sscanf(tag, tl_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.ttl))
				comprende = TRUE;
			else if (1 == sscanf(tag, tx_fmt, &ui) &&
				 1 == sscanf(val, "%d", &row.sent))
				comprende = TRUE;
			else if (1 == sscanf(tag, txerr_fmt, &ui) &&
				 1 == sscanf(val, "%d", &row.send_errors))
				comprende = TRUE;
			break;
		case 'u':
			if (1 == sscanf(tag, up_fmt, &ui) &&
			    1 == sscanf(val, "%d", &row.uptime))
				comprende = TRUE;
			break;
		}
		if (comprende) {
			validate_ifnum(fp, ui, &fields, &row);
			another_ifstats_field(&fields, &row, fp);
		}
	}
	if (fields != IFSTATS_FIELDS)
		fprintf(fp, "Warning: incomplete row with %d (of %d) fields",
			fields, IFSTATS_FIELDS);
	fflush(fp);
}
static void ifstats ( struct parse * pcmd , FILE * fp ) { const char addr_fmt [ ] = " " ; const char bcast_fmt [ ] = " " ; const char en_fmt [ ] = " " ; const char flags_fmt [ ] = " " ; const char mc_fmt [ ] = " " ; const char name_fmt [ ] = " " ; const char pc_fmt [ ] = " " ; const char rx_fmt [ ] = " " ; const char tl_fmt [ ] = " " ; const char tx_fmt [ ] = " " ; const char txerr_fmt [ ] = " " ; const char up_fmt [ ] = " " ; const char * datap ; int qres ; size_t dsize ; u_short rstatus ; char * tag ; char * val ; int fields ; u_int ui ; ifstats_row row ; int comprende ; size_t len ; qres = doquery ( CTL_OP_READ_ORDLIST_A , 0 , TRUE , 0 , NULL , & rstatus , & dsize , & datap ) ; if ( qres ) return ; fprintf ( fp , " \n " " \n " " \n " ) ; ZERO ( row ) ; fields = 0 ; ui = 0 ; while ( nextvar ( & dsize , & datap , & tag , & val ) ) { if ( debug > 1 ) fprintf ( stderr , " \n " , tag , ( NULL == val ) ? " " : val ) ; comprende = FALSE ; switch ( tag [ 0 ] ) { case ' ' : if ( 1 == sscanf ( tag , addr_fmt , & ui ) && decodenetnum ( val , & row . addr ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , bcast_fmt , & ui ) && ( NULL == val || decodenetnum ( val , & row . bcast ) ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , en_fmt , & ui ) && 1 == sscanf ( val , " " , & row . enabled ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , flags_fmt , & ui ) && 1 == sscanf ( val , " " , & row . flags ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , mc_fmt , & ui ) && 1 == sscanf ( val , " " , & row . mcast_count ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , name_fmt , & ui ) ) { INSIST ( val ) ; len = strlen ( val ) ; if ( len >= 2 && len - 2 < sizeof ( row . name ) ) { len -= 2 ; memcpy ( row . name , val + 1 , len ) ; row . name [ len ] = ' \0 ' ; comprende = TRUE ; } } break ; case ' ' : if ( 1 == sscanf ( tag , pc_fmt , & ui ) && 1 == sscanf ( val , " " , & row . peer_count ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , rx_fmt , & ui ) && 1 == sscanf ( val , " " , & row . received ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , tl_fmt , & ui ) && 1 == sscanf ( val , " " , & row . ttl ) ) comprende = TRUE ; else if ( 1 == sscanf ( tag , tx_fmt , & ui ) && 1 == sscanf ( val , " " , & row . sent ) ) comprende = TRUE ; else if ( 1 == sscanf ( tag , txerr_fmt , & ui ) && 1 == sscanf ( val , " " , & row . send_errors ) ) comprende = TRUE ; break ; case ' ' : if ( 1 == sscanf ( tag , up_fmt , & ui ) && 1 == sscanf ( val , " " , & row . uptime ) ) comprende = TRUE ; break ; } if ( comprende ) { validate_ifnum ( fp , ui , & fields , & row ) ; another_ifstats_field ( & fields , & row , fp ) ; } } if ( fields != IFSTATS_FIELDS ) fprintf ( fp , " " , fields , IFSTATS_FIELDS ) ; fflush ( fp ) ; }
ifstats - ntpq -c ifstats modeled on ntpdc -c ifstats.
ifstats - ntpq - c ifstats modeled on ntpdc - c ifstats .
/* enabled */ /* mcast count */ /* peer count */ /* ttl */ /* uptime */ /* message already displayed */ /* '=' x 78 */ /* strip quotes */ /* error out if rows out of order */ /* if the row is complete, print it */