📄 ntpq.c
字号:
/* * error - print a message and exit */static voiderror( const char *fmt, const char *st1, const char *st2 ){ warning(fmt, st1, st2); exit(1);}/* * getkeyid - prompt the user for a keyid to use */static u_longgetkeyid( const char *keyprompt ){ register char *p; register int c; FILE *fi; char pbuf[20];#ifndef SYS_WINNT if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)#else if ((fi = _fdopen((int)GetStdHandle(STD_INPUT_HANDLE), "r")) == NULL)#endif /* SYS_WINNT */ fi = stdin; else setbuf(fi, (char *)NULL); fprintf(stderr, "%s", keyprompt); fflush(stderr); for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) { if (p < &pbuf[18]) *p++ = (char)c; } *p = '\0'; if (fi != stdin) fclose(fi); if (strcmp(pbuf, "0") == 0) return 0; return (u_long) atoi(pbuf);}/* * atoascii - printable-ize possibly ascii data using the character * transformations cat -v uses. */static voidatoascii( int length, char *data, char *outdata ){ register u_char *cp; register u_char *ocp; register u_char c; if (!data) { *outdata = '\0'; return; } ocp = (u_char *)outdata; for (cp = (u_char *)data; cp < (u_char *)data + length; cp++) { c = *cp; if (c == '\0') break; if (c == '\0') break; if (c > 0177) { *ocp++ = 'M'; *ocp++ = '-'; c &= 0177; } if (c < ' ') { *ocp++ = '^'; *ocp++ = (u_char)(c + '@'); } else if (c == 0177) { *ocp++ = '^'; *ocp++ = '?'; } else { *ocp++ = c; } if (ocp >= ((u_char *)outdata + length - 4)) break; } *ocp++ = '\0';}/* * makeascii - print possibly ascii data using the character * transformations that cat -v uses. */static voidmakeascii( int length, char *data, FILE *fp ){ register u_char *cp; register int c; for (cp = (u_char *)data; cp < (u_char *)data + length; cp++) { c = (int)*cp; if (c > 0177) { putc('M', fp); putc('-', fp); c &= 0177; } if (c < ' ') { putc('^', fp); putc(c+'@', fp); } else if (c == 0177) { putc('^', fp); putc('?', fp); } else { putc(c, fp); } }}/* * asciize - same thing as makeascii except add a newline */voidasciize( int length, char *data, FILE *fp ){ makeascii(length, data, fp); putc('\n', fp);}/* * Some circular buffer space */#define CBLEN 80#define NUMCB 6char circ_buf[NUMCB][CBLEN];int nextcb = 0;/* * nextvar - find the next variable in the buffer */intnextvar( int *datalen, char **datap, char **vname, char **vvalue ){ register char *cp; register char *np; register char *cpend; register char *npend; /* character after last */ int quoted = 0; static char name[MAXVARLEN]; static char value[MAXVALLEN]; cp = *datap; cpend = cp + *datalen; /* * Space past commas and white space */ while (cp < cpend && (*cp == ',' || isspace((int)*cp))) cp++; if (cp == cpend) return 0; /* * Copy name until we hit a ',', an '=', a '\r' or a '\n'. Backspace * over any white space and terminate it. */ np = name; npend = &name[MAXVARLEN]; while (cp < cpend && np < npend && *cp != ',' && *cp != '=' && *cp != '\r' && *cp != '\n') *np++ = *cp++; /* * Check if we ran out of name space, without reaching the end or a * terminating character */ if (np == npend && !(cp == cpend || *cp == ',' || *cp == '=' || *cp == '\r' || *cp == '\n')) return 0; while (isspace((int)(*(np-1)))) np--; *np = '\0'; *vname = name; /* * Check if we hit the end of the buffer or a ','. If so we are done. */ if (cp == cpend || *cp == ',' || *cp == '\r' || *cp == '\n') { if (cp != cpend) cp++; *datap = cp; *datalen = cpend - cp; *vvalue = (char *)0; return 1; } /* * So far, so good. Copy out the value */ cp++; /* past '=' */ while (cp < cpend && (isspace((int)*cp) && *cp != '\r' && *cp != '\n')) cp++; np = value; npend = &value[MAXVALLEN]; while (cp < cpend && np < npend && ((*cp != ',') || quoted)) { quoted ^= ((*np++ = *cp++) == '"'); } /* * Check if we overran the value buffer while still in a quoted string * or without finding a comma */ if (np == npend && (quoted || *cp != ',')) return 0; /* * Trim off any trailing whitespace */ while (np > value && isspace((int)(*(np-1)))) np--; *np = '\0'; /* * Return this. All done. */ if (cp != cpend) cp++; *datap = cp; *datalen = cpend - cp; *vvalue = value; return 1;}/* * findvar - see if this variable is known to us. * If "code" is 1, return ctl_var->code. * Otherwise return the ordinal position of the found variable. */intfindvar( char *varname, struct ctl_var *varlist, int code ){ register char *np; register struct ctl_var *vl; vl = varlist; np = varname; while (vl->fmt != EOV) { if (vl->fmt != PADDING && STREQ(np, vl->text)) return (code) ? vl->code : (vl - varlist) ; vl++; } return 0;}/* * printvars - print variables returned in response packet */voidprintvars( int length, char *data, int status, int sttype, FILE *fp ){ if (rawmode) rawprint(sttype, length, data, status, fp); else cookedprint(sttype, length, data, status, fp);}/* * rawprint - do a printout of the data in raw mode */static voidrawprint( int datatype, int length, char *data, int status, FILE *fp ){ register char *cp; register char *cpend; /* * Essentially print the data as is. We reformat unprintables, though. */ cp = data; cpend = data + length; (void) fprintf(fp, "status=0x%04x,\n", status); while (cp < cpend) { if (*cp == '\r') { /* * If this is a \r and the next character is a * \n, supress this, else pretty print it. Otherwise * just output the character. */ if (cp == (cpend-1) || *(cp+1) != '\n') makeascii(1, cp, fp); } else if (isspace((int)*cp) || isprint((int)*cp)) { putc(*cp, fp); } else { makeascii(1, cp, fp); } cp++; }}/* * Global data used by the cooked output routines */int out_chars; /* number of characters output */int out_linecount; /* number of characters output on this line *//* * startoutput - get ready to do cooked output */static voidstartoutput(void){ out_chars = 0; out_linecount = 0;}/* * output - output a variable=value combination */static voidoutput( FILE *fp, char *name, char *value ){ int lenname; int lenvalue; lenname = strlen(name); lenvalue = strlen(value); if (out_chars != 0) { putc(',', fp); out_chars++; out_linecount++; if ((out_linecount + lenname + lenvalue + 3) > MAXOUTLINE) { putc('\n', fp); out_chars++; out_linecount = 0; } else { putc(' ', fp); out_chars++; out_linecount++; } } fputs(name, fp); putc('=', fp); fputs(value, fp); out_chars += lenname + 1 + lenvalue; out_linecount += lenname + 1 + lenvalue;}/* * endoutput - terminate a block of cooked output */static voidendoutput( FILE *fp ){ if (out_chars != 0) putc('\n', fp);}/* * outputarr - output an array of values */static voidoutputarr( FILE *fp, char *name, int narr, l_fp *lfp ){ register char *bp; register char *cp; register int i; register int len; char buf[256]; bp = buf; /* * Hack to align delay and offset values */ for (i = (int)strlen(name); i < 11; i++) *bp++ = ' '; for (i = narr; i > 0; i--) { if (i != narr) *bp++ = ' '; cp = lfptoms(lfp, 2); len = strlen(cp); if (len > 7) { cp[7] = '\0'; len = 7; } while (len < 7) { *bp++ = ' '; len++; } while (*cp != '\0') *bp++ = *cp++; lfp++; } *bp = '\0'; output(fp, name, buf);}static char *tstflags( u_long val ){ register char *cb, *s; register int i; register const char *sep; sep = ""; i = 0; s = cb = &circ_buf[nextcb][0]; if (++nextcb >= NUMCB) nextcb = 0; sprintf(cb, "%02lx", val); cb += strlen(cb); if (!val) { strcat(cb, " ok"); cb += strlen(cb); } else { *cb++ = ' '; for (i = 0; i < 14; i++) { if (val & 0x1) { sprintf(cb, "%s%s", sep, tstflagnames[i]); sep = ", "; cb += strlen(cb); } val >>= 1; } } *cb = '\0'; return s;}/* * cookedprint - output variables in cooked mode */static voidcookedprint( int datatype, int length, char *data, int status, FILE *fp ){ register int varid; char *name; char *value; char output_raw; int fmt; struct ctl_var *varlist; l_fp lfp; long ival; struct sockaddr_storage hval; u_long uval; l_fp lfparr[8]; int narr; switch (datatype) { case TYPE_PEER: varlist = peer_var; break; case TYPE_SYS: varlist = sys_var; break; case TYPE_CLOCK: varlist = clock_var; break; default: (void) fprintf(stderr, "Unknown datatype(0x%x) in cookedprint\n", datatype); return; } (void) fprintf(fp, "status=%04x %s,\n", status, statustoa(datatype, status)); startoutput(); while (nextvar(&length, &data, &name, &value)) { varid = findvar(name, varlist, 0); if (varid == 0) { output_raw = '*'; } else { output_raw = 0; fmt = varlist[varid].fmt; switch(fmt) { case TS: if (!decodets(value, &lfp)) output_raw = '?'; else output(fp, name, prettydate(&lfp)); break; case FL: case FU: case FS: if (!decodetime(value, &lfp)) output_raw = '?'; else { switch (fmt) { case FL: output(fp, name, lfptoms(&lfp, 3)); break; case FU: output(fp, name, ulfptoms(&lfp, 3)); break; case FS: output(fp, name, lfptoms(&lfp, 3)); break; } } break; case UI: if (!decodeuint(value, &uval)) output_raw = '?'; else output(fp, name, uinttoa(uval)); break; case SI: if (!decodeint(value, &ival)) output_raw = '?'; else output(fp, name, inttoa(ival)); break; case HA: case NA: if (!decodenetnum(value, &hval)) output_raw = '?'; else if (fmt == HA){ output(fp, name, nntohost(&hval)); } else { output(fp, name, stoa(&hval)); } break; case ST: output_raw = '*'; break; case RF: if (decodenetnum(value, &hval)) { if ((hval.ss_family == AF_INET) && ISREFCLOCKADR(&hval)) output(fp, name, refnumtoa(&hval)); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -