Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.78
      1 /*	$NetBSD: sysctl.c,v 1.78 2004/02/19 06:40:14 atatat Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  *	All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Brown.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. Neither the name of the University nor the names of its contributors
     48  *    may be used to endorse or promote products derived from this software
     49  *    without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     61  * SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #ifndef lint
     66 __COPYRIGHT(
     67 "@(#) Copyright (c) 1993\n\
     68 	The Regents of the University of California.  All rights reserved.\n");
     69 #endif /* not lint */
     70 
     71 #ifndef lint
     72 #if 0
     73 static char sccsid[] = "@(#)sysctl.c	8.1 (Berkeley) 6/6/93";
     74 #else
     75 __RCSID("$NetBSD: sysctl.c,v 1.78 2004/02/19 06:40:14 atatat Exp $");
     76 #endif
     77 #endif /* not lint */
     78 
     79 #include <sys/types.h>
     80 #include <sys/param.h>
     81 #include <sys/sysctl.h>
     82 #include <sys/mount.h>
     83 #include <sys/resource.h>
     84 #include <sys/stat.h>
     85 #include <sys/sched.h>
     86 #include <sys/socket.h>
     87 #include <netinet/in.h>
     88 #include <netinet/ip_var.h>
     89 #include <netinet/tcp.h>
     90 #include <netinet/tcp_timer.h>
     91 #include <netinet/tcp_var.h>
     92 #include <netinet/icmp6.h>
     93 #include <nfs/rpcv2.h>
     94 #include <nfs/nfsproto.h>
     95 #include <nfs/nfs.h>
     96 #include <machine/cpu.h>
     97 #include <netkey/key_var.h>
     98 
     99 #include <assert.h>
    100 #include <ctype.h>
    101 #include <err.h>
    102 #include <errno.h>
    103 #include <inttypes.h>
    104 #include <stdarg.h>
    105 #include <stdio.h>
    106 #include <stdlib.h>
    107 #include <string.h>
    108 #include <time.h>
    109 #include <unistd.h>
    110 
    111 /*
    112  * this needs to be able to do the printing and the setting
    113  */
    114 #define HANDLER_PROTO const char *, const char *, char *, \
    115 	int *, u_int, const struct sysctlnode *, \
    116 	u_int, void *
    117 #define HANDLER_ARGS const char *sname, const char *dname, char *value, \
    118 	int *name, u_int namelen, const struct sysctlnode *pnode, \
    119 	u_int type, void *v
    120 #define DISPLAY_VALUE	0
    121 #define DISPLAY_OLD	1
    122 #define DISPLAY_NEW	2
    123 
    124 /*
    125  * generic routines
    126  */
    127 static struct handlespec *findprinter(const int *, u_int);
    128 static struct handlespec *findwriter(const int *, u_int);
    129 static void print_tree(int *, u_int, struct sysctlnode *, u_int, int);
    130 static void write_number(int *, u_int, struct sysctlnode *, char *);
    131 static void write_string(int *, u_int, struct sysctlnode *, char *);
    132 static void display_number(const struct sysctlnode *, const char *,
    133 			   const void *, size_t, int);
    134 static void display_string(const struct sysctlnode *, const char *,
    135 			   const void *, size_t, int);
    136 static void display_struct(const struct sysctlnode *, const char *,
    137 			   const void *, size_t, int);
    138 static void hex_dump(const unsigned char *, size_t);
    139 static void usage(void);
    140 static void parse(char *);
    141 static void cparse(char *);
    142 static void dparse(char *);
    143 static void sysctlerror(int);
    144 
    145 /*
    146  * unexported from some place else (XXX tbd)
    147  */
    148 int learn_tree(int *, u_int, struct sysctlnode *);
    149 int sysctlbyname(const char *, void *, size_t *, void *, size_t);
    150 int sysctlgetmibinfo(const char *, int *, u_int *,
    151 		     char *, size_t *, struct sysctlnode **);
    152 
    153 /*
    154  * "handlers"
    155  */
    156 static void printother(HANDLER_PROTO);
    157 static void kern_clockrate(HANDLER_PROTO);
    158 static void kern_boottime(HANDLER_PROTO);
    159 static void kern_consdev(HANDLER_PROTO);
    160 static void kern_cp_time(HANDLER_PROTO);
    161 static void vm_loadavg(HANDLER_PROTO);
    162 static void proc_limit(HANDLER_PROTO);
    163 #ifdef CPU_DISKINFO
    164 static void machdep_diskinfo(HANDLER_PROTO);
    165 #endif /* CPU_DISKINFO */
    166 
    167 struct handlespec {
    168 	int ps_name[CTL_MAXNAME];
    169 	void (*ps_p)(HANDLER_PROTO);
    170 	void (*ps_w)(HANDLER_PROTO);
    171 	void *ps_d;
    172 } handlers[] = {
    173 	{ { CTL_KERN, KERN_CLOCKRATE },	kern_clockrate },
    174 	{ { CTL_KERN, KERN_VNODE },	printother,	NULL,	"pstat" },
    175 	{ { CTL_KERN, KERN_PROC },	printother,	NULL,	"ps" },
    176 	{ { CTL_KERN, KERN_PROC2 },	printother,	NULL,	"ps" },
    177 	{ { CTL_KERN, KERN_PROC_ARGS },	printother,	NULL,	"ps" },
    178 	{ { CTL_KERN, KERN_FILE },	printother,	NULL,	"pstat" },
    179 	{ { CTL_KERN, KERN_NTPTIME },	printother,	NULL,
    180 	  "ntpdc -c kerninfo" },
    181 	{ { CTL_KERN, KERN_MSGBUF },	printother,	NULL,	"dmesg" },
    182 	{ { CTL_KERN, KERN_BOOTTIME },	kern_boottime },
    183 	{ { CTL_KERN, KERN_CONSDEV },	kern_consdev },
    184 	{ { CTL_KERN, KERN_CP_TIME },	kern_cp_time },
    185 	{ { CTL_KERN, KERN_SYSVIPC_INFO }, printother,	NULL,	"ipcs" },
    186 	{ { CTL_VM,   VM_METER },	printother,	NULL,
    187 	  "vmstat' or 'systat" },
    188 	{ { CTL_VM,   VM_LOADAVG },	vm_loadavg },
    189 	{ { CTL_VM,   VM_UVMEXP },	printother,	NULL,
    190 	  "vmstat' or 'systat" },
    191 	{ { CTL_VM,   VM_UVMEXP2 },	printother,	NULL,
    192 	  "vmstat' or 'systat" },
    193 	{ { CTL_VFS,  2 /* NFS */, NFS_NFSSTATS },
    194 					printother,	NULL,	"nfsstat" },
    195 	{ { CTL_NET },			printother,	NULL,	NULL },
    196 	{ { CTL_NET, PF_LOCAL },	printother,	NULL,	NULL },
    197 	{ { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_IDENT },
    198 					printother,	NULL,	"identd" },
    199 	{ { CTL_NET, PF_INET6, IPPROTO_TCP, TCPCTL_IDENT },
    200 					printother,	NULL,	"identd" },
    201 
    202 	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST },
    203 					printother,	NULL,	"something else" },
    204 	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST },
    205 					printother,	NULL,	"something else" },
    206 
    207 
    208 	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSA },
    209 					printother,	NULL,	"setkey" },
    210 	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSP },
    211 					printother,	NULL,	"setkey" },
    212 	/* { { CTL_DEBUG },		printother,	NULL,	NULL }, */
    213 	{ { CTL_HW,   HW_DISKSTATS },	printother,	NULL,	"iostat" },
    214 #ifdef CPU_CONSDEV
    215 	{ { CTL_MACHDEP, CPU_CONSDEV },	kern_consdev },
    216 #endif /* CPU_CONSDEV */
    217 #ifdef CPU_DISKINFO
    218 	{ { CTL_MACHDEP, CPU_DISKINFO },machdep_diskinfo },
    219 #endif /* CPU_CONSDEV */
    220 	{ { CTL_DDB },			printother,	NULL,	NULL },
    221 	{ { CTL_PROC, -1, PROC_PID_LIMIT, -1, -1 }, proc_limit, proc_limit },
    222 	{ { CTL_UNSPEC }, },
    223 };
    224 
    225 struct sysctlnode my_root = {
    226 #if defined(lint)
    227 	0
    228 #else /* defined(lint) */
    229 	.sysctl_flags = SYSCTL_ROOT|
    230 	    SYSCTL_TYPE(CTLTYPE_NODE),
    231 	.sysctl_size = sizeof(struct sysctlnode),
    232 	.sysctl_num = 0,
    233 	.sysctl_name = "(prog_root)",
    234 #endif /* defined(lint) */
    235 };
    236 
    237 int	Aflag, aflag, Mflag, nflag, qflag, rflag, wflag, xflag;
    238 int	req;
    239 FILE	*warnfp = stderr;
    240 
    241 /*
    242  * vah-riables n stuff
    243  */
    244 char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    245 	gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
    246 char sep[2] = ".", *eq = " = ";
    247 const char *lname[] = {
    248 	"top", "second", "third", "fourth", "fifth", "sixth",
    249 	"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
    250 };
    251 
    252 /*
    253  * you've heard of main, haven't you?
    254  */
    255 int
    256 main(int argc, char *argv[])
    257 {
    258 	char *fn = NULL;
    259 	int name[CTL_MAXNAME];
    260 	int ch;
    261 
    262 	while ((ch = getopt(argc, argv, "Aaef:Mnqrwx")) != -1) {
    263 		switch (ch) {
    264 		case 'A':
    265 			Aflag++;
    266 			break;
    267 		case 'a':
    268 			aflag++;
    269 			break;
    270 		case 'e':
    271 			eq = "=";
    272 			break;
    273 		case 'f':
    274 			fn = optarg;
    275 			wflag++;
    276 			break;
    277 		case 'M':
    278 			Mflag++;
    279 			break;
    280 		case 'n':
    281 			nflag++;
    282 			break;
    283 		case 'q':
    284 			qflag++;
    285 			break;
    286 		case 'r':
    287 			rflag++;
    288 			break;
    289 		case 'w':
    290 			wflag++;
    291 			break;
    292 		case 'x':
    293 			xflag++;
    294 			break;
    295 		default:
    296 			usage();
    297 		}
    298 	}
    299 
    300 	argc -= optind;
    301 	argv += optind;
    302 
    303 	if (qflag && !wflag)
    304 		usage();
    305 	if (xflag && rflag)
    306 		usage();
    307 	/* if ((xflag || rflag) && wflag)
    308 		usage(); */
    309 	/* if (aflag && Mflag)
    310 		usage(); */
    311 	if ((Aflag || Mflag) && argc == 0)
    312 		aflag = 1;
    313 
    314 	if (Aflag)
    315 		warnfp = stdout;
    316 	req = 0;
    317 
    318 	if (aflag) {
    319 		print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1);
    320 		/* if (argc == 0) */
    321 		return (0);
    322 	}
    323 
    324 	if (fn) {
    325 		FILE *fp;
    326 		char *l;
    327 
    328 		fp = fopen(fn, "r");
    329 		if (fp == NULL) {
    330 			err(1, "%s", fn);
    331 		} else {
    332 			while ((l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL)
    333 			{
    334 				if (*l) {
    335 					parse(l);
    336 					free(l);
    337 				}
    338 			}
    339 			fclose(fp);
    340 		}
    341 		return (0);
    342 	}
    343 
    344 	if (argc == 0)
    345 		usage();
    346 
    347 	while (argc-- > 0)
    348 		parse(*argv++);
    349 
    350 	return (0);
    351 }
    352 
    353 /*
    354  * ********************************************************************
    355  * how to find someone special to handle the reading (or maybe even
    356  * writing) of a particular node
    357  * ********************************************************************
    358  */
    359 static struct handlespec *
    360 findprinter(const int *name, u_int namelen)
    361 {
    362 	struct handlespec *p;
    363 	int i, j;
    364 
    365 	if (namelen < 1)
    366 		return (NULL);
    367 
    368 	p = &handlers[0];
    369 	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
    370 		for (j = 0; j < namelen; j++)
    371 			if (p[i].ps_name[j] != name[j] &&
    372 			    p[i].ps_name[j] != -1)
    373 				break;
    374 		if (j == namelen && p[i].ps_p != NULL)
    375 			return (&p[i]);
    376 	}
    377 
    378 	return (NULL);
    379 }
    380 
    381 static struct handlespec *
    382 findwriter(const int *name, u_int namelen)
    383 {
    384 	struct handlespec *p;
    385 	int i, j;
    386 
    387         if (namelen < 1)
    388                 return (NULL);
    389 
    390 	p = &handlers[0];
    391 	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
    392 		for (j = 0; j < namelen; j++)
    393 			if (p[i].ps_name[j] != name[j] &&
    394 			    p[i].ps_name[j] != -1)
    395 				break;
    396 		if (j == namelen && p[i].ps_w != NULL)
    397 			return (&p[i]);
    398 	}
    399 
    400 	return (NULL);
    401 }
    402 
    403 /*
    404  * ********************************************************************
    405  * convert this special number to a special string so we can print the
    406  * mib
    407  * ********************************************************************
    408  */
    409 static const char *
    410 sf(u_int f)
    411 {
    412 	static char s[256];
    413 	char *c;
    414 
    415 	s[0] = '\0';
    416 	c = "";
    417 
    418 #define print_flag(_f, _s, _c, _q, _x) \
    419 	if ((/*CONSTCOND*/_x) ? \
    420 	    (((_f) & (_x)) == (__CONCAT(SYSCTL_,_q))) : \
    421 	    ((_f) & (__CONCAT(SYSCTL_,_q)))) { \
    422 		strlcat((_s), (_c), sizeof(_s)); \
    423 		strlcat((_s), __STRING(_q), sizeof(_s)); \
    424 		(_c) = ","; \
    425 		(_f) &= ~(__CONCAT(SYSCTL_,_q)|(_x)); \
    426 	}
    427 	print_flag(f, s, c, READONLY, SYSCTL_READWRITE);
    428 	print_flag(f, s, c, READONLY1, SYSCTL_READWRITE);
    429 	print_flag(f, s, c, READONLY2, SYSCTL_READWRITE);
    430 	print_flag(f, s, c, READWRITE, SYSCTL_READWRITE);
    431 	print_flag(f, s, c, ANYWRITE, 0);
    432 	print_flag(f, s, c, PRIVATE, 0);
    433 	print_flag(f, s, c, PERMANENT, 0);
    434 	print_flag(f, s, c, OWNDATA, 0);
    435 	print_flag(f, s, c, IMMEDIATE, 0);
    436 	print_flag(f, s, c, HEX, 0);
    437 	print_flag(f, s, c, ROOT, 0);
    438 	print_flag(f, s, c, ANYNUMBER, 0);
    439 	print_flag(f, s, c, HIDDEN, 0);
    440 	print_flag(f, s, c, ALIAS, 0);
    441 #undef print_flag
    442 
    443 	if (f) {
    444 		char foo[9];
    445 		snprintf(foo, sizeof(foo), "%x", f);
    446 		strlcat(s, c, sizeof(s));
    447 		strlcat(s, foo, sizeof(s));
    448 	}
    449 
    450 	return (s);
    451 }
    452 
    453 static const char *
    454 st(u_int t)
    455 {
    456 
    457 	switch (t) {
    458 	case CTLTYPE_NODE:
    459 		return "NODE";
    460 	case CTLTYPE_INT:
    461 		return "INT";
    462 	case CTLTYPE_STRING:
    463 		return "STRING";
    464 	case CTLTYPE_QUAD:
    465                 return "QUAD";
    466 	case CTLTYPE_STRUCT:
    467 		return "STRUCT";
    468 	}
    469 
    470 	return "???";
    471 }
    472 
    473 /*
    474  * ********************************************************************
    475  * print this node and any others underneath it
    476  * ********************************************************************
    477  */
    478 static void
    479 print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
    480 	   int add)
    481 {
    482 	struct sysctlnode *node;
    483 	int rc, ni;
    484 	size_t sz;
    485 	char *sp, *dp, n[20];
    486 	struct handlespec *p;
    487 
    488 	sp = &gsname[strlen(gsname)];
    489 	dp = &gdname[strlen(gdname)];
    490 
    491 	if (sp != &gsname[0] && dp == &gdname[0]) {
    492 		/*
    493 		 * aw...shucks.  now we must play catch up
    494 		 */
    495 		for (ni = 0; ni < namelen; ni++) {
    496 			(void)snprintf(n, sizeof(n), "%d", name[ni]);
    497 			if (ni > 0)
    498 				strncat(gdname, ".", sizeof(gdname));
    499 			strncat(gdname, n, sizeof(gdname));
    500 		}
    501 	}
    502 
    503 	if (pnode == NULL)
    504 		pnode = &my_root;
    505 	else if (add) {
    506 		snprintf(n, sizeof(n), "%d", pnode->sysctl_num);
    507 		if (namelen > 1) {
    508 			strncat(gsname, sep, sizeof(gsname));
    509 			strncat(gdname, ".", sizeof(gdname));
    510 		}
    511 		strncat(gsname, pnode->sysctl_name, sizeof(gsname));
    512 		strncat(gdname, n, sizeof(gdname));
    513 	}
    514 
    515 	if (Mflag && pnode != &my_root) {
    516 		if (nflag)
    517 			printf("%s: ", gdname);
    518 		else
    519 			printf("%s (%s): ", gsname, gdname);
    520 		printf("CTLTYPE_%s", st(type));
    521 		if (type == CTLTYPE_NODE) {
    522 			if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS)
    523 				printf(", alias %d",
    524 				       pnode->sysctl_alias);
    525 			else
    526 				printf(", children %d/%d",
    527 				       pnode->sysctl_clen,
    528 				       pnode->sysctl_csize);
    529 		}
    530 		printf(", size %zu", pnode->sysctl_size);
    531 		printf(", flags 0x%x<%s>",
    532 		       SYSCTL_FLAGS(pnode->sysctl_flags),
    533 		       sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
    534 		if (pnode->sysctl_func)
    535 			printf(", func=%p", pnode->sysctl_func);
    536 		printf(", ver=%d", pnode->sysctl_ver);
    537 		printf("\n");
    538 		if (type != CTLTYPE_NODE) {
    539 			*sp = *dp = '\0';
    540 			return;
    541 		}
    542 	}
    543 
    544 	/*
    545 	 * if this is an alias and we added our name, that means we
    546 	 * got here by recursing down into the tree, so skip it.  The
    547 	 * only way to print an aliased node is with either -M or by
    548 	 * name specifically.
    549 	 */
    550 	if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS && add) {
    551 		*sp = *dp = '\0';
    552 		return;
    553 	}
    554 
    555 	p = findprinter(name, namelen);
    556 	if (type != CTLTYPE_NODE && p != NULL) {
    557 		(*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
    558 			   p->ps_d);
    559 		*sp = *dp = '\0';
    560 		return;
    561 	}
    562 
    563 	if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
    564 		rc = sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
    565 		if (rc == -1) {
    566 			sysctlerror(1);
    567 			*sp = *dp = '\0';
    568 			return;
    569 		}
    570 		if (sz == 0) {
    571 			if ((Aflag || req) && !Mflag)
    572 				printf("%s: node contains no data\n", gsname);
    573 			*sp = *dp = '\0';
    574 			return;
    575 		}
    576 	}
    577 	else
    578 		sz = pnode->sysctl_size;
    579 
    580 	switch (type) {
    581 	case CTLTYPE_NODE: {
    582 		learn_tree(name, namelen, pnode);
    583 		node = pnode->sysctl_child;
    584 		if (node == NULL) {
    585 			if (p != NULL)
    586 				(*p->ps_p)(gsname, gdname, NULL, name, namelen,
    587 					   pnode, type, p->ps_d);
    588 			else if ((Aflag || req) && !Mflag)
    589 				printf("%s: no children\n", gsname);
    590 		}
    591 		else {
    592 			req = 0;
    593 			for (ni = 0; ni < pnode->sysctl_clen; ni++) {
    594 				name[namelen] = node[ni].sysctl_num;
    595 				if ((node[ni].sysctl_flags & SYSCTL_HIDDEN) &&
    596 				    !(Aflag || req))
    597 					continue;
    598 				print_tree(name, namelen + 1, &node[ni],
    599 					   SYSCTL_TYPE(node[ni].sysctl_flags),
    600 					   1);
    601 			}
    602 		}
    603 		break;
    604 	}
    605 	case CTLTYPE_INT: {
    606 		int i;
    607 		rc = sysctl(name, namelen, &i, &sz, NULL, 0);
    608 		if (rc == -1) {
    609 			sysctlerror(1);
    610 			break;
    611 		}
    612 		display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
    613 		break;
    614 	}
    615 	case CTLTYPE_STRING: {
    616 		unsigned char buf[1024], *tbuf;
    617 		tbuf = buf;
    618 		sz = sizeof(buf);
    619 		rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    620 		if (rc == -1 && errno == ENOMEM) {
    621 			tbuf = malloc(sz);
    622 			if (tbuf == NULL) {
    623 				sysctlerror(1);
    624 				break;
    625 			}
    626 			rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    627 		}
    628 		if (rc == -1)
    629 			sysctlerror(1);
    630 		else
    631 			display_string(pnode, gsname, buf, sz, DISPLAY_VALUE);
    632 		if (tbuf != buf)
    633 			free(tbuf);
    634 		break;
    635 	}
    636 	case CTLTYPE_QUAD: {
    637 		u_quad_t q;
    638 		sz = sizeof(q);
    639 		rc = sysctl(&name[0], namelen, &q, &sz, NULL, 0);
    640 		if (rc == -1) {
    641 			sysctlerror(1);
    642 			break;
    643 		}
    644 		display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
    645 		break;
    646 	}
    647 	case CTLTYPE_STRUCT: {
    648 		/*
    649 		 * we shouldn't actually get here, but if we
    650 		 * do, would it be nice to have *something* to
    651 		 * do other than completely ignore the
    652 		 * request.
    653 		 */
    654 		unsigned char *d;
    655 		if ((d = malloc(sz)) == NULL) {
    656 			fprintf(warnfp, "%s: !malloc failed!\n", gsname);
    657 			break;
    658 		}
    659 		rc = sysctl(&name[0], namelen, d, &sz, NULL, 0);
    660 		if (rc == -1) {
    661 			sysctlerror(1);
    662 			break;
    663 		}
    664 		display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
    665 		free(d);
    666 		break;
    667 	}
    668 	default:
    669 		/* should i print an error here? */
    670 		break;
    671 	}
    672 
    673 	*sp = *dp = '\0';
    674 }
    675 
    676 /*
    677  * ********************************************************************
    678  * parse a request, possibly determining that it's a create or destroy
    679  * request
    680  * ********************************************************************
    681  */
    682 static void
    683 parse(char *l)
    684 {
    685 	struct sysctlnode *node;
    686 	struct handlespec *w;
    687 	int name[CTL_MAXNAME];
    688 	u_int namelen, type;
    689 	char *key, *value, *dot;
    690 	size_t sz;
    691 
    692 	req = 1;
    693 	key = l;
    694 	value = strchr(l, '=');
    695 	if (value != NULL) {
    696 		if (!wflag) {
    697 			fprintf(warnfp,
    698 				"%s: Must specify -w to set variables\n",
    699 				getprogname());
    700 			exit(1);
    701 		}
    702 		*value++ = '\0';
    703 	}
    704 
    705 	if ((dot = strpbrk(key, "./")) == NULL)
    706 		sep[0] = '.';
    707 	else
    708 		sep[0] = dot[0];
    709 	sep[1] = '\0';
    710 
    711 	if (key[0] == sep[0] && key[1] == sep[0]) {
    712 		if (value != NULL)
    713 			value[-1] = '=';
    714 		if (strncmp(key + 2, "create=", 7) == 0)
    715 			cparse(key + 9);
    716 		else if (strncmp(key + 2, "destroy=", 8) == 0)
    717 			dparse(key + 10);
    718 		else
    719 			fprintf(warnfp, "%s: unable to parse '%s'\n",
    720 				getprogname(), key);
    721 		return;
    722 	}
    723 
    724 	node = &my_root;
    725 	namelen = CTL_MAXNAME;
    726 	sz = sizeof(gsname);
    727 
    728 	if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node) == -1)
    729 	{
    730 		fprintf(warnfp, "%s: %s level name '%s' in '%s' is invalid\n",
    731 			getprogname(), lname[namelen], gsname, l);
    732 		exit(1);
    733 	}
    734 
    735 	type = SYSCTL_TYPE(node->sysctl_flags);
    736 
    737 	if (value == NULL) {
    738 		print_tree(&name[0], namelen, node, type, 0);
    739 		gsname[0] = '\0';
    740 		return;
    741 	}
    742 
    743 	if (type != CTLTYPE_NODE && (w = findwriter(name, namelen)) != NULL) {
    744 		(*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
    745 			   w->ps_d);
    746 		gsname[0] = '\0';
    747 		return;
    748 	}
    749 
    750 	switch (type) {
    751 	case CTLTYPE_NODE:
    752 		/*
    753 		 * XXX old behavior is to print.  should we error instead?
    754 		 */
    755 		print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1);
    756 		break;
    757 	case CTLTYPE_INT:
    758 		write_number(&name[0], namelen, node, value);
    759 		break;
    760 	case CTLTYPE_STRING:
    761 		write_string(&name[0], namelen, node, value);
    762 		break;
    763 	case CTLTYPE_QUAD:
    764 		write_number(&name[0], namelen, node, value);
    765 		break;
    766 	case CTLTYPE_STRUCT:
    767 		/*
    768 		 * XXX old behavior is to print.  should we error instead?
    769 		 */
    770 		/* fprintf(warnfp, "you can't write to %s\n", gsname); */
    771 		print_tree(&name[0], namelen, node, type, 0);
    772 		break;
    773 	}
    774 }
    775 
    776 /*
    777 
    778   //create=foo.bar.whatever...,
    779   [type=(int|quad|string|struct|node),]
    780   [size=###,]
    781   [n=###,]
    782   [flags=(tiohxparw12),]
    783   [addr=0x####,|symbol=...|value=...]
    784 
    785   size is optional for some types.  type must be set before anything
    786   else.  nodes can have [r12whp], but nothing else applies.  if no
    787   size or type is given, node is asserted.  writeable is the default,
    788   with [r12w] being read-only, writeable below securelevel 1,
    789   writeable below securelevel 2, and unconditionally writeable
    790   respectively.  if you specify addr, it is assumed to be the name of
    791   a kernel symbol, if value, SYSCTL_OWNDATA will be asserted for
    792   strings, SYSCTL_IMMEDIATE for ints and u_quad_ts.  you cannot
    793   specify both value and addr.
    794 
    795 */
    796 
    797 static void
    798 cparse(char *l)
    799 {
    800 	struct sysctlnode node;
    801 	size_t sz;
    802 	char *nname, *key, *value, *data, *addr, *c, *t;
    803 	int name[CTL_MAXNAME], i, rc, method, flags, rw;
    804 	u_int namelen, type;
    805 	u_quad_t q;
    806 	long li, lo;
    807 
    808 	/*
    809 	 * these are the pieces that make up the description of a new
    810 	 * node
    811 	 */
    812 	memset(&node, 0, sizeof(node));
    813 	node.sysctl_num = CTL_CREATE;  /* any number is fine */
    814 	flags = 0;
    815 	rw = -1;
    816 	type = 0;
    817 	sz = 0;
    818 	data = addr = NULL;
    819 	memset(name, 0, sizeof(name));
    820 	namelen = 0;
    821 	method = 0;
    822 
    823 	/*
    824 	 * misc stuff used when constructing
    825 	 */
    826 	i = 0;
    827 	q = 0;
    828 	key = NULL;
    829 	value = NULL;
    830 
    831 	/*
    832 	 * the name of the thing we're trying to create is first, so
    833 	 * pick it off.
    834 	 */
    835 	nname = l;
    836 	if ((c = strchr(nname, ',')) != NULL)
    837 		*c++ = '\0';
    838 
    839 	while (c != NULL) {
    840 
    841 		/*
    842 		 * pull off the next "key=value" pair
    843 		 */
    844 		key = c;
    845 		if ((t = strchr(key, '=')) != NULL) {
    846 			*t++ = '\0';
    847 			value = t;
    848 		}
    849 		else
    850 			value = NULL;
    851 
    852 		/*
    853 		 * if the "key" is "value", then that eats the rest of
    854 		 * the string, so we're done, otherwise bite it off at
    855 		 * the next comma.
    856 		 */
    857 		if (strcmp(key, "value") == 0) {
    858 			c = NULL;
    859 			data = value;
    860 			break;
    861 		}
    862 		else {
    863 			if ((c = strchr(value, ',')) != NULL)
    864 				*c++ = '\0';
    865 		}
    866 
    867 		/*
    868 		 * note that we (mostly) let the invoker of sysctl(8)
    869 		 * play rampant here and depend on the kernel to tell
    870 		 * them that they were wrong.  well...within reason.
    871 		 * we later check the various parameters against each
    872 		 * other to make sure it makes some sort of sense.
    873 		 */
    874 		if (strcmp(key, "addr") == 0) {
    875 			/*
    876 			 * we can't check these two.  only the kernel
    877 			 * can tell us when it fails to find the name
    878 			 * (or if the address is invalid).
    879 			 */
    880 			if (method != 0) {
    881 				fprintf(warnfp,
    882 				    "%s: %s: already have %s for new node\n",
    883 				    getprogname(), nname,
    884 				    method == CTL_CREATE ? "addr" : "symbol");
    885 				exit(1);
    886 			}
    887 			errno = 0;
    888 			addr = (void*)strtoul(value, &t, 0);
    889 			if (*t != '\0' || errno != 0) {
    890 				fprintf(warnfp,
    891 				    "%s: %s: '%s' is not a valid address\n",
    892 				    getprogname(), nname, value);
    893 				exit(1);
    894 			}
    895 			method = CTL_CREATE;
    896 		}
    897 		else if (strcmp(key, "symbol") == 0) {
    898 			if (method != 0) {
    899 				fprintf(warnfp,
    900 				    "%s: %s: already have %s for new node\n",
    901 				    getprogname(), nname,
    902 				    method == CTL_CREATE ? "addr" : "symbol");
    903 				exit(1);
    904 			}
    905 			addr = value;
    906 			method = CTL_CREATESYM;
    907 		}
    908 		else if (strcmp(key, "type") == 0) {
    909 			if (strcmp(value, "node") == 0)
    910 				type = CTLTYPE_NODE;
    911 			else if (strcmp(value, "int") == 0) {
    912 				sz = sizeof(int);
    913 				type = CTLTYPE_INT;
    914 			}
    915 			else if (strcmp(value, "string") == 0)
    916 				type = CTLTYPE_STRING;
    917 			else if (strcmp(value, "quad") == 0) {
    918 				sz = sizeof(u_quad_t);
    919 				type = CTLTYPE_QUAD;
    920 			}
    921 			else if (strcmp(value, "struct") == 0)
    922 				type = CTLTYPE_STRUCT;
    923 			else {
    924 				fprintf(warnfp,
    925 					"%s: %s: '%s' is not a valid type\n",
    926 					getprogname(), nname, value);
    927 				exit(1);
    928 			}
    929 		}
    930 		else if (strcmp(key, "size") == 0) {
    931 			errno = 0;
    932 			/*
    933 			 * yes, i know size_t is not an unsigned long,
    934 			 * but we can all agree that it ought to be,
    935 			 * right?
    936 			 */
    937 			sz = strtoul(value, &t, 0);
    938 			if (*t != '\0' || errno != 0) {
    939 				fprintf(warnfp,
    940 					"%s: %s: '%s' is not a valid size\n",
    941 					getprogname(), nname, value);
    942 				exit(1);
    943 			}
    944 		}
    945 		else if (strcmp(key, "n") == 0) {
    946 			errno = 0;
    947 			li = strtol(value, &t, 0);
    948 			node.sysctl_num = li;
    949 			lo = node.sysctl_num;
    950 			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
    951 				fprintf(warnfp,
    952 				    "%s: %s: '%s' is not a valid mib number\n",
    953 				    getprogname(), nname, value);
    954 				exit(1);
    955 			}
    956 		}
    957 		else if (strcmp(key, "flags") == 0) {
    958 			t = value;
    959 			while (*t != '\0') {
    960 				switch (*t) {
    961 				case 'a':
    962 					flags |= SYSCTL_ANYWRITE;
    963 					break;
    964 				case 'h':
    965 					flags |= SYSCTL_HIDDEN;
    966 					break;
    967 				case 'i':
    968 					flags |= SYSCTL_IMMEDIATE;
    969 					break;
    970 				case 'o':
    971 					flags |= SYSCTL_OWNDATA;
    972 					break;
    973 				case 'p':
    974 					flags |= SYSCTL_PRIVATE;
    975 					break;
    976 				case 'x':
    977 					flags |= SYSCTL_HEX;
    978 					break;
    979 
    980 				case 'r':
    981 					rw = SYSCTL_READONLY;
    982 					break;
    983 				case '1':
    984 					rw = SYSCTL_READONLY1;
    985 					break;
    986 				case '2':
    987 					rw = SYSCTL_READONLY2;
    988 					break;
    989 				case 'w':
    990 					rw = SYSCTL_READWRITE;
    991 					break;
    992 				default:
    993 					fprintf(warnfp,
    994 					   "%s: %s: '%c' is not a valid flag\n",
    995 					    getprogname(), nname, *t);
    996 					exit(1);
    997 				}
    998 				t++;
    999 			}
   1000 		}
   1001 		else {
   1002 			fprintf(warnfp, "%s: %s: unrecognized keyword '%s'\n",
   1003 				getprogname(), nname, key);
   1004 			exit(1);
   1005 		}
   1006 	}
   1007 
   1008 	/*
   1009 	 * now that we've finished parsing the given string, fill in
   1010 	 * anything they didn't specify
   1011 	 */
   1012 	if (type == 0)
   1013 		type = CTLTYPE_NODE;
   1014 
   1015 	/*
   1016 	 * the "data" can be interpreted various ways depending on the
   1017 	 * type of node we're creating, as can the size
   1018 	 */
   1019 	if (data != NULL) {
   1020 		if (addr != NULL) {
   1021 			fprintf(warnfp,
   1022 				"%s: %s: cannot specify both value and "
   1023 				"address\n", getprogname(), nname);
   1024 			exit(1);
   1025 		}
   1026 
   1027 		switch (type) {
   1028 		case CTLTYPE_INT:
   1029 			errno = 0;
   1030 			li = strtol(data, &t, 0);
   1031 			i = li;
   1032 			lo = i;
   1033 			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
   1034 				fprintf(warnfp,
   1035 					"%s: %s: '%s' is not a valid integer\n",
   1036 					getprogname(), nname, value);
   1037 				exit(1);
   1038 			}
   1039 			if (!(flags & SYSCTL_OWNDATA)) {
   1040 				flags |= SYSCTL_IMMEDIATE;
   1041 				node.sysctl_idata = i;
   1042 			}
   1043 			else
   1044 				node.sysctl_data = &i;
   1045 			if (sz == 0)
   1046 				sz = sizeof(int);
   1047 			break;
   1048 		case CTLTYPE_STRING:
   1049 			flags |= SYSCTL_OWNDATA;
   1050 			node.sysctl_data = data;
   1051 			if (sz == 0)
   1052 				sz = strlen(data) + 1;
   1053 			else if (sz < strlen(data) + 1) {
   1054 				fprintf(warnfp, "%s: %s: ignoring size=%zu for "
   1055 					"string node, too small for given "
   1056 					"value\n", getprogname(), nname, sz);
   1057 				sz = strlen(data) + 1;
   1058 			}
   1059 			break;
   1060 		case CTLTYPE_QUAD:
   1061 			errno = 0;
   1062 			q = strtouq(data, &t, 0);
   1063 			if (*t != '\0' || errno != 0) {
   1064 				fprintf(warnfp,
   1065 					"%s: %s: '%s' is not a valid quad\n",
   1066 					getprogname(), nname, value);
   1067 				exit(1);
   1068 			}
   1069 			if (!(flags & SYSCTL_OWNDATA)) {
   1070 				flags |= SYSCTL_IMMEDIATE;
   1071 				node.sysctl_qdata = q;
   1072 			}
   1073 			else
   1074 				node.sysctl_data = &q;
   1075 			if (sz == 0)
   1076 				sz = sizeof(u_quad_t);
   1077 			break;
   1078 		case CTLTYPE_STRUCT:
   1079 			fprintf(warnfp,
   1080 				"%s: %s: struct not initializable\n",
   1081 				getprogname(), nname);
   1082 			exit(1);
   1083 		}
   1084 
   1085 		/*
   1086 		 * these methods have all provided local starting
   1087 		 * values that the kernel must copy in
   1088 		 */
   1089 	}
   1090 
   1091 	/*
   1092 	 * hmm...no data, but we have an address of data.  that's
   1093 	 * fine.
   1094 	 */
   1095 	else if (addr != 0)
   1096 		node.sysctl_data = (void*)addr;
   1097 
   1098 	/*
   1099 	 * no data and no address?  well...okay.  we might be able to
   1100 	 * manage that.
   1101 	 */
   1102 	else if (type != CTLTYPE_NODE) {
   1103 		if (sz == 0) {
   1104 			fprintf(warnfp,
   1105 				"%s: %s: need a size or a starting value\n",
   1106 				getprogname(), nname);
   1107                         exit(1);
   1108                 }
   1109 		if (!(flags & SYSCTL_IMMEDIATE))
   1110 			flags |= SYSCTL_OWNDATA;
   1111 	}
   1112 
   1113 	/*
   1114 	 * now we do a few sanity checks on the description we've
   1115 	 * assembled
   1116 	 */
   1117 	if ((flags & SYSCTL_IMMEDIATE) &&
   1118 	    (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
   1119 		fprintf(warnfp,
   1120 			"%s: %s: cannot make an immediate %s\n",
   1121 			getprogname(), nname,
   1122 			(type == CTLTYPE_STRING) ? "string" : "struct");
   1123 		exit(1);
   1124 	}
   1125 	if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
   1126 		fprintf(warnfp, "%s: %s: nodes do not have data\n",
   1127 			getprogname(), nname);
   1128 		exit(1);
   1129 	}
   1130 
   1131 	/*
   1132 	 * some types must have a particular size
   1133 	 */
   1134 	if (sz != 0) {
   1135 		if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
   1136 		    (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
   1137 		    (type == CTLTYPE_NODE && sz != 0)) {
   1138 			fprintf(warnfp, "%s: %s: wrong size for type\n",
   1139 				getprogname(), nname);
   1140 			exit(1);
   1141 		}
   1142 	}
   1143 	else if (type == CTLTYPE_STRUCT) {
   1144 		fprintf(warnfp, "%s: %s: struct must have size\n",
   1145 			getprogname(), nname);
   1146 		exit(1);
   1147 	}
   1148 
   1149 	/*
   1150 	 * now...if no one said anything yet, we default nodes or
   1151 	 * any type that owns data being writeable, and everything
   1152 	 * else being readonly.
   1153 	 */
   1154 	if (rw == -1) {
   1155 		if (type == CTLTYPE_NODE ||
   1156 		    (flags & (SYSCTL_OWNDATA|SYSCTL_IMMEDIATE)))
   1157 			rw = SYSCTL_READWRITE;
   1158 		else
   1159 			rw = SYSCTL_READONLY;
   1160 	}
   1161 
   1162 	/*
   1163 	 * if a kernel address was specified, that can't be made
   1164 	 * writeable by us.
   1165 	if (rw != SYSCTL_READONLY && addr) {
   1166 		fprintf(warnfp, "%s: %s: kernel data can only be readable\n",
   1167 			getprogname(), nname);
   1168 		exit(1);
   1169 	}
   1170 	 */
   1171 
   1172 	/*
   1173 	 * what separator were they using in the full name of the new
   1174 	 * node?
   1175 	 */
   1176 	if ((t = strpbrk(nname, "./")) == NULL)
   1177 		sep[0] = '.';
   1178 	else
   1179 		sep[0] = t[0];
   1180 	sep[1] = '\0';
   1181 
   1182 	/*
   1183 	 * put it all together, now.  t'ain't much, is it?
   1184 	 */
   1185 	node.sysctl_flags = flags|rw|type;
   1186 	node.sysctl_size = sz;
   1187 	t = strrchr(nname, sep[0]);
   1188 	if (t != NULL)
   1189 		strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
   1190 	else
   1191 		strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));
   1192 
   1193 	/*
   1194 	 * if this is a new top-level node, then we don't need to find
   1195 	 * the mib for its parent
   1196 	 */
   1197 	if (t == NULL) {
   1198 		namelen = 0;
   1199 		gsname[0] = '\0';
   1200 	}
   1201 
   1202 	/*
   1203 	 * on the other hand, if it's not a top-level node...
   1204 	 */
   1205 	else {
   1206 		namelen = sizeof(name) / sizeof(name[0]);
   1207 		sz = sizeof(gsname);
   1208 		*t = '\0';
   1209 		rc = sysctlgetmibinfo(nname, &name[0], &namelen,
   1210 				      gsname, &sz, NULL);
   1211 		*t = sep[0];
   1212 		if (rc == -1) {
   1213 			fprintf(warnfp,
   1214 				"%s: %s level name '%s' in '%s' is invalid\n",
   1215 				getprogname(), lname[namelen], gsname, nname);
   1216 			exit(1);
   1217 		}
   1218 	}
   1219 
   1220 	/*
   1221 	 * yes, a new node is being created
   1222 	 */
   1223 	if (method != 0)
   1224 		name[namelen++] = method;
   1225 	else
   1226 		name[namelen++] = CTL_CREATE;
   1227 
   1228 	sz = sizeof(node);
   1229 	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1230 
   1231 	if (rc == -1) {
   1232 		fprintf(warnfp,
   1233 			"%s: %s: CTL_CREATE failed: %s\n",
   1234 			getprogname(), nname, strerror(errno));
   1235 		exit(1);
   1236 	}
   1237 	else if (!qflag && !nflag)
   1238 		printf("%s(%s): (created)\n", nname, st(type));
   1239 }
   1240 
   1241 static void
   1242 dparse(char *l)
   1243 {
   1244 	struct sysctlnode node;
   1245 	size_t sz;
   1246 	int name[CTL_MAXNAME], rc;
   1247 	u_int namelen;
   1248 
   1249 	memset(name, 0, sizeof(name));
   1250 	namelen = sizeof(name) / sizeof(name[0]);
   1251 	sz = sizeof(gsname);
   1252 	rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL);
   1253 	if (rc == -1) {
   1254 		fprintf(warnfp,
   1255 			"%s: %s level name '%s' in '%s' is invalid\n",
   1256 			getprogname(), lname[namelen], gsname, l);
   1257 		exit(1);
   1258 	}
   1259 
   1260 	memset(&node, 0, sizeof(node));
   1261 	node.sysctl_num = name[namelen - 1];
   1262 	name[namelen - 1] = CTL_DESTROY;
   1263 
   1264 	sz = sizeof(node);
   1265 	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1266 
   1267 	if (rc == -1) {
   1268 		fprintf(warnfp,
   1269 			"%s: %s: CTL_DESTROY failed: %s\n",
   1270 			getprogname(), l, strerror(errno));
   1271 		exit(1);
   1272 	}
   1273 	else if (!qflag && !nflag)
   1274 		printf("%s(%s): (destroyed)\n", gsname,
   1275 		       st(SYSCTL_TYPE(node.sysctl_flags)));
   1276 }
   1277 
   1278 /*
   1279  * ********************************************************************
   1280  * when things go wrong...
   1281  * ********************************************************************
   1282  */
   1283 static void
   1284 usage(void)
   1285 {
   1286 	const char *progname = getprogname();
   1287 
   1288 	(void)fprintf(stderr,
   1289 		      "usage:\t%s %s\n"
   1290 		      "\t%s %s\n"
   1291 		      "\t%s %s\n"
   1292 		      "\t%s %s\n"
   1293 		      "\t%s %s\n"
   1294 		      "\t%s %s\n",
   1295 		      progname, "[-ne] [-x[x]|-r] variable ...",
   1296 		      progname, "[-ne] [-q] -w variable=value ...",
   1297 		      progname, "[-ne] -a",
   1298 		      progname, "[-ne] -A",
   1299 		      progname, "[-ne] -M",
   1300 		      progname, "[-ne] [-q] -f file");
   1301 	exit(1);
   1302 }
   1303 
   1304 void
   1305 sysctlerror(int soft)
   1306 {
   1307 	if (soft) {
   1308 		switch (errno) {
   1309 		case ENOENT:
   1310 		case ENOPROTOOPT:
   1311 		case ENOTDIR:
   1312 		case EOPNOTSUPP:
   1313 		case EPROTONOSUPPORT:
   1314 			if (Aflag || req)
   1315 				fprintf(warnfp,
   1316 					"%s: the value is not available\n",
   1317 					gsname);
   1318 			return;
   1319 		}
   1320 	}
   1321 
   1322 	fprintf(warnfp, "%s: sysctl() failed with %s\n",
   1323 		gsname, strerror(errno));
   1324 	if (!soft)
   1325 		exit(1);
   1326 }
   1327 
   1328 /*
   1329  * ********************************************************************
   1330  * how to write to a "simple" node
   1331  * ********************************************************************
   1332  */
   1333 static void
   1334 write_number(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1335 {
   1336 	int ii, io;
   1337 	u_quad_t qi, qo;
   1338 	size_t si, so;
   1339 	int rc;
   1340 	void *i, *o;
   1341 	char *t;
   1342 
   1343 	si = so = 0;
   1344 	i = o = NULL;
   1345 	errno = 0;
   1346 	qi = strtouq(value, &t, 0);
   1347 	if (errno != 0) {
   1348 		fprintf(warnfp, "%s: value too large\n", value);
   1349 		exit(1);
   1350 	}
   1351 	if (*t != '\0') {
   1352 		fprintf(warnfp, "%s: not a number\n", value);
   1353 		exit(1);
   1354 	}
   1355 
   1356 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1357 	    case CTLTYPE_INT:
   1358 		ii = (int)qi;
   1359 		qo = ii;
   1360 		if (qo != qi) {
   1361 			fprintf(warnfp, "%s: value too large\n", value);
   1362 			exit(1);
   1363 		}
   1364 		o = &io;
   1365 		so = sizeof(io);
   1366 		i = &ii;
   1367 		si = sizeof(ii);
   1368 		break;
   1369 	case CTLTYPE_QUAD:
   1370 		o = &qo;
   1371 		so = sizeof(qo);
   1372 		i = &qi;
   1373 		si = sizeof(qi);
   1374 		break;
   1375 	}
   1376 
   1377 	rc = sysctl(name, namelen, o, &so, i, si);
   1378 	if (rc == -1)
   1379 		sysctlerror(0);
   1380 
   1381 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1382 	case CTLTYPE_INT:
   1383 		display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
   1384 		display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
   1385 		break;
   1386 	case CTLTYPE_QUAD:
   1387 		display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
   1388 		display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
   1389 		break;
   1390 	}
   1391 }
   1392 
   1393 static void
   1394 write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1395 {
   1396 	char *i, *o;
   1397 	size_t si, so;
   1398 	int rc;
   1399 
   1400 	i = value;
   1401 	si = strlen(i) + 1;
   1402 	so = node->sysctl_size;
   1403 	if (si > so && so != 0) {
   1404 		fprintf(warnfp, "%s: string too long\n", value);
   1405 		exit(1);
   1406 	}
   1407 	o = malloc(so);
   1408 	if (o == NULL) {
   1409 		fprintf(warnfp, "%s: !malloc failed!\n", gsname);
   1410 		exit(1);
   1411 	}
   1412 
   1413 	rc = sysctl(name, namelen, o, &so, i, si);
   1414 	if (rc == -1)
   1415 		sysctlerror(0);
   1416 
   1417 	display_string(node, gsname, o, so, DISPLAY_OLD);
   1418 	display_string(node, gsname, i, si, DISPLAY_NEW);
   1419 	free(o);
   1420 }
   1421 
   1422 /*
   1423  * ********************************************************************
   1424  * simple ways to print stuff consistently
   1425  * ********************************************************************
   1426  */
   1427 static void
   1428 display_number(const struct sysctlnode *node, const char *name,
   1429 	       const void *data, size_t sz, int n)
   1430 {
   1431 	u_quad_t q;
   1432 	int i;
   1433 
   1434 	if (qflag)
   1435 		return;
   1436 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1437 		return;
   1438 
   1439 	if (rflag && n != DISPLAY_OLD) {
   1440 		fwrite(data, sz, 1, stdout);
   1441 		return;
   1442 	}
   1443 
   1444 	if (!nflag) {
   1445 		if (n == DISPLAY_VALUE)
   1446 			printf("%s%s", name, eq);
   1447 		else if (n == DISPLAY_OLD)
   1448 			printf("%s: ", name);
   1449 	}
   1450 
   1451 	if (xflag > 1) {
   1452 		printf("\n");
   1453 		hex_dump(data, sz);
   1454 		return;
   1455 	}
   1456 
   1457 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1458 	case CTLTYPE_INT:
   1459 		memcpy(&i, data, sz);
   1460 		if (xflag)
   1461 			printf("0x%0*x", (int)sz * 2, i);
   1462 		else if (node->sysctl_flags & SYSCTL_HEX)
   1463 			printf("%#x", i);
   1464 		else
   1465 			printf("%d", i);
   1466 		break;
   1467 	case CTLTYPE_QUAD:
   1468 		memcpy(&q, data, sz);
   1469 		if (xflag)
   1470 			printf("0x%0*" PRIx64, (int)sz * 2, q);
   1471 		else if (node->sysctl_flags & SYSCTL_HEX)
   1472 			printf("%#" PRIx64, q);
   1473 		else
   1474 			printf("%" PRIu64, q);
   1475 		break;
   1476 	}
   1477 
   1478 	if (n == DISPLAY_OLD)
   1479 		printf(" -> ");
   1480 	else
   1481 		printf("\n");
   1482 }
   1483 
   1484 static void
   1485 display_string(const struct sysctlnode *node, const char *name,
   1486 	       const void *data, size_t sz, int n)
   1487 {
   1488 	const unsigned char *buf = data;
   1489 	int ni;
   1490 
   1491 	if (qflag)
   1492 		return;
   1493 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1494 		return;
   1495 
   1496 	if (rflag && n != DISPLAY_OLD) {
   1497 		fwrite(data, sz, 1, stdout);
   1498 		return;
   1499 	}
   1500 
   1501 	if (!nflag) {
   1502 		if (n == DISPLAY_VALUE)
   1503 			printf("%s%s", name, eq);
   1504 		else if (n == DISPLAY_OLD)
   1505 			printf("%s: ", name);
   1506 	}
   1507 
   1508 	if (xflag > 1) {
   1509 		printf("\n");
   1510 		hex_dump(data, sz);
   1511 		return;
   1512 	}
   1513 
   1514 	if (xflag || node->sysctl_flags & SYSCTL_HEX) {
   1515 		for (ni = 0; ni < (int)sz; ni++) {
   1516 			if (xflag)
   1517 				printf("%02x", buf[ni]);
   1518 			if (buf[ni] == '\0')
   1519 				break;
   1520 			if (!xflag)
   1521 				printf("\\x%2.2x", buf[ni]);
   1522 		}
   1523 	}
   1524 	else
   1525 		printf("%.*s", (int)sz, buf);
   1526 
   1527 	if (n == DISPLAY_OLD)
   1528 		printf(" -> ");
   1529 	else
   1530 		printf("\n");
   1531 }
   1532 
   1533 /*ARGSUSED*/
   1534 static void
   1535 display_struct(const struct sysctlnode *node, const char *name,
   1536 	       const void *data, size_t sz, int n)
   1537 {
   1538 	const unsigned char *buf = data;
   1539 	int ni;
   1540 	size_t more;
   1541 
   1542 	if (qflag)
   1543 		return;
   1544 	if (!(xflag || rflag)) {
   1545 		if (Aflag || req)
   1546 			fprintf(warnfp,
   1547 				"%s: this type is unknown to this program\n",
   1548 				gsname);
   1549 		return;
   1550 	}
   1551 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1552 		return;
   1553 
   1554 	if (rflag && n != DISPLAY_OLD) {
   1555 		fwrite(data, sz, 1, stdout);
   1556 		return;
   1557 	}
   1558 
   1559         if (!nflag) {
   1560                 if (n == DISPLAY_VALUE)
   1561                         printf("%s%s", name, eq);
   1562                 else if (n == DISPLAY_OLD)
   1563                         printf("%s: ", name);
   1564         }
   1565 
   1566 	if (xflag > 1) {
   1567 		printf("\n");
   1568 		hex_dump(data, sz);
   1569 		return;
   1570 	}
   1571 
   1572 	if (sz > 16) {
   1573 		more = sz - 16;
   1574 		sz = 16;
   1575 	}
   1576 	else
   1577 		more = 0;
   1578 	for (ni = 0; ni < (int)sz; ni++)
   1579 		printf("%02x", buf[ni]);
   1580 	if (more)
   1581 		printf("...(%zu more bytes)", more);
   1582 	printf("\n");
   1583 }
   1584 
   1585 static void
   1586 hex_dump(const unsigned char *buf, size_t len)
   1587 {
   1588 	int i, j;
   1589 	char line[80], tmp[12];
   1590 
   1591 	memset(line, ' ', sizeof(line));
   1592 	for (i = 0, j = 15; i < len; i++) {
   1593 		j = i % 16;
   1594 		/* reset line */
   1595 		if (j == 0) {
   1596 			line[58] = '|';
   1597 			line[77] = '|';
   1598 			line[78] = 0;
   1599 			snprintf(tmp, sizeof(tmp), "%07d", i);
   1600 			memcpy(&line[0], tmp, 7);
   1601 		}
   1602 		/* copy out hex version of byte */
   1603 		snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
   1604 		memcpy(&line[9 + j * 3], tmp, 2);
   1605 		/* copy out plain version of byte */
   1606 		line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
   1607 		/* print a full line and erase it */
   1608 		if (j == 15) {
   1609 			printf("%s\n", line);
   1610 			memset(line, ' ', sizeof(line));
   1611 		}
   1612 	}
   1613 	if (line[0] != ' ')
   1614 		printf("%s\n", line);
   1615 	printf("%07zu bytes\n", len);
   1616 }
   1617 
   1618 /*
   1619  * ********************************************************************
   1620  * functions that handle particular nodes
   1621  * ********************************************************************
   1622  */
   1623 /*ARGSUSED*/
   1624 static void
   1625 printother(HANDLER_ARGS)
   1626 {
   1627 	int rc;
   1628 	void *p;
   1629 	size_t sz1, sz2;
   1630 
   1631 	if (!(Aflag || req) || Mflag)
   1632 		return;
   1633 
   1634 	/*
   1635 	 * okay...you asked for it, so let's give it a go
   1636 	 */
   1637 	while (type != CTLTYPE_NODE && (xflag || rflag)) {
   1638 		rc = sysctl(name, namelen, NULL, &sz1, NULL, 0);
   1639 		if (rc == -1 || sz1 == 0)
   1640 			break;
   1641 		p = malloc(sz1);
   1642 		if (p == NULL)
   1643 			break;
   1644 		sz2 = sz1;
   1645 		rc = sysctl(name, namelen, p, &sz2, NULL, 0);
   1646 		if (rc == -1 || sz1 != sz2) {
   1647 			free(p);
   1648 			break;
   1649 		}
   1650 		display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
   1651 		free(p);
   1652 		return;
   1653 	}
   1654 
   1655 	/*
   1656 	 * that didn't work...do we have a specific message for this
   1657 	 * thing?
   1658 	 */
   1659 	if (v != NULL) {
   1660 		fprintf(warnfp, "%s: use '%s' to view this information\n",
   1661 			gsname, (const char *)v);
   1662 		return;
   1663 	}
   1664 
   1665 	/*
   1666 	 * hmm...i wonder if we have any generic hints?
   1667 	 */
   1668 	switch (name[0]) {
   1669 	case CTL_NET:
   1670 		fprintf(warnfp, "%s: use 'netstat' to view this information\n",
   1671 			sname);
   1672 		break;
   1673 	case CTL_DEBUG:
   1674 		fprintf(warnfp, "%s: missing 'options DEBUG' from kernel?\n",
   1675 			sname);
   1676 		break;
   1677 	case CTL_DDB:
   1678 		fprintf(warnfp, "%s: missing 'options DDB' from kernel?\n",
   1679 			sname);
   1680 		break;
   1681 	}
   1682 }
   1683 
   1684 /*ARGSUSED*/
   1685 static void
   1686 kern_clockrate(HANDLER_ARGS)
   1687 {
   1688 	struct clockinfo clkinfo;
   1689 	size_t sz;
   1690 	int rc;
   1691 
   1692 	sz = sizeof(clkinfo);
   1693 	rc = sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
   1694 	if (rc == -1) {
   1695 		sysctlerror(1);
   1696 		return;
   1697 	}
   1698 	if (sz != sizeof(clkinfo))
   1699 		errx(1, "%s: !returned size wrong!", sname);
   1700 
   1701 	if (xflag || rflag)
   1702 		display_struct(pnode, sname, &clkinfo, sz,
   1703 			       DISPLAY_VALUE);
   1704 	else if (!nflag)
   1705 		printf("%s: ", sname);
   1706 	printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
   1707 	       clkinfo.tick, clkinfo.tickadj,
   1708 	       clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
   1709 }
   1710 
   1711 /*ARGSUSED*/
   1712 static void
   1713 kern_boottime(HANDLER_ARGS)
   1714 {
   1715 	struct timeval timeval;
   1716 	time_t boottime;
   1717 	size_t sz;
   1718 	int rc;
   1719 
   1720 	sz = sizeof(timeval);
   1721 	rc = sysctl(name, namelen, &timeval, &sz, NULL, 0);
   1722 	if (rc == -1) {
   1723 		sysctlerror(1);
   1724 		return;
   1725 	}
   1726 	if (sz != sizeof(timeval))
   1727 		errx(1, "%s: !returned size wrong!", sname);
   1728 
   1729 	boottime = timeval.tv_sec;
   1730 	if (xflag || rflag)
   1731 		display_struct(pnode, sname, &timeval, sz,
   1732 			       DISPLAY_VALUE);
   1733 	else if (!nflag)
   1734 		/* ctime() provides the \n */
   1735 		printf("%s%s%s", sname, eq, ctime(&boottime));
   1736 	else if (nflag == 1)
   1737 		printf("%ld\n", (long)boottime);
   1738 	else
   1739 		printf("%ld.%06ld\n", (long)timeval.tv_sec,
   1740 		       (long)timeval.tv_usec);
   1741 }
   1742 
   1743 /*ARGSUSED*/
   1744 static void
   1745 kern_consdev(HANDLER_ARGS)
   1746 {
   1747 	dev_t cons;
   1748 	size_t sz;
   1749 	int rc;
   1750 
   1751 	sz = sizeof(cons);
   1752 	rc = sysctl(name, namelen, &cons, &sz, NULL, 0);
   1753 	if (rc == -1) {
   1754 		sysctlerror(1);
   1755 		return;
   1756 	}
   1757 	if (sz != sizeof(cons))
   1758 		errx(1, "%s: !returned size wrong!", sname);
   1759 
   1760 	if (xflag || rflag)
   1761 		display_struct(pnode, sname, &cons, sz,
   1762 			       DISPLAY_VALUE);
   1763 	else if (!nflag)
   1764 		printf("%s%s%s\n", sname, eq, devname(cons, S_IFCHR));
   1765 	else
   1766 		printf("0x%x\n", cons);
   1767 }
   1768 
   1769 /*ARGSUSED*/
   1770 static void
   1771 kern_cp_time(HANDLER_ARGS)
   1772 {
   1773 	u_int64_t cp_time[CPUSTATES];
   1774 	size_t sz;
   1775 	int rc;
   1776 
   1777 	sz = sizeof(cp_time);
   1778 	rc = sysctl(name, namelen, &cp_time, &sz, NULL, 0);
   1779 	if (rc == -1) {
   1780 		sysctlerror(1);
   1781 		return;
   1782 	}
   1783 	if (sz != sizeof(cp_time))
   1784 		errx(1, "%s: !returned size wrong!", sname);
   1785 
   1786 	if (xflag || rflag)
   1787 		display_struct(pnode, sname, &cp_time, sz,
   1788 			       DISPLAY_VALUE);
   1789 	else if (!nflag)
   1790 		printf("%s: ", sname);
   1791 	printf("user = %" PRIu64
   1792 	       ", nice = %" PRIu64
   1793 	       ", sys = %" PRIu64
   1794 	       ", intr = %" PRIu64
   1795 	       ", idle = %" PRIu64
   1796 	       "\n",
   1797 	       cp_time[CP_USER],
   1798 	       cp_time[CP_NICE],
   1799 	       cp_time[CP_SYS],
   1800 	       cp_time[CP_INTR],
   1801 	       cp_time[CP_IDLE]);
   1802 }
   1803 
   1804 /*ARGSUSED*/
   1805 static void
   1806 vm_loadavg(HANDLER_ARGS)
   1807 {
   1808 	struct loadavg loadavg;
   1809 	size_t sz;
   1810 	int rc;
   1811 
   1812 	sz = sizeof(loadavg);
   1813 	rc = sysctl(name, namelen, &loadavg, &sz, NULL, 0);
   1814 	if (rc == -1) {
   1815 		sysctlerror(1);
   1816 		return;
   1817 	}
   1818 	if (sz != sizeof(loadavg))
   1819 		errx(1, "%s: !returned size wrong!", sname);
   1820 
   1821 	if (xflag || rflag)
   1822 		display_struct(pnode, sname, &loadavg, sz,
   1823 			       DISPLAY_VALUE);
   1824 	else if (!nflag)
   1825 		printf("%s: ", sname);
   1826 	printf("%.2f %.2f %.2f\n",
   1827 	       (double) loadavg.ldavg[0] / loadavg.fscale,
   1828 	       (double) loadavg.ldavg[1] / loadavg.fscale,
   1829 	       (double) loadavg.ldavg[2] / loadavg.fscale);
   1830 }
   1831 
   1832 /*ARGSUSED*/
   1833 static void
   1834 proc_limit(HANDLER_ARGS)
   1835 {
   1836 	u_quad_t olim, *newp, nlim;
   1837 	size_t osz, nsz;
   1838 	char *t;
   1839 	int rc;
   1840 
   1841 	osz = sizeof(olim);
   1842 	if (value != NULL) {
   1843 		nsz = sizeof(nlim);
   1844 		newp = &nlim;
   1845 		if (strcmp(value, "unlimited") == 0)
   1846 			nlim = RLIM_INFINITY;
   1847 		else {
   1848 			errno = 0;
   1849 			nlim = strtouq(value, &t, 0);
   1850 			if (*t != '\0' || errno != 0) {
   1851 				fprintf(warnfp,
   1852 					"%s: %s: '%s' is not a valid limit\n",
   1853 					getprogname(), sname, value);
   1854 				exit(1);
   1855 			}
   1856 		}
   1857 	}
   1858 	else {
   1859 		nsz = 0;
   1860 		newp = NULL;
   1861 	}
   1862 
   1863 	rc = sysctl(name, namelen, &olim, &osz, newp, nsz);
   1864 	if (rc == -1) {
   1865 		sysctlerror(newp == NULL);
   1866 		return;
   1867 	}
   1868 
   1869 	if (newp && qflag)
   1870 		return;
   1871 
   1872 	if (rflag || xflag || olim != RLIM_INFINITY)
   1873 		display_number(pnode, sname, &olim, sizeof(olim),
   1874 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   1875 	else
   1876 		display_string(pnode, sname, "unlimited", 10,
   1877 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   1878 
   1879 	if (newp) {
   1880 		if (rflag || xflag || nlim != RLIM_INFINITY)
   1881 			display_number(pnode, sname, &nlim, sizeof(nlim),
   1882 				       DISPLAY_NEW);
   1883 		else
   1884 			display_string(pnode, sname, "unlimited", 10,
   1885 				       DISPLAY_NEW);
   1886 	}
   1887 }
   1888 
   1889 #ifdef CPU_DISKINFO
   1890 /*ARGSUSED*/
   1891 static void
   1892 machdep_diskinfo(HANDLER_ARGS)
   1893 {
   1894 	struct disklist *dl;
   1895 	struct biosdisk_info *bi;
   1896 	struct nativedisk_info *ni;
   1897 	int rc;
   1898 	size_t sz;
   1899 	uint i, b, lim;
   1900 
   1901 	rc = sysctl(name, namelen, NULL, &sz, NULL, 0);
   1902 	if (rc == -1) {
   1903 		sysctlerror(1);
   1904 		return;
   1905 	}
   1906 	dl = malloc(sz);
   1907 	if (dl == NULL) {
   1908 		sysctlerror(1);
   1909 		return;
   1910 	}
   1911 	rc = sysctl(name, namelen, dl, &sz, NULL, 0);
   1912 	if (rc == -1) {
   1913 		sysctlerror(1);
   1914 		return;
   1915 	}
   1916 
   1917 	if (!nflag)
   1918 		printf("%s: ", sname);
   1919 	lim = dl->dl_nbiosdisks;
   1920 	if (lim > MAX_BIOSDISKS)
   1921 		lim = MAX_BIOSDISKS;
   1922 	for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
   1923 		printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
   1924 		       bi->bi_dev, bi->bi_lbasecs,
   1925 		       bi->bi_cyl, bi->bi_head, bi->bi_sec,
   1926 		       bi->bi_flags);
   1927 	lim = dl->dl_nnativedisks;
   1928 	ni = dl->dl_nativedisks;
   1929 	bi = dl->dl_biosdisks;
   1930 	/* LINTED -- pointer casts are tedious */
   1931 	if ((char *)&ni[lim] != (char *)dl + sz) {
   1932 		fprintf(warnfp, "size mismatch\n");
   1933 		return;
   1934 	}
   1935 	for (i = 0; i < lim; ni++, i++) {
   1936 		char t = ':';
   1937 		printf(" %.*s", (int)sizeof ni->ni_devname,
   1938 		       ni->ni_devname);
   1939 		for (b = 0; b < ni->ni_nmatches; t = ',', b++)
   1940 			printf("%c%x", t,
   1941 			       bi[ni->ni_biosmatches[b]].bi_dev);
   1942 	}
   1943 	printf("\n");
   1944 }
   1945 #endif /* CPU_DISKINFO */
   1946