util.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 794 行 · 第 1/2 页

C
794
字号
#ifndef lintstatic char *sccsid = "@(#)util.c	4.2	(ULTRIX)	1/25/91";#endif/* Based on: * RCSid = "$Header: /sparky/a/davy/system/nfswatch/RCS/util.c,v 3.0 91/01/23 08:23:29 davy Exp $"; *//* * util.c - miscellaneous utility routines. * * David A. Curry				Jeffrey C. Mogul * SRI International				Digital Equipment Corporation * 333 Ravenswood Avenue			Western Research Laboratory * Menlo Park, CA 94025				100 Hamilton Avenue * davy@erg.sri.com				Palo Alto, CA 94301 *						mogul@decwrl.dec.com * * $Log:	util.c,v $ * Revision 3.0  91/01/23  08:23:29  davy * NFSWATCH Version 3.0. *  * Revision 1.4  91/01/17  10:18:08  davy * New features from Jeff Mogul. *  * Revision 1.6  91/01/07  15:35:20  mogul * Must rebuild client hash table after sorting *  * Revision 1.5  91/01/07  14:09:10  mogul * Improved sortbyusage stuff *  * Revision 1.4  91/01/04  14:14:27  mogul * Support for client counters *  * Revision 1.3  91/01/03  17:42:20  mogul * Support for per-procedure counters *  * Revision 1.2  90/08/17  15:47:50  davy * NFSWATCH Version 2.0. *  * Revision 1.1  88/11/29  11:21:05  davy * NFSWATCH Release 1.0 *  */#include <sys/param.h>#include <sys/stat.h>#include <curses.h>#include <stdio.h>#ifdef sun#include <mntent.h>#include <exportent.h>#endif /* sun */#ifdef ultrix#include <sys/mount.h>#endif /* ultrix */#include "nfswatch.h"#include "externs.h"#include "screen.h"/* * clear_vars - set interval counters to zero. */voidclear_vars(){	register int i;	int_pkt_total = 0;	int_pkt_drops = 0;	int_dst_pkt_total = 0;	for (i = 0; i < PKT_NCOUNTERS; i++)		pkt_counters[i].pc_interval = 0;	for (i = 0; i < nnfscounters; i++) {		(void) bzero((char *) nfs_counters[i].nc_proc,			MAXNFSPROC * sizeof(Counter));		nfs_counters[i].nc_interval = 0;	}	for (i = 0; i < nfilecounters; i++) {		(void) bzero((char *) fil_counters[i].fc_proc,			MAXNFSPROC * sizeof(Counter));		fil_counters[i].fc_interval = 0;	}	for (i = 0; i < MAXNFSPROC; i++)		prc_counters[i].pr_interval = 0;	for (i = 0; i < nclientcounters; i++)		clnt_counters[i].cl_interval = 0;}/* * savestr - save a string in dynamically allocated memory. */char *savestr(s)register char *s;{	char *malloc();	register char *str;	str = malloc(strlen(s) + 1);	if (str == NULL) {		(void) fprintf(stderr, "%s: out of memory.\n", pname);		finish(-1);	}	(void) strcpy(str, s);	return(str);}/* * prtime - convert a time to hh:mm:ss. */char *prtime(sec)time_t sec;{	int hh, mm, ss;	static char tbuf[16];	hh = sec / 3600;	sec %= 3600;	mm = sec / 60;	sec %= 60;	ss = sec;	(void) sprintf(tbuf, "%02d:%02d:%02d", hh, mm, ss);	return(tbuf);}/* * error - print an error message preceded by the program name. */voiderror(str)register char *str;{	char buf[BUFSIZ];	(void) sprintf(buf, "%s: %s", pname, str);	(void) perror(buf);}/* * finish - clean up and exit. */voidfinish(code)int code;{	/*	 * Close the nit device.	 */	if (if_fd >= 0)		(void) close(if_fd);	/*	 * End curses.	 */	if (screen_inited) {#ifdef nocrmode		(void) nocrmode();#else		(void) nocbreak();#endif /* nocrmode */		(void) echo();		(void) move(SCR_PROMPT_Y, SCR_PROMPT_X0);		(void) clrtoeol();		(void) refresh();		(void) endwin();	}	if (logging) {		(void) fprintf(logfp, "#\n# endlog\n#\n");		(void) fclose(logfp);	}	(void) putchar('\n');	if (code < 0)		(void) exit(-code);	(void) exit(0);}/* * setup_pkt_counters - set up packet counter screen coordinates. */voidsetup_pkt_counters(){	register int i, j;	(void) bzero((char *) pkt_counters,		PKT_NCOUNTERS * sizeof(PacketCounter));	/*	 * Set up the strings.	 */	pkt_counters[PKT_NDREAD].pc_name = "ND Read";	pkt_counters[PKT_NDWRITE].pc_name = "ND Write";	pkt_counters[PKT_NFSREAD].pc_name = "NFS Read";	pkt_counters[PKT_NFSWRITE].pc_name = "NFS Write";	pkt_counters[PKT_NFSMOUNT].pc_name = "NFS Mount";	pkt_counters[PKT_YELLOWPAGES].pc_name = "Yellow Pages/NIS";	pkt_counters[PKT_RPCAUTH].pc_name = "RPC Authorization";	pkt_counters[PKT_OTHERRPC].pc_name = "Other RPC Packets";	pkt_counters[PKT_TCP].pc_name = "TCP Packets";	pkt_counters[PKT_UDP].pc_name = "UDP Packets";	pkt_counters[PKT_ICMP].pc_name = "ICMP Packets";	pkt_counters[PKT_ROUTING].pc_name = "Routing Control";	pkt_counters[PKT_ARP].pc_name = "Address Resolution";	pkt_counters[PKT_RARP].pc_name = "Reverse Addr Resol";	pkt_counters[PKT_BROADCAST].pc_name = "Ethernet Broadcast";	pkt_counters[PKT_OTHER].pc_name = "Other Packets";	/*	 * Set screen coordinates for everything.	 */	for (i = 0, j = PKT_NCOUNTERS/2; i < PKT_NCOUNTERS/2; i++, j++) {		pkt_counters[i].pc_namex = SCR_PKT_NAME_X;		pkt_counters[j].pc_namex = SCR_PKT_NAME_X + SCR_MIDDLE;		pkt_counters[i].pc_namey = SCR_PKT_Y + i;		pkt_counters[j].pc_namey = SCR_PKT_Y + i;		pkt_counters[i].pc_intx = SCR_PKT_INT_X;		pkt_counters[j].pc_intx = SCR_PKT_INT_X + SCR_MIDDLE;		pkt_counters[i].pc_inty = SCR_PKT_Y + i;		pkt_counters[j].pc_inty = SCR_PKT_Y + i;		pkt_counters[i].pc_totx = SCR_PKT_TOT_X;		pkt_counters[j].pc_totx = SCR_PKT_TOT_X + SCR_MIDDLE;		pkt_counters[i].pc_toty = SCR_PKT_Y + i;		pkt_counters[j].pc_toty = SCR_PKT_Y + i;		pkt_counters[i].pc_pctx = SCR_PKT_PCT_X;		pkt_counters[j].pc_pctx = SCR_PKT_PCT_X + SCR_MIDDLE;		pkt_counters[i].pc_pcty = SCR_PKT_Y + i;		pkt_counters[j].pc_pcty = SCR_PKT_Y + i;	}}/* * setup_nfs_counters- setup NFS counter screen coordinates, file system *		       names. */voidsetup_nfs_counters(){	FILE *fp;	struct stat st;	register int i, j;	register NFSCounter *nc;#ifdef sun	register struct mntent *mnt;#endif /* sun */#ifdef ultrix	int dummy, nmnts;	static struct fs_data fsData[MAXEXPORT];#endif /* ultrix */	(void) bzero((char *) nfs_counters,		MAXEXPORT * sizeof(NFSCounter));	/*	 * If we're not watching our own host, we can't look	 * for mounted file systems.	 */	if (strcmp(myhost, dsthost) != 0) {		learnfs = 1;		sort_nfs_counters();		return;	}#ifdef sun	/*	 * Open the list of mounted file systems.	 */	if ((fp = setmntent(MOUNTED, "r")) == NULL) {		error(MOUNTED);		finish(-1);	}	nc = nfs_counters;	/*	 * Save the first MAXEXPORT file systems of type "4.2"	 * which have been exported.  These are the ones which can	 * be mounted through NFS.	 */	while ((mnt = getmntent(fp)) != NULL) {		if (strcmp(mnt->mnt_type, MNTTYPE_42) != 0)			continue;		if (nnfscounters < MAXEXPORT) {			if (stat(mnt->mnt_dir, &st) < 0)				continue;			/*			 * Not exported; skip it.			 */			if (!is_exported(st.st_dev))				continue;			nc->nc_dev = st.st_dev;			nc->nc_name = savestr(mnt->mnt_dir);			nnfscounters++;			nc++;		}	}	(void) endmntent(fp);#endif /* sun */#ifdef ultrix	/*	 * Get the mounted file information.	 */	dummy = 0;	nmnts = getmnt(&dummy, fsData, sizeof(fsData), NOSTAT_MANY, 0);	if (nmnts < 0) {		error("getmnt");		finish(-1);	}	nc = nfs_counters;	/*	 * Save the first MAXEXPORT file systems which could have been	 * exported.  These are what can be mounted through NFS.	 */	if (nmnts > MAXEXPORT)		nmnts = MAXEXPORT;	for (i=0; i < nmnts; i++) {		if (fsData[i].fd_flags & M_LOCAL) {			nc->nc_dev = fsData[i].fd_dev;			nc->nc_name = savestr(fsData[i].fd_path);			nnfscounters++;			nc++;		}	}#endif /* ultrix */	sort_nfs_counters();}/* * sort_nfs_counters - sort and assign places on the screen */voidsort_nfs_counters(){	register int i, j;	(void) qsort(nfs_counters, nnfscounters, sizeof(NFSCounter), nfs_comp);	/*	 * Set screen coordinates for the ones which will be	 * displayed.	 */	for (i = 0, j = NFSLINES/2; i < NFSLINES/2; i++, j++) {		nfs_counters[i].nc_namex = SCR_NFS_NAME_X;		nfs_counters[j].nc_namex = SCR_NFS_NAME_X + SCR_MIDDLE;		nfs_counters[i].nc_namey = SCR_NFS_Y + i;		nfs_counters[j].nc_namey = SCR_NFS_Y + i;		nfs_counters[i].nc_intx = SCR_NFS_INT_X;		nfs_counters[j].nc_intx = SCR_NFS_INT_X + SCR_MIDDLE;		nfs_counters[i].nc_inty = SCR_NFS_Y + i;		nfs_counters[j].nc_inty = SCR_NFS_Y + i;		nfs_counters[i].nc_totx = SCR_NFS_TOT_X;		nfs_counters[j].nc_totx = SCR_NFS_TOT_X + SCR_MIDDLE;		nfs_counters[i].nc_toty = SCR_NFS_Y + i;		nfs_counters[j].nc_toty = SCR_NFS_Y + i;		nfs_counters[i].nc_pctx = SCR_NFS_PCT_X;		nfs_counters[j].nc_pctx = SCR_NFS_PCT_X + SCR_MIDDLE;		nfs_counters[i].nc_pcty = SCR_NFS_Y + i;		nfs_counters[j].nc_pcty = SCR_NFS_Y + i;	}}/*

⌨️ 快捷键说明

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