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

/* This file contains those routines named by Import pragmas in
   packages in the GNAT hierarchy (especially GNAT.OS_Lib) and in
   package Osint.  Many of the subprograms in OS_Lib import standard
   library calls directly. This file contains all other routines.  */

/* Ensure access to errno is thread safe.  */
#define _REENTRANT
#define _THREAD_SAFE

/* Use 64 bit Large File API */
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#define _FILE_OFFSET_BITS 64

#ifdef __vxworks

/* No need to redefine exit here.  */
#undef exit

/* We want to use the POSIX variants of include files.  */
#define POSIX
#include "vxWorks.h"

#if defined (__mips_vxworks)
#include "cacheLib.h"
#endif /* __mips_vxworks */

/* If SMP, access vxCpuConfiguredGet */
#ifdef _WRS_CONFIG_SMP
#include <vxCpuLib.h>
#endif /* _WRS_CONFIG_SMP */

/* We need to know the VxWorks version because some file operations
   (such as chmod) are only available on VxWorks 6.  */
#include "version.h"

#endif /* VxWorks */

#if defined (__APPLE__)
#include <unistd.h>
#endif

#if defined (__hpux__)
#include <sys/param.h>
#include <sys/pstat.h>
#endif

#ifdef __PikeOS__
#define __BSD_VISIBLE 1
#endif

#ifdef IN_RTS
#include "tconfig.h"
#include "tsystem.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#if defined (__vxworks) || defined (__ANDROID__)
/* S_IREAD and S_IWRITE are not defined in VxWorks or Android */
#ifndef S_IREAD
#define S_IREAD  (S_IRUSR | S_IRGRP | S_IROTH)
#endif

#ifndef S_IWRITE
#define S_IWRITE (S_IWUSR)
#endif
#endif

/* We don't have libiberty, so use malloc.  */
#define xmalloc(S) malloc (S)
#define xrealloc(V,S) realloc (V,S)
#else
#include "config.h"
#include "system.h"
#include "version.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if defined (__DJGPP__)

/* For isalpha-like tests in the compiler, we're expected to resort to
   safe-ctype.h/ISALPHA.  This isn't available for the runtime library
   build, so we fallback on ctype.h/isalpha there.  */

#ifdef IN_RTS
#include <ctype.h>
#define ISALPHA isalpha
#endif

#elif defined (__MINGW32__) || defined (__CYGWIN__)

#include "mingw32.h"

/* Current code page and CCS encoding to use, set in initialize.c.  */
UINT __gnat_current_codepage;
UINT __gnat_current_ccs_encoding;

#include <sys/utime.h>

/* For isalpha-like tests in the compiler, we're expected to resort to
   safe-ctype.h/ISALPHA.  This isn't available for the runtime library
   build, so we fallback on ctype.h/isalpha there.  */

#ifdef IN_RTS
#include <ctype.h>
#define ISALPHA isalpha
#endif

#elif defined (__Lynx__)

/* Lynx utime.h only defines the entities of interest to us if
   defined (VMOS_DEV), so ... */
#define VMOS_DEV
#include <utime.h>
#undef VMOS_DEV

#else
#include <utime.h>
#endif

/* wait.h processing */
#ifdef __MINGW32__
# if OLD_MINGW
#  include <sys/wait.h>
# endif
#elif defined (__vxworks) && defined (__RTP__)
# include <wait.h>
#elif defined (__Lynx__)
/* ??? We really need wait.h and it includes resource.h on Lynx.  GCC
   has a resource.h header as well, included instead of the lynx
   version in our setup, causing lots of errors.  We don't really need
   the lynx contents of this file, so just workaround the issue by
   preventing the inclusion of the GCC header from doing anything.  */
# define GCC_RESOURCE_H
# include <sys/wait.h>
#elif defined (__PikeOS__)
/* No wait() or waitpid() calls available.  */
#else
/* Default case.  */
#include <sys/wait.h>
#endif

#if defined (__DJGPP__)
#include <process.h>
#include <signal.h>
#include <dir.h>
#include <utime.h>
#undef DIR_SEPARATOR
#define DIR_SEPARATOR '\\'

#elif defined (_WIN32)

#include <windows.h>
#include <accctrl.h>
#include <aclapi.h>
#include <tlhelp32.h>
#include <signal.h>
#undef DIR_SEPARATOR
#define DIR_SEPARATOR '\\'

#else
#include <utime.h>
#endif

#include "adaint.h"

/* Define symbols O_BINARY and O_TEXT as harmless zeroes if they are not
   defined in the current system. On DOS-like systems these flags control
   whether the file is opened/created in text-translation mode (CR/LF in
   external file mapped to LF in internal file), but in Unix-like systems,
   no text translation is required, so these flags have no effect.  */

#ifndef O_BINARY
#define O_BINARY 0
#endif

#ifndef O_TEXT
#define O_TEXT 0
#endif

#ifndef HOST_EXECUTABLE_SUFFIX
#define HOST_EXECUTABLE_SUFFIX ""
#endif

#ifndef HOST_OBJECT_SUFFIX
#define HOST_OBJECT_SUFFIX ".o"
#endif

#ifndef PATH_SEPARATOR
#define PATH_SEPARATOR ':'
#endif

#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif

/* Check for cross-compilation.  */
#if defined (CROSS_COMPILE) || defined (CROSS_DIRECTORY_STRUCTURE)
#define IS_CROSS 1
int __gnat_is_cross_compiler = 1;
#else
#undef IS_CROSS
int __gnat_is_cross_compiler = 0;
#endif

char __gnat_dir_separator = DIR_SEPARATOR;

char __gnat_path_separator = PATH_SEPARATOR;

/* The GNAT_LIBRARY_TEMPLATE contains a list of expressions that define
   the base filenames that libraries specified with -lsomelib options
   may have. This is used by GNATMAKE to check whether an executable
   is up-to-date or not. The syntax is

     library_template ::= { pattern ; } pattern NUL
     pattern          ::= [ prefix ] * [ postfix ]

   These should only specify names of static libraries as it makes
   no sense to determine at link time if dynamic-link libraries are
   up to date or not. Any libraries that are not found are supposed
   to be up-to-date:

     * if they are needed but not present, the link
       will fail,

     * otherwise they are libraries in the system paths and so
       they are considered part of the system and not checked
       for that reason.

   ??? This should be part of a GNAT host-specific compiler
       file instead of being included in all user applications
       as well. This is only a temporary work-around for 3.11b.  */

#ifndef GNAT_LIBRARY_TEMPLATE
#define GNAT_LIBRARY_TEMPLATE "lib*.a"
#endif

const char *__gnat_library_template = GNAT_LIBRARY_TEMPLATE;

#if defined (__vxworks)
#define GNAT_MAX_PATH_LEN PATH_MAX

#else

#if defined (__MINGW32__)
#include "mingw32.h"

#if OLD_MINGW
#include <sys/param.h>
#endif

#else
#include <sys/param.h>
#endif

#ifdef MAXPATHLEN
#define GNAT_MAX_PATH_LEN MAXPATHLEN
#else
#define GNAT_MAX_PATH_LEN 256
#endif

#endif

/* Used for runtime check that Ada constant File_Attributes_Size is no
   less than the actual size of struct file_attributes (see Osint
   initialization). */
int __gnat_size_of_file_attributes = sizeof (struct file_attributes);

void __gnat_stat_to_attr (int fd, char* name, struct file_attributes* attr);

/* The __gnat_max_path_len variable is used to export the maximum
   length of a path name to Ada code. max_path_len is also provided
   for compatibility with older GNAT versions, please do not use
   it. */

int __gnat_max_path_len = GNAT_MAX_PATH_LEN;
int max_path_len = GNAT_MAX_PATH_LEN;

/* Control whether we can use ACL on Windows.  */

int __gnat_use_acl = 1;

/* The following macro HAVE_READDIR_R should be defined if the
   system provides the routine readdir_r.
   ... but we never define it anywhere???  */
#undef HAVE_READDIR_R

#define MAYBE_TO_PTR32(argv) argv

static const char ATTR_UNSET = 127;

/* Reset the file attributes as if no system call had been performed */

void
__gnat_reset_attributes (struct file_attributes* attr)
{
  attr->exists     = ATTR_UNSET;
  attr->error      = EINVAL;

  attr->writable   = ATTR_UNSET;
  attr->readable   = ATTR_UNSET;
  attr->executable = ATTR_UNSET;

  attr->regular    = ATTR_UNSET;
  attr->symbolic_link = ATTR_UNSET;
  attr->directory = ATTR_UNSET;

  attr->timestamp = (OS_Time)-2;
  attr->file_length = -1;
}

int
__gnat_error_attributes (struct file_attributes *attr) {
  return attr->error;
}

OS_Time
__gnat_current_time (void)
{
  time_t res = time (NULL);
  return (OS_Time) res;
}

/* Return the current local time as a string in the ISO 8601 format of
   "YYYY-MM-DD HH:MM:SS.SS". The returned string is 22 + 1 (NULL) characters
   long. */

void
__gnat_current_time_string (char *result)
{
  const char *format = "%Y-%m-%d %H:%M:%S";
  /* Format string necessary to describe the ISO 8601 format */

  const time_t t_val = time (NULL);

  strftime (result, 22, format, localtime (&t_val));
  /* Convert the local time into a string following the ISO format, copying
     at most 22 characters into the result string. */

  result [19] = '.';
  result [20] = '0';
  result [21] = '0';
  /* The sub-seconds are manually set to zero since type time_t lacks the
     precision necessary for nanoseconds. */
}

void
__gnat_to_gm_time (OS_Time *p_time, int *p_year, int *p_month, int *p_day,
		   int *p_hours, int *p_mins, int *p_secs)
{
  struct tm *res;
  time_t time = (time_t) *p_time;

#ifdef _WIN32
  /* On Windows systems, the time is sometimes rounded up to the nearest
     even second, so if the number of seconds is odd, increment it.  */
  if (time & 1)
    time++;
#endif

  res = gmtime (&time);
  if (res)
    {
      *p_year = res->tm_year;
      *p_month = res->tm_mon;
      *p_day = res->tm_mday;
      *p_hours = res->tm_hour;
      *p_mins = res->tm_min;
      *p_secs = res->tm_sec;
    }
  else
    *p_year = *p_month = *p_day = *p_hours = *p_mins = *p_secs = 0;
}

void
__gnat_to_os_time (OS_Time *p_time, int year, int month, int day,
		   int hours, int mins, int secs)
{
  struct tm v;

  v.tm_year  = year;
  v.tm_mon   = month;
  v.tm_mday  = day;
  v.tm_hour  = hours;
  v.tm_min   = mins;
  v.tm_sec   = secs;
  v.tm_isdst = -1;

  /* returns -1 of failing, this is s-os_lib Invalid_Time */

  *p_time = (OS_Time) mktime (&v);
}

/* Place the contents of the symbolic link named PATH in the buffer BUF,
   which has size BUFSIZ.  If PATH is a symbolic link, then return the number
   of characters of its content in BUF.  Otherwise, return -1.
   For systems not supporting symbolic links, always return -1.  */

int
__gnat_readlink (char *path ATTRIBUTE_UNUSED,
		 char *buf ATTRIBUTE_UNUSED,
		 size_t bufsiz ATTRIBUTE_UNUSED)
{
#if defined (_WIN32) \
  || defined(__vxworks) || defined (__PikeOS__)
  return -1;
#else
  return readlink (path, buf, bufsiz);
#endif
}

/* Creates a symbolic link named NEWPATH which contains the string OLDPATH.
   If NEWPATH exists it will NOT be overwritten.
   For systems not supporting symbolic links, always return -1.  */

int
__gnat_symlink (char *oldpath ATTRIBUTE_UNUSED,
		char *newpath ATTRIBUTE_UNUSED)
{
#if defined (_WIN32) \
  || defined(__vxworks) || defined (__PikeOS__)
  return -1;
#else
  return symlink (oldpath, newpath);
#endif
}

/* Try to lock a file, return 1 if success.  */

#if defined (__vxworks) \
  || defined (_WIN32) || defined (__PikeOS__)

/* Version that does not use link. */

int
__gnat_try_lock (char *dir, char *file)
{
  int fd;
#ifdef __MINGW32__
  TCHAR wfull_path[GNAT_MAX_PATH_LEN];
  TCHAR wfile[GNAT_MAX_PATH_LEN];
  TCHAR wdir[GNAT_MAX_PATH_LEN];

  S2WSC (wdir, dir, GNAT_MAX_PATH_LEN);
  S2WSC (wfile, file, GNAT_MAX_PATH_LEN);

  /* ??? the code below crash on MingW64 for obscure reasons, a ticket
     has been opened here:

     https://sourceforge.net/p/mingw-w64/bugs/414/

     As a workaround an equivalent set of code has been put in place below.

  _stprintf (wfull_path, _T("%s%c%s"), wdir, _T(DIR_SEPARATOR), wfile);
  */

  _tcscpy (wfull_path, wdir);
  _tcscat (wfull_path, L"\\");
  _tcscat (wfull_path, wfile);

  fd = _topen (wfull_path, O_CREAT | O_EXCL, 0600);
#else
  char full_path[256];

  sprintf (full_path, "%s%c%s", dir, DIR_SEPARATOR, file);
  fd = open (full_path, O_CREAT | O_EXCL, 0600);
#endif

  if (fd < 0)
    return 0;

  close (fd);
  return 1;
}

#else

/* Version using link(), more secure over NFS.  */
/* See TN 6913-016 for discussion ??? */

int
__gnat_try_lock (char *dir, char *file)
{
  char full_path[256];
  char temp_file[256];
  GNAT_STRUCT_STAT stat_result;
  int fd;

  sprintf (full_path, "%s%c%s", dir, DIR_SEPARATOR, file);
  sprintf (temp_file, "%s%cTMP-%ld-%ld",
           dir, DIR_SEPARATOR, (long)getpid(), (long)getppid ());

  /* Create the temporary file and write the process number.  */
  fd = open (temp_file, O_CREAT | O_WRONLY, 0600);
  if (fd < 0)
    return 0;

  close (fd);

  /* Link it with the new file.  */
  link (temp_file, full_path);

  /* Count the references on the old one. If we have a count of two, then
     the link did succeed. Remove the temporary file before returning.  */
  __gnat_stat (temp_file, &stat_result);
  unlink (temp_file);
  return stat_result.st_nlink == 2;
}
#endif

/* Return the maximum file name length.  */

int
__gnat_get_maximum_file_name_length (void)
{
  return -1;
}

/* Return nonzero if file names are case sensitive.  */

static int file_names_case_sensitive_cache = -1;

int
__gnat_get_file_names_case_sensitive (void)
{
  if (file_names_case_sensitive_cache == -1)
    {
      const char *sensitive = getenv ("GNAT_FILE_NAME_CASE_SENSITIVE");

      if (sensitive != NULL
          && (sensitive[0] == '0' || sensitive[0] == '1')
          && sensitive[1] == '\0')
        file_names_case_sensitive_cache = sensitive[0] - '0';
      else
	{
	  /* By default, we suppose filesystems aren't case sensitive on
	     Windows and Darwin (but they are on arm-darwin).  */
#if defined (WINNT) || defined (__DJGPP__) \
  || (defined (__APPLE__) && !(defined (__arm__) || defined (__arm64__)))
	  file_names_case_sensitive_cache = 0;
#else
	  file_names_case_sensitive_cache = 1;
#endif
	}
    }
  return file_names_case_sensitive_cache;
}

/* Return nonzero if environment variables are case sensitive.  */

int
__gnat_get_env_vars_case_sensitive (void)
{
#if defined (WINNT) || defined (__DJGPP__)
 return 0;
#else
 return 1;
#endif
}

char
__gnat_get_default_identifier_character_set (void)
{
  return '1';
}

/* Return the current working directory.  */

void
__gnat_get_current_dir (char *dir, int *length)
{
#if defined (__MINGW32__)
  TCHAR wdir[GNAT_MAX_PATH_LEN];

  _tgetcwd (wdir, *length);

  WS2SC (dir, wdir, GNAT_MAX_PATH_LEN);

#else
   getcwd (dir, *length);
#endif

   *length = strlen (dir);

   if (dir [*length - 1] != DIR_SEPARATOR)
     {
       dir [*length] = DIR_SEPARATOR;
       ++(*length);
     }
   dir[*length] = '\0';
}

/* Return the suffix for object files.  */

void
__gnat_get_object_suffix_ptr (int *len, const char **value)
{
  *value = HOST_OBJECT_SUFFIX;

  if (*value == 0)
    *len = 0;
  else
    *len = strlen (*value);

  return;
}

/* Return the suffix for executable files.  */

void
__gnat_get_executable_suffix_ptr (int *len, const char **value)
{
  *value = HOST_EXECUTABLE_SUFFIX;
  if (!*value)
    *len = 0;
  else
    *len = strlen (*value);

  return;
}

/* Return the suffix for debuggable files. Usually this is the same as the
   executable extension.  */

void
__gnat_get_debuggable_suffix_ptr (int *len, const char **value)
{
  *value = HOST_EXECUTABLE_SUFFIX;

  if (*value == 0)
    *len = 0;
  else
    *len = strlen (*value);

  return;
}

/* Returns the OS filename and corresponding encoding.  */

void
__gnat_os_filename (char *filename ATTRIBUTE_UNUSED,
		    char *w_filename ATTRIBUTE_UNUSED,
		    char *os_name, int *o_length,
		    char *encoding ATTRIBUTE_UNUSED, int *e_length)
{
#if defined (_WIN32) && ! defined (__vxworks) && ! defined (IS_CROSS)
  WS2SC (os_name, (TCHAR *)w_filename, (DWORD)*o_length);
  *o_length = strlen (os_name);
  strcpy (encoding, "encoding=utf8");
  *e_length = strlen (encoding);
#else
  strcpy (os_name, filename);
  *o_length = strlen (filename);
  *e_length = 0;
#endif
}

/* Delete a file.  */

int
__gnat_unlink (char *path)
{
#if defined (__MINGW32__) && ! defined (__vxworks) && ! defined (IS_CROSS)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    return _tunlink (wpath);
  }
#else
  return unlink (path);
#endif
}

/* Rename a file.  */

int
__gnat_rename (char *from, char *to)
{
#if defined (__MINGW32__) && ! defined (__vxworks) && ! defined (IS_CROSS)
  {
    TCHAR wfrom[GNAT_MAX_PATH_LEN], wto[GNAT_MAX_PATH_LEN];

    S2WSC (wfrom, from, GNAT_MAX_PATH_LEN);
    S2WSC (wto, to, GNAT_MAX_PATH_LEN);
    return _trename (wfrom, wto);
  }
#else
  return rename (from, to);
#endif
}

/* Changing directory.  */

int
__gnat_chdir (char *path)
{
#if defined (__MINGW32__) && ! defined (__vxworks) && ! defined (IS_CROSS)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    return _tchdir (wpath);
  }
#else
  return chdir (path);
#endif
}

/* Removing a directory.  */

int
__gnat_rmdir (char *path)
{
#if defined (__MINGW32__) && ! defined (__vxworks) && ! defined (IS_CROSS)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    return _trmdir (wpath);
  }
#elif defined (VTHREADS)
  /* rmdir not available */
  return -1;
#else
  return rmdir (path);
#endif
}

#if defined (_WIN32) || defined (__linux__) || defined (__sun__) \
  || defined (__FreeBSD__) || defined(__DragonFly__)
#define HAS_TARGET_WCHAR_T
#endif

#ifdef HAS_TARGET_WCHAR_T
#include <wchar.h>
#endif

int
__gnat_fputwc(int c, FILE *stream)
{
#ifdef HAS_TARGET_WCHAR_T
  return fputwc ((wchar_t)c, stream);
#else
  return fputc (c, stream);
#endif
}

FILE *
__gnat_fopen (char *path, char *mode, int encoding ATTRIBUTE_UNUSED)
{
#if defined (_WIN32) && ! defined (__vxworks) && ! defined (IS_CROSS)
  TCHAR wpath[GNAT_MAX_PATH_LEN];
  TCHAR wmode[10];

  S2WS (wmode, mode, 10);

  if (encoding == Encoding_Unspecified)
    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
  else if (encoding == Encoding_UTF8)
    S2WSU (wpath, path, GNAT_MAX_PATH_LEN);
  else
    S2WS (wpath, path, GNAT_MAX_PATH_LEN);

  return _tfopen (wpath, wmode);

#else
  return GNAT_FOPEN (path, mode);
#endif
}

FILE *
__gnat_freopen (char *path,
		char *mode,
		FILE *stream,
		int encoding ATTRIBUTE_UNUSED)
{
#if defined (_WIN32) && ! defined (__vxworks) && ! defined (IS_CROSS)
  TCHAR wpath[GNAT_MAX_PATH_LEN];
  TCHAR wmode[10];

  S2WS (wmode, mode, 10);

  if (encoding == Encoding_Unspecified)
    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
  else if (encoding == Encoding_UTF8)
    S2WSU (wpath, path, GNAT_MAX_PATH_LEN);
  else
    S2WS (wpath, path, GNAT_MAX_PATH_LEN);

  return _tfreopen (wpath, wmode, stream);
#else
  return freopen (path, mode, stream);
#endif
}

int
__gnat_open_read (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  if (fmode)
    o_fmode = O_TEXT;

#if defined (__vxworks)
  fd = open (path, O_RDONLY | o_fmode, 0444);
#elif defined (__MINGW32__)
 {
   TCHAR wpath[GNAT_MAX_PATH_LEN];

   S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
   fd = _topen (wpath, O_RDONLY | o_fmode, 0444);
 }
#else
  fd = GNAT_OPEN (path, O_RDONLY | o_fmode);
#endif

  return fd < 0 ? -1 : fd;
}

#if defined (__MINGW32__)
#define PERM (S_IREAD | S_IWRITE)
#else
#define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#endif

int
__gnat_open_rw (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  if (fmode)
    o_fmode = O_TEXT;

#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_RDWR | o_fmode, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_RDWR | o_fmode, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

int
__gnat_open_create (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  if (fmode)
    o_fmode = O_TEXT;

#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_WRONLY | O_CREAT | O_TRUNC | o_fmode, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_TRUNC | o_fmode, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

int
__gnat_create_output_file (char *path)
{
  int fd;
#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_WRONLY | O_CREAT | O_TRUNC | O_TEXT, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_TRUNC | O_TEXT, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

int
__gnat_create_output_file_new (char *path)
{
  int fd;
#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_WRONLY | O_CREAT | O_TRUNC | O_TEXT | O_EXCL, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_TRUNC | O_TEXT | O_EXCL, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

int
__gnat_open_append (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  if (fmode)
    o_fmode = O_TEXT;

#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_WRONLY | O_CREAT | O_APPEND | o_fmode, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_APPEND | o_fmode, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

/*  Open a new file.  Return error (-1) if the file already exists.  */

int
__gnat_open_new (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  if (fmode)
    o_fmode = O_TEXT;

#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, O_WRONLY | O_CREAT | O_EXCL | o_fmode, PERM);
  }
#else
  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_EXCL | o_fmode, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

/* Open a new temp file.  Return error (-1) if the file already exists.  */

int
__gnat_open_new_temp (char *path, int fmode)
{
  int fd;
  int o_fmode = O_BINARY;

  strcpy (path, "GNAT-XXXXXX");

#if (defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) \
  || defined (__linux__) || defined (__GLIBC__) || defined (__ANDROID__) \
  || defined (__DragonFly__)) && !defined (__vxworks)
  return mkstemp (path);
#elif defined (__Lynx__)
  mktemp (path);
#else
  if (mktemp (path) == NULL)
    return -1;
#endif

  if (fmode)
    o_fmode = O_TEXT;

  fd = GNAT_OPEN (path, O_WRONLY | O_CREAT | O_EXCL | o_fmode, PERM);
  return fd < 0 ? -1 : fd;
}

int
__gnat_open (char *path, int fmode)
{
  int fd;

#if defined (__MINGW32__)
  {
    TCHAR wpath[GNAT_MAX_PATH_LEN];

    S2WSC (wpath, path, GNAT_MAX_PATH_LEN);
    fd = _topen (wpath, fmode, PERM);
  }
#else
  fd = GNAT_OPEN (path, fmode, PERM);
#endif

  return fd < 0 ? -1 : fd;
}

/****************************************************************
 ** Perform a call to GNAT_STAT or GNAT_FSTAT, and extract as much information
 ** as possible from it, storing the result in a cache for later reuse
 ****************************************************************/

void
__gnat_stat_to_attr (int fd, char* name, struct file_attributes* attr)
{
  GNAT_STRUCT_STAT statbuf;
  int ret, error;

  if (fd != -1) {
    /* GNAT_FSTAT returns -1 and sets errno for failure */
    ret = GNAT_FSTAT (fd, &statbuf);
    error = ret ? errno : 0;

  } else {
    /* __gnat_stat returns errno value directly */
    error = __gnat_stat (name, &statbuf);
    ret = error ? -1 : 0;
  }

  /*
   * A missing file is reported as an attr structure with error == 0 and
   * exists == 0.
   */

  if (error == 0 || error == ENOENT)
    attr->error = 0;
  else
    attr->error = error;

  attr->regular   = (!ret && S_ISREG (statbuf.st_mode));
  attr->directory = (!ret && S_ISDIR (statbuf.st_mode));

  if (!attr->regular)
    attr->file_length = 0;
  else
    /* st_size may be 32 bits, or 64 bits which is converted to long. We
       don't return a useful value for files larger than 2 gigabytes in
       either case. */
    attr->file_length = statbuf.st_size;  /* all systems */

  attr->exists = !ret;

#if !defined (_WIN32)
  /* on Windows requires extra system call, see __gnat_is_readable_file_attr */
  attr->readable   = (!ret && (statbuf.st_mode & S_IRUSR));
  attr->writable   = (!ret && (statbuf.st_mode & S_IWUSR));
  attr->executable = (!ret && (statbuf.st_mode & S_IXUSR));
#endif

  if (ret != 0) {
     attr->timestamp = (OS_Time)-1;
  } else {
     attr->timestamp = (OS_Time)statbuf.st_mtime;
  }
}

/****************************************************************
 ** Return the number of bytes in the specified file
 ****************************************************************/

__int64
__gnat_file_length_attr (int fd, char* name, struct file_attributes* attr)
{
  if (attr->file_length == -1) {
    __gnat_stat_to_attr (fd, name, attr);
  }

  return attr->file_length;
}

__int64
__gnat_file_length (int fd)
{
  struct file_attributes attr;
  __gnat_reset_attributes (&attr);
  return __gnat_file_length_attr (fd, NULL, &attr);
}

long
__gnat_file_length_long (int fd)
{
  struct file_attributes attr;
  __gnat_reset_attributes (&attr);
  return (long)__gnat_file_length_attr (fd, NULL, &attr);
}

__int64
__gnat_named_file_length (char *name)
{
  struct file_attributes attr;
  __gnat_reset_attributes (&attr);
  return __gnat_file_length_attr (-1, name, &attr);
}

/* Create a temporary filename and put it in string pointed to by
   TMP_FILENAME.  */

void
__gnat_tmp_name (char *tmp_filename)
{
#if defined (__MINGW32__)
  {
    char *pname;
    char prefix[25];

    /* tempnam tries to create a temporary file in directory pointed to by
       TMP environment variable, in c:\temp if TMP is not set, and in
       directory specified by P_tmpdir in stdio.h if c:\temp does not
       exist. The filename will be created with the prefix "gnat-".  */

    sprintf (prefix, "gnat-%d-", (int)getpid());
    pname = (char *) _tempnam ("c:\\temp", prefix);

    /* if pname is NULL, the file was not created properly, the disk is full
       or there is no more free temporary files */

    if (pname == NULL)
      *tmp_filename = '\0';

    /* If pname start with a back slash and not path information it means that
       the filename is valid for the current working directory.  */

    else if (pname[0] == '\\')
      {
	strcpy (tmp_filename, ".\\");
	strcat (tmp_filename, pname+1);
      }
    else
      strcpy (tmp_filename, pname);

    free (pname);
  }

#elif defined (__linux__) || defined (__FreeBSD__) || defined (__NetBSD__) \
  || defined (__OpenBSD__) || defined (__GLIBC__) || defined (__ANDROID__) \
  || defined (__DragonFly__)
#define MAX_SAFE_PATH 1000
  char *tmpdir = getenv ("TMPDIR");

  /* If tmpdir is longer than MAX_SAFE_PATH, revert to default value to avoid
     a buffer overflow.  */
  if (tmpdir == NULL || strlen (tmpdir) > MAX_SAFE_PATH)
#ifdef __ANDROID__
    strcpy (tmp_filename, "/cache/gnat-XXXXXX");
#else
    strcpy (tmp_filename, "/tmp/gnat-XXXXXX");
#endif
  else
    sprintf (tmp_filename, "%s/gnat-XXXXXX", tmpdir);

  close (mkstemp(tmp_filename));
#elif defined (__vxworks) && !defined (VTHREADS)
  int index;
  char *pos;
  char *savepos;
  static ushort_t seed = 0; /* used to generate unique name */

  /* Generate a unique name.  */
  strcpy (tmp_filename, "tmp");

  index = 5;
  savepos = pos = tmp_filename + strlen (tmp_filename) + index;
  *pos = '\0';

  while (1)
    {
      FILE *f;
      ushort_t t;

      /* Fill up the name buffer from the last position.  */
      seed++;
      for (t = seed; 0 <= --index; t >>= 3)
        *--pos = '0' + (t & 07);

      /* Check to see if its unique, if not bump the seed and try again.  */
      f = fopen (tmp_filename, "r");
      if (f == NULL)
        break;
      fclose (f);
      pos = savepos;
      index = 5;
    }
#else
  tmpnam (tmp_filename);
#endif
}

/*  Open directory and returns a DIR pointer.  */

DIR* __gnat_opendir (char *name)
{
#if defined (__MINGW32__)
  TCHAR wname[GNAT_MAX_PATH_LEN];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN);
  return (DIR*)_topendir (wname);

#else
  return opendir (name);
#endif
}

/* Read the next entry in a directory.  The returned string points somewhere
   in the buffer.  */

#if defined (__sun__)
/* For Solaris, be sure to use the 64-bit version, otherwise NFS reads may
   fail with EOVERFLOW if the server uses 64-bit cookies.  */
#define dirent dirent64
#define readdir readdir64
#endif

char *
__gnat_readdir (DIR *dirp, char *buffer, int *len)
{
#if defined (__MINGW32__)
  struct _tdirent *dirent = _treaddir ((_TDIR*)dirp);

  if (dirent != NULL)
    {
      WS2SC (buffer, dirent->d_name, GNAT_MAX_PATH_LEN);
      *len = strlen (buffer);

      return buffer;
    }
  else
    return NULL;

#elif defined (HAVE_READDIR_R)
  /* If possible, try to use the thread-safe version.  */
  if (readdir_r (dirp, buffer) != NULL)
    {
      *len = strlen (((struct dirent*) buffer)->d_name);
      return ((struct dirent*) buffer)->d_name;
    }
  else
    return NULL;

#else
  struct dirent *dirent = (struct dirent *) readdir (dirp);

  if (dirent != NULL)
    {
      strcpy (buffer, dirent->d_name);
      *len = strlen (buffer);
      return buffer;
    }
  else
    return NULL;

#endif
}

/* Close a directory entry.  */

int __gnat_closedir (DIR *dirp)
{
#if defined (__MINGW32__)
  return _tclosedir ((_TDIR*)dirp);

#else
  return closedir (dirp);
#endif
}

/* Returns 1 if readdir is thread safe, 0 otherwise.  */

int
__gnat_readdir_is_thread_safe (void)
{
#ifdef HAVE_READDIR_R
  return 1;
#else
  return 0;
#endif
}

#if defined (_WIN32)
/* Number of seconds between <Jan 1st 1601> and <Jan 1st 1970>.  */
static const unsigned long long w32_epoch_offset = 11644473600ULL;

/* Returns the file modification timestamp using Win32 routines which are
   immune against daylight saving time change. It is in fact not possible to
   use fstat for this purpose as the DST modify the st_mtime field of the
   stat structure.  */

static time_t
win32_filetime (HANDLE h)
{
  union
  {
    FILETIME ft_time;
    unsigned long long ull_time;
  } t_write;

  /* GetFileTime returns FILETIME data which are the number of 100 nanosecs
     since <Jan 1st 1601>. This function must return the number of seconds
     since <Jan 1st 1970>.  */

  if (GetFileTime (h, NULL, NULL, &t_write.ft_time))
    return (time_t) (t_write.ull_time / 10000000ULL - w32_epoch_offset);
  return (time_t) 0;
}

/* As above but starting from a FILETIME.  */
static void
f2t (const FILETIME *ft, __time64_t *t)
{
  union
  {
    FILETIME ft_time;
    unsigned long long ull_time;
  } t_write;

  t_write.ft_time = *ft;
  *t = (__time64_t) (t_write.ull_time / 10000000ULL - w32_epoch_offset);
}
#endif

/* Return a GNAT time stamp given a file name.  */

OS_Time
__gnat_file_time_name_attr (char* name, struct file_attributes* attr)
{
   if (attr->timestamp == (OS_Time)-2) {
#if defined (_WIN32)
      BOOL res;
      WIN32_FILE_ATTRIBUTE_DATA fad;
      __time64_t ret = -1;
      TCHAR wname[GNAT_MAX_PATH_LEN];
      S2WSC (wname, name, GNAT_MAX_PATH_LEN);

      if ((res = GetFileAttributesEx (wname, GetFileExInfoStandard, &fad)))
	f2t (&fad.ftLastWriteTime, &ret);
      attr->timestamp = (OS_Time) ret;
#else
      __gnat_stat_to_attr (-1, name, attr);
#endif
  }
  return attr->timestamp;
}

OS_Time
__gnat_file_time_name (char *name)
{
   struct file_attributes attr;
   __gnat_reset_attributes (&attr);
   return __gnat_file_time_name_attr (name, &attr);
}

/* Return a GNAT time stamp given a file descriptor.  */

OS_Time
__gnat_file_time_fd_attr (int fd, struct file_attributes* attr)
{
   if (attr->timestamp == (OS_Time)-2) {
#if defined (_WIN32)
     HANDLE h = (HANDLE) _get_osfhandle (fd);
     time_t ret = win32_filetime (h);
     attr->timestamp = (OS_Time) ret;

#else
     __gnat_stat_to_attr (fd, NULL, attr);
#endif
   }

   return attr->timestamp;
}

OS_Time
__gnat_file_time_fd (int fd)
{
   struct file_attributes attr;
   __gnat_reset_attributes (&attr);
   return __gnat_file_time_fd_attr (fd, &attr);
}

/* Set the file time stamp.  */

void
__gnat_set_file_time_name (char *name, time_t time_stamp)
{
#if defined (__vxworks)

/* Code to implement __gnat_set_file_time_name for these systems.  */

#elif defined (_WIN32)
  union
  {
    FILETIME ft_time;
    unsigned long long ull_time;
  } t_write;
  TCHAR wname[GNAT_MAX_PATH_LEN];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN);

  HANDLE h  = CreateFile
    (wname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
     OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
     NULL);
  if (h == INVALID_HANDLE_VALUE)
    return;
  /* Add number of seconds between <Jan 1st 1601> and <Jan 1st 1970> */
  t_write.ull_time = ((unsigned long long)time_stamp + w32_epoch_offset);
  /*  Convert to 100 nanosecond units  */
  t_write.ull_time *= 10000000ULL;

  SetFileTime(h, NULL, NULL, &t_write.ft_time);
  CloseHandle (h);
  return;

#else
  struct utimbuf utimbuf;
  time_t t;

  /* Set modification time to requested time.  */
  utimbuf.modtime = time_stamp;

  /* Set access time to now in local time.  */
  t = time ((time_t) 0);
  utimbuf.actime = mktime (localtime (&t));

  utime (name, &utimbuf);
#endif
}

/* Get the list of installed standard libraries from the
   HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\GNAT\Standard Libraries
   key.  */

char *
__gnat_get_libraries_from_registry (void)
{
  char *result = (char *) xmalloc (1);

  result[0] = '\0';

#if defined (_WIN32) && ! defined (__vxworks) && ! defined (IS_CROSS)

  HKEY reg_key;
  DWORD name_size, value_size;
  char name[256];
  char value[256];
  DWORD type;
  DWORD index;
  LONG res;

  /* First open the key.  */
  res = RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE", 0, KEY_READ, &reg_key);

  if (res == ERROR_SUCCESS)
    res = RegOpenKeyExA (reg_key, "Ada Core Technologies", 0,
                         KEY_READ, &reg_key);

  if (res == ERROR_SUCCESS)
    res = RegOpenKeyExA (reg_key, "GNAT", 0, KEY_READ, &reg_key);

  if (res == ERROR_SUCCESS)
    res = RegOpenKeyExA (reg_key, "Standard Libraries", 0, KEY_READ, &reg_key);

  /* If the key exists, read out all the values in it and concatenate them
     into a path.  */
  for (index = 0; res == ERROR_SUCCESS; index++)
    {
      value_size = name_size = 256;
      res = RegEnumValueA (reg_key, index, name, &name_size, 0,
                           &type, (LPBYTE)value, &value_size);

      if (res == ERROR_SUCCESS && type == REG_SZ)
        {
          char *old_result = result;

          result = (char *) xmalloc (strlen (old_result) + value_size + 2);
          strcpy (result, old_result);
          strcat (result, value);
          strcat (result, ";");
          free (old_result);
        }
    }

  /* Remove the trailing ";".  */
  if (result[0] != 0)
    result[strlen (result) - 1] = 0;

#endif
  return result;
}

/* Query information for the given file NAME and return it in STATBUF.
 * Returns 0 for success, or errno value for failure.
 */
int
__gnat_stat (char *name, GNAT_STRUCT_STAT *statbuf)
{
#ifdef __MINGW32__
  WIN32_FILE_ATTRIBUTE_DATA fad;
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];
  int name_len;
  BOOL res;
  DWORD error;

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);
  name_len = _tcslen (wname);

  if (name_len > GNAT_MAX_PATH_LEN)
    return EINVAL;

  ZeroMemory (statbuf, sizeof(GNAT_STRUCT_STAT));

  res = GetFileAttributesEx (wname, GetFileExInfoStandard, &fad);

  if (res == FALSE) {
    error = GetLastError();

    /* Check file existence using GetFileAttributes() which does not fail on
       special Windows files like con:, aux:, nul: etc...  */

    if (GetFileAttributes(wname) != INVALID_FILE_ATTRIBUTES) {
      /* Just pretend that it is a regular and readable file  */
      statbuf->st_mode = S_IFREG | S_IREAD | S_IWRITE;
      return 0;
    }

    switch (error) {
      case ERROR_ACCESS_DENIED:
      case ERROR_SHARING_VIOLATION:
      case ERROR_LOCK_VIOLATION:
      case ERROR_SHARING_BUFFER_EXCEEDED:
	return EACCES;
      case ERROR_BUFFER_OVERFLOW:
	return ENAMETOOLONG;
      case ERROR_NOT_ENOUGH_MEMORY:
	return ENOMEM;
      default:
	return ENOENT;
    }
  }

  f2t (&fad.ftCreationTime, &statbuf->st_ctime);
  f2t (&fad.ftLastWriteTime, &statbuf->st_mtime);
  f2t (&fad.ftLastAccessTime, &statbuf->st_atime);

  statbuf->st_size =
    (__int64)fad.nFileSizeLow | (__int64)fad.nFileSizeHigh << 32;

  /* We do not have the S_IEXEC attribute, but this is not used on GNAT.  */
  statbuf->st_mode = S_IREAD;

  if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    statbuf->st_mode |= S_IFDIR;
  else
    statbuf->st_mode |= S_IFREG;

  if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
    statbuf->st_mode |= S_IWRITE;

  return 0;

#else
  return GNAT_STAT (name, statbuf) == 0 ? 0 : errno;
#endif
}

/*************************************************************************
 ** Check whether a file exists
 *************************************************************************/

int
__gnat_file_exists_attr (char* name, struct file_attributes* attr)
{
   if (attr->exists == ATTR_UNSET)
     __gnat_stat_to_attr (-1, name, attr);

   return attr->exists;
}

int
__gnat_file_exists (char *name)
{
   struct file_attributes attr;
   __gnat_reset_attributes (&attr);
   return __gnat_file_exists_attr (name, &attr);
}

/**********************************************************************
 ** Whether name is an absolute path
 **********************************************************************/

int
__gnat_is_absolute_path (char *name, int length)
{
#ifdef __vxworks
  /* On VxWorks systems, an absolute path can be represented (depending on
     the host platform) as either /dir/file, or device:/dir/file, or
     device:drive_letter:/dir/file. */

  int index;

  if (name[0] == '/')
    return 1;

  for (index = 0; index < length; index++)
    {
      if (name[index] == ':' &&
          ((name[index + 1] == '/') ||
           (isalpha (name[index + 1]) && index + 2 <= length &&
            name[index + 2] == '/')))
        return 1;

      else if (name[index] == '/')
        return 0;
    }
  return 0;
#else
  return (length != 0) &&
     (*name == '/' || *name == DIR_SEPARATOR
#if defined (WINNT) || defined(__DJGPP__)
      || (length > 1 && ISALPHA (name[0]) && name[1] == ':')
#endif
	  );
#endif
}

int
__gnat_is_regular_file_attr (char* name, struct file_attributes* attr)
{
   if (attr->regular == ATTR_UNSET)
     __gnat_stat_to_attr (-1, name, attr);

   return attr->regular;
}

int
__gnat_is_regular_file (char *name)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_regular_file_attr (name, &attr);
}

int
__gnat_is_regular_file_fd (int fd)
{
  int ret;
  GNAT_STRUCT_STAT statbuf;

  ret = GNAT_FSTAT (fd, &statbuf);
  return (!ret && S_ISREG (statbuf.st_mode));
}

int
__gnat_is_directory_attr (char* name, struct file_attributes* attr)
{
   if (attr->directory == ATTR_UNSET)
     __gnat_stat_to_attr (-1, name, attr);

   return attr->directory;
}

int
__gnat_is_directory (char *name)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_directory_attr (name, &attr);
}

#if defined (_WIN32)

/* Returns the same constant as GetDriveType but takes a pathname as
   argument. */

static UINT
GetDriveTypeFromPath (TCHAR *wfullpath)
{
  TCHAR wdrv[MAX_PATH];
  TCHAR wpath[MAX_PATH];
  TCHAR wfilename[MAX_PATH];
  TCHAR wext[MAX_PATH];

  _tsplitpath (wfullpath, wdrv, wpath, wfilename, wext);

  if (_tcslen (wdrv) != 0)
    {
      /* we have a drive specified. */
      _tcscat (wdrv, _T("\\"));
      return GetDriveType (wdrv);
    }
  else
    {
      /* No drive specified. */

      /* Is this a relative path, if so get current drive type. */
      if (wpath[0] != _T('\\') ||
	  (_tcslen (wpath) > 2 && wpath[0] == _T('\\')
	   && wpath[1] != _T('\\')))
	return GetDriveType (NULL);

      UINT result = GetDriveType (wpath);

      /* Cannot guess the drive type, is this \\.\ ? */

      if (result == DRIVE_NO_ROOT_DIR &&
	 _tcslen (wpath) >= 4 && wpath[0] == _T('\\') && wpath[1] == _T('\\')
	  && wpath[2] == _T('.') && wpath[3] == _T('\\'))
	{
	  if (_tcslen (wpath) == 4)
	    _tcscat (wpath, wfilename);

	  LPTSTR p = &wpath[4];
	  LPTSTR b = _tcschr (p, _T('\\'));

	  if (b != NULL)
	    {
	      /* logical drive \\.\c\dir\file */
	      *b++ = _T(':');
	      *b++ = _T('\\');
	      *b = _T('\0');
	    }
	  else
	    _tcscat (p, _T(":\\"));

	  return GetDriveType (p);
	}

      return result;
    }
}

/*  This MingW section contains code to work with ACL.  */
static int
__gnat_check_OWNER_ACL (TCHAR *wname,
			DWORD CheckAccessDesired,
			GENERIC_MAPPING CheckGenericMapping)
{
  DWORD dwAccessDesired, dwAccessAllowed;
  PRIVILEGE_SET PrivilegeSet;
  DWORD dwPrivSetSize = sizeof (PRIVILEGE_SET);
  BOOL fAccessGranted = FALSE;
  HANDLE hToken = NULL;
  DWORD nLength = 0;
  PSECURITY_DESCRIPTOR pSD = NULL;

  GetFileSecurity
    (wname, OWNER_SECURITY_INFORMATION |
     GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
     NULL, 0, &nLength);

  if ((pSD = (SECURITY_DESCRIPTOR *) HeapAlloc
       (GetProcessHeap (), HEAP_ZERO_MEMORY, nLength)) == NULL)
    return 0;

  /* Obtain the security descriptor.  */

  if (!GetFileSecurity
      (wname, OWNER_SECURITY_INFORMATION |
       GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
       pSD, nLength, &nLength))
    goto error;

  if (!ImpersonateSelf (SecurityImpersonation))
    goto error;

  if (!OpenThreadToken
      (GetCurrentThread(), TOKEN_DUPLICATE | TOKEN_QUERY, FALSE, &hToken))
    goto error;

  /*  Undoes the effect of ImpersonateSelf. */

  RevertToSelf ();

  /*  We want to test for write permissions. */

  dwAccessDesired = CheckAccessDesired;

  MapGenericMask (&dwAccessDesired, &CheckGenericMapping);

  if (!AccessCheck
      (pSD ,                 /* security descriptor to check */
       hToken,               /* impersonation token */
       dwAccessDesired,      /* requested access rights */
       &CheckGenericMapping, /* pointer to GENERIC_MAPPING */
       &PrivilegeSet,        /* receives privileges used in check */
       &dwPrivSetSize,       /* size of PrivilegeSet buffer */
       &dwAccessAllowed,     /* receives mask of allowed access rights */
       &fAccessGranted))
    goto error;

  CloseHandle (hToken);
  HeapFree (GetProcessHeap (), 0, pSD);
  return fAccessGranted;

 error:
  if (hToken)
    CloseHandle (hToken);
  HeapFree (GetProcessHeap (), 0, pSD);
  return 0;
}

static void
__gnat_set_OWNER_ACL (TCHAR *wname,
		      ACCESS_MODE AccessMode,
		      DWORD AccessPermissions)
{
  PACL pOldDACL = NULL;
  PACL pNewDACL = NULL;
  PSECURITY_DESCRIPTOR pSD = NULL;
  EXPLICIT_ACCESS ea;
  TCHAR username [100];
  DWORD unsize = 100;

  /*  Get current user, he will act as the owner */

  if (!GetUserName (username, &unsize))
    return;

  if (GetNamedSecurityInfo
      (wname,
       SE_FILE_OBJECT,
       DACL_SECURITY_INFORMATION,
       NULL, NULL, &pOldDACL, NULL, &pSD) != ERROR_SUCCESS)
    return;

  BuildExplicitAccessWithName
    (&ea, username, AccessPermissions, (ACCESS_MODE) AccessMode, NO_INHERITANCE);

  if (AccessMode == SET_ACCESS)
    {
      /*  SET_ACCESS, we want to set an explicte set of permissions, do not
	  merge with current DACL.  */
      if (SetEntriesInAcl (1, &ea, NULL, &pNewDACL) != ERROR_SUCCESS)
	return;
    }
  else
    if (SetEntriesInAcl (1, &ea, pOldDACL, &pNewDACL) != ERROR_SUCCESS)
      return;

  if (SetNamedSecurityInfo
      (wname, SE_FILE_OBJECT,
       DACL_SECURITY_INFORMATION, NULL, NULL, pNewDACL, NULL) != ERROR_SUCCESS)
    return;

  LocalFree (pSD);
  LocalFree (pNewDACL);
}

/* Check if it is possible to use ACL for wname, the file must not be on a
   network drive. */

static int
__gnat_can_use_acl (TCHAR *wname)
{
  return __gnat_use_acl && GetDriveTypeFromPath (wname) != DRIVE_REMOTE;
}

#endif /* defined (_WIN32) */

int
__gnat_is_readable_file_attr (char* name, struct file_attributes* attr)
{
   if (attr->readable == ATTR_UNSET)
     {
#if defined (_WIN32)
       TCHAR wname [GNAT_MAX_PATH_LEN + 2];
       GENERIC_MAPPING GenericMapping;

       S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

       if (__gnat_can_use_acl (wname))
	 {
	   ZeroMemory (&GenericMapping, sizeof (GENERIC_MAPPING));
	   GenericMapping.GenericRead = GENERIC_READ;
	   attr->readable =
	     __gnat_check_OWNER_ACL (wname, FILE_READ_DATA, GenericMapping);
	 }
       else
	 attr->readable = GetFileAttributes (wname) != INVALID_FILE_ATTRIBUTES;
#else
       __gnat_stat_to_attr (-1, name, attr);
#endif
     }

   return attr->readable;
}

int
__gnat_is_read_accessible_file (char *name)
{
#if defined (_WIN32)
   TCHAR wname [GNAT_MAX_PATH_LEN + 2];

   S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

   return !_waccess (wname, 4);

#elif defined (__vxworks)
   int fd;

   if ((fd = open (name, O_RDONLY, 0)) < 0)
     return 0;
   close (fd);
   return 1;

#else
   return !access (name, R_OK);
#endif
}

int
__gnat_is_readable_file (char *name)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_readable_file_attr (name, &attr);
}

int
__gnat_is_writable_file_attr (char* name, struct file_attributes* attr)
{
   if (attr->writable == ATTR_UNSET)
     {
#if defined (_WIN32)
       TCHAR wname [GNAT_MAX_PATH_LEN + 2];
       GENERIC_MAPPING GenericMapping;

       S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

       if (__gnat_can_use_acl (wname))
	 {
	   ZeroMemory (&GenericMapping, sizeof (GENERIC_MAPPING));
	   GenericMapping.GenericWrite = GENERIC_WRITE;

	   attr->writable = __gnat_check_OWNER_ACL
   	     (wname, FILE_WRITE_DATA | FILE_APPEND_DATA, GenericMapping)
   	     && !(GetFileAttributes (wname) & FILE_ATTRIBUTE_READONLY);
	 }
       else
	 attr->writable =
	   !(GetFileAttributes (wname) & FILE_ATTRIBUTE_READONLY);

#else
       __gnat_stat_to_attr (-1, name, attr);
#endif
     }

   return attr->writable;
}

int
__gnat_is_writable_file (char *name)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_writable_file_attr (name, &attr);
}

int
__gnat_is_write_accessible_file (char *name)
{
#if defined (_WIN32)
   TCHAR wname [GNAT_MAX_PATH_LEN + 2];

   S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

   return !_waccess (wname, 2);

#elif defined (__vxworks)
   int fd;

   if ((fd = open (name, O_WRONLY, 0)) < 0)
     return 0;
   close (fd);
   return 1;

#else
   return !access (name, W_OK);
#endif
}

int
__gnat_is_executable_file_attr (char* name, struct file_attributes* attr)
{
   if (attr->executable == ATTR_UNSET)
     {
#if defined (_WIN32)
       TCHAR wname [GNAT_MAX_PATH_LEN + 2];
       GENERIC_MAPPING GenericMapping;

       S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

       if (__gnat_can_use_acl (wname))
	 {
	   ZeroMemory (&GenericMapping, sizeof (GENERIC_MAPPING));
	   GenericMapping.GenericExecute = GENERIC_EXECUTE;

	   attr->executable =
	     __gnat_check_OWNER_ACL (wname, FILE_EXECUTE, GenericMapping);
	 }
       else
	 {
	   TCHAR *l, *last = _tcsstr(wname, _T(".exe"));

	   /* look for last .exe */
	   if (last)
	     while ((l = _tcsstr(last+1, _T(".exe"))))
	       last = l;

	   attr->executable =
	     GetFileAttributes (wname) != INVALID_FILE_ATTRIBUTES
	     && (last - wname) == (int) (_tcslen (wname) - 4);
	 }
#else
       __gnat_stat_to_attr (-1, name, attr);
#endif
     }

   return attr->regular && attr->executable;
}

int
__gnat_is_executable_file (char *name)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_executable_file_attr (name, &attr);
}

void
__gnat_set_writable (char *name)
{
#if defined (_WIN32)
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

  if (__gnat_can_use_acl (wname))
    __gnat_set_OWNER_ACL (wname, GRANT_ACCESS, FILE_GENERIC_WRITE);

  SetFileAttributes
    (wname, GetFileAttributes (wname) & ~FILE_ATTRIBUTE_READONLY);
#elif ! (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  GNAT_STRUCT_STAT statbuf;

  if (GNAT_STAT (name, &statbuf) == 0)
    {
      statbuf.st_mode = statbuf.st_mode | S_IWUSR;
      chmod (name, statbuf.st_mode);
    }
#endif
}

/* must match definition in s-os_lib.ads */
#define S_OWNER  1
#define S_GROUP  2
#define S_OTHERS 4

void
__gnat_set_executable (char *name, int mode ATTRIBUTE_UNUSED)
{
#if defined (_WIN32)
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

  if (__gnat_can_use_acl (wname))
    __gnat_set_OWNER_ACL (wname, GRANT_ACCESS, FILE_GENERIC_EXECUTE);

#elif ! (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  GNAT_STRUCT_STAT statbuf;

  if (GNAT_STAT (name, &statbuf) == 0)
    {
      if (mode & S_OWNER)
        statbuf.st_mode = statbuf.st_mode | S_IXUSR;
      if (mode & S_GROUP)
        statbuf.st_mode = statbuf.st_mode | S_IXGRP;
      if (mode & S_OTHERS)
        statbuf.st_mode = statbuf.st_mode | S_IXOTH;
      chmod (name, statbuf.st_mode);
    }
#endif
}

void
__gnat_set_non_writable (char *name)
{
#if defined (_WIN32)
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

  if (__gnat_can_use_acl (wname))
    __gnat_set_OWNER_ACL
      (wname, DENY_ACCESS,
       FILE_WRITE_DATA | FILE_APPEND_DATA |
       FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES);

  SetFileAttributes
    (wname, GetFileAttributes (wname) | FILE_ATTRIBUTE_READONLY);
#elif ! (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  GNAT_STRUCT_STAT statbuf;

  if (GNAT_STAT (name, &statbuf) == 0)
    {
      statbuf.st_mode = statbuf.st_mode & 07577;
      chmod (name, statbuf.st_mode);
    }
#endif
}

void
__gnat_set_readable (char *name)
{
#if defined (_WIN32)
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

  if (__gnat_can_use_acl (wname))
    __gnat_set_OWNER_ACL (wname, GRANT_ACCESS, FILE_GENERIC_READ);

#elif ! (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  GNAT_STRUCT_STAT statbuf;

  if (GNAT_STAT (name, &statbuf) == 0)
    {
      chmod (name, statbuf.st_mode | S_IREAD);
    }
#endif
}

void
__gnat_set_non_readable (char *name)
{
#if defined (_WIN32)
  TCHAR wname [GNAT_MAX_PATH_LEN + 2];

  S2WSC (wname, name, GNAT_MAX_PATH_LEN + 2);

  if (__gnat_can_use_acl (wname))
    __gnat_set_OWNER_ACL (wname, DENY_ACCESS, FILE_GENERIC_READ);

#elif ! (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  GNAT_STRUCT_STAT statbuf;

  if (GNAT_STAT (name, &statbuf) == 0)
    {
      chmod (name, statbuf.st_mode & (~S_IREAD));
    }
#endif
}

int
__gnat_is_symbolic_link_attr (char* name ATTRIBUTE_UNUSED,
                              struct file_attributes* attr)
{
   if (attr->symbolic_link == ATTR_UNSET)
     {
#if defined (__vxworks)
       attr->symbolic_link = 0;

#elif defined (_AIX) || defined (__APPLE__) || defined (__unix__)
       int ret;
       GNAT_STRUCT_STAT statbuf;
       ret = GNAT_LSTAT (name, &statbuf);
       attr->symbolic_link = (!ret && S_ISLNK (statbuf.st_mode));
#else
       attr->symbolic_link = 0;
#endif
     }
   return attr->symbolic_link;
}

int
__gnat_is_symbolic_link (char *name ATTRIBUTE_UNUSED)
{
   struct file_attributes attr;

   __gnat_reset_attributes (&attr);
   return __gnat_is_symbolic_link_attr (name, &attr);
}

#if defined (__sun__)
/* Using fork on Solaris will duplicate all the threads. fork1, which
   duplicates only the active thread, must be used instead, or spawning
   subprocess from a program with tasking will lead into numerous problems.  */
#define fork fork1
#endif

int
__gnat_portable_spawn (char *args[] ATTRIBUTE_UNUSED)
{
  int status ATTRIBUTE_UNUSED = 0;
  int finished ATTRIBUTE_UNUSED;
  int pid ATTRIBUTE_UNUSED;

#if defined (__vxworks) || defined(__PikeOS__)
  return -1;

#elif defined (__DJGPP__) || defined (_WIN32)
  /* args[0] must be quotes as it could contain a full pathname with spaces */
  char *args_0 = args[0];
  args[0] = (char *)xmalloc (strlen (args_0) + 3);
  strcpy (args[0], "\"");
  strcat (args[0], args_0);
  strcat (args[0], "\"");

  status = spawnvp (P_WAIT, args_0, (char ** const)args);

  /* restore previous value */
  free (args[0]);
  args[0] = (char *)args_0;

  if (status < 0)
    return -1;
  else
    return status;

#else

  pid = fork ();
  if (pid < 0)
    return -1;

  if (pid == 0)
    {
      /* The child. */
      if (execv (args[0], MAYBE_TO_PTR32 (args)) != 0)
	_exit (1);
    }

  /* The parent.  */
  finished = waitpid (pid, &status, 0);

  if (finished != pid || WIFEXITED (status) == 0)
    return -1;

  return WEXITSTATUS (status);
#endif

  return 0;
}

/* Create a copy of the given file descriptor.
   Return -1 if an error occurred.  */

int
__gnat_dup (int oldfd)
{
#if defined (__vxworks) && !defined (__RTP__)
  /* Not supported on VxWorks 5.x, but supported on VxWorks 6.0 when using
     RTPs. */
  return -1;
#else
  return dup (oldfd);
#endif
}

/* Make newfd be the copy of oldfd, closing newfd first if necessary.
   Return -1 if an error occurred.  */

int
__gnat_dup2 (int oldfd ATTRIBUTE_UNUSED, int newfd ATTRIBUTE_UNUSED)
{
#if defined (__vxworks) && !defined (__RTP__)
  /* Not supported on VxWorks 5.x, but supported on VxWorks 6.0 when using
     RTPs.  */
  return -1;
#elif defined (__PikeOS__)
  /* Not supported.  */
  return -1;
#elif defined (_WIN32)
  /* Special case when oldfd and newfd are identical and are the standard
     input, output or error as this makes Windows XP hangs. Note that we
     do that only for standard file descriptors that are known to be valid. */
  if (oldfd == newfd && newfd >= 0 && newfd <= 2)
    return newfd;
  else
    return dup2 (oldfd, newfd);
#else
  return dup2 (oldfd, newfd);
#endif
}

int
__gnat_number_of_cpus (void)
{
  int cores = 1;

#if defined (__linux__) || defined (__sun__) || defined (_AIX) \
  || defined (__APPLE__) || defined (__FreeBSD__) || defined (__OpenBSD__) \
  || defined (__DragonFly__) || defined (__NetBSD__)
  cores = (int) sysconf (_SC_NPROCESSORS_ONLN);

#elif defined (__hpux__)
  struct pst_dynamic psd;
  if (pstat_getdynamic (&psd, sizeof (psd), 1, 0) != -1)
    cores = (int) psd.psd_proc_cnt;

#elif defined (_WIN32)
  SYSTEM_INFO sysinfo;
  GetSystemInfo (&sysinfo);
  cores = (int) sysinfo.dwNumberOfProcessors;

#elif defined (_WRS_CONFIG_SMP)
  unsigned int vxCpuConfiguredGet (void);

  cores = vxCpuConfiguredGet ();

#endif

  return cores;
}

/* WIN32 code to implement a wait call that wait for any child process.  */

#if defined (_WIN32)

/* Synchronization code, to be thread safe.  */

#ifdef CERT

/* For the Cert run times on native Windows we use dummy functions
   for locking and unlocking tasks since we do not support multiple
   threads on this configuration (Cert run time on native Windows). */

static void EnterCS (void) {}
static void LeaveCS (void) {}
static void SignalListChanged (void) {}

#else

CRITICAL_SECTION ProcListCS;
HANDLE ProcListEvt = NULL;

static void EnterCS (void)
{
  EnterCriticalSection(&ProcListCS);
}

static void LeaveCS (void)
{
  LeaveCriticalSection(&ProcListCS);
}

static void SignalListChanged (void)
{
  SetEvent (ProcListEvt);
}

#endif

static HANDLE *HANDLES_LIST = NULL;
static int *PID_LIST = NULL, plist_length = 0, plist_max_length = 0;

static void
add_handle (HANDLE h, int pid)
{
  /* -------------------- critical section -------------------- */
  EnterCS();

  if (plist_length == plist_max_length)
    {
      plist_max_length += 100;
      HANDLES_LIST =
        (HANDLE *) xrealloc (HANDLES_LIST, sizeof (HANDLE) * plist_max_length);
      PID_LIST =
        (int *) xrealloc (PID_LIST, sizeof (int) * plist_max_length);
    }

  HANDLES_LIST[plist_length] = h;
  PID_LIST[plist_length] = pid;
  ++plist_length;

  SignalListChanged();
  LeaveCS();
  /* -------------------- critical section -------------------- */
}

int
__gnat_win32_remove_handle (HANDLE h, int pid)
{
  int j;
  int found = 0;

  /* -------------------- critical section -------------------- */
  EnterCS();

  for (j = 0; j < plist_length; j++)
    {
      if ((HANDLES_LIST[j] == h) || (PID_LIST[j] == pid))
        {
          CloseHandle (h);
          --plist_length;
          HANDLES_LIST[j] = HANDLES_LIST[plist_length];
          PID_LIST[j] = PID_LIST[plist_length];
          found = 1;
          break;
        }
    }

  LeaveCS();
  /* -------------------- critical section -------------------- */

  if (found)
    SignalListChanged();

  return found;
}

static void
win32_no_block_spawn (char *command, char *args[], HANDLE *h, int *pid)
{
  BOOL result;
  STARTUPINFO SI;
  PROCESS_INFORMATION PI;
  SECURITY_ATTRIBUTES SA;
  int csize = 1;
  char *full_command;
  int k;

  /* compute the total command line length */
  k = 0;
  while (args[k])
    {
      csize += strlen (args[k]) + 1;
      k++;
    }

  full_command = (char *) xmalloc (csize);

  /* Startup info. */
  SI.cb          = sizeof (STARTUPINFO);
  SI.lpReserved  = NULL;
  SI.lpReserved2 = NULL;
  SI.lpDesktop   = NULL;
  SI.cbReserved2 = 0;
  SI.lpTitle     = NULL;
  SI.dwFlags     = 0;
  SI.wShowWindow = SW_HIDE;

  /* Security attributes. */
  SA.nLength = sizeof (SECURITY_ATTRIBUTES);
  SA.bInheritHandle = TRUE;
  SA.lpSecurityDescriptor = NULL;

  /* Prepare the command string. */
  strcpy (full_command, command);
  strcat (full_command, " ");

  k = 1;
  while (args[k])
    {
      strcat (full_command, args[k]);
      strcat (full_command, " ");
      k++;
    }

  {
    int wsize = csize * 2;
    TCHAR *wcommand = (TCHAR *) xmalloc (wsize);

    S2WSC (wcommand, full_command, wsize);

    free (full_command);

    result = CreateProcess
      (NULL, wcommand, &SA, NULL, TRUE,
       GetPriorityClass (GetCurrentProcess()), NULL, NULL, &SI, &PI);

    free (wcommand);
  }

  if (result == TRUE)
    {
      CloseHandle (PI.hThread);
      *h = PI.hProcess;
      *pid = PI.dwProcessId;
    }
  else
    {
      *h = NULL;
      *pid = 0;
    }
}

static int
win32_wait (int *status)
{
  DWORD exitcode, pid;
  HANDLE *hl;
  HANDLE h;
  int *pidl;
  DWORD res;
  int hl_len;
  int found;

 START_WAIT:

  if (plist_length == 0)
    {
      errno = ECHILD;
      return -1;
    }

  /* -------------------- critical section -------------------- */
  EnterCS();

  hl_len = plist_length;

#ifdef CERT
  hl = (HANDLE *) xmalloc (sizeof (HANDLE) * hl_len);
  memmove (hl, HANDLES_LIST, sizeof (HANDLE) * hl_len);
  pidl = (int *) xmalloc (sizeof (int) * hl_len);
  memmove (pidl, PID_LIST, sizeof (int) * hl_len);
#else
  /* Note that index 0 contains the event handle that is signaled when the
     process list has changed */
  hl = (HANDLE *) xmalloc (sizeof (HANDLE) * hl_len + 1);
  hl[0] = ProcListEvt;
  memmove (&hl[1], HANDLES_LIST, sizeof (HANDLE) * hl_len);
  pidl = (int *) xmalloc (sizeof (int) * hl_len + 1);
  memmove (&pidl[1], PID_LIST, sizeof (int) * hl_len);
  hl_len++;
#endif

  LeaveCS();
  /* -------------------- critical section -------------------- */

  res = WaitForMultipleObjects (hl_len, hl, FALSE, INFINITE);

  /* if the ProcListEvt has been signaled then the list of processes has been
     updated to add or remove a handle, just loop over */

  if (res - WAIT_OBJECT_0 == 0)
    {
      free (hl);
      free (pidl);
      goto START_WAIT;
    }

  h = hl[res - WAIT_OBJECT_0];
  GetExitCodeProcess (h, &exitcode);
  pid = pidl [res - WAIT_OBJECT_0];

  found = __gnat_win32_remove_handle (h, -1);

  free (hl);
  free (pidl);

  /* if not found another process waiting has already handled this process */

  if (!found)
    {
      goto START_WAIT;
    }

  *status = (int) exitcode;
  return (int) pid;
}

#endif

int
__gnat_portable_no_block_spawn (char *args[] ATTRIBUTE_UNUSED)
{

#if defined (__vxworks) || defined (__PikeOS__)
  /* Not supported.  */
  return -1;

#elif defined(__DJGPP__)
  if (spawnvp (P_WAIT, args[0], args) != 0)
    return -1;
  else
    return 0;

#elif defined (_WIN32)

  HANDLE h = NULL;
  int pid;

  win32_no_block_spawn (args[0], args, &h, &pid);
  if (h != NULL)
    {
      add_handle (h, pid);
      return pid;
    }
  else
    return -1;

#else

  int pid = fork ();

  if (pid == 0)
    {
      /* The child.  */
      if (execv (args[0], MAYBE_TO_PTR32 (args)) != 0)
	_exit (1);
    }

  return pid;

  #endif
}

int
__gnat_portable_wait (int *process_status)
{
  int status = 0;
  int pid = 0;

#if defined (__vxworks) || defined (__PikeOS__)
  /* Not sure what to do here, so do nothing but return zero.  */

#elif defined (_WIN32)

  pid = win32_wait (&status);

#elif defined (__DJGPP__)
  /* Child process has already ended in case of DJGPP.
     No need to do anything. Just return success. */
#else

  pid = waitpid (-1, &status, 0);
  status = status & 0xffff;
#endif

  *process_status = status;
  return pid;
}

void
__gnat_os_exit (int status)
{
  exit (status);
}

int
__gnat_current_process_id (void)
{
#if defined (__vxworks) || defined (__PikeOS__)
  return -1;

#elif defined (_WIN32)

  return (int)GetCurrentProcessId();

#else

  return (int)getpid();
#endif
}

/* Locate file on path, that matches a predicate */

char *
__gnat_locate_file_with_predicate (char *file_name, char *path_val,
				   int (*predicate)(char *))
{
  char *ptr;
  char *file_path = (char *) alloca (strlen (file_name) + 1);
  int absolute;

  /* Return immediately if file_name is empty */

  if (*file_name == '\0')
    return 0;

  /* Remove quotes around file_name if present */

  ptr = file_name;
  if (*ptr == '"')
    ptr++;

  strcpy (file_path, ptr);

  ptr = file_path + strlen (file_path) - 1;

  if (*ptr == '"')
    *ptr = '\0';

  /* Handle absolute pathnames.  */

  absolute = __gnat_is_absolute_path (file_path, strlen (file_name));

  if (absolute)
    {
     if (predicate (file_path))
       return xstrdup (file_path);

      return 0;
    }

  /* If file_name include directory separator(s), try it first as
     a path name relative to the current directory */
  for (ptr = file_name; *ptr && *ptr != '/' && *ptr != DIR_SEPARATOR; ptr++)
    ;

  if (*ptr != 0)
    {
      if (predicate (file_name))
        return xstrdup (file_name);
    }

  if (path_val == 0)
    return 0;

  {
    /* The result has to be smaller than path_val + file_name.  */
    char *file_path =
      (char *) alloca (strlen (path_val) + strlen (file_name) + 2);

    for (;;)
      {
      /* Skip the starting quote */

      if (*path_val == '"')
	path_val++;

      for (ptr = file_path; *path_val && *path_val != PATH_SEPARATOR; )
	*ptr++ = *path_val++;

      /* If directory is empty, it is the current directory*/

      if (ptr == file_path)
        {
         *ptr = '.';
        }
      else
        ptr--;

      /* Skip the ending quote */

      if (*ptr == '"')
	ptr--;

      if (*ptr != '/' && *ptr != DIR_SEPARATOR)
        *++ptr = DIR_SEPARATOR;

      strcpy (++ptr, file_name);

      if (predicate (file_path))
        return xstrdup (file_path);

      if (*path_val == 0)
        return 0;

      /* Skip path separator */

      path_val++;
      }
  }

  return 0;
}

/* Locate an executable file, give a Path value.  */

char *
__gnat_locate_executable_file (char *file_name, char *path_val)
{
   return __gnat_locate_file_with_predicate
      (file_name, path_val, &__gnat_is_executable_file);
}

/* Locate a regular file, give a Path value.  */

char *
__gnat_locate_regular_file (char *file_name, char *path_val)
{
   return __gnat_locate_file_with_predicate
      (file_name, path_val, &__gnat_is_regular_file);
}

/* Locate an executable given a Path argument. This routine is only used by
   gnatbl and should not be used otherwise.  Use locate_exec_on_path
   instead.  */

char *
__gnat_locate_exec (char *exec_name, char *path_val)
{
  char *ptr;
  if (!strstr (exec_name, HOST_EXECUTABLE_SUFFIX))
    {
      char *full_exec_name =
        (char *) alloca
	  (strlen (exec_name) + strlen (HOST_EXECUTABLE_SUFFIX) + 1);

      strcpy (full_exec_name, exec_name);
      strcat (full_exec_name, HOST_EXECUTABLE_SUFFIX);
      ptr = __gnat_locate_executable_file (full_exec_name, path_val);

      if (ptr == 0)
         return __gnat_locate_executable_file (exec_name, path_val);
      return ptr;
    }
  else
    return __gnat_locate_executable_file (exec_name, path_val);
}

/* Locate an executable using the Systems default PATH.  */

char *
__gnat_locate_exec_on_path (char *exec_name)
{
  char *apath_val;

#if defined (_WIN32)
  TCHAR *wpath_val = _tgetenv (_T("PATH"));
  TCHAR *wapath_val;
  /* In Win32 systems we expand the PATH as for XP environment
     variables are not automatically expanded. We also prepend the
     ".;" to the path to match normal NT path search semantics */

  #define EXPAND_BUFFER_SIZE 32767

  wapath_val = (TCHAR *) alloca (EXPAND_BUFFER_SIZE);

  wapath_val [0] = '.';
  wapath_val [1] = ';';

  DWORD res = ExpandEnvironmentStrings
    (wpath_val, &wapath_val[2], EXPAND_BUFFER_SIZE - 2);

  if (!res) wapath_val [0] = _T('\0');

  apath_val = (char *) alloca (EXPAND_BUFFER_SIZE);

  WS2SC (apath_val, wapath_val, EXPAND_BUFFER_SIZE);

#else
  const char *path_val = getenv ("PATH");

  /* If PATH is not defined, proceed with __gnat_locate_exec anyway, so we can
     find files that contain directory names.  */

  if (path_val == NULL) path_val = "";
  apath_val = (char *) alloca (strlen (path_val) + 1);
  strcpy (apath_val, path_val);
#endif

  return __gnat_locate_exec (exec_name, apath_val);
}

/* Dummy functions for Osint import for non-VMS systems.
   ??? To be removed.  */

int
__gnat_to_canonical_file_list_init (char *dirspec ATTRIBUTE_UNUSED,
				    int onlydirs ATTRIBUTE_UNUSED)
{
  return 0;
}

char *
__gnat_to_canonical_file_list_next (void)
{
  static char empty[] = "";
  return empty;
}

void
__gnat_to_canonical_file_list_free (void)
{
}

char *
__gnat_to_canonical_dir_spec (char *dirspec, int prefixflag ATTRIBUTE_UNUSED)
{
  return dirspec;
}

char *
__gnat_to_canonical_file_spec (char *filespec)
{
  return filespec;
}

char *
__gnat_to_canonical_path_spec (char *pathspec)
{
  return pathspec;
}

char *
__gnat_to_host_dir_spec (char *dirspec, int prefixflag ATTRIBUTE_UNUSED)
{
  return dirspec;
}

char *
__gnat_to_host_file_spec (char *filespec)
{
  return filespec;
}

void
__gnat_adjust_os_resource_limits (void)
{
}

#if defined (__mips_vxworks)
int
_flush_cache (void)
{
   CACHE_USER_FLUSH (0, ENTIRE_CACHE);
}
#endif

#if defined (_WIN32)
int __gnat_argument_needs_quote = 1;
#else
int __gnat_argument_needs_quote = 0;
#endif

/* This option is used to enable/disable object files handling from the
   binder file by the GNAT Project module. For example, this is disabled on
   Windows (prior to GCC 3.4) as it is already done by the mdll module.
   Stating with GCC 3.4 the shared libraries are not based on mdll
   anymore as it uses the GCC's -shared option  */
#if defined (_WIN32) \
    && ((__GNUC__ < 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ < 4)))
int __gnat_prj_add_obj_files = 0;
#else
int __gnat_prj_add_obj_files = 1;
#endif

/* char used as prefix/suffix for environment variables */
#if defined (_WIN32)
char __gnat_environment_char = '%';
#else
char __gnat_environment_char = '$';
#endif

/* This functions copy the file attributes from a source file to a
   destination file.

   mode = 0  : In this mode copy only the file time stamps (last access and
               last modification time stamps).

   mode = 1  : In this mode, time stamps and read/write/execute attributes are
               copied.

   mode = 2  : In this mode, only read/write/execute attributes are copied

   Returns 0 if operation was successful and -1 in case of error. */

int
__gnat_copy_attribs (char *from ATTRIBUTE_UNUSED, char *to ATTRIBUTE_UNUSED,
                     int mode ATTRIBUTE_UNUSED)
{
#if (defined (__vxworks) && _WRS_VXWORKS_MAJOR < 6)
  return -1;

#elif defined (_WIN32)
  TCHAR wfrom [GNAT_MAX_PATH_LEN + 2];
  TCHAR wto [GNAT_MAX_PATH_LEN + 2];
  BOOL res;
  FILETIME fct, flat, flwt;
  HANDLE hfrom, hto;

  S2WSC (wfrom, from, GNAT_MAX_PATH_LEN + 2);
  S2WSC (wto, to, GNAT_MAX_PATH_LEN + 2);

  /*  Do we need to copy the timestamp ? */

  if (mode != 2) {
     /* retrieve from times */

     hfrom = CreateFile
       (wfrom, GENERIC_READ, 0, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, NULL);

     if (hfrom == INVALID_HANDLE_VALUE)
       return -1;

     res = GetFileTime (hfrom, &fct, &flat, &flwt);

     CloseHandle (hfrom);

     if (res == 0)
       return -1;

     /* retrieve from times */

     hto = CreateFile
       (wto, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, NULL);

     if (hto == INVALID_HANDLE_VALUE)
       return -1;

     res = SetFileTime (hto, NULL, &flat, &flwt);

     CloseHandle (hto);

     if (res == 0)
       return -1;
  }

  /* Do we need to copy the permissions ? */
  /* Set file attributes in full mode. */

  if (mode != 0)
    {
      DWORD attribs = GetFileAttributes (wfrom);

      if (attribs == INVALID_FILE_ATTRIBUTES)
	return -1;

      res = SetFileAttributes (wto, attribs);
      if (res == 0)
	return -1;
    }

  return 0;

#else
  GNAT_STRUCT_STAT fbuf;
  struct utimbuf tbuf;

  if (GNAT_STAT (from, &fbuf) == -1) {
     return -1;
  }

  /* Do we need to copy timestamp ? */
  if (mode != 2) {
     tbuf.actime = fbuf.st_atime;
     tbuf.modtime = fbuf.st_mtime;

     if (utime (to, &tbuf) == -1) {
        return -1;
     }
  }

  /* Do we need to copy file permissions ? */
  if (mode != 0 && (chmod (to, fbuf.st_mode) == -1)) {
	  return -1;
  }

  return 0;
#endif
}

int
__gnat_lseek (int fd, long offset, int whence)
{
  return (int) lseek (fd, offset, whence);
}

/* This function returns the major version number of GCC being used.  */
int
get_gcc_version (void)
{
#ifdef IN_RTS
  return __GNUC__;
#else
  return (int) (version_string[0] - '0');
#endif
}

/*
 * Set Close_On_Exec as indicated.
 * Note: this is used for both GNAT.OS_Lib and GNAT.Sockets.
 */

int
__gnat_set_close_on_exec (int fd ATTRIBUTE_UNUSED,
                          int close_on_exec_p ATTRIBUTE_UNUSED)
{
#if defined (F_GETFD) && defined (FD_CLOEXEC) && ! defined (__vxworks)
  int flags = fcntl (fd, F_GETFD, 0);
  if (flags < 0)
    return flags;
  if (close_on_exec_p)
    flags |= FD_CLOEXEC;
  else
    flags &= ~FD_CLOEXEC;
  return fcntl (fd, F_SETFD, flags);
#elif defined(_WIN32)
  HANDLE h = (HANDLE) _get_osfhandle (fd);
  if (h == (HANDLE) -1)
    return -1;
  if (close_on_exec_p)
    return ! SetHandleInformation (h, HANDLE_FLAG_INHERIT, 0);
  return ! SetHandleInformation (h, HANDLE_FLAG_INHERIT,
    HANDLE_FLAG_INHERIT);
#else
  /* TODO: Unimplemented. */
  return -1;
#endif
}

/* Indicates if platforms supports automatic initialization through the
   constructor mechanism */
int
__gnat_binder_supports_auto_init (void)
{
  return 1;
}

/* Indicates that Stand-Alone Libraries are automatically initialized through
   the constructor mechanism */
int
__gnat_sals_init_using_constructors (void)
{
#if defined (__vxworks) || defined (__Lynx__)
   return 0;
#else
   return 1;
#endif
}

#if defined (__linux__) || defined (__ANDROID__)
/* There is no function in the glibc to retrieve the LWP of the current
   thread. We need to do a system call in order to retrieve this
   information. */
#include <sys/syscall.h>
void *
__gnat_lwp_self (void)
{
   return (void *) syscall (__NR_gettid);
}
#endif

#if defined (__APPLE__)
#include <mach/thread_info.h>
#include <mach/mach_init.h>
#include <mach/thread_act.h>

/* System-wide thread identifier.  Note it could be truncated on 32 bit
   hosts.
   Previously was: pthread_mach_thread_np (pthread_self ()).  */
void *
__gnat_lwp_self (void)
{
  thread_identifier_info_data_t data;
  mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
  kern_return_t kret;

  kret = thread_info (mach_thread_self (), THREAD_IDENTIFIER_INFO,
		      (thread_info_t) &data, &count);
  if (kret == KERN_SUCCESS)
    return (void *)(uintptr_t)data.thread_id;
  else
    return 0;
}
#endif

#if defined (__linux__)
#include <sched.h>

/* glibc versions earlier than 2.7 do not define the routines to handle
   dynamically allocated CPU sets. For these targets, we use the static
   versions. */

#ifdef CPU_ALLOC

/* Dynamic cpu sets */

cpu_set_t *
__gnat_cpu_alloc (size_t count)
{
  return CPU_ALLOC (count);
}

size_t
__gnat_cpu_alloc_size (size_t count)
{
  return CPU_ALLOC_SIZE (count);
}

void
__gnat_cpu_free (cpu_set_t *set)
{
  CPU_FREE (set);
}

void
__gnat_cpu_zero (size_t count, cpu_set_t *set)
{
  CPU_ZERO_S (count, set);
}

void
__gnat_cpu_set (int cpu, size_t count, cpu_set_t *set)
{
  /* Ada handles CPU numbers starting from 1, while C identifies the first
     CPU by a 0, so we need to adjust. */
  CPU_SET_S (cpu - 1, count, set);
}

#else /* !CPU_ALLOC */

/* Static cpu sets */

cpu_set_t *
__gnat_cpu_alloc (size_t count ATTRIBUTE_UNUSED)
{
  return (cpu_set_t *) xmalloc (sizeof (cpu_set_t));
}

size_t
__gnat_cpu_alloc_size (size_t count ATTRIBUTE_UNUSED)
{
  return sizeof (cpu_set_t);
}

void
__gnat_cpu_free (cpu_set_t *set)
{
  free (set);
}

void
__gnat_cpu_zero (size_t count ATTRIBUTE_UNUSED, cpu_set_t *set)
{
  CPU_ZERO (set);
}

void
__gnat_cpu_set (int cpu, size_t count ATTRIBUTE_UNUSED, cpu_set_t *set)
{
  /* Ada handles CPU numbers starting from 1, while C identifies the first
     CPU by a 0, so we need to adjust. */
  CPU_SET (cpu - 1, set);
}
#endif /* !CPU_ALLOC */
#endif /* __linux__ */

/* Return the load address of the executable, or 0 if not known.  In the
   specific case of error, (void *)-1 can be returned. Beware: this unit may
   be in a shared library.  As low-level units are needed, we allow #include
   here.  */

#if defined (__APPLE__)
#include <mach-o/dyld.h>
#endif

const void *
__gnat_get_executable_load_address (void)
{
#if defined (__APPLE__)
  return _dyld_get_image_header (0);

#elif 0 && defined (__linux__)
  /* Currently disabled as it needs at least -ldl.  */
  struct link_map *map = _r_debug.r_map;

  return (const void *)map->l_addr;

#else
  return NULL;
#endif
}

void
__gnat_kill (int pid, int sig, int close ATTRIBUTE_UNUSED)
{
#if defined(_WIN32)
  HANDLE h = OpenProcess (PROCESS_ALL_ACCESS, FALSE, pid);
  if (h == NULL)
    return;
  if (sig == 9)
    {
      TerminateProcess (h, 1);
    }
  else if (sig == SIGINT)
    GenerateConsoleCtrlEvent (CTRL_C_EVENT, pid);
  else if (sig == SIGBREAK)
    GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid);
  /* ??? The last two alternatives don't really work. SIGBREAK requires setting
     up process groups at start time which we don't do; treating SIGINT is just
     not possible apparently. So we really only support signal 9. Fortunately
     that's all we use in GNAT.Expect */

  CloseHandle (h);
#elif defined (__vxworks)
  /* Not implemented */
#else
  kill (pid, sig);
#endif
}

void __gnat_killprocesstree (int pid, int sig_num)
{
#if defined(_WIN32)
  PROCESSENTRY32 pe;

  memset(&pe, 0, sizeof(PROCESSENTRY32));
  pe.dwSize = sizeof(PROCESSENTRY32);

  HANDLE hSnap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);

  /*  cannot take snapshot, just kill the parent process */

  if (hSnap == INVALID_HANDLE_VALUE)
    {
      __gnat_kill (pid, sig_num, 1);
      return;
    }

  if (Process32First(hSnap, &pe))
    {
      BOOL bContinue = TRUE;

      /* kill child processes first */

      while (bContinue)
        {
          if (pe.th32ParentProcessID == (DWORD)pid)
            __gnat_killprocesstree (pe.th32ProcessID, sig_num);

          bContinue = Process32Next (hSnap, &pe);
        }
    }

  CloseHandle (hSnap);

  /* kill process */

  __gnat_kill (pid, sig_num, 1);

#elif defined (__vxworks)
  /* not implemented */

#elif defined (__linux__)
  DIR *dir;
  struct dirent *d;

  /*  read all processes' pid and ppid */

  dir = opendir ("/proc");

  /*  cannot open proc, just kill the parent process */

  if (!dir)
    {
      __gnat_kill (pid, sig_num, 1);
      return;
    }

  /* kill child processes first */

  while ((d = readdir (dir)) != NULL)
    {
      if ((d->d_type & DT_DIR) == DT_DIR)
        {
          char statfile[64] = { 0 };
          int _pid, _ppid;

          /* read /proc/<PID>/stat */

          strncpy (statfile, "/proc/", sizeof(statfile));
          strncat (statfile, d->d_name, sizeof(statfile));
          strncat (statfile, "/stat", sizeof(statfile));

          FILE *fd = fopen (statfile, "r");

          if (fd)
            {
              const int match = fscanf (fd, "%d %*s %*s %d", &_pid, &_ppid);
              fclose (fd);

              if (match == 2 && _ppid == pid)
                __gnat_killprocesstree (_pid, sig_num);
            }
        }
    }

  closedir (dir);

  /* kill process */

  __gnat_kill (pid, sig_num, 1);
#else
  __gnat_kill (pid, sig_num, 1);
#endif
  /* Note on Solaris it is possible to read /proc/<PID>/status.
     The 5th and 6th words are the pid and the 7th and 8th the ppid.
     See: /usr/include/sys/procfs.h (struct pstatus).
  */
}

#ifdef __cplusplus
}
#endif