Commit 356b2ed28c66d165c966cde3f17e0a202dac8234

Thomas de Grivel 2024-07-18T20:13:08

Merge remote-tracking branch 'origin/main'

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
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
diff --git a/.gitignore b/.gitignore
index c805925..a6f6403 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 ChangeLog
+*~
 *.pc
 *.la
 *.lo
@@ -9,6 +10,8 @@ ChangeLog
 *.trs
 *.gcda
 *.gcno
+*.sym
+*.map
 .dirstamp
 .deps/
 .libs/
@@ -19,6 +22,6 @@ autom4te.cache/
 build-aux/
 configure
 config.*
+format.ld
 libtool
-m4/
 stamp-h1
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ca88de2..72b7625 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,19 +1,54 @@
-image: debian:buster
-
-before_script:
-  - apt-get update -qq
-  - apt-get install -qq -y --no-install-recommends
-            git gcc make autoconf automake libtool libmd-dev gcovr
+.vpath-tests:
+  script:
+    - ./autogen
+    - mkdir -p build-tree
+    - cd build-tree
+    - ../configure
+    - make check
 
-unit-tests:
-  stage: test
+.unit-tests:
   script:
     - ./autogen && ./configure
     - make check
 
-coverage:
-  stage: test
+.unit-tests-asan:
+  script:
+    - ./autogen && ./configure --enable-sanitize
+    - make check
+
+.coverage:
   script:
     - ./autogen && ./configure --disable-static
     - make check CFLAGS="--coverage -O0 -ggdb" LDFLAGS="--coverage -O0 -ggdb"
     - gcovr -s -e test/
+  coverage: /^TOTAL.*\s+(\d+\%)$/
+
+.debian:
+  image: debian:latest
+  stage: test
+  before_script:
+    - apt-get update -qq
+    - apt-get install -qq -y --no-install-recommends
+              git gcc make autoconf automake libtool libmd-dev gcovr
+
+.alpine:
+  image: alpine:latest
+  stage: test
+  before_script:
+    - apk add --no-cache git gcc make autoconf automake libtool
+              bsd-compat-headers linux-headers libmd-dev musl-dev gcovr
+
+vpath-tests:debian:
+  extends: [.debian, .vpath-tests]
+
+unit-tests:debian:
+  extends: [.debian, .unit-tests-asan]
+
+coverage:debian:
+  extends: [.debian, .coverage]
+
+unit-tests:alpine:
+  extends: [.alpine, .unit-tests]
+
+coverage:alpine:
+  extends: [.alpine, .coverage]
diff --git a/COPYING b/COPYING
index 8a99303..7814d8f 100644
--- a/COPYING
+++ b/COPYING
@@ -3,75 +3,10 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
 Files:
  *
 Copyright:
- Copyright © 2004-2006, 2008-2021 Guillem Jover <guillem@hadrons.org>
+ Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
 License: BSD-3-clause
 
 Files:
- man/arc4random.3bsd
- man/tree.3bsd
-Copyright:
- Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
- All rights reserved.
-License: BSD-4-clause-Niels-Provos
- 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. All advertising materials mentioning features or use of this software
-    must display the following acknowledgement:
-      This product includes software developed by Niels Provos.
- 4. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
- .
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-
-Files:
- man/getprogname.3bsd
-Copyright:
- Copyright © 2001 Christopher G. Demetriou
- All rights reserved.
-License: BSD-4-clause-Christopher-G-Demetriou
- 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. All advertising materials mentioning features or use of this software
-    must display the following acknowledgement:
-          This product includes software developed for the
-          NetBSD Project.  See http://www.netbsd.org/ for
-          information about NetBSD.
- 4. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
- .
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-
-Files:
  include/bsd/err.h
  include/bsd/stdlib.h
  include/bsd/sys/param.h
@@ -236,6 +171,7 @@ License: BSD-5-clause-Peter-Wemm
 
 Files:
  include/bsd/stringlist.h
+ man/arc4random.3bsd
  man/fmtcheck.3bsd
  man/humanize_number.3bsd
  man/stringlist.3bsd
@@ -250,6 +186,12 @@ Copyright:
  Copyright © 2013 John-Mark Gurney <jmg@FreeBSD.org>
  All rights reserved.
  .
+ Copyright © 2014 The NetBSD Foundation, Inc.
+ All rights reserved.
+ .
+ Some code was derived from software contributed to The NetBSD Foundation
+ by Taylor R. Campbell.
+ .
  Some code was contributed to The NetBSD Foundation by Allen Briggs.
  .
  Some code was contributed to The NetBSD Foundation by Luke Mewburn.
@@ -311,7 +253,6 @@ Copyright:
  All rights reserved.
  .
  Copyright © 2009 Advanced Computing Technologies LLC
- Written by: John H. Baldwin <jhb@FreeBSD.org>
  All rights reserved.
  .
  Copyright © 2011 Guillem Jover <guillem@hadrons.org>
@@ -348,6 +289,7 @@ License: BSD-2-clause-verbatim
 Files:
  include/bsd/sys/tree.h
  man/fparseln.3bsd
+ man/tree.3bsd
  src/fparseln.c
 Copyright:
  Copyright © 1997 Christos Zoulas.
@@ -364,7 +306,6 @@ Files:
  man/strtonum.3bsd
  src/arc4random.c
  src/arc4random_linux.h
- src/arc4random_openbsd.h
  src/arc4random_uniform.c
  src/arc4random_unix.h
  src/arc4random_win.h
@@ -383,6 +324,8 @@ Files:
  src/recallocarray.c
  src/strlcat.c
  src/strlcpy.c
+ test/explicit_bzero.c
+ test/strtonum.c
 Copyright:
  Copyright © 2004 Ted Unangst and Todd Miller
  All rights reserved.
@@ -391,6 +334,7 @@ Copyright:
  Copyright © 1998, 2000-2002, 2004-2005, 2007, 2010, 2012-2015
      Todd C. Miller <Todd.Miller@courtesan.com>
  Copyright © 2004 Ted Unangst
+ Copyright © 2004 Otto Moerbeek <otto@drijf.net>
  Copyright © 2008 Damien Miller <djm@openbsd.org>
  Copyright © 2008, 2010-2011, 2016-2017 Otto Moerbeek <otto@drijf.net>
  Copyright © 2013 Markus Friedl <markus@openbsd.org>
@@ -398,8 +342,9 @@ Copyright:
  Copyright © 2014 Brent Cook <bcook@openbsd.org>
  Copyright © 2014 Pawel Jakub Dawidek <pjd@FreeBSD.org>
  Copyright © 2014 Theo de Raadt <deraadt@openbsd.org>
+ Copyright © 2014 Google Inc.
  Copyright © 2015 Michael Felt <aixtools@gmail.com>
- Copyright © 2015 Guillem Jover <guillem@hadrons.org>
+ Copyright © 2015, 2022 Guillem Jover <guillem@hadrons.org>
 License: ISC
  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
diff --git a/Makefile.am b/Makefile.am
index b839ae1..bdb8376 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,13 +1,18 @@
 ## Process this file with automake to produce Makefile.in
 
-SUBDIRS = include man src test
+SUBDIRS = \
+	include \
+	man \
+	src \
+	test \
+	# EOL
 
 ACLOCAL_AMFLAGS = -I m4
 
 EXTRA_DIST = \
 	autogen \
 	get-version \
-	$(nil)
+	# EOL
 
 dist-hook:
 	echo $(VERSION) >$(distdir)/.dist-version
diff --git a/README b/README
index 9b2bbdb..c545bdc 100644
--- a/README
+++ b/README
@@ -9,6 +9,7 @@ A BSD compatible message-digest library is required, on systems where
 this is not provided by its libc or libmd libraries, the canonical
 implementation to use is <https://www.hadrons.org/software/libmd/>.
 
+
 Releases
 --------
 
@@ -30,10 +31,44 @@ The mail address is:
 Source Repository
 -----------------
 
-The master repository can be browsed at:
+The primary repository can be browsed at:
 
   <https://cgit.freedesktop.org/libbsd>
 
 and cloned from:
 
   <https://anongit.freedesktop.org/git/libbsd>
+
+
+Building from git source
+------------------------
+
+To prepare the libbsd source tree from git before starting the build process
+some required software needs to be installed:
+
+  GNU autoconf >= 2.67
+  GNU automake >= 1.9
+  GNU libtool >= 2.0
+
+After installing the needed software, and running the following command on
+the git tree:
+
+  $ ./autogen
+
+the source should be roughly equivalent to the distributed tar source.
+
+
+Building from tar source
+------------------------
+
+The minimum software required to configure and build dpkg from a tarball is:
+
+  C89 compiler
+  make
+
+The following software might be required depending on the system:
+
+  libmd (whenever the libc does not provide the needed digest functions)
+
+The build process is done by running the usual «./configure; make». To
+see all available configuration options please run «./configure --help».
diff --git a/configure.ac b/configure.ac
index 09cb310..2a15d72 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,17 +6,22 @@ AC_CONFIG_SRCDIR([src/strlcpy.c])
 AC_CONFIG_AUX_DIR([build-aux])
 AC_CONFIG_MACRO_DIR([m4])
 
-AM_INIT_AUTOMAKE([1.9 foreign nostdinc subdir-objects no-dist-gzip dist-xz])
-
-m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])],
-                            [AC_SUBST([AM_DEFAULT_VERBOSITY], [1])])
+AM_INIT_AUTOMAKE(
+  [1.11]
+  [-Wall]
+  [foreign]
+  [nostdinc]
+  [subdir-objects]
+  [no-dist-gzip dist-xz]
+)
+AM_SILENT_RULES([yes])
 
-LIBBSD_ABI_MAJOR=0
-LIBBSD_ABI_MINOR=11
-LIBBSD_ABI_PATCH=3
+SOVERSION_MAJOR=0
+SOVERSION_MINOR=12
+SOVERSION_PATCH=2
 
-LIBBSD_ABI="$LIBBSD_ABI_MAJOR:$LIBBSD_ABI_MINOR:$LIBBSD_ABI_PATCH"
-AC_SUBST([LIBBSD_ABI])
+SOVERSION="$SOVERSION_MAJOR:$SOVERSION_MINOR:$SOVERSION_PATCH"
+AC_SUBST([SOVERSION])
 
 # Check and store if we got user supplied variables
 user_CFLAGS=${CFLAGS-unset}
@@ -24,70 +29,447 @@ user_CFLAGS=${CFLAGS-unset}
 # Checks for operating system services and capabilities.
 AC_USE_SYSTEM_EXTENSIONS
 AC_SYS_LARGEFILE
+LIBBSD_SYS_TIME64
 
+AM_PROG_AR
 LT_INIT
+LIBBSD_LINKER_VERSION_SCRIPT
 
-AC_CACHE_CHECK([if ld supports --version-script flag],
-  [libbsd_cv_version_script], [
-    echo "{ global: symbol; local: *; };" >conftest.map
-    save_LDFLAGS=$LDFLAGS
-    LDFLAGS="$LDFLAGS -Wl,--version-script=conftest.map"
-    AC_LINK_IFELSE([
-      AC_LANG_PROGRAM([], [])
-    ], [
-      libbsd_cv_version_script=yes
-    ], [
-      libbsd_cv_version_script=no
-    ])
-    LDFLAGS="$save_LDFLAGS"
-    rm -f conftest.map
-  ]
+## Select library ABI to expose.
+#
+# The systems have DROP comments for interfaces that could be dropped on
+# the next SOVERSION bump.
+
+is_windows=no
+
+api_time_macros=unknown
+
+abi_accmode=unknown
+abi_arc4random=unknown
+abi_arc4random_stir=unknown
+abi_asprintf=unknown
+abi_bsd_getopt=unknown
+abi_closefrom=unknown
+abi_err=unknown
+abi_errc=unknown
+abi_expand_number=unknown
+abi_explicit_bzero=unknown
+abi_fgetln=unknown
+abi_flopen=unknown
+abi_fmtcheck=unknown
+abi_fpurge=unknown
+abi_freezero=unknown
+abi_funopen=unknown
+abi_getbsize=unknown
+abi_getpeereid=unknown
+abi_humanize_number=unknown
+abi_id_from_name=unknown
+abi_inet_net_pton=unknown
+abi_md5=unknown
+abi_name_from_id=unknown
+abi_nlist=unknown
+abi_pidfile=unknown
+abi_proctitle=unknown
+abi_progname=unknown
+abi_readpassphrase=unknown
+abi_reallocarray=unknown
+abi_reallocf=unknown
+abi_recallocarray=unknown
+abi_stringlist=unknown
+abi_sort=unknown
+abi_strl=unknown
+abi_strmode=unknown
+abi_strnstr=unknown
+abi_strtonum=unknown
+abi_strtox=unknown
+abi_timeconv=unknown
+abi_vis=unknown
+abi_wcsl=unknown
+
+AS_CASE([$host_os],
+  [*-gnu* | gnu*], [
+    api_time_macros=no
+
+    abi_accmode=yes
+    # DROP: On glibc >= 2.36.
+    #abi_arc4random=no
+    abi_arc4random=yes
+    abi_arc4random_stir=yes
+    abi_asprintf=no
+    abi_bsd_getopt=yes
+    # DROP: On glibc >= 2.34.
+    #abi_closefrom=no
+    abi_closefrom=yes
+    abi_err=no
+    abi_errc=yes
+    abi_expand_number=yes
+    # DROP: On glibc >= 2.25.
+    #abi_explicit_bzero=no
+    abi_explicit_bzero=yes
+    abi_fgetln=yes
+    abi_flopen=yes
+    abi_fmtcheck=yes
+    abi_fpurge=yes
+    abi_freezero=yes
+    abi_funopen=yes
+    abi_getbsize=yes
+    abi_getpeereid=yes
+    abi_humanize_number=yes
+    abi_id_from_name=yes
+    # DROP: On glibc.
+    #abi_inet_net_pton=no
+    abi_inet_net_pton=yes
+    # DROP: On libmd.
+    #abi_md5=no
+    abi_md5=yes
+    abi_name_from_id=yes
+    abi_nlist=yes
+    abi_pidfile=yes
+    abi_proctitle=yes
+    abi_progname=yes
+    abi_readpassphrase=yes
+    # DROP: On glibc >= 2.26.
+    #abi_reallocarray=no
+    abi_reallocarray=yes
+    abi_reallocf=yes
+    abi_recallocarray=yes
+    abi_stringlist=yes
+    abi_sort=yes
+    # DROP: On glibc >= 2.38.
+    #abi_strl=no
+    abi_strl=yes
+    abi_strmode=yes
+    abi_strnstr=yes
+    abi_strtonum=yes
+    abi_strtox=yes
+    abi_timeconv=yes
+    # DROP: On libmd.
+    abi_transparent_libmd=yes
+    abi_vis=yes
+    abi_wcsl=yes
+  ],
+  [*-musl*], [
+    api_time_macros=no
+
+    abi_accmode=yes
+    abi_arc4random=yes
+    abi_arc4random_stir=yes
+    abi_asprintf=no
+    # DROP: On musl >= 0.9.7, optreset got implemented.
+    #abi_bsd_getopt=no
+    abi_bsd_getopt=yes
+    abi_closefrom=yes
+    abi_err=no
+    abi_errc=yes
+    abi_expand_number=yes
+    # DROP: On musl >= 1.1.20.
+    #abi_explicit_bzero=no
+    abi_explicit_bzero=yes
+    # DROP: On musl >= 0.9.4.
+    #abi_fgetln=no
+    abi_fgetln=yes
+    abi_flopen=yes
+    abi_fmtcheck=yes
+    abi_fpurge=yes
+    abi_freezero=yes
+    # On musl >= 1.1.19, fopencookie() got implemented, and because we were
+    # checking for its presence to decide whether to build funopen(), it got
+    # included in builds even when previously it had not been included, which
+    # is partially an ABI issue, but given that disabling it now would be
+    # worse, we'll ignore this as this is only a problem with downgrades. And
+    # enable it explicitly
+    #abi_funopen=no
+    abi_funopen=yes
+    abi_getbsize=yes
+    abi_getpeereid=yes
+    abi_humanize_number=yes
+    abi_id_from_name=yes
+    abi_inet_net_pton=yes
+    # DROP: On libmd.
+    #abi_md5=no
+    abi_md5=yes
+    abi_name_from_id=yes
+    abi_nlist=yes
+    abi_pidfile=yes
+    abi_proctitle=yes
+    abi_progname=yes
+    abi_readpassphrase=yes
+    # DROP: On musl >= 1.2.2.
+    #abi_reallocarray=no
+    abi_reallocarray=yes
+    abi_reallocf=ues
+    abi_recallocarray=yes
+    abi_stringlist=yes
+    abi_sort=yes
+    # DROP: On musl >= 0.5.0, strlcpy() and strlcat() were already implemented.
+    #abi_strl=no
+    abi_strl=yes
+    abi_strmode=yes
+    abi_strnstr=yes
+    abi_strtonum=yes
+    abi_strtox=yes
+    abi_timeconv=yes
+    # DROP: On libmd.
+    abi_transparent_libmd=yes
+    abi_vis=yes
+    abi_wcsl=yes
+  ],
+  [darwin*], [
+    api_time_macros=no
+
+    abi_accmode=no
+    abi_arc4random=no
+    abi_arc4random_stir=no
+    abi_asprintf=no
+    abi_bsd_getopt=no
+    abi_closefrom=yes
+    abi_err=no
+    abi_errc=no
+    abi_expand_number=yes
+    abi_explicit_bzero=yes
+    abi_fgetln=no
+    abi_flopen=yes
+    abi_fmtcheck=no
+    abi_fpurge=no
+    abi_freezero=yes
+    abi_funopen=no
+    abi_getbsize=yes
+    abi_getpeereid=no
+    abi_humanize_number=yes
+    abi_id_from_name=yes
+    abi_inet_net_pton=no
+    # On libmd.
+    abi_md5=no
+    abi_name_from_id=no
+    abi_nlist=no
+    abi_pidfile=yes
+    abi_proctitle=yes
+    abi_progname=no
+    abi_readpassphrase=no
+    abi_reallocarray=yes
+    abi_reallocf=no
+    abi_recallocarray=yes
+    abi_stringlist=no
+    abi_sort=no
+    abi_strl=no
+    abi_strmode=no
+    abi_strnstr=no
+    abi_strtonum=no
+    abi_strtox=yes
+    abi_timeconv=no
+    # On libmd.
+    abi_transparent_libmd=no
+    abi_vis=no
+    abi_wcsl=no
+  ],
+  [solaris2*], [
+    api_time_macros=yes
+
+    abi_accmode=yes
+    abi_arc4random=no
+    abi_arc4random_stir=no
+    abi_asprintf=no
+    abi_bsd_getopt=yes
+    abi_closefrom=no
+    abi_err=no
+    abi_errc=yes
+    abi_expand_number=yes
+    abi_explicit_bzero=yes
+    abi_fgetln=yes
+    abi_flopen=yes
+    abi_fmtcheck=no
+    abi_fpurge=yes
+    abi_freezero=yes
+    abi_funopen=no
+    abi_getbsize=yes
+    abi_getpeereid=yes
+    abi_humanize_number=yes
+    abi_id_from_name=yes
+    abi_inet_net_pton=yes
+    # On libmd.
+    abi_md5=no
+    abi_name_from_id=yes
+    abi_nlist=no
+    abi_pidfile=yes
+    abi_proctitle=yes
+    abi_progname=no
+    abi_readpassphrase=yes
+    abi_reallocarray=yes
+    abi_reallocf=no
+    abi_recallocarray=yes
+    abi_stringlist=yes
+    abi_sort=yes
+    abi_strl=no
+    abi_strmode=yes
+    abi_strnstr=no
+    abi_strtonum=no
+    abi_strtox=yes
+    abi_timeconv=yes
+    # On libmd.
+    abi_transparent_libmd=no
+    abi_vis=yes
+    abi_wcsl=yes
+  ],
+  [aix*], [
+    api_time_macros=yes
+
+    abi_accmode=yes
+    abi_arc4random=yes
+    abi_arc4random_stir=yes
+    abi_asprintf=yes
+    abi_bsd_getopt=yes
+    abi_closefrom=yes
+    abi_err=yes
+    abi_errc=yes
+    abi_expand_number=yes
+    abi_explicit_bzero=yes
+    abi_fgetln=yes
+    abi_flopen=yes
+    abi_fmtcheck=yes
+    abi_fpurge=no
+    abi_freezero=yes
+    abi_funopen=no
+    abi_getbsize=yes
+    abi_getpeereid=yes
+    abi_humanize_number=yes
+    abi_id_from_name=yes
+    abi_inet_net_pton=no
+    # On libmd.
+    abi_md5=no
+    abi_name_from_id=yes
+    abi_nlist=no
+    abi_pidfile=yes
+    abi_proctitle=yes
+    abi_progname=yes
+    abi_readpassphrase=yes
+    abi_reallocarray=yes
+    abi_reallocf=yes
+    abi_recallocarray=yes
+    abi_stringlist=yes
+    abi_sort=yes
+    abi_strl=yes
+    abi_strmode=yes
+    abi_strnstr=yes
+    abi_strtonum=yes
+    abi_strtox=yes
+    abi_timeconv=yes
+    # On libmd.
+    abi_transparent_libmd=no
+    abi_vis=yes
+    abi_wcsl=yes
+  ],
+  [mingw*], [
+    is_windows=yes
+  ],
 )
-AM_CONDITIONAL([HAVE_LINKER_VERSION_SCRIPT],
-  [test "x$libbsd_cv_version_script" = "xyes"])
+
+# Handle artificial ABI selections that are combinations of others.
+AS_IF([test "$abi_name_from_id" = "yes" || test "$abi_id_from_name" = "yes"], [
+  abi_pwcache=yes
+], [
+  abi_pwcache=no
+])
+AS_IF([test "$abi_err" = "yes" || test "$abi_errc" = "yes"], [
+  abi_err_h=yes
+], [
+  abi_err_h=no
+])
+
+AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
 
 # Checks for programs.
+AC_CHECK_TOOL([OBJDUMP], [objdump])
 AC_PROG_CC
+AC_PROG_SED
 AC_PROG_INSTALL
 AC_PROG_LN_S
 
 # Set default compiler variables
-if test "$user_CFLAGS" = unset && test "$GCC" = yes; then
-  CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter"
-fi
+AS_IF([test "$user_CFLAGS" = unset], [
+  LIBBSD_CHECK_COMPILER_FLAG([-Wall])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wextra])
+
+  LIBBSD_CHECK_COMPILER_FLAG([-Wbad-function-cast])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wcast-align])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wdocumentation])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-branches])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wduplicated-cond])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wformat=2])
+  LIBBSD_CHECK_COMPILER_FLAG([-Winit-self])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wlogical-op])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-declarations])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wnested-externs])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-missing-field-initializers])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-nonnull-compare])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wnull-dereference])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wold-style-definition])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wpointer-arith])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wregister])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wrestrict])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wshadow])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wshift-negative-value])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wswitch-bool])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wvla])
+  LIBBSD_CHECK_COMPILER_FLAG([-Wwrite-strings])
+
+  CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
+
+  AC_ARG_ENABLE([sanitize],
+    [AS_HELP_STRING([--enable-sanitize], [enable compiler sanitizer support])],
+  [
+    LIBBSD_COMPILER_FLAGS=''
+    LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=address])
+    LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=leak])
+    LIBBSD_CHECK_COMPILER_FLAG([-fsanitize=undefined])
+
+    CFLAGS="$CFLAGS $LIBBSD_COMPILER_FLAGS"
+    LDFLAGS="$LDFLAGS $LIBBSD_COMPILER_FLAGS"
+  ])
+])
 
 # Checks for libraries.
 AC_CHECK_LIB([testu01], [unif01_CreateExternGenBits],
-             [TESTU01_LIBS="-ltestu01"])
+  [TESTU01_LIBS="-ltestu01"])
 AC_SUBST([TESTU01_LIBS])
 AM_CONDITIONAL([HAVE_LIBTESTU01],
-               [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
+  [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
 
 saved_LIBS="$LIBS"
-AC_SEARCH_LIBS([dlsym], [dl], [
-  AS_IF([test "x$ac_cv_search_dlsym" != "xnone required"], [
-    LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_dlsym"
-  ])
-], [
-  AC_MSG_ERROR([cannot find required dlsym function])
-])
-AC_SEARCH_LIBS([MD5Update], [md], [
-  AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
-    LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_MD5Update"
+AS_IF([test "$abi_md5" = "yes"], [
+  AC_SEARCH_LIBS([MD5Update], [md], [
+    AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
+      MD5_LIBS="$MD5_LIBS $ac_cv_search_MD5Update"
+      abi_transparent_libmd=yes
+    ])
+  ], [
+    AC_MSG_ERROR([cannot find required MD5 functions in libc or libmd])
   ])
-], [
-  AC_MSG_ERROR([cannot find required MD5 functions in libc or libmd])
 ])
-AC_SEARCH_LIBS([SHA512Update], [md], [
-  AS_IF([test "x$ac_cv_search_SHA512Update" != "xnone required"], [
-    LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_SHA512Update"
+AS_IF([test "$abi_arc4random" = "yes"], [
+  AC_CHECK_FUNCS([getentropy])
+
+  AS_IF([test "$ac_cv_func_getentropy" != "yes"], [
+    AC_SEARCH_LIBS([SHA512Update], [md], [
+      AS_IF([test "x$ac_cv_search_SHA512Update" != "xnone required"], [
+        LIBBSD_LIBS="$SHA512_LIBS $ac_cv_search_SHA512Update"
+      ])
+    ], [
+      AC_MSG_ERROR([cannot find required SHA-2 functions in libc or libmd])
+    ])
   ])
-], [
-  AC_MSG_ERROR([cannot find required SHA-2 functions in libc or libmd])
 ])
 LIBS="$saved_LIBS"
 
-is_windows=no
 AS_CASE([$host_os],
   [*-gnu*], [
     # In old glibc versions (< 2.17) clock_gettime() is in librt.
@@ -99,18 +481,28 @@ AS_CASE([$host_os],
     ])
     LIBS="$saved_LIBS"
   ],
-  [*-musl*], [
-    # Upstream refuses to define this, we will do it ourselves then.
-    AC_DEFINE([__MUSL__], [1], [Define to 1 if we are building for musl])
-  ],
-  [mingw*], [
-    is_windows=yes
+  [aix*], [
+    saved_LIBS="$LIBS"
+    AC_SEARCH_LIBS([perfstat_cpu_total], [perfstat], [
+      AS_IF([test "x$ac_cv_search_perfstat_cpu_total" != "xnone required"], [
+        LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_perfstat_cpu_total"
+      ])
+    ])
+    LIBS="$saved_LIBS"
   ],
 )
-AM_CONDITIONAL([OS_WINDOWS], [test "x$is_windows" = "xyes"])
 
 # Checks for header files.
-AC_CHECK_HEADERS([sys/ndir.h sys/dir.h ndir.h dirent.h pwd.h grp.h])
+AC_CHECK_HEADERS([\
+  sys/ndir.h \
+  sys/dir.h \
+  ndir.h \
+  dirent.h \
+  pwd.h \
+  grp.h \
+  stdio_ext.h \
+  procinfo.h \
+])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_INLINE
@@ -123,107 +515,115 @@ AC_TYPE_UID_T
 AC_TYPE_SIZE_T
 AC_TYPE_SSIZE_T
 
-AC_CHECK_DECL([F_CLOSEM],
-              [AC_DEFINE([HAVE_FCNTL_CLOSEM], [1],
-                         [Define to 1 if you have fcntl(F_CLOSEM)])],
-              [],
-              [#include <limits.h>
-               #include <fcntl.h>])
-
-AC_CACHE_CHECK(
-	[for GNU .init_array section support],
-	[libbsd_cv_gnu_init_array_support],
-	[AC_RUN_IFELSE(
-		[AC_LANG_SOURCE(
-[[
-static int rc = 1;
-static void init(int argc) { if (argc == 1) rc = 0; }
-void (*init_func)(int argc) __attribute__((__section__(".init_array"))) = init;
-int main() { return rc; }
-]]
-		)],
-		[libbsd_cv_gnu_init_array_support=yes],
-		[libbsd_cv_gnu_init_array_support=no],
-		[AC_PREPROC_IFELSE(
-			[AC_LANG_SOURCE(
-[[
-/* Look for a known libc that supports .init_array with the GNU extension
- * to pass main() arguments to the init functions. */
-#include <stdlib.h>
-#if defined __GLIBC_PREREQ
-#  if __GLIBC_PREREQ(2, 4)
-/* glibc supports GNU .init_array since 2.4. */
-#  else
-#    error glibc does not support GNU .init_array
-#  endif
-#else
-/*
- * Basic SysV ABI .init_array support, init functions do not get arguments:
- * - Bionic since its inception.
- * - uClibc since 0.9.29.
- */
-#  error unknown whether libc supports GNU .init_array
-#endif
-]]
-			)],
-			[libbsd_cv_gnu_init_array_support=yes],
-			[libbsd_cv_gnu_init_array_support=no])
-		]
-	)]
-)
-AM_CONDITIONAL([BUILD_LIBBSD_CTOR],
-               [test "$libbsd_cv_gnu_init_array_support" = yes])
+AC_CHECK_DECL([F_CLOSEM], [
+  AC_DEFINE([HAVE_FCNTL_CLOSEM], [1],
+    [Define to 1 if you have fcntl(F_CLOSEM)])
+], [], [[
+#include <limits.h>
+#include <fcntl.h>
+]])
+
+AC_CHECK_DECLS([environ], [], [], [[
+#include <unistd.h>
+]])
+
+LIBBSD_HAS_GNU_INIT_ARRAY
 
 # Checks for library functions.
-AC_MSG_CHECKING([for program_invocation_short_name])
-AC_LINK_IFELSE(
-	[AC_LANG_PROGRAM([[#include <errno.h>]],
-	                 [[const char *p = program_invocation_short_name;]])],
-	[AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
-	           [Define to 1 if you have program_invocation_short_name])
-	 AC_MSG_RESULT([yes])],
-	[AC_MSG_RESULT([no])])
-
-AC_MSG_CHECKING([for __progname])
-AC_LINK_IFELSE(
-	[AC_LANG_PROGRAM([[extern char *__progname;]],
-	                 [[printf("%s", __progname);]])],
-	[AC_DEFINE([HAVE___PROGNAME], [1], [Define to 1 if you have __progname])
-	 AC_MSG_RESULT([yes])],
-	[AC_MSG_RESULT([no])])
-
-AC_MSG_CHECKING([for __register_atfork])
-AC_LINK_IFELSE(
-	[AC_LANG_PROGRAM([[
-		#include <stddef.h>
-		extern void *__dso_handle;
-		extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
-	]], [[
-		__register_atfork(NULL, NULL, NULL, __dso_handle);
-	]])],
-	[AC_DEFINE([HAVE___REGISTER_ATFORK], [1],
-	           [Define to 1 if you have __register_atfork])
-	 AC_MSG_RESULT([yes])],
-	[LIBBSD_LIBS="$LIBBSD_LIBS -pthread"
-	 AC_MSG_RESULT([no])
-	])
-
-AC_CHECK_FUNCS([clearenv dirfd fopencookie __fpurge \
-                getauxval getentropy getexecname getline \
-                pstat_getproc sysconf])
-AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xtrue"])
+LIBBSD_CHECK_PROGNAME
+LIBBSD_CHECK_REGISTER_ATFORK
+
+AC_CHECK_FUNCS([\
+  vasprintf \
+  asprintf \
+  clearenv \
+  dirfd \
+  flock \
+  fopencookie \
+  __fpurge \
+  funopen \
+  getauxval \
+  getentropy \
+  getexecname \
+  getline \
+  open_memstream \
+  pstat_getproc \
+  sysconf \
+  uid_from_user \
+  gid_from_group \
+  user_from_uid \
+  group_from_gid \
+])
+
+AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = "xyes"])
+
+# API selection
+LIBBSD_SELECT_API([time_macros], [time struct conversion macros])
+
+# ABI selection
+LIBBSD_SELECT_ABI([accmode], [setmode()/getmode()])
+LIBBSD_SELECT_ABI([arc4random], [arc4random() API])
+LIBBSD_SELECT_ABI([arc4random_stir], [arc4random_stir()/arc4random_addrandom()])
+LIBBSD_SELECT_ABI([asprintf], [vasprintf()/asprintf()])
+LIBBSD_SELECT_ABI([bsd_getopt], [BSD getopt()])
+LIBBSD_SELECT_ABI([closefrom], [closefrom()])
+LIBBSD_SELECT_ABI([err_h], [err.h header])
+LIBBSD_SELECT_ABI([err], [err API])
+LIBBSD_SELECT_ABI([errc], [errc API])
+LIBBSD_SELECT_ABI([expand_number], [expand_number()])
+LIBBSD_SELECT_ABI([explicit_bzero], [explicit_bzero()])
+LIBBSD_SELECT_ABI([fgetln], [fgetln/fgetwln()])
+LIBBSD_SELECT_ABI([flopen], [flopen()/flopenat()])
+LIBBSD_SELECT_ABI([fmtcheck], [fmtcheck()])
+LIBBSD_SELECT_ABI([fpurge], [fpurge()])
+LIBBSD_SELECT_ABI([freezero], [freezero()])
+LIBBSD_SELECT_ABI([funopen], [funopen()])
+LIBBSD_SELECT_ABI([getbsize], [getbsize()])
+LIBBSD_SELECT_ABI([getpeereid], [getpeereid()])
+LIBBSD_SELECT_ABI([humanize_number], [humanize_number()/dehumanize_number()])
+LIBBSD_SELECT_ABI([id_from_name], [uid_from_user()/gid_from_user()])
+LIBBSD_SELECT_ABI([inet_net_pton], [inet_net_pton()])
+LIBBSD_SELECT_ABI([md5], [MD5 digest functions])
+LIBBSD_SELECT_ABI([name_from_id], [user_from_uid()/group_from_gid()])
+LIBBSD_SELECT_ABI([nlist], [nlist()])
+LIBBSD_SELECT_ABI([pidfile], [pidfile API])
+LIBBSD_SELECT_ABI([proctitle], [setproctitle() API])
+LIBBSD_SELECT_ABI([progname], [setprogname()/getprogname()])
+LIBBSD_SELECT_ABI([pwcache], [password cache API])
+LIBBSD_SELECT_ABI([readpassphrase], [readpassphrase()])
+LIBBSD_SELECT_ABI([reallocarray], [reallocarray()])
+LIBBSD_SELECT_ABI([reallocf], [reallocf()])
+LIBBSD_SELECT_ABI([recallocarray], [recallocarray()])
+LIBBSD_SELECT_ABI([stringlist], [stringlist sl_*() API])
+LIBBSD_SELECT_ABI([sort], [BSD sort functions])
+LIBBSD_SELECT_ABI([strl], [strlcpy()/strlcat()])
+LIBBSD_SELECT_ABI([strmode], [strmode()])
+LIBBSD_SELECT_ABI([strnstr], [strnstr()])
+LIBBSD_SELECT_ABI([strtonum], [strtonum()])
+LIBBSD_SELECT_ABI([strtox], [strtoi()/strtou()])
+LIBBSD_SELECT_ABI([timeconv], [timeconv.h API])
+LIBBSD_SELECT_ABI([transparent_libmd], [transparent libmd support])
+LIBBSD_SELECT_ABI([vis], [vis API])
+LIBBSD_SELECT_ABI([wcsl], [wcslcpy()/wcslcat()])
+
+AS_IF([test "x$abi_funopen" = "xno" && \
+       test "x$ac_cv_func_funopen" != "xyes" && \
+       test "x$ac_cv_func_fopencookie" = "xyes"], [
+  AC_MSG_WARN([[can implement funopen() now based on newly added fopencooke(), report upstream]])
+])
 
+AC_SUBST([MD5_LIBS])
 AC_SUBST([LIBBSD_LIBS])
 
 AC_CONFIG_FILES([
-	Makefile
-	include/Makefile
-	man/Makefile
-	src/Makefile
-	src/libbsd.pc
-	src/libbsd-ctor.pc
-	src/libbsd-overlay.pc
-	test/Makefile
+  Makefile
+  include/Makefile
+  man/Makefile
+  src/Makefile
+  src/libbsd.pc
+  src/libbsd-ctor.pc
+  src/libbsd-overlay.pc
+  test/Makefile
 ])
 AC_CONFIG_HEADERS([config.h])
 AC_OUTPUT
diff --git a/include/Makefile.am b/include/Makefile.am
index e6f66bd..cf2713e 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -12,21 +12,77 @@ nobase_include_HEADERS = \
 	bsd/netinet/ip_icmp.h \
 	bsd/bitstring.h \
 	bsd/bsd.h \
-	bsd/err.h \
-	bsd/getopt.h \
-	bsd/grp.h \
 	bsd/inttypes.h \
 	bsd/libutil.h \
-	bsd/md5.h \
-	bsd/nlist.h \
-	bsd/pwd.h \
-	bsd/readpassphrase.h \
 	bsd/stdio.h \
 	bsd/stdlib.h \
 	bsd/string.h \
+	bsd/unistd.h \
+	bsd/wchar.h \
+	# EOL
+
+if ABI_ERR_H
+nobase_include_HEADERS += \
+	bsd/err.h \
+	# EOL
+endif
+
+if ABI_BSD_GETOPT
+nobase_include_HEADERS += \
+	bsd/getopt.h \
+	# EOL
+endif
+
+if ABI_MD5
+nobase_include_HEADERS += \
+	bsd/md5.h \
+	# EOL
+endif
+
+if ABI_NLIST
+nobase_include_HEADERS += \
+	bsd/nlist.h \
+	# EOL
+endif
+
+if ABI_READPASSPHRASE
+nobase_include_HEADERS += \
+	bsd/readpassphrase.h \
+	# EOL
+endif
+
+if ABI_PWCACHE
+nobase_include_HEADERS += \
+	bsd/grp.h \
+	bsd/pwd.h \
+	# EOL
+endif
+
+if ABI_STRINGLIST
+nobase_include_HEADERS += \
 	bsd/stringlist.h \
+	# EOL
+endif
+
+if ABI_TIMECONV
+nobase_include_HEADERS += \
 	bsd/timeconv.h \
-	bsd/unistd.h \
+	# EOL
+endif
+
+if ABI_VIS
+nobase_include_HEADERS += \
 	bsd/vis.h \
-	bsd/wchar.h \
-	$(nil)
+	# EOL
+endif
+
+do_header_subst = $(AM_V_GEN) $(SED) \
+	-e 's:^//\(.define LIBBSD_SYS_TIME_BITS\).*:\1 $(LIBBSD_SYS_TIME_BITS):' \
+	-e 's:^//\(.define LIBBSD_SYS_HAS_TIME64\).*:\1 $(LIBBSD_SYS_HAS_TIME64):' \
+	# EOL
+
+install-data-hook:
+	$(do_header_subst) <$(DESTDIR)$(includedir)/bsd/sys/cdefs.h \
+	                   >$(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new
+	mv  $(DESTDIR)$(includedir)/bsd/sys/cdefs.h.new \
+	    $(DESTDIR)$(includedir)/bsd/sys/cdefs.h
diff --git a/include/bsd/grp.h b/include/bsd/grp.h
index b2705e5..acf3d25 100644
--- a/include/bsd/grp.h
+++ b/include/bsd/grp.h
@@ -44,8 +44,10 @@
 __BEGIN_DECLS
 int
 gid_from_group(const char *, gid_t *);
+#ifndef __APPLE__
 const char *
 group_from_gid(gid_t, int);
+#endif
 __END_DECLS
 
 #endif
diff --git a/include/bsd/md5.h b/include/bsd/md5.h
index 5bd58d8..bfeecfd 100644
--- a/include/bsd/md5.h
+++ b/include/bsd/md5.h
@@ -24,8 +24,10 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#ifndef _AIX
 #ifdef LIBBSD_OVERLAY
 #include_next <md5.h>
 #else
 #include <md5.h>
 #endif
+#endif
diff --git a/include/bsd/pwd.h b/include/bsd/pwd.h
index 798af4b..38214ae 100644
--- a/include/bsd/pwd.h
+++ b/include/bsd/pwd.h
@@ -44,8 +44,10 @@
 __BEGIN_DECLS
 int
 uid_from_user(const char *, uid_t *);
+#ifndef __APPLE__
 const char *
 user_from_uid(uid_t, int);
+#endif
 __END_DECLS
 
 #endif
diff --git a/include/bsd/stdio.h b/include/bsd/stdio.h
index 8929bfa..eab415c 100644
--- a/include/bsd/stdio.h
+++ b/include/bsd/stdio.h
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2004-2005, 2009, 2011-2013 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2004-2024 Guillem Jover <guillem@hadrons.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -44,8 +44,18 @@
 #include <bsd/sys/cdefs.h>
 #endif
 #include <sys/types.h>
+#if defined(_AIX)
+#include <stdarg.h>
+#endif
 
 __BEGIN_DECLS
+#if defined(_AIX)
+int asprintf(char **strp, char const *fmt, ...)
+	__printflike(2, 3);
+int vasprintf(char **strp, char const *fmt, va_list args)
+	__printflike(2, 0);
+#endif
+
 const char *fmtcheck(const char *, const char *);
 
 /* XXX: The function requires cooperation from the system libc to store the
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index 2bee974..e404bdb 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -51,16 +51,24 @@
 #include <stdint.h>
 
 __BEGIN_DECLS
+#if !defined(__APPLE__) && !defined(__sun)
+#if !defined(__GLIBC__) || \
+    !__GLIBC_PREREQ(2, 36) || \
+    !defined(_DEFAULT_SOURCE)
 uint32_t arc4random(void);
-void arc4random_stir(void);
-void arc4random_addrandom(unsigned char *dat, int datlen);
 void arc4random_buf(void *_buf, size_t n);
 uint32_t arc4random_uniform(uint32_t upper_bound);
+#endif
+void arc4random_stir(void);
+void arc4random_addrandom(unsigned char *dat, int datlen);
+#endif
 
 int dehumanize_number(const char *str, int64_t *size);
 
+#if !defined(__APPLE__)
 const char *getprogname(void);
 void setprogname(const char *);
+#endif
 
 int heapsort(void *, size_t, size_t, int (*)(const void *, const void *));
 int mergesort(void *base, size_t nmemb, size_t size,
@@ -72,7 +80,9 @@ int sradixsort(const unsigned char **base, int nmemb,
 
 void *reallocf(void *ptr, size_t size);
 #if !defined(__GLIBC__) || \
-    (defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 26) || !defined(_GNU_SOURCE)))
+    !__GLIBC_PREREQ(2, 26) || \
+    (__GLIBC_PREREQ(2, 26) && !__GLIBC_PREREQ(2, 29) && !defined(_GNU_SOURCE)) || \
+    (__GLIBC_PREREQ(2, 29) && !defined(_DEFAULT_SOURCE))
 void *reallocarray(void *ptr, size_t nmemb, size_t size);
 #endif
 void *recallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size);
diff --git a/include/bsd/string.h b/include/bsd/string.h
index f987fee..64b4e13 100644
--- a/include/bsd/string.h
+++ b/include/bsd/string.h
@@ -41,13 +41,19 @@
 #include <sys/types.h>
 
 __BEGIN_DECLS
+#if !defined(__APPLE__) && \
+    (!defined(__GLIBC__) || !__GLIBC_PREREQ(2, 38) || !defined(_DEFAULT_SOURCE))
 size_t strlcpy(char *dst, const char *src, size_t siz);
 size_t strlcat(char *dst, const char *src, size_t siz);
+#endif
 char *strnstr(const char *str, const char *find, size_t str_len);
+#ifndef __APPLE__
 void strmode(mode_t mode, char *str);
+#endif
 
 #if !defined(__GLIBC__) || \
-    (defined(__GLIBC__) && (!__GLIBC_PREREQ(2, 25) || !defined(_GNU_SOURCE)))
+    !__GLIBC_PREREQ(2, 25) || \
+    !defined(_DEFAULT_SOURCE)
 void explicit_bzero(void *buf, size_t len);
 #endif
 __END_DECLS
diff --git a/include/bsd/sys/cdefs.h b/include/bsd/sys/cdefs.h
index ac18296..d2627c4 100644
--- a/include/bsd/sys/cdefs.h
+++ b/include/bsd/sys/cdefs.h
@@ -37,6 +37,9 @@
 #ifndef __is_identifier
 #define __is_identifier(x) 1
 #endif
+#ifndef __has_builtin
+#define __has_builtin(x) !__is_identifier(x)
+#endif
 
 #ifdef LIBBSD_OVERLAY
 /*
@@ -83,6 +86,10 @@
 #define _SYS_CDEFS_H
 #endif
 
+/* Define the ABI for the current system. */
+//#define LIBBSD_SYS_TIME_BITS 0
+//#define LIBBSD_SYS_HAS_TIME64 0
+
 #define LIBBSD_CONCAT(x, y)	x ## y
 #define LIBBSD_STRING(x)	#x
 
@@ -182,7 +189,7 @@
  * require it.
  */
 #ifndef __offsetof
-# if LIBBSD_GCC_VERSION >= 0x0401 || !__is_identifier(__builtin_offsetof)
+# if LIBBSD_GCC_VERSION >= 0x0401 || __has_builtin(__builtin_offsetof)
 #  define __offsetof(type, field)	__builtin_offsetof(type, field)
 # else
 #  ifndef __cplusplus
diff --git a/include/bsd/timeconv.h b/include/bsd/timeconv.h
index a426bd3..755c89c 100644
--- a/include/bsd/timeconv.h
+++ b/include/bsd/timeconv.h
@@ -58,4 +58,17 @@ time_t _long_to_time(long tlong);
 int _time_to_int(time_t t);
 time_t _int_to_time(int tint);
 
+#if LIBBSD_SYS_TIME_BITS == 32 && LIBBSD_SYS_HAS_TIME64
+#if defined _TIME_BITS &&  _TIME_BITS == 64
+time_t LIBBSD_REDIRECT(_time32_to_time, (int32_t t32), _time32_to_time_time64);
+int32_t LIBBSD_REDIRECT(_time_to_time32, (time_t t), _time_to_time32_time64);
+time_t LIBBSD_REDIRECT(_time64_to_time, (int64_t t64), _time64_to_time_time64);
+int64_t LIBBSD_REDIRECT(_time_to_time64, (time_t t), _time_to_time64_time64);
+long LIBBSD_REDIRECT(_time_to_long, (time_t t), _time_to_long_time64);
+time_t LIBBSD_REDIRECT(_long_to_time, (long tlong), _long_to_time_time64);
+int LIBBSD_REDIRECT(_time_to_int, (time_t t), _time_to_int_time64);
+time_t LIBBSD_REDIRECT(_int_to_time, (int tint), _int_to_time_time64);
+#endif
+#endif
+
 #endif /* LIBBSD_TIMECONV_H */
diff --git a/include/bsd/unistd.h b/include/bsd/unistd.h
index 847a0f7..3277fda 100644
--- a/include/bsd/unistd.h
+++ b/include/bsd/unistd.h
@@ -47,6 +47,7 @@
 #endif
 
 __BEGIN_DECLS
+#if !defined(__APPLE__)
 extern int optreset;
 
 #ifdef LIBBSD_OVERLAY
@@ -55,6 +56,7 @@ extern int optreset;
 #endif
 
 int bsd_getopt(int argc, char * const argv[], const char *shortopts);
+#endif
 
 mode_t getmode(const void *set, mode_t mode);
 
@@ -65,7 +67,11 @@ mode_t getmode(const void *set, mode_t mode);
 
 void *bsd_setmode(const char *mode_str);
 
+#if !defined(__GLIBC__) || \
+    !__GLIBC_PREREQ(2, 34) || \
+    !defined(_DEFAULT_SOURCE)
 void closefrom(int lowfd);
+#endif
 
 /* Compatibility with sendmail implementations. */
 #define initsetproctitle(c, a, e) setproctitle_init((c), (a), (e))
diff --git a/include/bsd/vis.h b/include/bsd/vis.h
index fb9ad27..dd150d7 100644
--- a/include/bsd/vis.h
+++ b/include/bsd/vis.h
@@ -93,7 +93,7 @@
  * existing one in OpenBSD and Freedesktop's libbsd (the former having existed
  * for over ten years). Despite this incompatibility being reported during
  * development (see http://gnats.netbsd.org/44977) they still shipped it.
- * Even more unfortunately FreeBSD and later MacOS picked up this incompatible
+ * Even more unfortunately FreeBSD and later macOS picked up this incompatible
  * implementation.
  *
  * Provide both implementations and default for now on the historical one to
diff --git a/include/bsd/wchar.h b/include/bsd/wchar.h
index 7216503..8edef23 100644
--- a/include/bsd/wchar.h
+++ b/include/bsd/wchar.h
@@ -50,8 +50,10 @@
 __BEGIN_DECLS
 wchar_t *fgetwln(FILE *stream, size_t *len);
 
+#if !defined(__APPLE__)
 size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t size);
 size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t size);
+#endif
 __END_DECLS
 
 #endif
diff --git a/m4/.gitignore b/m4/.gitignore
new file mode 100644
index 0000000..10fb5d4
--- /dev/null
+++ b/m4/.gitignore
@@ -0,0 +1,2 @@
+*.m4
+!libbsd*.m4
diff --git a/m4/libbsd-compiler.m4 b/m4/libbsd-compiler.m4
new file mode 100644
index 0000000..ace5ce1
--- /dev/null
+++ b/m4/libbsd-compiler.m4
@@ -0,0 +1,104 @@
+# Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_CHECK_COMPILER_FLAG
+# --------------------------
+AC_DEFUN([LIBBSD_CHECK_COMPILER_FLAG], [
+  AS_VAR_PUSHDEF([libbsd_varname_cache], [libbsd_cv_cflags_$1])
+  AC_CACHE_CHECK([whether $CC accepts $1], [libbsd_varname_cache], [
+    m4_define([libbsd_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
+    AS_VAR_COPY([libbsd_save_CFLAGS], [CFLAGS])
+    AS_VAR_SET([CFLAGS], ["-Werror libbsd_check_flag"])
+    AC_COMPILE_IFELSE([
+      AC_LANG_SOURCE([[]])
+    ], [
+      AS_VAR_SET([libbsd_varname_cache], [yes])
+    ], [
+      AS_VAR_SET([libbsd_varname_cache], [no])
+    ])
+    AS_VAR_COPY([CFLAGS], [libbsd_save_CFLAGS])
+  ])
+  AS_VAR_IF([libbsd_varname_cache], [yes], [
+    AS_VAR_APPEND([LIBBSD_COMPILER_FLAGS], [" $1"])
+  ])
+  AS_VAR_POPDEF([libbsd_varname_cache])
+])
+
+# LIBBSD_HAS_GNU_INIT_ARRAY
+# -------------------------
+AC_DEFUN([LIBBSD_HAS_GNU_INIT_ARRAY], [
+  AC_CACHE_CHECK([for GNU .init_array section support],
+    [libbsd_cv_gnu_init_array_support], [
+    AC_RUN_IFELSE([
+      AC_LANG_SOURCE([[
+static int rc = 1;
+static void init(int argc) { if (argc == 1) rc = 0; }
+void (*init_func)(int argc) __attribute__((__section__(".init_array"), __used__)) = init;
+int main() { return rc; }
+      ]])
+    ], [
+      libbsd_cv_gnu_init_array_support=yes
+    ], [
+      libbsd_cv_gnu_init_array_support=no
+    ], [
+      AC_PREPROC_IFELSE([
+        AC_LANG_SOURCE([[
+/* Look for a known libc that supports .init_array with the GNU extension
+ * to pass main() arguments to the init functions. */
+#include <stdlib.h>
+#if defined __GLIBC_PREREQ
+#  if __GLIBC_PREREQ(2, 4)
+/* glibc supports GNU .init_array since 2.4. */
+#  else
+#    error glibc does not support GNU .init_array
+#  endif
+#else
+/*
+ * Basic SysV ABI .init_array support, init functions do not get arguments:
+ * - Bionic since its inception.
+ * - uClibc since 0.9.29.
+ */
+#  error unknown whether libc supports GNU .init_array
+#endif
+        ]])
+      ], [
+        libbsd_cv_gnu_init_array_support=yes
+      ], [
+        libbsd_cv_gnu_init_array_support=no
+      ])
+    ])
+  ])
+  AM_CONDITIONAL([BUILD_LIBBSD_CTOR],
+    [test "$libbsd_cv_gnu_init_array_support" = yes])
+])
+
+# LIBBSD_SELECT_API(name, desc)
+# -----------------
+AC_DEFUN([LIBBSD_SELECT_API], [
+  AS_IF([test -z "$AS_TR_SH([api_$1])"], [
+    AC_MSG_ERROR([missing API selection for $1])
+  ], [test "$AS_TR_SH([api_$1])" = "unknown"], [
+    AC_MSG_ERROR([unknown ABI selection for $1])
+  ], [test "$AS_TR_SH([api_$1])" = "yes"], [
+    AC_DEFINE(AS_TR_CPP([LIBBSD_API_$1]), [1], [Provide API for $2])
+  ], [
+    AC_DEFINE(AS_TR_CPP([LIBBSD_API_$1]), [0])
+  ])
+  AM_CONDITIONAL(AS_TR_CPP([API_$1]),
+    [test "x$AS_TR_SH([api_$1])" = "xyes"])
+])
+
+# LIBBSD_SELECT_ABI(name, desc)
+# -----------------
+AC_DEFUN([LIBBSD_SELECT_ABI], [
+  AS_IF([test -z "$AS_TR_SH([abi_$1])"], [
+    AC_MSG_ERROR([missing ABI selection for $1])
+  ], [test "$AS_TR_SH([abi_$1])" = "unknown"], [
+    AC_MSG_ERROR([unknown ABI selection for $1])
+  ], [test "$AS_TR_SH([abi_$1])" = "yes"], [
+    AC_DEFINE(AS_TR_CPP([LIBBSD_ABI_$1]), [1], [Provide ABI for $2])
+  ], [
+    AC_DEFINE(AS_TR_CPP([LIBBSD_ABI_$1]), [0])
+  ])
+  AM_CONDITIONAL(AS_TR_CPP([ABI_$1]),
+    [test "x$AS_TR_SH([abi_$1])" = "xyes"])
+])
diff --git a/m4/libbsd-funcs.m4 b/m4/libbsd-funcs.m4
new file mode 100644
index 0000000..379afc5
--- /dev/null
+++ b/m4/libbsd-funcs.m4
@@ -0,0 +1,57 @@
+# Copyright © 2011-2024 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_CHECK_PROGNAME
+# ---------------------
+AC_DEFUN([LIBBSD_CHECK_PROGNAME], [
+  AC_MSG_CHECKING([for program_invocation_short_name])
+  AC_LINK_IFELSE([
+    AC_LANG_PROGRAM([[
+#include <errno.h>
+    ]], [[
+const char *p = program_invocation_short_name;
+    ]])
+  ], [
+    AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
+      [Define to 1 if you have program_invocation_short_name])
+    AC_MSG_RESULT([yes])
+  ], [
+    AC_MSG_RESULT([no])
+  ])
+
+  AC_MSG_CHECKING([for __progname])
+  AC_LINK_IFELSE([
+    AC_LANG_PROGRAM([[
+#include <stdio.h>
+extern char *__progname;
+    ]], [[
+printf("%s", __progname);
+    ]])
+  ], [
+    AC_DEFINE([HAVE___PROGNAME], [1], [Define to 1 if you have __progname])
+    AC_MSG_RESULT([yes])
+  ], [
+    AC_MSG_RESULT([no])
+  ])
+])
+
+# LIBBSD_CHECK_REGISTER_ATFORK
+# ----------------------------
+AC_DEFUN([LIBBSD_CHECK_REGISTER_ATFORK], [
+  AC_MSG_CHECKING([for __register_atfork])
+  AC_LINK_IFELSE([
+    AC_LANG_PROGRAM([[
+#include <stddef.h>
+extern void *__dso_handle;
+extern int __register_atfork(void (*)(void), void(*)(void), void (*)(void), void *);
+    ]], [[
+__register_atfork(NULL, NULL, NULL, __dso_handle);
+    ]])
+  ], [
+    AC_DEFINE([HAVE___REGISTER_ATFORK], [1],
+      [Define to 1 if you have __register_atfork])
+    AC_MSG_RESULT([yes])
+  ], [
+    LIBBSD_LIBS="$LIBBSD_LIBS -pthread"
+    AC_MSG_RESULT([no])
+  ])
+])
diff --git a/m4/libbsd-large.m4 b/m4/libbsd-large.m4
new file mode 100644
index 0000000..213f2cb
--- /dev/null
+++ b/m4/libbsd-large.m4
@@ -0,0 +1,61 @@
+# Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_SYS_TIME64
+# -----------------
+# Check for availability of time64 support.
+AC_DEFUN([LIBBSD_SYS_TIME64], [
+  # Check the default time_t size.
+  AC_CHECK_SIZEOF([time_t], [], [[
+#undef _TIME_BITS
+#include <time.h>
+]])
+  AS_IF([test $ac_cv_sizeof_time_t -eq 8], [
+    libbsd_sys_time_bits=64
+  ], [
+    libbsd_sys_time_bits=32
+  ])
+  AC_DEFINE_UNQUOTED([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits],
+    [The number of bits for the default system time_t ABI])
+  AC_SUBST([LIBBSD_SYS_TIME_BITS], [$libbsd_sys_time_bits])
+  AS_UNSET([ac_cv_sizeof_time_t])
+  AM_CONDITIONAL([LIBBSD_SYS_IS_TIME32], [test "$libbsd_sys_time_bits" -eq 32])
+
+  # Check the whether the system supports 64-bit time_t.
+  AC_CHECK_SIZEOF([time_t], [], [[
+#define _FILE_OFFSET_BITS 64
+#define _TIME_BITS 64
+#include <time.h>
+]])
+  AS_IF([test $ac_cv_sizeof_time_t -eq 8], [
+    libbsd_sys_has_time64=1
+  ], [
+    libbsd_sys_has_time64=0
+  ])
+  AC_DEFINE_UNQUOTED([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64],
+    [Enable if the system supports 64-bit time_t])
+  AC_SUBST([LIBBSD_SYS_HAS_TIME64], [$libbsd_sys_has_time64])
+  AM_CONDITIONAL([LIBBSD_SYS_HAS_TIME64], [test "$libbsd_sys_has_time64" -eq 1])
+  AS_IF([test "$libbsd_sys_time_bits" -eq 32 && \
+         test "$libbsd_sys_has_time64" -eq 1], [
+    abi_time64=yes
+  ], [
+    abi_time64=no
+  ])
+  LIBBSD_SELECT_ABI([time64], [explicit time64 time_t support])
+
+  AC_CHECK_SIZEOF([off_t], [], [[
+#define _FILE_OFFSET_BITS 64
+#include <sys/types.h>
+]])
+  AS_IF([test $ac_cv_sizeof_off_t = 8], [
+    libbsd_sys_has_lfs=1
+  ], [
+    libbsd_sys_has_lfs=0
+  ])
+
+  AS_IF([test $libbsd_sys_has_lfs -eq 1 && \
+         test $libbsd_sys_time_bits -eq 32 && \
+         test $ac_cv_sizeof_time_t -eq 8], [
+    AC_DEFINE([_TIME_BITS], [64], [Enable 64-bit time_t support])
+  ])
+])
diff --git a/m4/libbsd-linker.m4 b/m4/libbsd-linker.m4
new file mode 100644
index 0000000..394bac5
--- /dev/null
+++ b/m4/libbsd-linker.m4
@@ -0,0 +1,27 @@
+# Copyright © 2019, 2022 Guillem Jover <guillem@hadrons.org>
+
+# LIBBSD_LINKER_VERSION_SCRIPT
+# ----------------------------
+AC_DEFUN([LIBBSD_LINKER_VERSION_SCRIPT], [
+  AC_CACHE_CHECK([if ld supports --version-script flag],
+    [libbsd_cv_version_script], [
+    echo "{ global: symbol; local: *; };" >conftest.map
+    save_LDFLAGS=$LDFLAGS
+    LDFLAGS="$LDFLAGS -Wl,--version-script=conftest.map"
+    AC_LINK_IFELSE([
+      AC_LANG_PROGRAM([[
+extern int symbol(void);
+int symbol(void) { return 0; }
+]], [[
+]])
+    ], [
+      libbsd_cv_version_script=yes
+    ], [
+      libbsd_cv_version_script=no
+    ])
+    LDFLAGS="$save_LDFLAGS"
+    rm -f conftest.map
+  ])
+  AM_CONDITIONAL([HAVE_LINKER_VERSION_SCRIPT],
+    [test "x$libbsd_cv_version_script" = "xyes"])
+])
diff --git a/man/Makefile.am b/man/Makefile.am
index 5dd37bf..02c8ffb 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -2,16 +2,16 @@
 
 EXTRA_DIST = \
 	mdX.3bsd \
-	$(nil)
+	# EOL
 
 CLEANFILES = \
 	md5.3bsd \
-	$(nil)
+	# EOL
 
 SED_MD5_SUBST = -e 's/mdX/md5/g' -e 's/mdY/md4/g' -e 's/MDX/MD5/g'
 
 md5.3bsd: $(srcdir)/mdX.3bsd
-	$(AM_V_GEN) sed $(SED_MD5_SUBST) $< > $@
+	$(AM_V_GEN) $(SED) $(SED_MD5_SUBST) $< >$@
 
 dist_man_MANS = \
 	LIST_CLASS_ENTRY.3bsd \
@@ -141,13 +141,6 @@ dist_man_MANS = \
 	TAILQ_PREV.3bsd \
 	TAILQ_REMOVE.3bsd \
 	TAILQ_SWAP.3bsd \
-	TIMESPEC_TO_TIMEVAL.3bsd \
-	TIMEVAL_TO_TIMESPEC.3bsd \
-	arc4random.3bsd \
-	arc4random_addrandom.3bsd \
-	arc4random_buf.3bsd \
-	arc4random_stir.3bsd \
-	arc4random_uniform.3bsd \
 	be16dec.3bsd \
 	be16enc.3bsd \
 	be32dec.3bsd \
@@ -166,89 +159,277 @@ dist_man_MANS = \
 	bitstr_size.3bsd \
 	bitstring.3bsd \
 	byteorder.3bsd \
+	le16dec.3bsd \
+	le16enc.3bsd \
+	le32dec.3bsd \
+	le32enc.3bsd \
+	le64dec.3bsd \
+	le64enc.3bsd \
+	libbsd.7 \
+	queue.3bsd \
+	timeradd.3bsd \
+	timerclear.3bsd \
+	timercmp.3bsd \
+	timerisset.3bsd \
+	timersub.3bsd \
+	timespecadd.3bsd \
+	timespecclear.3bsd \
+	timespeccmp.3bsd \
+	timespecisset.3bsd \
+	timespecsub.3bsd \
+	tree.3bsd \
+	# EOL
+
+if API_TIME_MACROS
+dist_man_MANS += \
+	TIMESPEC_TO_TIMEVAL.3bsd \
+	TIMEVAL_TO_TIMESPEC.3bsd \
+	# EOL
+endif
+
+if ABI_ACCMODE
+dist_man_MANS += \
+	getmode.3bsd \
+	setmode.3bsd \
+	# EOL
+endif
+
+if ABI_ARC4RANDOM
+dist_man_MANS += \
+	arc4random.3bsd \
+	arc4random_addrandom.3bsd \
+	arc4random_buf.3bsd \
+	arc4random_stir.3bsd \
+	arc4random_uniform.3bsd \
+	# EOL
+endif
+
+if ABI_CLOSEFROM
+dist_man_MANS += \
 	closefrom.3bsd \
-	dehumanize_number.3bsd \
+	# EOL
+endif
+
+if ABI_ERRC
+dist_man_MANS += \
 	errc.3bsd \
+	verrc.3bsd \
+	vwarnc.3bsd \
+	warnc.3bsd \
+	# EOL
+endif
+
+if ABI_EXPAND_NUMBER
+dist_man_MANS += \
 	expand_number.3bsd \
+	# EOL
+endif
+
+if ABI_EXPLICIT_BZERO
+dist_man_MANS += \
 	explicit_bzero.3bsd \
+	# EOL
+endif
+
+if ABI_FGETLN
+dist_man_MANS += \
 	fgetln.3bsd \
 	fgetwln.3bsd \
+	fparseln.3bsd \
+	# EOL
+endif
+
+if ABI_FLOPEN
+dist_man_MANS += \
 	flopen.3bsd \
-	freezero.3bsd \
+	# EOL
+endif
+
+if ABI_FMTCHECK
+dist_man_MANS += \
 	fmtcheck.3bsd \
-	fparseln.3bsd \
+	# EOL
+endif
+
+if ABI_FPURGE
+dist_man_MANS += \
 	fpurge.3bsd \
+	# EOL
+endif
+
+if ABI_FREEZERO
+dist_man_MANS += \
+	freezero.3bsd \
+	# EOL
+endif
+
+if ABI_FUNOPEN
+dist_man_MANS += \
 	funopen.3bsd \
+	# EOL
+endif
+
+if ABI_GETBSIZE
+dist_man_MANS += \
 	getbsize.3bsd \
-	getmode.3bsd \
+	# EOL
+endif
+
+if ABI_GETPEEREID
+dist_man_MANS += \
 	getpeereid.3bsd \
-	getprogname.3bsd \
-	gid_from_group.3bsd \
-	group_from_gid.3bsd \
-	heapsort.3bsd \
+	# EOL
+endif
+
+if ABI_HUMANIZE_NUMBER
+dist_man_MANS += \
+	dehumanize_number.3bsd \
 	humanize_number.3bsd \
-	le16dec.3bsd \
-	le16enc.3bsd \
-	le32dec.3bsd \
-	le32enc.3bsd \
-	le64dec.3bsd \
-	le64enc.3bsd \
-	libbsd.7 \
+	# EOL
+endif
+
+if ABI_MD5
+dist_man_MANS += \
 	md5.3bsd \
-	mergesort.3bsd \
+	# EOL
+endif
+
+if ABI_NLIST
+dist_man_MANS += \
 	nlist.3bsd \
+	# EOL
+endif
+
+if ABI_PIDFILE
+dist_man_MANS += \
 	pidfile.3bsd \
 	pidfile_close.3bsd \
 	pidfile_open.3bsd \
 	pidfile_remove.3bsd \
 	pidfile_write.3bsd \
+	# EOL
+endif
+
+if ABI_PROCTITLE
+dist_man_MANS += \
+	setproctitle.3bsd \
+	setproctitle_init.3bsd \
+	# EOL
+endif
+
+if ABI_PROGNAME
+dist_man_MANS += \
+	getprogname.3bsd \
+	setprogname.3bsd \
+	# EOL
+endif
+
+if ABI_PWCACHE
+dist_man_MANS += \
 	pwcache.3bsd \
-	queue.3bsd \
-	radixsort.3bsd \
+	gid_from_group.3bsd \
+	uid_from_user.3bsd \
+	# EOL
+
+if ABI_NAME_FROM_ID
+dist_man_MANS += \
+	group_from_gid.3bsd \
+	user_from_uid.3bsd \
+	# EOL
+endif
+endif
+
+if ABI_READPASSPHRASE
+dist_man_MANS += \
 	readpassphrase.3bsd \
+	# EOL
+endif
+
+if ABI_REALLOCARRAY
+dist_man_MANS += \
 	reallocarray.3bsd \
+	# EOL
+endif
+
+if ABI_REALLOCF
+dist_man_MANS += \
 	reallocf.3bsd \
+	# EOL
+endif
+
+if ABI_RECALLOCARRAY
+dist_man_MANS += \
 	recallocarray.3bsd \
-	setmode.3bsd \
-	setproctitle.3bsd \
-	setproctitle_init.3bsd \
-	setprogname.3bsd \
+	# EOL
+endif
+
+if ABI_SORT
+dist_man_MANS += \
+	heapsort.3bsd \
+	mergesort.3bsd \
+	radixsort.3bsd \
+	sradixsort.3bsd \
+	# EOL
+endif
+
+if ABI_STRINGLIST
+dist_man_MANS += \
 	sl_add.3bsd \
 	sl_delete.3bsd \
 	sl_find.3bsd \
 	sl_free.3bsd \
 	sl_init.3bsd \
-	sradixsort.3bsd \
 	stringlist.3bsd \
+	# EOL
+endif
+
+if ABI_STRL
+dist_man_MANS += \
 	strlcat.3bsd \
 	strlcpy.3bsd \
+	# EOL
+endif
+
+if ABI_STRMODE
+dist_man_MANS += \
 	strmode.3bsd \
+	# EOL
+endif
+
+if ABI_STRNSTR
+dist_man_MANS += \
 	strnstr.3bsd \
-	strnunvis.3bsd \
-	strnvis.3bsd \
+	# EOL
+endif
+
+if ABI_STRTOX
+dist_man_MANS += \
 	strtoi.3bsd \
-	strtonum.3bsd \
 	strtou.3bsd \
+	# EOL
+endif
+
+if ABI_STRTONUM
+dist_man_MANS += \
+	strtonum.3bsd \
+	# EOL
+endif
+
+if ABI_VIS
+dist_man_MANS += \
+	strnunvis.3bsd \
+	strnvis.3bsd \
 	strunvis.3bsd \
 	strvis.3bsd \
 	strvisx.3bsd \
-	timeradd.3bsd \
-	timerclear.3bsd \
-	timercmp.3bsd \
-	timerisset.3bsd \
-	timersub.3bsd \
-	timespec.3bsd \
-	timespecadd.3bsd \
-	timespecclear.3bsd \
-	timespeccmp.3bsd \
-	timespecisset.3bsd \
-	timespecsub.3bsd \
-	timeval.3bsd \
-	tree.3bsd \
-	uid_from_user.3bsd \
 	unvis.3bsd \
-	user_from_uid.3bsd \
 	vis.3bsd \
+	# EOL
+endif
+
+if ABI_WCSL
+dist_man_MANS += \
 	wcslcat.3bsd \
 	wcslcpy.3bsd \
-	$(nil)
+	# EOL
+endif
diff --git a/man/TIMESPEC_TO_TIMEVAL.3bsd b/man/TIMESPEC_TO_TIMEVAL.3bsd
index e47176e..28ebcf4 100644
--- a/man/TIMESPEC_TO_TIMEVAL.3bsd
+++ b/man/TIMESPEC_TO_TIMEVAL.3bsd
@@ -1 +1,100 @@
-.so man3/timeval.3bsd
+.\" $NetBSD: timeval.3,v 1.12 2011/04/12 08:39:26 jruoho Exp $
+.\"
+.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Jukka Ruohonen.
+.\"
+.\" 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.
+.\"
+.\" 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.
+.\"
+.Dd April 12, 2011
+.Dt TIMEVAL_TO_TIMESPEC 3bsd
+.Os
+.Sh NAME
+.Nm TIMEVAL_TO_TIMESPEC ,
+.Nm TIMESPEC_TO_TIMEVAL
+.Nd time structures conversion macros
+.Sh LIBRARY
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
+.Lb libbsd
+.Sh SYNOPSIS
+.In sys/time.h
+(See
+.Xr libbsd 7
+for include usage.)
+.Ft void
+.Fn TIMEVAL_TO_TIMESPEC "struct timeval *tv" "struct timespec *ts"
+.Ft void
+.Fn TIMESPEC_TO_TIMEVAL "struct timeval *tv" "struct timespec *ts"
+.Sh DESCRIPTION
+The
+.Va timeval
+structure represents elapsed time, in whole seconds,
+and the rest of the elapsed time in microseconds.
+.Pp
+The
+.Va timespec
+structure represents elapsed time, in whole seconds,
+and the rest of the elapsed time in nanoseconds.
+.Pp
+A microsecond is equal to one millionth of a second,
+1000 nanoseconds, or 1/1000 milliseconds.
+To ease the conversions, the macros
+.Fn TIMEVAL_TO_TIMESPEC
+and
+.Fn TIMESPEC_TO_TIMEVAL
+can be used to convert between
+.Em struct timeval
+and
+.Em struct timespec .
+.Sh EXAMPLES
+It can be stressed that the traditional
+.Tn UNIX
+.Va timeval
+and
+.Va timespec
+structures represent elapsed time, measured by the system clock.
+The following sketch implements a function suitable
+for use in a context where the
+.Va timespec
+structure is required for a conditional timeout:
+.Bd -literal -offset indent
+static void
+example(struct timespec *spec, time_t minutes)
+{
+	struct timeval elapsed;
+
+	(void)gettimeofday(&elapsed, NULL);
+
+	TIMEVAL_TO_TIMESPEC(&elapsed, spec);
+
+	/* Add the offset for timeout in minutes. */
+	spec->tv_sec = spec->tv_sec + minutes * 60;
+}
+.Ed
+.Pp
+A better alternative would use the more precise
+.Xr clock_gettime 2 .
+.Sh SEE ALSO
+.Xr timeradd 3bsd
diff --git a/man/TIMEVAL_TO_TIMESPEC.3bsd b/man/TIMEVAL_TO_TIMESPEC.3bsd
index e47176e..7741548 100644
--- a/man/TIMEVAL_TO_TIMESPEC.3bsd
+++ b/man/TIMEVAL_TO_TIMESPEC.3bsd
@@ -1 +1 @@
-.so man3/timeval.3bsd
+.so man3/TIMESPEC_TO_TIMEVAL.3bsd
diff --git a/man/arc4random.3bsd b/man/arc4random.3bsd
index c509a8e..7205f44 100644
--- a/man/arc4random.3bsd
+++ b/man/arc4random.3bsd
@@ -1,8 +1,11 @@
-.\" $OpenBSD: arc4random.3,v 1.34 2014/07/19 16:11:16 naddy Exp $
+.\"	$NetBSD: arc4random.3,v 1.21 2016/07/15 21:19:19 wiz Exp $
 .\"
-.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
+.\" Copyright (c) 2014 The NetBSD Foundation, Inc.
 .\" All rights reserved.
 .\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Taylor R. Campbell.
+.\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
 .\" are met:
@@ -11,35 +14,29 @@
 .\" 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. All advertising materials mentioning features or use of this software
-.\"    must display the following acknowledgement:
-.\"      This product includes software developed by Niels Provos.
-.\" 4. The name of the author may not be used to endorse or promote products
-.\"    derived from this software without specific prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 .\"
-.\" Manual page, using -mandoc macros
+.\" 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.
 .\"
-.Dd $Mdocdate: July 19 2014 $
-.Dt ARC4RANDOM 3bsd
+.Dd November 16, 2014
+.Dt arc4random 3bsd
 .Os
 .Sh NAME
 .Nm arc4random ,
-.Nm arc4random_buf ,
 .Nm arc4random_uniform ,
+.Nm arc4random_buf ,
 .Nm arc4random_stir ,
 .Nm arc4random_addrandom
-.Nd arc4 random number generator
+.Nd random number generator
 .Sh LIBRARY
 .ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
 .ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@@ -51,83 +48,208 @@
 for include usage.)
 .Ft uint32_t
 .Fn arc4random "void"
-.Ft void
-.Fn arc4random_buf "void *buf" "size_t nbytes"
 .Ft uint32_t
-.Fn arc4random_uniform "uint32_t upper_bound"
+.Fn arc4random_uniform "uint32_t bound"
+.Ft void
+.Fn arc4random_buf "void *buf" "size_t len"
 .Ft void
 .Fn arc4random_stir "void"
 .Ft void
-.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
+.Fn arc4random_addrandom "unsigned char *buf" "int len"
 .Sh DESCRIPTION
-This family of functions provides higher quality data than those
-described in
-.Xr rand 3 ,
-.Xr random 3 ,
-and
-.Xr rand48 3 .
-.Pp
-Use of these functions is encouraged for almost all random number
-consumption because the other interfaces are deficient in either
-quality, portability, standardization, or availability.
-These functions can be called in almost all coding environments,
-including
-.Xr pthreads 3
+The
+.Nm
+family of functions provides a cryptographic pseudorandom number
+generator automatically seeded from the system entropy pool and safe to
+use from multiple threads.
+.Nm
+is designed to prevent an adversary from guessing outputs,
+unlike
+.Xr rand 3
 and
-.Xr chroot 2 .
-.Pp
-High quality 32-bit pseudo-random numbers are generated very quickly.
-On each call, a cryptographic pseudo-random number generator is used
-to generate a new result.
-One data pool is used for all consumers in a process, so that consumption
-under program flow can act as additional stirring.
-The subsystem is re-seeded from the kernel random number subsystem using
-.Xr getentropy 2
-on a regular basis, and also upon
-.Xr fork 2 .
+.Xr random 3 ,
+and is faster and more convenient than reading from
+.Pa /dev/urandom
+directly.
 .Pp
-The
 .Fn arc4random
-function returns a single 32-bit value.
-.Pp
-The
-.Fn arc4random_buf
-function fills the region
-.Fa buf
-of length
-.Fa nbytes
-with random data.
+returns an integer in [0, 2^32) chosen independently with uniform
+distribution.
 .Pp
 .Fn arc4random_uniform
-will return a single 32-bit value, uniformly distributed but less than
-.Fa upper_bound .
-This is recommended over constructions like
-.Dq Li arc4random() % upper_bound
-as it avoids "modulo bias" when the upper bound is not a power of two.
-In the worst case, this function may consume multiple iterations
-to ensure uniformity; see the source code to understand the problem
-and solution.
+returns an integer in [0,
+.Fa bound )
+chosen independently with uniform distribution.
+.Pp
+.Fn arc4random_buf
+stores
+.Fa len
+bytes into the memory pointed to by
+.Fa buf ,
+each byte chosen independently from [0, 256) with uniform
+distribution.
 .Pp
-The
 .Fn arc4random_stir
-function reads data from
-.Xr getentropy 2
-and uses it to re-seed the subsystem via
-.Fn arc4random_addrandom .
+draws entropy from the operating system and incorporates it into the
+library's PRNG state to influence future outputs.
 .Pp
-There is no need to call
+.Fn arc4random_addrandom
+incorporates
+.Fa len
+bytes, which must be nonnegative, from the buffer
+.Fa buf ,
+into the library's PRNG state to influence future outputs.
+.Pp
+It is not necessary for an application to call
 .Fn arc4random_stir
-before using
+or
+.Fn arc4random_addrandom
+before calling other
+.Nm
+functions.
+The first call to any
+.Nm
+function will initialize the PRNG state unpredictably from the system
+entropy pool.
+.Sh SECURITY MODEL
+The
+.Nm
+functions provide the following security properties against three
+different classes of attackers, assuming enough entropy is provided by
+the operating system:
+.Bl -enum -offset abcd
+.It
+An attacker who has seen some outputs of any of the
+.Nm
+functions cannot predict past or future unseen outputs.
+.It
+An attacker who has seen the library's PRNG state in memory cannot
+predict past outputs.
+.It
+An attacker who has seen one process's PRNG state cannot predict past
+or future outputs in other processes, particularly its parent or
+siblings.
+.El
+.Pp
+One
+.Sq output
+means the result of any single request to an
+.Nm
+function, no matter how short it is.
+.Pp
+The second property is sometimes called
+.Sq forward secrecy ,
+.Sq backtracking resistance ,
+or
+.Sq key erasure after each output .
+.Sh IMPLEMENTATION NOTES
+The
+.Nm
+functions are currently implemented using the ChaCha20 pseudorandom
+function family.
+For any 32-byte string
+.Fa s ,
+.Pf ChaCha20_ Fa s
+is a function from 16-byte strings to 64-byte strings.
+It is conjectured that if
+.Fa s
+is chosen with uniform distribution, then the distribution on
+.Pf ChaCha20_ Fa s
+is indistinguishable to a computationally bounded adversary from a
+uniform distribution on all functions from 16-byte strings to 64-byte
+strings.
+.Pp
+The PRNG state is a 32-byte ChaCha20 key
+.Fa s .
+Each request to
+an
+.Nm
+function
+.Bl -bullet -offset abcd -compact
+.It
+computes the 64-byte quantity
+.Fa x
+=
+.Pf ChaCha20_ Fa s Ns Pq 0 ,
+.It
+splits
+.Fa x
+into two 32-byte quantities
+.Fa s'
+and
+.Fa k ,
+.It
+replaces
+.Fa s
+by
+.Fa s' ,
+and
+.It
+uses
+.Fa k
+as output.
+.El
+.Pp
+.Fn arc4random
+yields the first four bytes of
+.Fa k
+as output directly.
+.Fn arc4random_buf
+either yields up to 32 bytes of
+.Fa k
+as output directly, or, for longer
+requests, uses
+.Fa k
+as a ChaCha20 key and yields the concatenation
+.Pf ChaCha20_ Fa k Ns Pq 0
+||
+.Pf ChaCha20_ Fa k Ns Pq 1
+|| ... as output.
+.Fn arc4random_uniform
+repeats
 .Fn arc4random
-functions family, since
-they automatically initialize themselves.
-.Sh RETURN VALUES
-These functions are always successful, and no return value is
-reserved to indicate an error.
+until it obtains an integer in [2^32 %
+.Fa bound ,
+2^32), and reduces that modulo
+.Fa bound .
+.Pp
+The PRNG state is per-thread, unless memory allocation fails inside the
+library, in which case some threads may share global PRNG state with a
+mutex.
+The global PRNG state is zeroed on fork in the parent via
+.Xr pthread_atfork 3 ,
+and the per-thread PRNG state is zeroed on fork in the child via
+.Xr minherit 2
+with
+.Dv MAP_INHERIT_ZERO ,
+so that the child cannot reuse or see the parent's PRNG state.
+The PRNG state is reseeded automatically from the system entropy pool
+on the first use of an
+.Nm
+function after zeroing.
+.Pp
+The first use of an
+.Nm
+function may abort the process in the highly unlikely event that
+library initialization necessary to implement the security model fails.
+Additionally,
+.Fn arc4random_stir
+and
+.Fn arc4random_addrandom
+may abort the process in the highly unlikely event that the operating
+system fails to provide entropy.
 .Sh SEE ALSO
 .Xr rand 3 ,
-.Xr rand48 3 ,
-.Xr random 3
+.Xr random 3 ,
+.Xr rnd 4 ,
+.Xr cprng 9
+.Rs
+.%A Daniel J. Bernstein
+.%T ChaCha, a variant of Salsa20
+.%D 2008-01-28
+.%O Document ID: 4027b5256e17b9796842e6d0f68b0b5e
+.%U http://cr.yp.to/papers.html#chacha
+.Re
 .Sh HISTORY
 These functions first appeared in
 .Ox 2.1 ,
@@ -135,12 +257,85 @@ These functions first appeared in
 .Nx 1.6 ,
 and
 .Dx 1.0 .
+The functions
+.Fn arc4random ,
+.Fn arc4random_buf
+and
+.Fn arc4random_uniform
+appeared in glibc 2.36.
+.Sh BUGS
+There is no way to get deterministic, reproducible results out of
+.Nm
+for testing purposes.
+.Pp
+The name
+.Sq arc4random
+was chosen for hysterical raisins -- it was originally implemented
+using the RC4 stream cipher, which has been known since shortly after
+it was published in 1994 to have observable biases in the output, and
+is now known to be broken badly enough to admit practical attacks in
+the real world.
+.\" Bob Jenkins, sci.crypt post dated 1994-09-16, message-id
+.\" <359qjg$55v$1@mhadg.production.compuserve.com>,
+.\" https://groups.google.com/d/msg/sci.crypt/JsO3xEATGFA/-wO4ttv7BCYJ
+.\"
+.\" Andrew Roos, `A Class of Weak Keys in the RC4 Stream Cipher',
+.\" sci.crypt posts dated 1995-09-22, message-ids
+.\" <43u1eh$1j3@hermes.is.co.za> and <44ebge$llf@hermes.is.co.za>.
+.\"
+.\" Paul Crowley, `Small bias in RC4 experimentally verified', March
+.\" 1998, http://www.ciphergoth.org/crypto/rc4/
+Unfortunately, the library found widespread adoption and the name stuck
+before anyone recognized that it was silly.
+.Pp
+The signature of
+.Fn arc4random_addrandom
+is silly.
+There is no reason to require casts or accept negative lengths:
+it should take a
+.Vt void *
+buffer and a
+.Vt size_t
+length.
+But it's too late to change that now.
+.Pp
+.Fn arc4random_uniform
+does not help to choose integers in [0,
+.Fa n )
+uniformly at random when
+.Fa n
+> 2^32.
+.Pp
+The security model of
+.Nm
+is stronger than many applications need, and stronger than other
+operating systems provide.
+For example, applications encrypting messages with random, but not
+secret, initialization vectors need only prevent an adversary from
+guessing future outputs, since past outputs will have been published
+already.
+.Pp
+On the one hand,
+.Nm
+could be marginally faster if it were not necessary to prevent an
+adversary who sees the state from predicting past outputs.
+On the other hand, there are applications in the wild that use
+.Nm
+to generate key material, such as OpenSSH, so for the sake of
+.Nx
+users it would be imprudent to weaken the security model.
+On the third hand, relying on the security model of
+.Nm
+in
+.Nx
+may lead you to an unpleasant surprise on another operating system
+whose implementation of
+.Nm
+has a weaker security model.
 .Pp
-The original version of this random number generator used the
-RC4 (also known as ARC4) algorithm.
-In
-.Ox 5.5
-it was replaced with the ChaCha20 cipher, and it may be replaced
-again in the future as cryptographic techniques advance.
-A good mnemonic is
-.Dq A Replacement Call for Random .
+One may be tempted to create new APIs to accommodate different
+security models and performance constraints without unpleasant
+surprises on different operating systems.
+This should not be done lightly, though, because there are already too
+many different choices, and too many opportunities for programmers to
+reach for one and pick the wrong one.
diff --git a/man/bitstring.3bsd b/man/bitstring.3bsd
index 6006af0..feb919d 100644
--- a/man/bitstring.3bsd
+++ b/man/bitstring.3bsd
@@ -31,7 +31,7 @@
 .\" $FreeBSD$
 .\"
 .Dd July 19, 1993
-.Dt BITSTRING 3bsd
+.Dt bitstring 3bsd
 .Os
 .Sh NAME
 .Nm bit_alloc ,
diff --git a/man/byteorder.3bsd b/man/byteorder.3bsd
index 5e5d6d3..635a6a1 100644
--- a/man/byteorder.3bsd
+++ b/man/byteorder.3bsd
@@ -25,7 +25,7 @@
 .\" $FreeBSD$
 .\"
 .Dd April 29, 2002
-.Dt BYTEORDER 3bsd
+.Dt byteorder 3bsd
 .Os
 .Sh NAME
 .Nm be16enc , be16dec , be32enc , be32dec , be64enc , be64dec ,
diff --git a/man/closefrom.3bsd b/man/closefrom.3bsd
index b2d3853..eff6c64 100644
--- a/man/closefrom.3bsd
+++ b/man/closefrom.3bsd
@@ -26,7 +26,7 @@
 .\" $FreeBSD$
 .\"
 .Dd June 12, 2009
-.Dt CLOSEFROM 3bsd
+.Dt closefrom 3bsd
 .Os
 .Sh NAME
 .Nm closefrom
@@ -51,8 +51,17 @@ from the per-process object reference table.
 Any errors encountered while closing file descriptors are ignored.
 .Sh SEE ALSO
 .Xr close 2
+.Sh STANDARDS
+.Fn closefrom
+is a
+.Bx
+and Solaris extension.
 .Sh HISTORY
 The
 .Fn closefrom
-function first appeared in
-.Fx 8.0 .
+function first appeared in Solaris 9,
+.Ox 3.5 ,
+.Dx 1.2 ,
+.Nx 3.0 ,
+.Fx 8.0
+and glibc 2.34.
diff --git a/man/errc.3bsd b/man/errc.3bsd
index b330b6b..964a528 100644
--- a/man/errc.3bsd
+++ b/man/errc.3bsd
@@ -28,7 +28,7 @@
 .\" SUCH DAMAGE.
 .\"
 .Dd $Mdocdate: April 23 2014 $
-.Dt ERRC 3bsd
+.Dt errc 3bsd
 .Os
 .Sh NAME
 .Nm errc ,
diff --git a/man/expand_number.3bsd b/man/expand_number.3bsd
index e0ba501..c8050f4 100644
--- a/man/expand_number.3bsd
+++ b/man/expand_number.3bsd
@@ -26,7 +26,7 @@
 .\" $FreeBSD$
 .\"
 .Dd August 15, 2010
-.Dt EXPAND_NUMBER 3bsd
+.Dt expand_number 3bsd
 .Os
 .Sh NAME
 .Nm expand_number
diff --git a/man/explicit_bzero.3bsd b/man/explicit_bzero.3bsd
index ca077b3..5b55dc9 100644
--- a/man/explicit_bzero.3bsd
+++ b/man/explicit_bzero.3bsd
@@ -30,7 +30,7 @@
 .\"	$OpenBSD: bzero.3,v 1.10 2014/01/22 21:06:45 tedu Exp $
 .\"
 .Dd $Mdocdate: January 22 2014 $
-.Dt EXPLICIT_BZERO 3bsd
+.Dt explicit_bzero 3bsd
 .Os
 .Sh NAME
 .Nm explicit_bzero
diff --git a/man/fgetln.3bsd b/man/fgetln.3bsd
index c9f7059..685e05f 100644
--- a/man/fgetln.3bsd
+++ b/man/fgetln.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/stdio/fgetln.3,v 1.8 2004/07/16 06:07:12 tjr Exp $
 .\"
 .Dd April 19, 1994
-.Dt FGETLN 3bsd
+.Dt fgetln 3bsd
 .Os
 .Sh NAME
 .Nm fgetln
diff --git a/man/fgetwln.3bsd b/man/fgetwln.3bsd
index 21b8695..0f1f3cb 100644
--- a/man/fgetwln.3bsd
+++ b/man/fgetwln.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD$
 .\"
 .Dd July 16, 2004
-.Dt FGETWLN 3bsd
+.Dt fgetwln 3bsd
 .Os
 .Sh NAME
 .Nm fgetwln
diff --git a/man/flopen.3bsd b/man/flopen.3bsd
index 6ee153c..e0816ba 100644
--- a/man/flopen.3bsd
+++ b/man/flopen.3bsd
@@ -26,7 +26,7 @@
 .\" $FreeBSD$
 .\"
 .Dd July 28, 2017
-.Dt FLOPEN 3bsd
+.Dt flopen 3bsd
 .Os
 .Sh NAME
 .Nm flopen ,
diff --git a/man/fmtcheck.3bsd b/man/fmtcheck.3bsd
index 93e7673..9fb769f 100644
--- a/man/fmtcheck.3bsd
+++ b/man/fmtcheck.3bsd
@@ -27,7 +27,7 @@
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
 .Dd June 14, 2014
-.Dt FMTCHECK 3bsd
+.Dt fmtcheck 3bsd
 .Os
 .Sh NAME
 .Nm fmtcheck
diff --git a/man/fparseln.3bsd b/man/fparseln.3bsd
index 0f9ebc8..b227f0d 100644
--- a/man/fparseln.3bsd
+++ b/man/fparseln.3bsd
@@ -23,7 +23,7 @@
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
 .Dd November 30, 2002
-.Dt FPARSELN 3bsd
+.Dt fparseln 3bsd
 .Os
 .Sh NAME
 .Nm fparseln
@@ -150,4 +150,6 @@ if it runs out of memory.
 The
 .Fn fparseln
 function first appeared in
-.Nx 1.4 .
+.Nx 1.4
+and
+.Fx 4.0 .
diff --git a/man/fpurge.3bsd b/man/fpurge.3bsd
index eb3fc95..1a7dc58 100644
--- a/man/fpurge.3bsd
+++ b/man/fpurge.3bsd
@@ -32,7 +32,7 @@
 .\" SUCH DAMAGE.
 .\"
 .Dd $Mdocdate: November 4 2015 $
-.Dt FPURGE 3bsd
+.Dt fpurge 3bsd
 .Os
 .Sh NAME
 .Nm fpurge
diff --git a/man/funopen.3bsd b/man/funopen.3bsd
index b322525..234922f 100644
--- a/man/funopen.3bsd
+++ b/man/funopen.3bsd
@@ -31,7 +31,7 @@
 .\" $FreeBSD$
 .\"
 .Dd March 19, 2004
-.Dt FUNOPEN 3bsd
+.Dt funopen 3bsd
 .Os
 .Sh NAME
 .Nm funopen ,
@@ -168,9 +168,12 @@ functions first appeared in
 .Sh BUGS
 The
 .Fn funopen
-function
-may not be portable to systems other than
-.Bx .
+function may not be portable to systems other than
+.Bx ,
+glibc- and musl-based ones
+(as the libbsd implementation is only provided when the system has
+.Fn fopencookie
+available).
 .Pp
 On
 .Fx ,
diff --git a/man/getbsize.3bsd b/man/getbsize.3bsd
index d091db3..b371a7f 100644
--- a/man/getbsize.3bsd
+++ b/man/getbsize.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD$
 .\"
 .Dd November 16, 2012
-.Dt GETBSIZE 3bsd
+.Dt getbsize 3bsd
 .Os
 .Sh NAME
 .Nm getbsize
diff --git a/man/getpeereid.3bsd b/man/getpeereid.3bsd
index cce3909..4cc7cc6 100644
--- a/man/getpeereid.3bsd
+++ b/man/getpeereid.3bsd
@@ -26,7 +26,7 @@
 .\" $FreeBSD$
 .\"
 .Dd July 15, 2001
-.Dt GETPEEREID 3bsd
+.Dt getpeereid 3bsd
 .Os
 .Sh NAME
 .Nm getpeereid
@@ -139,4 +139,7 @@ or the kernel returned invalid data.
 The
 .Fn getpeereid
 function appeared in
-.Fx 4.6 .
+.Fx 4.6 ,
+.Nx 5.0
+and
+.Ox 3.0 .
diff --git a/man/getprogname.3bsd b/man/getprogname.3bsd
index d2a3e34..e933f34 100644
--- a/man/getprogname.3bsd
+++ b/man/getprogname.3bsd
@@ -1,6 +1,4 @@
-.\"
-.\" Copyright (c) 2001 Christopher G. Demetriou
-.\" All rights reserved.
+.\" Copyright (c) 2022 Guillem Jover <guillem@hadrons.org>
 .\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
@@ -10,34 +8,27 @@
 .\" 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. All advertising materials mentioning features or use of this software
-.\"    must display the following acknowledgement:
-.\"          This product includes software developed for the
-.\"          NetBSD Project.  See http://www.netbsd.org/ for
-.\"          information about NetBSD.
-.\" 4. The name of the author may not be used to endorse or promote products
+.\" 3. The name of the author may not be used to endorse or promote products
 .\"    derived from this software without specific prior written permission.
+.\" .
+.\" THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
 .\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-.\"
-.\" $FreeBSD$
-.\"
-.Dd May 1, 2001
-.Dt GETPROGNAME 3bsd
+.Dd August 03, 2022
+.Dt getprogname 3bsd
 .Os
 .Sh NAME
 .Nm getprogname ,
 .Nm setprogname
-.Nd get or set the program name
+.Nd get and set program name
 .Sh LIBRARY
 .ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
 .ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@@ -54,46 +45,39 @@ for include usage.)
 .Sh DESCRIPTION
 The
 .Fn getprogname
-and
-.Fn setprogname
-functions manipulate the name of the current program.
-They are used by error-reporting routines to produce
-consistent output.
-.Pp
-The
-.Fn getprogname
-function returns the name of the program.
-If the name has not been set yet, it will return
-.Dv NULL .
+returns a string with the current program name,
+excluding any directory component.
+The function will return
+.Dv NULL
+if it was unable to get the program name from any known source.
 .Pp
 The
 .Fn setprogname
-function sets the name of the program to be the last component of the
-.Fa progname
-argument.
-Since a pointer to the given string is kept as the program name,
-it should not be modified for the rest of the program's lifetime.
+function sets the current program name,
+stripping any directory component prefix.
+The function will keep a reference to the passed string pointer,
+so it must not be freed or modified while these functions might
+be called.
 .Pp
-In
-.Fx ,
-the name of the program is set by the start-up code that is run before
-.Fn main ;
-thus,
-running
-.Fn setprogname
-is not necessary.
-Programs that desire maximum portability should still call it;
-on another operating system,
-these functions may be implemented in a portability library.
-Calling
-.Fn setprogname
-allows the aforementioned library to learn the program name without
-modifications to the start-up code.
+The implementations on most BSDs will try to initialize the program name
+at program startup time, and
+.Nm libbsd
+will try to infer it from various known sources depending on the
+target system.
+But it is not a portable assumption that the program name will be set
+without calling
+.Fn setprogname ,
+so portable programs should always call it after starting up.
 .Sh SEE ALSO
-.Xr err 3 ,
-.Xr setproctitle 3bsd
+.Xr setproctitle 3bsd .
 .Sh HISTORY
-These functions first appeared in
+The
+.Fn setprogname
+and
+.Fn getprogname
+functions first appeared in
 .Nx 1.6 ,
-and made their way into
-.Fx 4.4 .
+.Fx 4.4 ,
+.Dx 2.1
+and
+.Ox 5.4 .
diff --git a/man/heapsort.3bsd b/man/heapsort.3bsd
index 106de56..58c9092 100644
--- a/man/heapsort.3bsd
+++ b/man/heapsort.3bsd
@@ -33,7 +33,7 @@
 .\" $FreeBSD$
 .\"
 .Dd September 30, 2003
-.Dt HEAPSORT 3bsd
+.Dt heapsort 3bsd
 .Os
 .Sh NAME
 .Nm heapsort , mergesort
diff --git a/man/humanize_number.3bsd b/man/humanize_number.3bsd
index ff1db2b..02ce9af 100644
--- a/man/humanize_number.3bsd
+++ b/man/humanize_number.3bsd
@@ -28,7 +28,7 @@
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
 .Dd February 9, 2008
-.Dt HUMANIZE_NUMBER 3bsd
+.Dt humanize_number 3bsd
 .Os
 .Sh NAME
 .Nm dehumanize_number ,
@@ -166,7 +166,9 @@ represented a number that does not fit in
 .Sh HISTORY
 .Fn humanize_number
 first appeared in
-.Nx 2.0 .
+.Nx 2.0
+and
+.Fx 5.3 .
 .Pp
 .Fn dehumanize_number
 first appeared in
diff --git a/man/libbsd.7 b/man/libbsd.7
index cad9e7c..b3c848e 100644
--- a/man/libbsd.7
+++ b/man/libbsd.7
@@ -1,6 +1,6 @@
 .\" libbsd man page
 .\"
-.\" Copyright © 2017-2021 Guillem Jover <guillem@hadrons.org>
+.\" Copyright © 2017-2024 Guillem Jover <guillem@hadrons.org>
 .\"
 .\" Redistribution and use in source and binary forms, with or without
 .\" modification, are permitted provided that the following conditions
@@ -24,8 +24,8 @@
 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd Feb 13, 2021
-.Dt LIBBSD 7
+.Dd January 8, 2024
+.Dt libbsd 7
 .Os
 .Sh NAME
 .Nm libbsd
@@ -42,7 +42,7 @@ The library can be used in an overlay mode, which is the preferred way, so
 that the code is portable and requires no modification to the original BSD
 code.
 This can be done easily with the
-.Xr pkg-config 1
+.Xr pkgconf 1
 library named
 .Pa libbsd-overlay .
 Or by adding the system-specific include directory with the
@@ -59,12 +59,12 @@ The includes in this case should be the usual system ones, such as
 .In unistd.h .
 .Pp
 The other way to use the library is to use the namespaced headers,
-this is less portable as it makes using
+which is a discouraged way, being less portable as it makes using
 .Nm libbsd
 mandatory and it will not work on BSD-based systems, and requires
 modifying original BSD code.
 This can be done with the
-.Xr pkg-config 1
+.Xr pkgconf 1
 library named
 .Pa libbsd .
 The includes in this case should be namespaced with
@@ -76,10 +76,10 @@ The package also provides a
 .Nm libbsd-ctor
 static library that can be used to inject automatic constructors into a
 program so that the
-.Fn setproctitle_init 3
+.Xr setproctitle_init 3bsd
 function gets invoked automatically at startup time.
 This can be done with the
-.Xr pkg-config 1
+.Xr pkgconf 1
 library named
 .Pa libbsd-ctor .
 .Sh HEADERS
@@ -128,20 +128,21 @@ It only works in non-overlay mode.
 .Bl -tag -width 4m -compact
 .It In bsd/bsd.h
 .El
-.Sh ALTERNATIVES
+.Sh VARIANTS
 Some functions have different prototypes depending on the BSD where they
 originated from, and these various implementations provided are selectable
 at build-time.
 .Pp
 This is the list of functions that provide multiple implementations:
-.Bl -tag -width 4m
-.It Fn strnvis 3
-.It Fn strnunvis 3
+.Pp
+.Bl -tag -width 4m -compact
+.It Xr strnvis 3bsd
+.It Xr strnunvis 3bsd
 .Nx
 added
-.Fn strnvis 3
+.Xr strnvis 3bsd
 and
-.Fn strnunvis 3
+.Xr strnunvis 3bsd
 but unfortunately made it incompatible with the existing one in
 .Ox
 and Freedesktop's libbsd (the former having existed for over ten years).
@@ -149,18 +150,19 @@ Despite this incompatibility being reported during development (see
 http://gnats.netbsd.org/44977) they still shipped it.
 Even more unfortunately
 .Fx
-and later MacOS picked up this incompatible implementation.
+and later macOS picked up this incompatible implementation.
 .Pp
 Provide both implementations and default for now to the historical one to
 avoid breakage, but we will switch to the
 .Nx
 one in a later release, which is internally consistent with the other
-.Xr vis 3
+.Xr vis 3bsd
 functions and is now more widespread.
 Define
 .Dv LIBBSD_NETBSD_VIS
 to switch to the
-.Nx one now.
+.Nx
+one now.
 Define
 .Dv LIBBSD_OPENBSD_VIS
 to keep using the
@@ -176,30 +178,30 @@ or non-buggy way; or because there are better more portable replacements now.
 .Pp
 This is the list of currently deprecated macros and functions:
 .Bl -tag -width 4m
-.It Fn fgetln 3
+.It Xr fgetln 3bsd
 Unportable, requires assistance from the stdio layer.
 An implementation has to choose between leaking buffers or being reentrant
 for a limited amount of streams (this implementation chose the latter with
 a limit of 32).
 Use
-.Fn getline 3
+.Xr getline 3
 instead, which is available in many systems and required by
 .St -p1003.1-2008 .
-.It Fn fgetwln 3
+.It Xr fgetwln 3bsd
 Unportable, requires assistance from the stdio layer.
 An implementation has to choose between leaking buffers or being reentrant
 for a limited amount of streams (this implementation chose the latter with
 a limit of 32).
 Use
-.Fn fgetwc 3
+.Xr fgetwc 3
 instead, which is available in many systems and required by
 .St -isoC-99
 and
 .St -p1003.1-2001 .
-.It Fn funopen 3
+.It Xr funopen 3bsd
 Unportable, requires assistance from the stdio layer or some hook framework.
-On GNU systems the
-.Fn fopencookie 3
+On glibc- and musl-based systems the
+.Xr fopencookie 3
 function can be used.
 Otherwise the code needs to be prepared for neither of these functions being
 available.
@@ -213,27 +215,53 @@ are present in all major
 for example.
 .Pp
 .Bl -tag -width 4m -compact
-.It Fn MD5Init 3
-.It Fn MD5Update 3
-.It Fn MD5Pad 3
-.It Fn MD5Final 3
-.It Fn MD5Transform 3
-.It Fn MD5End 3
-.It Fn MD5File 3
-.It Fn MD5FileChunk 3
-.It Fn MD5Data 3
+.It Xr MD5Init 3
+.It Xr MD5Update 3
+.It Xr MD5Pad 3
+.It Xr MD5Final 3
+.It Xr MD5Transform 3
+.It Xr MD5End 3
+.It Xr MD5File 3
+.It Xr MD5FileChunk 3
+.It Xr MD5Data 3
 The set of MD5 digest functions are now proxies for the implementations
 provided by the
 .Nm libmd
 companion library, so it is advised to switch to use that directly instead.
-.It Fn explicit_bzero 3
+.It Xr explicit_bzero 3bsd
 This function is provided by
 .Nm glibc
-2.25.
-.It Fn reallocarray 3
+2.25, and
+.Nm musl
+1.1.20.
+.It Xr reallocarray 3bsd
 This function is provided by
 .Nm glibc
-2.26.
+2.26, and
+.Nm musl
+1.2.2.
+.It Xr closefrom 3bsd
+This function is provided by
+.Nm glibc
+2.34.
+.It Xr arc4random 3bsd
+.It Xr arc4random_buf 3bsd
+.It Xr arc4random_uniform 3bsd
+These functions are provided by
+.Nm glibc
+2.36.
+Note that it does not provide the
+.Xr arc4random_stir 3bsd
+and
+.Xr arc4random_addrandom 3bsd
+functions.
+.It Xr strlcpy 3bsd
+.It Xr strlcat 3bsd
+These functions are provided by
+.Nm glibc
+2.38, and
+.Nm musl
+0.5.0.
 .El
 .Sh SEE ALSO
 .Xr arc4random 3bsd ,
diff --git a/man/nlist.3bsd b/man/nlist.3bsd
index beb32a3..fb11d50 100644
--- a/man/nlist.3bsd
+++ b/man/nlist.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/nlist.3,v 1.7 2001/10/01 16:08:51 ru Exp $
 .\"
 .Dd April 19, 1994
-.Dt NLIST 3bsd
+.Dt nlist 3bsd
 .Os
 .Sh NAME
 .Nm nlist
diff --git a/man/pidfile.3bsd b/man/pidfile.3bsd
index 489419b..547c556 100644
--- a/man/pidfile.3bsd
+++ b/man/pidfile.3bsd
@@ -25,7 +25,7 @@
 .\" $FreeBSD$
 .\"
 .Dd February 8, 2012
-.Dt PIDFILE 3bsd
+.Dt pidfile 3bsd
 .Os
 .Sh NAME
 .Nm pidfile_open ,
@@ -292,6 +292,20 @@ Probably called not from the process which used
 .Xr open 2 ,
 .Xr daemon 3 ,
 .Xr flopen 3bsd
+.Sh HISTORY
+The functions
+.Fn pidfile_open ,
+.Fn pidfile_write ,
+.Fn pidfile_close
+and
+.Fn pidfile_remove
+first appeared in
+.Fx 5.5 .
+.Pp
+The function
+.Fn pidfile_fileno
+first appeared in
+.Fx 10.0 .
 .Sh AUTHORS
 .An -nosplit
 The
diff --git a/man/pwcache.3bsd b/man/pwcache.3bsd
index dac9bcd..06c54e1 100644
--- a/man/pwcache.3bsd
+++ b/man/pwcache.3bsd
@@ -28,7 +28,7 @@
 .\" SUCH DAMAGE.
 .\"
 .Dd $Mdocdate: September 13 2018 $
-.Dt USER_FROM_UID 3
+.Dt user_from_uid 3
 .Os
 .Sh NAME
 .Nm user_from_uid ,
@@ -137,7 +137,7 @@ The
 .Fn uid_from_user
 and
 .Fn gid_from_group
-functions were ported from
-.Nx
-and first appeared in
+functions first appeared in
+.Nx 1.4
+and
 .Ox 6.4 .
diff --git a/man/queue.3bsd b/man/queue.3bsd
index 46a0503..5128a2c 100644
--- a/man/queue.3bsd
+++ b/man/queue.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD$
 .\"
 .Dd September 8, 2016
-.Dt QUEUE 3bsd
+.Dt queue 3bsd
 .Os
 .Sh NAME
 .Nm SLIST_CLASS_ENTRY ,
diff --git a/man/radixsort.3bsd b/man/radixsort.3bsd
index 306a311..ff0c826 100644
--- a/man/radixsort.3bsd
+++ b/man/radixsort.3bsd
@@ -30,7 +30,7 @@
 .\"     from: @(#)radixsort.3	8.2 (Berkeley) 1/27/94
 .\"
 .Dd January 27, 1994
-.Dt RADIXSORT 3bsd
+.Dt radixsort 3bsd
 .Os
 .Sh NAME
 .Nm radixsort ,
diff --git a/man/readpassphrase.3bsd b/man/readpassphrase.3bsd
index 9887eb9..3c9275d 100644
--- a/man/readpassphrase.3bsd
+++ b/man/readpassphrase.3bsd
@@ -19,7 +19,7 @@
 .\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
 .\"
 .Dd $Mdocdate: March 6 2014 $
-.Dt READPASSPHRASE 3bsd
+.Dt readpassphrase 3bsd
 .Os
 .Sh NAME
 .Nm readpassphrase
@@ -165,10 +165,12 @@ will reprint the prompt and the user may then enter a passphrase.
 The
 .Fn readpassphrase
 function is an
-.Ox
+.Bx
 extension and should not be used if portability is desired.
 .Sh HISTORY
 The
 .Fn readpassphrase
 function first appeared in
-.Ox 2.9 .
+.Ox 2.9
+and
+.Fx 4.6 .
diff --git a/man/reallocarray.3bsd b/man/reallocarray.3bsd
index b4e5cbe..a63de81 100644
--- a/man/reallocarray.3bsd
+++ b/man/reallocarray.3bsd
@@ -33,7 +33,7 @@
 .\"	$OpenBSD: malloc.3,v 1.126 2019/09/14 13:16:50 otto Exp $
 .\"
 .Dd $Mdocdate: September 14 2019 $
-.Dt REALLOCARRAY 3bsd
+.Dt reallocarray 3bsd
 .Os
 .Sh NAME
 .Nm reallocarray ,
@@ -283,12 +283,18 @@ The
 .Fn reallocarray
 function appeared in
 .Ox 5.6 ,
+.Dx 5.5
 and glibc 2.26.
+.Pp
 The
 .Fn recallocarray
 function appeared in
-.Ox 6.1 .
+.Ox 6.1
+and
+.Dx 5.5 .
 The
 .Fn freezero
 function appeared in
-.Ox 6.2 .
+.Ox 6.2
+and
+.Dx 5.5 .
diff --git a/man/reallocf.3bsd b/man/reallocf.3bsd
index 5d8b66d..8509b5e 100644
--- a/man/reallocf.3bsd
+++ b/man/reallocf.3bsd
@@ -33,7 +33,7 @@
 .\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.80.2.2.2.1 2010/06/14 02:09:06 kensmith Exp $
 .\"
 .Dd September 26, 2009
-.Dt REALLOCF 3bsd
+.Dt reallocf 3bsd
 .Os
 .Sh NAME
 .Nm reallocf
diff --git a/man/setmode.3bsd b/man/setmode.3bsd
index 0710f2a..6177a0f 100644
--- a/man/setmode.3bsd
+++ b/man/setmode.3bsd
@@ -31,7 +31,7 @@
 .\" $FreeBSD: src/lib/libc/gen/setmode.3,v 1.12 2007/01/09 00:27:55 imp Exp $
 .\"
 .Dd January 4, 2009
-.Dt SETMODE 3bsd
+.Dt setmode 3bsd
 .Os
 .Sh NAME
 .Nm getmode ,
diff --git a/man/setproctitle.3bsd b/man/setproctitle.3bsd
index ad1f44f..c8aeb4b 100644
--- a/man/setproctitle.3bsd
+++ b/man/setproctitle.3bsd
@@ -20,8 +20,8 @@
 .\" $FreeBSD$
 .\"
 .\" The following requests are required for all man pages.
-.Dd December 16, 1995
-.Dt SETPROCTITLE 3bsd
+.Dd December 7, 2022
+.Dt setproctitle 3bsd
 .Os
 .Sh NAME
 .Nm setproctitle
@@ -31,7 +31,6 @@
 .ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
 .Lb libbsd
 .Sh SYNOPSIS
-.In sys/types.h
 .In unistd.h
 (See
 .Xr libbsd 7
@@ -54,7 +53,7 @@ library routine only needs to be called (before any call to
 and with
 .Fn main
 arguments), if the automatic constructor support has not
-been linked in through the libbsd-ctor pkg-config file.
+been linked in through the libbsd-ctor pkgconf file.
 .Pp
 The title is set from the executable's name, followed by the
 result of a
@@ -113,6 +112,8 @@ The
 .Fn setproctitle
 function
 first appeared in
+.Nx 1.0
+and
 .Fx 2.2 .
 Other operating systems have
 similar functions.
diff --git a/man/stringlist.3bsd b/man/stringlist.3bsd
index d5c0756..ec06b1a 100644
--- a/man/stringlist.3bsd
+++ b/man/stringlist.3bsd
@@ -27,7 +27,7 @@
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
 .Dd May 6, 2010
-.Dt STRINGLIST 3bsd
+.Dt stringlist 3bsd
 .Os
 .Sh NAME
 .Nm stringlist ,
diff --git a/man/strlcpy.3bsd b/man/strlcpy.3bsd
index fd5d71d..c3eee4f 100644
--- a/man/strlcpy.3bsd
+++ b/man/strlcpy.3bsd
@@ -14,8 +14,8 @@
 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 .\"
-.Dd $Mdocdate: May 31 2007 $
-.Dt STRLCPY 3bsd
+.Dd $Mdocdate: January 7 2024 $
+.Dt strlcpy 3bsd
 .Os
 .Sh NAME
 .Nm strlcpy ,
@@ -192,4 +192,7 @@ and
 functions first appeared in
 .Ox 2.4 ,
 and made their appearance in
-.Fx 3.3 .
+.Nx 1.4.3 ,
+.Fx 3.3
+and
+glibc 2.38.
diff --git a/man/strmode.3bsd b/man/strmode.3bsd
index 224e099..7f613a2 100644
--- a/man/strmode.3bsd
+++ b/man/strmode.3bsd
@@ -29,7 +29,7 @@
 .\" $FreeBSD: src/lib/libc/string/strmode.3,v 1.9 2003/07/01 15:28:05 maxim Exp $
 .\"
 .Dd July 28, 1994
-.Dt STRMODE 3bsd
+.Dt strmode 3bsd
 .Os
 .Sh NAME
 .Nm strmode
diff --git a/man/strnstr.3bsd b/man/strnstr.3bsd
index e7a116d..95e729e 100644
--- a/man/strnstr.3bsd
+++ b/man/strnstr.3bsd
@@ -34,7 +34,7 @@
 .\" $FreeBSD$
 .\"
 .Dd October 11, 2001
-.Dt STRSTR 3bsd
+.Dt strstr 3bsd
 .Os
 .Sh NAME
 .Nm strnstr
@@ -112,3 +112,8 @@ ptr = strnstr(largestring, smallstring, 4);
 .Xr strspn 3 ,
 .Xr strtok 3 ,
 .Xr wcsstr 3
+.Sh HISTORY
+The
+.Fn strnstr
+function originated in
+.Fx .
diff --git a/man/strtoi.3bsd b/man/strtoi.3bsd
index 80aab7b..5988361 100644
--- a/man/strtoi.3bsd
+++ b/man/strtoi.3bsd
@@ -1,4 +1,4 @@
-.\"	$NetBSD: strtoi.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
+.\"	$NetBSD: strtoi.3,v 1.10 2024/02/10 18:43:51 andvar Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -36,12 +36,12 @@
 .\" Created by Kamil Rytarowski, based on ID:
 .\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp
 .\"
-.Dd November 13, 2015
-.Dt STRTOI 3bsd
+.Dd January 20, 2024
+.Dt strtoi 3bsd
 .Os
 .Sh NAME
 .Nm strtoi
-.Nd convert string value to an intmax_t integer
+.Nd convert a string value to an intmax_t integer
 .Sh LIBRARY
 .ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
 .ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@@ -63,8 +63,7 @@ for include usage.)
 .Sh DESCRIPTION
 The
 .Fn strtoi
-function
-converts the string in
+function converts the string in
 .Fa nptr
 to an
 .Ft intmax_t
@@ -77,7 +76,7 @@ and ensures that the result is always in the range [
 .Fa lo ..
 .Fa hi
 ].
-In adddition it always places
+In addition it always places
 .Dv 0
 on success or a conversion status in the
 .Fa rstatus
@@ -122,10 +121,11 @@ is taken as 10 (decimal) unless the next character is
 .Ql 0 ,
 in which case it is taken as 8 (octal).
 .Pp
-The remainder of the string is converted to a
+The remainder of the string is converted to an
 .Em intmax_t
 value in the obvious manner,
-stopping at the first character which is not a valid digit
+stopping at the end of the string
+or at the first character which is not a valid digit
 in the given base.
 (In bases above 10, the letter
 .Ql A
@@ -206,6 +206,12 @@ or the range given was invalid, i.e.
 >
 .Fa hi .
 .El
+.Pp
+The range check is more important than the unconverted characters check,
+and it is performed first.
+If a program needs to know if there were unconverted characters when an
+out of range number has been provided, it needs to supply and test
+.Fa endptr.
 .Sh SEE ALSO
 .Xr atof 3 ,
 .Xr atoi 3 ,
@@ -233,7 +239,7 @@ function first appeared in
 .Nx 7.0 .
 .Ox
 introduced the
-.Fn strtonum 3bsd
+.Xr strtonum 3bsd
 function for the same purpose, but the interface makes it impossible to
 properly differentiate illegal returns.
 .Sh BUGS
diff --git a/man/strtonum.3bsd b/man/strtonum.3bsd
index d216d00..a4ed2bb 100644
--- a/man/strtonum.3bsd
+++ b/man/strtonum.3bsd
@@ -16,7 +16,7 @@
 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 .\"
 .Dd January 18, 2015
-.Dt STRTONUM 3bsd
+.Dt strtonum 3bsd
 .Os
 .Sh NAME
 .Nm strtonum
@@ -141,8 +141,8 @@ The string did not consist solely of digit characters.
 .Xr strtoull 3
 .Sh STANDARDS
 .Fn strtonum
-is an
-.Ox
+is a
+.Bx
 extension.
 .Sh HISTORY
 .ds doc-operating-system-NetBSD-8.0 8.0
@@ -154,9 +154,9 @@ function first appeared in
 was redesigned in
 .Nx 8.0
 as
-.Fn strtoi 3bsd
+.Xr strtoi 3bsd
 and
-.Fn strtou 3bsd .
+.Xr strtou 3bsd .
 .Sh CAVEATS
 The
 .Fn strtonum
@@ -186,6 +186,6 @@ To overcome the shortcomings of
 .Fn strtonum
 .Nx
 provides
-.Fn strtou 3bsd
+.Xr strtou 3bsd
 and
-.Fn strtoi 3bsd .
+.Xr strtoi 3bsd .
diff --git a/man/strtou.3bsd b/man/strtou.3bsd
index 09cc458..0291568 100644
--- a/man/strtou.3bsd
+++ b/man/strtou.3bsd
@@ -1,4 +1,4 @@
-.\"	$NetBSD: strtou.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
+.\"	$NetBSD: strtou.3,v 1.8 2024/01/20 16:13:39 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -36,12 +36,12 @@
 .\" Created by Kamil Rytarowski, based on ID:
 .\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp
 .\"
-.Dd November 13, 2015
-.Dt STRTOU 3bsd
+.Dd January 20, 2024
+.Dt strtou 3bsd
 .Os
 .Sh NAME
 .Nm strtou
-.Nd convert a string to an uintmax_t integer
+.Nd convert a string value to an uintmax_t integer
 .Sh LIBRARY
 .ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
 .ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
@@ -76,7 +76,7 @@ and ensures that the result is always in the range [
 .Fa lo ..
 .Fa hi
 ].
-In adddition it always places
+In addition it always places
 .Dv 0
 on success or a conversion status in the
 .Fa rstatus
@@ -125,7 +125,7 @@ The remainder of the string is converted to an
 .Em uintmax_t
 value in the obvious manner,
 stopping at the end of the string
-or at the first character that does not produce a valid digit
+or at the first character which is not a valid digit
 in the given base.
 (In bases above 10, the letter
 .Ql A
@@ -200,12 +200,18 @@ In this case,
 .Fa endptr
 points to the first unconverted character.
 .It Bq Er ERANGE
-The given string was out of range; the value converted has been clamped; or
-the range given was invalid, i.e.
+The given string was out of range; the value converted has been clamped;
+or the range given was invalid, i.e.
 .Fa lo
 >
 .Fa hi .
 .El
+.Pp
+The range check is more important than the unconverted characters check,
+and it is performed first.
+If a program needs to know if there were unconverted characters when an
+out of range number has been provided, it needs to supply and test
+.Fa endptr.
 .Sh SEE ALSO
 .Xr atof 3 ,
 .Xr atoi 3 ,
@@ -220,20 +226,20 @@ the range given was invalid, i.e.
 .Xr strtoull 3 ,
 .Xr strtoumax 3
 .Sh STANDARDS
-.ds doc-operating-system-NetBSD-7.0 7.0
 The
 .Fn strtou
 function is a
 .Nx
 extension.
 .Sh HISTORY
+.ds doc-operating-system-NetBSD-7.0 7.0
 The
 .Fn strtou
 function first appeared in
 .Nx 7.0 .
 .Ox
 introduced the
-.Fn strtonum 3bsd
+.Xr strtonum 3bsd
 function for the same purpose, but the interface makes it impossible to
 properly differentiate illegal returns.
 .Sh BUGS
diff --git a/man/timeradd.3bsd b/man/timeradd.3bsd
index e9e78e9..2dd43c5 100644
--- a/man/timeradd.3bsd
+++ b/man/timeradd.3bsd
@@ -31,7 +31,7 @@
 .\" $FreeBSD: src/share/man/man3/timeradd.3,v 1.3 2003/09/08 19:57:19 ru Exp $
 .\"
 .Dd June 7, 2010
-.Dt TIMERADD 3bsd
+.Dt timeradd 3bsd
 .Os
 .Sh NAME
 .Nm timeradd ,
diff --git a/man/timespec.3bsd b/man/timespec.3bsd
deleted file mode 100644
index e47176e..0000000
--- a/man/timespec.3bsd
+++ /dev/null
@@ -1 +0,0 @@
-.so man3/timeval.3bsd
diff --git a/man/timeval.3bsd b/man/timeval.3bsd
deleted file mode 100644
index 31431a3..0000000
--- a/man/timeval.3bsd
+++ /dev/null
@@ -1,134 +0,0 @@
-.\" $NetBSD: timeval.3,v 1.12 2011/04/12 08:39:26 jruoho Exp $
-.\"
-.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
-.\" All rights reserved.
-.\"
-.\" This code is derived from software contributed to The NetBSD Foundation
-.\" by Jukka Ruohonen.
-.\"
-.\" 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.
-.\"
-.\" 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.
-.\"
-.Dd April 12, 2011
-.Dt TIMEVAL 3bsd
-.Os
-.Sh NAME
-.Nm timeval ,
-.Nm timespec
-.Nd time structures
-.Sh LIBRARY
-.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
-.ds doc-str-Lb-libbsd \*[str-Lb-libbsd]
-.Lb libbsd
-.Sh SYNOPSIS
-.In sys/time.h
-(See
-.Xr libbsd 7
-for include usage.)
-.Ft void
-.Fn TIMEVAL_TO_TIMESPEC "struct timeval *tv" "struct timespec *ts"
-.Ft void
-.Fn TIMESPEC_TO_TIMEVAL "struct timeval *tv" "struct timespec *ts"
-.Sh DESCRIPTION
-The
-.In sys/time.h
-header, included by
-.In time.h ,
-defines various structures related to time and timers.
-.Bl -enum -offset 1n
-.It
-The following structure is used by
-.Xr gettimeofday 2 ,
-among others:
-.Bd -literal -offset indent
-struct timeval {
-	time_t		tv_sec;
-	suseconds_t	tv_usec;
-};
-.Ed
-.Pp
-The
-.Va tv_sec
-member represents the elapsed time, in whole seconds.
-The
-.Va tv_usec
-member captures rest of the elapsed time,
-represented as the number of microseconds.
-.It
-The following structure is used by
-.Xr nanosleep 2 ,
-among others:
-.Bd -literal -offset indent
-struct timespec {
-	time_t		tv_sec;
-	long		tv_nsec;
-};
-.Ed
-.Pp
-The
-.Va tv_sec
-member is again the elapsed time in whole seconds.
-The
-.Va tv_nsec
-member represents the rest of the elapsed time in nanoseconds.
-.Pp
-A microsecond is equal to one millionth of a second,
-1000 nanoseconds, or 1/1000 milliseconds.
-To ease the conversions, the macros
-.Fn TIMEVAL_TO_TIMESPEC
-and
-.Fn TIMESPEC_TO_TIMEVAL
-can be used to convert between
-.Em struct timeval
-and
-.Em struct timespec .
-.El
-.Sh EXAMPLES
-It can be stressed that the traditional
-.Tn UNIX
-.Va timeval
-and
-.Va timespec
-structures represent elapsed time, measured by the system clock.
-The following sketch implements a function suitable
-for use in a context where the
-.Va timespec
-structure is required for a conditional timeout:
-.Bd -literal -offset indent
-static void
-example(struct timespec *spec, time_t minutes)
-{
-	struct timeval elapsed;
-
-	(void)gettimeofday(&elapsed, NULL);
-
-	_DIAGASSERT(spec != NULL);
-	TIMEVAL_TO_TIMESPEC(&elapsed, spec);
-
-	/* Add the offset for timeout in minutes. */
-	spec->tv_sec = spec->tv_sec + minutes * 60;
-}
-.Ed
-.Pp
-A better alternative would use the more precise
-.Xr clock_gettime 2 .
-.Sh SEE ALSO
-.Xr timeradd 3bsd
diff --git a/man/tree.3bsd b/man/tree.3bsd
index 6800c74..4c9dfb6 100644
--- a/man/tree.3bsd
+++ b/man/tree.3bsd
@@ -1,37 +1,30 @@
-.\"	$OpenBSD: tree.3,v 1.7 2002/06/12 01:09:20 provos Exp $
-.\"
-.\" Copyright 2002 Niels Provos <provos@citi.umich.edu>
-.\" 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. All advertising materials mentioning features or use of this software
-.\"    must display the following acknowledgement:
-.\"      This product includes software developed by Niels Provos.
-.\" 4. The name of the author may not be used to endorse or promote products
-.\"    derived from this software without specific prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-.\"
-.\" $FreeBSD$
-.\"
-.Dd December 27, 2007
-.Dt TREE 3bsd
+.\"	$OpenBSD: tree.3,v 1.30 2019/05/10 13:13:14 florian Exp $
+.\"/*
+.\" * Copyright 2002 Niels Provos <provos@citi.umich.edu>
+.\" * 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.
+.\" *
+.\" * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+.\" */
+.Dd May 10, 2019
+.Dt tree 3bsd
 .Os
 .Sh NAME
 .Nm SPLAY_PROTOTYPE ,
@@ -70,7 +63,9 @@
 .Nm RB_RIGHT ,
 .Nm RB_PARENT ,
 .Nm RB_FOREACH ,
+.Nm RB_FOREACH_SAFE ,
 .Nm RB_FOREACH_REVERSE ,
+.Nm RB_FOREACH_REVERSE_SAFE ,
 .Nm RB_INIT ,
 .Nm RB_INSERT ,
 .Nm RB_REMOVE
@@ -84,88 +79,92 @@
 (See
 .Xr libbsd 7
 for include usage.)
-.Fn SPLAY_PROTOTYPE NAME TYPE FIELD CMP
-.Fn SPLAY_GENERATE NAME TYPE FIELD CMP
-.Fn SPLAY_ENTRY TYPE
-.Fn SPLAY_HEAD HEADNAME TYPE
+.Pp
+.Fn SPLAY_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
+.Fn SPLAY_GENERATE "NAME" "TYPE" "FIELD" "CMP"
+.Fn SPLAY_ENTRY "TYPE"
+.Fn SPLAY_HEAD "HEADNAME" "TYPE"
 .Ft "struct TYPE *"
 .Fn SPLAY_INITIALIZER "SPLAY_HEAD *head"
 .Fn SPLAY_ROOT "SPLAY_HEAD *head"
-.Ft bool
+.Ft "int"
 .Fn SPLAY_EMPTY "SPLAY_HEAD *head"
 .Ft "struct TYPE *"
-.Fn SPLAY_NEXT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
+.Fn SPLAY_NEXT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn SPLAY_MIN NAME "SPLAY_HEAD *head"
+.Fn SPLAY_MIN "NAME" "SPLAY_HEAD *head"
 .Ft "struct TYPE *"
-.Fn SPLAY_MAX NAME "SPLAY_HEAD *head"
+.Fn SPLAY_MAX "NAME" "SPLAY_HEAD *head"
 .Ft "struct TYPE *"
-.Fn SPLAY_FIND NAME "SPLAY_HEAD *head" "struct TYPE *elm"
+.Fn SPLAY_FIND "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
 .Fn SPLAY_LEFT "struct TYPE *elm" "SPLAY_ENTRY NAME"
 .Ft "struct TYPE *"
 .Fn SPLAY_RIGHT "struct TYPE *elm" "SPLAY_ENTRY NAME"
-.Fn SPLAY_FOREACH VARNAME NAME "SPLAY_HEAD *head"
+.Fn SPLAY_FOREACH "VARNAME" "NAME" "SPLAY_HEAD *head"
 .Ft void
 .Fn SPLAY_INIT "SPLAY_HEAD *head"
 .Ft "struct TYPE *"
-.Fn SPLAY_INSERT NAME "SPLAY_HEAD *head" "struct TYPE *elm"
+.Fn SPLAY_INSERT "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn SPLAY_REMOVE NAME "SPLAY_HEAD *head" "struct TYPE *elm"
-.Fn RB_PROTOTYPE NAME TYPE FIELD CMP
-.Fn RB_PROTOTYPE_STATIC NAME TYPE FIELD CMP
-.Fn RB_GENERATE NAME TYPE FIELD CMP
-.Fn RB_GENERATE_STATIC NAME TYPE FIELD CMP
-.Fn RB_ENTRY TYPE
-.Fn RB_HEAD HEADNAME TYPE
+.Fn SPLAY_REMOVE "NAME" "SPLAY_HEAD *head" "struct TYPE *elm"
+.Pp
+.Fn RB_PROTOTYPE "NAME" "TYPE" "FIELD" "CMP"
+.Fn RB_PROTOTYPE_STATIC "NAME" "TYPE" "FIELD" "CMP"
+.Fn RB_GENERATE "NAME" "TYPE" "FIELD" "CMP"
+.Fn RB_GENERATE_STATIC "NAME" "TYPE" "FIELD" "CMP"
+.Fn RB_ENTRY "TYPE"
+.Fn RB_HEAD "HEADNAME" "TYPE"
 .Fn RB_INITIALIZER "RB_HEAD *head"
 .Ft "struct TYPE *"
 .Fn RB_ROOT "RB_HEAD *head"
-.Ft "bool"
+.Ft "int"
 .Fn RB_EMPTY "RB_HEAD *head"
 .Ft "struct TYPE *"
-.Fn RB_NEXT NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_NEXT "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn RB_PREV NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_PREV "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn RB_MIN NAME "RB_HEAD *head"
+.Fn RB_MIN "NAME" "RB_HEAD *head"
 .Ft "struct TYPE *"
-.Fn RB_MAX NAME "RB_HEAD *head"
+.Fn RB_MAX "NAME" "RB_HEAD *head"
 .Ft "struct TYPE *"
-.Fn RB_FIND NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_FIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn RB_NFIND NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_NFIND "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
 .Fn RB_LEFT "struct TYPE *elm" "RB_ENTRY NAME"
 .Ft "struct TYPE *"
 .Fn RB_RIGHT "struct TYPE *elm" "RB_ENTRY NAME"
 .Ft "struct TYPE *"
 .Fn RB_PARENT "struct TYPE *elm" "RB_ENTRY NAME"
-.Fn RB_FOREACH VARNAME NAME "RB_HEAD *head"
-.Fn RB_FOREACH_REVERSE VARNAME NAME "RB_HEAD *head"
+.Fn RB_FOREACH "VARNAME" "NAME" "RB_HEAD *head"
+.Fn RB_FOREACH_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
+.Fn RB_FOREACH_REVERSE "VARNAME" "NAME" "RB_HEAD *head"
+.Fn RB_FOREACH_REVERSE_SAFE "VARNAME" "NAME" "RB_HEAD *head" "TEMP_VARNAME"
 .Ft void
 .Fn RB_INIT "RB_HEAD *head"
 .Ft "struct TYPE *"
-.Fn RB_INSERT NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_INSERT "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Ft "struct TYPE *"
-.Fn RB_REMOVE NAME "RB_HEAD *head" "struct TYPE *elm"
+.Fn RB_REMOVE "NAME" "RB_HEAD *head" "struct TYPE *elm"
 .Sh DESCRIPTION
 These macros define data structures for different types of trees:
 splay trees and red-black trees.
 .Pp
 In the macro definitions,
 .Fa TYPE
-is the name tag of a user defined structure that must contain a field of type
-.Vt SPLAY_ENTRY ,
+is the name tag of a user defined structure that must contain a field named
+.Fa FIELD ,
+of type
+.Li SPLAY_ENTRY
 or
-.Vt RB_ENTRY ,
-named
-.Fa ENTRYNAME .
+.Li RB_ENTRY .
 The argument
 .Fa HEADNAME
 is the name tag of a user defined structure that must be declared
 using the macros
-.Fn SPLAY_HEAD ,
+.Fn SPLAY_HEAD
 or
 .Fn RB_HEAD .
 The argument
@@ -173,46 +172,38 @@ The argument
 has to be a unique name prefix for every tree that is defined.
 .Pp
 The function prototypes are declared with
-.Fn SPLAY_PROTOTYPE ,
-.Fn RB_PROTOTYPE ,
+.Li SPLAY_PROTOTYPE ,
+.Li RB_PROTOTYPE ,
 or
-.Fn RB_PROTOTYPE_STATIC .
+.Li RB_PROTOTYPE_STATIC .
 The function bodies are generated with
-.Fn SPLAY_GENERATE ,
-.Fn RB_GENERATE ,
+.Li SPLAY_GENERATE ,
+.Li RB_GENERATE ,
 or
-.Fn RB_GENERATE_STATIC .
+.Li RB_GENERATE_STATIC .
 See the examples below for further explanation of how these macros are used.
 .Sh SPLAY TREES
 A splay tree is a self-organizing data structure.
 Every operation on the tree causes a splay to happen.
-The splay moves the requested
-node to the root of the tree and partly rebalances it.
+The splay moves the requested node to the root of the tree and partly
+rebalances it.
 .Pp
 This has the benefit that request locality causes faster lookups as
 the requested nodes move to the top of the tree.
 On the other hand, every lookup causes memory writes.
 .Pp
-The Balance Theorem bounds the total access time for
-.Ar m
-operations and
-.Ar n
-inserts on an initially empty tree as
-.Fn O "\*[lp]m + n\*[rp]lg n" .
-The
-amortized cost for a sequence of
-.Ar m
-accesses to a splay tree is
-.Fn O "lg n" .
+The Balance Theorem bounds the total access time for m operations
+and n inserts on an initially empty tree as O((m + n)lg n).
+The amortized cost for a sequence of m accesses to a splay tree is O(lg n).
 .Pp
 A splay tree is headed by a structure defined by the
 .Fn SPLAY_HEAD
 macro.
 A
+.Fa SPLAY_HEAD
 structure is declared as follows:
-.Bd -ragged -offset indent
-.Fn SPLAY_HEAD HEADNAME TYPE
-.Va head ;
+.Bd -literal -offset indent
+SPLAY_HEAD(HEADNAME, TYPE) head;
 .Ed
 .Pp
 where
@@ -251,16 +242,15 @@ macro, but should be used only once.
 Finally,
 the
 .Fa CMP
-argument is the name of a function used to compare tree nodes
+argument is the name of a function used to compare trees' nodes
 with each other.
 The function takes two arguments of type
-.Vt "struct TYPE *" .
+.Fa "struct TYPE *" .
 If the first argument is smaller than the second, the function returns a
 value smaller than zero.
 If they are equal, the function returns zero.
 Otherwise, it should return a value greater than zero.
-The compare
-function defines the order of the tree elements.
+The compare function defines the order of the tree elements.
 .Pp
 The
 .Fn SPLAY_INIT
@@ -270,11 +260,8 @@ macro initializes the tree referenced by
 The splay tree can also be initialized statically by using the
 .Fn SPLAY_INITIALIZER
 macro like this:
-.Bd -ragged -offset indent
-.Fn SPLAY_HEAD HEADNAME TYPE
-.Va head
-=
-.Fn SPLAY_INITIALIZER &head ;
+.Bd -literal -offset indent
+SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);
 .Ed
 .Pp
 The
@@ -282,6 +269,11 @@ The
 macro inserts the new element
 .Fa elm
 into the tree.
+Upon success,
+.Va NULL
+is returned.
+If a matching element already exists in the tree, the insertion is
+aborted, and a pointer to the existing element is returned.
 .Pp
 The
 .Fn SPLAY_REMOVE
@@ -289,6 +281,11 @@ macro removes the element
 .Fa elm
 from the tree pointed by
 .Fa head .
+Upon success, a pointer to the removed element is returned.
+.Va NULL
+is returned if
+.Fa elm
+is not present in the tree.
 .Pp
 The
 .Fn SPLAY_FIND
@@ -296,7 +293,7 @@ macro can be used to find a particular element in the tree.
 .Bd -literal -offset indent
 struct TYPE find, *res;
 find.key = 30;
-res = SPLAY_FIND(NAME, head, &find);
+res = SPLAY_FIND(NAME, &head, &find);
 .Ed
 .Pp
 The
@@ -313,8 +310,8 @@ for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))
 Or, for simplicity, one can use the
 .Fn SPLAY_FOREACH
 macro:
-.Bd -ragged -offset indent
-.Fn SPLAY_FOREACH np NAME head
+.Bd -literal -offset indent
+SPLAY_FOREACH(np, NAME, &head)
 .Ed
 .Pp
 The
@@ -324,29 +321,28 @@ macro should be used to check whether a splay tree is empty.
 A red-black tree is a binary search tree with the node color as an
 extra attribute.
 It fulfills a set of conditions:
-.Bl -enum -offset indent
+.Pp
+.Bl -enum -compact -offset indent
 .It
-Every search path from the root to a leaf consists of the same number of
-black nodes.
+every search path from the root to a leaf consists of the same number of
+black nodes,
 .It
-Each red node (except for the root) has a black parent.
+each red node (except for the root) has a black parent,
 .It
-Each leaf node is black.
+each leaf node is black.
 .El
 .Pp
-Every operation on a red-black tree is bounded as
-.Fn O "lg n" .
-The maximum height of a red-black tree is
-.Fn 2lg "n + 1" .
+Every operation on a red-black tree is bounded as O(lg n).
+The maximum height of a red-black tree is 2lg (n+1).
 .Pp
 A red-black tree is headed by a structure defined by the
 .Fn RB_HEAD
 macro.
 A
+.Fa RB_HEAD
 structure is declared as follows:
-.Bd -ragged -offset indent
-.Fn RB_HEAD HEADNAME TYPE
-.Va head ;
+.Bd -literal -offset indent
+RB_HEAD(HEADNAME, TYPE) head;
 .Ed
 .Pp
 where
@@ -364,7 +360,7 @@ their prototypes need to be declared with the
 .Fn RB_PROTOTYPE
 or
 .Fn RB_PROTOTYPE_STATIC
-macro,
+macros,
 where
 .Fa NAME
 is a unique identifier for this particular tree.
@@ -381,7 +377,7 @@ The function bodies are generated with the
 .Fn RB_GENERATE
 or
 .Fn RB_GENERATE_STATIC
-macro.
+macros.
 These macros take the same arguments as the
 .Fn RB_PROTOTYPE
 and
@@ -391,16 +387,15 @@ macros, but should be used only once.
 Finally,
 the
 .Fa CMP
-argument is the name of a function used to compare tree nodes
+argument is the name of a function used to compare trees' nodes
 with each other.
 The function takes two arguments of type
-.Vt "struct TYPE *" .
+.Fa "struct TYPE *" .
 If the first argument is smaller than the second, the function returns a
 value smaller than zero.
 If they are equal, the function returns zero.
 Otherwise, it should return a value greater than zero.
-The compare
-function defines the order of the tree elements.
+The compare function defines the order of the tree elements.
 .Pp
 The
 .Fn RB_INIT
@@ -410,11 +405,8 @@ macro initializes the tree referenced by
 The red-black tree can also be initialized statically by using the
 .Fn RB_INITIALIZER
 macro like this:
-.Bd -ragged -offset indent
-.Fn RB_HEAD HEADNAME TYPE
-.Va head
-=
-.Fn RB_INITIALIZER &head ;
+.Bd -literal -offset indent
+RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);
 .Ed
 .Pp
 The
@@ -422,6 +414,11 @@ The
 macro inserts the new element
 .Fa elm
 into the tree.
+Upon success,
+.Va NULL
+is returned.
+If a matching element already exists in the tree, the insertion is
+aborted, and a pointer to the existing element is returned.
 .Pp
 The
 .Fn RB_REMOVE
@@ -429,16 +426,24 @@ macro removes the element
 .Fa elm
 from the tree pointed by
 .Fa head .
+.Fn RB_REMOVE
+returns
+.Fa elm .
 .Pp
 The
 .Fn RB_FIND
 and
 .Fn RB_NFIND
 macros can be used to find a particular element in the tree.
+.Fn RB_FIND
+finds the node with the same key as
+.Fa elm .
+.Fn RB_NFIND
+finds the first node greater than or equal to the search key.
 .Bd -literal -offset indent
 struct TYPE find, *res;
 find.key = 30;
-res = RB_FIND(NAME, head, &find);
+res = RB_FIND(NAME, &head, &find);
 .Ed
 .Pp
 The
@@ -449,26 +454,122 @@ The
 and
 .Fn RB_PREV
 macros can be used to traverse the tree:
-.Pp
-.Dl "for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))"
+.Bd -literal -offset indent
+for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))
+.Ed
 .Pp
 Or, for simplicity, one can use the
 .Fn RB_FOREACH
 or
 .Fn RB_FOREACH_REVERSE
-macro:
-.Bd -ragged -offset indent
-.Fn RB_FOREACH np NAME head
+macros:
+.Bd -literal -offset indent
+RB_FOREACH(np, NAME, &head)
 .Ed
 .Pp
+The macros
+.Fn RB_FOREACH_SAFE
+and
+.Fn RB_FOREACH_REVERSE_SAFE
+traverse the tree referenced by head
+in a forward or reverse direction respectively,
+assigning each element in turn to np.
+However, unlike their unsafe counterparts,
+they permit both the removal of np
+as well as freeing it from within the loop safely
+without interfering with the traversal.
+.Pp
 The
 .Fn RB_EMPTY
 macro should be used to check whether a red-black tree is empty.
+.Sh EXAMPLES
+The following example demonstrates how to declare a red-black tree
+holding integers.
+Values are inserted into it and the contents of the tree are printed
+in order.
+Lastly, the internal structure of the tree is printed.
+.Bd -literal -offset 3n
+#include <sys/tree.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+struct node {
+	RB_ENTRY(node) entry;
+	int i;
+};
+
+int	intcmp(struct node *, struct node *);
+void	print_tree(struct node *);
+
+int
+intcmp(struct node *e1, struct node *e2)
+{
+	return (e1->i < e2->i ? -1 : e1->i > e2->i);
+}
+
+RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
+RB_PROTOTYPE(inttree, node, entry, intcmp)
+RB_GENERATE(inttree, node, entry, intcmp)
+
+int testdata[] = {
+	20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
+	7, 11, 14
+};
+
+void
+print_tree(struct node *n)
+{
+	struct node *left, *right;
+
+	if (n == NULL) {
+		printf("nil");
+		return;
+	}
+	left = RB_LEFT(n, entry);
+	right = RB_RIGHT(n, entry);
+	if (left == NULL && right == NULL)
+		printf("%d", n->i);
+	else {
+		printf("%d(", n->i);
+		print_tree(left);
+		printf(",");
+		print_tree(right);
+		printf(")");
+	}
+}
+
+int
+main(void)
+{
+	int i;
+	struct node *n;
+
+	for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
+		if ((n = malloc(sizeof(struct node))) == NULL)
+			err(1, NULL);
+		n->i = testdata[i];
+		RB_INSERT(inttree, &head, n);
+	}
+
+	RB_FOREACH(n, inttree, &head) {
+		printf("%d\en", n->i);
+	}
+	print_tree(RB_ROOT(&head));
+	printf("\en");
+	return (0);
+}
+.Ed
+.Sh SEE ALSO
+.Xr queue 3bsd
+.Sh HISTORY
+The tree macros first appeared in
+.Fx 4.6 .
 .Sh NOTES
 Trying to free a tree in the following way is a common error:
 .Bd -literal -offset indent
-SPLAY_FOREACH(var, NAME, head) {
-	SPLAY_REMOVE(NAME, head, var);
+SPLAY_FOREACH(var, NAME, &head) {
+	SPLAY_REMOVE(NAME, &head, var);
 	free(var);
 }
 free(head);
@@ -476,36 +577,17 @@ free(head);
 .Pp
 Since
 .Va var
-is freed, the
+is free'd, the
 .Fn FOREACH
 macro refers to a pointer that may have been reallocated already.
 Proper code needs a second variable.
 .Bd -literal -offset indent
-for (var = SPLAY_MIN(NAME, head); var != NULL; var = nxt) {
-	nxt = SPLAY_NEXT(NAME, head, var);
-	SPLAY_REMOVE(NAME, head, var);
+for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) {
+	nxt = SPLAY_NEXT(NAME, &head, var);
+	SPLAY_REMOVE(NAME, &head, var);
 	free(var);
 }
 .Ed
-.Pp
-Both
-.Fn RB_INSERT
-and
-.Fn SPLAY_INSERT
-return
-.Dv NULL
-if the element was inserted in the tree successfully, otherwise they
-return a pointer to the element with the colliding key.
-.Pp
-Accordingly,
-.Fn RB_REMOVE
-and
-.Fn SPLAY_REMOVE
-return the pointer to the removed element otherwise they return
-.Dv NULL
-to indicate an error.
-.Sh SEE ALSO
-.Xr queue 3bsd
 .Sh AUTHORS
 The author of the tree macros is
 .An Niels Provos .
diff --git a/man/unvis.3bsd b/man/unvis.3bsd
index 22ed7c9..fd3f729 100644
--- a/man/unvis.3bsd
+++ b/man/unvis.3bsd
@@ -30,7 +30,7 @@
 .\"     @(#)unvis.3	8.2 (Berkeley) 12/11/93
 .\"
 .Dd March 12, 2011
-.Dt UNVIS 3bsd
+.Dt unvis 3bsd
 .Os
 .Sh NAME
 .Nm unvis ,
diff --git a/man/verrc.3bsd b/man/verrc.3bsd
new file mode 100644
index 0000000..3777fac
--- /dev/null
+++ b/man/verrc.3bsd
@@ -0,0 +1 @@
+.so man3/errc.3bsd
diff --git a/man/vis.3bsd b/man/vis.3bsd
index 7895af4..56d520a 100644
--- a/man/vis.3bsd
+++ b/man/vis.3bsd
@@ -30,7 +30,7 @@
 .\"     @(#)vis.3	8.1 (Berkeley) 6/9/93
 .\"
 .Dd April 22, 2017
-.Dt VIS 3bsd
+.Dt vis 3bsd
 .Os
 .Sh NAME
 .Nm vis ,
diff --git a/man/vwarnc.3bsd b/man/vwarnc.3bsd
new file mode 100644
index 0000000..3777fac
--- /dev/null
+++ b/man/vwarnc.3bsd
@@ -0,0 +1 @@
+.so man3/errc.3bsd
diff --git a/man/warnc.3bsd b/man/warnc.3bsd
new file mode 100644
index 0000000..3777fac
--- /dev/null
+++ b/man/warnc.3bsd
@@ -0,0 +1 @@
+.so man3/errc.3bsd
diff --git a/man/wcslcpy.3bsd b/man/wcslcpy.3bsd
index c112ff3..61e1e97 100644
--- a/man/wcslcpy.3bsd
+++ b/man/wcslcpy.3bsd
@@ -36,7 +36,7 @@
 .\" $FreeBSD$
 .\"
 .Dd March 4, 2009
-.Dt WCSLCPY 3bsd
+.Dt wcslcpy 3bsd
 .Os
 .Sh NAME
 .Nm wcslcat ,
@@ -69,3 +69,14 @@ counterpart, such as
 and
 .Fn wcslcpy ,
 which are BSD extensions.
+.Sh HISTORY
+The
+.Fn wcslcpy
+and
+.Fn wcslcat
+functions first appeared in
+.Ox 3.8 ,
+.Nx 1.6 ,
+.Fx 5.0
+and
+.Dx 2.1 .
diff --git a/src/Makefile.am b/src/Makefile.am
index 1c12ea4..00181bd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,13 +5,14 @@ AM_CPPFLAGS = \
 	-isystem $(top_srcdir)/include/bsd/ \
 	-include $(top_builddir)/config.h \
 	-DLIBBSD_OVERLAY -DLIBBSD_DISABLE_DEPRECATED \
-	-D__REENTRANT
+	-D__REENTRANT \
+	# EOL
 
 if OS_WINDOWS
 AM_CPPFLAGS += \
 	-D_CRT_SECURE_NO_WARNINGS \
 	-D_CRT_NONSTDC_NO_WARNINGS \
-	$(nil)
+	# EOL
 endif
 
 libbsd_la_included_sources = \
@@ -23,21 +24,22 @@ libbsd_la_included_sources = \
 	getentropy_osx.c \
 	getentropy_solaris.c \
 	getentropy_win.c \
-	$(nil)
+	# EOL
 
+CLEANFILES =
 EXTRA_DIST = \
-	libbsd.map \
+	libbsd.map.in \
 	libbsd.pc.in \
 	libbsd-ctor.pc.in \
 	libbsd-overlay.pc.in \
 	$(libbsd_la_included_sources) \
-	$(nil)
+	# EOL
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = \
 	libbsd.pc \
 	libbsd-overlay.pc \
-	$(nil)
+	# EOL
 
 lib_LTLIBRARIES = libbsd.la
 lib_LIBRARIES =
@@ -48,87 +50,351 @@ pkgconfig_DATA += libbsd-ctor.pc
 lib_LIBRARIES += libbsd-ctor.a
 endif
 
-libbsd_la_DEPENDENCIES = \
+EXTRA_libbsd_la_DEPENDENCIES = \
 	$(libbsd_la_included_sources) \
-	libbsd.map
+	libbsd.map \
+	libbsd.map.in \
+	# EOL
 libbsd_la_LIBADD = \
+	$(MD5_LIBS) \
 	$(LIBBSD_LIBS) \
-	$(nil)
+	# EOL
 libbsd_la_LDFLAGS = \
-	-version-number $(LIBBSD_ABI)
+	-no-undefined \
+	-version-number $(SOVERSION) \
+	# EOL
 if HAVE_LINKER_VERSION_SCRIPT
 libbsd_la_LDFLAGS += \
-	-Wl,--version-script=$(srcdir)/libbsd.map
+	-Wl,--version-script=libbsd.map \
+	# EOL
+else
+libbsd_la_LDFLAGS += \
+	-export-symbols libbsd.sym \
+	# EOL
+EXTRA_libbsd_la_DEPENDENCIES += \
+	libbsd.sym \
+	# EOL
 endif
-libbsd_la_LDFLAGS += --shared -no-undefined -Wl,-no-undefined
 libbsd_la_SOURCES = \
+	local-elf.h \
+	local-link.h \
+	# EOL
+
+if ABI_ACCMODE
+libbsd_la_SOURCES += \
+	setmode.c \
+	# EOL
+endif
+
+if ABI_ARC4RANDOM
+if !HAVE_GETENTROPY
+libbsd_la_SOURCES += \
+	getentropy.c \
+	# EOL
+endif
+libbsd_la_SOURCES += \
 	arc4random.c \
 	arc4random.h \
 	arc4random_linux.h \
-	arc4random_openbsd.h \
 	arc4random_uniform.c \
 	arc4random_unix.h \
 	arc4random_win.h \
-	bsd_getopt.c \
 	chacha_private.h \
+	# EOL
+endif
+
+if ABI_ASPRINTF
+libbsd_la_SOURCES += \
+	asprintf.c \
+	vasprintf.c \
+	# EOL
+endif
+
+if ABI_BSD_GETOPT
+libbsd_la_SOURCES += \
+	bsd_getopt.c \
+	# EOL
+endif
+
+if ABI_CLOSEFROM
+libbsd_la_SOURCES += \
 	closefrom.c \
-	dehumanize_number.c \
+	# EOL
+endif
+
+if ABI_ERR
+libbsd_la_SOURCES += \
 	err.c \
+	# EOL
+endif
+
+if ABI_ERRC
+libbsd_la_SOURCES += \
+	errc.c \
+	# EOL
+endif
+
+if ABI_EXPAND_NUMBER
+libbsd_la_SOURCES += \
 	expand_number.c \
+	# EOL
+endif
+
+if ABI_EXPLICIT_BZERO
+libbsd_la_SOURCES += \
 	explicit_bzero.c \
-	freezero.c \
+	# EOL
+endif
+
+if ABI_FGETLN
+libbsd_la_SOURCES += \
+	fgetln.c \
 	fgetwln.c \
-	fmtcheck.c \
 	fparseln.c \
+	# EOL
+endif
+
+if ABI_FLOPEN
+libbsd_la_SOURCES += \
+	flopen.c \
+	# EOL
+endif
+
+if ABI_FMTCHECK
+libbsd_la_SOURCES += \
+	fmtcheck.c \
+	# EOL
+endif
+
+if ABI_FPURGE
+libbsd_la_SOURCES += \
+	fpurge.c \
+	# EOL
+endif
+
+if ABI_FREEZERO
+libbsd_la_SOURCES += \
+	freezero.c \
+	# EOL
+endif
+
+if ABI_FUNOPEN
+libbsd_la_SOURCES += \
+	funopen.c \
+	# EOL
+endif
+
+if ABI_GETBSIZE
+libbsd_la_SOURCES += \
 	getbsize.c \
-	heapsort.c \
+	# EOL
+endif
+
+if ABI_GETPEEREID
+libbsd_la_SOURCES += \
+	getpeereid.c \
+	# EOL
+endif
+
+if ABI_HUMANIZE_NUMBER
+libbsd_la_SOURCES += \
+	dehumanize_number.c \
 	humanize_number.c \
+	# EOL
+endif
+
+if ABI_INET_NET_PTON
+libbsd_la_SOURCES += \
 	inet_net_pton.c \
-	local-elf.h \
-	local-link.h \
-	merge.c \
+	# EOL
+endif
+
+if ABI_MD5
+libbsd_la_SOURCES += \
+	md5.c \
+	# EOL
+endif
+
+if ABI_NLIST
+libbsd_la_SOURCES += \
+	nlist.c \
+	# EOL
+endif
+
+if ABI_PIDFILE
+libbsd_la_SOURCES += \
+	pidfile.c \
+	# EOL
+endif
+
+if ABI_PROCTITLE
+libbsd_la_SOURCES += \
+	setproctitle.c \
+	# EOL
+endif
+
+if ABI_PROGNAME
+libbsd_la_SOURCES += \
 	progname.c \
-	radixsort.c \
+	# EOL
+endif
+
+if ABI_PWCACHE
+libbsd_la_SOURCES += \
+	pwcache.c \
+	# EOL
+endif
+
+if ABI_READPASSPHRASE
+libbsd_la_SOURCES += \
+	readpassphrase.c \
+	# EOL
+endif
+
+if ABI_REALLOCARRAY
+libbsd_la_SOURCES += \
 	reallocarray.c \
+	# EOL
+endif
+
+if ABI_REALLOCF
+libbsd_la_SOURCES += \
 	reallocf.c \
+	# EOL
+endif
+
+if ABI_RECALLOCARRAY
+libbsd_la_SOURCES += \
 	recallocarray.c \
-	setproctitle.c \
+	# EOL
+endif
+
+if ABI_SORT
+libbsd_la_SOURCES += \
+	heapsort.c \
+	merge.c \
+	radixsort.c \
+	# EOL
+endif
+
+if ABI_STRINGLIST
+libbsd_la_SOURCES += \
+	stringlist.c \
+	# EOL
+endif
+
+if ABI_STRL
+libbsd_la_SOURCES += \
 	strlcat.c \
 	strlcpy.c \
-	stringlist.c \
+	# EOL
+endif
+
+if ABI_STRMODE
+libbsd_la_SOURCES += \
+	strmode.c \
+	# EOL
+endif
+
+if ABI_STRNSTR
+libbsd_la_SOURCES += \
 	strnstr.c \
-	strtoi.c \
+	# EOL
+endif
+
+if ABI_STRTONUM
+libbsd_la_SOURCES += \
 	strtonum.c \
+	# EOL
+endif
+
+if ABI_STRTOX
+libbsd_la_SOURCES += \
+	strtoi.c \
 	strtou.c \
+	# EOL
+endif
+
+if ABI_TIMECONV
+libbsd_la_SOURCES += \
 	timeconv.c \
+	# EOL
+endif
+
+if ABI_VIS
+libbsd_la_SOURCES += \
 	unvis.c \
 	vis.c \
+	# EOL
+endif
+
+if ABI_WCSL
+libbsd_la_SOURCES += \
 	wcslcat.c \
 	wcslcpy.c \
-	$(nil)
+	# EOL
+endif
 
-#if !HAVE_GETENTROPY
-#libbsd_la_SOURCES += \
-#	getentropy.c \
-#	$(nil)
-#endif
+if ABI_TRANSPARENT_LIBMD
+CLEANFILES += \
+	format.ld \
+	# EOL
+endif
+
+DISTCLEANFILES = \
+	libbsd.sym \
+	libbsd.map \
+	# EOL
 
 libbsd_ctor_a_SOURCES = \
 	setproctitle_ctor.c \
-	$(nil)
+	# EOL
+
+# Generate the library map file with the pre-processor to selectively include
+# symbols depending on the host system, otherwise some linkers might fail.
+libbsd.map: libbsd.map.in
+	$(AM_V_GEN) $(CPP) $(AM_CPPFLAGS) $(CPPFLAGS) -P - <$(srcdir)/libbsd.map.in >$@
+
+# Generate a simple libtool symbol export list to be used as a fallback if
+# there is no version script support.
+libbsd.sym: libbsd.map
+	$(AM_V_GEN) $(SED) -ne 's/^[[:space:]]\{1,\}\([A-Za-z0-9_]\{1,\}\);/\1/p' libbsd.map >$@
+
+if ABI_TRANSPARENT_LIBMD
+TRANSPARENT_LIBMD_DEPENDS = format.ld
+
+format.ld:
+	$(CC) -shared -nostdlib -nostartfiles -x assembler /dev/null -o $@.so
+	$(OBJDUMP) -f $@.so | sed -n 's/.*file format \(.*\)/OUTPUT_FORMAT(\1)/;T;p' >$@
+	rm -f $@.so
+endif
 
 runtimelibdir = $(libdir)
 
-install-exec-hook:
+install-exec-hook: $(TRANSPARENT_LIBMD_DEPENDS)
 	if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
 		$(MKDIR_P) $(DESTDIR)$(runtimelibdir); \
 		mv $(DESTDIR)$(libdir)/libbsd*.so.* \
 		   $(DESTDIR)$(runtimelibdir)/; \
+	fi
+if ABI_TRANSPARENT_LIBMD
+# The "GNU ld script" magic is required so that GNU ldconfig does not complain
+# about an unknown format file.
+	soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
+	$(RM) $(DESTDIR)$(libdir)/libbsd.so; \
+	(echo '/* GNU ld script'; \
+	 echo ' * The MD5 functions are provided by the libmd library. */'; \
+	 cat format.ld; \
+	 echo "GROUP($(runtimelibdir)/$$soname AS_NEEDED($(MD5_LIBS)))"; \
+	)>$(DESTDIR)$(libdir)/libbsd.so
+else
+	if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
 		soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
-		sorelprefix=`echo $(libdir) | sed -r -e 's:(^/)?[^/]+:..:g'`; \
+		sorelprefix=`echo $(libdir) | $(SED) -r -e 's:(^/)?[^/]+:..:g'`; \
 		ln -sf $$sorelprefix$(runtimelibdir)/$$soname \
 		       $(DESTDIR)$(libdir)/libbsd.so; \
 	fi
+endif
 
 uninstall-hook:
 	rm -f $(DESTDIR)$(runtimelibdir)/libbsd*.so*
diff --git a/src/arc4random.c b/src/arc4random.c
index 1a7b72f..b046a82 100644
--- a/src/arc4random.c
+++ b/src/arc4random.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: arc4random.c,v 1.53 2015/09/10 18:53:50 bcook Exp $	*/
+/*	$OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $	*/
 
 /*
  * Copyright (c) 1996, David Mazieres <dm@uun.org>
@@ -50,6 +50,8 @@
 #define BLOCKSZ	64
 #define RSBUFSZ	(16*BLOCKSZ)
 
+#define REKEY_BASE	(1024*1024) /* NB. should be a power of 2 */
+
 /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
 static struct _rs {
 	size_t		rs_have;	/* valid bytes at end of rs_buf */
@@ -74,10 +76,10 @@ _rs_init(unsigned char *buf, size_t n)
 
 	if (rs == NULL) {
 		if (_rs_allocate(&rs, &rsx) == -1)
-			abort();
+			_exit(1);
 	}
 
-	chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0);
+	chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
 	chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
 }
 
@@ -108,6 +110,7 @@ static void
 _rs_stir(void)
 {
 	unsigned char rnd[KEYSZ + IVSZ];
+	uint32_t rekey_fuzz = 0;
 
 	if (getentropy(rnd, sizeof rnd) == -1)
 		_getentropy_fail();
@@ -122,7 +125,10 @@ _rs_stir(void)
 	rs->rs_have = 0;
 	memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
 
-	rs->rs_count = 1600000;
+	/* rekey interval should not be predictable */
+	chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
+	    (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
+	rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
 }
 
 static inline void
diff --git a/src/arc4random.h b/src/arc4random.h
index 812188b..b8675b4 100644
--- a/src/arc4random.h
+++ b/src/arc4random.h
@@ -32,9 +32,7 @@
 int
 getentropy(void *buf, size_t len);
 
-#if defined(__OpenBSD__)
-#include "arc4random_openbsd.h"
-#elif defined(__linux__)
+#if defined(__linux__)
 #include "arc4random_linux.h"
 #elif defined(_WIN32)
 #include "arc4random_win.h"
diff --git a/src/arc4random_openbsd.h b/src/arc4random_openbsd.h
deleted file mode 100644
index 9cd8b68..0000000
--- a/src/arc4random_openbsd.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*	$OpenBSD: arc4random.h,v 1.3 2014/07/20 20:51:13 bcook Exp $	*/
-
-/*
- * Copyright (c) 1996, David Mazieres <dm@uun.org>
- * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
- * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
- * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/*
- * Stub functions for portability.
- */
-#include <sys/mman.h>
-
-#include <signal.h>
-
-#include "thread_private.h"
-
-static inline void
-_getentropy_fail(void)
-{
-	raise(SIGKILL);
-}
-
-static inline int
-_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
-{
-	struct {
-		struct _rs rs;
-		struct _rsx rsx;
-	} *p;
-
-	if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
-	    MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
-		return (-1);
-	if (minherit(p, sizeof(*p), MAP_INHERIT_ZERO) == -1) {
-		munmap(p, sizeof(*p));
-		return (-1);
-	}
-
-	*rsp = &p->rs;
-	*rsxp = &p->rsx;
-	return (0);
-}
-
-static inline void
-_rs_forkdetect(void)
-{
-}
diff --git a/src/asprintf.c b/src/asprintf.c
new file mode 100644
index 0000000..23576e3
--- /dev/null
+++ b/src/asprintf.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2010-2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 <config.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+int
+asprintf(char **strp, char const *fmt, ...)
+{
+	va_list args;
+	int n;
+
+	va_start(args, fmt);
+	n = vasprintf(strp, fmt, args);
+	va_end(args);
+
+	return n;
+}
diff --git a/src/chacha_private.h b/src/chacha_private.h
index 3b4ec93..ef1b931 100644
--- a/src/chacha_private.h
+++ b/src/chacha_private.h
@@ -4,7 +4,7 @@ D. J. Bernstein
 Public domain.
 */
 
-/* $OpenBSD: chacha_private.h,v 1.2 2013/10/04 07:02:27 djm Exp $ */
+/* $OpenBSD: chacha_private.h,v 1.3 2022/02/28 21:56:29 dtucker Exp $ */
 
 typedef unsigned char u8;
 typedef unsigned int u32;
@@ -52,7 +52,7 @@ static const char sigma[16] = "expand 32-byte k";
 static const char tau[16] = "expand 16-byte k";
 
 static void
-chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
+chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits)
 {
   const char *constants;
 
diff --git a/src/closefrom.c b/src/closefrom.c
index 2df5d03..41c9668 100644
--- a/src/closefrom.c
+++ b/src/closefrom.c
@@ -153,6 +153,9 @@ closefrom_procfs(int lowfd)
 		const char *errstr;
 		int fd;
 
+		if (dent->d_name[0] == '.')
+			continue;
+
 		fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
 		if (errstr != NULL || fd == dirfd(dirp))
 			continue;
diff --git a/src/err.c b/src/err.c
index 8f09972..44359e8 100644
--- a/src/err.c
+++ b/src/err.c
@@ -1,6 +1,5 @@
 /*
- * Copyright © 2006 Robert Millan
- * Copyright © 2011, 2019 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2019 Guillem Jover <guillem@hadrons.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -26,59 +25,13 @@
  */
 
 #include <err.h>
-#ifdef LIBBSD_NEED_ERR_H_FUNCS
 #include <errno.h>
-#endif
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 void
-vwarnc(int code, const char *format, va_list ap)
-{
-	fprintf(stderr, "%s: ", getprogname());
-	if (format) {
-		vfprintf(stderr, format, ap);
-		fprintf(stderr, ": ");
-	}
-	fprintf(stderr, "%s\n", strerror(code));
-}
-
-void
-warnc(int code, const char *format, ...)
-{
-	va_list ap;
-
-	va_start(ap, format);
-	vwarnc(code, format, ap);
-	va_end(ap);
-}
-
-void
-verrc(int status, int code, const char *format, va_list ap)
-{
-	fprintf(stderr, "%s: ", getprogname());
-	if (format) {
-		vfprintf(stderr, format, ap);
-		fprintf(stderr, ": ");
-	}
-	fprintf(stderr, "%s\n", strerror(code));
-	exit(status);
-}
-
-void
-errc(int status, int code, const char *format, ...)
-{
-	va_list ap;
-
-	va_start(ap, format);
-	verrc(status, code, format, ap);
-	va_end(ap);
-}
-
-#ifdef LIBBSD_NEED_ERR_H_FUNCS
-void
 vwarn(const char *format, va_list ap)
 {
 	vwarnc(errno, format, ap);
@@ -148,4 +101,3 @@ errx(int eval, const char *format, ...)
 	verrx(eval, format, ap);
 	va_end(ap);
 }
-#endif
diff --git a/src/errc.c b/src/errc.c
new file mode 100644
index 0000000..45f1b61
--- /dev/null
+++ b/src/errc.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright © 2006 Robert Millan
+ * Copyright © 2011, 2019 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 <err.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void
+vwarnc(int code, const char *format, va_list ap)
+{
+	fprintf(stderr, "%s: ", getprogname());
+	if (format) {
+		vfprintf(stderr, format, ap);
+		fprintf(stderr, ": ");
+	}
+	fprintf(stderr, "%s\n", strerror(code));
+}
+
+void
+warnc(int code, const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	vwarnc(code, format, ap);
+	va_end(ap);
+}
+
+void
+verrc(int status, int code, const char *format, va_list ap)
+{
+	fprintf(stderr, "%s: ", getprogname());
+	if (format) {
+		vfprintf(stderr, format, ap);
+		fprintf(stderr, ": ");
+	}
+	fprintf(stderr, "%s\n", strerror(code));
+	exit(status);
+}
+
+void
+errc(int status, int code, const char *format, ...)
+{
+	va_list ap;
+
+	va_start(ap, format);
+	verrc(status, code, format, ap);
+	va_end(ap);
+}
diff --git a/src/explicit_bzero.c b/src/explicit_bzero.c
index 52a7517..d223631 100644
--- a/src/explicit_bzero.c
+++ b/src/explicit_bzero.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: explicit_bzero.c,v 1.3 2014/06/21 02:34:26 matthew Exp $ */
+/*	$OpenBSD: explicit_bzero.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
 /*
  * Public domain.
  * Written by Matthew Dempsky.
@@ -7,6 +7,9 @@
 #include <string.h>
 
 __attribute__((__weak__)) void
+__explicit_bzero_hook(void *, size_t);
+
+__attribute__((__weak__)) void
 __explicit_bzero_hook(void *buf, size_t len)
 {
 }
diff --git a/src/fgetln.c b/src/fgetln.c
index 502eb6f..3b62666 100644
--- a/src/fgetln.c
+++ b/src/fgetln.c
@@ -25,10 +25,11 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <stdio.h>
 #include <sys/cdefs.h>
 #include <sys/types.h>
+
 #include <string.h>
+#include <stdio.h>
 
 #include "local-link.h"
 
@@ -76,7 +77,7 @@ fgetln(FILE *stream, size_t *len)
 	}
 }
 libbsd_link_warning(fgetln,
-                    "This function cannot be safely ported, use getline(3) "
+                    "The fgetln() function cannot be safely ported, use getline(3) "
                     "instead, as it is supported by GNU and POSIX.1-2008.");
 #else
 #error "Function fgetln() needs to be ported."
diff --git a/src/fgetwln.c b/src/fgetwln.c
index e6a0a17..0b8e7d9 100644
--- a/src/fgetwln.c
+++ b/src/fgetwln.c
@@ -87,6 +87,9 @@ fgetwln(FILE *stream, size_t *lenp)
 	*lenp = wused;
 	return wused ? fb->wbuf : NULL;
 }
+/* XXX: Ideally we'd recommend getwline(3), but unfortunately even though it
+ * was part of the ISO/IEC TR 24731-2:2010 draft, it did not make it into C11
+ * and is not widely implemented. */
 libbsd_link_warning(fgetwln,
-                    "This function cannot be safely ported, use fgetwc(3) "
+                    "The fgetwln() function cannot be safely ported, use fgetwc(3) "
                     "instead, as it is supported by C99 and POSIX.1-2001.");
diff --git a/src/flopen.c b/src/flopen.c
index ff20d07..679445f 100644
--- a/src/flopen.c
+++ b/src/flopen.c
@@ -38,6 +38,27 @@
 
 #include <libutil.h>
 
+static int
+lock_file(int fd, int flags)
+{
+	int operation;
+
+#if HAVE_FLOCK
+	operation = LOCK_EX;
+	if (flags & O_NONBLOCK)
+		operation |= LOCK_NB;
+
+	return flock(fd, operation);
+#else
+	if (flags & O_NONBLOCK)
+		operation = F_TLOCK;
+	else
+		operation = F_LOCK;
+
+	return lockf(fd, operation, 0);
+#endif
+}
+
 /*
  * Reliably open and lock a file.
  *
@@ -49,7 +70,7 @@
 static int
 vflopenat(int dirfd, const char *path, int flags, va_list ap)
 {
-	int fd, operation, serrno, trunc;
+	int fd, serrno, trunc;
 	struct stat sb, fsb;
 	mode_t mode;
 
@@ -62,10 +83,6 @@ vflopenat(int dirfd, const char *path, int flags, va_list ap)
 		mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
 	}
 
-        operation = LOCK_EX;
-        if (flags & O_NONBLOCK)
-                operation |= LOCK_NB;
-
 	trunc = (flags & O_TRUNC);
 	flags &= ~O_TRUNC;
 
@@ -73,7 +90,7 @@ vflopenat(int dirfd, const char *path, int flags, va_list ap)
 		if ((fd = openat(dirfd, path, flags, mode)) == -1)
 			/* non-existent or no access */
 			return (-1);
-		if (flock(fd, operation) == -1) {
+		if (lock_file(fd, flags) == -1) {
 			/* unsupported or interrupted */
 			serrno = errno;
 			(void)close(fd);
diff --git a/src/fpurge.c b/src/fpurge.c
index 462535a..4a20c51 100644
--- a/src/fpurge.c
+++ b/src/fpurge.c
@@ -26,13 +26,15 @@
 
 #include <errno.h>
 #include <stdio.h>
+#if HAVE_STDIO_EXT_H
 #include <stdio_ext.h>
+#endif
 
 #ifdef HAVE___FPURGE
 int
 fpurge(FILE *fp)
 {
-	if (fp == NULL || fileno(fp) < 0) {
+	if (fp == NULL) {
 		errno = EBADF;
 		return EOF;
 	}
diff --git a/src/funopen.c b/src/funopen.c
index 1e6f43a..fdcdcba 100644
--- a/src/funopen.c
+++ b/src/funopen.c
@@ -65,7 +65,7 @@ funopen_write(void *cookie, const char *buf, size_t size)
 }
 
 static int
-funopen_seek(void *cookie, off64_t *offset, int whence)
+funopen_seek(void *cookie, off_t *offset, int whence)
 {
 	struct funopen_cookie *cookiewrap = cookie;
 	off_t soff = *offset;
@@ -137,12 +137,6 @@ funopen(const void *cookie,
 
 	return fopencookie(cookiewrap, mode, funcswrap);
 }
-#elif defined(__MUSL__)
-/*
- * This is unimplementable on musl based systems, and upstream has stated
- * they will not add the needed support to implement it. Just ignore this
- * interface there, as it has never been provided anyway.
- */
 #else
-#error "Function funopen() needs to be ported or disabled."
+#error "Function funopen() needs to be ported."
 #endif
diff --git a/src/getentropy_bsd.c b/src/getentropy_bsd.c
index 705f65b..551b646 100644
--- a/src/getentropy_bsd.c
+++ b/src/getentropy_bsd.c
@@ -26,6 +26,8 @@
 #include <errno.h>
 #include <stddef.h>
 
+int	getentropy(void *buf, size_t len);
+
 /*
  * Derived from lib/libc/gen/arc4random.c from FreeBSD.
  */
diff --git a/src/getentropy_hurd.c b/src/getentropy_hurd.c
index 2f76baa..8f2ba16 100644
--- a/src/getentropy_hurd.c
+++ b/src/getentropy_hurd.c
@@ -415,17 +415,17 @@ getentropy_fallback(void *buf, size_t len)
 #ifdef HAVE_GETAUXVAL
 #ifdef AT_RANDOM
 		/* Not as random as you think but we take what we are given */
-		p = (char *) getauxval(AT_RANDOM);
+		p = (char *) ((intptr_t) getauxval(AT_RANDOM));
 		if (p)
 			HR(p, 16);
 #endif
 #ifdef AT_SYSINFO_EHDR
-		p = (char *) getauxval(AT_SYSINFO_EHDR);
+		p = (char *) ((intptr_t) getauxval(AT_SYSINFO_EHDR));
 		if (p)
 			HR(p, pgs);
 #endif
 #ifdef AT_BASE
-		p = (char *) getauxval(AT_BASE);
+		p = (char *) ((intptr_t) getauxval(AT_BASE));
 		if (p)
 			HD(p);
 #endif
diff --git a/src/libbsd-overlay.pc.in b/src/libbsd-overlay.pc.in
index 01b7ea5..7ef4e4d 100644
--- a/src/libbsd-overlay.pc.in
+++ b/src/libbsd-overlay.pc.in
@@ -8,5 +8,6 @@ Description: Utility functions from BSD systems (overlay)
 Version: @VERSION@
 URL: https://libbsd.freedesktop.org/
 Libs: -L${libdir} -lbsd
-Libs.private: @LIBBSD_LIBS@
+Libs.private: @LIBBSD_LIBS@ @MD5_LIBS@
+# We use -isystem instead of -I due the overlay via #include_next usage.
 Cflags: -isystem ${includedir}/bsd -DLIBBSD_OVERLAY
diff --git a/src/libbsd.map b/src/libbsd.map
deleted file mode 100644
index 9281b3a..0000000
--- a/src/libbsd.map
+++ /dev/null
@@ -1,197 +0,0 @@
-LIBBSD_0.0 {
-  global:
-    arc4random;
-    arc4random_stir;
-    arc4random_addrandom;
-
-    bsd_getopt;
-    optreset;
-
-    errc;
-    warnc;
-    verrc;
-    vwarnc;
-
-    fgetln;
-    fmtcheck;
-    heapsort;
-    humanize_number;
-
-    inet_net_pton; /* XXX: Already provided by glibc, remove. */
-
-    getprogname;
-    setprogname;
-
-    strlcpy;
-    strlcat;
-
-    /*setmode;*/
-    getmode;
-
-    vis;
-    strvis;
-    strvisx;
-    unvis;
-    strunvis;
-    strunvisx;
-
-    /* The following functions are provided via the system libc or libmd
-     * implementations. */
-    MD5Init;
-    MD5Update;
-    MD5Pad;
-    MD5Final;
-    MD5Transform;
-    MD5End;
-    MD5File;
-    MD5FileChunk;
-    MD5Data;
-
-  local:
-    *;
-};
-
-LIBBSD_0.1 {
-    strmode;
-
-    __fdnlist; /* Private symbol, but libkvm uses it. */
-    nlist;
-} LIBBSD_0.0;
-
-LIBBSD_0.2 {
-    strtonum;
-
-    strnvis;
-    strnunvis;
-
-    dehumanize_number;
-
-    readpassphrase;
-
-    flopen;
-
-    pidfile_open;
-    pidfile_write;
-    pidfile_close;
-    pidfile_remove;
-
-    arc4random_buf;
-    arc4random_uniform;
-} LIBBSD_0.1;
-
-LIBBSD_0.3 {
-    reallocf;
-    /*getpeereid;*/
-
-    mergesort;
-    radixsort;
-    sradixsort;
-
-    fpurge;
-} LIBBSD_0.2;
-
-LIBBSD_0.4 {
-    closefrom;
-    expand_number;
-} LIBBSD_0.3;
-
-LIBBSD_0.5 {
-    fgetwln;
-    fparseln;
-
-    /* Introduced in 0.2 as a stub, implemented in 0.5. */
-    setproctitle;
-
-    strnstr;
-
-    wcslcat;
-    wcslcpy;
-} LIBBSD_0.4;
-
-LIBBSD_0.6 {
-    /* Exported to cope with the constructor+dlopen+threads mess. */
-    setproctitle_init;
-} LIBBSD_0.5;
-
-LIBBSD_0.7 {
-    getbsize;
-
-    /* This symbol might not be present on some specific systems, such
-     * as musl based ones. It might need to be removed on SOVERSION bump,
-     * as it cannot be portabily implemented everywhere. */
-    /*funopen;*/
-
-    reallocarray;
-
-    sl_init;
-    sl_add;
-    sl_free;
-    sl_find;
-
-    _time32_to_time;
-    _time_to_time32;
-    _time64_to_time;
-    _time_to_time64;
-    _time_to_long;
-    _long_to_time;
-    _time_to_int;
-    _int_to_time;
-} LIBBSD_0.6;
-
-LIBBSD_0.8 {
-    explicit_bzero;
-} LIBBSD_0.7;
-
-LIBBSD_0.9 {
-    flopenat;
-
-    pidfile_fileno;
-
-    strtoi;
-    strtou;
-
-    nvis;
-    snvis;
-    stravis;
-    strenvisx;
-    strnunvisx;
-    strsenvisx;
-    strsnvis;
-    strsnvisx;
-    strsvis;
-    strsvisx;
-    svis;
-} LIBBSD_0.8;
-
-LIBBSD_0.9.1 {
-    /* The strnvis() and strnunvis() symbols changed prototype to match
-     * the NetBSD implementation. Provided as versioned nodes in 0.9.1, and
-     * exposed here explicitly so that we can redirect at compile-time. */
-    strnvis_netbsd;
-    strnunvis_netbsd;
-} LIBBSD_0.9;
-
-LIBBSD_0.10.0 {
-    /* These BSD extensions are available on GNU systems, but not on other
-     * systems such as Windows or musl libc based ones. */
-    vwarn;
-    vwarnx;
-    warn;
-    warnx;
-    verr;
-    verrx;
-    err;
-    errx;
-} LIBBSD_0.9.1;
-
-LIBBSD_0.11.0 {
-    strnvisx;
-
-    recallocarray;
-    freezero;
-
-    gid_from_group;
-    group_from_gid;
-    uid_from_user;
-    user_from_uid;
-} LIBBSD_0.10.0;
diff --git a/src/libbsd.map.in b/src/libbsd.map.in
new file mode 100644
index 0000000..d7047dc
--- /dev/null
+++ b/src/libbsd.map.in
@@ -0,0 +1,319 @@
+#include "config.h"
+
+LIBBSD_0.0 {
+  global:
+#if LIBBSD_ABI_ARC4RANDOM
+    arc4random;
+#endif
+#if LIBBSD_ABI_ARC4RANDOM_STIR
+    arc4random_stir;
+    arc4random_addrandom;
+#endif
+
+#if LIBBSD_ABI_BSD_GETOPT
+    bsd_getopt;
+    optreset;
+#endif
+
+#if LIBBSD_ABI_ERRC
+    errc;
+    warnc;
+    verrc;
+    vwarnc;
+#endif
+
+#if LIBBSD_ABI_FGETLN
+    fgetln;
+#endif
+
+#if LIBBSD_ABI_FMTCHECK
+    fmtcheck;
+#endif
+
+#if LIBBSD_ABI_SORT
+    heapsort;
+#endif
+
+#if LIBBSD_ABI_HUMANIZE_NUMBER
+    humanize_number;
+#endif
+
+#if LIBBSD_ABI_INET_NET_PTON
+    inet_net_pton;
+#endif
+
+#if LIBBSD_ABI_PROGNAME
+    getprogname;
+    setprogname;
+#endif
+
+#if LIBBSD_ABI_STRL
+    strlcpy;
+    strlcat;
+#endif
+
+#if LIBBSD_ABI_ACCMODE
+    setmode;
+    getmode;
+#endif
+
+#if LIBBSD_ABI_VIS
+    vis;
+    strvis;
+    strvisx;
+    unvis;
+    strunvis;
+    strunvisx;
+#endif
+
+#if LIBBSD_ABI_MD5
+    MD5Init;
+    MD5Update;
+    MD5Pad;
+    MD5Final;
+    MD5Transform;
+    MD5End;
+    MD5File;
+    MD5FileChunk;
+    MD5Data;
+#endif
+
+  local:
+    *;
+};
+
+LIBBSD_0.1 {
+#if LIBBSD_ABI_STRMODE
+    strmode;
+#endif
+
+#if LIBBSD_ABI_NLIST
+    /* Private symbol, but libkvm uses it. */
+    __fdnlist;
+    nlist;
+#endif
+} LIBBSD_0.0;
+
+LIBBSD_0.2 {
+#if LIBBSD_ABI_STRTONUM
+    strtonum;
+#endif
+
+#if LIBBSD_ABI_VIS
+    strnvis;
+    strnunvis;
+#endif
+
+#if LIBBSD_ABI_HUMANIZE_NUMBER
+    dehumanize_number;
+#endif
+
+#if LIBBSD_ABI_READPASSPHRASE
+    readpassphrase;
+#endif
+
+#if LIBBSD_ABI_FLOPEN
+    flopen;
+#endif
+
+#if LIBBSD_ABI_PIDFILE
+    pidfile_open;
+    pidfile_write;
+    pidfile_close;
+    pidfile_remove;
+#endif
+
+#if LIBBSD_ABI_ARC4RANDOM
+    arc4random_buf;
+    arc4random_uniform;
+#endif
+} LIBBSD_0.1;
+
+LIBBSD_0.3 {
+#if LIBBSD_ABI_REALLOCF
+    reallocf;
+#endif
+
+#if LIBBSD_ABI_GETPEEREID
+    getpeereid;
+#endif
+
+#if LIBBSD_ABI_SORT
+    mergesort;
+    radixsort;
+    sradixsort;
+#endif
+
+#if LIBBSD_ABI_FPURGE
+    fpurge;
+#endif
+} LIBBSD_0.2;
+
+LIBBSD_0.4 {
+#if LIBBSD_ABI_CLOSEFROM
+    closefrom;
+#endif
+
+#if LIBBSD_ABI_EXPAND_NUMBER
+    expand_number;
+#endif
+} LIBBSD_0.3;
+
+LIBBSD_0.5 {
+#if LIBBSD_ABI_FGETLN
+    fgetwln;
+    fparseln;
+#endif
+
+#if LIBBSD_ABI_PROCTITLE
+    /* Introduced in 0.2 as a stub, implemented in 0.5. */
+    setproctitle;
+#endif
+
+#if LIBBSD_ABI_STRNSTR
+    strnstr;
+#endif
+
+#if LIBBSD_ABI_WCSL
+    wcslcat;
+    wcslcpy;
+#endif
+} LIBBSD_0.4;
+
+LIBBSD_0.6 {
+#if LIBBSD_ABI_PROCTITLE
+    /* Exported to cope with the constructor+dlopen+threads mess. */
+    setproctitle_init;
+#endif
+} LIBBSD_0.5;
+
+LIBBSD_0.7 {
+#if LIBBSD_ABI_GETBSIZE
+    getbsize;
+#endif
+
+#if LIBBSD_ABI_FUNOPEN
+    funopen;
+#endif
+
+#if LIBBSD_ABI_REALLOCARRAY
+    reallocarray;
+#endif
+
+#if LIBBSD_ABI_STRINGLIST
+    sl_init;
+    sl_add;
+    sl_free;
+    sl_find;
+#endif
+
+#if LIBBSD_ABI_TIMECONV
+    _time32_to_time;
+    _time_to_time32;
+    _time64_to_time;
+    _time_to_time64;
+    _time_to_long;
+    _long_to_time;
+    _time_to_int;
+    _int_to_time;
+#endif
+} LIBBSD_0.6;
+
+LIBBSD_0.8 {
+#if LIBBSD_ABI_EXPLICIT_BZERO
+    explicit_bzero;
+#endif
+} LIBBSD_0.7;
+
+LIBBSD_0.9 {
+#if LIBBSD_ABI_FLOPEN
+    flopenat;
+#endif
+
+#if LIBBSD_ABI_PIDFILE
+    pidfile_fileno;
+#endif
+
+#if LIBBSD_ABI_STRTOX
+    strtoi;
+    strtou;
+#endif
+
+#if LIBBSD_ABI_VIS
+    nvis;
+    snvis;
+    stravis;
+    strenvisx;
+    strnunvisx;
+    strsenvisx;
+    strsnvis;
+    strsnvisx;
+    strsvis;
+    strsvisx;
+    svis;
+#endif
+} LIBBSD_0.8;
+
+LIBBSD_0.9.1 {
+#if LIBBSD_ABI_VIS
+    /* The strnvis() and strnunvis() symbols changed prototype to match
+     * the NetBSD implementation. Provided as versioned nodes in 0.9.1, and
+     * exposed here explicitly so that we can redirect at compile-time. */
+    strnvis_netbsd;
+    strnunvis_netbsd;
+#endif
+} LIBBSD_0.9;
+
+LIBBSD_0.10.0 {
+#if LIBBSD_ABI_ERR
+    vwarn;
+    vwarnx;
+    warn;
+    warnx;
+    verr;
+    verrx;
+    err;
+    errx;
+#endif
+} LIBBSD_0.9.1;
+
+LIBBSD_0.11.0 {
+#if LIBBSD_ABI_VIS
+    strnvisx;
+#endif
+
+#if LIBBSD_ABI_RECALLOCARRAY
+    recallocarray;
+#endif
+
+#if LIBBSD_ABI_FREEZERO
+    freezero;
+#endif
+
+#if LIBBSD_ABI_ID_FROM_NAME
+    gid_from_group;
+    uid_from_user;
+#endif
+#if LIBBSD_ABI_NAME_FROM_ID
+    user_from_uid;
+    group_from_gid;
+#endif
+} LIBBSD_0.10.0;
+
+LIBBSD_0.12.0 {
+#if LIBBSD_ABI_ASPRINTF
+    vasprintf;
+    asprintf;
+#endif
+
+#if LIBBSD_ABI_TIMECONV && LIBBSD_ABI_TIME64
+    _time32_to_time_time64;
+    _time_to_time32_time64;
+    _time64_to_time_time64;
+    _time_to_time64_time64;
+    _time_to_long_time64;
+    _long_to_time_time64;
+    _time_to_int_time64;
+    _int_to_time_time64;
+#endif
+} LIBBSD_0.11.0;
diff --git a/src/libbsd.pc.in b/src/libbsd.pc.in
index 2c0ac17..4bca715 100644
--- a/src/libbsd.pc.in
+++ b/src/libbsd.pc.in
@@ -8,5 +8,5 @@ Description: Utility functions from BSD systems
 Version: @VERSION@
 URL: https://libbsd.freedesktop.org/
 Libs: -L${libdir} -lbsd
-Libs.private: @LIBBSD_LIBS@
+Libs.private: @LIBBSD_LIBS@ @MD5_LIBS@
 Cflags: -I${includedir}
diff --git a/src/local-elf.h b/src/local-elf.h
index 98a2962..8c4794f 100644
--- a/src/local-elf.h
+++ b/src/local-elf.h
@@ -124,6 +124,16 @@
 #define ELF_TARG_CLASS	ELFCLASS64
 #define ELF_TARG_DATA	ELFDATA2LSB
 
+#elif defined(__loongarch__)
+
+#define ELF_TARG_MACH	EM_LOONGARCH
+#if defined(__loongarch64)
+#define ELF_TARG_CLASS	ELFCLASS64
+#else
+#error Unsupported ELF class
+#endif
+#define ELF_TARG_DATA	ELFDATA2LSB
+
 #elif defined(__m32r__)
 
 #define ELF_TARG_MACH	EM_M32R
@@ -220,7 +230,7 @@
 #if defined(__LITTLE_ENDIAN__)
 #define ELF_TARG_DATA	ELFDATA2LSB
 #elif defined(__BIG_ENDIAN__)
-#define ELF_TARG_DATA	ELFDATA2LMSB
+#define ELF_TARG_DATA	ELFDATA2MSB
 #else
 #error Unknown SH endianness
 #endif
diff --git a/src/local-link.h b/src/local-link.h
index 55fd028..dd67c70 100644
--- a/src/local-link.h
+++ b/src/local-link.h
@@ -27,21 +27,58 @@
 #ifndef LIBBSD_LOCAL_LINK_H
 #define LIBBSD_LOCAL_LINK_H
 
+#include <sys/cdefs.h>
+
+#ifdef __ELF__
 #define libbsd_link_warning(symbol, msg) \
 	static const char libbsd_emit_link_warning_##symbol[] \
 		__attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg
+#else
+#define libbsd_link_warning(symbol, msg)
+#endif
 
-#ifdef __ELF__
-#define libbsd_symver_default(alias, symbol, version) \
+#if defined(__APPLE__)
+#define libbsd_strong_alias(symbol, alias) \
+	__asm__(".globl _" #alias); \
+	__asm__(".set _" #alias ", _" #symbol); \
+	extern __typeof(symbol) alias
+#elif !defined(_MSC_VER)
+#define libbsd_strong_alias(symbol, alias) \
+	extern __typeof__(symbol) alias __attribute__((__alias__(#symbol)))
+#endif
+
+#if defined(__ELF__) && !defined(__sun)
+#  if __has_attribute(symver)
+/* The symver attribute is supported since gcc 10.x. */
+#define libbsd_symver_default(symbol, alias, version) \
+	extern __typeof__(symbol) symbol \
+		__attribute__((__symver__(#alias "@@" #version)))
+#define libbsd_symver_variant(symbol, alias, version) \
+	extern __typeof__(symbol) symbol \
+		__attribute__((__symver__(#alias "@" #version)))
+
+#define libbsd_symver_weak(symbol, alias, version) \
+	extern __typeof__(symbol) symbol \
+		__attribute__((__symver__(#alias "@" #version), __weak__))
+#  else
+#define libbsd_symver_default(symbol, alias, version) \
 	__asm__(".symver " #symbol "," #alias "@@" #version)
 
-#define libbsd_symver_variant(alias, symbol, version) \
+#define libbsd_symver_variant(symbol, alias, version) \
 	__asm__(".symver " #symbol "," #alias "@" #version)
+
+#define libbsd_symver_weak(symbol, alias, version) \
+	libbsd_symver_variant(symbol, alias, version); \
+	extern __typeof__(symbol) alias \
+		__attribute__((__weak__))
+#  endif
 #else
-#define libbsd_symver_default(alias, symbol, version) \
-	extern __typeof(symbol) alias __attribute__((__alias__(#symbol)))
+#define libbsd_symver_default(symbol, alias, version) \
+	libbsd_strong_alias(symbol, alias)
+
+#define libbsd_symver_variant(symbol, alias, version)
 
-#define libbsd_symver_variant(alias, symbol, version)
+#define libbsd_symver_weak(symbol, alias, version)
 #endif
 
 #endif
diff --git a/src/md5.c b/src/md5.c
index 00769aa..289cba4 100644
--- a/src/md5.c
+++ b/src/md5.c
@@ -24,119 +24,97 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <stddef.h>
-#include <stdlib.h>
-#include <dlfcn.h>
 #include <md5.h>
 #include "local-link.h"
 
-static void (*libmd_MD5Init)(MD5_CTX *);
-static void (*libmd_MD5Update)(MD5_CTX *, const uint8_t *, size_t);
-static void (*libmd_MD5Pad)(MD5_CTX *);
-static void (*libmd_MD5Final)(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
-static void (*libmd_MD5Transform)(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
-static char *(*libmd_MD5End)(MD5_CTX *, char *);
-static char *(*libmd_MD5File)(const char *, char *);
-static char *(*libmd_MD5FileChunk)(const char *, char *, off_t, off_t);
-static char *(*libmd_MD5Data)(const uint8_t *, size_t, char *);
-
-static void *
-libmd_loader(const char *symbol)
-{
-	void *func;
-
-	func = dlsym(RTLD_NEXT, symbol);
-	if (func == NULL) {
-		fprintf(stderr,
-		        "libbsd: cannot find wrapped symbol %s in libc or libmd\n",
-		        symbol);
-		abort();
-	}
-
-	return func;
-}
-
-#define libmd_wrapper(symbol) \
-	if (libmd_ ## symbol == NULL) \
-		libmd_ ## symbol = libmd_loader(#symbol)
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
 void
-MD5Init(MD5_CTX *context)
+libbsd_MD5Init(MD5_CTX *context)
 {
-	libmd_wrapper(MD5Init);
-	libmd_MD5Init(context);
+	MD5Init(context);
 }
 libbsd_link_warning(MD5Init,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Init() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Init, MD5Init, LIBBSD_0.0);
 
 void
-MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
+libbsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
 {
-	libmd_wrapper(MD5Update);
-	libmd_MD5Update(context, data, len);
+	MD5Update(context, data, len);
 }
 libbsd_link_warning(MD5Update,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Update() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Update, MD5Update, LIBBSD_0.0);
 
 void
-MD5Pad(MD5_CTX *context)
+libbsd_MD5Pad(MD5_CTX *context)
 {
-	libmd_wrapper(MD5Pad);
-	libmd_MD5Pad(context);
+	MD5Pad(context);
 }
 libbsd_link_warning(MD5Pad,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Pad() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Pad, MD5Pad, LIBBSD_0.0);
 
 void
-MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
+libbsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
 {
-	libmd_wrapper(MD5Final);
-	libmd_MD5Final(digest, context);
+	MD5Final(digest, context);
 }
 libbsd_link_warning(MD5Final,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Final() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Final, MD5Final, LIBBSD_0.0);
 
 void
-MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
+libbsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
 {
-	libmd_wrapper(MD5Transform);
-	libmd_MD5Transform(state, block);
+	MD5Transform(state, block);
 }
 libbsd_link_warning(MD5Transform,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Transform() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Transform, MD5Transform, LIBBSD_0.0);
 
 char *
-MD5End(MD5_CTX *context, char *buf)
+libbsd_MD5End(MD5_CTX *context, char *buf)
 {
-	libmd_wrapper(MD5End);
-	return libmd_MD5End(context, buf);
+	return MD5End(context, buf);
 }
 libbsd_link_warning(MD5End,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5End() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5End, MD5End, LIBBSD_0.0);
 
 char *
-MD5File(const char *filename, char *buf)
+libbsd_MD5File(const char *filename, char *buf)
 {
-	libmd_wrapper(MD5File);
 	return MD5File(filename, buf);
 }
 libbsd_link_warning(MD5File,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5File() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5File, MD5File, LIBBSD_0.0);
 
 char *
-MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
+libbsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
 {
-	libmd_wrapper(MD5FileChunk);
-	return libmd_MD5FileChunk(filename, buf, offset, length);
+	return MD5FileChunk(filename, buf, offset, length);
 }
 libbsd_link_warning(MD5FileChunk,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5FileChunk() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5FileChunk, MD5FileChunk, LIBBSD_0.0);
 
 char *
-MD5Data(const uint8_t *data, size_t len, char *buf)
+libbsd_MD5Data(const uint8_t *data, size_t len, char *buf)
 {
-	libmd_wrapper(MD5Data);
-	return libmd_MD5Data(data, len, buf);
+	return MD5Data(data, len, buf);
 }
 libbsd_link_warning(MD5Data,
-                    "This function is a deprecated wrapper, use libmd instead.");
+                    "The MD5Data() function in libbsd is a deprecated wrapper, "
+                    "use libmd instead.");
+libbsd_symver_weak(libbsd_MD5Data, MD5Data, LIBBSD_0.0);
diff --git a/src/merge.c b/src/merge.c
index c43866f..3f1b3fb 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -84,9 +84,8 @@ static void insertionsort(unsigned char *, size_t, size_t,
  */
 /* Assumption: PSIZE is a power of 2. */
 #define EVAL(p) (unsigned char **)					\
-	((unsigned char *)0 +						\
 	 (((unsigned char *)p + PSIZE - 1 -				\
-	   (unsigned char *)0) & ~(PSIZE - 1)))
+	   (unsigned char *)0) & ~(PSIZE - 1))
 
 /*
  * Arguments are as for qsort.
diff --git a/src/nlist.c b/src/nlist.c
index 8012d5a..1cb9d18 100644
--- a/src/nlist.c
+++ b/src/nlist.c
@@ -43,10 +43,6 @@
 
 #include "local-elf.h"
 
-#ifndef SIZE_T_MAX
-#define SIZE_T_MAX 0xffffffffU
-#endif
-
 /* Note: This function is used by libkvm0, so we need to export it.
  * It is not declared in the include files though. */
 int __fdnlist(int, struct nlist *);
@@ -153,7 +149,7 @@ __fdnlist(int fd, struct nlist *list)
 	shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
 
 	/* Make sure it's not too big to mmap */
-	if (shdr_size > SIZE_T_MAX || shdr_size > st.st_size) {
+	if (shdr_size > st.st_size) {
 		errno = EFBIG;
 		return (-1);
 	}
@@ -186,7 +182,7 @@ __fdnlist(int fd, struct nlist *list)
 	}
 
 	/* Check for files too large to mmap. */
-	if (symstrsize > SIZE_T_MAX || symstrsize > st.st_size) {
+	if (symstrsize > st.st_size) {
 		errno = EFBIG;
 		goto done;
 	}
diff --git a/src/progname.c b/src/progname.c
index bebf714..83e8652 100644
--- a/src/progname.c
+++ b/src/progname.c
@@ -35,6 +35,9 @@
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#ifdef HAVE_PROCINFO_H
+#include <procinfo.h>
+#endif
 #ifdef _WIN32
 #include <Windows.h>
 #include <shlwapi.h>
@@ -62,6 +65,14 @@ getprogname(void)
 	/* getexecname(3) returns an absolute pathname, normalize it. */
 	if (__progname == NULL)
 		setprogname(getexecname());
+#elif defined(_AIX)
+	if (__progname == NULL) {
+		struct procentry64 procs;
+		pid_t pid = getpid ();
+
+		if (getprocs64(&procs, sizeof procs, NULL, 0, &pid, 1) > 0)
+			__progname = strdup(procs.pi_comm);
+	}
 #elif defined(_WIN32)
 	if (__progname == NULL) {
 		WCHAR *wpath = NULL;
@@ -126,6 +137,8 @@ done:
 		free(wpath);
 		free(mbname);
 	}
+#else
+#error "Function getprogname() needs to be ported."
 #endif
 
 	return __progname;
diff --git a/src/pwcache.c b/src/pwcache.c
index d54daa0..8ed855c 100644
--- a/src/pwcache.c
+++ b/src/pwcache.c
@@ -83,8 +83,12 @@ typedef struct gidc {
  * cache both hits and misses.
  */
 
+#ifndef HAVE_USER_FROM_UID
 static UIDC **uidtb;	/* uid to name cache */
+#endif
+#ifndef HAVE_GROUP_FROM_GID
 static GIDC **gidtb;	/* gid to name cache */
+#endif
 static UIDC **usrtb;	/* user name to uid cache */
 static GIDC **grptb;	/* group name to gid cache */
 
@@ -103,6 +107,7 @@ st_hash(const char *name, size_t len, int tabsz)
 	return key % tabsz;
 }
 
+#ifndef HAVE_USER_FROM_UID
 /*
  * uidtb_start
  *	creates an an empty uidtb
@@ -124,7 +129,9 @@ uidtb_start(void)
 	}
 	return 0;
 }
+#endif
 
+#ifndef HAVE_GROUP_FROM_GID
 /*
  * gidtb_start
  *	creates an an empty gidtb
@@ -146,6 +153,7 @@ gidtb_start(void)
 	}
 	return 0;
 }
+#endif
 
 /*
  * usrtb_start
@@ -191,6 +199,7 @@ grptb_start(void)
 	return 0;
 }
 
+#ifndef HAVE_USER_FROM_UID
 /*
  * user_from_uid()
  *	caches the name (if any) for the uid. If noname clear, we always
@@ -251,7 +260,9 @@ user_from_uid(uid_t uid, int noname)
 	}
 	return ptr->name;
 }
+#endif
 
+#ifndef HAVE_GROUP_FROM_GID
 /*
  * group_from_gid()
  *	caches the name (if any) for the gid. If noname clear, we always
@@ -312,6 +323,7 @@ group_from_gid(gid_t gid, int noname)
 	}
 	return ptr->name;
 }
+#endif
 
 /*
  * uid_from_user()
diff --git a/src/readpassphrase.c b/src/readpassphrase.c
index f9f6195..0082c82 100644
--- a/src/readpassphrase.c
+++ b/src/readpassphrase.c
@@ -36,6 +36,15 @@
 #define TCSASOFT 0
 #endif
 
+#ifndef _NSIG
+#if defined(NSIG)
+#define _NSIG NSIG
+#else
+/* The SIGRTMAX define might be set to a function such as sysconf(). */
+#define _NSIG (SIGRTMAX + 1)
+#endif
+#endif
+
 static volatile sig_atomic_t signo[_NSIG];
 
 static void handler(int);
diff --git a/src/setmode.c b/src/setmode.c
index 48ac2dd..12b5f7e 100644
--- a/src/setmode.c
+++ b/src/setmode.c
@@ -36,6 +36,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <stddef.h>
 #include <ctype.h>
 #include <errno.h>
 #include <signal.h>
@@ -144,12 +145,13 @@ common:			if (set->cmd2 & CMD2_CLR) {
 
 #define	ADDCMD(a, b, c, d) do {						\
 	if (set >= endset) {						\
+		ptrdiff_t setdiff = set - saveset;			\
 		BITCMD *newset;						\
 		setlen += SET_LEN_INCR;					\
 		newset = reallocarray(saveset, setlen, sizeof(BITCMD));	\
 		if (newset == NULL)					\
 			goto out;					\
-		set = newset + (set - saveset);				\
+		set = newset + setdiff;					\
 		saveset = newset;					\
 		endset = newset + (setlen - 2);				\
 	}								\
diff --git a/src/setproctitle.c b/src/setproctitle.c
index ff32aa3..06928e6 100644
--- a/src/setproctitle.c
+++ b/src/setproctitle.c
@@ -33,6 +33,10 @@
 #include <string.h>
 #include "local-link.h"
 
+#if !HAVE_DECL_ENVIRON
+extern char **environ;
+#endif
+
 static struct {
 	/* Original value. */
 	const char *arg0;
@@ -222,6 +226,10 @@ setproctitle_init(int argc, char *argv[], char *envp[])
 #define SPT_MAXTITLE 255
 #endif
 
+__printflike(1, 2)
+void
+setproctitle_impl(const char *fmt, ...);
+
 void
 setproctitle_impl(const char *fmt, ...)
 {
@@ -276,24 +284,22 @@ setproctitle_impl(const char *fmt, ...)
 
 	if (nul < SPT.nul) {
 		*SPT.nul = '.';
-	} else if (nul == SPT.nul && &nul[1] < SPT.end) {
+	} else if (nul == SPT.nul && (nul + 1) < SPT.end) {
 		*SPT.nul = ' ';
 		*++nul = '\0';
 	}
 }
-libbsd_symver_default(setproctitle, setproctitle_impl, LIBBSD_0.5);
+libbsd_symver_default(setproctitle_impl, setproctitle, LIBBSD_0.5);
 
 /* The original function introduced in 0.2 was a stub, it only got implemented
  * in 0.5, make the implementation available in the old version as an alias
  * for code linking against that version, and change the default to use the
  * new version, so that new code depends on the implemented version. */
-#ifdef HAVE_TYPEOF
-extern __typeof__(setproctitle_impl)
-setproctitle_stub
-	__attribute__((__alias__("setproctitle_impl")));
+#if defined(libbsd_strong_alias)
+libbsd_strong_alias(setproctitle_impl, setproctitle_stub);
 #else
 void
 setproctitle_stub(const char *fmt, ...)
 	__attribute__((__alias__("setproctitle_impl")));
 #endif
-libbsd_symver_variant(setproctitle, setproctitle_stub, LIBBSD_0.2);
+libbsd_symver_variant(setproctitle_stub, setproctitle, LIBBSD_0.2);
diff --git a/src/setproctitle_ctor.c b/src/setproctitle_ctor.c
index 2c5b6d0..d97de50 100644
--- a/src/setproctitle_ctor.c
+++ b/src/setproctitle_ctor.c
@@ -38,7 +38,7 @@
  *
  * To avoid any other possible fallout, the constructor is split into a
  * new static library that needs to be linked explicitly into programs
- * using setproctitle(). As an additional safety measure the pkg-config
+ * using setproctitle(). As an additional safety measure the pkgconf(1)
  * linker flags will mark the program as not allowing to be dlopen()ed
  * so that we make sure to avoid the problem described above.
  */
@@ -49,4 +49,4 @@
  * move them from .ctors to .init_array.
  */
 void (*libbsd_init_func)(int argc, char *argv[], char *envp[])
-	__attribute__((__section__(".init_array"))) = setproctitle_init;
+	__attribute__((__section__(".init_array"), __used__)) = setproctitle_init;
diff --git a/src/strtoi.c b/src/strtoi.c
index 9e3771d..7a27ca7 100644
--- a/src/strtoi.c
+++ b/src/strtoi.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
+/*	$NetBSD: _strtoi.h,v 1.3 2024/01/20 16:13:39 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -32,7 +32,7 @@
  * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
  *
  * Created by Kamil Rytarowski, based on ID:
- * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joe…
+ * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joerg Exp
  */
 
 #include <sys/cdefs.h>
@@ -73,25 +73,25 @@ strtoi(const char *__restrict nptr,
 	*rstatus = errno;
 	errno = serrno;
 
-	if (*rstatus == 0) {
-		/* No digits were found */
-		if (nptr == *endptr)
-			*rstatus = ECANCELED;
-		/* There are further characters after number */
-		else if (**endptr != '\0')
-			*rstatus = ENOTSUP;
-	}
+	/* No digits were found */
+	if (*rstatus == 0 && nptr == *endptr)
+		*rstatus = ECANCELED;
 
 	if (im < lo) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return lo;
 	}
+
 	if (im > hi) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return hi;
 	}
 
+	/* There are further characters after number */
+	if (*rstatus == 0 && **endptr != '\0')
+		*rstatus = ENOTSUP;
+
 	return im;
 }
diff --git a/src/strtonum.c b/src/strtonum.c
index 2fa0fcf..f85f741 100644
--- a/src/strtonum.c
+++ b/src/strtonum.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: strtonum.c,v 1.5 2018/01/04 20:57:29 kamil Exp $	*/
+/*	$NetBSD: strtonum.c,v 1.7 2024/01/20 16:13:39 christos Exp $	*/
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -42,21 +42,34 @@ strtonum(const char *nptr, long long minval, long long maxval,
 	int e;
 	long long rv;
 	const char *resp;
+	char *eptr;
 
 	if (errstr == NULL)
 		errstr = &resp;
 
-	rv = (long long)strtoi(nptr, NULL, 10, minval, maxval, &e);
+	if (minval > maxval)
+		goto out;
 
-	if (e == 0) {
+	rv = (long long)strtoi(nptr, &eptr, 10, minval, maxval, &e);
+
+	switch (e) {
+	case 0:
 		*errstr = NULL;
 		return rv;
+	case ECANCELED:
+	case ENOTSUP:
+	case EINVAL:
+		goto out;
+	case ERANGE:
+		if (*eptr)
+			goto out;
+		*errstr = rv == maxval ? "too large" : "too small";
+		return 0;
+	default:
+		abort();
 	}
 
-	if (e == ERANGE)
-		*errstr = (rv == maxval ? "too large" : "too small");
-	else
-		*errstr = "invalid";
-
+out:
+	*errstr = "invalid";
 	return 0;
 }
diff --git a/src/strtou.c b/src/strtou.c
index 0e22a88..a87adcc 100644
--- a/src/strtou.c
+++ b/src/strtou.c
@@ -1,4 +1,4 @@
-/*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
+/*	$NetBSD: _strtoi.h,v 1.3 2024/01/20 16:13:39 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -73,25 +73,25 @@ strtou(const char *__restrict nptr,
 	*rstatus = errno;
 	errno = serrno;
 
-	if (*rstatus == 0) {
-		/* No digits were found */
-		if (nptr == *endptr)
-			*rstatus = ECANCELED;
-		/* There are further characters after number */
-		else if (**endptr != '\0')
-			*rstatus = ENOTSUP;
-	}
+	/* No digits were found */
+	if (*rstatus == 0 && nptr == *endptr)
+		*rstatus = ECANCELED;
 
 	if (im < lo) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return lo;
 	}
+
 	if (im > hi) {
 		if (*rstatus == 0)
 			*rstatus = ERANGE;
 		return hi;
 	}
 
+	/* There are further characters after number */
+	if (*rstatus == 0 && **endptr != '\0')
+		*rstatus = ENOTSUP;
+
 	return im;
 }
diff --git a/src/timeconv.c b/src/timeconv.c
index 11f926e..0c122e4 100644
--- a/src/timeconv.c
+++ b/src/timeconv.c
@@ -32,17 +32,161 @@
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
-#include <timeconv.h>
+
+#include "local-link.h"
+
+#if LIBBSD_SYS_TIME_BITS == 64
+/*
+ * - enable time64 functions
+ * - symver libbsd<func>_time64 -> <func> 0.7
+ */
+#define time32_symbol(name)
+#define time64_symbol(name) \
+	libbsd_strong_alias(libbsd ## name ## _time64, name)
+#elif LIBBSD_SYS_TIME_BITS == 32 && LIBBSD_SYS_HAS_TIME64
+/*
+ * - enable time32 functions
+ * - enable time64 functions
+ * - symver libbsd<func>_time32 -> <func> 0.7
+ * - map libbsd<func>_time64 <func>_time64 0.12
+ */
+#define time32_symbol(name) \
+	libbsd_strong_alias(libbsd ## name ## _time32, name)
+#define time64_symbol(name) \
+	libbsd_strong_alias(libbsd ## name ## _time64, name ## _time64)
+#elif LIBBSD_SYS_TIME_BITS == 32 && !LIBBSD_SYS_HAS_TIME64
+/*
+ * - enable time32 functions
+ * - symver libbsd<func>_time32 -> <func>
+ */
+#define time32_symbol(name) \
+	libbsd_strong_alias(libbsd ## name ## _time32, name)
+#define time64_symbol(name)
+#else
+#error "Unknown time_t support"
+#endif
+
+#if LIBBSD_SYS_HAS_TIME64
+typedef int64_t libbsd_time64_t;
 
 /*
  * Convert a 32 bit representation of time_t into time_t.  XXX needs to
  * implement the 50-year rule to handle post-2038 conversions.
  */
-time_t
-_time32_to_time(int32_t t32)
+libbsd_time64_t
+libbsd_time32_to_time_time64(int32_t t32);
+libbsd_time64_t
+libbsd_time32_to_time_time64(int32_t t32)
+{
+    return((libbsd_time64_t)t32);
+}
+time64_symbol(_time32_to_time);
+
+/*
+ * Convert time_t to a 32 bit representation.  If time_t is 64 bits we can
+ * simply chop it down.   The resulting 32 bit representation can be
+ * converted back to a temporally local 64 bit time_t using time32_to_time.
+ */
+int32_t
+libbsd_time_to_time32_time64(libbsd_time64_t t);
+int32_t
+libbsd_time_to_time32_time64(libbsd_time64_t t)
+{
+    return((int32_t)t);
+}
+time64_symbol(_time_to_time32);
+
+/*
+ * Convert a 64 bit representation of time_t into time_t.  If time_t is
+ * represented as 32 bits we can simply chop it and not support times
+ * past 2038.
+ */
+libbsd_time64_t
+libbsd_time64_to_time_time64(int64_t t64);
+libbsd_time64_t
+libbsd_time64_to_time_time64(int64_t t64)
+{
+    return((libbsd_time64_t)t64);
+}
+time64_symbol(_time64_to_time);
+
+/*
+ * Convert time_t to a 64 bit representation.  If time_t is represented
+ * as 32 bits we simply sign-extend and do not support times past 2038.
+ */
+int64_t
+libbsd_time_to_time64_time64(libbsd_time64_t t);
+int64_t
+libbsd_time_to_time64_time64(libbsd_time64_t t)
+{
+    return((int64_t)t);
+}
+time64_symbol(_time_to_time64);
+
+/*
+ * Convert to/from 'long'.  Depending on the sizeof(long) this may or
+ * may not require using the 50-year rule.
+ */
+long
+libbsd_time_to_long_time64(libbsd_time64_t t);
+long
+libbsd_time_to_long_time64(libbsd_time64_t t)
+{
+    if (sizeof(long) == sizeof(int64_t))
+	return(libbsd_time_to_time64_time64(t));
+    return((long)t);
+}
+time64_symbol(_time_to_long);
+
+libbsd_time64_t
+libbsd_long_to_time_time64(long tlong);
+libbsd_time64_t
+libbsd_long_to_time_time64(long tlong)
+{
+    if (sizeof(long) == sizeof(int32_t))
+	return(libbsd_time32_to_time_time64(tlong));
+    return((libbsd_time64_t)tlong);
+}
+time64_symbol(_long_to_time);
+
+/*
+ * Convert to/from 'int'.  Depending on the sizeof(int) this may or
+ * may not require using the 50-year rule.
+ */
+int
+libbsd_time_to_int_time64(time_t t);
+int
+libbsd_time_to_int_time64(time_t t)
 {
-    return((time_t)t32);
+    if (sizeof(int) == sizeof(int64_t))
+	return(libbsd_time_to_time64_time64(t));
+    return((int)t);
 }
+time64_symbol(_time_to_int);
+
+libbsd_time64_t
+libbsd_int_to_time_time64(int tint);
+libbsd_time64_t
+libbsd_int_to_time_time64(int tint)
+{
+    if (sizeof(int) == sizeof(int32_t))
+	return(libbsd_time32_to_time_time64(tint));
+    return((libbsd_time64_t)tint);
+}
+time64_symbol(_int_to_time);
+#endif
+
+#if LIBBSD_SYS_TIME_BITS == 32
+typedef int32_t libbsd_time32_t;
+
+libbsd_time32_t
+libbsd_time32_to_time_time32(int32_t t32);
+libbsd_time32_t
+libbsd_time32_to_time_time32(int32_t t32)
+{
+    return((libbsd_time32_t)t32);
+}
+time32_symbol(_time32_to_time);
 
 /*
  * Convert time_t to a 32 bit representation.  If time_t is 64 bits we can
@@ -50,68 +194,90 @@ _time32_to_time(int32_t t32)
  * converted back to a temporally local 64 bit time_t using time32_to_time.
  */
 int32_t
-_time_to_time32(time_t t)
+libbsd_time_to_time32_time32(libbsd_time32_t t);
+int32_t
+libbsd_time_to_time32_time32(libbsd_time32_t t)
 {
     return((int32_t)t);
 }
+time32_symbol(_time_to_time32);
 
 /*
  * Convert a 64 bit representation of time_t into time_t.  If time_t is
  * represented as 32 bits we can simply chop it and not support times
  * past 2038.
  */
-time_t
-_time64_to_time(int64_t t64)
+libbsd_time32_t
+libbsd_time64_to_time_time32(int64_t t64);
+libbsd_time32_t
+libbsd_time64_to_time_time32(int64_t t64)
 {
-    return((time_t)t64);
+    return((libbsd_time32_t)t64);
 }
+time32_symbol(_time64_to_time);
 
 /*
  * Convert time_t to a 64 bit representation.  If time_t is represented
  * as 32 bits we simply sign-extend and do not support times past 2038.
  */
 int64_t
-_time_to_time64(time_t t)
+libbsd_time_to_time64_time32(libbsd_time32_t t);
+int64_t
+libbsd_time_to_time64_time32(libbsd_time32_t t)
 {
     return((int64_t)t);
 }
+time32_symbol(_time_to_time64);
 
 /*
  * Convert to/from 'long'.  Depending on the sizeof(long) this may or
  * may not require using the 50-year rule.
  */
 long
-_time_to_long(time_t t)
+libbsd_time_to_long_time32(libbsd_time32_t t);
+long
+libbsd_time_to_long_time32(libbsd_time32_t t)
 {
     if (sizeof(long) == sizeof(int64_t))
-	return(_time_to_time64(t));
+	return(libbsd_time_to_time64_time32(t));
     return((long)t);
 }
+time32_symbol(_time_to_long);
 
-time_t
-_long_to_time(long tlong)
+libbsd_time32_t
+libbsd_long_to_time_time32(long tlong);
+libbsd_time32_t
+libbsd_long_to_time_time32(long tlong)
 {
     if (sizeof(long) == sizeof(int32_t))
-	return(_time32_to_time(tlong));
-    return((time_t)tlong);
+	return(libbsd_time32_to_time_time32(tlong));
+    return((libbsd_time32_t)tlong);
 }
+time32_symbol(_long_to_time);
 
 /*
  * Convert to/from 'int'.  Depending on the sizeof(int) this may or
  * may not require using the 50-year rule.
  */
 int
-_time_to_int(time_t t)
+libbsd_time_to_int_time32(libbsd_time32_t t);
+int
+libbsd_time_to_int_time32(libbsd_time32_t t)
 {
     if (sizeof(int) == sizeof(int64_t))
-	return(_time_to_time64(t));
+	return(libbsd_time_to_time64_time32(t));
     return((int)t);
 }
+time32_symbol(_time_to_int);
 
-time_t
-_int_to_time(int tint)
+libbsd_time32_t
+libbsd_int_to_time_time32(int tint);
+libbsd_time32_t
+libbsd_int_to_time_time32(int tint)
 {
     if (sizeof(int) == sizeof(int32_t))
-	return(_time32_to_time(tint));
-    return((time_t)tint);
+	return(libbsd_time32_to_time_time32(tint));
+    return((libbsd_time32_t)tint);
 }
+time32_symbol(_int_to_time);
+#endif
diff --git a/src/unvis.c b/src/unvis.c
index 166421a..378cee6 100644
--- a/src/unvis.c
+++ b/src/unvis.c
@@ -554,7 +554,7 @@ strunvis(char *dst, const char *src)
  * existing one in OpenBSD and Freedesktop's libbsd (the former having existed
  * for over ten years). Despite this incompatibility being reported during
  * development (see http://gnats.netbsd.org/44977) they still shipped it.
- * Even more unfortunately FreeBSD and later MacOS picked up this incompatible
+ * Even more unfortunately FreeBSD and later macOS picked up this incompatible
  * implementation.
  *
  * Provide both implementations and default for now on the historical one to
@@ -564,15 +564,19 @@ strunvis(char *dst, const char *src)
  * NetBSD: 2012,  strnunvis(char *dst, size_t dlen, const char *src);
  */
 ssize_t
+strnunvis_openbsd(char *, const char *, size_t);
+ssize_t
 strnunvis_openbsd(char *dst, const char *src, size_t dlen)
 {
 	return strnunvisx(dst, dlen, src, 0);
 }
-libbsd_symver_default(strnunvis, strnunvis_openbsd, LIBBSD_0.2);
+libbsd_symver_default(strnunvis_openbsd, strnunvis, LIBBSD_0.2);
 
 int
+strnunvis_netbsd(char *, size_t, const char *);
+int
 strnunvis_netbsd(char *dst, size_t dlen, const char *src)
 {
 	return strnunvisx(dst, dlen, src, 0);
 }
-libbsd_symver_variant(strnunvis, strnunvis_netbsd, LIBBSD_0.9.1);
+libbsd_symver_variant(strnunvis_netbsd, strnunvis, LIBBSD_0.9.1);
diff --git a/src/vasprintf.c b/src/vasprintf.c
new file mode 100644
index 0000000..d9ae8bb
--- /dev/null
+++ b/src/vasprintf.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright © 2010-2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 <config.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+vasprintf(char **strp, char const *fmt, va_list args)
+{
+	va_list args_copy;
+	int needed, n;
+	char *str;
+
+	va_copy(args_copy, args);
+	needed = vsnprintf(NULL, 0, fmt, args_copy);
+	va_end(args_copy);
+
+	if (needed < 0) {
+		*strp = NULL;
+		return -1;
+	}
+
+	str = malloc(needed + 1);
+	if (str == NULL) {
+		*strp = NULL;
+		return -1;
+	}
+
+	n = vsnprintf(str, needed + 1, fmt, args);
+	if (n < 0) {
+		free(str);
+		str = NULL;
+	}
+
+	*strp = str;
+
+	return n;
+}
diff --git a/src/vis.c b/src/vis.c
index c8e5ae8..0dafa62 100644
--- a/src/vis.c
+++ b/src/vis.c
@@ -723,7 +723,7 @@ strvis(char *mbdst, const char *mbsrc, int flags)
  * existing one in OpenBSD and Freedesktop's libbsd (the former having existed
  * for over ten years). Despite this incompatibility being reported during
  * development (see http://gnats.netbsd.org/44977) they still shipped it.
- * Even more unfortunately FreeBSD and later MacOS picked up this incompatible
+ * Even more unfortunately FreeBSD and later macOS picked up this incompatible
  * implementation.
  *
  * Provide both implementations and default for now on the historical one to
@@ -733,18 +733,22 @@ strvis(char *mbdst, const char *mbsrc, int flags)
  * NetBSD: 2012,  strnvis(char *dst, size_t dlen, const char *src, int flag);
  */
 int
+strnvis_openbsd(char *, const char *, size_t, int);
+int
 strnvis_openbsd(char *mbdst, const char *mbsrc, size_t dlen, int flags)
 {
 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
 }
-libbsd_symver_default(strnvis, strnvis_openbsd, LIBBSD_0.2);
+libbsd_symver_default(strnvis_openbsd, strnvis, LIBBSD_0.2);
 
 int
+strnvis_netbsd(char *, size_t, const char *, int);
+int
 strnvis_netbsd(char *mbdst, size_t dlen, const char *mbsrc, int flags)
 {
 	return istrsenvisxl(mbdst, &dlen, mbsrc, flags, "", NULL);
 }
-libbsd_symver_variant(strnvis, strnvis_netbsd, LIBBSD_0.9.1);
+libbsd_symver_variant(strnvis_netbsd, strnvis, LIBBSD_0.9.1);
 
 int
 stravis(char **mbdstp, const char *mbsrc, int flags)
diff --git a/test/.gitignore b/test/.gitignore
index 2c39751..bfd5572 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -2,23 +2,27 @@ arc4random
 bzero
 closefrom
 endian
+explicit_bzero
 fgetln
-funopen
 fparseln
 fpurge
+funopen
 headers-overlay-gen.c
 headers-system-gen.c
 humanize
 md5
 nlist
 overlay
-proctitle-init
 proctitle
+proctitle-init
 progname
 pwcache
 setmode
 strl
 strmode
 strnstr
+strtonum
+timeconv32
+timeconv64
 vis
 vis-openbsd
diff --git a/test/Makefile.am b/test/Makefile.am
index 90fe384..13c3309 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -4,75 +4,190 @@ HEADERS_CPPFLAGS = \
 	-I$(top_builddir) \
 	-include $(top_builddir)/config.h \
 	-DLIBBSD_DISABLE_DEPRECATED \
-	-D__REENTRANT
+	-D__REENTRANT \
+	# EOL
 
 AM_CPPFLAGS = \
 	-isystem $(top_srcdir)/include/bsd/ \
 	$(HEADERS_CPPFLAGS) \
-	-DLIBBSD_OVERLAY
+	-DLIBBSD_OVERLAY \
+	# EOL
 
 AM_TESTS_ENVIRONMENT = \
 	export CC="$(CC)"; \
 	export CPPFLAGS="$(HEADERS_CPPFLAGS)"; \
 	export top_srcdir="$(top_srcdir)"; \
-	$(nil)
+	# EOL
 
 LDADD = $(top_builddir)/src/libbsd.la
 
 EXTRA_DIST = \
 	headers-overlay.sh \
 	headers-system.sh \
-	$(nil)
+	# EOL
 
 check_SCRIPTS = \
 	headers-overlay.sh \
 	headers-system.sh \
-	$(nil)
+	# EOL
 
 check_PROGRAMS = \
 	overlay \
-	bzero \
-	closefrom \
 	endian \
-	humanize \
-	fgetln \
-	funopen \
-	fparseln \
-	fpurge \
-	md5 \
-	nlist \
-	proctitle-init \
-	progname \
-	pwcache \
+	# EOL
+
+if ABI_ACCMODE
+check_PROGRAMS += \
 	setmode \
-	strl \
-	strmode \
-	strnstr \
-	vis \
-	vis-openbsd \
-	$(nil)
+	# EOL
+endif
 
+if ABI_ARC4RANDOM
 if HAVE_LIBTESTU01
+check_PROGRAMS += arc4random
+
 arc4random_LDADD = $(LDADD) $(TESTU01_LIBS)
+endif
+endif
 
-check_PROGRAMS += arc4random
+if ABI_CLOSEFROM
+check_PROGRAMS += \
+	closefrom \
+	# EOL
+endif
+
+if ABI_EXPLICIT_BZERO
+check_PROGRAMS += \
+	bzero \
+	explicit_bzero \
+	# EOL
+endif
+
+if ABI_FGETLN
+check_PROGRAMS += \
+	fgetln \
+	fparseln \
+	# EOL
+
+fgetln_SOURCES = \
+	test-stream.c \
+	test-stream.h \
+	fgetln.c \
+	# EOL
+
+fgetln_CFLAGS = -Wno-deprecated-declarations
+
+fparseln_SOURCES = \
+	test-stream.c \
+	test-stream.h \
+	fparseln.c \
+	# EOL
+endif
+
+if ABI_FPURGE
+check_PROGRAMS += fpurge
+endif
+
+if ABI_FUNOPEN
+check_PROGRAMS += funopen
+endif
+
+if ABI_HUMANIZE_NUMBER
+check_PROGRAMS += \
+	humanize \
+	# EOL
+endif
+
+if ABI_FPURGE
+check_PROGRAMS += fpurge
+endif
+
+if ABI_FUNOPEN
+check_PROGRAMS += funopen
+endif
+
+if ABI_MD5
+check_PROGRAMS += md5
+
+if ABI_TRANSPARENT_LIBMD
+# On the installed system this is handled via the ld script.
+md5_LDADD = $(LDADD) $(MD5_LIBS)
+endif
 endif
 
+if ABI_NLIST
+check_PROGRAMS += nlist
+endif
+
+if ABI_PROGNAME
+check_PROGRAMS += progname
+endif
+
+if ABI_PROCTITLE
+check_PROGRAMS += \
+	proctitle-init \
+	# EOL
+
+proctitle_init_SOURCES = \
+	proctitle.c \
+	# EOL
+
+proctitle_init_CPPFLAGS = $(AM_CPPFLAGS) -DTEST_USE_SETPROCTITLE_INIT=1
+
 if BUILD_LIBBSD_CTOR
+check_PROGRAMS += proctitle
+
 proctitle_LDFLAGS = \
 	-Wl,-u,libbsd_init_func \
 	$(top_builddir)/src/libbsd-ctor.a \
 	$(top_builddir)/src/libbsd.la \
-	$(nil)
+	# EOL
+endif
+endif
 
-check_PROGRAMS += proctitle
+if ABI_PWCACHE
+check_PROGRAMS += \
+	pwcache \
+	# EOL
 endif
 
-fgetln_SOURCES = test-stream.c test-stream.h fgetln.c
-fgetln_CFLAGS = -Wno-deprecated-declarations
-fparseln_SOURCES = test-stream.c test-stream.h fparseln.c
+if ABI_PROGNAME
+check_PROGRAMS += progname
+endif
 
-proctitle_init_SOURCES = proctitle.c
-proctitle_init_CPPFLAGS = $(AM_CPPFLAGS) -DTEST_USE_SETPROCTITLE_INIT=1
+if ABI_STRL
+check_PROGRAMS += strl
+endif
+
+if ABI_STRMODE
+check_PROGRAMS += strmode
+endif
+
+if ABI_STRNSTR
+check_PROGRAMS += \
+	strnstr \
+	# EOL
+endif
+
+if ABI_STRTONUM
+check_PROGRAMS += \
+	strtonum \
+	# EOL
+endif
+
+if ABI_VIS
+check_PROGRAMS += \
+	vis \
+	vis-openbsd \
+	# EOL
+endif
+
+if LIBBSD_SYS_IS_TIME32
+check_PROGRAMS += timeconv32
+endif
+
+if LIBBSD_SYS_HAS_TIME64
+check_PROGRAMS += timeconv64
+endif
 
 TESTS = $(check_SCRIPTS) $(check_PROGRAMS)
diff --git a/test/closefrom.c b/test/closefrom.c
index 160e6b1..f9d74cc 100644
--- a/test/closefrom.c
+++ b/test/closefrom.c
@@ -35,18 +35,46 @@ main(int argc, char *argv[])
 {
 	int i;
 	int fd;
+	int fd_max;
 
 	fd = open("/dev/null", O_RDONLY);
 
-	for (i = 4; i < 1024; i *= 2)
-		assert(dup2(fd, i) == i);
+	fd_max = 1024;
+
+	/* First make sure we have a clean fd list. */
+	for (i = 4; i < fd_max; i++)
+		close(i);
+
+	/* Then initialize each even fd. */
+	for (i = 4; i < fd_max; i *= 2) {
+		int fd_new = dup2(fd, i);
+
+		if (fd_new < 0 && (errno == EMFILE || errno == EBADF)) {
+			fd_max = i - 1;
+			break;
+		}
+		assert(fd_new == i);
+	}
 
 	if (fd < 4)
 		close(fd);
 	closefrom(4);
 
-	for (i = 4; i < 1024; i++)
-		assert(fcntl(i, F_GETFL) == -1 && errno == EBADF);
+	for (i = 4; i < fd_max; i++) {
+		int rc;
+
+		errno = 0;
+		rc = fcntl(i, F_GETFD);
+#ifdef __APPLE__
+		/* On macOS we only set close-on-exec. */
+		if ((i & (i - 1)) == 0)
+			assert(rc == FD_CLOEXEC);
+		else
+			assert(rc == -1 && errno == EBADF);
+#else
+		assert(rc == -1 && errno == EBADF);
+#endif
+	}
 	assert(fcntl(fd, F_GETFL) == -1 && errno == EBADF);
 
 	return 0;
diff --git a/test/explicit_bzero.c b/test/explicit_bzero.c
new file mode 100644
index 0000000..031aa42
--- /dev/null
+++ b/test/explicit_bzero.c
@@ -0,0 +1,242 @@
+/*	$OpenBSD: explicit_bzero.c,v 1.7 2021/03/27 11:17:58 bcook Exp $	*/
+/*
+ * Copyright (c) 2014 Google Inc.
+ * Copyright (c) 2022 Guillem Jover <guillem@hadrons.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define ASSERT_EQ(a, b) assert((a) == (b))
+#define ASSERT_NE(a, b) assert((a) != (b))
+#define ASSERT_GE(a, b) assert((a) >= (b))
+
+#if defined(__has_feature)
+#if __has_feature(address_sanitizer)
+#define __SANITIZE_ADDRESS__
+#endif
+#endif
+#ifdef __SANITIZE_ADDRESS__
+#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
+#else
+#define ATTRIBUTE_NO_SANITIZE_ADDRESS
+#endif
+
+/* 128 bits of random data. */
+static const char secret[16] = {
+	0xa0, 0x6c, 0x0c, 0x81, 0xba, 0xd8, 0x5b, 0x0c,
+	0xb0, 0xd6, 0xd4, 0xe3, 0xeb, 0x52, 0x5f, 0x96,
+};
+
+enum {
+	SECRETCOUNT = 64,
+	SECRETBYTES = SECRETCOUNT * sizeof(secret)
+};
+
+/*
+ * As of glibc 2.34, when _GNU_SOURCE is defined, SIGSTKSZ is no longer
+ * constant on Linux. SIGSTKSZ is redefined to sysconf (_SC_SIGSTKSZ).
+ */
+static char *altstack;
+#define ALTSTACK_SIZE (SIGSTKSZ + SECRETBYTES)
+
+static void
+setup_stack(void)
+{
+	const stack_t sigstk = {
+		.ss_sp = altstack = calloc(1, ALTSTACK_SIZE),
+		.ss_size = ALTSTACK_SIZE
+	};
+
+	ASSERT_NE(NULL, altstack);
+	ASSERT_EQ(0, sigaltstack(&sigstk, NULL));
+}
+
+static void
+cleanup_stack(void)
+{
+	free(altstack);
+}
+
+static void
+assert_on_stack(void)
+{
+	stack_t cursigstk;
+	ASSERT_EQ(0, sigaltstack(NULL, &cursigstk));
+	ASSERT_EQ(SS_ONSTACK, cursigstk.ss_flags & (SS_DISABLE|SS_ONSTACK));
+}
+
+static void
+call_on_stack(void (*fn)(int))
+{
+	/*
+	 * This is a bit more complicated than strictly necessary, but
+	 * it ensures we don't have any flaky test failures due to
+	 * inherited signal masks/actions/etc.
+	 *
+	 * On systems where SA_ONSTACK is not supported, this could
+	 * alternatively be implemented using makecontext() or
+	 * pthread_attr_setstack().
+	 */
+
+	const struct sigaction sigact = {
+		.sa_handler = fn,
+		.sa_flags = SA_ONSTACK,
+	};
+	struct sigaction oldsigact;
+	sigset_t sigset, oldsigset;
+
+	/* First, block all signals. */
+	ASSERT_EQ(0, sigemptyset(&sigset));
+	ASSERT_EQ(0, sigfillset(&sigset));
+	ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &sigset, &oldsigset));
+
+	/* Next setup the signal handler for SIGUSR1. */
+	ASSERT_EQ(0, sigaction(SIGUSR1, &sigact, &oldsigact));
+
+	/* Raise SIGUSR1 and momentarily unblock it to run the handler. */
+	ASSERT_EQ(0, raise(SIGUSR1));
+	ASSERT_EQ(0, sigdelset(&sigset, SIGUSR1));
+	ASSERT_EQ(-1, sigsuspend(&sigset));
+	ASSERT_EQ(EINTR, errno);
+
+	/* Restore the original signal action, stack, and mask. */
+	ASSERT_EQ(0, sigaction(SIGUSR1, &oldsigact, NULL));
+	ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &oldsigset, NULL));
+}
+
+static void
+populate_secret(char *buf, ssize_t len)
+{
+	int i, fds[2];
+	ASSERT_EQ(0, pipe(fds));
+
+	for (i = 0; i < SECRETCOUNT; i++)
+		ASSERT_EQ(sizeof(secret), write(fds[1], secret, sizeof(secret)));
+	ASSERT_EQ(0, close(fds[1]));
+
+	ASSERT_EQ(len, read(fds[0], buf, len));
+	ASSERT_EQ(0, close(fds[0]));
+}
+
+static void __attribute__((__noinline__))
+blank_stack_side_effects(char *buf, size_t len)
+{
+#if defined(__GNU__) && !defined(__SANITIZE_ADDRESS__)
+	char scratch[SECRETBYTES * 4];
+
+	/* If the read(3) in populate_secret() wrote into the stack, as it
+	 * might happen on the Hurd for small data, then we might incorrectly
+	 * detect the wrong secret on the stack. */
+	memset(scratch, 0xFF, sizeof(scratch));
+	ASSERT_EQ(NULL, memmem(scratch, sizeof(scratch), buf, len));
+#endif
+}
+
+static int
+count_secrets(const char *buf)
+{
+	int res = 0;
+	size_t i;
+	for (i = 0; i < SECRETCOUNT; i++) {
+		if (memcmp(buf + i * sizeof(secret), secret,
+		    sizeof(secret)) == 0)
+			res += 1;
+	}
+	return (res);
+}
+
+ATTRIBUTE_NO_SANITIZE_ADDRESS static char *
+test_without_bzero(void)
+{
+	char buf[SECRETBYTES];
+	char *res;
+	assert_on_stack();
+	populate_secret(buf, sizeof(buf));
+	blank_stack_side_effects(buf, sizeof(buf));
+	res = memmem(altstack, ALTSTACK_SIZE, buf, sizeof(buf));
+	ASSERT_NE(NULL, res);
+	return (res);
+}
+
+ATTRIBUTE_NO_SANITIZE_ADDRESS static char *
+test_with_bzero(void)
+{
+	char buf[SECRETBYTES];
+	char *res;
+	assert_on_stack();
+	populate_secret(buf, sizeof(buf));
+	blank_stack_side_effects(buf, sizeof(buf));
+	res = memmem(altstack, ALTSTACK_SIZE, buf, sizeof(buf));
+	ASSERT_NE(NULL, res);
+	explicit_bzero(buf, sizeof(buf));
+	return (res);
+}
+
+static void
+do_test_without_bzero(int signo)
+{
+	char *buf = test_without_bzero();
+	ASSERT_GE(count_secrets(buf), 1);
+}
+
+static void
+do_test_with_bzero(int signo)
+{
+	char *buf = test_with_bzero();
+	ASSERT_EQ(count_secrets(buf), 0);
+}
+
+int
+main(int argc, char **argv)
+{
+	setup_stack();
+
+	/*
+	 * Solaris and OS X clobber the signal stack after returning to the
+	 * normal stack, so we need to inspect altstack while we're still
+	 * running on it.  Unfortunately, this means we risk clobbering the
+	 * buffer ourselves.
+	 *
+	 * To minimize this risk, test_with{,out}_bzero() are responsible for
+	 * locating the offset of their buf variable within altstack, and
+	 * and returning that address.  Then we can simply memcmp() repeatedly
+	 * to count how many instances of secret we found.
+	 */
+
+	/*
+	 * First, test that if we *don't* call explicit_bzero, that we
+	 * *are* able to find at least one instance of the secret data still
+	 * on the stack.  This sanity checks that call_on_stack() and
+	 * populate_secret() work as intended.
+	 */
+	memset(altstack, 0, ALTSTACK_SIZE);
+	call_on_stack(do_test_without_bzero);
+
+	/*
+	 * Now test with a call to explicit_bzero() and check that we
+	 * *don't* find any instances of the secret data.
+	 */
+	memset(altstack, 0, ALTSTACK_SIZE);
+	call_on_stack(do_test_with_bzero);
+
+	cleanup_stack();
+
+	return (0);
+}
diff --git a/test/fgetln.c b/test/fgetln.c
index d08fb3b..e137fe7 100644
--- a/test/fgetln.c
+++ b/test/fgetln.c
@@ -75,7 +75,7 @@ test_fgetln_single(void)
 	size_t len;
 	int i;
 
-	fp = pipe_feed("%s", (const void **)data_ascii, DATA_LINES);
+	fp = pipe_feed(PIPE_DATA_ASCII, (const void **)data_ascii, DATA_LINES);
 	for (i = 0; i < DATA_LINES; i++) {
 		char *str = fgetln(fp, &len);
 
@@ -96,13 +96,14 @@ test_fgetln_multi(void)
 		char *str;
 
 		str = strdup("A\n");
+		assert(str);
 		str[0] += i;
 
 		files[i].line_alloc = str;
 		files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
 		files[i].lines[0] = str;
 		files[i].lines[1] = str;
-		files[i].fp = pipe_feed("%s", files[i].lines, LINE_COUNT);
+		files[i].fp = pipe_feed(PIPE_DATA_ASCII, files[i].lines, LINE_COUNT);
 	}
 
 	for (l = 0; l < LINE_COUNT; l++) {
@@ -139,7 +140,7 @@ test_fgetwln_single(void)
 	size_t len;
 	int i;
 
-	fp = pipe_feed("%ls", (const void **)data_wide, DATA_LINES);
+	fp = pipe_feed(PIPE_DATA_WIDE, (const void **)data_wide, DATA_LINES);
 	for (i = 0; i < DATA_LINES; i++) {
 		wchar_t *wstr;
 
@@ -168,7 +169,7 @@ test_fgetwln_multi(void)
 		files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *));
 		files[i].lines[0] = wstr;
 		files[i].lines[1] = wstr;
-		files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT);
+		files[i].fp = pipe_feed(PIPE_DATA_WIDE, files[i].lines, LINE_COUNT);
 	}
 
 	for (l = 0; l < LINE_COUNT; l++) {
diff --git a/test/fparseln.c b/test/fparseln.c
index daff6bd..5d7898d 100644
--- a/test/fparseln.c
+++ b/test/fparseln.c
@@ -68,7 +68,7 @@ test_fparseln(const char **data_expect, int flags)
 	FILE *fp;
 	size_t i, len, lineno = 0;
 
-	fp = pipe_feed("%s", (const void **)data_test, TEST_LINES);
+	fp = pipe_feed(PIPE_DATA_ASCII, (const void **)data_test, TEST_LINES);
 	for (i = 0; i < EXPECT_LINES; i++) {
 		char *str = fparseln(fp, &len, &lineno, NULL, flags);
 
diff --git a/test/fpurge.c b/test/fpurge.c
index a99cff7..784f32e 100644
--- a/test/fpurge.c
+++ b/test/fpurge.c
@@ -25,24 +25,51 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int
+test_memstream(void)
+{
+	int rc = 0;
+#if HAVE_OPEN_MEMSTREAM
+	FILE *fp;
+	char *buf = NULL;
+	size_t bufsz = 0;
+
+	fp = open_memstream(&buf, &bufsz);
+	if (fp == NULL)
+		return 1;
+
+	fputs("World", fp);
+	if (fpurge(fp) < 0)
+		rc = 1;
+	fflush(fp);
+	if (bufsz != 0)
+		rc = 1;
+
+	fclose(fp);
+	free(buf);
+#endif
+
+	return rc;
+}
 
 int
 main(int argc, char *argv[])
 {
-	static FILE fp_bad;
 	FILE *fp;
+	int rc;
 
 	if (fpurge(NULL) == 0)
 		return 1;
 
-	if (fpurge(&fp_bad) == 0)
-		return 1;
-
 	fp = fopen("/dev/zero", "r");
 	if (fpurge(fp) < 0)
 		return 1;
-
 	fclose(fp);
 
-	return 0;
+	rc = test_memstream();
+
+	return rc;
 }
diff --git a/test/funopen.c b/test/funopen.c
index 2b34f7e..65c493e 100644
--- a/test/funopen.c
+++ b/test/funopen.c
@@ -114,7 +114,7 @@ main(int argc, char **argv)
 	size_t i;
 
 	/* Test invalid hooks. */
-	fp = funopen(&tc, NULL, NULL, NULL, NULL);
+	fp = funopen(NULL, NULL, NULL, NULL, NULL);
 	assert(fp == NULL);
 	assert(errno == EINVAL);
 
diff --git a/test/headers-overlay.sh b/test/headers-overlay.sh
index be92eff..176623e 100755
--- a/test/headers-overlay.sh
+++ b/test/headers-overlay.sh
@@ -18,8 +18,9 @@ int main() { return 0; }
 SOURCE
 
   echo "testing header $inc"
-  run $CC -isystem "$incdir" $CPPFLAGS headers-overlay-gen.c -o /dev/null
+  run $CC -isystem "$incdir" $CPPFLAGS headers-overlay-gen.c -o headers-overlay
   echo
 
+  rm -f headers-overlay
   rm -f headers-overlay-gen*
 done
diff --git a/test/headers-system.sh b/test/headers-system.sh
index 5ff0517..458fbbb 100755
--- a/test/headers-system.sh
+++ b/test/headers-system.sh
@@ -17,8 +17,9 @@ int main() { return 0; }
 SOURCE
 
   echo "testing header $inc"
-  run $CC -isystem "$incdir" $CPPFLAGS headers-system-gen.c -o /dev/null
+  run $CC -isystem "$incdir" $CPPFLAGS headers-system-gen.c -o headers-system
   echo
 
+  rm -f headers-system
   rm -f headers-system-gen.*
 done
diff --git a/test/nlist.c b/test/nlist.c
index 82e24e9..0031a4d 100644
--- a/test/nlist.c
+++ b/test/nlist.c
@@ -33,17 +33,29 @@
 #include <fcntl.h>
 #include <nlist.h>
 
+#if defined(__has_attribute)
+#if __has_attribute(__retain__)
+#define ATTRIBUTE_RETAIN __attribute__((__retain__))
+#else
+#define ATTRIBUTE_RETAIN
+#endif
+#endif
+
 static int data_prv_init = 50;
 extern int data_pub_init;
 extern int data_pub_uninit[2048];
 extern int *data_pub_ptr;
 
+/* GCC's __has_attribute has odd behavior for retain: https://gcc.gnu.org/PR99587 */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wattributes"
 int *data_pub_ptr = &data_prv_init;
-int data_pub_init = 10;
-int data_pub_uninit[2048];
+int data_pub_init ATTRIBUTE_RETAIN __attribute__((__used__)) = 10;
+int data_pub_uninit[2048] ATTRIBUTE_RETAIN __attribute__((__used__));
 
 extern int
-func_pub(void);
+func_pub(void) ATTRIBUTE_RETAIN __attribute__((__used__));
+#pragma GCC diagnostic pop
 
 int
 func_pub(void)
@@ -55,11 +67,11 @@ int
 main(int argc, char **argv)
 {
 	struct nlist nl[] = {
-		{ .n_un.n_name = "main" },
-		{ .n_un.n_name = "func_pub" },
-		{ .n_un.n_name = "data_pub_uninit" },
-		{ .n_un.n_name = "data_pub_init" },
-		{ .n_un.n_name = "data_prv_init" },
+		{ .n_un.n_name = (char *)"main" },
+		{ .n_un.n_name = (char *)"func_pub" },
+		{ .n_un.n_name = (char *)"data_pub_uninit" },
+		{ .n_un.n_name = (char *)"data_pub_init" },
+		{ .n_un.n_name = (char *)"data_prv_init" },
 		{ .n_un.n_name = NULL },
 	};
 	int rc;
diff --git a/test/overlay.c b/test/overlay.c
index d82f14e..af83232 100644
--- a/test/overlay.c
+++ b/test/overlay.c
@@ -28,12 +28,14 @@
  * other headers through magic macros, to check that the overlay is working
  * properly. */
 #include <errno.h>
+#ifndef __APPLE__
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
 #ifdef HAVE_GRP_H
 #include <grp.h>
 #endif
+#endif
 #include <stdint.h>
 
 /* Include libbsd overlayed headers that might get partially included. */
diff --git a/test/pwcache.c b/test/pwcache.c
index d68ea4c..458903f 100644
--- a/test/pwcache.c
+++ b/test/pwcache.c
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ * Copyright © 2021, 2023 Guillem Jover <guillem@hadrons.org>
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -26,24 +26,50 @@
 
 #include <assert.h>
 #include <string.h>
+#include <stdlib.h>
 #include <pwd.h>
 #include <grp.h>
 
+#define TEST_SKIP	77
+
 int
 main(int argc, char **argv)
 {
+	struct group *gr;
+	struct passwd *pw;
+	char *uname;
+	char *gname;
 	uid_t uid;
 	gid_t gid;
 
-	assert(uid_from_user("root", &uid) == 0);
+	/* Do not hardcode user or group names. */
+	pw = getpwuid(0);
+	if (pw == NULL)
+		return TEST_SKIP;
+	uname = strdup(pw->pw_name);
+	assert(uname != NULL);
+
+	gr = getgrgid(0);
+	if (gr == NULL) {
+		free(uname);
+		return TEST_SKIP;
+	}
+	gname = strdup(gr->gr_name);
+	assert(gname != NULL);
+
+	/* Test the functions. */
+	assert(uid_from_user(uname, &uid) == 0);
 	assert(uid == 0);
 
-	assert(strcmp(user_from_uid(0, 0), "root") == 0);
+	assert(strcmp(user_from_uid(0, 0), uname) == 0);
 
-	assert(gid_from_group("root", &gid) == 0);
+	assert(gid_from_group(gname, &gid) == 0);
 	assert(gid == 0);
 
-	assert(strcmp(group_from_gid(0, 0), "root") == 0);
+	assert(strcmp(group_from_gid(0, 0), gname) == 0);
+
+	free(uname);
+	free(gname);
 
 	return 0;
 }
diff --git a/test/strtonum.c b/test/strtonum.c
new file mode 100644
index 0000000..6dee3da
--- /dev/null
+++ b/test/strtonum.c
@@ -0,0 +1,64 @@
+/*	$OpenBSD: strtonumtest.c,v 1.1 2004/08/03 20:38:36 otto Exp $	*/
+/*
+ * Copyright (c) 2004 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int fail;
+
+static void
+test(const char *p, long long lb, long long ub, int ok)
+{
+	long long val;
+	const char *q;
+
+	val = strtonum(p, lb, ub, &q);
+	if (ok && q != NULL) {
+		fprintf(stderr, "%s [%lld-%lld] ", p, lb, ub);
+		fprintf(stderr, "NUMBER NOT ACCEPTED %s\n", q);
+		fail = 1;
+	} else if (!ok && q == NULL) {
+		fprintf(stderr, "%s [%lld-%lld] %lld ", p, lb, ub, val);
+		fprintf(stderr, "NUMBER ACCEPTED\n");
+		fail = 1;
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	test("1", 0, 10, 1);
+	test("0", -2, 5, 1);
+	test("0", 2, 5, 0);
+	test("0", 2, LLONG_MAX, 0);
+	test("-2", 0, LLONG_MAX, 0);
+	test("0", -5, LLONG_MAX, 1);
+	test("-3", -3, LLONG_MAX, 1);
+	test("-9223372036854775808", LLONG_MIN, LLONG_MAX, 1);
+	test("9223372036854775807", LLONG_MIN, LLONG_MAX, 1);
+	test("-9223372036854775809", LLONG_MIN, LLONG_MAX, 0);
+	test("9223372036854775808", LLONG_MIN, LLONG_MAX, 0);
+	test("1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0);
+	test("-1000000000000000000000000", LLONG_MIN, LLONG_MAX, 0);
+	test("-2", 10, -1, 0);
+	test("-2", -10, -1, 1);
+	test("-20", -10, -1, 0);
+	test("20", -10, -1, 0);
+
+	return (fail);
+}
+
diff --git a/test/test-stream.c b/test/test-stream.c
index 849f419..0bbb641 100644
--- a/test/test-stream.c
+++ b/test/test-stream.c
@@ -27,12 +27,13 @@
 #include <sys/wait.h>
 #include <assert.h>
 #include <unistd.h>
+#include <wchar.h>
 #include <stdio.h>
 
 #include "test-stream.h"
 
 FILE *
-pipe_feed(const char *fmt, const void **buf, int buf_nmemb)
+pipe_feed(enum pipe_data_mode mode, const void **buf, int buf_nmemb)
 {
 	FILE *fp;
 	int rc;
@@ -56,7 +57,10 @@ pipe_feed(const char *fmt, const void **buf, int buf_nmemb)
 		assert(fp);
 
 		for (line = 0; line < buf_nmemb; line++) {
-			rc = fprintf(fp, fmt, buf[line]);
+			if (mode == PIPE_DATA_ASCII)
+				rc = fprintf(fp, "%s", (const char *)buf[line]);
+			else
+				rc = fprintf(fp, "%ls", (const wchar_t *)buf[line]);
 			assert(rc >= 0);
 		}
 
diff --git a/test/test-stream.h b/test/test-stream.h
index cee4e60..8caf3fc 100644
--- a/test/test-stream.h
+++ b/test/test-stream.h
@@ -29,8 +29,13 @@
 
 #include <stdio.h>
 
+enum pipe_data_mode {
+	PIPE_DATA_ASCII,
+	PIPE_DATA_WIDE,
+};
+
 FILE *
-pipe_feed(const char *fmt, const void **buf, int buf_nmemb);
+pipe_feed(enum pipe_data_mode mode, const void **buf, int buf_nmemb);
 void
 pipe_close(FILE *fp);
 
diff --git a/test/timeconv32.c b/test/timeconv32.c
new file mode 100644
index 0000000..549e612
--- /dev/null
+++ b/test/timeconv32.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
+ */
+
+#undef _TIME_BITS
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <timeconv.h>
+
+int
+main(int argc, char **argv)
+{
+	time_t t;
+
+	t = _time64_to_time(INT64_MAX);
+	assert(t < INT64_MAX);
+
+	return 0;
+}
diff --git a/test/timeconv64.c b/test/timeconv64.c
new file mode 100644
index 0000000..d175ed2
--- /dev/null
+++ b/test/timeconv64.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2024 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 <assert.h>
+#include <stdint.h>
+#include <timeconv.h>
+
+int
+main(int argc, char **argv)
+{
+	time_t t;
+
+	t = _time64_to_time(INT64_MAX);
+	assert(t == INT64_MAX);
+
+	return 0;
+}