File: 2008.xs

package info (click to toggle)
libposix-2008-perl 0.26-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 888 kB
  • sloc: perl: 1,072; makefile: 3
file content (4052 lines) | stat: -rw-r--r-- 85,010 bytes parent folder | download
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
#define PACKNAME "POSIX::2008"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* ppport.h says we don't need caller_cx but a few cpantesters report
 * "undefined symbol: caller_cx".
 */
#define NEED_caller_cx
#define NEED_croak_xs_usage
#include "ppport.h"
#include "2008.h"

#if defined(PERL_IMPLICIT_SYS)
#undef dup
#undef open
#undef close
#undef stat
#undef fstat
#undef lstat
#undef readlink
# if !defined(_WIN32) || defined(__CYGWIN__)
#undef abort
#undef access
#undef chdir
#undef fchdir
#undef chmod
#undef fchmod
#undef chown
#undef fchown
#undef fdopen
#undef fdopendir
#undef getegid
#undef geteuid
#undef getgid
#undef gethostname
#undef getuid
#undef isatty
#undef killpg
#undef link
#undef mkdir
#undef read
#undef rename
#undef rmdir
#undef setgid
#undef setuid
#undef symlink
#undef unlink
#undef write
# endif
#endif

#ifdef PSX2008_HAS_COMPLEX_H
#include <complex.h>
#endif
#include <ctype.h>
#ifdef I_DIRENT
#include <dirent.h>
#endif
#if defined(I_DLFCN) && defined(PSX2008_HAS_DLFCN_H)
#include <dlfcn.h>
#endif
#include <errno.h>
#ifdef I_FLOAT
#include <float.h>
#endif
#ifdef I_FCNTL
#include <fcntl.h>
#endif
#include <fenv.h>
#ifdef PSX2008_HAS_FNMATCH_H
#include <fnmatch.h>
#endif
#ifdef I_INTTYPES
#include <inttypes.h>
#endif
#ifdef PSX2008_HAS_LIBGEN_H
#include <libgen.h>
#endif
#ifdef I_LIMITS
#include <limits.h>
#endif
#ifdef I_NETDB
#include <netdb.h>
#endif
#ifdef I_MATH
#include <math.h>
#endif
#ifdef PSX2008_HAS_NL_TYPES_H
#include <nl_types.h>
#endif
#ifdef PSX2008_HAS_SIGNAL_H
#include <signal.h>
#endif
#ifdef I_STDLIB
#include <stdlib.h>
#endif
#ifdef I_STRING
#include <string.h>
#endif
#ifdef PSX2008_HAS_STRINGS_H
#include <strings.h>
#endif
#ifdef I_SUNMATH
#include <sunmath.h>
#endif
#ifdef I_SYS_PARAM
#include <sys/param.h>
#endif
#ifdef I_SYS_RESOURCE
#include <sys/resource.h>
#endif
#ifdef I_SYS_STAT
#include <sys/stat.h>
#endif
#ifdef PSX2008_HAS_STATVFS
#include <sys/statvfs.h>
#endif
#ifdef I_SYS_TYPES
#include <sys/types.h>
#endif
#if defined(I_SYSUIO) && defined(PSX2008_HAS_SYS_UIO_H)
#include <sys/uio.h>
#endif
#ifdef I_TIME
#include <time.h>
#endif
#if defined(I_UNISTD) || defined(_WIN32)
#include <unistd.h>
#endif
#ifdef PSX2008_HAS_UTMPX_H
#include <utmpx.h>
#endif
#if defined(PSX2008_HAS_POLL_H)
#  include <poll.h>
#elif defined(PSX2008_HAS_SYS_POLL_H)
#  include <sys/poll.h>
#endif

#if defined(__linux__) && defined(PSX2008_HAS_OPENAT2)
#include <sys/syscall.h>
#include <linux/openat2.h>
#endif

#include "const-c.inc"

#ifndef Size_t_MAX
#define Size_t_MAX (~(Size_t)0)
#endif

#ifndef SSize_t_MAX
#define SSize_t_MAX (SSize_t)(~(Size_t)0 >> 1)
#endif

#ifndef SVf_QUOTEDPREFIX
#define SVf_QUOTEDPREFIX SVf
#endif

#ifndef PSX2008_HAS_NFDS_T
#define nfds_t unsigned long
#endif

#ifdef I64TYPE
#define INT_MAX_TYPE I64TYPE
#define UINT_MAX_TYPE U64TYPE
#else
#define INT_MAX_TYPE I32TYPE
#define UINT_MAX_TYPE U32TYPE
#endif

#if IVSIZE > LONGSIZE
# if defined(PSX2008_HAS_LLDIV)
#  define PSX2008_DIV_T lldiv_t
#  define PSX2008_DIV(numer, denom) lldiv(numer, denom)
# elif defined(PSX2008_HAS_LDIV)
#  define PSX2008_DIV_T ldiv_t
#  define PSX2008_DIV(numer, denom) ldiv(numer, denom)
# elif defined(PSX2008_HAS_DIV)
#  define PSX2008_DIV_T div_t
#  define PSX2008_DIV(numer, denom) div(numer, denom)
# endif
#elif IVSIZE > INTSIZE
# if defined(PSX2008_HAS_LDIV)
#  define PSX2008_DIV_T ldiv_t
#  define PSX2008_DIV(numer, denom) ldiv(numer, denom)
# elif defined(PSX2008_HAS_DIV)
#  define PSX2008_DIV_T div_t
#  define PSX2008_DIV(numer, denom) div(numer, denom)
# endif
#elif defined(PSX2008_HAS_DIV)
# define PSX2008_DIV_T div_t
# define PSX2008_DIV(numer, denom) div(numer, denom)
#endif

#if IVSIZE > LONGSIZE
# if defined(PSX2008_HAS_ATOLL)
#  define PSX2008_ATOI(a) atoll(a)
# elif defined(PSX2008_HAS_ATOL)
#  define PSX2008_ATOI(a) atol(a)
# elif defined(PSX2008_HAS_ATOI)
#  define PSX2008_ATOI(a) atoi(a)
# endif
# if defined(PSX2008_HAS_FFSLL)
#  define PSX2008_FFS(i) ffsll(i)
# elif defined(PSX2008_HAS_FFSL)
#  define PSX2008_FFS(i) ffsl(i)
# elif defined(PSX2008_HAS_FFS)
#  define PSX2008_FFS(i) ffs(i)
# endif
# if defined(PSX2008_HAS_LLABS)
#  define PSX2008_ABS(i) llabs(i)
# elif defined(PSX2008_HAS_LABS)
#  define PSX2008_ABS(i) labs(i)
# elif defined(PSX2008_HAS_ABS)
#  define PSX2008_ABS(i) abs(i)
# endif
#elif IVSIZE > INTSIZE
# if defined(PSX2008_HAS_ATOL)
#  define PSX2008_ATOI(a) atol(a)
# elif defined(PSX2008_HAS_ATOI)
#  define PSX2008_ATOI(a) atoi(a)
# endif
# if defined(PSX2008_HAS_FFSL)
#  define PSX2008_FFS(i) ffsl(i)
# elif defined(PSX2008_HAS_FFS)
#  define PSX2008_FFS(i) ffs(i)
# endif
# if defined(PSX2008_HAS_LABS)
#  define PSX2008_ABS(i) labs(i)
# elif defined(PSX2008_HAS_ABS)
#  define PSX2008_ABS(i) abs(i)
# endif
#else
# if defined(PSX2008_HAS_ATOI)
#  define PSX2008_ATOI(a) atoi(a)
# endif
# if defined(PSX2008_HAS_FFS)
#  define PSX2008_FFS(i) ffs(i)
# endif
# if defined(PSX2008_HAS_ABS)
#  define PSX2008_ABS(i) abs(i)
# endif
#endif

#if defined(PSX2008_HAS_LLROUND)
# define PSX2008_LROUND(x) llround(x)
# define PSX2008_LROUND_T long long
#elif defined(PSX2008_HAS_LROUND)
# define PSX2008_LROUND(x) lround(x)
# define PSX2008_LROUND_T long
#endif

#if defined(PSX2008_HAS_SCALBLN)
#define PSX2008_SCALBN(x, n) scalbln(x, n)
#elif defined(PSX2008_HAS_SCALBN)
#define PSX2008_SCALBN(x, n) scalbn(x, n)
#endif

#if defined(AT_FDCWD) ||                   \
  defined(PSX2008_HAS_CHDIR) ||            \
  defined(PSX2008_HAS_CHMOD) ||            \
  defined(PSX2008_HAS_CHOWN) ||            \
  defined(PSX2008_HAS_FDATASYNC) ||        \
  defined(PSX2008_HAS_FDOPEN) ||           \
  defined(PSX2008_HAS_FDOPENDIR) ||        \
  defined(PSX2008_HAS_FSYNC) ||            \
  defined(PSX2008_HAS_FUTIMENS) ||         \
  defined(PSX2008_HAS_ISATTY) ||           \
  defined(PSX2008_HAS_POLL) ||             \
  defined(PSX2008_HAS_POSIX_FADVISE) ||    \
  defined(PSX2008_HAS_POSIX_FALLOCATE) ||  \
  defined(PSX2008_HAS_PTSNAME) ||          \
  defined(PSX2008_HAS_READ) ||             \
  defined(PSX2008_HAS_STAT) ||             \
  defined(PSX2008_HAS_TRUNCATE) ||         \
  defined(PSX2008_HAS_TTYNAME) ||          \
  defined(PSX2008_HAS_WRITE)
#define PSX2008_NEED_PSX_FILENO
#endif

#define RETURN_COMPLEX(z) { \
    EXTEND(SP, 2);          \
    mPUSHn(creal(z));       \
    mPUSHn(cimag(z));       \
}

typedef IV SysRet; /* returns -1 as undef, 0 as "0 but true", other unchanged */
typedef IV SysRet0; /* returns -1 as undef, other unchanged */
typedef IV SysRetTrue; /* returns 0 as "0 but true", undef otherwise */
typedef int psx_fd_t; /* checks for file handle or descriptor via typemap */

/* Round up l to the next multiple of PERL_STRLEN_ROUNDUP_QUANTUM even if it
 * already is a multiple so that we always have room for a trailing '\0'. +1
 * does the trick. */
#define TopUpLEN(l) ((l)+1 < (l) ? (croak_memory_wrap(),0) : PERL_STRLEN_ROUNDUP((l)+1))

/* Treat omitted and explicitly undef arguments as if intended by the caller
 * to avoid annoying "uninitialized" warnings. */
#define SvUNDEF_purposely(sv) (!(sv) || (sv) == &PL_sv_undef)

#define SvNEGATIVE(sv) _psx_sv_negative(aTHX_ sv)

#if IVSIZE < Off_t_size
#define SvOFFt(sv) ((Off_t)SvNV(sv))
#else
#define SvOFFt(sv) ((Off_t)SvIV(sv))
#endif

#if IVSIZE < Size_t_size
#define SvSIZEt(sv) ((Size_t)SvNV(sv))
#else
#define SvSIZEt(sv) ((Size_t)SvUV(sv))
#endif

#define SvSTRLEN(sv) (IVSIZE < sizeof(STRLEN) ? (STRLEN)SvNV(sv) : (STRLEN)SvUV(sv))

/* From perldoc perlguts. */
#if PERL_BCDVERSION >= 0x5018000
# define SvTRULYREADONLY(sv) SvREADONLY(sv)
#else
# define SvTRULYREADONLY(sv) (SvREADONLY(sv) && !SvIsCOW(sv))
#endif

static int
_psx_looks_like_number(pTHX_ SV *sv)
{
  SV *tmpsv;
  SvGETMAGIC(sv);
  if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv, numer))) {
    sv = tmpsv;
  }
#if PERL_BCDVERSION >= 0x5008005
  return looks_like_number(sv);
#else
  if (SvPOK(sv) || SvPOKp(sv))
    return looks_like_number(sv);
  else
    return (SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK));
#endif
}

static int
_psx_sv_negative(pTHX_ SV *sv)
{
  if (!sv)
    return 0;
  SvGETMAGIC(sv);
  if (!SvOK(sv))
    return 0;
  if (SvIOK(sv))
    return !SvIsUV(sv) && SvIVX(sv) < 0;
  if (SvNOK(sv))
    return SvNVX(sv) < 0;
  if (SvPOK(sv))
    return (_psx_looks_like_number(aTHX_ sv) & IS_NUMBER_NEG) != 0;
  return 0;
}

/* strnlen() shamelessly plagiarized from dietlibc (https://www.fefe.de/) */
#if !defined(PSX2008_HAS_STRNLEN)
static STRLEN
strnlen(const char *s, STRLEN maxlen)
{
  const char *n = memchr(s, 0, maxlen);
  if (!n)
    n = s + maxlen;
  return n - s;
}
#endif

/* _fmt_uint() shamelessly plagiarized from libowfat (https://www.fefe.de/) */
static UV
_fmt_uint(char *dest, UINT_MAX_TYPE u)
{
  UV len, len2;
  UINT_MAX_TYPE tmp;
  /* count digits */
  for (len=1, tmp=u; tmp>9; ++len)
    tmp /= 10;
  if (dest)
    for (tmp=u, dest+=len, len2=len+1; --len2; tmp/=10)
      *--dest = (char)((tmp%10)+'0');
  return len;
}

static UV
_fmt_neg_int(char *dest, INT_MAX_TYPE i)
{
  if (dest)
    *dest++ = '-';
  return _fmt_uint(dest, (UINT_MAX_TYPE)(-(i+1))+1) + 1;
}

#ifndef CLANG_DIAG_IGNORE_STMT
# define CLANG_DIAG_IGNORE_STMT(x) NOOP
# define CLANG_DIAG_RESTORE_STMT NOOP
#endif
#ifndef GCC_DIAG_IGNORE_STMT
# define GCC_DIAG_IGNORE_STMT(x) NOOP
# define GCC_DIAG_RESTORE_STMT NOOP
#endif

/* Push int_val as an IV, UV or PV depending on how big the value is. */
#define PUSH_INT_OR_PV(int_val) STMT_START {                \
    SV *_piop_tmp_sv;                                       \
    CLANG_DIAG_IGNORE_STMT(-Wtautological-compare);         \
    GCC_DIAG_IGNORE_STMT(-Wtype-limits);                    \
    if ((int_val) < 0) {                                    \
      if ((int_val) >= IV_MIN)                              \
        _piop_tmp_sv = newSViv(int_val);                    \
      else {                                                \
        char _piop_buf[24];                                 \
        UV _piop_len = _fmt_neg_int(_piop_buf, (int_val));  \
        _piop_tmp_sv = newSVpvn(_piop_buf, _piop_len);      \
      }                                                     \
    }                                                       \
    else if ((int_val) <= IV_MAX)                           \
      _piop_tmp_sv = newSViv(int_val);                      \
    else if ((int_val) <= UV_MAX)                           \
      _piop_tmp_sv = newSVuv(int_val);                      \
    else {                                                  \
      char _piop_buf[24];                                   \
      UV _piop_len = _fmt_uint(_piop_buf, (int_val));       \
      _piop_tmp_sv = newSVpvn(_piop_buf, _piop_len);        \
    }                                                       \
    GCC_DIAG_RESTORE_STMT;                                  \
    CLANG_DIAG_RESTORE_STMT;                                \
    mPUSHs(_piop_tmp_sv);                                   \
  } STMT_END

#ifdef PSX2008_HAS_STAT
/* We return decimal strings for values outside the IV_MIN..UV_MAX range. */
static SV **
_push_stat_buf(pTHX_ SV **SP, struct stat *st)
{
  PUSH_INT_OR_PV(st->st_dev);
  PUSH_INT_OR_PV(st->st_ino);
  PUSH_INT_OR_PV(st->st_mode);
  PUSH_INT_OR_PV(st->st_nlink);
  PUSH_INT_OR_PV(st->st_uid);
  PUSH_INT_OR_PV(st->st_gid);
  PUSH_INT_OR_PV(st->st_rdev);
  PUSH_INT_OR_PV(st->st_size);
  PUSH_INT_OR_PV(st->st_atime);
  PUSH_INT_OR_PV(st->st_mtime);
#ifdef PSX2008_HAS_ST_CTIME
  PUSH_INT_OR_PV(st->st_ctime);
#else
  PUSHs(&PL_sv_undef);
#endif
  /* Actually these come before the times but we follow core stat. */
#ifdef USE_STAT_BLOCKS
  PUSH_INT_OR_PV(st->st_blksize);
  PUSH_INT_OR_PV(st->st_blocks);
#else
  PUSHs(&PL_sv_undef);
  PUSHs(&PL_sv_undef);
#endif
#if defined(PSX2008_HAS_ST_ATIM)
  PUSH_INT_OR_PV(st->st_atim.tv_nsec);
  PUSH_INT_OR_PV(st->st_mtim.tv_nsec);
# ifdef PSX2008_HAS_ST_CTIME
  PUSH_INT_OR_PV(st->st_ctim.tv_nsec);
# else
  PUSHs(&PL_sv_undef);
# endif
#elif defined PSX2008_HAS_ST_ATIMENSEC
  PUSH_INT_OR_PV(st->st_atimensec);
  PUSH_INT_OR_PV(st->st_mtimensec);
# ifdef PSX2008_HAS_ST_CTIME
  PUSH_INT_OR_PV(st->st_ctimensec);
# else
  PUSHs(&PL_sv_undef);
# endif
#endif

  return SP;
}

#define RETURN_STAT_BUF(rv, buf) STMT_START {   \
    switch (GIMME_V) {                          \
      case G_SCALAR:                            \
        PUSHs(boolSV(rv == 0));                 \
        break;                                  \
      case G_LIST:                              \
        if (rv == 0) {                          \
          EXTEND(SP, 16);                       \
          SP = _push_stat_buf(aTHX_ SP, &buf);  \
        }                                       \
    }                                           \
} STMT_END
#endif

#ifdef PSX2008_HAS_STATVFS
static SV **
_push_statvfs_buf(pTHX_ SV **SP, struct statvfs *st)
{
  PUSH_INT_OR_PV(st->f_bsize);
  PUSH_INT_OR_PV(st->f_frsize);
  PUSH_INT_OR_PV(st->f_blocks);
  PUSH_INT_OR_PV(st->f_bfree);
  PUSH_INT_OR_PV(st->f_bavail);
  PUSH_INT_OR_PV(st->f_files);
  PUSH_INT_OR_PV(st->f_ffree);
  PUSH_INT_OR_PV(st->f_favail);
  PUSH_INT_OR_PV(st->f_fsid);
  PUSH_INT_OR_PV(st->f_flag);
  PUSH_INT_OR_PV(st->f_namemax);

  return SP;
}

#define RETURN_STATVFS_BUF(rv, buf) STMT_START {        \
    switch (GIMME_V) {                                  \
      case G_SCALAR:                                    \
        PUSHs(boolSV(rv == 0));                         \
        break;                                          \
      case G_LIST:                                      \
        if (rv == 0) {                                  \
          EXTEND(SP, 16);                               \
          SP = _push_statvfs_buf(aTHX_ SP, &buf);       \
        }                                               \
    }                                                   \
} STMT_END
#endif

#if defined(PSX2008_HAS_EXECVEAT) || defined(PSX2008_HAS_FEXECVE)
/* We don't check for '\0' or '=' within args or env. Not our business. */
static void
_execve50c(pTHX_ int fd, const char *path, AV *args, SV *envsv, int flags)
{
  HV *envhv;
  Size_t argc, n;
  char **argv, **envp;
  char *empty_env[] = { NULL };
  const char *const func = path ? "execveat" : "fexecve";

# ifndef PSX2008_HAS_EXECVEAT
  if (path) { errno = ENOSYS; return; }
# endif
# ifndef PSX2008_HAS_FEXECVE
  if (!path) { errno = ENOSYS; return; }
# endif

  if (SvUNDEF_purposely(envsv))
    envhv = NULL;
  else {
    SvGETMAGIC(envsv);
    if (SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV)
      envhv = (HV*)SvRV(envsv);
    else
      croak("%s::%s: 'env' is not a HASH reference: %"SVf_QUOTEDPREFIX,
           PACKNAME, func, SVfARG(envsv));
  }

  /* Allocate memory for argv pointers; +1 for terminating NULL pointer. */
  argc = av_count(args);
  if (UNLIKELY(argc+1 == 0))
    --argc;
  if (UNLIKELY((argc+1)*sizeof(char*)/sizeof(char*) != (argc+1)))
    goto e2big;
  argv = safemalloc((argc+1)*sizeof(char*));
  SAVEFREEPV(argv);

  /* Build argv string array from args array ref. */
  for (n = 0; n < argc; n++) {
    SV **argsv = av_fetch(args, n, 0);
    if (!argsv)
      argv[n] = "";
    else {
      STRLEN cur;
      (void)SvPV(*argsv, cur);
      if (LIKELY(cur+1)) {
        /* +1 for final '\0' to be on the safe side. */
        char *arg = SvGROW(*argsv, cur+1);
        arg[cur] = '\0';
        argv[n] = arg;
      }
      else
        goto e2big;
    }
  }
  argv[argc] = NULL;

  if (!envhv) {
    extern char **environ;
    envp = environ ? environ : empty_env;
  }
  else {
    Size_t envc;
    /* Count envhv keys. */
    if (!SvMAGICAL(envhv))
      envc = HvUSEDKEYS(envhv);
    else {
      /* HvUSEDKEYS() doesn't work for magic hashes (e.g. DB_File). */
      hv_iterinit(envhv);
      for (envc = 0; hv_iternext(envhv) && envc+1; envc++) {}
    }
    if (envc+1 == 0)
      --envc;

    /* Allocate and zero-fill memory for envp; +1 for terminating NULL
     * pointer. */
    envp = safecalloc(envc+1, sizeof(char*));
    SAVEFREEPV(envp);

    /* Build envp ("key=value") string array from envhv hash ref. Iterate at
     * most envc times (envhv could be changed in another thread). */
    hv_iterinit(envhv);
    for (n = 0; n < envc; n++) {
      I32 klen;
      char *env_key;
      SV *valsv = hv_iternextsv(envhv, &env_key, &klen);
      if (!valsv) /* envhv shrunk along the way. */
        break;
      else {
        char *env_ent;
        STRLEN env_ent_len, env_val_len;
        /* klen < 0 means "utf8". klen == I32_MIN cannot happen because
         * hash keys cannot be longer than I32_MAX, so -klen is safe. */
        STRLEN env_key_len = klen < 0 ? -klen : klen;
        const char *env_val = SvPV_const(valsv, env_val_len);

        env_ent_len = env_key_len + env_val_len;
        if (UNLIKELY(env_ent_len < env_key_len))
          goto e2big;
        env_ent_len += 2; /* +2 for '=' and terminating NUL byte. */
        if (UNLIKELY(env_ent_len < 2))
          goto e2big;

        envp[n] = env_ent = safemalloc(env_ent_len);
        SAVEFREEPV(env_ent);
        env_ent[env_ent_len-1] = '\0';
        env_ent[env_key_len] = '=';
        Copy(env_key, env_ent, env_key_len, char);
        Copy(env_val, env_ent+env_key_len+1, env_val_len, char);
      }
    }
  }

  PERL_FLUSHALL_FOR_CHILD;

  /* These ifdefs only serve to silence dumb compilers who don't realize that
   * the returns at the top mean that this code is never reached. */
  if (path) {
# ifdef PSX2008_HAS_EXECVEAT
    execveat(fd, path, (char *const *)argv, (char *const *)envp, flags);
# else
    errno = ENOSYS;
# endif
  }
  else {
# ifdef PSX2008_HAS_FEXECVE
    fexecve(fd, (char *const *)argv, (char *const *)envp);
# else
    errno = ENOSYS;
# endif
  }
  return;

 e2big:
  errno = E2BIG;
}
#endif

#ifdef PSX2008_HAS_READLINK
static SV *
_readlink50c(pTHX_ const char *path, const int *dirfdp)
{
  /*
   * CORE::readlink() is broken because it uses a fixed-size result buffer of
   * PATH_MAX bytes (the manpage explicitly advises against this). We use a
   * dynamically growing buffer instead, leaving it up to the file system how
   * long a symlink may be.
   */
  size_t bufsize = 1023; /* This should be enough in most cases to read the
                          * link in one go. */
  ssize_t rv;
  char *buf;

  /* If available, we use readlinkat() with AT_FDCWD instead of readlink() to
   * avoid a branch in the loop. */
#ifdef PSX2008_HAS_READLINKAT
  /* Cast AT_FDCWD to cope with Solaris 0xffd19553 artwork. */
  int dirfd = dirfdp ? *dirfdp : (int)AT_FDCWD;
#else
  if (dirfdp != NULL) {
    errno = ENOSYS;
    return NULL;
  }
#endif

  buf = NULL;  /* Makes saferealloc() act like safemalloc() the first time. */
  while (1) {
    buf = saferealloc(buf, bufsize);
    if (buf == NULL) {
      errno = ENOMEM;
      return NULL;
    }

#ifdef PSX2008_HAS_READLINKAT
    rv = readlinkat(dirfd, path, buf, bufsize);
#else
    rv = readlink(path, buf, bufsize);
#endif

    if (LIKELY(rv != -1)) {
      size_t linklen = (size_t)rv;
      if (UNLIKELY((STRLEN)linklen != linklen))
        goto MicrosoftMustDie;
      if (LIKELY(linklen < bufsize)) {
        buf[linklen] = '\0';
        SAVEFREEPV(buf);
        return newSVpvn_flags(buf, linklen, SVs_TEMP);
      }
    }
    else if (errno != ERANGE)
      /* gnulib says, on some systems ERANGE means that bufsize is too small */
      goto FuckTheSkullOfGoogle;

    if (UNLIKELY(bufsize+1 == 0)) {
    MicrosoftMustDie:
      errno = ENAMETOOLONG;
    FuckTheSkullOfGoogle:
      Safefree(buf);
      return NULL;
    }

    bufsize <<= 1;
    bufsize |= 1;
  }
}
#endif

#if defined(PSX2008_HAS_READV) || defined(PSX2008_HAS_PREADV)   \
  || defined(PSX2008_HAS_PREADV2)
static void
_free_iov(const struct iovec *iov, size_t cnt) {
  size_t i;
  if (iov)
    for (i = 0; i < cnt; i++)
      if (iov[i].iov_base)
        Safefree(iov[i].iov_base);
}

static ssize_t
_readv50c(pTHX_ int fd, SV *buffers, AV *sizes, SV *offset_sv, SV *flags_sv)
{
  ssize_t rv;
  Size_t i, iovcnt;
  struct iovec *iov;
  const char *func = flags_sv ? "preadv2" : offset_sv ? "preadv" : "readv";

  /* The prototype for buffers is \[@$] so that we can be called either with
   * @buffers or $buffers. @buffers gives us an array reference. $buffers
   * gives us a reference to a scalar (which in return is hopefully an array
   * reference). In the latter case we need to resolve the argument twice to
   * get the array. */
  for (i = 0; i < 2; i++) {
    if (SvROK(buffers)) {
      buffers = SvRV(buffers);
      if (SvTRULYREADONLY(buffers))
        croak("%s::%s: Can't modify read-only 'buffers'", PACKNAME, func);
      if (SvTYPE(buffers) == SVt_PVAV)
        break;
      if (i == 0) {
        if (!SvOK(buffers)) { /* Turn plain "my $buf" into array ref. */
#if PERL_BCDVERSION >= 0x5035004
          sv_setrv_noinc(buffers, (SV*)newAV());
#else
          /* Since Perl 5.12 references are SVt_IV (and SVt_RV is just an
           * alias), but before that SVt_RV was a separate type and DEBUGGING
           * perl whines 'Assertion ((svtype)((buffers)->sv_flags & 0xff))
           * >= SVt_RV failed: file "2008.xs"' */
#  if PERL_BCDVERSION < 0x5012000
          sv_upgrade(buffers, SVt_RV);
#  else
          sv_upgrade(buffers, SVt_IV);
#  endif
          SvOK_off(buffers);
          SvRV_set(buffers, (SV*)newAV());
          SvROK_on(buffers);
#endif
        }
        continue;
      }
    }
    croak("%s::%s: 'buffers' is not an array or array ref", PACKNAME, func);
  }

  iovcnt = av_count(sizes);
  if (iovcnt > PERL_INT_MAX) {
    SETERRNO(EINVAL, LIB_INVARG);
    return -1;
  }

  iov = safecalloc(iovcnt, sizeof(struct iovec));
  if (!iov && iovcnt) {
    errno = ENOMEM;
    return -1;
  }
  SAVEFREEPV(iov);

  for (i = 0; i < iovcnt; i++) {
    STRLEN iov_len;
    void *iov_base;
    SV **size = av_fetch(sizes, i, 0);
    if (!size)
      continue;
    if (UNLIKELY(SvNEGATIVE(*size))) { /* Performs 'get' magic. */
      _free_iov(iov, i);
      croak("%s::%s: Can't handle negative count: sizes[%" UVuf "] = %" SVf_QUOTEDPREFIX,
            PACKNAME, func, (UV)i, SVfARG(*size));
    }
    iov_len = SvSTRLEN(*size);
    if (!iov_len)
      continue;
    if (iov_len > SSIZE_MAX) {
      _free_iov(iov, i);
      SETERRNO(EINVAL, LIB_INVARG);
      return -1;
    }
    iov_base = safemalloc(TopUpLEN(iov_len));
    if (!iov_base) {
      _free_iov(iov, i);
      errno = ENOMEM;
      return -1;
    }
    iov[i].iov_base = iov_base;
    iov[i].iov_len = (Size_t)iov_len;
  }

  if (offset_sv == NULL) {
#ifdef PSX2008_HAS_READV
    rv = readv(fd, iov, iovcnt);
#else
    rv = -1;
    errno = ENOSYS;
#endif
  }
  else if (flags_sv == NULL) {
#ifdef PSX2008_HAS_PREADV
    Off_t offset = SvUNDEF_purposely(offset_sv) ? 0 : SvOFFt(offset_sv);
    rv = preadv(fd, iov, iovcnt, offset);
#else
    rv = -1;
    errno = ENOSYS;
#endif
  }
  else {
#ifdef PSX2008_HAS_PREADV2
    Off_t offset = SvUNDEF_purposely(offset_sv) ? 0 : SvOFFt(offset_sv);
    int flags = SvUNDEF_purposely(flags_sv) ? 0 : (int)SvIV(flags_sv);
    rv = preadv2(fd, iov, iovcnt, offset, flags);
#else
    rv = -1;
    errno = ENOSYS;
#endif
  }

  if (UNLIKELY(rv == -1)) {
    _free_iov(iov, iovcnt);
    return rv;
  }

  av_extend((AV*)buffers, iovcnt);

  {
    SV *tmp_sv;
    size_t sv_len;
    size_t bytes_left = (size_t)rv;
    for (i = 0; i < iovcnt; i++) {
      size_t iov_len = iov[i].iov_len;
      if (bytes_left >= iov_len)
        /* Current buffer filled completely (this includes an empty buffer). */
        sv_len = iov_len;
      else
        /* Current buffer filled partly. */
        sv_len = bytes_left;
      bytes_left -= sv_len;

      tmp_sv = sv_len ? newSV_type(SVt_PV) : newSVpvn("", 0);
      if (!tmp_sv) {
        _free_iov(iov + i, iovcnt - i);
        errno = ENOMEM;
        return -1;
      }

      if (sv_len) {
        char *iov_base = (char*)iov[i].iov_base;
        iov_base[sv_len] = '\0';
        SvPV_set(tmp_sv, iov_base);
        SvCUR_set(tmp_sv, sv_len);
        SvLEN_set(tmp_sv, TopUpLEN(iov_len));
        SvPOK_on(tmp_sv);
        SvTAINTED_on(tmp_sv);
      }

      if (!av_store((AV*)buffers, i, tmp_sv)) {
        SvREFCNT_dec(tmp_sv);
        if (SvMAGICAL(buffers))
          mg_set(tmp_sv);
      }
    }
  }

  return rv;
}
#endif

#ifdef PSX2008_HAS_WRITEV
static int
_psx_av2iov(pTHX_ AV *buffers, struct iovec **iov_dest)
{
  Size_t i, iovcnt;
  struct iovec *iov;

  iovcnt = av_count(buffers);
  if (iovcnt > PERL_INT_MAX) {
    SETERRNO(EINVAL, LIB_INVARG);
    return -1;
  }

  iov = safecalloc(iovcnt, sizeof(struct iovec));
  if (!iov && iovcnt) {
    errno = ENOMEM;
    return -1;
  }
  SAVEFREEPV(iov);

  for (i = 0; i < iovcnt; i++) {
    SV **av_elt = av_fetch(buffers, i, 0);
    if (av_elt) {
      STRLEN iov_len;
      iov[i].iov_base = (void*)SvPV(*av_elt, iov_len);
      iov[i].iov_len = (Size_t)iov_len;
      if (iov_len > SSIZE_MAX) {
        SETERRNO(EINVAL, LIB_INVARG);
        return -1;
      }
    }
  }

  *iov_dest = iov;
  return iovcnt;
}
#endif

#ifdef PSX2008_HAS_OPENAT
/* Convert open() flags to POSIX "r", "a", "w" mode string. */
static const char *
_flags2raw(int flags)
{
  int accmode = flags & O_ACCMODE;
  if (accmode == O_RDONLY)
    return "rb";
#ifdef O_APPEND
  if (flags & O_APPEND)
    return (accmode == O_WRONLY) ? "ab" : "a+b";
#endif
  if (accmode == O_WRONLY)
    return "wb";
  if (accmode == O_RDWR)
    return "r+b";
  return "";
}
#endif

#if defined(PSX2008_HAS_FDOPEN) || defined(PSX2008_HAS_FDOPENDIR)
static SV *
_psx_fd_to_handle(pTHX_ int fd, const char *mode)
{
  SV *rv;
  GV *gv;
  int return_handle = 0;

  gv = newGVgen(PACKNAME);
  if (gv) {
    if (mode) {
# ifdef PSX2008_HAS_FDOPEN
      FILE *filep = fdopen(fd, mode);
      if (filep) {
        PerlIO *pio = PerlIO_importFILE(filep, mode);
        if (pio) {
          if (do_open(gv, "+<&", 3, FALSE, 0, 0, pio))
            return_handle = 1;
          else
            PerlIO_releaseFILE(pio, filep);
        }
      }
# else
      errno = ENOSYS;
# endif
    }
    else {
# ifdef PSX2008_HAS_FDOPENDIR
      DIR *dirp = fdopendir(fd);
      if (dirp) {
        IO *io = GvIOn(gv);
        IoDIRP(io) = dirp;
        return_handle = 1;
      }
# else
      errno = ENOSYS;
# endif
    }
  }

  if (return_handle) {
    const char *io_class = mode ? "IO::File" : "IO::Dir";
    rv = newRV_inc((SV*)gv);
    rv = sv_bless(rv, gv_stashpv(io_class, 0));
    rv = sv_2mortal(rv);
  }
  else
    rv = NULL;

  /* https://github.com/Perl/perl5/issues/9493 */
  if (gv)
    (void) hv_delete(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), G_DISCARD);

  return rv;
}
#endif

#ifdef PSX2008_NEED_PSX_FILENO
static int
_psx_fileno(pTHX_ SV *sv)
{
  IO *io;
  int fn = -1;

  /* On Solaris, AT_FDCWD is 0xffd19553 (4291925331), so don't do any integer
   * range checks, just cast the SvIV to int and may the --force be with you.
   * https://github.com/python/cpython/issues/60169
   */
  if (SvOK(sv)) {
    if (_psx_looks_like_number(aTHX_ sv))
      fn = (int)SvIV(sv);
    else if ((io = sv_2io(sv))) {
      /* Magic part taken from Perl 5.8.9's pp_fileno. */
      const MAGIC *mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar);
      if (mg) {
        dSP;
        PUSHMARK(SP);
        XPUSHs(SvTIED_obj((SV*)io, mg));
        PUTBACK;
        ENTER;
        call_method("FILENO", G_SCALAR);
        LEAVE;
        SPAGAIN;
        fn = (int)POPi;
        PUTBACK;
      }
      else if (IoIFP(io))  /* from open() or sysopen() */
        fn = PerlIO_fileno(IoIFP(io));
      else if (IoDIRP(io)) {  /* from opendir() */
#if defined(HAS_DIRFD) || defined(HAS_DIR_DD_FD)
        fn = my_dirfd(IoDIRP(io));
#endif
      }
    }
  }

  return fn;
}
#endif

#ifdef PSX2008_HAS_CLOSE
static int
_psx_close(pTHX_ SV *sv)
{
  IO *io;
  int rv = -1;

  SvGETMAGIC(sv);
  if (!SvOK(sv))
    SETERRNO(EBADF, RMS_IFI);
  else if (_psx_looks_like_number(aTHX_ sv))
    rv = close((int)SvIV(sv));
  else if ((io = sv_2io(sv))) {
    if (IoIFP(io))
      rv = PerlIO_close(IoIFP(io));
    else if (IoDIRP(io)) {
#ifdef VOID_CLOSEDIR
      SETERRNO(0, 0);
      PerlDir_close(IoDIRP(io));
      rv = errno ? -1 : 0;
#else
      rv = PerlDir_close(IoDIRP(io));
#endif
      IoDIRP(io) = 0;
    }
    else
      SETERRNO(EBADF, RMS_IFI);
  }
  else
    SETERRNO(EBADF, RMS_IFI);

  return rv;
}
#endif

#ifdef PSX2008_HAS_OPENAT
static SV *
_openat50c(pTHX_ SV *dirfdsv,
           const char *path, int flags, mode_t mode, HV *how_hv)
{
  int got_fd, dir_fd, path_fd;
  struct stat st;

#ifndef PSX2008_HAS_OPENAT2
  if (how_hv != NULL) {
    errno = ENOSYS;
    return NULL;
  }
#endif

  SvGETMAGIC(dirfdsv);
  if (!SvOK(dirfdsv))
    dir_fd = -1;
  else if (SvROK(dirfdsv) && SvTYPE(SvRV(dirfdsv)) == SVt_IV) {
    /* Allow dirfdsv to be a reference to AT_FDCWD to get a file handle
     * instead of a file descriptor. */
    if (SvIV(SvRV(dirfdsv)) != (IV)AT_FDCWD)
      dir_fd = -1;
    else {
      got_fd = 0;
      dir_fd = (int)AT_FDCWD;
    }
  }
  else {
    got_fd = _psx_looks_like_number(aTHX_ dirfdsv);
    dir_fd = _psx_fileno(aTHX_ dirfdsv);
  }

  if (dir_fd == -1) {
    SETERRNO(EBADF, RMS_IFI);
    path_fd = -1;
  }
  else if (how_hv == NULL) {  /* openat() */
    path_fd = openat(dir_fd, path, flags, mode);
  }
#ifdef PSX2008_HAS_OPENAT2
  /* openat2() */
  else {
    SV** how_flags = hv_fetchs(how_hv, "flags", 0);
    SV** how_mode = hv_fetchs(how_hv, "mode", 0);
    SV** how_resolve = hv_fetchs(how_hv, "resolve", 0);
    struct open_how how = {
      .flags   = how_flags ? SvUV(*how_flags) : 0,
      .mode    = how_mode ? SvUV(*how_mode) : 0,
      .resolve = how_resolve ? SvUV(*how_resolve) : 0
    };
    flags = (int)how.flags; /* Needed for _psx_fd_to_handle() below. */
    path_fd = syscall(SYS_openat2, dir_fd, path, &how, sizeof(how));
  }
#endif

  if (path_fd < 0)
    return NULL;
  else if (got_fd)
    /* If we were passed a file descriptor, return a file descriptor. */
    return sv_2mortal(newSViv((IV)path_fd));
  else if (fstat(path_fd, &st) != 0)
    return NULL;
  else {
    const char *raw = S_ISDIR(st.st_mode) ? NULL : _flags2raw(flags);
    return _psx_fd_to_handle(aTHX_ path_fd, raw);
  }
}

#endif

/* Macro for isalnum, isdigit, etc.
 * Contains the fix for https://github.com/Perl/perl5/issues/11148 which was
 * "solved" by them Perl guys by cowardly removing the functions from POSIX.
 */
#define ISFUNC(isfunc) {                                          \
    STRLEN len;                                                   \
    unsigned char *s = (unsigned char *) SvPV(charstring, len);   \
    unsigned char *e = s + len;                                   \
    for (RETVAL = len ? 1 : 0; RETVAL && s < e; s++)              \
      if (!isfunc(*s))                                            \
        RETVAL = 0;                                               \
  }

MODULE = POSIX::2008    PACKAGE = POSIX::2008

PROTOTYPES: ENABLE

INCLUDE: const-xs.inc

#ifdef PSX2008_HAS_A64L
long
a64l(char* s);

#endif

#ifdef PSX2008_HAS_L64A
char *
l64a(long value);

#endif

#ifdef PSX2008_HAS_ABORT
void
abort();

#endif

#ifdef PSX2008_HAS_ALARM
UV
alarm(UV seconds);

#endif

#ifdef PSX2008_HAS_ATOF
NV
atof(const char *str);

#endif

#ifdef PSX2008_ATOI
IV
atoi(const char *str);
  CODE:
    RETVAL = PSX2008_ATOI(str);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_BASENAME
char *
basename(char *path);

#endif

#ifdef PSX2008_HAS_CATCLOSE
SysRetTrue
catclose(nl_catd catd);

#endif

#ifdef PSX2008_HAS_CATGETS
char *
catgets(nl_catd catd, int set_id, int msg_id, const char *dflt);

#endif

#ifdef PSX2008_HAS_CATOPEN
nl_catd
catopen(const char *name, int oflag);

#endif

#ifdef PSX2008_HAS_CLOCK
clock_t
clock();

#endif

#ifdef PSX2008_HAS_CLOCK_GETCPUCLOCKID
void
clock_getcpuclockid(pid_t pid=0);
  INIT:
    int rv;
    clockid_t clock_id;
  PPCODE:
    rv = clock_getcpuclockid(pid, &clock_id);
    if (LIKELY(rv == 0))
      PUSH_INT_OR_PV(clock_id);
    else {
      PUSHs(&PL_sv_undef);
      errno = rv;
    }

#endif

#define PUSH_TIMESPEC(_tspec) STMT_START {                  \
    switch (GIMME_V) {                                      \
      case G_SCALAR:                                        \
        mPUSHn(_tspec.tv_sec + _tspec.tv_nsec/(NV)1e9);     \
        break;                                              \
      case G_LIST:                                          \
        EXTEND(SP, 2);                                      \
        mPUSHi(_tspec.tv_sec);                              \
        mPUSHi(_tspec.tv_nsec);                             \
    }                                                       \
} STMT_END

#ifdef PSX2008_HAS_CLOCK_GETRES
void
clock_getres(clockid_t clock_id=CLOCK_REALTIME);
  ALIAS:
    clock_gettime = 1
  INIT:
    int rv;
    struct timespec res;
  PPCODE:
    rv = (!ix) ? clock_getres(clock_id, &res) : clock_gettime(clock_id, &res);
    if (rv == 0)
      PUSH_TIMESPEC(res);

#endif

#define LOOKS_LIKE_NV(_sv)                                              \
  (SvNOK(_sv) ||                                                        \
   (                                                                    \
    (SvPOK(_sv) || SvPOKp(_sv))                                         \
    && (_psx_looks_like_number(aTHX_ (_sv)) & IS_NUMBER_NOT_INT)        \
   )                                                                    \
  )

#define TIMESPEC_FROM_IV(_tspec, sec_sv, nsec_long) STMT_START {        \
    _tspec.tv_sec = (time_t)SvIV(sec_sv);                               \
    _tspec.tv_nsec = nsec_long;                                         \
} STMT_END

#define TIMESPEC_FROM_NV(_tspec, sec_sv) STMT_START {   \
    NV sec_nv = SvNV(sec_sv);                           \
    _tspec.tv_sec = (time_t)sec_nv;                     \
    _tspec.tv_nsec = (sec_nv - _tspec.tv_sec)*1e9;      \
} STMT_END

#ifdef PSX2008_HAS_CLOCK_NANOSLEEP
void
clock_nanosleep(clockid_t clock_id, int flags, SV *sec, long nsec=0);
  PROTOTYPE: $$@
  INIT:
    int rv;
    struct timespec request;
    struct timespec remain = { 0, 0 };
  PPCODE:
    SvGETMAGIC(sec);
    if (items == 3 && LOOKS_LIKE_NV(sec))
      TIMESPEC_FROM_NV(request, sec);
    else
      TIMESPEC_FROM_IV(request, sec, nsec);
    rv = clock_nanosleep(clock_id, flags, &request, &remain);
    if (rv == 0 || ((errno = rv) == EINTR))
      PUSH_TIMESPEC(remain);

#endif

#ifdef PSX2008_HAS_CLOCK_SETTIME
void
clock_settime(clockid_t clock_id, SV *sec, long nsec=0);
  PROTOTYPE: $@
  INIT:
    struct timespec tp;
  PPCODE:
    SvGETMAGIC(sec);
    if (items == 2 && LOOKS_LIKE_NV(sec))
      TIMESPEC_FROM_NV(tp, sec);
    else
      TIMESPEC_FROM_IV(tp, sec, nsec);
    if (clock_settime(clock_id, &tp) == 0)
      mPUSHp("0 but true", 10);
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_NANOSLEEP
void
nanosleep(SV *sec, long nsec=0);
  PROTOTYPE: @
  INIT:
    struct timespec request;
    struct timespec remain = { 0, 0 };
  PPCODE:
    SvGETMAGIC(sec);
    if (items == 1 && LOOKS_LIKE_NV(sec))
      TIMESPEC_FROM_NV(request, sec);
    else
      TIMESPEC_FROM_IV(request, sec, nsec);
    if (nanosleep(&request, &remain) == 0 || errno == EINTR)
      PUSH_TIMESPEC(remain);

#endif

#ifdef PSX2008_HAS_DIRNAME
char *
dirname(char *path);

#endif

#ifdef PSX2008_HAS_DLCLOSE
SysRetTrue
dlclose(void *handle);

#endif

#ifdef PSX2008_HAS_DLERROR
char *
dlerror();

#endif

#ifdef PSX2008_HAS_DLOPEN
void *
dlopen(const char *file, int mode);

#endif

#ifdef PSX2008_HAS_DLSYM
void *
dlsym(void *handle, const char *name);

#endif

#ifdef PSX2008_HAS_FEGETROUND
int
fegetround();

#endif

#ifdef PSX2008_HAS_FESETROUND
SysRetTrue
fesetround(int rounding_mode);

#endif

#ifdef PSX2008_HAS_FECLEAREXCEPT
SysRetTrue
feclearexcept(int excepts);

#endif

#ifdef PSX2008_HAS_FERAISEEXCEPT
SysRetTrue
feraiseexcept(int excepts);

#endif

#ifdef PSX2008_HAS_FETESTEXCEPT
int
fetestexcept(int excepts);

#endif

#ifdef PSX2008_FFS
IV
ffs(IV i);
  CODE:
    RETVAL = PSX2008_FFS(i);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_FNMATCH
void
fnmatch(const char *pattern, const char *string, int flags);
  INIT:
    int rv;
  PPCODE:
    rv = fnmatch(pattern, string, flags);
    if (LIKELY(rv == 0 || rv == FNM_NOMATCH))
      mPUSHi(rv);
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_KILLPG
SysRetTrue
killpg(pid_t pgrp, int sig);

#endif

#ifdef PSX2008_HAS_RAISE
SysRetTrue
raise(int sig);

#endif

#ifdef PSX2008_HAS_GETDATE
void
getdate(const char *string);
    INIT:
        struct tm *tm = getdate(string);
    PPCODE:
        if (tm != NULL) {
            EXTEND(SP, 9);
            mPUSHi(tm->tm_sec);
            mPUSHi(tm->tm_min);
            mPUSHi(tm->tm_hour);
            mPUSHi(tm->tm_mday);
            mPUSHi(tm->tm_mon);
            mPUSHi(tm->tm_year);
            mPUSHi(tm->tm_wday);
            mPUSHi(tm->tm_yday);
            mPUSHi(tm->tm_isdst);
        }

#endif

#ifdef PSX2008_HAS_GETDATE_ERR
int
getdate_err();
    CODE:
        RETVAL = getdate_err;
    OUTPUT:
        RETVAL

#endif

#ifdef PSX2008_HAS_STRPTIME
void
strptime(const char *s, const char *format, ...);
  PROTOTYPE: $$@
  INIT:
    struct tm tm = {
      INT_MIN, INT_MIN, INT_MIN,
      INT_MIN, INT_MIN, INT_MIN,
      INT_MIN, INT_MIN, INT_MIN,
    };
    char *remainder;
    size_t i, tm_count;
    AV *tm_av = NULL;
    U8 gimme = GIMME_V;
  PPCODE:
  {
    if (items > 2) {
      SV *tm_arg = ST(2l);
      SvGETMAGIC(tm_arg);
      if (SvROK(tm_arg) && SvTYPE(SvRV(tm_arg)) == SVt_PVAV) {
        if (items == 3)
          tm_av = (AV*)SvRV(tm_arg);
        else
          croak("%s::strptime: Unexpected argument after %s", PACKNAME, "tm");
      }
      else if (items > 11)
        croak("%s::strptime: Unexpected argument after %s", PACKNAME, "isdst");
    }

    tm_count = tm_av ? av_count(tm_av) : (size_t)items - 2;
    if (tm_count > 9)
      tm_count = 9;

    for (i = 0; i < tm_count; i++) {
      SV *tm_sv;
      if (!tm_av)
        tm_sv = ST(i+2);
      else {
        SV **av_elt = av_fetch(tm_av, i, 0);
        if (!av_elt)
          continue;
        tm_sv = *av_elt;
      }
      SvGETMAGIC(tm_sv);
      if (SvOK(tm_sv)) {
        int tm_int = (int)SvIV(tm_sv);
        switch(i) {
          case 0: tm.tm_sec = tm_int; break;
          case 1: tm.tm_min = tm_int; break;
          case 2: tm.tm_hour = tm_int; break;
          case 3: tm.tm_mday = tm_int; break;
          case 4: tm.tm_mon = tm_int; break;
          case 5: tm.tm_year = tm_int; break;
          case 6: tm.tm_wday = tm_int; break;
          case 7: tm.tm_yday = tm_int; break;
          case 8: tm.tm_isdst = tm_int; break;
        }
      }
    }

    remainder = strptime(s, format, &tm);
    if (!remainder) {
      if (gimme == G_SCALAR)
        PUSHs(&PL_sv_undef);
    }
    else {
      if (tm_av) {
        av_extend(tm_av, 8);
        for (i = 0; i < 9; i++) {
          int tm_int;
          switch(i) {
            case 0: tm_int = tm.tm_sec; break;
            case 1: tm_int = tm.tm_min; break;
            case 2: tm_int = tm.tm_hour; break;
            case 3: tm_int = tm.tm_mday; break;
            case 4: tm_int = tm.tm_mon; break;
            case 5: tm_int = tm.tm_year; break;
            case 6: tm_int = tm.tm_wday; break;
            case 7: tm_int = tm.tm_yday; break;
            case 8: tm_int = tm.tm_isdst; break;
          }
          if (tm_int != INT_MIN) {
            SV *tm_sv = newSViv(tm_int);
            if (!av_store((AV*)tm_av, i, tm_sv)) {
              SvREFCNT_dec(tm_sv);
              if (SvMAGICAL(tm_av))
                mg_set(tm_sv);
            }
          }
        }
      }
      switch(gimme) {
        case G_SCALAR: mPUSHs(newSViv(remainder - s)); break;
        case G_LIST: {
          SV *undef = &PL_sv_undef;
          EXTEND(SP, 8);
          for (i = 0; i < 9; i++) {
            int tm_int;
            switch(i) {
              case 0: tm_int = tm.tm_sec; break;
              case 1: tm_int = tm.tm_min; break;
              case 2: tm_int = tm.tm_hour; break;
              case 3: tm_int = tm.tm_mday; break;
              case 4: tm_int = tm.tm_mon; break;
              case 5: tm_int = tm.tm_year; break;
              case 6: tm_int = tm.tm_wday; break;
              case 7: tm_int = tm.tm_yday; break;
              case 8: tm_int = tm.tm_isdst; break;
            }
            PUSHs(tm_int == INT_MIN ? undef : sv_2mortal(newSViv(tm_int)));
          }
        }
      }
    }
  }

#endif

#ifdef PSX2008_HAS_GETHOSTID
long
gethostid();

#endif

#ifdef PSX2008_HAS_GETHOSTNAME
void
gethostname();
  INIT:
#if !defined(MAXHOSTNAMELEN) || MAXHOSTNAMELEN < 256
    char name[256];
#else
    char name[MAXHOSTNAMELEN];
#endif
  PPCODE:
    if (LIKELY(gethostname(name, sizeof(name)) == 0))
      mPUSHp(name, strnlen(name, sizeof(name)));
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_GETITIMER
void
getitimer(int which);
    INIT:
        struct itimerval value;
    PPCODE:
        if (getitimer(which, &value) == 0) {
            EXTEND(SP, 4);
            mPUSHi(value.it_interval.tv_sec);
            mPUSHi(value.it_interval.tv_usec);
            mPUSHi(value.it_value.tv_sec);
            mPUSHi(value.it_value.tv_usec);
        }

#endif

#ifdef PSX2008_HAS_SETITIMER
void
setitimer(int which,                      \
          time_t int_sec, long int_usec,  \
          time_t val_sec, long val_usec);
    PROTOTYPE: $@
    INIT:
        struct itimerval value = { {int_sec, int_usec}, {val_sec, val_usec} };
        struct itimerval ovalue;
    PPCODE:
        if (setitimer(which, &value, &ovalue) == 0) {
            EXTEND(SP, 4);
            mPUSHi(ovalue.it_interval.tv_sec);
            mPUSHi(ovalue.it_interval.tv_usec);
            mPUSHi(ovalue.it_value.tv_sec);
            mPUSHi(ovalue.it_value.tv_usec);
        }

#endif

#ifdef PSX2008_HAS_NICE
void
nice(int incr);
  PREINIT:
    int rv;
  PPCODE:
  {
    SETERRNO(0, 0);
    rv = nice(incr);
    if (rv != -1 || errno == 0)
      mPUSHi(rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_GETPRIORITY
void
getpriority(int which=PRIO_PROCESS, id_t who=0);
  PREINIT:
    int rv;
  PPCODE:
  {
    SETERRNO(0, 0);
    rv = getpriority(which, who);
    if (rv != -1 || errno == 0)
      mPUSHi(rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_SETPRIORITY
SysRetTrue
setpriority(int prio, int which=PRIO_PROCESS, id_t who=0);

#endif

#ifdef PSX2008_HAS_GETSID
pid_t
getsid(pid_t pid=0);

#endif

#ifdef PSX2008_HAS_SETSID
pid_t
setsid();

#endif

#define RETURN_UTXENT {                                                 \
    if (utxent != NULL) {                                               \
      EXTEND(SP, 7);                                                    \
      mPUSHp(utxent->ut_user, strnlen(utxent->ut_user, sizeof(utxent->ut_user))); \
      mPUSHp(utxent->ut_id,   strnlen(utxent->ut_id,   sizeof(utxent->ut_id  ))); \
      mPUSHp(utxent->ut_line, strnlen(utxent->ut_line, sizeof(utxent->ut_line))); \
      mPUSHi(utxent->ut_pid);                                           \
      mPUSHi(utxent->ut_type);                                          \
      mPUSHi(utxent->ut_tv.tv_sec);                                     \
      mPUSHi(utxent->ut_tv.tv_usec);                                    \
    }                                                                   \
}

#ifdef PSX2008_HAS_ENDUTXENT
void
endutxent();

#endif

#ifdef PSX2008_HAS_GETUTXENT
void
getutxent();
  INIT:
    struct utmpx *utxent = getutxent();
  PPCODE:
    RETURN_UTXENT;

#endif

#ifdef PSX2008_HAS_GETUTXID
void
getutxid(short ut_type, char *ut_id=NULL);
  INIT:
    struct utmpx *utxent;
    struct utmpx utxent_req = {0};
  PPCODE:
    utxent_req.ut_type = ut_type;
    if (ut_id != NULL) {
      memcpy(utxent_req.ut_id, ut_id,
             strnlen(ut_id, sizeof(utxent_req.ut_id)));
    }
    utxent = getutxline(&utxent_req);
    RETURN_UTXENT;

#endif

#ifdef PSX2008_HAS_GETUTXLINE
void
getutxline(char *ut_line);
  INIT:
    struct utmpx *utxent;
    struct utmpx utxent_req = {0};
  PPCODE:
    if (ut_line != NULL) {
      memcpy(utxent_req.ut_line, ut_line,
             strnlen(ut_line, sizeof(utxent_req.ut_line)));
      utxent = getutxline(&utxent_req);
      RETURN_UTXENT;
    }

#endif

#ifdef PSX2008_HAS_SETUTXENT
void
setutxent();

#endif

#ifdef PSX2008_HAS_DRAND48
NV
drand48();

#endif

#ifdef PSX2008_HAS_ERAND48
void
erand48(unsigned short X0, unsigned short X1, unsigned short X2);
    INIT:
        unsigned short xsubi[3] = { X0, X1, X2 };
        double result = erand48(xsubi);
    PPCODE:
        EXTEND(SP, 4);
        mPUSHn(result);
        mPUSHu(xsubi[0]);
        mPUSHu(xsubi[1]);
        mPUSHu(xsubi[2]);

#endif

#ifdef PSX2008_HAS_JRAND48
void
jrand48(unsigned short X0, unsigned short X1, unsigned short X2);
    ALIAS:
        nrand48 = 1
    INIT:
        unsigned short xsubi[3] = { X0, X1, X2 };
        long result = ix == 0 ? jrand48(xsubi) : nrand48(xsubi);
    PPCODE:
        EXTEND(SP, 4);
        mPUSHi(result);
        mPUSHu(xsubi[0]);
        mPUSHu(xsubi[1]);
        mPUSHu(xsubi[2]);

#endif

#ifdef PSX2008_HAS_LRAND48
long
lrand48();

#endif

#ifdef PSX2008_HAS_MRAND48
long
mrand48();

#endif

#ifdef PSX2008_HAS_SEED48
void
seed48(unsigned short seed1, unsigned short seed2, unsigned short seed3);
    INIT:
        unsigned short seed16v[3] = { seed1, seed2, seed3 };
        unsigned short *old = seed48(seed16v);
    PPCODE:
        EXTEND(SP, 3);
        mPUSHu(old[0]);
        mPUSHu(old[1]);
        mPUSHu(old[2]);

#endif

#ifdef PSX2008_HAS_SRAND48
void
srand48(long seedval);

#endif

#ifdef PSX2008_HAS_RANDOM
long
random();

#endif

#ifdef PSX2008_HAS_SRANDOM
void
srandom(unsigned seed);

#endif

#ifdef PSX2008_HAS_GETEGID
gid_t
getegid();

#endif

#ifdef PSX2008_HAS_GETEUID
uid_t
geteuid();

#endif

#ifdef PSX2008_HAS_GETGID
gid_t
getgid();

#endif

#ifdef PSX2008_HAS_GETUID
uid_t
getuid();

#endif

#ifdef PSX2008_HAS_SETEGID
SysRetTrue
setegid(gid_t gid);

#endif

#ifdef PSX2008_HAS_SETEUID
SysRetTrue
seteuid(uid_t uid);

#endif

#ifdef PSX2008_HAS_SETGID
SysRetTrue
setgid(gid_t gid);

#endif

#ifdef PSX2008_HAS_SETREGID
SysRetTrue
setregid(gid_t rgid, gid_t egid);

#endif

#ifdef PSX2008_HAS_SETREUID
SysRetTrue
setreuid(uid_t ruid, uid_t euid);

#endif

#ifdef PSX2008_HAS_SETUID
SysRetTrue
setuid(uid_t uid);

#endif

#ifdef PSX2008_HAS_SIGHOLD
SysRetTrue
sighold(int sig);

#endif

#ifdef PSX2008_HAS_SIGIGNORE
SysRetTrue
sigignore(int sig);

#endif

#ifdef PSX2008_HAS_SIGPAUSE
SysRetTrue
sigpause(int sig);

#endif

#ifdef PSX2008_HAS_SIGRELSE
SysRetTrue
sigrelse(int sig);

#endif

#ifdef PSX2008_HAS_PSIGNAL
void
psignal(int sig, const char *msg);

#endif

#ifdef PSX2008_HAS_STRSIGNAL
char*
strsignal(int sig);

#endif

#ifdef PSX2008_HAS_TIMER_CREATE
timer_t
timer_create(clockid_t clockid, SV *sig = NULL);
  PREINIT:
    struct sigevent sevp = {0};
    timer_t timerid;
    int rv;
  CODE:
  {
    if (sig) {
      SvGETMAGIC(sig);
      sevp.sigev_notify = SIGEV_SIGNAL;
      sevp.sigev_signo = SvIV(sig);
    }
    else {
      sevp.sigev_notify = SIGEV_NONE;
    }
    rv = timer_create(clockid, &sevp, &timerid);
    RETVAL = (rv == 0) ? timerid : (timer_t)0;
  }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_TIMER_DELETE
SysRetTrue
timer_delete(timer_t timerid);

#endif

#ifdef PSX2008_HAS_TIMER_GETOVERRUN
SysRet0
timer_getoverrun(timer_t timerid);

#endif

#ifdef PSX2008_HAS_TIMER_GETTIME
void
timer_gettime(timer_t timerid);
  PREINIT:
    struct itimerspec curr_value;
    int rv;
  PPCODE:
  {
    rv = timer_gettime(timerid, &curr_value);
    if (rv == 0) {
      EXTEND(SP, 4);
      mPUSHi(curr_value.it_interval.tv_sec);
      mPUSHi(curr_value.it_interval.tv_nsec);
      mPUSHi(curr_value.it_value.tv_sec);
      mPUSHi(curr_value.it_value.tv_nsec);
    }
  }

#endif

#ifdef PSX2008_HAS_TIMER_SETTIME
void
timer_settime(timer_t timerid, int flags,                               \
              time_t interval_sec, long interval_nsec,                  \
              time_t initial_sec=-1, long initial_nsec=-1);
  PROTOTYPE: $$@
  PREINIT:
    struct itimerspec new_value, old_value;
    int rv;
  PPCODE:
  {
    new_value.it_interval.tv_sec = interval_sec;
    new_value.it_interval.tv_nsec = interval_nsec;
    if (initial_sec < 0 || initial_nsec < 0)
      new_value.it_value = new_value.it_interval;
    else {
      new_value.it_value.tv_sec = initial_sec;
      new_value.it_value.tv_nsec = initial_nsec;
    }

    rv = timer_settime(timerid, flags, &new_value, &old_value);
    if (rv == 0) {
      EXTEND(SP, 4);
      mPUSHi(old_value.it_interval.tv_sec);
      mPUSHi(old_value.it_interval.tv_nsec);
      mPUSHi(old_value.it_value.tv_sec);
      mPUSHi(old_value.it_value.tv_nsec);
    }
  }

#endif

## I/O-related functions
########################

#ifdef PSX2008_HAS_CHDIR
SysRetTrue
chdir(SV *what);
  CODE:
    SvGETMAGIC(what);
    if (!SvOK(what)) {
      errno = ENOENT;
      RETVAL = -1;
    }
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      RETVAL = chdir(path);
    }
    else {
#ifdef PSX2008_HAS_FCHDIR
      int fd = _psx_fileno(aTHX_ what);
      RETVAL = fchdir(fd);
#else
      errno = ENOSYS;
      RETVAL = -1;
#endif
    }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CHMOD
SysRetTrue
chmod(SV *what, mode_t mode);
  CODE:
    SvGETMAGIC(what);
    if (!SvOK(what)) {
      errno = ENOENT;
      RETVAL = -1;
    }
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      RETVAL = chmod(path, mode);
    }
    else {
#ifdef PSX2008_HAS_FCHMOD
      int fd = _psx_fileno(aTHX_ what);
      RETVAL = fchmod(fd, mode);
#else
      errno = ENOSYS;
      RETVAL = -1;
#endif
    }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CHOWN
SysRetTrue
chown(SV *what, uid_t owner, gid_t group);
  CODE:
    SvGETMAGIC(what);
    if (!SvOK(what)) {
      errno = ENOENT;
      RETVAL = -1;
    }
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      RETVAL = chown(path, owner, group);
    }
    else {
#ifdef PSX2008_HAS_FCHOWN
      int fd = _psx_fileno(aTHX_ what);
      RETVAL = fchown(fd, owner, group);
#else
      errno = ENOSYS;
      RETVAL = -1;
#endif
    }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_TRUNCATE
SysRetTrue
truncate(SV *what, Off_t length);
  CODE:
    SvGETMAGIC(what);
    if (!SvOK(what)) {
      errno = ENOENT;
      RETVAL = -1;
    }
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      RETVAL = truncate(path, length);
    }
    else {
#ifdef PSX2008_HAS_FTRUNCATE
      int fd = _psx_fileno(aTHX_ what);
      RETVAL = ftruncate(fd, length);
#else
      errno = ENOSYS;
      RETVAL = -1;
#endif
    }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_PATHCONF
void
pathconf(SV *what, int name);
  INIT:
    long rv = -1;
  PPCODE:
  {
    SETERRNO(0, 0);
    SvGETMAGIC(what);
    if (!SvOK(what))
      errno = ENOENT;
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      rv = pathconf(path, name);
    }
    else {
#ifdef PSX2008_HAS_FPATHCONF
      int fd = _psx_fileno(aTHX_ what);
      rv = fpathconf(fd, name);
#else
      errno = ENOSYS;
#endif
    }
    if (rv == -1 && errno != 0)
      PUSHs(&PL_sv_undef);
    else
      PUSH_INT_OR_PV(rv);
  }

#endif

#ifdef PSX2008_HAS_SYSCONF
void
sysconf(int name);
  INIT:
    long rv;
  PPCODE:
  {
    SETERRNO(0, 0);
    rv = sysconf(name);
    if (rv == -1 && errno != 0)
      PUSHs(&PL_sv_undef);
    else
      PUSH_INT_OR_PV(rv);
  }

#endif

#ifdef PSX2008_HAS_CONFSTR
char *
confstr(int name);
  INIT:
    size_t len;
  CODE:
  {
    SETERRNO(0, 0);
    len = confstr(name, NULL, 0);
    if (len) {
      RETVAL = safemalloc(len);
      if (RETVAL != NULL) {
        SAVEFREEPV(RETVAL);
        confstr(name, RETVAL, len);
      }
      else
        errno = ENOMEM;
    }
    else if (errno == 0)
      RETVAL = "";
    else
      RETVAL = NULL;
  }
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_LCHOWN
SysRetTrue
lchown(const char *path, uid_t owner, gid_t group);

#endif

#ifdef PSX2008_HAS_ACCESS
SysRetTrue
access(const char *path, int mode);

#endif

#ifdef PSX2008_HAS_FDATASYNC
SysRetTrue
fdatasync(psx_fd_t fd);

#endif

#ifdef PSX2008_HAS_FSYNC
SysRetTrue
fsync(psx_fd_t fd);

#endif

#ifdef PSX2008_HAS_STAT
void
stat(SV *what);
  INIT:
    int rv = -1;
    struct stat buf;
  PPCODE:
    SvGETMAGIC(what);
    if (!SvOK(what))
      errno = ENOENT;
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      rv = stat(path, &buf);
    }
    else {
#ifdef PSX2008_HAS_FSTAT
      int fd = _psx_fileno(aTHX_ what);
      rv = fstat(fd, &buf);
#else
      errno = ENOSYS;
#endif
    }
    RETURN_STAT_BUF(rv, buf);

#endif

#ifdef PSX2008_HAS_LSTAT
void
lstat(const char *path);
  INIT:
    int rv;
    struct stat buf;
  PPCODE:
    rv = lstat(path, &buf);
    RETURN_STAT_BUF(rv, buf);

#endif

#ifdef PSX2008_HAS_STATVFS
void
statvfs(SV *what);
  INIT:
    int rv = -1;
    struct statvfs buf;
  PPCODE:
    SvGETMAGIC(what);
    if (!SvOK(what))
      errno = ENOENT;
    else if (SvPOK(what)) {
      const char *path = SvPV_nomg_const_nolen(what);
      rv = statvfs(path, &buf);
    }
    else {
#ifdef PSX2008_HAS_FSTATVFS
      int fd = _psx_fileno(aTHX_ what);
      rv = fstatvfs(fd, &buf);
#else
      errno = ENOSYS;
#endif
    }
    RETURN_STATVFS_BUF(rv, buf);

#endif

#ifdef PSX2008_HAS_ISATTY
int
isatty(psx_fd_t fd);

#endif

#ifdef PSX2008_HAS_ISALNUM
int
isalnum(SV *charstring);
  CODE:
    ISFUNC(isalnum)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISALPHA
int
isalpha(SV *charstring);
  CODE:
    ISFUNC(isalpha)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISASCII
int
isascii(SV *charstring);
  CODE:
    ISFUNC(isascii)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISBLANK
int
isblank(SV *charstring);
  CODE:
    ISFUNC(isblank)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISCNTRL
int
iscntrl(SV *charstring);
  CODE:
    ISFUNC(iscntrl)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISDIGIT
int
isdigit(SV *charstring);
  CODE:
    ISFUNC(isdigit)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISGRAPH
int
isgraph(SV *charstring);
  CODE:
    ISFUNC(isgraph)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISLOWER
int
islower(SV *charstring);
  CODE:
    ISFUNC(islower)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISPRINT
int
isprint(SV *charstring);
  CODE:
    ISFUNC(isprint)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISPUNCT
int
ispunct(SV *charstring);
  CODE:
    ISFUNC(ispunct)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISSPACE
int
isspace(SV *charstring);
  CODE:
    ISFUNC(isspace)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISUPPER
int
isupper(SV *charstring);
  CODE:
    ISFUNC(isupper)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ISXDIGIT
int
isxdigit(SV *charstring);
  CODE:
    ISFUNC(isxdigit)
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_LINK
SysRetTrue
link(const char *oldpath, const char *newpath);

#endif

#ifdef PSX2008_HAS_MKDIR
SysRetTrue
mkdir(const char *path, mode_t mode=0777);

#endif

#ifdef PSX2008_HAS_MKFIFO
SysRetTrue
mkfifo(const char *path, mode_t mode);

#endif

#ifdef PSX2008_HAS_MKNOD
SysRetTrue
mknod(const char *path, mode_t mode, dev_t dev);

#endif

#ifdef PSX2008_HAS_MKDTEMP
void
mkdtemp(SV *template);
  PPCODE:
  {
    STRLEN len;
    const char *ctmp = SvPV_const(template, len);
    /* Copy the original template to avoid overwriting it. */
    SV *tmpsv = newSVpvn_flags(ctmp, len, SVs_TEMP);
    char *dtemp = mkdtemp(SvPVX(tmpsv));
    PUSHs(dtemp ? tmpsv : &PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_MKSTEMP
void
mkstemp(SV *template);
  PPCODE:
  {
    STRLEN len;
    const char *ctmp = SvPV_const(template, len);
    /* Copy the original template to avoid overwriting it. */
    SV *tmpsv = newSVpvn_flags(ctmp, len, SVs_TEMP);
    int fd = mkstemp(SvPVX(tmpsv));
    if (fd >= 0) {
      EXTEND(SP, 2);
      mPUSHi(fd);
      PUSHs(tmpsv);
    }
  }

#endif

#if defined(PSX2008_HAS_FDOPEN)
void
fdopen(IV fd, const char *mode);
  PPCODE:
  {
    SV *rv = NULL;
    if (UNLIKELY(fd < 0 || fd > PERL_INT_MAX))
      SETERRNO(EBADF, RMS_IFI);
    else if (UNLIKELY(!mode || !*mode))
      SETERRNO(EINVAL, LIB_INVARG);
    else
      rv = _psx_fd_to_handle(aTHX_ fd, mode);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif

#if defined(PSX2008_HAS_FDOPENDIR)
void
fdopendir(IV fd);
  PPCODE:
  {
    SV *rv = NULL;
    if (UNLIKELY(fd < 0 || fd > PERL_INT_MAX))
      SETERRNO(EBADF, RMS_IFI);
    else
      rv = _psx_fd_to_handle(aTHX_ fd, NULL);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif


##
## POSIX::open(), read() and write() return "0 but true" for 0, which
## is not quite what you would expect. We return a real 0.
##

#ifdef PSX2008_HAS_CREAT
SysRet0
creat(const char *path, mode_t mode=0666)

#endif

#ifdef PSX2008_HAS_OPEN
SysRet0
open(const char *path, int oflag=O_RDONLY, mode_t mode=0666);

#endif

#ifdef PSX2008_HAS_CLOSE
SysRetTrue
close(SV *fd);
    CODE:
        RETVAL = _psx_close(aTHX_ fd);
    OUTPUT:
        RETVAL

#endif

#ifdef PSX2008_HAS_FACCESSAT
SysRetTrue
faccessat(psx_fd_t dirfd, const char *path, int amode, int flags=0);

#endif

#ifdef PSX2008_HAS_FCHMODAT
SysRetTrue
fchmodat(psx_fd_t dirfd, const char *path, mode_t mode, int flags=0);

#endif

#ifdef PSX2008_HAS_FCHOWNAT
SysRetTrue
fchownat(psx_fd_t dirfd,                                                \
         const char *path, uid_t owner, gid_t group, int flags=0);

#endif

#ifdef PSX2008_HAS_FSTATAT
void
fstatat(psx_fd_t dirfd, const char *path, int flags=0);
  INIT:
    int rv;
    struct stat buf;
  PPCODE:
    rv = fstatat(dirfd, path, &buf, flags);
    RETURN_STAT_BUF(rv, buf);

#endif

#ifdef PSX2008_HAS_LINKAT
SysRetTrue
linkat(psx_fd_t olddirfd, const char *oldpath,                  \
       psx_fd_t newdirfd, const char *newpath, int flags=0);

#endif

#ifdef PSX2008_HAS_MKDIRAT
SysRetTrue
mkdirat(psx_fd_t dirfd, const char *path, mode_t mode);

#endif

#ifdef PSX2008_HAS_MKFIFOAT
SysRetTrue
mkfifoat(psx_fd_t dirfd, const char *path, mode_t mode);

#endif

#ifdef PSX2008_HAS_MKNODAT
SysRetTrue
mknodat(psx_fd_t dirfd, const char *path, mode_t mode, dev_t dev);

#endif

#ifdef PSX2008_HAS_OPENAT
void
openat(SV *dirfdsv, const char *path, int flags=O_RDONLY, mode_t mode=0666);
  PPCODE:
  {
    SV *rv = _openat50c(aTHX_ dirfdsv, path, flags, mode, NULL);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_OPENAT2
void
openat2(SV *dirfdsv, const char *path, HV *how);
  PPCODE:
  {
    SV *rv = _openat50c(aTHX_ dirfdsv, path, 0, 0, how);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_READLINK
void
readlink(const char *path);
  PPCODE:
  {
    SV *rv = _readlink50c(aTHX_ path, NULL);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_READLINKAT
void
readlinkat(psx_fd_t dirfd, const char *path);
  PPCODE:
  {
    SV *rv = _readlink50c(aTHX_ path, &dirfd);
    PUSHs(rv ? rv : &PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_REALPATH
char *
realpath(const char *path);
  CODE:
    RETVAL = realpath(path, NULL);
  OUTPUT:
    RETVAL
  CLEANUP:
    free(RETVAL);

#endif

#ifdef PSX2008_HAS_RENAMEAT
SysRetTrue
renameat(psx_fd_t olddirfd, const char *oldpath,        \
         psx_fd_t newdirfd, const char *newpath);

#endif

#ifdef PSX2008_HAS_RENAMEAT2
SysRetTrue
renameat2(psx_fd_t olddirfd, const char *oldpath,                       \
          psx_fd_t newdirfd, const char *newpath, unsigned int flags=0);

#endif

#ifdef PSX2008_HAS_SYMLINKAT
SysRetTrue
symlinkat(const char *target, psx_fd_t newdirfd, const char *linkpath);

#endif

#ifdef PSX2008_HAS_UNLINKAT
SysRetTrue
unlinkat(psx_fd_t dirfd, const char *path, int flags=0);

#endif

#ifdef PSX2008_HAS_UTIMENSAT
SysRetTrue
utimensat(psx_fd_t dirfd, const char *path, int flags = 0,      \
          time_t atime_sec = 0, long atime_nsec = UTIME_NOW,    \
          time_t mtime_sec = 0, long mtime_nsec = UTIME_NOW);
    PROTOTYPE: $$;$@
    INIT:
        const struct timespec times[2] = { { atime_sec, atime_nsec },
                                           { mtime_sec, mtime_nsec } };
    CODE:
        RETVAL = utimensat(dirfd, path, times, flags);
    OUTPUT:
        RETVAL

#endif

#ifdef PSX2008_HAS_READ
void
read(psx_fd_t fd, SV *buf, SV *count);
  PREINIT:
    SSize_t rv;
    STRLEN nbytes;
    char *cbuf;
  PPCODE:
  {
    if (UNLIKELY(SvNEGATIVE(count))) /* Performs 'get' magic. */
      croak("%s::read: Can't handle negative count: %" SVf_QUOTEDPREFIX,
            PACKNAME, SVfARG(count));
    nbytes = SvSTRLEN(count);
    if ((Size_t)nbytes != nbytes)
      nbytes = SSize_t_MAX;
    if (UNLIKELY(SvTRULYREADONLY(buf))) {
      if (nbytes)
        croak("%s::read: Can't modify read-only buf", PACKNAME);
      cbuf = NULL;
    }
    else {
      if (nbytes+1 == 0)
        --nbytes;
      if (!SvPOK(buf))
        sv_setpvn(buf, "", 0);
      (void)SvPV_force_nolen(buf);
      /* +1 for final '\0' to be on the safe side. */
      cbuf = SvGROW(buf, nbytes+1);
    }
    rv = read(fd, cbuf, nbytes);
    if (UNLIKELY(rv == -1))
      PUSHs(&PL_sv_undef);
    else {
      Size_t nread = (Size_t)rv;
      if (cbuf) {
        cbuf[nread] = '\0';
        SvCUR_set(buf, nread);
        SvPOK_only(buf);
        SvTAINTED_on(buf);
        SvSETMAGIC(buf);
      }
      PUSH_INT_OR_PV(nread);
    }
  }

#endif

#ifdef PSX2008_HAS_WRITE
void
write(psx_fd_t fd, SV *buf, SV *count=NULL);
  PREINIT:
    STRLEN cbuflen, nbytes;
    SSize_t rv;
  PPCODE:
  {
    const char *cbuf = SvPV_const(buf, cbuflen);
    if (UNLIKELY(SvNEGATIVE(count))) /* Performs 'get' magic. */
      croak("%s::write: Can't handle negative count: %" SVf_QUOTEDPREFIX,
            PACKNAME, SVfARG(count));
    else if (SvUNDEF_purposely(count))
      nbytes = cbuflen;
    else {
      nbytes = SvSTRLEN(count);
      if (nbytes > cbuflen)
        nbytes = cbuflen;
    }
    if ((Size_t)nbytes != nbytes)
      nbytes = SSize_t_MAX;
    rv = write(fd, cbuf, nbytes);
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((Size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_READV
void
readv(psx_fd_t fd, SV *buffers, AV *sizes);
  PROTOTYPE: $\[@$]$
  PPCODE:
  {
    SSize_t rv = _readv50c(aTHX_ fd, buffers, sizes, NULL, NULL);
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((Size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_PREADV
void
preadv(psx_fd_t fd, SV *buffers, AV *sizes, SV *offset=&PL_sv_undef);
  PROTOTYPE: $\[@$]$;$
  PPCODE:
  {
    SSize_t rv = _readv50c(aTHX_ fd, buffers, sizes, offset, NULL);
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((Size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_PREADV2
void
preadv2(psx_fd_t fd, SV *buffers, AV *sizes, SV *offset=&PL_sv_undef, \
        SV *flags=&PL_sv_undef);
  PROTOTYPE: $\[@$]$;$$
  PPCODE:
  {
    SSize_t rv = _readv50c(aTHX_ fd, buffers, sizes, offset, flags);
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((Size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_WRITEV
void
writev(psx_fd_t fd, AV *buffers);
  PPCODE:
  {
    struct iovec *iov;
    int iovcnt = _psx_av2iov(aTHX_ buffers, &iov);
    ssize_t rv = LIKELY(iovcnt >= 0) ? writev(fd, iov, iovcnt) : -1;
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_PWRITEV
void
pwritev(psx_fd_t fd, AV *buffers, SV *offset=NULL);
  PPCODE:
  {
    struct iovec *iov;
    int iovcnt = _psx_av2iov(aTHX_ buffers, &iov);
    Off_t offs = SvUNDEF_purposely(offset) ? 0 : SvOFFt(offset);
    ssize_t rv = LIKELY(iovcnt >= 0) ? pwritev(fd, iov, iovcnt, offs) : -1;
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_PWRITEV2
void
pwritev2(psx_fd_t fd, AV *buffers, SV *offset=NULL, SV *flags=NULL);
  PPCODE:
  {
    struct iovec *iov;
    int iovcnt = _psx_av2iov(aTHX_ buffers, &iov);
    Off_t offs = SvUNDEF_purposely(offset) ? 0 : SvOFFt(offset);
    int i_flags = SvUNDEF_purposely(flags) ? 0 : (int)SvIV(flags);
    ssize_t rv =
      LIKELY(iovcnt >= 0) ? pwritev2(fd, iov, iovcnt, offs, i_flags) : -1;
    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_PREAD
void
pread(psx_fd_t fd, SV *buf, SV *count, SV *offset=NULL, SV *buf_offset=NULL);
  PREINIT:
    Off_t f_offset;
    char *cbuf;
    STRLEN cbuflen, new_len, b_offset, nbytes;
    SSize_t rv;
  PPCODE:
  {
    if (UNLIKELY(SvNEGATIVE(count))) /* Performs 'get' magic. */
      croak("%s::pread: Can't handle negative count: %" SVf_QUOTEDPREFIX,
            PACKNAME, SVfARG(count));

    nbytes = SvSTRLEN(count);
    if ((Size_t)nbytes != nbytes)
      nbytes = SSize_t_MAX;

    if (UNLIKELY(SvTRULYREADONLY(buf))) {
      if (nbytes)
        croak("%s::pread: Can't modify read-only buf", PACKNAME);
      cbuf = NULL;
      b_offset = 0;
    }
    else {
      if (!SvPOK(buf))
        sv_setpvn(buf, "", 0);
      (void)SvPV_force(buf, cbuflen);

      /* Ensure buf_offset results in a valid string index. */
      if (SvUNDEF_purposely(buf_offset))
        b_offset = 0;
      else {
        int neg = SvNEGATIVE(buf_offset);
        b_offset = SvSTRLEN(buf_offset);
        if (neg) {
          b_offset += cbuflen;
          if (b_offset > cbuflen)
           croak("%s::pread: buf_offset %" SVf_QUOTEDPREFIX " outside string",
                 PACKNAME, SVfARG(buf_offset));
        }
      }

      new_len = b_offset + nbytes;
      if (new_len < b_offset || new_len+1 == 0)
        croak("%s::pread: buf_offset[%" SVf_QUOTEDPREFIX
              "] + count[%" SVf_QUOTEDPREFIX "] is too big for a Perl string",
              PACKNAME, SVfARG(buf_offset), SVfARG(count));

      /* +1 for final '\0' to be on the safe side. */
      cbuf = SvGROW(buf, new_len+1);

      /* Pad buffer with zeros if b_offset is past the buffer. */
      if (b_offset > cbuflen)
        Zero(cbuf + cbuflen, b_offset - cbuflen, char);
    }

    /* Now fscking finally read teh data! */
    f_offset = SvUNDEF_purposely(offset) ? 0 : SvOFFt(offset);
    rv = pread(fd, cbuf + b_offset, nbytes, f_offset);

    if (UNLIKELY(rv == -1))
      PUSHs(&PL_sv_undef);
    else {
      Size_t nread = (Size_t)rv;
      if (cbuf) {
        cbuf[b_offset + nread] = '\0';
        SvCUR_set(buf, b_offset + nread);
        SvPOK_only(buf);
        SvTAINTED_on(buf);
        SvSETMAGIC(buf);
      }
      PUSH_INT_OR_PV(nread);
    }
  }

#endif

#ifdef PSX2008_HAS_PWRITE
void
pwrite(psx_fd_t fd, SV *buf,                            \
       SV *count=NULL, SV *offset=NULL, SV *buf_offset=NULL);
  PREINIT:
    Off_t f_offset;
    const char *cbuf;
    STRLEN cbuflen, b_offset, max_nbytes, nbytes;
    SSize_t rv;
  PPCODE:
  {
    if (UNLIKELY(SvNEGATIVE(count))) /* Performs 'get' magic. */
      croak("%s::pwrite: Can't handle negative count: %" SVf_QUOTEDPREFIX,
            PACKNAME, SVfARG(count));

    cbuf = SvPV_const(buf, cbuflen);

    /* Ensure buf_offset results in a valid string index. This is slightly
     * different from pread() because we can't allow offsets beyond the buffer
     * at all (zero is okay, though). */
    if (SvUNDEF_purposely(buf_offset))
      b_offset = 0;
    else {
      int neg = SvNEGATIVE(buf_offset);
      b_offset = SvSTRLEN(buf_offset);
      if (neg)
        b_offset += cbuflen;
      if (LIKELY(b_offset) && UNLIKELY(b_offset >= cbuflen))
          croak("%s::pwrite: buf_offset %" SVf_QUOTEDPREFIX " outside string",
                PACKNAME, SVfARG(buf_offset));
    }

    max_nbytes = cbuflen - b_offset;
    nbytes = SvUNDEF_purposely(count) ? max_nbytes : SvSTRLEN(count);
    if (nbytes > max_nbytes)
      nbytes = max_nbytes;
    if ((Size_t)nbytes != nbytes)
      nbytes = SSize_t_MAX;

    f_offset = SvUNDEF_purposely(offset) ? 0 : SvOFFt(offset);
    rv = pwrite(fd, cbuf + b_offset, nbytes, f_offset);

    if (LIKELY(rv != -1))
      PUSH_INT_OR_PV((Size_t)rv);
    else
      PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_POSIX_FADVISE
SysRetTrue
posix_fadvise(psx_fd_t fd, Off_t offset, Off_t len, int advice);
  INIT:
    int rv;
  CODE:
    rv = posix_fadvise(fd, offset, len, advice);
    RETVAL = (LIKELY(rv == 0)) ? 0 : -1;
    if (rv) errno = rv;
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_POSIX_FALLOCATE
SysRetTrue
posix_fallocate(psx_fd_t fd, Off_t offset, Off_t len);
  INIT:
    int rv;
  CODE:
    rv = posix_fallocate(fd, offset, len);
    RETVAL = (LIKELY(rv == 0)) ? 0 : -1;
    if (rv) errno = rv;
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_PTSNAME
char *
ptsname(psx_fd_t fd);
  INIT:
#ifdef PSX2008_HAS_PTSNAME_R
    int rv;
    char name[MAXPATHLEN];
#endif
  CODE:
#ifdef PSX2008_HAS_PTSNAME_R
    rv = ptsname_r(fd, name, sizeof(name));
    RETVAL = (LIKELY(rv == 0)) ? name : NULL;
    /* Some implementations return -1 on error and set errno. */
    if (rv > 0) errno = rv;
#else
    RETVAL = ptsname(fd);
#endif
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_TTYNAME
char *
ttyname(psx_fd_t fd);
  INIT:
#ifdef PSX2008_HAS_TTYNAME_R
    int rv;
    char name[MAXPATHLEN];
#endif
  CODE:
#ifdef PSX2008_HAS_TTYNAME_R
    rv = ttyname_r(fd, name, sizeof(name));
    RETVAL = (LIKELY(rv == 0)) ? name : NULL;
    if (rv) errno = rv;
#else
    RETVAL = ttyname(fd);
#endif
  OUTPUT:
    RETVAL

#endif

##
## POSIX::remove() is incorrectly implemented as:
## '(-d $_[0]) ? CORE::rmdir($_[0]) : CORE::unlink($_[0])'.
##
## If $_[0] is a symlink to a directory, POSIX::remove() fails with ENOTDIR
## from rmdir() instead of removing the symlink (POSIX requires remove() to
## be equivalent to unlink() for non-directories).
##
## This could be fixed like this (correct errno check depends on OS):
## 'unlink $_[0] or ($!{EISDIR} or $!{EPERM}) and rmdir $_[0]'
##
## Or just use the actual library call like we do here.
##

#if defined(__linux__) || defined(__CYGWIN__)
#define UNLINK_ISDIR_ERRNO (errno == EISDIR)
#elif !defined(_WIN32)
#define UNLINK_ISDIR_ERRNO (errno == EISDIR || errno == EPERM)
#else
#define UNLINK_ISDIR_ERRNO (errno == EISDIR || errno == EPERM || errno == EACCES)
#endif

#if !defined(PSX2008_HAS_REMOVE) || (defined(_WIN32) && !defined(__CYGWIN__))
# if defined(PSX2008_HAS_UNLINK) && defined(PSX2008_HAS_RMDIR)
void
remove(const char *path);
  PPCODE:
    if (unlink(path) == 0 || (UNLINK_ISDIR_ERRNO && rmdir(path) == 0))
      mPUSHp("0 but true", 10);
    else
      PUSHs(&PL_sv_undef);

# else

# endif
#else
SysRetTrue
remove(const char *path);

#endif

#ifdef PSX2008_HAS_UNLINKAT
void
removeat(psx_fd_t dirfd, const char *path);
  PPCODE:
    if (unlinkat(dirfd, path, 0) == 0
        || (UNLINK_ISDIR_ERRNO && unlinkat(dirfd, path, AT_REMOVEDIR) == 0))
      mPUSHp("0 but true", 10);
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_RENAME
SysRetTrue
rename(const char *old, const char *new);

#endif

#ifdef PSX2008_HAS_RMDIR
SysRetTrue
rmdir(const char *path);

#endif

#ifdef PSX2008_HAS_SYMLINK
SysRetTrue
symlink(const char *target, const char *linkpath);

#endif

#ifdef PSX2008_HAS_SYNC
void
sync();

#endif

#ifdef PSX2008_HAS_UNLINK
SysRetTrue
unlink(const char *path);

#endif

#ifdef PSX2008_HAS_FUTIMENS
SysRetTrue
futimens(psx_fd_t fd,                                           \
         time_t atime_sec = 0, long atime_nsec = UTIME_NOW,     \
         time_t mtime_sec = 0, long mtime_nsec = UTIME_NOW);
  PROTOTYPE: $@
  INIT:
    const struct timespec times[2] = { { atime_sec, atime_nsec },
                                       { mtime_sec, mtime_nsec } };
  CODE:
    RETVAL = futimens(fd, times);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_EXECVEAT
void
execveat(psx_fd_t dirfd, const char *path,              \
         AV *args, SV *env=NULL, int flags=0);
  PPCODE:
  {
    _execve50c(aTHX_ dirfd, path, args, env, flags);
    PUSHs(&PL_sv_undef);
  }

#endif

#ifdef PSX2008_HAS_FEXECVE
void
fexecve(psx_fd_t fd, AV *args, SV *env=NULL);
  PPCODE:
  {
    _execve50c(aTHX_ fd, NULL, args, env, 0);
    PUSHs(&PL_sv_undef);
  }

#endif

#if defined(PSX2008_HAS_POLL)
void
poll(SV *pollfds, int timeout=-1);
  PPCODE:
  {
    AV *pollfds_av = NULL;
    Size_t nfds = 0;

    SvGETMAGIC(pollfds);
    if (SvOK(pollfds)) {
      if (!SvROK(pollfds) || SvTYPE(SvRV(pollfds)) != SVt_PVAV)
        croak("%s::poll: pollfds is not an ARRAY reference: %" SVf_QUOTEDPREFIX,
              PACKNAME, SVfARG(pollfds));
      pollfds_av = (AV*)SvRV(pollfds);
      nfds = av_count(pollfds_av);
    }

    /* poll() expects nfds_t, av_count() returns Size_t, av_fetch() expects
     * SSize_t, so we'll accept only the smallest of these. */
    if (UNLIKELY((nfds_t)nfds != nfds || nfds > SSize_t_MAX)) {
      errno = EINVAL;
      XSRETURN_IV(-1);
    }
    if (UNLIKELY((nfds*sizeof(struct pollfd))/sizeof(struct pollfd) != nfds)) {
      errno = EINVAL;
      XSRETURN_IV(-1);
    }
    else {
      int rv;
      SSize_t i;
      SV **pollfd;
      const struct pollfd initfd = {.fd=-1, .events=0, .revents=0};
      struct pollfd *fds = safemalloc(nfds * sizeof(*fds));
      SAVEFREEPV(fds);
      for (i = 0; i < nfds; i++) {
        /* Initialize fds item with no-op defaults in case we skip undef or
         * placeholders. Copy() yields less bloat than assignment (with gcc,
         * clang is smarter) and shouldn't require a function call. We could
         * use a fancy C99 compound literal for initfd, but then, it's only
         * 2025 ... */
        Copy(&initfd, fds+i, 1, struct pollfd);
        pollfd = av_fetch(pollfds_av, i, 0);
        if (!pollfd)
          continue;
        SvGETMAGIC(*pollfd);
        if (!SvOK(*pollfd))
          continue;
        if (!SvROK(*pollfd) || SvTYPE(SvRV(*pollfd)) != SVt_PVAV)
          croak("%s::poll: pollfds[%" IVdf
                "] is not an ARRAY reference: %" SVf_QUOTEDPREFIX,
                PACKNAME, (IV)i, SVfARG(*pollfd));
        else {
          AV *pollfd_av = (AV*)SvRV(*pollfd);
          SV **pollfd_fd = av_fetch(pollfd_av, 0, 0);
          if (pollfd_fd) {
            fds[i].fd = _psx_fileno(aTHX_ *pollfd_fd);
            if (fds[i].fd >= 0) {
              SV **pollfd_events = av_fetch(pollfd_av, 1, 0);
              if (pollfd_events)
                fds[i].events = (short)(SvIV(*pollfd_events) & PERL_USHORT_MAX);
            }
          }
        }
      }

      rv = poll(fds, nfds, timeout);

      if (rv > 0) {
        for (i = 0; i < nfds; i++) {
          SV **pollfd = av_fetch(pollfds_av, i, 0);
          /* Paranoid safeguard against threads messing with pollfds_av. */
          if (LIKELY(pollfd && SvROK(*pollfd) && SvTYPE(SvRV(*pollfd)) == SVt_PVAV)) {
            AV *pollfd_av = (AV*)SvRV(*pollfd);
            /* Cast revents to unsigned short to prevent the compiler from
             * sign-extending it to IV. */
            SV *revents = newSViv((unsigned short)fds[i].revents);
            if (!av_store(pollfd_av, 2, revents)) {
              SvREFCNT_dec(revents);
              if (SvMAGICAL(pollfd_av))
                mg_set(revents);
            }
          }
        }
      }
      XSRETURN_IV(rv);
    }
  }

#endif

## Integer and real number arithmetic
#####################################

#ifdef PSX2008_ABS
IV
abs(IV i)
  CODE:
    RETVAL = PSX2008_ABS(i);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_ACOS
NV
acos(double x);

#endif

#ifdef PSX2008_HAS_ACOSH
NV
acosh(double x);

#endif

#ifdef PSX2008_HAS_ASIN
NV
asin(double x);

#endif

#ifdef PSX2008_HAS_ASINH
NV
asinh(double x);

#endif

#ifdef PSX2008_HAS_ATAN
NV
atan(double x);

#endif

#ifdef PSX2008_HAS_ATAN2
NV
atan2(double y, double x);

#endif

#ifdef PSX2008_HAS_ATANH
NV
atanh(double x);

#endif

#ifdef PSX2008_HAS_CBRT
NV
cbrt(double x);

#endif

#ifdef PSX2008_HAS_CEIL
NV
ceil(double x);

#endif

#ifdef PSX2008_HAS_COPYSIGN
NV
copysign(double x, double y);

#endif

#ifdef PSX2008_HAS_COS
NV
cos(double x);

#endif

#ifdef PSX2008_HAS_COSH
NV
cosh(double x);

#endif

#ifdef PSX2008_DIV
void
div(IV numer, IV denom);
    INIT:
        PSX2008_DIV_T result;
    PPCODE:
        result = PSX2008_DIV(numer, denom);
        EXTEND(SP, 2);
        mPUSHi(result.quot);
        mPUSHi(result.rem);

#endif

#ifdef PSX2008_HAS_ERF
NV
erf(double x);

#endif

#ifdef PSX2008_HAS_ERFC
NV
erfc(double x);

#endif

#ifdef PSX2008_HAS_EXP
NV
exp(double x);

#endif

#ifdef PSX2008_HAS_EXP2
NV
exp2(double x);

#endif

#ifdef PSX2008_HAS_EXPM1
NV
expm1(double x);

#endif

#ifdef PSX2008_HAS_FDIM
NV
fdim(double x, double y);

#endif

#ifdef PSX2008_HAS_FLOOR
NV
floor(double x);

#endif

#ifdef PSX2008_HAS_FMA
NV
fma(double x, double y, double z);

#endif

#ifdef PSX2008_HAS_FMAX
NV
fmax(double x, double y);

#endif

#ifdef PSX2008_HAS_FMIN
NV
fmin(double x, double y);

#endif

#ifdef PSX2008_HAS_FMOD
NV
fmod(double x, double y);

#endif

#ifdef PSX2008_HAS_FPCLASSIFY

int
fpclassify(double x);

#endif

#ifdef PSX2008_HAS_HYPOT
NV
hypot(double x, double y);

#endif

#ifdef PSX2008_HAS_ILOGB
int
ilogb(double x);

#endif

#ifdef PSX2008_HAS_ISFINITE
int
isfinite(double x);

#endif

#ifdef PSX2008_HAS_ISINF
int
isinf(double x);

#endif

#ifdef PSX2008_HAS_ISNAN
int
isnan(double x);

#endif

#ifdef PSX2008_HAS_ISNORMAL
int
isnormal(double x);

#endif

#ifdef PSX2008_HAS_ISGREATEREQUAL
int
isgreaterequal(NV x, NV y);

#endif

#ifdef PSX2008_HAS_ISLESS
int
isless(NV x, NV y);

#endif

#ifdef PSX2008_HAS_ISLESSEQUAL
int
islessequal(NV x, NV y);

#endif

#ifdef PSX2008_HAS_ISLESSGREATER
int
islessgreater(NV x, NV y);

#endif

#ifdef PSX2008_HAS_ISUNORDERED
int
isunordered(NV x, NV y);

#endif

#ifdef PSX2008_HAS_J0
NV
j0(double x);

#endif

#ifdef PSX2008_HAS_J1
NV
j1(double x);

#endif

#ifdef PSX2008_HAS_JN
NV
jn(int n, double x);

#endif

#ifdef PSX2008_HAS_LDEXP
NV
ldexp(double x, int exp);

#endif

#ifdef PSX2008_HAS_LGAMMA
NV
lgamma(double x);

#endif

#ifdef PSX2008_HAS_LOG
NV
log(double x);

#endif

#ifdef PSX2008_HAS_LOG10
NV
log10(double x);

#endif

#ifdef PSX2008_HAS_LOG1P
NV
log1p(double x);

#endif

#ifdef PSX2008_HAS_LOG2
NV
log2(double x);

#endif

#ifdef PSX2008_HAS_LOGB
NV
logb(double x);

#endif

#ifdef PSX2008_LROUND
void
lround(double x)
  INIT:
    PSX2008_LROUND_T ret;
  PPCODE:
    SETERRNO(0, 0);
    feclearexcept(FE_ALL_EXCEPT);
    ret = PSX2008_LROUND(x);
    if (errno == 0 && fetestexcept(FE_ALL_EXCEPT) == 0)
      PUSH_INT_OR_PV(ret);
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_NEARBYINT
NV
nearbyint(double x);

#endif

#ifdef PSX2008_HAS_NEXTAFTER
NV
nextafter(double x, double y);

#endif

#ifdef PSX2008_HAS_NEXTTOWARD
NV
nexttoward(double x, NV y);

#endif

#ifdef PSX2008_HAS_REMAINDER
void
remainder(double x, double y);
  INIT:
    double res;
  PPCODE:
    SETERRNO(0, 0);
    feclearexcept(FE_ALL_EXCEPT);
    res = remainder(x, y);
    if (errno == 0 && fetestexcept(FE_ALL_EXCEPT) == 0)
      mPUSHn(res);
    else
      PUSHs(&PL_sv_undef);

#endif

#ifdef PSX2008_HAS_REMQUO
void
remquo(double x, double y);
  INIT:
    int quo;
    double res;
  PPCODE:
    SETERRNO(0, 0);
    feclearexcept(FE_ALL_EXCEPT);
    res = remquo(x, y, &quo);
    if (errno == 0 && fetestexcept(FE_ALL_EXCEPT) == 0) {
      mPUSHn(res);
      mPUSHi(quo);
    }

#endif

#ifdef PSX2008_HAS_ROUND
NV
round(double x);

#endif

#ifdef PSX2008_SCALBN
NV
scalbn(double x, IV n);
  CODE:
    RETVAL = PSX2008_SCALBN(x, n);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_SIGNBIT
int
signbit(double x);

#endif

#ifdef PSX2008_HAS_SIN
NV
sin(double x);

#endif

#ifdef PSX2008_HAS_SINH
NV
sinh(double x);

#endif

#ifdef PSX2008_HAS_TAN
NV
tan(double x);

#endif

#ifdef PSX2008_HAS_TANH
NV
tanh(double x);

#endif

#ifdef PSX2008_HAS_TGAMMA
NV
tgamma(double x);

#endif

#ifdef PSX2008_HAS_TRUNC
NV
trunc(double x);

#endif

#ifdef PSX2008_HAS_Y0
NV
y0(double x);

#endif

#ifdef PSX2008_HAS_Y1
NV
y1(double x);

#endif

#ifdef PSX2008_HAS_YN
NV
yn(int n, double x);

#endif

## Complex arithmetic functions
###############################

#ifdef PSX2008_HAS_CABS
NV
cabs(double re, double im);
  INIT:
    double complex z = re + im * I;
  CODE:
    RETVAL = cabs(z);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CARG
NV
carg(double re, double im);
  INIT:
    double complex z = re + im * I;
  CODE:
    RETVAL = carg(z);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CIMAG
NV
cimag(double re, double im);
  INIT:
    double complex z = re + im * I;
  CODE:
    RETVAL = cimag(z);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CONJ
void
conj(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = conj(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CPROJ
NV
cproj(double re, double im);
  INIT:
    double complex z = re + im * I;
  CODE:
    RETVAL = cproj(z);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CREAL
NV
creal(double re, double im);
  INIT:
    double complex z = re + im * I;
  CODE:
    RETVAL = creal(z);
  OUTPUT:
    RETVAL

#endif

#ifdef PSX2008_HAS_CEXP
void
cexp(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = cexp(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CLOG
void
clog(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = clog(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CPOW
void
cpow(double re_x, double im_x, double re_y, double im_y);
  INIT:
    double complex x = re_x + im_x * I;
    double complex y = re_y + im_y * I;
    double complex result = cpow(x, y);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CSQRT
void
csqrt(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = csqrt(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CACOS
void
cacos(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = cacos(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CACOSH
void
cacosh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = cacosh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CASIN
void
casin(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = casin(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CASINH
void
casinh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = casinh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CATAN
void
catan(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = catan(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CATANH
void
catanh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = catanh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CCOS
void
ccos(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = ccos(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CCOSH
void
ccosh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = ccosh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CSIN
void
csin(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = csin(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CSINH
void
csinh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = csinh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CTAN
void
ctan(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = ctan(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

#ifdef PSX2008_HAS_CTANH
void
ctanh(double re, double im);
  INIT:
    double complex z = re + im * I;
    double complex result = ctanh(z);
  PPCODE:
    RETURN_COMPLEX(result);

#endif

## DESTROY is called when a file handle we created (e.g. in openat)
## is cleaned up. This is just a dummy to silence AUTOLOAD. We leave
## it up to Perl to take the necessary steps.
void
DESTROY(...);
PPCODE:

BOOT:
{
}

# vim: set ts=4 sw=4 sts=4 expandtab: