Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1983, 1988 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.2  mycroft /*static char sccsid[] = "from: @(#)main.c	5.23 (Berkeley) 7/1/91";*/
     42  1.4   brezak static char rcsid[] = "$Id: main.c,v 1.4 1994/01/11 19:42:53 brezak Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #include <sys/param.h>
     46  1.1      cgd #include <sys/socket.h>
     47  1.1      cgd #include <sys/file.h>
     48  1.1      cgd #include <errno.h>
     49  1.1      cgd #include <netdb.h>
     50  1.1      cgd #include <nlist.h>
     51  1.1      cgd #include <kvm.h>
     52  1.1      cgd #include <stdio.h>
     53  1.1      cgd #include <ctype.h>
     54  1.1      cgd #include <stdlib.h>
     55  1.1      cgd #include <string.h>
     56  1.1      cgd #include <paths.h>
     57  1.1      cgd 
     58  1.1      cgd struct nlist nl[] = {
     59  1.1      cgd #define	N_MBSTAT	0
     60  1.1      cgd 	{ "_mbstat" },
     61  1.1      cgd #define	N_IPSTAT	1
     62  1.1      cgd 	{ "_ipstat" },
     63  1.1      cgd #define	N_TCB		2
     64  1.1      cgd 	{ "_tcb" },
     65  1.1      cgd #define	N_TCPSTAT	3
     66  1.1      cgd 	{ "_tcpstat" },
     67  1.1      cgd #define	N_UDB		4
     68  1.1      cgd 	{ "_udb" },
     69  1.1      cgd #define	N_UDPSTAT	5
     70  1.1      cgd 	{ "_udpstat" },
     71  1.1      cgd #define	N_IFNET		6
     72  1.1      cgd 	{ "_ifnet" },
     73  1.1      cgd #define	N_IMP		7
     74  1.1      cgd 	{ "_imp_softc" },
     75  1.1      cgd #define	N_RTHOST	8
     76  1.1      cgd 	{ "_rthost" },
     77  1.1      cgd #define	N_RTNET		9
     78  1.1      cgd 	{ "_rtnet" },
     79  1.1      cgd #define	N_ICMPSTAT	10
     80  1.1      cgd 	{ "_icmpstat" },
     81  1.1      cgd #define	N_RTSTAT	11
     82  1.1      cgd 	{ "_rtstat" },
     83  1.1      cgd #define	N_FILEHEAD	12
     84  1.1      cgd 	{ "_filehead" },
     85  1.1      cgd #define	N_FILES		13
     86  1.1      cgd 	{ "_nfiles" },
     87  1.1      cgd #define	N_UNIXSW	14
     88  1.1      cgd 	{ "_unixsw" },
     89  1.1      cgd #define N_RTHASHSIZE	15
     90  1.1      cgd 	{ "_rthashsize" },
     91  1.1      cgd #define N_IDP		16
     92  1.1      cgd 	{ "_nspcb"},
     93  1.1      cgd #define N_IDPSTAT	17
     94  1.1      cgd 	{ "_idpstat"},
     95  1.1      cgd #define N_SPPSTAT	18
     96  1.1      cgd 	{ "_spp_istat"},
     97  1.1      cgd #define N_NSERR		19
     98  1.1      cgd 	{ "_ns_errstat"},
     99  1.1      cgd #define	N_CLNPSTAT	20
    100  1.1      cgd 	{ "_clnp_stat"},
    101  1.1      cgd #define	IN_TP		21
    102  1.1      cgd 	{ "_tp_inpcb" },
    103  1.1      cgd #define	ISO_TP		22
    104  1.1      cgd 	{ "_tp_isopcb" },
    105  1.1      cgd #define	N_TPSTAT	23
    106  1.1      cgd 	{ "_tp_stat" },
    107  1.1      cgd #define	N_ESISSTAT	24
    108  1.1      cgd 	{ "_esis_stat"},
    109  1.1      cgd #define N_NIMP		25
    110  1.1      cgd 	{ "_nimp"},
    111  1.1      cgd #define N_RTREE		26
    112  1.1      cgd 	{ "_radix_node_head"},
    113  1.1      cgd #define N_CLTP		27
    114  1.1      cgd 	{ "_cltb"},
    115  1.1      cgd #define N_CLTPSTAT	28
    116  1.1      cgd 	{ "_cltpstat"},
    117  1.4   brezak #define N_IGMPSTAT	29
    118  1.4   brezak 	{ "_igmpstat" },
    119  1.4   brezak #define N_MRTPROTO	30
    120  1.4   brezak 	{ "_ip_mrtproto" },
    121  1.4   brezak #define N_MRTSTAT	31
    122  1.4   brezak 	{ "_mrtstat" },
    123  1.4   brezak #define N_MRTTABLE	32
    124  1.4   brezak 	{ "_mrttable" },
    125  1.4   brezak #define N_VIFTABLE	33
    126  1.4   brezak 	{ "_viftable" },
    127  1.1      cgd 	"",
    128  1.1      cgd };
    129  1.1      cgd 
    130  1.1      cgd /* internet protocols */
    131  1.1      cgd extern	int protopr();
    132  1.1      cgd extern	int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
    133  1.4   brezak extern	int igmp_stats();
    134  1.1      cgd #ifdef NS
    135  1.1      cgd /* ns protocols */
    136  1.1      cgd extern	int nsprotopr();
    137  1.1      cgd extern	int spp_stats(), idp_stats(), nserr_stats();
    138  1.1      cgd #endif
    139  1.1      cgd #ifdef ISO
    140  1.1      cgd /* iso protocols */
    141  1.1      cgd extern	int iso_protopr();
    142  1.1      cgd extern	int tp_stats(), esis_stats(), clnp_stats(), cltp_stats();
    143  1.1      cgd #endif
    144  1.1      cgd 
    145  1.1      cgd struct protox {
    146  1.1      cgd 	u_char	pr_index;		/* index into nlist of cb head */
    147  1.1      cgd 	u_char	pr_sindex;		/* index into nlist of stat block */
    148  1.1      cgd 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    149  1.1      cgd 	int	(*pr_cblocks)();	/* control blocks printing routine */
    150  1.1      cgd 	int	(*pr_stats)();		/* statistics printing routine */
    151  1.1      cgd 	char	*pr_name;		/* well-known name */
    152  1.1      cgd } protox[] = {
    153  1.1      cgd 	{ N_TCB,	N_TCPSTAT,	1,	protopr,
    154  1.1      cgd 	  tcp_stats,	"tcp" },
    155  1.1      cgd 	{ N_UDB,	N_UDPSTAT,	1,	protopr,
    156  1.1      cgd 	  udp_stats,	"udp" },
    157  1.1      cgd #ifdef ISO
    158  1.1      cgd 	{ IN_TP,	N_TPSTAT,	1,	protopr,
    159  1.1      cgd 	  tp_stats,	"tpip" },
    160  1.1      cgd #endif
    161  1.1      cgd 	{ -1,		N_IPSTAT,	1,	0,
    162  1.1      cgd 	  ip_stats,	"ip" },
    163  1.1      cgd 	{ -1,		N_ICMPSTAT,	1,	0,
    164  1.1      cgd 	  icmp_stats,	"icmp" },
    165  1.4   brezak 	{ -1,		N_IGMPSTAT,	1,	0,
    166  1.4   brezak 	  igmp_stats,	"igmp" },
    167  1.1      cgd 	{ -1,		-1,		0,	0,
    168  1.1      cgd 	  0,		0 }
    169  1.1      cgd };
    170  1.1      cgd 
    171  1.1      cgd #ifdef NS
    172  1.1      cgd struct protox nsprotox[] = {
    173  1.1      cgd 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    174  1.1      cgd 	  idp_stats,	"idp" },
    175  1.1      cgd 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    176  1.1      cgd 	  spp_stats,	"spp" },
    177  1.1      cgd 	{ -1,		N_NSERR,	1,	0,
    178  1.1      cgd 	  nserr_stats,	"ns_err" },
    179  1.1      cgd 	{ -1,		-1,		0,	0,
    180  1.1      cgd 	  0,		0 }
    181  1.1      cgd };
    182  1.1      cgd #endif
    183  1.1      cgd 
    184  1.1      cgd #ifdef ISO
    185  1.1      cgd struct protox isoprotox[] = {
    186  1.1      cgd 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    187  1.1      cgd 	  tp_stats,	"tp" },
    188  1.1      cgd 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    189  1.1      cgd 	  cltp_stats,	"cltp" },
    190  1.1      cgd 	{ -1,		N_CLNPSTAT,	1,	 0,
    191  1.1      cgd 	  clnp_stats,	"clnp"},
    192  1.1      cgd 	{ -1,		N_ESISSTAT,	1,	 0,
    193  1.1      cgd 	  esis_stats,	"esis"},
    194  1.1      cgd 	{ -1,		-1,		0,	0,
    195  1.1      cgd 	  0,		0 }
    196  1.1      cgd };
    197  1.1      cgd #endif
    198  1.1      cgd 
    199  1.1      cgd struct protox *protoprotox[] = { protox,
    200  1.1      cgd 
    201  1.1      cgd #ifdef NS
    202  1.1      cgd nsprotox,
    203  1.1      cgd #endif
    204  1.1      cgd 
    205  1.1      cgd #ifdef ISO
    206  1.1      cgd isoprotox,
    207  1.1      cgd #endif
    208  1.1      cgd 
    209  1.1      cgd NULL };
    210  1.1      cgd 
    211  1.1      cgd char	*vmunix = _PATH_UNIX;
    212  1.1      cgd char	*kmemf;
    213  1.1      cgd int	kmem;
    214  1.1      cgd int	kflag;
    215  1.1      cgd int	Aflag;
    216  1.1      cgd int	aflag;
    217  1.1      cgd int	hflag;
    218  1.1      cgd int	iflag;
    219  1.4   brezak int	Mflag;
    220  1.1      cgd int	mflag;
    221  1.1      cgd int	nflag;
    222  1.1      cgd int	pflag;
    223  1.1      cgd int	rflag;
    224  1.1      cgd int	sflag;
    225  1.1      cgd int	tflag;
    226  1.1      cgd int	dflag;
    227  1.1      cgd int	interval;
    228  1.1      cgd char	*interface;
    229  1.1      cgd int	unit;
    230  1.1      cgd 
    231  1.1      cgd int	af = AF_UNSPEC;
    232  1.1      cgd 
    233  1.1      cgd main(argc, argv)
    234  1.1      cgd 	int argc;
    235  1.1      cgd 	char **argv;
    236  1.1      cgd {
    237  1.1      cgd 	extern char *optarg;
    238  1.1      cgd 	extern int optind;
    239  1.1      cgd 	register struct protoent *p;
    240  1.1      cgd 	register struct protox *tp;	/* for printing cblocks & stats */
    241  1.1      cgd 	struct protox *name2protox();	/* for -p */
    242  1.1      cgd 	int ch;
    243  1.1      cgd 	void usage();
    244  1.1      cgd 
    245  1.4   brezak 	while ((ch = getopt(argc, argv, "Aac:df:hI:iMmN:np:rstuw:")) != EOF)
    246  1.1      cgd 		switch((char)ch) {
    247  1.1      cgd 		case 'A':
    248  1.1      cgd 			Aflag = 1;
    249  1.1      cgd 			break;
    250  1.1      cgd 		case 'a':
    251  1.1      cgd 			aflag = 1;
    252  1.1      cgd 			break;
    253  1.4   brezak 		case 'c':
    254  1.4   brezak 			kmemf = optarg;
    255  1.4   brezak 			kflag = 1;
    256  1.4   brezak 			break;
    257  1.1      cgd 		case 'd':
    258  1.1      cgd 			dflag = 1;
    259  1.1      cgd 			break;
    260  1.1      cgd 		case 'f':
    261  1.1      cgd 			if (strcmp(optarg, "ns") == 0)
    262  1.1      cgd 				af = AF_NS;
    263  1.1      cgd 			else if (strcmp(optarg, "inet") == 0)
    264  1.1      cgd 				af = AF_INET;
    265  1.1      cgd 			else if (strcmp(optarg, "unix") == 0)
    266  1.1      cgd 				af = AF_UNIX;
    267  1.1      cgd 			else if (strcmp(optarg, "iso") == 0)
    268  1.1      cgd 				af = AF_ISO;
    269  1.1      cgd 			else {
    270  1.1      cgd 				(void)fprintf(stderr,
    271  1.1      cgd 				    "%s: unknown address family\n", optarg);
    272  1.1      cgd 				exit(1);
    273  1.1      cgd 			}
    274  1.1      cgd 			break;
    275  1.1      cgd 		case 'h':
    276  1.1      cgd 			hflag = 1;
    277  1.1      cgd 			break;
    278  1.1      cgd 		case 'I': {
    279  1.1      cgd 			char *cp;
    280  1.1      cgd 
    281  1.1      cgd 			iflag = 1;
    282  1.1      cgd 			for (cp = interface = optarg; isalpha(*cp); cp++);
    283  1.1      cgd 			unit = atoi(cp);
    284  1.1      cgd 			*cp = '\0';
    285  1.1      cgd 			break;
    286  1.1      cgd 		}
    287  1.1      cgd 		case 'i':
    288  1.1      cgd 			iflag = 1;
    289  1.1      cgd 			break;
    290  1.1      cgd 		case 'M':
    291  1.4   brezak 			Mflag++;
    292  1.1      cgd 			break;
    293  1.1      cgd 		case 'm':
    294  1.1      cgd 			mflag = 1;
    295  1.1      cgd 			break;
    296  1.1      cgd 		case 'N':
    297  1.1      cgd 			vmunix = optarg;
    298  1.1      cgd 			break;
    299  1.1      cgd 		case 'n':
    300  1.1      cgd 			nflag = 1;
    301  1.1      cgd 			break;
    302  1.1      cgd 		case 'p':
    303  1.1      cgd 			if ((tp = name2protox(optarg)) == NULL) {
    304  1.1      cgd 				(void)fprintf(stderr,
    305  1.1      cgd 				    "%s: unknown or uninstrumented protocol\n",
    306  1.1      cgd 				    optarg);
    307  1.1      cgd 				exit(1);
    308  1.1      cgd 			}
    309  1.1      cgd 			pflag = 1;
    310  1.1      cgd 			break;
    311  1.1      cgd 		case 'r':
    312  1.1      cgd 			rflag = 1;
    313  1.1      cgd 			break;
    314  1.1      cgd 		case 's':
    315  1.1      cgd 			sflag = 1;
    316  1.1      cgd 			break;
    317  1.1      cgd 		case 't':
    318  1.1      cgd 			tflag = 1;
    319  1.1      cgd 			break;
    320  1.1      cgd 		case 'u':
    321  1.1      cgd 			af = AF_UNIX;
    322  1.1      cgd 			break;
    323  1.1      cgd 		case 'w':
    324  1.1      cgd 			interval = atoi(optarg);
    325  1.1      cgd 			break;
    326  1.1      cgd 		case '?':
    327  1.1      cgd 		default:
    328  1.1      cgd 			usage();
    329  1.1      cgd 		}
    330  1.1      cgd 	argv += optind;
    331  1.1      cgd 	argc -= optind;
    332  1.1      cgd 
    333  1.1      cgd #define	BACKWARD_COMPATIBILITY
    334  1.1      cgd #ifdef	BACKWARD_COMPATIBILITY
    335  1.1      cgd 	if (*argv) {
    336  1.1      cgd 		if (isdigit(**argv)) {
    337  1.1      cgd 			interval = atoi(*argv);
    338  1.1      cgd 			if (interval <= 0)
    339  1.1      cgd 				usage();
    340  1.1      cgd 			++argv;
    341  1.1      cgd 			iflag = 1;
    342  1.1      cgd 		}
    343  1.1      cgd 		if (*argv) {
    344  1.1      cgd 			vmunix = *argv;
    345  1.1      cgd 			if (*++argv) {
    346  1.1      cgd 				kmemf = *argv;
    347  1.1      cgd 				kflag = 1;
    348  1.1      cgd 			}
    349  1.1      cgd 		}
    350  1.1      cgd 	}
    351  1.1      cgd #endif
    352  1.1      cgd 	if (kvm_openfiles(vmunix, kmemf, NULL) == -1) {
    353  1.1      cgd 		fprintf(stderr, "netstat: kvm_openfiles: %s\n", kvm_geterr());
    354  1.1      cgd 		exit(1);
    355  1.1      cgd 	}
    356  1.1      cgd 	if (kvm_nlist(nl) < 0 || nl[0].n_type == 0) {
    357  1.1      cgd 		fprintf(stderr, "%s: no namelist\n", vmunix);
    358  1.1      cgd 		exit(1);
    359  1.1      cgd 	}
    360  1.1      cgd 	if (mflag) {
    361  1.1      cgd 		mbpr((off_t)nl[N_MBSTAT].n_value);
    362  1.1      cgd 		exit(0);
    363  1.1      cgd 	}
    364  1.1      cgd 	if (pflag) {
    365  1.1      cgd 		if (tp->pr_stats)
    366  1.1      cgd 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    367  1.1      cgd 				tp->pr_name);
    368  1.1      cgd 		else
    369  1.1      cgd 			printf("%s: no stats routine\n", tp->pr_name);
    370  1.1      cgd 		exit(0);
    371  1.1      cgd 	}
    372  1.1      cgd 	if (hflag) {
    373  1.1      cgd #ifdef IMP
    374  1.1      cgd 		hostpr(nl[N_IMP].n_value, nl[N_NIMP].n_value);
    375  1.1      cgd #else
    376  1.1      cgd 		(void)fprintf(stderr, "netstat: IMP code not compiled in\n");
    377  1.1      cgd #endif
    378  1.1      cgd 		exit(0);
    379  1.1      cgd 	}
    380  1.1      cgd 	/*
    381  1.1      cgd 	 * Keep file descriptors open to avoid overhead
    382  1.1      cgd 	 * of open/close on each call to get* routines.
    383  1.1      cgd 	 */
    384  1.1      cgd 	sethostent(1);
    385  1.1      cgd 	setnetent(1);
    386  1.1      cgd 	if (iflag) {
    387  1.1      cgd 		intpr(interval, nl[N_IFNET].n_value);
    388  1.1      cgd 		exit(0);
    389  1.1      cgd 	}
    390  1.1      cgd 	if (rflag) {
    391  1.1      cgd 		if (sflag)
    392  1.1      cgd 			rt_stats((off_t)nl[N_RTSTAT].n_value);
    393  1.1      cgd 		else
    394  1.1      cgd 			routepr((off_t)nl[N_RTHOST].n_value,
    395  1.1      cgd 				(off_t)nl[N_RTNET].n_value,
    396  1.1      cgd 				(off_t)nl[N_RTHASHSIZE].n_value,
    397  1.1      cgd 				(off_t)nl[N_RTREE].n_value);
    398  1.1      cgd 		exit(0);
    399  1.1      cgd 	}
    400  1.4   brezak 	if (Mflag) {
    401  1.4   brezak 		if (sflag)
    402  1.4   brezak 			mrt_stats((off_t)nl[N_MRTPROTO].n_value,
    403  1.4   brezak 				  (off_t)nl[N_MRTSTAT].n_value);
    404  1.4   brezak 		else
    405  1.4   brezak 			mroutepr((off_t)nl[N_MRTPROTO].n_value,
    406  1.4   brezak 				 (off_t)nl[N_MRTTABLE].n_value,
    407  1.4   brezak 				 (off_t)nl[N_VIFTABLE].n_value);
    408  1.4   brezak 		exit(0);
    409  1.4   brezak 	}
    410  1.4   brezak 
    411  1.1      cgd     if (af == AF_INET || af == AF_UNSPEC) {
    412  1.1      cgd 	setprotoent(1);
    413  1.1      cgd 	setservent(1);
    414  1.1      cgd 	while (p = getprotoent()) {
    415  1.1      cgd 
    416  1.1      cgd 		for (tp = protox; tp->pr_name; tp++)
    417  1.1      cgd 			if (strcmp(tp->pr_name, p->p_name) == 0)
    418  1.1      cgd 				break;
    419  1.1      cgd 		if (tp->pr_name == 0 || tp->pr_wanted == 0)
    420  1.1      cgd 			continue;
    421  1.1      cgd 		if (sflag) {
    422  1.1      cgd 			if (tp->pr_stats)
    423  1.1      cgd 				(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    424  1.1      cgd 					p->p_name);
    425  1.1      cgd 		} else
    426  1.1      cgd 			if (tp->pr_cblocks)
    427  1.1      cgd 				(*tp->pr_cblocks)(nl[tp->pr_index].n_value,
    428  1.1      cgd 					p->p_name);
    429  1.1      cgd 	}
    430  1.1      cgd 	endprotoent();
    431  1.1      cgd     }
    432  1.1      cgd     if (af == AF_NS || af == AF_UNSPEC) {
    433  1.1      cgd #ifdef NS
    434  1.1      cgd 	for (tp = nsprotox; tp->pr_name; tp++) {
    435  1.1      cgd 		if (sflag) {
    436  1.1      cgd 			if (tp->pr_stats)
    437  1.1      cgd 				(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    438  1.1      cgd 					tp->pr_name);
    439  1.1      cgd 		} else
    440  1.1      cgd 			if (tp->pr_cblocks)
    441  1.1      cgd 				(*tp->pr_cblocks)(nl[tp->pr_index].n_value,
    442  1.1      cgd 					tp->pr_name);
    443  1.1      cgd 	}
    444  1.1      cgd #endif
    445  1.1      cgd     }
    446  1.1      cgd     if (af == AF_ISO || af == AF_UNSPEC) {
    447  1.1      cgd #ifdef ISO
    448  1.1      cgd 	for (tp = isoprotox; tp->pr_name; tp++) {
    449  1.1      cgd 		if (sflag) {
    450  1.1      cgd 			if (tp->pr_stats)
    451  1.1      cgd 				(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    452  1.1      cgd 					tp->pr_name);
    453  1.1      cgd 		} else
    454  1.1      cgd 			if (tp->pr_cblocks)
    455  1.1      cgd 				(*tp->pr_cblocks)(nl[tp->pr_index].n_value,
    456  1.1      cgd 					tp->pr_name);
    457  1.1      cgd 	}
    458  1.1      cgd #endif
    459  1.1      cgd     }
    460  1.1      cgd     if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
    461  1.1      cgd 	    unixpr((off_t)nl[N_FILEHEAD].n_value, (off_t)nl[N_FILES].n_value,
    462  1.1      cgd 		(struct protosw *)nl[N_UNIXSW].n_value);
    463  1.1      cgd     if (af == AF_UNSPEC && sflag)
    464  1.1      cgd #ifdef IMP
    465  1.1      cgd 	impstats(nl[N_IMP].n_value, nl[N_NIMP].n_value);
    466  1.1      cgd #endif
    467  1.1      cgd     exit(0);
    468  1.1      cgd }
    469  1.1      cgd 
    470  1.1      cgd char *
    471  1.1      cgd plural(n)
    472  1.1      cgd 	int n;
    473  1.1      cgd {
    474  1.1      cgd 	return (n != 1 ? "s" : "");
    475  1.1      cgd }
    476  1.1      cgd 
    477  1.1      cgd /*
    478  1.1      cgd  * Find the protox for the given "well-known" name.
    479  1.1      cgd  */
    480  1.1      cgd struct protox *
    481  1.1      cgd knownname(name)
    482  1.1      cgd 	char *name;
    483  1.1      cgd {
    484  1.1      cgd 	struct protox **tpp, *tp;
    485  1.1      cgd 
    486  1.1      cgd 	for (tpp = protoprotox; *tpp; tpp++)
    487  1.1      cgd 	    for (tp = *tpp; tp->pr_name; tp++)
    488  1.1      cgd 		if (strcmp(tp->pr_name, name) == 0)
    489  1.1      cgd 			return(tp);
    490  1.1      cgd 	return(NULL);
    491  1.1      cgd }
    492  1.1      cgd 
    493  1.1      cgd /*
    494  1.1      cgd  * Find the protox corresponding to name.
    495  1.1      cgd  */
    496  1.1      cgd struct protox *
    497  1.1      cgd name2protox(name)
    498  1.1      cgd 	char *name;
    499  1.1      cgd {
    500  1.1      cgd 	struct protox *tp;
    501  1.1      cgd 	char **alias;			/* alias from p->aliases */
    502  1.1      cgd 	struct protoent *p;
    503  1.1      cgd 
    504  1.1      cgd 	/*
    505  1.1      cgd 	 * Try to find the name in the list of "well-known" names. If that
    506  1.1      cgd 	 * fails, check if name is an alias for an Internet protocol.
    507  1.1      cgd 	 */
    508  1.1      cgd 	if (tp = knownname(name))
    509  1.1      cgd 		return(tp);
    510  1.1      cgd 
    511  1.1      cgd 	setprotoent(1);			/* make protocol lookup cheaper */
    512  1.1      cgd 	while (p = getprotoent()) {
    513  1.1      cgd 		/* assert: name not same as p->name */
    514  1.1      cgd 		for (alias = p->p_aliases; *alias; alias++)
    515  1.1      cgd 			if (strcmp(name, *alias) == 0) {
    516  1.1      cgd 				endprotoent();
    517  1.1      cgd 				return(knownname(p->p_name));
    518  1.1      cgd 			}
    519  1.1      cgd 	}
    520  1.1      cgd 	endprotoent();
    521  1.1      cgd 	return(NULL);
    522  1.1      cgd }
    523  1.1      cgd 
    524  1.1      cgd void
    525  1.1      cgd usage()
    526  1.1      cgd {
    527  1.1      cgd 	(void)fprintf(stderr,
    528  1.4   brezak "usage: netstat [-Aan] [-f address_family] [-c core] [-N system]\n");
    529  1.1      cgd 	(void)fprintf(stderr,
    530  1.4   brezak "               [-himnrs] [-f address_family] [-c core] [-N system]\n");
    531  1.1      cgd 	(void)fprintf(stderr,
    532  1.4   brezak "               [-n] [-I interface] [-c core] [-N system] [-w wait]\n");
    533  1.1      cgd 	(void)fprintf(stderr,
    534  1.4   brezak "               [-c core] [-N system] [-p protocol]\n");
    535  1.1      cgd 	exit(1);
    536  1.1      cgd }
    537