summaryrefslogtreecommitdiffstats
path: root/pttbbs/innbbsd/dbz.c
blob: 08b038e3e9484ddaf45e8fde11a6d55c6f0162ff (plain) (blame)
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
/*
 * dbz.c  V3.2
 * 
 * Copyright 1988 Jon Zeeff (zeeff@b-tech.ann-arbor.mi.us) You can use this code
 * in any manner, as long as you leave my name on it and don't hold me
 * responsible for any problems with it.
 * 
 * Hacked on by gdb@ninja.UUCP (David Butler); Sun Jun  5 00:27:08 CDT 1988
 * 
 * Various improvments + INCORE by moraes@ai.toronto.edu (Mark Moraes)
 * 
 * Major reworking by Henry Spencer as part of the C News project.
 * 
 * Minor lint and CodeCenter (Saber) fluff removal by Rich $alz (March, 1991).
 * Non-portable CloseOnExec() calls added by Rich $alz (September, 1991).
 * Added "writethrough" and tagmask calculation code from
 * <rob@violet.berkeley.edu> and <leres@ee.lbl.gov> by Rich $alz (December,
 * 1992). Merged in MMAP code by David Robinson, formerly
 * <david@elroy.jpl.nasa.gov> now <david.robinson@sun.com> (January, 1993).
 * 
 * These routines replace dbm as used by the usenet news software (it's not a
 * full dbm replacement by any means).  It's fast and simple.  It contains no
 * AT&T code.
 * 
 * In general, dbz's files are 1/20 the size of dbm's.  Lookup performance is
 * somewhat better, while file creation is spectacularly faster, especially
 * if the incore facility is used.
 * 
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#ifndef __STDC__
extern int      errno;
#endif
#include <dbz.h>
#include "clibrary.h"

/*
 * #ifdef index.  "LIA" = "leave it alone unless you know what you're doing".
 * 
 * FUNNYSEEKS   SEEK_SET is not 0, get it from <unistd.h> INDEX_SIZE
 * backward compatibility with old dbz; avoid using this NMEMORY
 * number of days of memory for use in sizing new table (LIA) INCORE
 * backward compatibility with old dbz; use dbzincore() instead DBZDEBUG
 * enable debugging DEFSIZE default table size (not as critical as in old
 * dbz) OLDBNEWS    default case mapping as in old B News; set NOBUFFER
 * BNEWS    default case mapping as in current B News; set NOBUFFER
 * DEFCASE  default case-map algorithm selector NOTAGS  fseek offsets
 * are strange, do not do tagging (see below) NPAGBUF   size of .pag buffer,
 * in longs (LIA) SHISTBUF  size of ASCII-file buffer, in bytes (LIA)
 * MAXRUN   length of run which shifts to next table (see below) (LIA)
 * OVERFLOW long-int arithmetic overflow must be avoided, will trap
 * NOBUFFER do not buffer hash-table i/o, B News locking is defective
 * MMAP     Use SunOS style mmap() for efficient incore
 */
/* SUPPRESS 530 *//* Empty body for statement */
/* SUPPRESS 701 on free *//* Conflicting declaration */

#ifdef FUNNYSEEKS
#include <unistd.h>
#else
#define SEEK_SET    0
#endif
#ifdef OVERFLOW
#include <limits.h>
#endif

static int      dbzversion = 3; /* for validating .dir file format */

/*
 * The dbz database exploits the fact that when news stores a <key,value>
 * tuple, the `value' part is a seek offset into a text file, pointing to a
 * copy of the `key' part.  This avoids the need to store a copy of the key
 * in the dbz files.  However, the text file *must* exist and be consistent
 * with the dbz files, or things will fail.
 * 
 * The basic format of the database is a simple hash table containing the
 * values.  A value is stored by indexing into the table using a hash value
 * computed from the key; collisions are resolved by linear probing (just
 * search forward for an empty slot, wrapping around to the beginning of the
 * table if necessary).  Linear probing is a performance disaster when the
 * table starts to get full, so a complication is introduced.  The database
 * is actually one *or more* tables, stored sequentially in the .pag file,
 * and the length of linear-probe sequences is limited.  The search (for an
 * existing item or an empty slot) always starts in the first table, and
 * whenever MAXRUN probes have been done in table N, probing continues in
 * table N+1.  This behaves reasonably well even in cases of massive
 * overflow.  There are some other small complications added, see comments
 * below.
 * 
 * The table size is fixed for any particular database, but is determined
 * dynamically when a database is rebuilt.  The strategy is to try to pick
 * the size so the first table will be no more than 2/3 full, that being
 * slightly before the point where performance starts to degrade.  (It is
 * desirable to be a bit conservative because the overflow strategy tends to
 * produce files with holes in them, which is a nuisance.)
 */

/*
 * The following is for backward compatibility.
 */
#ifdef INDEX_SIZE
#define DEFSIZE INDEX_SIZE
#endif

/*
 * ANSI C says an offset into a file is a long, not an off_t, for some
 * reason.  This actually does simplify life a bit, but it's still nice to
 * have a distinctive name for it.  Beware, this is just for readability,
 * don't try to change this.
 */
#define of_t    long
#define SOF (sizeof(of_t))

/*
 * We assume that unused areas of a binary file are zeros, and that the bit
 * pattern of `(of_t)0' is all zeros.  The alternative is rather painful file
 * initialization.  Note that okayvalue(), if OVERFLOW is defined, knows what
 * value of an offset would cause overflow.
 */
#define VACANT      ((of_t)0)
#define BIAS(o)     ((o)+1) /* make any valid of_t non-VACANT */
#define UNBIAS(o)   ((o)-1) /* reverse BIAS() effect */

/*
 * In a Unix implementation, or indeed any in which an of_t is a byte count,
 * there are a bunch of high bits free in an of_t.  There is a use for them.
 * Checking a possible hit by looking it up in the base file is relatively
 * expensive, and the cost can be dramatically reduced by using some of those
 * high bits to tag the value with a few more bits of the key's hash.  This
 * detects most false hits without the overhead of seek+read+strcmp.  We use
 * the top bit to indicate whether the value is tagged or not, and don't tag
 * a value which is using the tag bits itself. We're in trouble if the of_t
 * representation wants to use the top bit. The actual bitmasks and offset
 * come from the configuration stuff, which permits fiddling with them as
 * necessary, and also suppressing them completely (by defining the masks to
 * 0).  We build pre-shifted versions of the masks for efficiency.
 */
static of_t     tagbits;    /* pre-shifted tag mask */
static of_t     taghere;    /* pre-shifted tag-enable bit */
static of_t     tagboth;    /* tagbits|taghere */
#define HASTAG(o)   ((o)&taghere)
#define TAG(o)      ((o)&tagbits)
#define NOTAG(o)    ((o)&~tagboth)
#define CANTAG(o)   (((o)&tagboth) == 0)
#define MKTAG(v)    (((v)<<conf.tagshift)&tagbits)

/*
 * A new, from-scratch database, not built as a rebuild of an old one, needs
 * to know table size, casemap algorithm, and tagging.  Normally the user
 * supplies this info, but there have to be defaults.
 */
#ifndef DEFSIZE
#define DEFSIZE 120011      /* 300007 might be better */
#endif
#ifdef OLDBNEWS
#define DEFCASE '0'     /* B2.10 -- no mapping */
#define NOBUFFER        /* B News locking is defective */
#endif
#ifdef BNEWS
#define DEFCASE '='     /* B2.11 -- all mapped */
#define NOBUFFER        /* B News locking is defective */
#endif
#ifndef DEFCASE         /* C News compatibility is the default */
#define DEFCASE 'C'     /* C News -- RFC822 mapping */
#endif
#ifndef NOTAGS
#define TAGENB  0x80        /* tag enable is top bit, tag is next 7 */
#define TAGMASK 0x7f
#define TAGSHIFT    24
#else
#define TAGENB  0       /* no tags */
#define TAGMASK 0
#define TAGSHIFT    0
#endif

/*
 * We read configuration info from the .dir file into this structure, so we
 * can avoid wired-in assumptions for an existing database.
 * 
 * Among the info is a record of recent peak usages, so that a new table size
 * can be chosen intelligently when rebuilding.  10 is a good number of
 * usages to keep, since news displays marked fluctuations in volume on a
 * 7-day cycle.
 */
struct dbzconfig {
    int             olddbz; /* .dir file empty but .pag not? */
    of_t            tsize;  /* table size */
#ifndef NMEMORY
#define NMEMORY 10      /* # days of use info to remember */
#endif
#define NUSEDS  (1+NMEMORY)
    of_t            used[NUSEDS];   /* entries used today, yesterday, ... */
    int             valuesize;  /* size of table values, == SOF */
    int             bytemap[SOF];   /* byte-order map */
    char            casemap;    /* case-mapping algorithm (see cipoint()) */
    char            fieldsep;   /* field separator in base file, if any */
    of_t            tagenb; /* unshifted tag-enable bit */
    of_t            tagmask;    /* unshifted tag mask */
    int             tagshift;   /* shift count for tagmask and tagenb */
};
static struct dbzconfig conf;
static int      getconf();
static long     getno();
static int      putconf();
static void     mybytemap();
static of_t     bytemap();

/*
 * Using mmap() is a more efficent way of keeping the .pag file incore.  On
 * average, it cuts the number of system calls and buffer copies in half. It
 * also allows one copy to be shared among many processes without consuming
 * any extra resources.
 */
#ifdef MMAP
#include <sys/mman.h>
#ifdef MAP_FILE
#define MAP__ARG    (MAP_FILE | MAP_SHARED)
#else
#define MAP__ARG    (MAP_SHARED)
#endif
#ifndef INCORE
#define INCORE
#endif
#endif

/*
 * For a program that makes many, many references to the database, it is a
 * large performance win to keep the table in core, if it will fit. Note that
 * this does hurt robustness in the event of crashes, and dbmclose() *must*
 * be called to flush the in-core database to disk. The code is prepared to
 * deal with the possibility that there isn't enough memory.  There *is* an
 * assumption that a size_t is big enough to hold the size (in bytes) of one
 * table, so dbminit() tries to figure out whether this is possible first.
 * 
 * The preferred way to ask for an in-core table is to do dbzincore(1) before
 * dbminit().  The default is not to do it, although -DINCORE overrides this
 * for backward compatibility with old dbz.
 * 
 * We keep only the first table in core.  This greatly simplifies the code, and
 * bounds memory demand.  Furthermore, doing this is a large performance win
 * even in the event of massive overflow.
 */
#ifdef INCORE
static int      incore = 1;
#else
static int      incore = 0;
#endif

/*
 * Write to filesystem even if incore?  This replaces a single multi-
 * megabyte write when doing a dbzsync with a multi-byte write each time an
 * article is added.  On most systems, this will give an overall performance
 * boost.
 */
static int      writethrough = 0;

/*
 * Stdio buffer for .pag reads.  Buffering more than about 16 does not help
 * significantly at the densities we try to maintain, and the much larger
 * buffers that most stdios default to are much more expensive to fill. With
 * small buffers, stdio is performance-competitive with raw read(), and it's
 * much more portable.
 */
#ifndef NPAGBUF
#define NPAGBUF 16
#endif
#ifndef NOBUFFER
#ifdef _IOFBF
static of_t     pagbuf[NPAGBUF];/* only needed if !NOBUFFER && _IOFBF */
#endif
#endif

/*
 * Stdio buffer for base-file reads.  Message-IDs (all news ever needs to
 * read) are essentially never longer than 64 bytes, and the typical stdio
 * buffer is so much larger that it is much more expensive to fill.
 */
#ifndef SHISTBUF
#define SHISTBUF    64
#endif
#ifdef _IOFBF
static char     basebuf[SHISTBUF];  /* only needed if _IOFBF exists */
#endif

/*
 * Data structure for recording info about searches.
 */
struct searcher {
    of_t            place;  /* current location in file */
    int             tabno;  /* which table we're in */
    int             run;    /* how long we'll stay in this table */
#ifndef MAXRUN
#define MAXRUN  100
#endif
    long            hash;   /* the key's hash code (for optimization) */
    of_t            tag;    /* tag we are looking for */
    int             seen;   /* have we examined current location? */
    int             aborted;    /* has i/o error aborted search? */
};
static void     start();
#define FRESH   ((struct searcher *)NULL)
static of_t     search();
#define NOTFOUND    ((of_t)-1)
static int      okayvalue();
static int      set();

/*
 * Arguably the searcher struct for a given routine ought to be local to it,
 * but a fetch() is very often immediately followed by a store(), and in some
 * circumstances it is a useful performance win to remember where the fetch()
 * completed.  So we use a global struct and remember whether it is current.
 */
static struct searcher srch;
static struct searcher *prevp;  /* &srch or FRESH */

/* byte-ordering stuff */
static int      mybmap[SOF];    /* my byte order (see mybytemap()) */
static int      bytesame;   /* is database order same as mine? */
#define MAPIN(o)    ((bytesame) ? (o) : bytemap((o), conf.bytemap, mybmap))
#define MAPOUT(o)   ((bytesame) ? (o) : bytemap((o), mybmap, conf.bytemap))

/*
 * The double parentheses needed to make this work are ugly, but the
 * alternative (under most compilers) is to pack around 2K of unused strings
 * -- there's just no way to get rid of them.
 */
#ifdef DBZDEBUG
static int      debug;      /* controlled by dbzdebug() */
#define DEBUG(args) if (debug) { (void) printf args ; } else
#else
#define DEBUG(args) ;
#endif

/* externals used */
#if 0
extern char    *memcpy();
extern char    *memchr();
extern char    *malloc();
extern char    *calloc();
extern void     free();     /* ANSI C; some old implementations say int */
#endif              /* 0 */
extern int      atoi();
extern long     atol();
extern void     CloseOnExec();

/* misc. forwards */
static long     hash();
static void     crcinit();
static char    *cipoint();
static char    *mapcase();
static int      isprime();
static FILE    *latebase();

/* file-naming stuff */
static char     dir[] = ".dir";
static char     pag[] = ".pag";
static char    *enstring();

/* central data structures */
static FILE    *basef;      /* descriptor for base file */
static char    *basefname;  /* name for not-yet-opened base file */
static FILE    *dirf;       /* descriptor for .dir file */
static int      dirronly;   /* dirf open read-only? */
static FILE    *pagf = NULL;    /* descriptor for .pag file */
static of_t     pagpos;     /* posn in pagf; only search may set != -1 */
static int      pagronly;   /* pagf open read-only? */
static of_t    *corepag;    /* incore version of .pag file, if any */
static FILE    *bufpagf;    /* well-buffered pagf, for incore rewrite */
static of_t    *getcore();
#ifndef MMAP
static int      putcore();
#endif
static int      written;    /* has a store() been done? */

/*
 * - dbzfresh - set up a new database, no historical info
 */
int             /* 0 success, -1 failure */
dbzfresh(name, size, fs, cmap, tagmask)
    char           *name;   /* base name; .dir and .pag must exist */
    long            size;   /* table size (0 means default) */
    int             fs;     /* field-separator character in base file */
    int             cmap;   /* case-map algorithm (0 means default) */
    of_t            tagmask;    /* 0 default, 1 no tags */
{
    register char  *fn;
    struct dbzconfig c;
    register of_t   m;
    register FILE  *f;

    if (pagf != NULL) {
    DEBUG(("dbzfresh: database already open\n"));
    return (-1);
    }
    if (size != 0 && size < 2) {
    DEBUG(("dbzfresh: preposterous size (%ld)\n", size));
    return (-1);
    }
    /* get default configuration */
    if (getconf((FILE *) NULL, (FILE *) NULL, &c) < 0)
    return (-1);        /* "can't happen" */

    /* and mess with it as specified */
    if (size != 0)
    c.tsize = size;
    c.fieldsep = fs;
    switch (cmap) {
    case 0:
    case '0':
    case 'B':           /* 2.10 compat */
    c.casemap = '0';    /* '\0' nicer, but '0' printable! */
    break;
    case '=':
    case 'b':           /* 2.11 compat */
    c.casemap = '=';
    break;
    case 'C':
    c.casemap = 'C';
    break;
    case '?':
    c.casemap = DEFCASE;
    break;
    default:
    DEBUG(("dbzfresh case map `%c' unknown\n", cmap));
    return (-1);
    }
    switch ((int)tagmask) {
    case 0:         /* default */
    break;
    case 1:         /* no tags */
    c.tagshift = 0;
    c.tagmask = 0;
    c.tagenb = 0;
    break;
    default:
    m = tagmask;
    c.tagshift = 0;
    while (!(m & 01)) {
        m >>= 1;
        c.tagshift++;
    }
    c.tagmask = m;
    c.tagenb = (m << 1) & ~m;
    break;
    }

    /* write it out */
    fn = enstring(name, dir);
    if (fn == NULL)
    return (-1);
    f = fopen(fn, "w");
    free((POINTER) fn);
    if (f == NULL) {
    DEBUG(("dbzfresh: unable to write config\n"));
    return (-1);
    }
    if (putconf(f, &c) < 0) {
    (void)fclose(f);
    return (-1);
    }
    if (fclose(f) == EOF) {
    DEBUG(("dbzfresh: fclose failure\n"));
    return (-1);
    }
    /* create/truncate .pag */
    fn = enstring(name, pag);
    if (fn == NULL)
    return (-1);
    f = fopen(fn, "w");
    free((POINTER) fn);
    if (f == NULL) {
    DEBUG(("dbzfresh: unable to create/truncate .pag file\n"));
    return (-1);
    } else
    (void)fclose(f);

    /* and punt to dbminit for the hard work */
    return (dbminit(name));
}

/*
 * - dbzsize - what's a good table size to hold this many entries?
 */
long
dbzsize(contents)
    long            contents;   /* 0 means what's the default */
{
    register long   n;

    if (contents <= 0) {    /* foulup or default inquiry */
    DEBUG(("dbzsize: preposterous input (%ld)\n", contents));
    return (DEFSIZE);
    }
    n = (contents / 2) * 3; /* try to keep table at most 2/3 full */
    if (!(n & 01))      /* make it odd */
    n++;
    DEBUG(("dbzsize: tentative size %ld\n", n));
    while (!isprime(n))     /* and look for a prime */
    n += 2;
    DEBUG(("dbzsize: final size %ld\n", n));

    return (n);
}

/*
 * - isprime - is a number prime?
 * 
 * This is not a terribly efficient approach.
 */
static int          /* predicate */
isprime(x)
    register long   x;
{
    static int      quick[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 0};
    register int   *ip;
    register long   div;
    register long   stop;

    /* hit the first few primes quickly to eliminate easy ones */
    /* this incidentally prevents ridiculously small tables */
    for (ip = quick; (div = *ip) != 0; ip++)
    if (x % div == 0) {
        DEBUG(("isprime: quick result on %ld\n", (long)x));
        return (0);
    }
    /* approximate square root of x */
    for (stop = x; x / stop < stop; stop >>= 1)
    continue;
    stop <<= 1;

    /* try odd numbers up to stop */
    for (div = *--ip; div < stop; div += 2)
    if (x % div == 0)
        return (0);

    return (1);
}

/*
 * - dbzagain - set up a new database to be a rebuild of an old one
 */
int             /* 0 success, -1 failure */
dbzagain(name, oldname)
    char           *name;   /* base name; .dir and .pag must exist */
    char           *oldname;    /* base name; all must exist */
{
    register char  *fn;
    struct dbzconfig c;
    register int    i;
    register long   top;
    register FILE  *f;
    register int    newtable;
    register of_t   newsize;
    struct stat     sb;
    register of_t   m;

    if (pagf != NULL) {
    DEBUG(("dbzagain: database already open\n"));
    return (-1);
    }
    /* pick up the old configuration */
    fn = enstring(oldname, dir);
    if (fn == NULL)
    return (-1);
    f = fopen(fn, "r");
    free((POINTER) fn);
    if (f == NULL) {
    DEBUG(("dbzagain: cannot open old .dir file\n"));
    return (-1);
    }
    i = getconf(f, (FILE *) NULL, &c);
    (void)fclose(f);
    if (i < 0) {
    DEBUG(("dbzagain: getconf failed\n"));
    return (-1);
    }
    /* calculate tagging from old file */
    if (stat(oldname, &sb) != -1) {
    for (m = 1, i = 0; m < sb.st_size; i++, m <<= 1)
        continue;

    /* if we had more tags than the default, use the new data */
    if ((c.tagmask | c.tagenb) && m > (1 << TAGSHIFT)) {
        c.tagshift = i;
        c.tagmask = (~(unsigned long)0) >> (i + 1);
        c.tagenb = (c.tagmask << 1) & ~c.tagmask;
    }
    }
    /* tinker with it */
    top = 0;
    newtable = 0;
    for (i = 0; i < NUSEDS; i++) {
    if (top < c.used[i])
        top = c.used[i];
    if (c.used[i] == 0)
        newtable = 1;   /* hasn't got full usage history yet */
    }
    if (top == 0) {
    DEBUG(("dbzagain: old table has no contents!\n"));
    newtable = 1;
    }
    for (i = NUSEDS - 1; i > 0; i--)
    c.used[i] = c.used[i - 1];
    c.used[0] = 0;
    newsize = dbzsize(top);
    if (!newtable || newsize > c.tsize) /* don't shrink new table */
    c.tsize = newsize;

    /* write it out */
    fn = enstring(name, dir);
    if (fn == NULL)
    return (-1);
    f = fopen(fn, "w");
    free((POINTER) fn);
    if (f == NULL) {
    DEBUG(("dbzagain: unable to write new .dir\n"));
    return (-1);
    }
    i = putconf(f, &c);
    (void)fclose(f);
    if (i < 0) {
    DEBUG(("dbzagain: putconf failed\n"));
    return (-1);
    }
    /* create/truncate .pag */
    fn = enstring(name, pag);
    if (fn == NULL)
    return (-1);
    f = fopen(fn, "w");
    free((POINTER) fn);
    if (f == NULL) {
    DEBUG(("dbzagain: unable to create/truncate .pag file\n"));
    return (-1);
    } else
    (void)fclose(f);

    /* and let dbminit do the work */
    return (dbminit(name));
}

/*
 * - dbminit - open a database, creating it (using defaults) if necessary
 * 
 * We try to leave errno set plausibly, to the extent that underlying functions
 * permit this, since many people consult it if dbminit() fails.
 */
int             /* 0 success, -1 failure */
dbminit(name)
    char           *name;
{
    register int    i;
    register size_t s;
    register char  *dirfname;
    register char  *pagfname;

    if (pagf != NULL) {
    DEBUG(("dbminit: dbminit already called once\n"));
    errno = 0;
    return (-1);
    }
    /* open the .dir file */
    dirfname = enstring(name, dir);
    if (dirfname == NULL)
    return (-1);
    dirf = fopen(dirfname, "r+");
    if (dirf == NULL) {
    dirf = fopen(dirfname, "r");
    dirronly = 1;
    } else
    dirronly = 0;
    free((POINTER) dirfname);
    if (dirf == NULL) {
    DEBUG(("dbminit: can't open .dir file\n"));
    return (-1);
    }
    CloseOnExec((int)fileno(dirf), 1);

    /* open the .pag file */
    pagfname = enstring(name, pag);
    if (pagfname == NULL) {
    (void)fclose(dirf);
    return (-1);
    }
    pagf = fopen(pagfname, "r+b");
    if (pagf == NULL) {
    pagf = fopen(pagfname, "rb");
    if (pagf == NULL) {
        DEBUG(("dbminit: .pag open failed\n"));
        (void)fclose(dirf);
        free((POINTER) pagfname);
        return (-1);
    }
    pagronly = 1;
    } else if (dirronly)
    pagronly = 1;
    else
    pagronly = 0;
    if (pagf != NULL)
    CloseOnExec((int)fileno(pagf), 1);
#ifdef NOBUFFER
    /*
     * B News does not do adequate locking on its database accesses. Why it
     * doesn't get into trouble using dbm is a mystery.  In any case, doing
     * unbuffered i/o does not cure the problem, but does enormously reduce
     * its incidence.
     */
    (void)setbuf(pagf, (char *)NULL);
#else
#ifdef _IOFBF
    (void)setvbuf(pagf, (char *)pagbuf, _IOFBF, sizeof(pagbuf));
#endif
#endif
    pagpos = -1;
    /* don't free pagfname, need it below */

    /* open the base file */
    basef = fopen(name, "r");
    if (basef == NULL) {
    DEBUG(("dbminit: basefile open failed\n"));
    basefname = enstring(name, "");
    if (basefname == NULL) {
        (void)fclose(pagf);
        (void)fclose(dirf);
        free((POINTER) pagfname);
        pagf = NULL;
        return (-1);
    }
    } else
    basefname = NULL;
    if (basef != NULL)
    CloseOnExec((int)fileno(basef), 1);
#ifdef _IOFBF
    if (basef != NULL)
    (void)setvbuf(basef, basebuf, _IOFBF, sizeof(basebuf));
#endif

    /* pick up configuration */
    if (getconf(dirf, pagf, &conf) < 0) {
    DEBUG(("dbminit: getconf failure\n"));
    (void)fclose(basef);
    (void)fclose(pagf);
    (void)fclose(dirf);
    free((POINTER) pagfname);
    pagf = NULL;
    errno = EDOM;       /* kind of a kludge, but very portable */
    return (-1);
    }
    tagbits = conf.tagmask << conf.tagshift;
    taghere = conf.tagenb << conf.tagshift;
    tagboth = tagbits | taghere;
    mybytemap(mybmap);
    bytesame = 1;
    for (i = 0; i < SOF; i++)
    if (mybmap[i] != conf.bytemap[i])
        bytesame = 0;

    /* get first table into core, if it looks desirable and feasible */
    s = (size_t) conf.tsize * SOF;
    if (incore && (of_t) (s / SOF) == conf.tsize) {
    bufpagf = fopen(pagfname, (pagronly) ? "rb" : "r+b");
    if (bufpagf != NULL) {
        corepag = getcore(bufpagf);
        CloseOnExec((int)fileno(bufpagf), 1);
    }
    } else {
    bufpagf = NULL;
    corepag = NULL;
    }
    free((POINTER) pagfname);

    /* misc. setup */
    crcinit();
    written = 0;
    prevp = FRESH;
    DEBUG(("dbminit: succeeded\n"));
    return (0);
}

/*
 * - enstring - concatenate two strings into a malloced area
 */
static char    *        /* NULL if malloc fails */
enstring(s1, s2)
    char           *s1;
    char           *s2;
{
    register char  *p;

    p = malloc((size_t) strlen(s1) + (size_t) strlen(s2) + 1);
    if (p != NULL) {
    (void)strcpy(p, s1);
    (void)strcat(p, s2);
    } else {
    DEBUG(("enstring(%s, %s) out of memory\n", s1, s2));
    }
    return (p);
}

/*
 * - dbmclose - close a database
 */
int
dbmclose()
{
    register int    ret = 0;

    if (pagf == NULL) {
    DEBUG(("dbmclose: not opened!\n"));
    return (-1);
    }
    if (fclose(pagf) == EOF) {
    DEBUG(("dbmclose: fclose(pagf) failed\n"));
    ret = -1;
    }
    pagf = basef;       /* ensure valid pointer; dbzsync checks it */
    if (dbzsync() < 0)
    ret = -1;
    if (bufpagf != NULL && fclose(bufpagf) == EOF) {
    DEBUG(("dbmclose: fclose(bufpagf) failed\n"));
    ret = -1;
    }
    if (corepag != NULL)
#ifdef MMAP
    if (munmap((caddr_t) corepag, (int)conf.tsize * SOF) == -1) {
        DEBUG(("dbmclose: munmap failed\n"));
        ret = -1;
    }
#else
    free((POINTER) corepag);
#endif
    corepag = NULL;
    if (basef) {
    if (fclose(basef) == EOF) {
        DEBUG(("dbmclose: fclose(basef) failed\n"));
        ret = -1;
    }
    }
    if (basefname != NULL)
    free((POINTER) basefname);
    basef = NULL;
    pagf = NULL;
    if (fclose(dirf) == EOF) {
    DEBUG(("dbmclose: fclose(dirf) failed\n"));
    ret = -1;
    }
    DEBUG(("dbmclose: %s\n", (ret == 0) ? "succeeded" : "failed"));
    return (ret);
}

/*
 * - dbzsync - push all in-core data out to disk
 */
int
dbzsync()
{
    register int    ret = 0;

    if (pagf == NULL) {
    DEBUG(("dbzsync: not opened!\n"));
    return (-1);
    }
    if (!written)
    return (0);

#ifndef MMAP
    if (corepag != NULL && !writethrough) {
    if (putcore(corepag, bufpagf) < 0) {
        DEBUG(("dbzsync: putcore failed\n"));
        ret = -1;
    }
    }
#endif
    if (!conf.olddbz)
    if (putconf(dirf, &conf) < 0)
        ret = -1;

    DEBUG(("dbzsync: %s\n", (ret == 0) ? "succeeded" : "failed"));
    return (ret);
}

/*
 * - dbzcancel - cancel writing of in-core data Mostly for use from child
 * processes. Note that we don't need to futz around with stdio buffers,
 * because we always fflush them immediately anyway and so they never have
 * stale data.
 */
int
dbzcancel()
{
    if (pagf == NULL) {
    DEBUG(("dbzcancel: not opened!\n"));
    return (-1);
    }
    written = 0;
    return (0);
}

/*
 * - dbzfetch - fetch() with case mapping built in
 */
datum
dbzfetch(key)
    datum           key;
{
    char            buffer[DBZMAXKEY + 1];
    datum           mappedkey;
    register size_t keysize;

    DEBUG(("dbzfetch: (%s)\n", key.dptr));

    /* Key is supposed to be less than DBZMAXKEY */
    keysize = key.dsize;
    if (keysize >= DBZMAXKEY) {
    keysize = DBZMAXKEY;
    DEBUG(("keysize is %d - truncated to %d\n", key.dsize, DBZMAXKEY));
    }
    mappedkey.dptr = mapcase(buffer, key.dptr, keysize);
    buffer[keysize] = '\0'; /* just a debug aid */
    mappedkey.dsize = keysize;

    return (fetch(mappedkey));
}

/*
 * - fetch - get an entry from the database
 * 
 * Disgusting fine point, in the name of backward compatibility:  if the last
 * character of "key" is a NUL, that character is (effectively) not part of
 * the comparison against the stored keys.
 */
datum               /* dptr NULL, dsize 0 means failure */
fetch(key)
    datum           key;
{
    char            buffer[DBZMAXKEY + 1];
    static of_t     key_ptr;    /* return value points here */
    datum           output;
    register size_t keysize;
    register size_t cmplen;
    register char  *sepp;

    DEBUG(("fetch: (%s)\n", key.dptr));
    output.dptr = NULL;
    output.dsize = 0;
    prevp = FRESH;

    /* Key is supposed to be less than DBZMAXKEY */
    keysize = key.dsize;
    if (keysize >= DBZMAXKEY) {
    keysize = DBZMAXKEY;
    DEBUG(("keysize is %d - truncated to %d\n", key.dsize, DBZMAXKEY));
    }
    if (pagf == NULL) {
    DEBUG(("fetch: database not open!\n"));
    return (output);
    } else if (basef == NULL) { /* basef didn't exist yet */
    basef = latebase();
    if (basef == NULL)
        return (output);
    }
    cmplen = keysize;
    sepp = &conf.fieldsep;
    if (key.dptr[keysize - 1] == '\0') {
    cmplen--;
    sepp = &buffer[keysize - 1];
    }
    start(&srch, &key, FRESH);
    while ((key_ptr = search(&srch)) != NOTFOUND) {
    DEBUG(("got 0x%lx\n", key_ptr));

    /* fetch the key */
    if (fseek(basef, key_ptr, SEEK_SET) != 0) {
        DEBUG(("fetch: seek failed\n"));
        return (output);
    }
    if (fread((POINTER) buffer, 1, keysize, basef) != keysize) {
        DEBUG(("fetch: read failed\n"));
        return (output);
    }
    /* try it */
    buffer[keysize] = '\0'; /* terminated for DEBUG */
    (void)mapcase(buffer, buffer, keysize);
    DEBUG(("fetch: buffer (%s) looking for (%s) size = %ld\n",
           buffer, key.dptr, (long)keysize));
    if (memcmp((POINTER) key.dptr, (POINTER) buffer, cmplen) == 0 &&
        (*sepp == conf.fieldsep || *sepp == '\0')) {
        /* we found it */
        output.dptr = (char *)&key_ptr;
        output.dsize = SOF;
        DEBUG(("fetch: successful\n"));
        return (output);
    }
    }

    /* we didn't find it */
    DEBUG(("fetch: failed\n"));
    prevp = &srch;      /* remember where we stopped */
    return (output);
}

/*
 * - latebase - try to open a base file that wasn't there at the start
 */
static FILE    *
latebase()
{
    register FILE  *it;

    if (basefname == NULL) {
    DEBUG(("latebase: name foulup\n"));
    return (NULL);
    }
    it = fopen(basefname, "r");
    if (it == NULL) {
    DEBUG(("latebase: still can't open base\n"));
    } else {
    DEBUG(("latebase: late open succeeded\n"));
    free((POINTER) basefname);
    basefname = NULL;
#ifdef _IOFBF
    (void)setvbuf(it, basebuf, _IOFBF, sizeof(basebuf));
#endif
    }
    if (it != NULL)
    CloseOnExec((int)fileno(it), 1);
    return (it);
}

/*
 * - dbzstore - store() with case mapping built in
 */
int
dbzstore(key, data)
    datum           key;
    datum           data;
{
    char            buffer[DBZMAXKEY + 1];
    datum           mappedkey;
    register size_t keysize;

    DEBUG(("dbzstore: (%s)\n", key.dptr));

    /* Key is supposed to be less than DBZMAXKEY */
    keysize = key.dsize;
    if (keysize >= DBZMAXKEY) {
    DEBUG(("dbzstore: key size too big (%d)\n", key.dsize));
    return (-1);
    }
    mappedkey.dptr = mapcase(buffer, key.dptr, keysize);
    buffer[keysize] = '\0'; /* just a debug aid */
    mappedkey.dsize = keysize;

    return (store(mappedkey, data));
}

/*
 * - store - add an entry to the database
 */
int             /* 0 success, -1 failure */
store(key, data)
    datum           key;
    datum           data;
{
    of_t            value;

    if (pagf == NULL) {
    DEBUG(("store: database not open!\n"));
    return (-1);
    } else if (basef == NULL) { /* basef didn't exist yet */
    basef = latebase();
    if (basef == NULL)
        return (-1);
    }
    if (pagronly) {
    DEBUG(("store: database open read-only\n"));
    return (-1);
    }
    if (data.dsize != SOF) {
    DEBUG(("store: value size wrong (%d)\n", data.dsize));
    return (-1);
    }
    if (key.dsize >= DBZMAXKEY) {
    DEBUG(("store: key size too big (%d)\n", key.dsize));
    return (-1);
    }
    /* copy the value in to ensure alignment */
    (void)memcpy((POINTER) & value, (POINTER) data.dptr, SOF);
    DEBUG(("store: (%s, %ld)\n", key.dptr, (long)value));
    if (!okayvalue(value)) {
    DEBUG(("store: reserved bit or overflow in 0x%lx\n", value));
    return (-1);
    }
    /* find the place, exploiting previous search if possible */
    start(&srch, &key, prevp);
    while (search(&srch) != NOTFOUND)
    continue;

    prevp = FRESH;
    conf.used[0]++;
    DEBUG(("store: used count %ld\n", conf.used[0]));
    written = 1;
    return (set(&srch, value));
}

/*
 * - dbzincore - control attempts to keep .pag file in core
 */
int             /* old setting */
dbzincore(value)
    int             value;
{
    register int    old = incore;

#ifndef MMAP
    incore = value;
#endif
    return (old);
}

/*
 * - dbzwritethrough - write through the pag file in core
 */
int             /* old setting */
dbzwritethrough(value)
    int             value;
{
    register int    old = writethrough;

    writethrough = value;
    return (old);
}

/*
 * - dbztagmask - calculate the correct tagmask for the given base file size
 */
long
dbztagmask(size)
    register long   size;
{
    register long   m;
    register long   tagmask;
    register int    i;

    if (size <= 0)
    return (0L);        /* silly size */

    for (m = 1, i = 0; m < size; i++, m <<= 1)
    continue;

    if (m < (1 << TAGSHIFT))
    return (0L);        /* not worth tagging */

    tagmask = (~(unsigned long)0) >> (i + 1);
    tagmask = tagmask << i;
    return (tagmask);
}

/*
 * - getconf - get configuration from .dir file
 */
static int          /* 0 success, -1 failure */
getconf(df, pf, cp)
    register FILE  *df;     /* NULL means just give me the default */
    register FILE  *pf;     /* NULL means don't care about .pag */
    register struct dbzconfig *cp;
{
    register int    c;
    register int    i;
    int             err = 0;

    c = (df != NULL) ? getc(df) : EOF;
    if (c == EOF) {     /* empty file, no configuration known */
    cp->olddbz = 0;
    if (df != NULL && pf != NULL && getc(pf) != EOF)
        cp->olddbz = 1;
    cp->tsize = DEFSIZE;
    cp->fieldsep = '\t';
    for (i = 0; i < NUSEDS; i++)
        cp->used[i] = 0;
    cp->valuesize = SOF;
    mybytemap(cp->bytemap);
    cp->casemap = DEFCASE;
    cp->tagenb = TAGENB;
    cp->tagmask = TAGMASK;
    cp->tagshift = TAGSHIFT;
    DEBUG(("getconf: defaults (%ld, %c, (0x%lx/0x%lx<<%d))\n",
           cp->tsize, cp->casemap, cp->tagenb,
           cp->tagmask, cp->tagshift));
    return (0);
    }
    (void)ungetc(c, df);

    /* first line, the vital stuff */
    if (getc(df) != 'd' || getc(df) != 'b' || getc(df) != 'z')
    err = -1;
    if (getno(df, &err) != dbzversion)
    err = -1;
    cp->tsize = getno(df, &err);
    cp->fieldsep = (int)getno(df, &err);
    while ((c = getc(df)) == ' ')
    continue;
    cp->casemap = c;
    cp->tagenb = getno(df, &err);
    cp->tagmask = getno(df, &err);
    cp->tagshift = getno(df, &err);
    cp->valuesize = getno(df, &err);
    if (cp->valuesize != SOF) {
    DEBUG(("getconf: wrong of_t size (%d)\n", cp->valuesize));
    err = -1;
    cp->valuesize = SOF;    /* to protect the loops below */
    }
    for (i = 0; i < cp->valuesize; i++)
    cp->bytemap[i] = getno(df, &err);
    if (getc(df) != '\n')
    err = -1;
#ifdef DBZDEBUG
    DEBUG(("size %ld, sep %d, cmap %c, tags 0x%lx/0x%lx<<%d, ", cp->tsize,
       cp->fieldsep, cp->casemap, cp->tagenb, cp->tagmask,
       cp->tagshift));
    DEBUG(("bytemap (%d)", cp->valuesize));
    for (i = 0; i < cp->valuesize; i++) {
    DEBUG((" %d", cp->bytemap[i]));
    }
    DEBUG(("\n"));
#endif

    /* second line, the usages */
    for (i = 0; i < NUSEDS; i++)
    cp->used[i] = getno(df, &err);
    if (getc(df) != '\n')
    err = -1;
    DEBUG(("used %ld %ld %ld...\n", cp->used[0], cp->used[1], cp->used[2]));

    if (err < 0) {
    DEBUG(("getconf error\n"));
    return (-1);
    }
    return (0);
}

/*
 * - getno - get a long
 */
static long
getno(f, ep)
    FILE           *f;
    int            *ep;
{
    register char  *p;
#define MAXN    50
    char            getbuf[MAXN];
    register int    c;

    while ((c = getc(f)) == ' ')
    continue;
    if (c == EOF || c == '\n') {
    DEBUG(("getno: missing number\n"));
    *ep = -1;
    return (0);
    }
    p = getbuf;
    *p++ = c;
    while ((c = getc(f)) != EOF && c != '\n' && c != ' ')
    if (p < &getbuf[MAXN - 1])
        *p++ = c;
    if (c == EOF) {
    DEBUG(("getno: EOF\n"));
    *ep = -1;
    } else
    (void)ungetc(c, f);
    *p = '\0';

    if (strspn(getbuf, "-1234567890") != strlen(getbuf)) {
    DEBUG(("getno: `%s' non-numeric\n", getbuf));
    *ep = -1;
    }
    return (atol(getbuf));
}

/*
 * - putconf - write configuration to .dir file
 */
static int          /* 0 success, -1 failure */
putconf(f, cp)
    register FILE  *f;
    register struct dbzconfig *cp;
{
    register int    i;
    register int    ret = 0;

    if (fseek(f, (of_t) 0, SEEK_SET) != 0) {
    DEBUG(("fseek failure in putconf\n"));
    ret = -1;
    }
    (void)fprintf(f, "dbz %d %ld %d %c %ld %ld %d %d", dbzversion, cp->tsize,
          cp->fieldsep, cp->casemap, cp->tagenb,
          cp->tagmask, cp->tagshift, cp->valuesize);
    for (i = 0; i < cp->valuesize; i++)
    (void)fprintf(f, " %d", cp->bytemap[i]);
    (void)fprintf(f, "\n");
    for (i = 0; i < NUSEDS; i++)
    (void)fprintf(f, "%ld%c", cp->used[i], (i < NUSEDS - 1) ? ' ' : '\n');

    (void)fflush(f);
    if (ferror(f))
    ret = -1;

    DEBUG(("putconf status %d\n", ret));
    return (ret);
}

/*
 * - getcore - try to set up an in-core copy of .pag file
 */
static of_t    *        /* pointer to copy, or NULL */
getcore(f)
    FILE           *f;
{
    register char  *it;
#ifdef MMAP
    struct stat     st;

    if (fstat(fileno(f), &st) == -1) {
    DEBUG(("getcore: fstat failed\n"));
    return (NULL);
    }
    if (((size_t) conf.tsize * SOF) > st.st_size) {
    /* file too small; extend it */
    if (ftruncate((int)fileno(f), conf.tsize * SOF) == -1) {
        DEBUG(("getcore: ftruncate failed\n"));
        return (NULL);
    }
    }
    it = mmap((caddr_t) 0, (size_t) conf.tsize * SOF,
          pagronly ? PROT_READ : PROT_WRITE | PROT_READ, MAP__ARG,
          (int)fileno(f), (off_t) 0);
    if (it == (char *)-1) {
    DEBUG(("getcore: mmap failed\n"));
    return (NULL);
    }
#ifdef MC_ADVISE
    /* not present in all versions of mmap() */
    madvise(it, (size_t) conf.tsize * SOF, MADV_RANDOM);
#endif
#else
    register of_t  *p;
    register size_t i;
    register size_t nread;
    it = malloc((size_t) conf.tsize * SOF);
    if (it == NULL) {
    DEBUG(("getcore: malloc failed\n"));
    return (NULL);
    }
    nread = fread((POINTER) it, SOF, (size_t) conf.tsize, f);
    if (ferror(f)) {
    DEBUG(("getcore: read failed\n"));
    free((POINTER) it);
    return (NULL);
    }
    /* NOSTRICT *//* Possible pointer alignment problem */
    p = (of_t *) it + nread;
    i = (size_t) conf.tsize - nread;
    while (i-- > 0)
    *p++ = VACANT;
#endif
    /* NOSTRICT *//* Possible pointer alignment problem */
    return ((of_t *) it);
}

#ifndef MMAP
/*
 * - putcore - try to rewrite an in-core table
 */
static int          /* 0 okay, -1 fail */
putcore(tab, f)
    of_t           *tab;
    FILE           *f;
{
    if (fseek(f, (of_t) 0, SEEK_SET) != 0) {
    DEBUG(("fseek failure in putcore\n"));
    return (-1);
    }
    (void)fwrite((POINTER) tab, SOF, (size_t) conf.tsize, f);
    (void)fflush(f);
    return ((ferror(f)) ? -1 : 0);
}
#endif

/*
 * - start - set up to start or restart a search
 */
static void
start(sp, kp, osp)
    register struct searcher *sp;
    register datum *kp;
    register struct searcher *osp;  /* may be FRESH, i.e. NULL */
{
    register long   h;

    h = hash(kp->dptr, kp->dsize);
    if (osp != FRESH && osp->hash == h) {
    if (sp != osp)
        *sp = *osp;
    DEBUG(("search restarted\n"));
    } else {
    sp->hash = h;
    sp->tag = MKTAG(h / conf.tsize);
    DEBUG(("tag 0x%lx\n", sp->tag));
    sp->place = h % conf.tsize;
    sp->tabno = 0;
    sp->run = (conf.olddbz) ? conf.tsize : MAXRUN;
    sp->aborted = 0;
    }
    sp->seen = 0;
}

/*
 * - search - conduct part of a search
 */
static          of_t        /* NOTFOUND if we hit VACANT or error */
search(sp)
    register struct searcher *sp;
{
    register of_t   dest;
    register of_t   value;
    of_t            val;    /* buffer for value (can't fread register) */
    register of_t   place;

    if (sp->aborted)
    return (NOTFOUND);

    for (;;) {
    /* determine location to be examined */
    place = sp->place;
    if (sp->seen) {
        /* go to next location */
        if (--sp->run <= 0) {
        sp->tabno++;
        sp->run = MAXRUN;
        }
        place = (place + 1) % conf.tsize + sp->tabno * conf.tsize;
        sp->place = place;
    } else
        sp->seen = 1;   /* now looking at current location */
    DEBUG(("search @ %ld\n", place));

    /* get the tagged value */
    if (corepag != NULL && place < conf.tsize) {
        DEBUG(("search: in core\n"));
        value = MAPIN(corepag[place]);
    } else {
        /* seek, if necessary */
        dest = place * SOF;
        if (pagpos != dest) {
        if (fseek(pagf, dest, SEEK_SET) != 0) {
            DEBUG(("search: seek failed\n"));
            pagpos = -1;
            sp->aborted = 1;
            return (NOTFOUND);
        }
        pagpos = dest;
        }
        /* read it */
        if (fread((POINTER) & val, sizeof(val), 1, pagf) == 1)
        value = MAPIN(val);
        else if (ferror(pagf)) {
        DEBUG(("search: read failed\n"));
        pagpos = -1;
        sp->aborted = 1;
        return (NOTFOUND);
        } else
        value = VACANT;

        /* and finish up */
        pagpos += sizeof(val);
    }

    /* vacant slot is always cause to return */
    if (value == VACANT) {
        DEBUG(("search: empty slot\n"));
        return (NOTFOUND);
    };

    /* check the tag */
    value = UNBIAS(value);
    DEBUG(("got 0x%lx\n", value));
    if (!HASTAG(value)) {
        DEBUG(("tagless\n"));
        return (value);
    } else if (TAG(value) == sp->tag) {
        DEBUG(("match\n"));
        return (NOTAG(value));
    } else {
        DEBUG(("mismatch 0x%lx\n", TAG(value)));
    }
    }
    /* NOTREACHED */
}

/*
 * - okayvalue - check that a value can be stored
 */
static int          /* predicate */
okayvalue(value)
    of_t            value;
{
    if (HASTAG(value))
    return (0);
#ifdef OVERFLOW
    if (value == LONG_MAX)  /* BIAS() and UNBIAS() will overflow */
    return (0);
#endif
    return (1);
}

/*
 * - set - store a value into a location previously found by search
 */
static int          /* 0 success, -1 failure */
set(sp, value)
    register struct searcher *sp;
    of_t            value;
{
    register of_t   place = sp->place;
    register of_t   v = value;

    if (sp->aborted)
    return (-1);

    if (CANTAG(v) && !conf.olddbz) {
    v |= sp->tag | taghere;
    if (v != UNBIAS(VACANT))/* BIAS(v) won't look VACANT */
#ifdef OVERFLOW
        if (v != LONG_MAX)  /* and it won't overflow */
#endif
        value = v;
    }
    DEBUG(("tagged value is 0x%lx\n", value));
    value = BIAS(value);
    value = MAPOUT(value);

    /* If we have the index file in memory, use it */
    if (corepag != NULL && place < conf.tsize) {
    corepag[place] = value;
    DEBUG(("set: incore\n"));
#ifdef MMAP
    return (0);
#else
    if (!writethrough)
        return (0);
#endif
    }
    /* seek to spot */
    pagpos = -1;        /* invalidate position memory */
    if (fseek(pagf, (of_t) (place * SOF), SEEK_SET) != 0) {
    DEBUG(("set: seek failed\n"));
    sp->aborted = 1;
    return (-1);
    }
    /* write in data */
    if (fwrite((POINTER) & value, SOF, 1, pagf) != 1) {
    DEBUG(("set: write failed\n"));
    sp->aborted = 1;
    return (-1);
    }
    /* fflush improves robustness, and buffer re-use is rare anyway */
    if (fflush(pagf) == EOF) {
    DEBUG(("set: fflush failed\n"));
    sp->aborted = 1;
    return (-1);
    }
    DEBUG(("set: succeeded\n"));
    return (0);
}

/*
 * - mybytemap - determine this machine's byte map
 * 
 * A byte map is an array of ints, sizeof(of_t) of them.  The 0th int is the
 * byte number of the high-order byte in my of_t, and so forth.
 */
static void
mybytemap(map)
    int             map[];  /* -> int[SOF] */
{
    union {
    of_t            o;
    char            c[SOF];
    }               u;
    register int   *mp = &map[SOF];
    register int    ntodo;
    register int    i;

    u.o = 1;
    for (ntodo = (int)SOF; ntodo > 0; ntodo--) {
    for (i = 0; i < SOF; i++)
        /* SUPPRESS 112 *//* Retrieving char where long is stored */
        if (u.c[i] != 0)
        break;
    if (i == SOF) {
        /* trouble -- set it to *something* consistent */
        DEBUG(("mybytemap: nonexistent byte %d!!!\n", ntodo));
        for (i = 0; i < SOF; i++)
        map[i] = i;
        return;
    }
    DEBUG(("mybytemap: byte %d\n", i));
    *--mp = i;
    /* SUPPRESS 112 *//* Retrieving char where long is stored */
    while (u.c[i] != 0)
        u.o <<= 1;
    }
}

/*
 * - bytemap - transform an of_t from byte ordering map1 to map2
 */
static          of_t        /* transformed result */
bytemap(ino, map1, map2)
    of_t            ino;
    int            *map1;
    int            *map2;
{
    union oc {
    of_t            o;
    char            c[SOF];
    };
    union oc        in;
    union oc        out;
    register int    i;

    in.o = ino;
    for (i = 0; i < SOF; i++)
    out.c[map2[i]] = in.c[map1[i]];
    return (out.o);
}

/*
 * This is a simplified version of the pathalias hashing function. Thanks to
 * Steve Belovin and Peter Honeyman
 * 
 * hash a string into a long int.  31 bit crc (from andrew appel). the crc table
 * is computed at run time by crcinit() -- we could precompute, but it takes
 * 1 clock tick on a 750.
 * 
 * This fast table calculation works only if POLY is a prime polynomial in the
 * field of integers modulo 2.  Since the coefficients of a 32-bit polynomial
 * won't fit in a 32-bit word, the high-order bit is implicit.  IT MUST ALSO
 * BE THE CASE that the coefficients of orders 31 down to 25 are zero.
 * Happily, we have candidates, from E. J.  Watson, "Primitive Polynomials
 * (Mod 2)", Math. Comp. 16 (1962): x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
 * x^31 + x^3 + x^0
 * 
 * We reverse the bits to get: 111101010000000000000000000000001 but drop the
 * last 1 f   5   0   0   0   0   0   0 010010000000000000000000000000001
 * ditto, for 31-bit crc 4   8   0   0   0   0   0   0
 */

#define POLY 0x48000000L    /* 31-bit polynomial (avoids sign problems) */

static long     CrcTable[128];

/*
 * - crcinit - initialize tables for hash function
 */
static void
crcinit()
{
    register int    i, j;
    register long   sum;

    for (i = 0; i < 128; ++i) {
    sum = 0L;
    for (j = 7 - 1; j >= 0; --j)
        if (i & (1 << j))
        sum ^= POLY >> j;
    CrcTable[i] = sum;
    }
    DEBUG(("crcinit: done\n"));
}

/*
 * - hash - Honeyman's nice hashing function
 */
static long
hash(name, size)
    register char  *name;
    register int    size;
{
    register long   sum = 0L;

    while (size--) {
    sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
    }
    DEBUG(("hash: returns (%ld)\n", sum));
    return (sum);
}

/*
 * case-mapping stuff
 * 
 * Borrowed from C News, by permission of the authors.  Somewhat modified.
 * 
 * We exploit the fact that we are dealing only with headers here, and headers
 * are limited to the ASCII characters by RFC822.  It is barely possible that
 * we might be dealing with a translation into another character set, but in
 * particular it's very unlikely for a header character to be outside
 * -128..255.
 * 
 * Life would be a whole lot simpler if tolower() could safely and portably be
 * applied to any char.
 */

#define OFFSET  128     /* avoid trouble with negative chars */

/* must call casencmp before invoking TOLOW... */
#define TOLOW(c)    (cmap[(c)+OFFSET])

/* ...but the use of it in CISTREQN is safe without the preliminary call (!) */
/* CISTREQN is an optimised case-insensitive strncmp(a,b,n)==0; n > 0 */
#define CISTREQN(a, b, n) \
    (TOLOW((a)[0]) == TOLOW((b)[0]) && casencmp(a, b, n) == 0)

#define MAPSIZE (256+OFFSET)
static char     cmap[MAPSIZE];  /* relies on init to '\0' */
static int      mprimed = 0;    /* has cmap been set up? */

/*
 * - mapprime - set up case-mapping stuff
 */
static void
mapprime()
{
    register char  *lp;
    register char  *up;
    register int    c;
    register int    i;
    static char     lower[] = "abcdefghijklmnopqrstuvwxyz";
    static char     upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (lp = lower, up = upper; *lp != '\0'; lp++, up++) {
    c = *lp;
    cmap[c + OFFSET] = c;
    cmap[*up + OFFSET] = c;
    }
    for (i = 0; i < MAPSIZE; i++)
    if (cmap[i] == '\0')
        cmap[i] = (char)(i - OFFSET);
    mprimed = 1;
}

/*
 * - casencmp - case-independent strncmp
 */
static int          /* < == > 0 */
casencmp(s1, s2, len)
    char           *s1;
    char           *s2;
    int             len;
{
    register char  *p1;
    register char  *p2;
    register int    n;

    if (!mprimed)
    mapprime();

    p1 = s1;
    p2 = s2;
    n = len;
    while (--n >= 0 && *p1 != '\0' && TOLOW(*p1) == TOLOW(*p2)) {
    p1++;
    p2++;
    }
    if (n < 0)
    return (0);

    /*
     * The following case analysis is necessary so that characters which look
     * negative collate low against normal characters but high against the
     * end-of-string NUL.
     */
    if (*p1 == '\0' && *p2 == '\0')
    return (0);
    else if (*p1 == '\0')
    return (-1);
    else if (*p2 == '\0')
    return (1);
    else
    return (TOLOW(*p1) - TOLOW(*p2));
}

/*
 * - mapcase - do case-mapped copy
 */
static char    *        /* returns src or dst */
mapcase(dst, src, siz)
    char           *dst;    /* destination, used only if mapping needed */
    char           *src;    /* source; src == dst is legal */
    size_t          siz;
{
    register char  *s;
    register char  *d;
    register char  *c;      /* case break */
    register char  *e;      /* end of source */


    c = cipoint(src, siz);
    if (c == NULL)
    return (src);

    if (!mprimed)
    mapprime();
    s = src;
    e = s + siz;
    d = dst;

    while (s < c)
    *d++ = *s++;
    while (s < e)
    *d++ = TOLOW(*s++);

    return (dst);
}

/*
 * - cipoint - where in this message-ID does it become case-insensitive?
 * 
 * The RFC822 code is not quite complete.  Absolute, total, full RFC822
 * compliance requires a horrible parsing job, because of the arcane quoting
 * conventions -- abc"def"ghi is not equivalent to abc"DEF"ghi, for example.
 * There are three or four things that might occur in the domain part of a
 * message-id that are case-sensitive.  They don't seem to ever occur in real
 * news, thank Cthulhu.  (What?  You were expecting a merciful and forgiving
 * deity to be invoked in connection with RFC822? Forget it; none of them
 * would come near it.)
 */
static char    *        /* pointer into s, or NULL for "nowhere" */
cipoint(s, siz)
    char           *s;
    size_t          siz;
{
    register char  *p;
    static char     post[] = "postmaster";
    static int      plen = sizeof(post) - 1;

    switch (conf.casemap) {
    case '0':           /* unmapped, sensible */
    return (NULL);
    case 'C':           /* C News, RFC 822 conformant (approx.) */
    p = memchr((POINTER) s, '@', siz);
    if (p == NULL)      /* no local/domain split */
        return (NULL);  /* assume all local */
    if (p - (s + 1) == plen && CISTREQN(s + 1, post, plen)) {
        /* crazy -- "postmaster" is case-insensitive */
        return (s);
    }
    return (p);
    case '=':           /* 2.11, neither sensible nor conformant */
    return (s);     /* all case-insensitive */
    }

    DEBUG(("cipoint: unknown case mapping `%c'\n", conf.casemap));
    return (NULL);      /* just leave it alone */
}

/*
 * - dbzdebug - control dbz debugging at run time
 */
#ifdef DBZDEBUG
int             /* old value */
dbzdebug(value)
    int             value;
{
    register int    old = debug;

    debug = value;
    return (old);
}
#endif