summaryrefslogtreecommitdiff
path: root/sbin/sysctl/sysctl.c
blob: 3e662d7d3d49543d77950277eb6ca4e94552cdfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
/*	$NetBSD: sysctl.c,v 1.77 2004/01/05 23:23:33 jmmv Exp $ */

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 *	All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Brown.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"@(#) Copyright (c) 1993\n\
	The Regents of the University of California.  All rights reserved.\n");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)sysctl.c	8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: sysctl.c,v 1.77 2004/01/05 23:23:33 jmmv Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/mount.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/sched.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/icmp6.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfs.h>
#include <machine/cpu.h>
#include <netkey/key_var.h>

#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/*
 * this needs to be able to do the printing and the setting
 */
#define HANDLER_PROTO const char *, const char *, char *, \
	int *, u_int, const struct sysctlnode *, \
	u_int, void *
#define HANDLER_ARGS const char *sname, const char *dname, char *value, \
	int *name, u_int namelen, const struct sysctlnode *pnode, \
	u_int type, void *v
#define DISPLAY_VALUE	0
#define DISPLAY_OLD	1
#define DISPLAY_NEW	2

/*
 * generic routines
 */
static struct handlespec *findprinter(const int *, u_int);
static struct handlespec *findwriter(const int *, u_int);
static void print_tree(int *, u_int, struct sysctlnode *, u_int, int);
static void write_number(int *, u_int, struct sysctlnode *, char *);
static void write_string(int *, u_int, struct sysctlnode *, char *);
static void display_number(const struct sysctlnode *, const char *,
			   const void *, size_t, int);
static void display_string(const struct sysctlnode *, const char *,
			   const void *, size_t, int);
static void display_struct(const struct sysctlnode *, const char *,
			   const void *, size_t, int);
static void hex_dump(const unsigned char *, size_t);
static void usage(void);
static void parse(char *);
static void cparse(char *);
static void dparse(char *);
static void sysctlerror(int);

/*
 * unexported from some place else (XXX tbd)
 */
int learn_tree(int *, u_int, struct sysctlnode *);
int sysctlnametomib(const char *, int *, u_int *,
		    char *, size_t *, struct sysctlnode **);

/*
 * "handlers"
 */
static void printother(HANDLER_PROTO);
static void kern_clockrate(HANDLER_PROTO);
static void kern_boottime(HANDLER_PROTO);
static void kern_consdev(HANDLER_PROTO);
static void kern_cp_time(HANDLER_PROTO);
static void vm_loadavg(HANDLER_PROTO);
static void proc_limit(HANDLER_PROTO);
#ifdef CPU_DISKINFO
static void machdep_diskinfo(HANDLER_PROTO);
#endif /* CPU_DISKINFO */

struct handlespec {
	int ps_name[CTL_MAXNAME];
	void (*ps_p)(HANDLER_PROTO);
	void (*ps_w)(HANDLER_PROTO);
	void *ps_d;
} handlers[] = {
	{ { CTL_KERN, KERN_CLOCKRATE },	kern_clockrate },
	{ { CTL_KERN, KERN_VNODE },	printother,	NULL,	"pstat" },
	{ { CTL_KERN, KERN_PROC },	printother,	NULL,	"ps" },
	{ { CTL_KERN, KERN_PROC2 },	printother,	NULL,	"ps" },
	{ { CTL_KERN, KERN_PROC_ARGS },	printother,	NULL,	"ps" },
	{ { CTL_KERN, KERN_FILE },	printother,	NULL,	"pstat" },
	{ { CTL_KERN, KERN_NTPTIME },	printother,	NULL,
	  "ntpdc -c kerninfo" },
	{ { CTL_KERN, KERN_MSGBUF },	printother,	NULL,	"dmesg" },
	{ { CTL_KERN, KERN_BOOTTIME },	kern_boottime },
	{ { CTL_KERN, KERN_CONSDEV },	kern_consdev },
	{ { CTL_KERN, KERN_CP_TIME },	kern_cp_time },
	{ { CTL_KERN, KERN_SYSVIPC_INFO }, printother,	NULL,	"ipcs" },
	{ { CTL_VM,   VM_METER },	printother,	NULL,
	  "vmstat' or 'systat" },
	{ { CTL_VM,   VM_LOADAVG },	vm_loadavg },
	{ { CTL_VM,   VM_UVMEXP },	printother,	NULL,
	  "vmstat' or 'systat" },
	{ { CTL_VM,   VM_UVMEXP2 },	printother,	NULL,
	  "vmstat' or 'systat" },
	{ { CTL_VFS,  2 /* NFS */, NFS_NFSSTATS },
					printother,	NULL,	"nfsstat" },
	{ { CTL_NET },			printother,	NULL,	NULL },
	{ { CTL_NET, PF_LOCAL },	printother,	NULL,	NULL },
	{ { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_IDENT },
					printother,	NULL,	"identd" },
	{ { CTL_NET, PF_INET6, IPPROTO_TCP, TCPCTL_IDENT },
					printother,	NULL,	"identd" },

	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST },
					printother,	NULL,	"something else" },
	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST },
					printother,	NULL,	"something else" },


	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSA },
					printother,	NULL,	"setkey" },
	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSP },
					printother,	NULL,	"setkey" },
	/* { { CTL_DEBUG },		printother,	NULL,	NULL }, */
	{ { CTL_HW,   HW_DISKSTATS },	printother,	NULL,	"iostat" },
#ifdef CPU_CONSDEV
	{ { CTL_MACHDEP, CPU_CONSDEV },	kern_consdev },
#endif /* CPU_CONSDEV */
#ifdef CPU_DISKINFO
	{ { CTL_MACHDEP, CPU_DISKINFO },machdep_diskinfo },
#endif /* CPU_CONSDEV */
	{ { CTL_DDB },			printother,	NULL,	NULL },
	{ { CTL_PROC, -1, PROC_PID_LIMIT, -1, -1 }, proc_limit, proc_limit },
	{ { CTL_UNSPEC }, },
};

struct sysctlnode my_root = {
#if defined(lint)
	0
#else /* defined(lint) */
	.sysctl_flags = SYSCTL_ROOT|
	    SYSCTL_TYPE(CTLTYPE_NODE),
	.sysctl_size = sizeof(struct sysctlnode),
	.sysctl_num = 0,
	.sysctl_name = "(prog_root)",
#endif /* defined(lint) */
};

int	Aflag, aflag, Mflag, nflag, qflag, rflag, wflag, xflag;
int	req;
FILE	*warnfp = stderr;

/*
 * vah-riables n stuff
 */
char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
	gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
char sep[2] = ".", *eq = " = ";
const char *lname[] = {
	"top", "second", "third", "fourth", "fifth", "sixth",
	"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
};

/*
 * you've heard of main, haven't you?
 */
int
main(int argc, char *argv[])
{
	char *fn = NULL;
	int name[CTL_MAXNAME];
	int ch;

	while ((ch = getopt(argc, argv, "Aaef:Mnqrwx")) != -1) {
		switch (ch) {
		case 'A':
			Aflag++;
			break;
		case 'a':
			aflag++;
			break;
		case 'e':
			eq = "=";
			break;
		case 'f':
			fn = optarg;
			wflag++;
			break;
		case 'M':
			Mflag++;
			break;
		case 'n':
			nflag++;
			break;
		case 'q':
			qflag++;
			break;
		case 'r':
			rflag++; 
			break;
		case 'w':
			wflag++;
			break;
		case 'x':
			xflag++;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;

	if (qflag && !wflag)
		usage();
	if (xflag && rflag)
		usage();
	/* if ((xflag || rflag) && wflag)
		usage(); */
	/* if (aflag && Mflag)
		usage(); */
	if ((Aflag || Mflag) && argc == 0)
		aflag = 1;

	if (Aflag)
		warnfp = stdout;
	req = 0;

	if (aflag) {
		print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1);
		/* if (argc == 0) */
		return (0);
	}

	if (fn) {
		FILE *fp;
		char *l;

		fp = fopen(fn, "r");
		if (fp == NULL) {
			err(1, "%s", fn);
		} else {
			while ((l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL)
			{
				if (*l) {
					parse(l);
					free(l);
				}
			}
			fclose(fp);
		}
		return (0);
	}

	if (argc == 0)
		usage();

	while (argc-- > 0)
		parse(*argv++);

	return (0);
}

/*
 * ********************************************************************
 * how to find someone special to handle the reading (or maybe even
 * writing) of a particular node
 * ********************************************************************
 */
static struct handlespec *
findprinter(const int *name, u_int namelen)
{
	struct handlespec *p;
	int i, j;

	if (namelen < 1)
		return (NULL);

	p = &handlers[0];
	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
		for (j = 0; j < namelen; j++)
			if (p[i].ps_name[j] != name[j] &&
			    p[i].ps_name[j] != -1)
				break;
		if (j == namelen && p[i].ps_p != NULL)
			return (&p[i]);
	}

	return (NULL);
}

static struct handlespec *
findwriter(const int *name, u_int namelen)
{
	struct handlespec *p;
	int i, j;

        if (namelen < 1)
                return (NULL);

	p = &handlers[0];
	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
		for (j = 0; j < namelen; j++)
			if (p[i].ps_name[j] != name[j] &&
			    p[i].ps_name[j] != -1)
				break;
		if (j == namelen && p[i].ps_w != NULL)
			return (&p[i]);
	}

	return (NULL);
}

/*
 * ********************************************************************
 * convert this special number to a special string so we can print the
 * mib
 * ********************************************************************
 */
static const char *
sf(u_int f)
{
	static char s[256];
	char *c;

	s[0] = '\0';
	c = "";

#define print_flag(_f, _s, _c, _q, _x) \
	if ((/*CONSTCOND*/_x) ? \
	    (((_f) & (_x)) == (__CONCAT(SYSCTL_,_q))) : \
	    ((_f) & (__CONCAT(SYSCTL_,_q)))) { \
		strlcat((_s), (_c), sizeof(_s)); \
		strlcat((_s), __STRING(_q), sizeof(_s)); \
		(_c) = ","; \
		(_f) &= ~(__CONCAT(SYSCTL_,_q)|(_x)); \
	}
	print_flag(f, s, c, READONLY, SYSCTL_READWRITE);
	print_flag(f, s, c, READONLY1, SYSCTL_READWRITE);
	print_flag(f, s, c, READONLY2, SYSCTL_READWRITE);
	print_flag(f, s, c, READWRITE, SYSCTL_READWRITE);
	print_flag(f, s, c, ANYWRITE, 0);
	print_flag(f, s, c, PRIVATE, 0);
	print_flag(f, s, c, PERMANENT, 0);
	print_flag(f, s, c, OWNDATA, 0);
	print_flag(f, s, c, IMMEDIATE, 0);
	print_flag(f, s, c, HEX, 0);
	print_flag(f, s, c, ROOT, 0);
	print_flag(f, s, c, ANYNUMBER, 0);
	print_flag(f, s, c, HIDDEN, 0);
	print_flag(f, s, c, ALIAS, 0);
#undef print_flag

	if (f) {
		char foo[9];
		snprintf(foo, sizeof(foo), "%x", f);
		strlcat(s, c, sizeof(s));
		strlcat(s, foo, sizeof(s));
	}

	return (s);
}

static const char *
st(u_int t)
{

	switch (t) {
	case CTLTYPE_NODE:
		return "NODE";
	case CTLTYPE_INT:
		return "INT";
	case CTLTYPE_STRING:
		return "STRING";
	case CTLTYPE_QUAD:
                return "QUAD";
	case CTLTYPE_STRUCT:
		return "STRUCT";
	}

	return "???";
}

/*
 * ********************************************************************
 * print this node and any others underneath it
 * ********************************************************************
 */
static void
print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
	   int add)
{
	struct sysctlnode *node;
	int rc, ni;
	size_t sz;
	char *sp, *dp, n[20];
	struct handlespec *p;

	sp = &gsname[strlen(gsname)];
	dp = &gdname[strlen(gdname)];

	if (sp != &gsname[0] && dp == &gdname[0]) {
		/*
		 * aw...shucks.  now we must play catch up
		 */
		for (ni = 0; ni < namelen; ni++) {
			(void)snprintf(n, sizeof(n), "%d", name[ni]);
			if (ni > 0)
				strncat(gdname, ".", sizeof(gdname));
			strncat(gdname, n, sizeof(gdname));
		}
	}

	if (pnode == NULL)
		pnode = &my_root;
	else if (add) {
		snprintf(n, sizeof(n), "%d", pnode->sysctl_num);
		if (namelen > 1) {
			strncat(gsname, sep, sizeof(gsname));
			strncat(gdname, ".", sizeof(gdname));
		}
		strncat(gsname, pnode->sysctl_name, sizeof(gsname));
		strncat(gdname, n, sizeof(gdname));
	}

	if (Mflag && pnode != &my_root) {
		if (nflag)
			printf("%s: ", gdname);
		else
			printf("%s (%s): ", gsname, gdname);
		printf("CTLTYPE_%s", st(type));
		if (type == CTLTYPE_NODE) {
			if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS)
				printf(", alias %d",
				       pnode->sysctl_alias);
			else
				printf(", children %d/%d",
				       pnode->sysctl_clen,
				       pnode->sysctl_csize);
		}
		printf(", size %zu", pnode->sysctl_size);
		printf(", flags 0x%x<%s>",
		       SYSCTL_FLAGS(pnode->sysctl_flags),
		       sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
		if (pnode->sysctl_func)
			printf(", func=%p", pnode->sysctl_func);
		printf(", ver=%d", pnode->sysctl_ver);
		printf("\n");
		if (type != CTLTYPE_NODE) {
			*sp = *dp = '\0';
			return;
		}
	}

	/*
	 * if this is an alias and we added our name, that means we
	 * got here by recursing down into the tree, so skip it.  The
	 * only way to print an aliased node is with either -M or by
	 * name specifically.
	 */
	if (SYSCTL_FLAGS(pnode->sysctl_flags) & SYSCTL_ALIAS && add) {
		*sp = *dp = '\0';
		return;
	}

	p = findprinter(name, namelen);
	if (type != CTLTYPE_NODE && p != NULL) {
		(*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
			   p->ps_d);
		*sp = *dp = '\0';
		return;
	}

	if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
		rc = sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
		if (rc == -1) {
			sysctlerror(1);
			*sp = *dp = '\0';
			return;
		}
		if (sz == 0) {
			if ((Aflag || req) && !Mflag)
				printf("%s: node contains no data\n", gsname);
			*sp = *dp = '\0';
			return;
		}
	}
	else
		sz = pnode->sysctl_size;

	switch (type) {
	case CTLTYPE_NODE: {
		learn_tree(name, namelen, pnode);
		node = pnode->sysctl_child;
		if (node == NULL) {
			if (p != NULL)
				(*p->ps_p)(gsname, gdname, NULL, name, namelen,
					   pnode, type, p->ps_d);
			else if ((Aflag || req) && !Mflag)
				printf("%s: no children\n", gsname);
		}
		else {
			req = 0;
			for (ni = 0; ni < pnode->sysctl_clen; ni++) {
				name[namelen] = node[ni].sysctl_num;
				if ((node[ni].sysctl_flags & SYSCTL_HIDDEN) &&
				    !(Aflag || req))
					continue;
				print_tree(name, namelen + 1, &node[ni],
					   SYSCTL_TYPE(node[ni].sysctl_flags),
					   1);
			}
		}
		break;
	}
	case CTLTYPE_INT: {
		int i;
		rc = sysctl(name, namelen, &i, &sz, NULL, 0);
		if (rc == -1) {
			sysctlerror(1);
			break;
		}
		display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
		break;
	}
	case CTLTYPE_STRING: {
		unsigned char buf[1024], *tbuf;
		tbuf = buf;
		sz = sizeof(buf);
		rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
		if (rc == -1 && errno == ENOMEM) {
			tbuf = malloc(sz);
			if (tbuf == NULL) {
				sysctlerror(1);
				break;
			}
			rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
		}
		if (rc == -1)
			sysctlerror(1);
		else
			display_string(pnode, gsname, buf, sz, DISPLAY_VALUE);
		if (tbuf != buf)
			free(tbuf);
		break;
	}
	case CTLTYPE_QUAD: {
		u_quad_t q;
		sz = sizeof(q);
		rc = sysctl(&name[0], namelen, &q, &sz, NULL, 0);
		if (rc == -1) {
			sysctlerror(1);
			break;
		}
		display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
		break;
	}
	case CTLTYPE_STRUCT: {
		/*
		 * we shouldn't actually get here, but if we
		 * do, would it be nice to have *something* to
		 * do other than completely ignore the
		 * request.
		 */
		unsigned char *d;
		if ((d = malloc(sz)) == NULL) {
			fprintf(warnfp, "%s: !malloc failed!\n", gsname);
			break;
		}
		rc = sysctl(&name[0], namelen, d, &sz, NULL, 0);
		if (rc == -1) {
			sysctlerror(1);
			break;
		}
		display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
		free(d);
		break;
	}
	default:
		/* should i print an error here? */
		break;
	}

	*sp = *dp = '\0';
}

/*
 * ********************************************************************
 * parse a request, possibly determining that it's a create or destroy
 * request
 * ********************************************************************
 */
static void
parse(char *l)
{
	struct sysctlnode *node;
	struct handlespec *w;
	int name[CTL_MAXNAME];
	u_int namelen, type;
	char *key, *value, *dot;
	size_t sz;

	req = 1;
	key = l;
	value = strchr(l, '=');
	if (value != NULL) {
		if (!wflag) {
			fprintf(warnfp,
				"%s: Must specify -w to set variables\n",
				getprogname());
			exit(1);
		}
		*value++ = '\0';
	}

	if ((dot = strpbrk(key, "./")) == NULL)
		sep[0] = '.';
	else
		sep[0] = dot[0];
	sep[1] = '\0';

	if (key[0] == sep[0] && key[1] == sep[0]) {
		if (value != NULL)
			value[-1] = '=';
		if (strncmp(key + 2, "create=", 7) == 0)
			cparse(key + 9);
		else if (strncmp(key + 2, "destroy=", 8) == 0)
			dparse(key + 10);
		else
			fprintf(warnfp, "%s: unable to parse '%s'\n",
				getprogname(), key);
		return;
	}

	node = &my_root;
	namelen = CTL_MAXNAME;
	sz = sizeof(gsname);

	if (sysctlnametomib(key, &name[0], &namelen, gsname, &sz, &node) == -1)
	{
		fprintf(warnfp, "%s: %s level name '%s' in '%s' is invalid\n",
			getprogname(), lname[namelen], gsname, l);
		exit(1);
	}

	type = SYSCTL_TYPE(node->sysctl_flags);

	if (value == NULL) {
		print_tree(&name[0], namelen, node, type, 0);
		gsname[0] = '\0';
		return;
	}

	if (type != CTLTYPE_NODE && (w = findwriter(name, namelen)) != NULL) {
		(*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
			   w->ps_d);
		gsname[0] = '\0';
		return;
	}

	switch (type) {
	case CTLTYPE_NODE:
		/*
		 * XXX old behavior is to print.  should we error instead?
		 */
		print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1);
		break;
	case CTLTYPE_INT:
		write_number(&name[0], namelen, node, value);
		break;
	case CTLTYPE_STRING:
		write_string(&name[0], namelen, node, value);
		break;
	case CTLTYPE_QUAD:
		write_number(&name[0], namelen, node, value);
		break;
	case CTLTYPE_STRUCT:
		/*
		 * XXX old behavior is to print.  should we error instead?
		 */
		/* fprintf(warnfp, "you can't write to %s\n", gsname); */
		print_tree(&name[0], namelen, node, type, 0);
		break;
	}
}

/*

  //create=foo.bar.whatever...,
  [type=(int|quad|string|struct|node),]
  [size=###,]
  [n=###,]
  [flags=(tiohxparw12),]
  [addr=0x####,|symbol=...|value=...]

  size is optional for some types.  type must be set before anything
  else.  nodes can have [r12whp], but nothing else applies.  if no
  size or type is given, node is asserted.  writeable is the default,
  with [r12w] being read-only, writeable below securelevel 1,
  writeable below securelevel 2, and unconditionally writeable
  respectively.  if you specify addr, it is assumed to be the name of
  a kernel symbol, if value, SYSCTL_OWNDATA will be asserted for
  strings, SYSCTL_IMMEDIATE for ints and u_quad_ts.  you cannot
  specify both value and addr.

*/

static void
cparse(char *l)
{
	struct sysctlnode node;
	size_t sz;
	char *nname, *key, *value, *data, *addr, *c, *t;
	int name[CTL_MAXNAME], i, rc, method, flags, rw;
	u_int namelen, type;
	u_quad_t q;
	long li, lo;

	/*
	 * these are the pieces that make up the description of a new
	 * node
	 */
	memset(&node, 0, sizeof(node));
	node.sysctl_num = CTL_CREATE;  /* any number is fine */
	flags = 0;
	rw = -1;
	type = 0;
	sz = 0;
	data = addr = NULL;
	memset(name, 0, sizeof(name));
	namelen = 0;
	method = 0;

	/*
	 * misc stuff used when constructing
	 */
	i = 0;
	q = 0;
	key = NULL;
	value = NULL;

	/*
	 * the name of the thing we're trying to create is first, so
	 * pick it off.
	 */
	nname = l;
	if ((c = strchr(nname, ',')) != NULL)
		*c++ = '\0';

	while (c != NULL) {

		/*
		 * pull off the next "key=value" pair
		 */
		key = c;
		if ((t = strchr(key, '=')) != NULL) {
			*t++ = '\0';
			value = t;
		}
		else
			value = NULL;

		/*
		 * if the "key" is "value", then that eats the rest of
		 * the string, so we're done, otherwise bite it off at
		 * the next comma.
		 */
		if (strcmp(key, "value") == 0) {
			c = NULL;
			data = value;
			break;
		}
		else {
			if ((c = strchr(value, ',')) != NULL)
				*c++ = '\0';
		}

		/*
		 * note that we (mostly) let the invoker of sysctl(8)
		 * play rampant here and depend on the kernel to tell
		 * them that they were wrong.  well...within reason.
		 * we later check the various parameters against each
		 * other to make sure it makes some sort of sense.
		 */
		if (strcmp(key, "addr") == 0) {
			/*
			 * we can't check these two.  only the kernel
			 * can tell us when it fails to find the name
			 * (or if the address is invalid).
			 */
			if (method != 0) {
				fprintf(warnfp,
				    "%s: %s: already have %s for new node\n",
				    getprogname(), nname,
				    method == CTL_CREATE ? "addr" : "symbol");
				exit(1);
			}
			errno = 0;
			addr = (void*)strtoul(value, &t, 0);
			if (*t != '\0' || errno != 0) {
				fprintf(warnfp,
				    "%s: %s: '%s' is not a valid address\n",
				    getprogname(), nname, value);
				exit(1);
			}
			method = CTL_CREATE;
		}
		else if (strcmp(key, "symbol") == 0) {
			if (method != 0) {
				fprintf(warnfp,
				    "%s: %s: already have %s for new node\n",
				    getprogname(), nname,
				    method == CTL_CREATE ? "addr" : "symbol");
				exit(1);
			}
			addr = value;
			method = CTL_CREATESYM;
		}
		else if (strcmp(key, "type") == 0) {
			if (strcmp(value, "node") == 0)
				type = CTLTYPE_NODE;
			else if (strcmp(value, "int") == 0) {
				sz = sizeof(int);
				type = CTLTYPE_INT;
			}
			else if (strcmp(value, "string") == 0)
				type = CTLTYPE_STRING;
			else if (strcmp(value, "quad") == 0) {
				sz = sizeof(u_quad_t);
				type = CTLTYPE_QUAD;
			}
			else if (strcmp(value, "struct") == 0)
				type = CTLTYPE_STRUCT;
			else {
				fprintf(warnfp,
					"%s: %s: '%s' is not a valid type\n",
					getprogname(), nname, value);
				exit(1);
			}
		}
		else if (strcmp(key, "size") == 0) {
			errno = 0;
			/*
			 * yes, i know size_t is not an unsigned long,
			 * but we can all agree that it ought to be,
			 * right?
			 */
			sz = strtoul(value, &t, 0);
			if (*t != '\0' || errno != 0) {
				fprintf(warnfp,
					"%s: %s: '%s' is not a valid size\n",
					getprogname(), nname, value);
				exit(1);
			}
		}
		else if (strcmp(key, "n") == 0) {
			errno = 0;
			li = strtol(value, &t, 0);
			node.sysctl_num = li;
			lo = node.sysctl_num;
			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
				fprintf(warnfp,
				    "%s: %s: '%s' is not a valid mib number\n",
				    getprogname(), nname, value);
				exit(1);
			}
		}
		else if (strcmp(key, "flags") == 0) {
			t = value;
			while (*t != '\0') {
				switch (*t) {
				case 'a':
					flags |= SYSCTL_ANYWRITE;
					break;
				case 'h':
					flags |= SYSCTL_HIDDEN;
					break;
				case 'i':
					flags |= SYSCTL_IMMEDIATE;
					break;
				case 'o':
					flags |= SYSCTL_OWNDATA;
					break;
				case 'p':
					flags |= SYSCTL_PRIVATE;
					break;
				case 'x':
					flags |= SYSCTL_HEX;
					break;

				case 'r':
					rw = SYSCTL_READONLY;
					break;
				case '1':
					rw = SYSCTL_READONLY1;
					break;
				case '2':
					rw = SYSCTL_READONLY2;
					break;
				case 'w':
					rw = SYSCTL_READWRITE;
					break;
				default:
					fprintf(warnfp,
					   "%s: %s: '%c' is not a valid flag\n",
					    getprogname(), nname, *t);
					exit(1);
				}
				t++;
			}
		}
		else {
			fprintf(warnfp, "%s: %s: unrecognized keyword '%s'\n",
				getprogname(), nname, key);
			exit(1);
		}
	}

	/*
	 * now that we've finished parsing the given string, fill in
	 * anything they didn't specify
	 */
	if (type == 0)
		type = CTLTYPE_NODE;

	/*
	 * the "data" can be interpreted various ways depending on the
	 * type of node we're creating, as can the size
	 */
	if (data != NULL) {
		if (addr != NULL) {
			fprintf(warnfp,
				"%s: %s: cannot specify both value and "
				"address\n", getprogname(), nname);
			exit(1);
		}

		switch (type) {
		case CTLTYPE_INT:
			errno = 0;
			li = strtol(data, &t, 0);
			i = li;
			lo = i;
			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
				fprintf(warnfp,
					"%s: %s: '%s' is not a valid integer\n",
					getprogname(), nname, value);
				exit(1);
			}
			if (!(flags & SYSCTL_OWNDATA)) {
				flags |= SYSCTL_IMMEDIATE;
				node.sysctl_idata = i;
			}
			else
				node.sysctl_data = &i;
			if (sz == 0)
				sz = sizeof(int);
			break;
		case CTLTYPE_STRING:
			flags |= SYSCTL_OWNDATA;
			node.sysctl_data = data;
			if (sz == 0)
				sz = strlen(data) + 1;
			else if (sz < strlen(data) + 1) {
				fprintf(warnfp, "%s: %s: ignoring size=%zu for "
					"string node, too small for given "
					"value\n", getprogname(), nname, sz);
				sz = strlen(data) + 1;
			}
			break;
		case CTLTYPE_QUAD:
			errno = 0;
			q = strtouq(data, &t, 0);
			if (*t != '\0' || errno != 0) {
				fprintf(warnfp,
					"%s: %s: '%s' is not a valid quad\n",
					getprogname(), nname, value);
				exit(1);
			}
			if (!(flags & SYSCTL_OWNDATA)) {
				flags |= SYSCTL_IMMEDIATE;
				node.sysctl_qdata = q;
			}
			else
				node.sysctl_data = &q;
			if (sz == 0)
				sz = sizeof(u_quad_t);
			break;
		case CTLTYPE_STRUCT:
			fprintf(warnfp,
				"%s: %s: struct not initializable\n",
				getprogname(), nname);
			exit(1);
		}

		/*
		 * these methods have all provided local starting
		 * values that the kernel must copy in
		 */
	}

	/*
	 * hmm...no data, but we have an address of data.  that's
	 * fine.
	 */
	else if (addr != 0)
		node.sysctl_data = (void*)addr;

	/*
	 * no data and no address?  well...okay.  we might be able to
	 * manage that.
	 */
	else if (type != CTLTYPE_NODE) {
		if (sz == 0) {
			fprintf(warnfp,
				"%s: %s: need a size or a starting value\n",
				getprogname(), nname);
                        exit(1);
                }
		if (!(flags & SYSCTL_IMMEDIATE))
			flags |= SYSCTL_OWNDATA;
	}

	/*
	 * now we do a few sanity checks on the description we've
	 * assembled
	 */
	if ((flags & SYSCTL_IMMEDIATE) &&
	    (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
		fprintf(warnfp,
			"%s: %s: cannot make an immediate %s\n", 
			getprogname(), nname,
			(type == CTLTYPE_STRING) ? "string" : "struct");
		exit(1);
	}
	if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
		fprintf(warnfp, "%s: %s: nodes do not have data\n",
			getprogname(), nname);
		exit(1);
	}
	
	/*
	 * some types must have a particular size
	 */
	if (sz != 0) {
		if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
		    (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
		    (type == CTLTYPE_NODE && sz != 0)) {
			fprintf(warnfp, "%s: %s: wrong size for type\n",
				getprogname(), nname);
			exit(1);
		}
	}
	else if (type == CTLTYPE_STRUCT) {
		fprintf(warnfp, "%s: %s: struct must have size\n",
			getprogname(), nname);
		exit(1);
	}

	/*
	 * now...if no one said anything yet, we default nodes or
	 * any type that owns data being writeable, and everything
	 * else being readonly.
	 */
	if (rw == -1) {
		if (type == CTLTYPE_NODE ||
		    (flags & (SYSCTL_OWNDATA|SYSCTL_IMMEDIATE)))
			rw = SYSCTL_READWRITE;
		else
			rw = SYSCTL_READONLY;
	}

	/*
	 * if a kernel address was specified, that can't be made
	 * writeable by us.
	if (rw != SYSCTL_READONLY && addr) {
		fprintf(warnfp, "%s: %s: kernel data can only be readable\n",
			getprogname(), nname);
		exit(1);
	}
	 */

	/*
	 * what separator were they using in the full name of the new
	 * node?
	 */
	if ((t = strpbrk(nname, "./")) == NULL)
		sep[0] = '.';
	else
		sep[0] = t[0];
	sep[1] = '\0';

	/*
	 * put it all together, now.  t'ain't much, is it?
	 */
	node.sysctl_flags = flags|rw|type;
	node.sysctl_size = sz;
	t = strrchr(nname, sep[0]);
	if (t != NULL)
		strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
	else
		strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));

	/*
	 * if this is a new top-level node, then we don't need to find
	 * the mib for its parent
	 */
	if (t == NULL) {
		namelen = 0;
		gsname[0] = '\0';
	}

	/*
	 * on the other hand, if it's not a top-level node...
	 */
	else {
		namelen = sizeof(name) / sizeof(name[0]);
		sz = sizeof(gsname);
		*t = '\0';
		rc = sysctlnametomib(nname, &name[0], &namelen,
				     gsname, &sz, NULL);
		*t = sep[0];
		if (rc == -1) {
			fprintf(warnfp,
				"%s: %s level name '%s' in '%s' is invalid\n",
				getprogname(), lname[namelen], gsname, nname);
			exit(1);
		}
	}

	/*
	 * yes, a new node is being created
	 */
	if (method != 0)
		name[namelen++] = method;
	else
		name[namelen++] = CTL_CREATE;

	sz = sizeof(node);
	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));

	if (rc == -1) {
		fprintf(warnfp,
			"%s: %s: CTL_CREATE failed: %s\n",
			getprogname(), nname, strerror(errno));
		exit(1);
	}
	else if (!qflag && !nflag)
		printf("%s(%s): (created)\n", nname, st(type));
}

static void
dparse(char *l)
{
	struct sysctlnode node;
	size_t sz;
	int name[CTL_MAXNAME], rc;
	u_int namelen;

	memset(name, 0, sizeof(name));
	namelen = sizeof(name) / sizeof(name[0]);
	sz = sizeof(gsname);
	rc = sysctlnametomib(l, &name[0], &namelen, gsname, &sz, NULL);
	if (rc == -1) {
		fprintf(warnfp,
			"%s: %s level name '%s' in '%s' is invalid\n",
			getprogname(), lname[namelen], gsname, l);
		exit(1);
	}

	memset(&node, 0, sizeof(node));
	node.sysctl_num = name[namelen - 1];
	name[namelen - 1] = CTL_DESTROY;

	sz = sizeof(node);
	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));

	if (rc == -1) {
		fprintf(warnfp,
			"%s: %s: CTL_DESTROY failed: %s\n",
			getprogname(), l, strerror(errno));
		exit(1);
	}
	else if (!qflag && !nflag)
		printf("%s(%s): (destroyed)\n", gsname,
		       st(SYSCTL_TYPE(node.sysctl_flags)));
}

/*
 * ********************************************************************
 * when things go wrong...
 * ********************************************************************
 */
static void
usage(void)
{
	const char *progname = getprogname();

	(void)fprintf(stderr,
		      "usage:\t%s %s\n"
		      "\t%s %s\n"
		      "\t%s %s\n"
		      "\t%s %s\n"
		      "\t%s %s\n"
		      "\t%s %s\n",
		      progname, "[-ne] [-x[x]|-r] variable ...",
		      progname, "[-ne] [-q] -w variable=value ...",
		      progname, "[-ne] -a",
		      progname, "[-ne] -A",
		      progname, "[-ne] -M",
		      progname, "[-ne] [-q] -f file");
	exit(1);
}

void
sysctlerror(int soft)
{
	if (soft) {
		switch (errno) {
		case ENOENT:
		case ENOPROTOOPT:
		case ENOTDIR:
		case EOPNOTSUPP:
		case EPROTONOSUPPORT:
			if (Aflag || req)
				fprintf(warnfp,
					"%s: the value is not available\n",
					gsname);
			return;
		}
	}

	fprintf(warnfp, "%s: sysctl() failed with %s\n",
		gsname, strerror(errno));
	if (!soft)
		exit(1);
}

/*
 * ********************************************************************
 * how to write to a "simple" node
 * ********************************************************************
 */
static void
write_number(int *name, u_int namelen, struct sysctlnode *node, char *value)
{
	int ii, io;
	u_quad_t qi, qo;
	size_t si, so;
	int rc;
	void *i, *o;
	char *t;

	si = so = 0;
	i = o = NULL;
	errno = 0;
	qi = strtouq(value, &t, 0);
	if (errno != 0) {
		fprintf(warnfp, "%s: value too large\n", value);
		exit(1);
	}
	if (*t != '\0') {
		fprintf(warnfp, "%s: not a number\n", value);
		exit(1);
	}

	switch (SYSCTL_TYPE(node->sysctl_flags)) {
	    case CTLTYPE_INT:
		ii = (int)qi;
		qo = ii;
		if (qo != qi) {
			fprintf(warnfp, "%s: value too large\n", value);
			exit(1);
		}
		o = &io;
		so = sizeof(io);
		i = &ii;
		si = sizeof(ii);
		break;
	case CTLTYPE_QUAD:
		o = &qo;
		so = sizeof(qo);
		i = &qi;
		si = sizeof(qi);
		break;
	}

	rc = sysctl(name, namelen, o, &so, i, si);
	if (rc == -1)
		sysctlerror(0);

	switch (SYSCTL_TYPE(node->sysctl_flags)) {
	case CTLTYPE_INT:
		display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
		display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
		break;
	case CTLTYPE_QUAD:
		display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
		display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
		break;
	}
}

static void
write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
{
	char *i, *o;
	size_t si, so;
	int rc;

	i = value;
	si = strlen(i) + 1;
	so = node->sysctl_size;
	if (si > so && so != 0) {
		fprintf(warnfp, "%s: string too long\n", value);
		exit(1);
	}
	o = malloc(so);
	if (o == NULL) {
		fprintf(warnfp, "%s: !malloc failed!\n", gsname);
		exit(1);
	}

	rc = sysctl(name, namelen, o, &so, i, si);
	if (rc == -1)
		sysctlerror(0);

	display_string(node, gsname, o, so, DISPLAY_OLD);
	display_string(node, gsname, i, si, DISPLAY_NEW);
	free(o);
}

/*
 * ********************************************************************
 * simple ways to print stuff consistently
 * ********************************************************************
 */
static void
display_number(const struct sysctlnode *node, const char *name,
	       const void *data, size_t sz, int n)
{
	u_quad_t q;
	int i;

	if (qflag)
		return;
	if ((nflag || rflag) && (n == DISPLAY_OLD))
		return;

	if (rflag && n != DISPLAY_OLD) {
		fwrite(data, sz, 1, stdout);
		return;
	}

	if (!nflag) {
		if (n == DISPLAY_VALUE)
			printf("%s%s", name, eq);
		else if (n == DISPLAY_OLD)
			printf("%s: ", name);
	}

	if (xflag > 1) {
		printf("\n");
		hex_dump(data, sz);
		return;
	}

	switch (SYSCTL_TYPE(node->sysctl_flags)) {
	case CTLTYPE_INT:
		memcpy(&i, data, sz);
		if (xflag)
			printf("0x%0*x", (int)sz * 2, i);
		else if (node->sysctl_flags & SYSCTL_HEX)
			printf("%#x", i);
		else
			printf("%d", i);
		break;
	case CTLTYPE_QUAD:
		memcpy(&q, data, sz);
		if (xflag)
			printf("0x%0*" PRIx64, (int)sz * 2, q);
		else if (node->sysctl_flags & SYSCTL_HEX)
			printf("%#" PRIx64, q);
		else
			printf("%" PRIu64, q);
		break;
	}

	if (n == DISPLAY_OLD)
		printf(" -> ");
	else
		printf("\n");
}

static void
display_string(const struct sysctlnode *node, const char *name,
	       const void *data, size_t sz, int n)
{
	const unsigned char *buf = data;
	int ni;

	if (qflag)
		return;
	if ((nflag || rflag) && (n == DISPLAY_OLD))
		return;

	if (rflag && n != DISPLAY_OLD) {
		fwrite(data, sz, 1, stdout);
		return;
	}

	if (!nflag) {
		if (n == DISPLAY_VALUE)
			printf("%s%s", name, eq);
		else if (n == DISPLAY_OLD)
			printf("%s: ", name);
	}

	if (xflag > 1) {
		printf("\n");
		hex_dump(data, sz);
		return;
	}

	if (xflag || node->sysctl_flags & SYSCTL_HEX) {
		for (ni = 0; ni < (int)sz; ni++) {
			if (xflag)
				printf("%02x", buf[ni]);
			if (buf[ni] == '\0')
				break;
			if (!xflag)
				printf("\\x%2.2x", buf[ni]);
		}
	}
	else
		printf("%.*s", (int)sz, buf);

	if (n == DISPLAY_OLD)
		printf(" -> ");
	else
		printf("\n");
}

/*ARGSUSED*/
static void
display_struct(const struct sysctlnode *node, const char *name,
	       const void *data, size_t sz, int n)
{
	const unsigned char *buf = data;
	int ni;
	size_t more;

	if (qflag)
		return;
	if (!(xflag || rflag)) {
		if (Aflag || req)
			fprintf(warnfp,
				"%s: this type is unknown to this program\n",
				gsname);
		return;
	}
	if ((nflag || rflag) && (n == DISPLAY_OLD))
		return;

	if (rflag && n != DISPLAY_OLD) {
		fwrite(data, sz, 1, stdout);
		return;
	}

        if (!nflag) {
                if (n == DISPLAY_VALUE)
                        printf("%s%s", name, eq);
                else if (n == DISPLAY_OLD)
                        printf("%s: ", name);
        }

	if (xflag > 1) {
		printf("\n");
		hex_dump(data, sz);
		return;
	}

	if (sz > 16) {
		more = sz - 16;
		sz = 16;
	}
	else
		more = 0;
	for (ni = 0; ni < (int)sz; ni++)
		printf("%02x", buf[ni]);
	if (more)
		printf("...(%zu more bytes)", more);
	printf("\n");
}

static void
hex_dump(const unsigned char *buf, size_t len)
{
	int i, j;
	char line[80], tmp[12];

	memset(line, ' ', sizeof(line));
	for (i = 0, j = 15; i < len; i++) {
		j = i % 16;
		/* reset line */
		if (j == 0) {
			line[58] = '|';
			line[77] = '|';
			line[78] = 0;
			snprintf(tmp, sizeof(tmp), "%07d", i);
			memcpy(&line[0], tmp, 7);
		}
		/* copy out hex version of byte */
		snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
		memcpy(&line[9 + j * 3], tmp, 2);
		/* copy out plain version of byte */
		line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
		/* print a full line and erase it */
		if (j == 15) {
			printf("%s\n", line);
			memset(line, ' ', sizeof(line));
		}
	}
	if (line[0] != ' ')
		printf("%s\n", line);
	printf("%07zu bytes\n", len);
}

/*
 * ********************************************************************
 * functions that handle particular nodes
 * ********************************************************************
 */
/*ARGSUSED*/
static void
printother(HANDLER_ARGS)
{
	int rc;
	void *p;
	size_t sz1, sz2;

	if (!(Aflag || req) || Mflag)
		return;

	/*
	 * okay...you asked for it, so let's give it a go
	 */
	while (type != CTLTYPE_NODE && (xflag || rflag)) {
		rc = sysctl(name, namelen, NULL, &sz1, NULL, 0);
		if (rc == -1 || sz1 == 0)
			break;
		p = malloc(sz1);
		if (p == NULL)
			break;
		sz2 = sz1;
		rc = sysctl(name, namelen, p, &sz2, NULL, 0);
		if (rc == -1 || sz1 != sz2) {
			free(p);
			break;
		}
		display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
		free(p);
		return;
	}

	/*
	 * that didn't work...do we have a specific message for this
	 * thing?
	 */
	if (v != NULL) {
		fprintf(warnfp, "%s: use '%s' to view this information\n",
			gsname, (const char *)v);
		return;
	}

	/*
	 * hmm...i wonder if we have any generic hints?
	 */
	switch (name[0]) {
	case CTL_NET:
		fprintf(warnfp, "%s: use 'netstat' to view this information\n",
			sname);
		break;
	case CTL_DEBUG:
		fprintf(warnfp, "%s: missing 'options DEBUG' from kernel?\n",
			sname);
		break;
	case CTL_DDB:
		fprintf(warnfp, "%s: missing 'options DDB' from kernel?\n",
			sname);
		break;
	}
}

/*ARGSUSED*/
static void
kern_clockrate(HANDLER_ARGS)
{
	struct clockinfo clkinfo;
	size_t sz;
	int rc;

	sz = sizeof(clkinfo);
	rc = sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	if (sz != sizeof(clkinfo))
		errx(1, "%s: !returned size wrong!", sname);

	if (xflag || rflag)
		display_struct(pnode, sname, &clkinfo, sz,
			       DISPLAY_VALUE);
	else if (!nflag)
		printf("%s: ", sname);
	printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
	       clkinfo.tick, clkinfo.tickadj,
	       clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
}

/*ARGSUSED*/
static void
kern_boottime(HANDLER_ARGS)
{
	struct timeval timeval;
	time_t boottime;
	size_t sz;
	int rc;

	sz = sizeof(timeval);
	rc = sysctl(name, namelen, &timeval, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	if (sz != sizeof(timeval))
		errx(1, "%s: !returned size wrong!", sname);

	boottime = timeval.tv_sec;
	if (xflag || rflag)
		display_struct(pnode, sname, &timeval, sz,
			       DISPLAY_VALUE);
	else if (!nflag)
		/* ctime() provides the \n */
		printf("%s%s%s", sname, eq, ctime(&boottime));
	else if (nflag == 1)
		printf("%ld\n", (long)boottime);
	else
		printf("%ld.%06ld\n", (long)timeval.tv_sec,
		       (long)timeval.tv_usec);
}

/*ARGSUSED*/
static void
kern_consdev(HANDLER_ARGS)
{
	dev_t cons;
	size_t sz;
	int rc;

	sz = sizeof(cons);
	rc = sysctl(name, namelen, &cons, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	if (sz != sizeof(cons))
		errx(1, "%s: !returned size wrong!", sname);

	if (xflag || rflag)
		display_struct(pnode, sname, &cons, sz,
			       DISPLAY_VALUE);
	else if (!nflag)
		printf("%s%s%s\n", sname, eq, devname(cons, S_IFCHR));
	else
		printf("0x%x\n", cons);
}

/*ARGSUSED*/
static void
kern_cp_time(HANDLER_ARGS)
{
	u_int64_t cp_time[CPUSTATES];
	size_t sz;
	int rc;

	sz = sizeof(cp_time);
	rc = sysctl(name, namelen, &cp_time, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	if (sz != sizeof(cp_time))
		errx(1, "%s: !returned size wrong!", sname);

	if (xflag || rflag)
		display_struct(pnode, sname, &cp_time, sz,
			       DISPLAY_VALUE);
	else if (!nflag)
		printf("%s: ", sname);
	printf("user = %" PRIu64
	       ", nice = %" PRIu64
	       ", sys = %" PRIu64
	       ", intr = %" PRIu64
	       ", idle = %" PRIu64
	       "\n",
	       cp_time[CP_USER],
	       cp_time[CP_NICE],
	       cp_time[CP_SYS],
	       cp_time[CP_INTR],
	       cp_time[CP_IDLE]);
}

/*ARGSUSED*/
static void
vm_loadavg(HANDLER_ARGS)
{
	struct loadavg loadavg;
	size_t sz;
	int rc;

	sz = sizeof(loadavg);
	rc = sysctl(name, namelen, &loadavg, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	if (sz != sizeof(loadavg))
		errx(1, "%s: !returned size wrong!", sname);

	if (xflag || rflag)
		display_struct(pnode, sname, &loadavg, sz,
			       DISPLAY_VALUE);
	else if (!nflag)
		printf("%s: ", sname);
	printf("%.2f %.2f %.2f\n",
	       (double) loadavg.ldavg[0] / loadavg.fscale,
	       (double) loadavg.ldavg[1] / loadavg.fscale,
	       (double) loadavg.ldavg[2] / loadavg.fscale);
}

/*ARGSUSED*/
static void
proc_limit(HANDLER_ARGS)
{
	u_quad_t olim, *newp, nlim;
	size_t osz, nsz;
	char *t;
	int rc;

	osz = sizeof(olim);
	if (value != NULL) {
		nsz = sizeof(nlim);
		newp = &nlim;
		if (strcmp(value, "unlimited") == 0)
			nlim = RLIM_INFINITY;
		else {
			errno = 0;
			nlim = strtouq(value, &t, 0);
			if (*t != '\0' || errno != 0) {
				fprintf(warnfp,
					"%s: %s: '%s' is not a valid limit\n",
					getprogname(), sname, value);
				exit(1);
			}
		}
	}
	else {
		nsz = 0;
		newp = NULL;
	}

	rc = sysctl(name, namelen, &olim, &osz, newp, nsz);
	if (rc == -1) {
		sysctlerror(newp == NULL);
		return;
	}

	if (newp && qflag)
		return;

	if (rflag || xflag || olim != RLIM_INFINITY)
		display_number(pnode, sname, &olim, sizeof(olim),
			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
	else
		display_string(pnode, sname, "unlimited", 10,
			       newp ? DISPLAY_OLD : DISPLAY_VALUE);

	if (newp) {
		if (rflag || xflag || nlim != RLIM_INFINITY)
			display_number(pnode, sname, &nlim, sizeof(nlim),
				       DISPLAY_NEW);
		else
			display_string(pnode, sname, "unlimited", 10,
				       DISPLAY_NEW);
	}
}

#ifdef CPU_DISKINFO
/*ARGSUSED*/
static void
machdep_diskinfo(HANDLER_ARGS)
{
	struct disklist *dl;
	struct biosdisk_info *bi;
	struct nativedisk_info *ni;
	int rc;
	size_t sz;
	uint i, b, lim;

	rc = sysctl(name, namelen, NULL, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}
	dl = malloc(sz);
	if (dl == NULL) {
		sysctlerror(1);
		return;
	}
	rc = sysctl(name, namelen, dl, &sz, NULL, 0);
	if (rc == -1) {
		sysctlerror(1);
		return;
	}

	if (!nflag)
		printf("%s: ", sname);
	lim = dl->dl_nbiosdisks;
	if (lim > MAX_BIOSDISKS)
		lim = MAX_BIOSDISKS;
	for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
		printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
		       bi->bi_dev, bi->bi_lbasecs,
		       bi->bi_cyl, bi->bi_head, bi->bi_sec,
		       bi->bi_flags);
	lim = dl->dl_nnativedisks;
	ni = dl->dl_nativedisks;
	bi = dl->dl_biosdisks;
	/* LINTED -- pointer casts are tedious */
	if ((char *)&ni[lim] != (char *)dl + sz) {
		fprintf(warnfp, "size mismatch\n");
		return;
	}
	for (i = 0; i < lim; ni++, i++) {
		char t = ':';
		printf(" %.*s", (int)sizeof ni->ni_devname,
		       ni->ni_devname);
		for (b = 0; b < ni->ni_nmatches; t = ',', b++)
			printf("%c%x", t,
			       bi[ni->ni_biosmatches[b]].bi_dev);
	}
	printf("\n");
}
#endif /* CPU_DISKINFO */