1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
|
/**********************************************************************
io.c -
$Author$
$Date$
created at: Fri Oct 15 18:08:59 JST 1993
Copyright (C) 1993-2002 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#if defined(__VMS)
#define _XOPEN_SOURCE
#define _POSIX_C_SOURCE 2
#endif
#include "ruby.h"
#include "rubyio.h"
#include "rubysig.h"
#include "env.h"
#include <ctype.h>
#include <errno.h>
#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(NT) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
# define NO_SAFE_RENAME
#endif
#if defined(MSDOS) || defined(__CYGWIN__) || defined(NT)
# define NO_LONG_FNAME
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(sun) || defined(_nec_ews)
# define USE_SETVBUF
#endif
#ifdef __QNXNTO__
#include "unix.h"
#endif
#include <sys/types.h>
#if !defined(DJGPP) && !defined(NT) && !defined(__human68k__)
#include <sys/ioctl.h>
#endif
#if defined(HAVE_FCNTL_H) || defined(NT)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
#include <sys/fcntl.h>
#endif
#if !HAVE_OFF_T && !defined(off_t)
# define off_t long
#endif
#if !HAVE_FSEEKO && !defined(fseeko)
# define fseeko fseek
#endif
#if !HAVE_FTELLO && !defined(ftello)
# define ftello ftell
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
#ifndef NT
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
#endif
#endif
#include <sys/stat.h>
/* EMX has sys/param.h, but.. */
#if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
# include <sys/param.h>
#endif
#if !defined NOFILE
# define NOFILE 64
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
extern void Init_File _((void));
#ifdef __BEOS__
# ifndef NOFILE
# define NOFILE (OPEN_MAX)
# endif
#include <net/socket.h>
#endif
#include "util.h"
#if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
# error off_t is bigger than long, but you have no long long...
#endif
VALUE rb_cIO;
VALUE rb_eEOFError;
VALUE rb_eIOError;
VALUE rb_stdin, rb_stdout, rb_stderr, rb_defout;
static VALUE orig_stdin, orig_stdout, orig_stderr;
static int saved_fd[3] = {0, 1, 2};
VALUE rb_output_fs;
VALUE rb_rs;
VALUE rb_output_rs;
VALUE rb_default_rs;
static VALUE argf;
static ID id_write;
extern char *ruby_inplace_mode;
struct timeval rb_time_interval _((VALUE));
static VALUE filename, current_file;
static int gets_lineno;
static int init_p = 0, next_p = 0, first_p = 1;
static VALUE lineno;
#ifdef _STDIO_USES_IOSTREAM /* GNU libc */
# ifdef _IO_fpos_t
# define READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_IO_read_end - (fp)->_IO_read_ptr)
# define READ_DATA_PENDING_PTR(fp) ((fp)->_IO_read_ptr)
# else
# define READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_egptr - (fp)->_gptr)
# define READ_DATA_PENDING_PTR(fp) ((fp)->_gptr)
# endif
#elif defined(FILE_COUNT)
# define READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_COUNT)
#elif defined(FILE_READEND)
# define READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_READEND - (fp)->FILE_READPTR)
#elif defined(__BEOS__)
# define READ_DATA_PENDING(fp) (fp->_state._eof == 0)
#elif defined(__VMS)
# define READ_DATA_PENDING(fp) (((unsigned int)((*(fp))->_flag) & _IOEOF) == 0)
#else
/* requires systems own version of the ReadDataPending() */
extern int ReadDataPending();
# define READ_DATA_PENDING(fp) (!feof(fp))
#endif
#ifndef READ_DATA_PENDING_PTR
# ifdef FILE_READPTR
# define READ_DATA_PENDING_PTR(fp) ((fp)->FILE_READPTR)
# endif
#endif
#define READ_CHECK(fp) do {\
if (!READ_DATA_PENDING(fp)) {\
rb_thread_wait_fd(fileno(fp));\
rb_io_check_closed(fptr);\
}\
} while(0)
void
rb_eof_error()
{
rb_raise(rb_eEOFError, "End of file reached");
}
VALUE
rb_io_taint_check(io)
VALUE io;
{
if (!OBJ_TAINTED(io) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
return io;
}
void
rb_io_check_closed(fptr)
OpenFile *fptr;
{
if (!fptr) {
rb_raise(rb_eIOError, "uninitialized stream");
}
if (!fptr->f && !fptr->f2) {
rb_raise(rb_eIOError, "closed stream");
}
}
void
rb_io_check_readable(fptr)
OpenFile *fptr;
{
if (!(fptr->mode & FMODE_READABLE)) {
rb_raise(rb_eIOError, "not opened for reading");
}
}
void
rb_io_check_writable(fptr)
OpenFile *fptr;
{
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
}
}
int
rb_read_pending(fp)
FILE *fp;
{
return READ_DATA_PENDING(fp);
}
void
rb_read_check(fp)
FILE *fp;
{
if (!READ_DATA_PENDING(fp)) {
rb_thread_wait_fd(fileno(fp));
}
}
static int
ruby_dup(orig)
int orig;
{
int fd;
fd = dup(orig);
if (fd < 0) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
fd = dup(orig);
}
if (fd < 0) {
rb_sys_fail(0);
}
}
return fd;
}
static void
io_fflush(f, fptr)
FILE *f;
OpenFile *fptr;
{
int n;
rb_thread_fd_writable(fileno(f));
TRAP_BEG;
n = fflush(f);
TRAP_END;
if (n == EOF) rb_sys_fail(fptr->path);
fptr->mode &= ~FMODE_WBUF;
}
/* writing functions */
static VALUE
io_write(io, str)
VALUE io, str;
{
OpenFile *fptr;
FILE *f;
long n;
rb_secure(4);
if (TYPE(str) != T_STRING)
str = rb_obj_as_string(str);
if (RSTRING(str)->len == 0) return INT2FIX(0);
if (TYPE(io) != T_FILE) {
/* port is not IO, call write method for it. */
return rb_funcall(io, id_write, 1, str);
}
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
#ifdef __human68k__
{
register char *ptr = RSTRING(str)->ptr;
n = RSTRING(str)->len;
while (--n >= 0)
if (fputc(*ptr++, f) == EOF)
break;
n = ptr - RSTRING(str)->ptr;
}
if (n != RSTRING(str)->len && ferror(f))
rb_sys_fail(fptr->path);
#else
n = fwrite(RSTRING(str)->ptr, 1, RSTRING(str)->len, f);
if (n != RSTRING(str)->len && ferror(f)) {
rb_sys_fail(fptr->path);
}
#endif
if (fptr->mode & FMODE_SYNC) {
io_fflush(f, fptr);
}
else {
fptr->mode |= FMODE_WBUF;
}
return LONG2FIX(n);
}
VALUE
rb_io_write(io, str)
VALUE io, str;
{
return rb_funcall(io, id_write, 1, str);
}
VALUE
rb_io_addstr(io, str)
VALUE io, str;
{
rb_io_write(io, str);
return io;
}
static VALUE
rb_io_flush(io)
VALUE io;
{
OpenFile *fptr;
FILE *f;
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
io_fflush(f, fptr);
return io;
}
static VALUE
rb_io_tell(io)
VALUE io;
{
OpenFile *fptr;
off_t pos;
GetOpenFile(io, fptr);
pos = ftello(fptr->f);
if (ferror(fptr->f)) rb_sys_fail(fptr->path);
return OFFT2NUM(pos);
}
#ifndef SEEK_CUR
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
static VALUE
rb_io_seek(io, offset, whence)
VALUE io, offset;
int whence;
{
OpenFile *fptr;
off_t pos;
GetOpenFile(io, fptr);
pos = fseeko(fptr->f, NUM2OFFT(offset), whence);
if (pos != 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
return INT2FIX(0);
}
static VALUE
rb_io_seek_m(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE offset, ptrname;
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
whence = NUM2INT(ptrname);
}
return rb_io_seek(io, offset, whence);
}
static VALUE
rb_io_set_pos(io, offset)
VALUE io, offset;
{
OpenFile *fptr;
off_t pos;
GetOpenFile(io, fptr);
pos = fseeko(fptr->f, NUM2OFFT(offset), SEEK_SET);
if (pos != 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
return OFFT2NUM(pos);
}
static VALUE
rb_io_rewind(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (fseeko(fptr->f, 0L, 0) != 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
if (io == current_file) {
gets_lineno -= fptr->lineno;
}
fptr->lineno = 0;
return INT2FIX(0);
}
VALUE
rb_io_eof(io)
VALUE io;
{
OpenFile *fptr;
int ch;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (feof(fptr->f)) return Qtrue;
if (READ_DATA_PENDING(fptr->f)) return Qfalse;
READ_CHECK(fptr->f);
TRAP_BEG;
ch = getc(fptr->f);
TRAP_END;
if (ch != EOF) {
ungetc(ch, fptr->f);
return Qfalse;
}
return Qtrue;
}
static VALUE
rb_io_sync(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
}
static VALUE
rb_io_set_sync(io, mode)
VALUE io, mode;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (RTEST(mode)) {
fptr->mode |= FMODE_SYNC;
}
else {
fptr->mode &= ~FMODE_SYNC;
}
return mode;
}
static VALUE
rb_io_fsync(io)
VALUE io;
{
#ifdef HAVE_FSYNC
OpenFile *fptr;
FILE *f;
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
io_fflush(f, fptr);
if (fsync(fileno(f)) < 0)
rb_sys_fail(fptr->path);
return INT2FIX(0);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
static VALUE
rb_io_fileno(io)
VALUE io;
{
OpenFile *fptr;
int fd;
GetOpenFile(io, fptr);
fd = fileno(fptr->f);
return INT2FIX(fd);
}
static VALUE
rb_io_pid(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (!fptr->pid)
return Qnil;
return INT2FIX(fptr->pid);
}
static VALUE
rb_io_inspect(obj)
VALUE obj;
{
OpenFile *fptr;
char *buf, *cname;
GetOpenFile(obj, fptr);
if (!fptr->path) return rb_any_to_s(obj);
cname = rb_class2name(CLASS_OF(obj));
buf = ALLOCA_N(char, strlen(cname) + strlen(fptr->path) + 5);
sprintf(buf, "#<%s:%s>", cname, fptr->path);
return rb_str_new2(buf);
}
static VALUE
rb_io_to_io(io)
VALUE io;
{
return io;
}
/* reading functions */
long
rb_io_fread(ptr, len, f)
char *ptr;
long len;
FILE *f;
{
long n = len;
int c;
while (n > 0) {
#ifdef READ_DATA_PENDING_COUNT
int i = READ_DATA_PENDING_COUNT(f);
if (i <= 0) {
rb_thread_wait_fd(fileno(f));
i = READ_DATA_PENDING_COUNT(f);
}
if (i > 0) {
if (i > n) i = n;
TRAP_BEG;
c = fread(ptr, 1, i, f);
TRAP_END;
if (c < 0) goto eof;
ptr += c;
n -= c;
if (c < i) goto eof;
continue;
}
#else
if (!READ_DATA_PENDING(f)) {
rb_thread_wait_fd(fileno(f));
}
#endif
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c == EOF) {
eof:
if (ferror(f)) {
switch (errno) {
case EINTR:
continue;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
return len - n;
}
return 0;
}
*ptr = '\0';
break;
}
*ptr++ = c;
n--;
}
return len - n;
}
#ifndef S_ISREG
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
#endif
#define SMALLBUF 100
static long
remain_size(fptr)
OpenFile *fptr;
{
struct stat st;
off_t siz = BUFSIZ;
off_t pos;
if (feof(fptr->f)) return 0;
if (fstat(fileno(fptr->f), &st) == 0 && S_ISREG(st.st_mode)
#ifdef __BEOS__
&& (st.st_dev > 3)
#endif
)
{
pos = ftello(fptr->f);
if (st.st_size > pos && pos >= 0) {
siz = st.st_size - pos + 1;
if (siz > LONG_MAX) {
rb_raise(rb_eIOError, "file too big for single read");
}
}
}
return (long)siz;
}
static VALUE
read_all(fptr, siz)
OpenFile *fptr;
long siz;
{
VALUE str;
long bytes = 0;
long n;
long pos = 0;
if (feof(fptr->f)) return Qnil;
READ_CHECK(fptr->f);
if (!siz) siz = BUFSIZ;
str = rb_tainted_str_new(0, siz);
pos = ftell(fptr->f);
for (;;) {
n = rb_io_fread(RSTRING(str)->ptr+bytes, siz-bytes, fptr->f);
if (pos > 0 && n == 0 && bytes == 0) {
if (feof(fptr->f)) return Qnil;
rb_sys_fail(fptr->path);
}
bytes += n;
if (bytes < siz) break;
siz += BUFSIZ;
rb_str_resize(str, siz);
}
if (bytes == 0) return rb_str_new(0,0);
if (bytes != siz) rb_str_resize(str, bytes);
return str;
}
static VALUE
io_read(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
OpenFile *fptr;
long n, len;
VALUE length, str;
rb_scan_args(argc, argv, "01", &length);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (NIL_P(length)) {
return read_all(fptr, remain_size(fptr));
}
len = NUM2LONG(length);
if (len < 0) {
rb_raise(rb_eArgError, "negative length %d given", len);
}
if (feof(fptr->f)) return Qnil;
str = rb_str_new(0, len);
if (len == 0) return str;
READ_CHECK(fptr->f);
n = rb_io_fread(RSTRING(str)->ptr, len, fptr->f);
if (n == 0) {
if (feof(fptr->f)) return Qnil;
rb_sys_fail(fptr->path);
}
RSTRING(str)->len = n;
RSTRING(str)->ptr[n] = '\0';
OBJ_TAINT(str);
return str;
}
static int
appendline(fptr, delim, strp)
OpenFile *fptr;
int delim;
VALUE *strp;
{
FILE *f = fptr->f;
VALUE str = *strp;
int c = EOF;
#ifndef READ_DATA_PENDING_PTR
char buf[8192];
char *bp = buf, *bpe = buf + sizeof buf - 3;
int cnt;
#endif
do {
#ifdef READ_DATA_PENDING_PTR
int pending = READ_DATA_PENDING_COUNT(f);
if (pending > 0) {
const char *p = READ_DATA_PENDING_PTR(f);
const char *e = memchr(p, delim, pending);
long last = 0;
if (e) pending = e - p + 1;
if (!NIL_P(str)) {
last = RSTRING(str)->len;
rb_str_resize(str, last + (c != EOF) + pending);
}
else {
*strp = str = rb_str_new(0, (c != EOF) + pending);
}
if (c != EOF) {
RSTRING(str)->ptr[last++] = c;
}
fread(RSTRING(str)->ptr + last, 1, pending, f); /* must not fail */
if (e) return delim;
}
rb_thread_wait_fd(fileno(f));
rb_io_check_closed(fptr);
#else
READ_CHECK(f);
#endif
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c == EOF) {
if (ferror(f)) {
if (errno == EINTR) continue;
rb_sys_fail(fptr->path);
}
return c;
}
#ifndef READ_DATA_PENDING_PTR
if ((*bp++ = c) == delim || bp == bpe) {
cnt = bp - buf;
if (cnt > 0) {
if (!NIL_P(str))
rb_str_cat(str, buf, cnt);
else
*strp = str = rb_str_new(buf, cnt);
}
bp = buf;
}
#endif
} while (c != delim);
#ifdef READ_DATA_PENDING_PTR
{
char ch = c;
if (!NIL_P(str)) {
rb_str_cat(str, &ch, 1);
}
else {
*strp = str = rb_str_new(&ch, 1);
}
}
#endif
return c;
}
static inline int
swallow(fptr, term)
OpenFile *fptr;
int term;
{
FILE *f = fptr->f;
int c;
do {
#ifdef READ_DATA_PENDING_PTR
int cnt;
while ((cnt = READ_DATA_PENDING_COUNT(f)) > 0) {
char buf[1024];
const char *p = READ_DATA_PENDING_PTR(f);
int i;
if (cnt > sizeof buf) cnt = sizeof buf;
if (*p != term) return Qtrue;
i = cnt;
while (--i && *++p == term);
if (!fread(buf, 1, cnt - i, f)) /* must not fail */
rb_sys_fail(fptr->path);
}
rb_thread_wait_fd(fileno(f));
rb_io_check_closed(fptr);
#else
READ_CHECK(f);
#endif
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c != term) {
ungetc(c, f);
return Qtrue;
}
} while (c != EOF);
return Qfalse;
}
static VALUE
rb_io_getline_fast(fptr, delim)
OpenFile *fptr;
int delim;
{
VALUE str = Qnil;
int c;
while ((c = appendline(fptr, delim, &str)) != EOF && c != delim);
if (!NIL_P(str)) {
fptr->lineno++;
lineno = INT2FIX(fptr->lineno);
OBJ_TAINT(str);
}
return str;
}
static VALUE
rb_io_getline(rs, fptr)
VALUE rs;
OpenFile *fptr;
{
VALUE str = Qnil;
if (NIL_P(rs)) {
str = read_all(fptr, 0);
}
else if (rs == rb_default_rs) {
return rb_io_getline_fast(fptr, '\n');
}
else {
int c, newline;
char *rsptr;
long rslen;
int rspara = 0;
StringValue(rs);
rslen = RSTRING(rs)->len;
if (rslen == 0) {
rsptr = "\n\n";
rslen = 2;
rspara = 1;
swallow(fptr, '\n');
}
else if (rslen == 1) {
return rb_io_getline_fast(fptr, RSTRING(rs)->ptr[0]);
}
else {
rsptr = RSTRING(rs)->ptr;
}
newline = rsptr[rslen - 1];
while ((c = appendline(fptr, newline, &str)) != EOF &&
(c != newline || RSTRING(str)->len < rslen ||
memcmp(RSTRING(str)->ptr+RSTRING(str)->len-rslen,rsptr,rslen)));
if (rspara) {
if (c != EOF) {
swallow(fptr, '\n');
}
}
}
if (!NIL_P(str)) {
fptr->lineno++;
lineno = INT2FIX(fptr->lineno);
OBJ_TAINT(str);
}
return str;
}
VALUE
rb_io_gets(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
return rb_io_getline_fast(fptr, '\n');
}
static VALUE
rb_io_gets_m(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE rs, str;
OpenFile *fptr;
if (argc == 0) {
rs = rb_rs;
}
else {
rb_scan_args(argc, argv, "1", &rs);
}
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
str = rb_io_getline(rs, fptr);
if (!NIL_P(str)) {
rb_lastline_set(str);
}
return str;
}
static VALUE
rb_io_lineno(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
return INT2NUM(fptr->lineno);
}
static VALUE
rb_io_set_lineno(io, lineno)
VALUE io, lineno;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
fptr->lineno = NUM2INT(lineno);
return lineno;
}
static void
lineno_setter(val, id, var)
VALUE val;
ID id;
VALUE *var;
{
gets_lineno = NUM2INT(val);
*var = INT2FIX(gets_lineno);
}
static VALUE
argf_set_lineno(argf, val)
VALUE argf, val;
{
gets_lineno = NUM2INT(val);
lineno = INT2FIX(gets_lineno);
return Qnil;
}
static VALUE
argf_lineno()
{
return lineno;
}
static VALUE
rb_io_readline(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE line = rb_io_gets_m(argc, argv, io);
if (NIL_P(line)) {
rb_eof_error();
}
return line;
}
static VALUE
rb_io_readlines(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE line, ary;
VALUE rs;
OpenFile *fptr;
if (argc == 0) {
rs = rb_rs;
}
else {
rb_scan_args(argc, argv, "1", &rs);
}
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
ary = rb_ary_new();
while (!NIL_P(line = rb_io_getline(rs, fptr))) {
rb_ary_push(ary, line);
}
return ary;
}
static VALUE
rb_io_each_line(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE str;
OpenFile *fptr;
VALUE rs;
if (argc == 0) {
rs = rb_rs;
}
else {
rb_scan_args(argc, argv, "1", &rs);
}
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
while (!NIL_P(str = rb_io_getline(rs, fptr))) {
rb_yield(str);
}
return io;
}
static VALUE
rb_io_each_byte(io)
VALUE io;
{
OpenFile *fptr;
FILE *f;
int c;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
f = fptr->f;
for (;;) {
READ_CHECK(f);
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c == EOF) {
if (ferror(f)) {
if (errno == EINTR) continue;
rb_sys_fail(fptr->path);
}
break;
}
rb_yield(INT2FIX(c & 0xff));
}
if (ferror(f)) rb_sys_fail(fptr->path);
return io;
}
VALUE
rb_io_getc(io)
VALUE io;
{
OpenFile *fptr;
FILE *f;
int c;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
f = fptr->f;
retry:
READ_CHECK(f);
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c == EOF) {
if (ferror(f)) {
if (errno == EINTR) goto retry;
rb_sys_fail(fptr->path);
}
return Qnil;
}
return INT2FIX(c & 0xff);
}
int
rb_getc(f)
FILE *f;
{
int c;
if (!READ_DATA_PENDING(f)) {
rb_thread_wait_fd(fileno(f));
}
TRAP_BEG;
c = getc(f);
TRAP_END;
return c;
}
static VALUE
rb_io_readchar(io)
VALUE io;
{
VALUE c = rb_io_getc(io);
if (NIL_P(c)) {
rb_eof_error();
}
return c;
}
VALUE
rb_io_ungetc(io, c)
VALUE io, c;
{
OpenFile *fptr;
int cc = NUM2INT(c);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (ungetc(cc, fptr->f) == EOF && cc != EOF)
rb_sys_fail(fptr->path);
return Qnil;
}
static VALUE
rb_io_isatty(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (isatty(fileno(fptr->f)) == 0)
return Qfalse;
return Qtrue;
}
static void
fptr_finalize(fptr, fin)
OpenFile *fptr;
int fin;
{
int n1 = 0, n2 = 0, e = 0, f1, f2 = -1;
if (fptr->f2) {
f2 = fileno(fptr->f2);
n2 = fclose(fptr->f2);
fptr->f2 = 0;
if (n2 < 0) e = errno;
}
if (fptr->f) {
f1 = fileno(fptr->f);
n1 = fclose(fptr->f);
fptr->f = 0;
if (n1 < 0 && errno == EBADF) {
if (f1 == f2 || !(fptr->mode & FMODE_WBUF)) {
n1 = 0;
}
}
}
if (!fin && (n1 < 0 || n2 < 0)) {
if (n1 == 0) errno = e;
rb_sys_fail(fptr->path);
}
}
static void
rb_io_fptr_cleanup(fptr, fin)
OpenFile *fptr;
int fin;
{
if (fptr->finalize) {
(*fptr->finalize)(fptr);
}
else {
fptr_finalize(fptr, fin);
}
if (fptr->path) {
free(fptr->path);
fptr->path = 0;
}
}
void
rb_io_fptr_finalize(fptr)
OpenFile *fptr;
{
if (!fptr) return;
if (!fptr->f && !fptr->f2) return;
if (fileno(fptr->f) < 3) return;
rb_io_fptr_cleanup(fptr, Qtrue);
}
VALUE
rb_io_close(io)
VALUE io;
{
OpenFile *fptr;
int fd;
fptr = RFILE(io)->fptr;
if (!fptr) return Qnil;
if (!fptr->f && !fptr->f2) return Qnil;
fd = fileno(fptr->f);
rb_io_fptr_cleanup(fptr, Qfalse);
rb_thread_fd_close(fd);
if (fptr->pid) {
rb_syswait(fptr->pid);
fptr->pid = 0;
}
return Qnil;
}
static VALUE
rb_io_close_m(io)
VALUE io;
{
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
rb_raise(rb_eSecurityError, "Insecure: can't close");
}
rb_io_check_closed(RFILE(io)->fptr);
rb_io_close(io);
return Qnil;
}
static VALUE
rb_io_closed(io)
VALUE io;
{
OpenFile *fptr;
fptr = RFILE(io)->fptr;
return (fptr->f || fptr->f2)?Qfalse:Qtrue;
}
static VALUE
rb_io_close_read(io)
VALUE io;
{
OpenFile *fptr;
int n;
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
rb_raise(rb_eSecurityError, "Insecure: can't close");
}
GetOpenFile(io, fptr);
if (fptr->f2 == 0 && (fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
}
if (fptr->f2 == 0) {
return rb_io_close(io);
}
n = fclose(fptr->f);
fptr->mode &= ~FMODE_READABLE;
fptr->f = fptr->f2;
fptr->f2 = 0;
if (n != 0) rb_sys_fail(fptr->path);
return Qnil;
}
static VALUE
rb_io_close_write(io)
VALUE io;
{
OpenFile *fptr;
int n;
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
rb_raise(rb_eSecurityError, "Insecure: can't close");
}
GetOpenFile(io, fptr);
if (fptr->f2 == 0 && (fptr->mode & FMODE_READABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
if (fptr->f2 == 0) {
return rb_io_close(io);
}
n = fclose(fptr->f2);
fptr->f2 = 0;
fptr->mode &= ~FMODE_WRITABLE;
if (n != 0) rb_sys_fail(fptr->path);
return Qnil;
}
static VALUE
rb_io_sysseek(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE offset, ptrname;
int whence = SEEK_SET;
OpenFile *fptr;
off_t pos;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
whence = NUM2INT(ptrname);
}
GetOpenFile(io, fptr);
if ((fptr->mode & FMODE_READABLE) && READ_DATA_PENDING(fptr->f)) {
rb_raise(rb_eIOError, "sysseek for buffered IO");
}
if ((fptr->mode & FMODE_WRITABLE) && (fptr->mode & FMODE_WBUF)) {
rb_warn("sysseek for buffered IO");
}
pos = lseek(fileno(fptr->f), NUM2OFFT(offset), whence);
if (pos == -1) rb_sys_fail(fptr->path);
clearerr(fptr->f);
return OFFT2NUM(pos);
}
static VALUE
rb_io_syswrite(io, str)
VALUE io, str;
{
OpenFile *fptr;
FILE *f;
long n;
rb_secure(4);
if (TYPE(str) != T_STRING)
str = rb_obj_as_string(str);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
if (fptr->mode & FMODE_WBUF) {
rb_warn("syswrite for buffered IO");
}
if (!rb_thread_fd_writable(fileno(f))) {
rb_io_check_closed(fptr);
}
n = write(fileno(f), RSTRING(str)->ptr, RSTRING(str)->len);
if (n == -1) rb_sys_fail(fptr->path);
return LONG2FIX(n);
}
static VALUE
rb_io_sysread(io, len)
VALUE io, len;
{
OpenFile *fptr;
long n, ilen;
VALUE str;
ilen = NUM2LONG(len);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (READ_DATA_PENDING(fptr->f)) {
rb_raise(rb_eIOError, "sysread for buffered IO");
}
str = rb_str_new(0, ilen);
n = fileno(fptr->f);
rb_thread_wait_fd(fileno(fptr->f));
TRAP_BEG;
n = read(fileno(fptr->f), RSTRING(str)->ptr, RSTRING(str)->len);
TRAP_END;
if (n == -1) rb_sys_fail(fptr->path);
if (n == 0 && ilen > 0) {
rb_eof_error();
}
RSTRING(str)->len = n;
RSTRING(str)->ptr[n] = '\0';
OBJ_TAINT(str);
return str;
}
VALUE
rb_io_binmode(io)
VALUE io;
{
#if defined(NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
OpenFile *fptr;
GetOpenFile(io, fptr);
#ifdef __human68k__
if (fptr->f)
fmode(fptr->f, _IOBIN);
if (fptr->f2)
fmode(fptr->f2, _IOBIN);
#else
if (fptr->f && setmode(fileno(fptr->f), O_BINARY) == -1)
rb_sys_fail(fptr->path);
if (fptr->f2 && setmode(fileno(fptr->f2), O_BINARY) == -1)
rb_sys_fail(fptr->path);
#endif
fptr->mode |= FMODE_BINMODE;
#endif
return io;
}
int
rb_io_mode_flags(mode)
const char *mode;
{
int flags = 0;
const char *m = mode;
switch (*m++) {
case 'r':
flags |= FMODE_READABLE;
break;
case 'w':
flags |= FMODE_WRITABLE;
break;
case 'a':
flags |= FMODE_WRITABLE;
break;
default:
error:
rb_raise(rb_eArgError, "illegal access mode %s", mode);
}
while (*m) {
switch (*m++) {
case 'b':
flags |= FMODE_BINMODE;
break;
case '+':
flags |= FMODE_READWRITE;
break;
default:
goto error;
}
}
return flags;
}
static int
rb_io_modenum_flags(mode)
int mode;
{
int flags = 0;
switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
flags = FMODE_READABLE;
break;
case O_WRONLY:
flags = FMODE_WRITABLE;
break;
case O_RDWR:
flags = FMODE_WRITABLE|FMODE_READABLE;
break;
}
#ifdef O_BINARY
if (mode & O_BINARY) {
flags |= FMODE_BINMODE;
}
#endif
return flags;
}
static int
rb_io_mode_modenum(mode)
const char *mode;
{
int flags = 0;
const char *m = mode;
switch (*m++) {
case 'r':
flags |= O_RDONLY;
break;
case 'w':
flags |= O_WRONLY | O_CREAT | O_TRUNC;
break;
case 'a':
flags |= O_WRONLY | O_CREAT | O_APPEND;
break;
default:
error:
rb_raise(rb_eArgError, "illegal access mode %s", mode);
}
while (*m) {
switch (*m++) {
case 'b':
#ifdef O_BINARY
flags |= O_BINARY;
#endif
break;
case '+':
flags |= O_RDWR;
break;
default:
goto error;
}
}
return flags;
}
static char*
rb_io_modenum_mode(flags, mode)
int flags;
char *mode;
{
char *p = mode;
switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
*p++ = 'r';
break;
case O_WRONLY:
*p++ = 'w';
break;
case O_RDWR:
*p++ = 'r';
*p++ = '+';
break;
}
*p++ = '\0';
#ifdef O_BINARY
if (flags & O_BINARY) {
if (mode[1] == '+') {
mode[1] = 'b'; mode[2] = '+'; mode[3] = '\0';
}
else {
mode[1] = 'b'; mode[2] = '\0';
}
}
#endif
return mode;
}
static int
rb_sysopen(fname, flags, mode)
char *fname;
int flags;
unsigned int mode;
{
int fd;
fd = open(fname, flags, mode);
if (fd < 0) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
fd = open(fname, flags, mode);
}
if (fd < 0) {
rb_sys_fail(fname);
}
}
return fd;
}
FILE *
rb_fopen(fname, mode)
const char *fname;
const char *mode;
{
FILE *file;
file = fopen(fname, mode);
if (!file) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
file = fopen(fname, mode);
}
if (!file) {
rb_sys_fail(fname);
}
}
#ifdef USE_SETVBUF
if (setvbuf(file, NULL, _IOFBF, 0) != 0)
rb_warn("setvbuf() can't be honered for %s", fname);
#endif
#ifdef __human68k__
fmode(file, _IOTEXT);
#endif
return file;
}
FILE *
rb_fdopen(fd, mode)
int fd;
const char *mode;
{
FILE *file;
file = fdopen(fd, mode);
if (!file) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
file = fdopen(fd, mode);
}
if (!file) {
rb_sys_fail(0);
}
}
#ifdef USE_SETVBUF
if (setvbuf(file, NULL, _IOFBF, 0) != 0)
rb_warn("setvbuf() can't be honered (fd=%d)", fd);
#endif
return file;
}
static VALUE
rb_file_open_internal(io, fname, mode)
VALUE io;
const char *fname, *mode;
{
OpenFile *fptr;
MakeOpenFile(io, fptr);
fptr->mode = rb_io_mode_flags(mode);
fptr->f = rb_fopen(fname, mode);
fptr->path = strdup(fname);
return io;
}
VALUE
rb_file_open(fname, mode)
const char *fname, *mode;
{
VALUE io = rb_obj_alloc(rb_cFile);
return rb_file_open_internal(io, fname, mode);
}
static VALUE
rb_file_sysopen_internal(io, fname, flags, mode)
VALUE io;
char *fname;
int flags, mode;
{
OpenFile *fptr;
int fd;
char *m;
char mbuf[4];
MakeOpenFile(io, fptr);
fd = rb_sysopen(fname, flags, mode);
m = rb_io_modenum_mode(flags, mbuf);
fptr->mode = rb_io_modenum_flags(flags);
fptr->f = rb_fdopen(fd, m);
fptr->path = strdup(fname);
return io;
}
VALUE
rb_file_sysopen(fname, flags, mode)
const char *fname;
int flags, mode;
{
VALUE io = rb_obj_alloc(rb_cFile);
return rb_file_sysopen_internal(io, fname, flags, mode);
}
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
static struct pipe_list {
OpenFile *fptr;
struct pipe_list *next;
} *pipe_list;
static void
pipe_add_fptr(fptr)
OpenFile *fptr;
{
struct pipe_list *list;
list = ALLOC(struct pipe_list);
list->fptr = fptr;
list->next = pipe_list;
pipe_list = list;
}
static void
pipe_del_fptr(fptr)
OpenFile *fptr;
{
struct pipe_list *list = pipe_list;
struct pipe_list *tmp;
if (list->fptr == fptr) {
pipe_list = list->next;
free(list);
return;
}
while (list->next) {
if (list->next->fptr == fptr) {
tmp = list->next;
list->next = list->next->next;
free(tmp);
return;
}
list = list->next;
}
}
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
static void
pipe_atexit _((void))
{
struct pipe_list *list = pipe_list;
struct pipe_list *tmp;
while (list) {
tmp = list->next;
rb_io_fptr_finalize(list->fptr);
list = tmp;
}
}
#endif
static void pipe_finalize _((OpenFile *fptr));
static void
pipe_finalize(fptr)
OpenFile *fptr;
{
#if !defined (__CYGWIN__) && !defined(NT)
extern VALUE rb_last_status;
int status;
if (fptr->f) {
status = pclose(fptr->f);
}
if (fptr->f2) {
status = pclose(fptr->f2);
}
fptr->f = fptr->f2 = 0;
#if defined DJGPP
status <<= 8;
#endif
rb_last_status = INT2FIX(status);
#else
fptr_finalize(fptr, Qtrue);
#endif
pipe_del_fptr(fptr);
}
#endif
void
rb_io_synchronized(fptr)
OpenFile *fptr;
{
fptr->mode |= FMODE_SYNC;
}
void
rb_io_unbuffered(fptr)
OpenFile *fptr;
{
rb_io_synchronized(fptr);
}
static VALUE
pipe_open(pname, mode)
char *pname, *mode;
{
int modef = rb_io_mode_flags(mode);
OpenFile *fptr;
#if defined(DJGPP) || defined(__human68k__) || defined(__VMS)
FILE *f = popen(pname, mode);
if (!f) rb_sys_fail(pname);
else {
VALUE port = rb_obj_alloc(rb_cIO);
MakeOpenFile(port, fptr);
fptr->finalize = pipe_finalize;
fptr->mode = modef;
pipe_add_fptr(fptr);
if (modef & FMODE_READABLE) fptr->f = f;
if (modef & FMODE_WRITABLE) {
if (fptr->f) fptr->f2 = f;
else fptr->f = f;
rb_io_synchronized(fptr);
}
return (VALUE)port;
}
#else
#if defined(NT)
int pid;
FILE *fpr, *fpw;
retry:
pid = pipe_exec(pname, rb_io_mode_modenum(mode), &fpr, &fpw);
if (pid == -1) { /* exec failed */
if (errno == EAGAIN) {
rb_thread_sleep(1);
goto retry;
}
rb_sys_fail(pname);
}
else {
VALUE port = rb_obj_alloc(rb_cIO);
MakeOpenFile(port, fptr);
fptr->mode = modef;
fptr->mode |= FMODE_SYNC;
fptr->pid = pid;
if (modef & FMODE_READABLE) {
fptr->f = fpr;
}
if (modef & FMODE_WRITABLE) {
if (fptr->f) fptr->f2 = fpw;
else fptr->f = fpw;
}
fptr->finalize = pipe_finalize;
pipe_add_fptr(fptr);
return (VALUE)port;
}
#else
int pid, pr[2], pw[2];
volatile int doexec;
if (((modef & FMODE_READABLE) && pipe(pr) == -1) ||
((modef & FMODE_WRITABLE) && pipe(pw) == -1))
rb_sys_fail(pname);
doexec = (strcmp("-", pname) != 0);
if (!doexec) {
fflush(stdin); /* is it really needed? */
fflush(stdout);
fflush(stderr);
}
retry:
switch ((pid = fork())) {
case 0: /* child */
if (modef & FMODE_READABLE) {
close(pr[0]);
if (pr[1] != 1) {
dup2(pr[1], 1);
close(pr[1]);
}
}
if (modef & FMODE_WRITABLE) {
close(pw[1]);
if (pw[0] != 0) {
dup2(pw[0], 0);
close(pw[0]);
}
}
if (doexec) {
int fd;
for (fd = 3; fd < NOFILE; fd++)
close(fd);
rb_proc_exec(pname);
fprintf(stderr, "%s:%d: command not found: %s\n",
ruby_sourcefile, ruby_sourceline, pname);
_exit(127);
}
rb_io_synchronized(RFILE(orig_stdout)->fptr);
rb_io_synchronized(RFILE(orig_stderr)->fptr);
return Qnil;
case -1: /* fork failed */
if (errno == EAGAIN) {
rb_thread_sleep(1);
goto retry;
}
close(pr[0]); close(pw[1]);
rb_sys_fail(pname);
break;
default: /* parent */
if (pid < 0) rb_sys_fail(pname);
else {
VALUE port = rb_obj_alloc(rb_cIO);
MakeOpenFile(port, fptr);
fptr->mode = modef;
fptr->mode |= FMODE_SYNC;
fptr->pid = pid;
if (modef & FMODE_READABLE) {
close(pr[1]);
fptr->f = rb_fdopen(pr[0], "r");
}
if (modef & FMODE_WRITABLE) {
FILE *f = rb_fdopen(pw[1], "w");
close(pw[0]);
if (fptr->f) fptr->f2 = f;
else fptr->f = f;
}
#if defined (__CYGWIN__)
fptr->finalize = pipe_finalize;
pipe_add_fptr(fptr);
#endif
return port;
}
}
#endif
#endif
}
static VALUE
rb_io_popen(str, argc, argv, klass)
char *str;
int argc;
VALUE *argv;
VALUE klass;
{
char *mode;
VALUE pname, pmode, port;
char mbuf[4];
if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
mode = "r";
}
else if (FIXNUM_P(pmode)) {
mode = rb_io_modenum_mode(FIX2INT(pmode), mbuf);
}
else {
mode = StringValuePtr(pmode);
}
SafeStringValue(pname);
port = pipe_open(str, mode);
if (NIL_P(port)) {
/* child */
if (rb_block_given_p()) {
rb_yield(Qnil);
fflush(stdout);
fflush(stderr);
_exit(0);
}
return Qnil;
}
RBASIC(port)->klass = klass;
if (rb_block_given_p()) {
return rb_ensure(rb_yield, port, rb_io_close, port);
}
return port;
}
static VALUE
rb_io_s_popen(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
char *str = 0;
if (argc >= 1) {
str = StringValuePtr(argv[0]);
}
return rb_io_popen(str, argc, argv, klass);
}
static VALUE
rb_open_file(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE fname, vmode, perm;
char *path, *mode;
int flags, fmode;
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
SafeStringValue(fname);
path = RSTRING(fname)->ptr;
if (FIXNUM_P(vmode) || !NIL_P(perm)) {
flags = FIXNUM_P(vmode) ? NUM2INT(vmode) : rb_io_mode_modenum(StringValuePtr(vmode));
fmode = NIL_P(perm) ? 0666 : NUM2INT(perm);
rb_file_sysopen_internal(io, path, flags, fmode);
}
else {
mode = NIL_P(vmode) ? "r" : StringValuePtr(vmode);
rb_file_open_internal(io, RSTRING(fname)->ptr, mode);
}
return io;
}
static VALUE
rb_io_s_open(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
VALUE io = rb_class_new_instance(argc, argv, klass);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, rb_io_close, io);
}
return io;
}
static VALUE
rb_io_s_sysopen(argc, argv)
int argc;
VALUE *argv;
{
VALUE fname, vmode, perm;
int flags, fmode, fd;
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
SafeStringValue(fname);
if (NIL_P(vmode)) flags = O_RDONLY;
else if (FIXNUM_P(vmode)) flags = NUM2INT(vmode);
else {
flags = rb_io_mode_modenum(StringValuePtr(vmode));
}
if (NIL_P(perm)) fmode = 0666;
else fmode = NUM2INT(perm);
fd = rb_sysopen(RSTRING(fname)->ptr, flags, fmode);
return INT2NUM(fd);
}
static VALUE
rb_f_open(argc, argv)
int argc;
VALUE *argv;
{
if (argc >= 1) {
char *str = StringValuePtr(argv[0]);
if (str[0] == '|') {
return rb_io_popen(str+1, argc, argv, rb_cIO);
}
}
return rb_io_s_open(argc, argv, rb_cFile);
}
static VALUE
rb_io_open(fname, mode)
char *fname, *mode;
{
if (fname[0] == '|') {
return pipe_open(fname+1, mode);
}
else {
return rb_file_open(fname, mode);
}
}
static VALUE
rb_io_get_io(io)
VALUE io;
{
return rb_convert_type(io, T_FILE, "IO", "to_io");
}
static char*
rb_io_mode_string(fptr)
OpenFile *fptr;
{
switch (fptr->mode & FMODE_READWRITE) {
case FMODE_READABLE:
default:
return "r";
case FMODE_WRITABLE:
return "w";
case FMODE_READWRITE:
return "r+";
}
}
static VALUE
io_reopen(io, nfile)
VALUE io, nfile;
{
OpenFile *fptr, *orig;
char *mode;
int fd;
off_t pos = 0;
nfile = rb_io_get_io(nfile);
if (rb_safe_level() >= 4 && (!OBJ_TAINTED(io) || !OBJ_TAINTED(nfile))) {
rb_raise(rb_eSecurityError, "Insecure: can't reopen");
}
GetOpenFile(io, fptr);
GetOpenFile(nfile, orig);
if (fptr == orig) return io;
if (orig->mode & FMODE_READABLE) {
pos = ftello(orig->f);
}
if (orig->f2) {
io_fflush(orig->f2, orig);
}
else if (orig->mode & FMODE_WRITABLE) {
io_fflush(orig->f, orig);
}
rb_thread_fd_close(fileno(fptr->f));
/* copy OpenFile structure */
fptr->mode = orig->mode;
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
if (fptr->path) free(fptr->path);
if (orig->path) fptr->path = strdup(orig->path);
else fptr->path = 0;
fptr->finalize = orig->finalize;
mode = rb_io_mode_string(fptr);
fd = fileno(fptr->f);
if (fd < 3) {
clearerr(fptr->f);
/* need to keep stdio objects */
if (dup2(fileno(orig->f), fd) < 0)
rb_sys_fail(orig->path);
}
else {
fclose(fptr->f);
if (dup2(fileno(orig->f), fd) < 0)
rb_sys_fail(orig->path);
fptr->f = rb_fdopen(fd, mode);
}
if ((orig->mode & FMODE_READABLE) && pos >= 0) {
fseeko(fptr->f, pos, SEEK_SET);
fseeko(orig->f, pos, SEEK_SET);
}
if (fptr->f2) {
fd = fileno(fptr->f2);
fclose(fptr->f2);
if (orig->f2) {
if (dup2(fileno(orig->f2), fd) < 0)
rb_sys_fail(orig->path);
fptr->f2 = rb_fdopen(fd, "w");
}
else {
fptr->f2 = 0;
}
}
if (fptr->mode & FMODE_BINMODE) {
rb_io_binmode(io);
}
RBASIC(io)->klass = RBASIC(nfile)->klass;
return io;
}
static VALUE
rb_io_reopen(argc, argv, file)
int argc;
VALUE *argv;
VALUE file;
{
VALUE fname, nmode;
char *mode;
OpenFile *fptr;
rb_secure(4);
if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
if (TYPE(fname) != T_STRING) { /* fname must be IO */
return io_reopen(file, fname);
}
}
SafeStringValue(fname);
if (!NIL_P(nmode)) {
mode = StringValuePtr(nmode);
}
else {
mode = "r";
}
GetOpenFile(file, fptr);
if (fptr->path) {
free(fptr->path);
fptr->path = 0;
}
fptr->path = strdup(RSTRING(fname)->ptr);
fptr->mode = rb_io_mode_flags(mode);
if (!fptr->f) {
fptr->f = rb_fopen(RSTRING(fname)->ptr, mode);
if (fptr->f2) {
fclose(fptr->f2);
fptr->f2 = 0;
}
return file;
}
if (freopen(RSTRING(fname)->ptr, mode, fptr->f) == 0) {
rb_sys_fail(fptr->path);
}
#ifdef USE_SETVBUF
if (setvbuf(fptr->f, NULL, _IOFBF, 0) != 0)
rb_warn("setvbuf() can't be honered for %s", RSTRING(fname)->ptr);
#endif
if (fptr->f2) {
if (freopen(RSTRING(fname)->ptr, "w", fptr->f2) == 0) {
rb_sys_fail(fptr->path);
}
}
return file;
}
static VALUE
rb_io_become(clone, io)
VALUE io;
{
OpenFile *fptr, *orig;
int fd;
char *mode;
io = rb_io_get_io(io);
GetOpenFile(io, orig);
MakeOpenFile(clone, fptr);
if (orig->f2) {
io_fflush(orig->f2, orig);
}
else if (orig->mode & FMODE_WRITABLE) {
io_fflush(orig->f, orig);
}
/* copy OpenFile structure */
fptr->mode = orig->mode;
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
if (orig->path) fptr->path = strdup(orig->path);
fptr->finalize = orig->finalize;
switch (fptr->mode & FMODE_READWRITE) {
case FMODE_READABLE:
default:
mode = "r"; break;
case FMODE_WRITABLE:
mode = "w"; break;
case FMODE_READWRITE:
if (orig->f2) mode = "r";
else mode = "r+";
break;
}
fd = ruby_dup(fileno(orig->f));
fptr->f = rb_fdopen(fd, mode);
if (orig->f2) {
if (fileno(orig->f) != fileno(orig->f2)) {
fd = ruby_dup(fileno(orig->f2));
}
fptr->f2 = rb_fdopen(fd, "w");
}
if (fptr->mode & FMODE_BINMODE) {
rb_io_binmode(clone);
}
return clone;
}
VALUE
rb_io_printf(argc, argv, out)
int argc;
VALUE argv[];
VALUE out;
{
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
}
static VALUE
rb_f_printf(argc, argv)
int argc;
VALUE argv[];
{
VALUE out;
if (argc == 0) return Qnil;
if (TYPE(argv[0]) == T_STRING) {
out = rb_defout;
}
else {
out = argv[0];
argv++;
argc--;
}
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
}
VALUE
rb_io_print(argc, argv, out)
int argc;
VALUE *argv;
VALUE out;
{
int i;
VALUE line;
/* if no argument given, print `$_' */
if (argc == 0) {
argc = 1;
line = rb_lastline_get();
argv = &line;
}
for (i=0; i<argc; i++) {
if (!NIL_P(rb_output_fs) && i>0) {
rb_io_write(out, rb_output_fs);
}
switch (TYPE(argv[i])) {
case T_NIL:
rb_io_write(out, rb_str_new2("nil"));
break;
default:
rb_io_write(out, argv[i]);
break;
}
}
if (!NIL_P(rb_output_rs)) {
rb_io_write(out, rb_output_rs);
}
return Qnil;
}
static VALUE
rb_f_print(argc, argv)
int argc;
VALUE *argv;
{
rb_io_print(argc, argv, rb_defout);
return Qnil;
}
static VALUE
rb_io_putc(io, ch)
VALUE io, ch;
{
char c = NUM2CHR(ch);
rb_io_write(io, rb_str_new(&c, 1));
return ch;
}
static VALUE
rb_f_putc(recv, ch)
VALUE recv, ch;
{
return rb_io_putc(rb_defout, ch);
}
static VALUE
io_puts_ary(ary, out)
VALUE ary, out;
{
VALUE tmp;
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
tmp = RARRAY(ary)->ptr[i];
if (rb_inspecting_p(tmp)) {
tmp = rb_str_new2("[...]");
}
rb_io_puts(1, &tmp, out);
}
return Qnil;
}
VALUE
rb_io_puts(argc, argv, out)
int argc;
VALUE *argv;
VALUE out;
{
int i;
VALUE line;
/* if no argument given, print newline. */
if (argc == 0) {
rb_io_write(out, rb_default_rs);
return Qnil;
}
for (i=0; i<argc; i++) {
if (NIL_P(argv[i])) {
line = rb_str_new2("nil");
}
else {
line = rb_check_convert_type(argv[i], T_ARRAY, "Array", "to_ary");
if (!NIL_P(line)) {
rb_protect_inspect(io_puts_ary, line, out);
continue;
}
line = rb_obj_as_string(argv[i]);
}
rb_io_write(out, line);
if (RSTRING(line)->ptr[RSTRING(line)->len-1] != '\n') {
rb_io_write(out, rb_default_rs);
}
}
return Qnil;
}
static VALUE
rb_f_puts(argc, argv)
int argc;
VALUE *argv;
{
rb_io_puts(argc, argv, rb_defout);
return Qnil;
}
void
rb_p(obj) /* for debug print within C code */
VALUE obj;
{
rb_io_write(rb_defout, rb_obj_as_string(rb_inspect(obj)));
rb_io_write(rb_defout, rb_default_rs);
}
static VALUE
rb_f_p(argc, argv)
int argc;
VALUE *argv;
{
int i;
for (i=0; i<argc; i++) {
rb_p(argv[i]);
}
if (TYPE(rb_defout) == T_FILE) {
rb_io_flush(rb_defout);
}
return Qnil;
}
static VALUE
rb_obj_display(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE out;
if (rb_scan_args(argc, argv, "01", &out) == 0) {
out = rb_defout;
}
rb_io_write(out, self);
return Qnil;
}
static void
rb_io_defset(val, id)
VALUE val;
ID id;
{
if (!rb_respond_to(val, id_write)) {
rb_raise(rb_eTypeError, "$> must have write method, %s given",
rb_class2name(CLASS_OF(val)));
}
rb_defout = val;
}
static void
set_stdin(val, id, var)
VALUE val;
ID id;
VALUE *var;
{
OpenFile *fptr;
if (val == *var) return;
if (TYPE(val) != T_FILE) {
*var = val;
return;
}
if (TYPE(*var) != T_FILE) {
*var = orig_stdin;
}
GetOpenFile(val, fptr);
rb_io_check_readable(fptr);
if (fileno(fptr->f) == 0 && saved_fd[0] != 0) {
dup2(saved_fd[0], 0);
close(saved_fd[0]);
saved_fd[0] = 0;
}
else {
saved_fd[0] = dup(0);
dup2(fileno(fptr->f), 0);
}
*var = val;
}
static void
set_outfile(val, var, orig, stdf)
VALUE val;
VALUE *var;
VALUE orig;
FILE *stdf;
{
OpenFile *fptr;
FILE *f;
int fd;
if (val == *var) return;
if (TYPE(*var) == T_FILE && !rb_io_closed(*var)) {
rb_io_flush(*var);
}
if (TYPE(val) != T_FILE) {
*var = val;
return;
}
if (TYPE(*var) != T_FILE) {
*var = orig;
}
GetOpenFile(val, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
fd = fileno(stdf);
if (fileno(fptr->f) == fd && saved_fd[fd] != fd) {
dup2(saved_fd[fd], fd);
close(saved_fd[fd]);
saved_fd[fd] = fd;
}
else {
saved_fd[fd] = dup(fd);
dup2(fileno(fptr->f), fd);
}
*var = val;
}
static void
set_stdout(val, id, var)
VALUE val;
ID id;
VALUE *var;
{
set_outfile(val, var, orig_stdout, stdout);
}
static void
set_stderr(val, id, var)
VALUE val;
ID id;
VALUE *var;
{
set_outfile(val, var, orig_stderr, stderr);
}
static VALUE
prep_stdio(f, mode, klass)
FILE *f;
int mode;
VALUE klass;
{
OpenFile *fp;
VALUE io = rb_obj_alloc(klass);
MakeOpenFile(io, fp);
fp->f = f;
fp->mode = mode;
return io;
}
static void
prep_path(io, path)
VALUE io;
char *path;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (fptr->path) rb_bug("illegal prep_path() call");
fptr->path = strdup(path);
}
static VALUE
rb_io_s_alloc(klass)
VALUE klass;
{
NEWOBJ(io, struct RFile);
OBJSETUP(io, klass, T_FILE);
io->fptr = 0;
return (VALUE)io;
}
static VALUE
rb_io_initialize(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE fnum, mode;
OpenFile *fp;
int fd, flags;
char mbuf[4];
rb_scan_args(argc, argv, "11", &fnum, &mode);
fd = NUM2INT(fnum);
if (argc == 2) {
SafeStringValue(mode);
flags = rb_io_mode_modenum(RSTRING(mode)->ptr);
}
else {
#if defined(HAVE_FCNTL) && defined(F_GETFL)
flags = fcntl(fd, F_GETFL);
#else
flags = O_RDONLY;
#endif
}
MakeOpenFile(io, fp);
fp->mode = rb_io_modenum_flags(flags);
fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags, mbuf));
return io;
}
static VALUE
rb_file_initialize(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
if (RFILE(io)->fptr) {
rb_io_close_m(io);
free(RFILE(io)->fptr);
RFILE(io)->fptr = 0;
}
if (0 < argc && argc < 3) {
VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
if (!NIL_P(fd)) {
argv[0] = fd;
return rb_io_initialize(argc, argv, io);
}
}
rb_open_file(argc, argv, io);
return io;
}
static VALUE
rb_io_s_new(argc, argv, klass)
int argc;
VALUE *argv;
VALUE klass;
{
if (rb_block_given_p()) {
char *cname = rb_class2name(klass);
rb_warn("%s::new() does not take block; use %s::open() instead",
cname, cname);
}
return rb_class_new_instance(argc, argv, klass);
}
static int binmode = 0;
static VALUE
argf_forward()
{
return rb_funcall3(current_file, ruby_frame->last_func,
ruby_frame->argc, ruby_frame->argv);
}
static int
next_argv()
{
extern VALUE rb_argv;
char *fn;
if (init_p == 0) {
if (RARRAY(rb_argv)->len > 0) {
next_p = 1;
}
else {
next_p = -1;
current_file = rb_stdin;
filename = rb_str_new2("-");
}
init_p = 1;
first_p = 0;
gets_lineno = 0;
}
retry:
if (next_p == 1) {
next_p = 0;
if (RARRAY(rb_argv)->len > 0) {
filename = rb_ary_shift(rb_argv);
fn = StringValuePtr(filename);
if (strlen(fn) == 1 && fn[0] == '-') {
current_file = rb_stdin;
if (ruby_inplace_mode) {
rb_warn("Can't do inplace edit for stdio");
rb_defout = rb_stdout;
}
}
else {
FILE *fr = rb_fopen(fn, "r");
if (ruby_inplace_mode) {
struct stat st, st2;
VALUE str;
FILE *fw;
if (TYPE(rb_defout) == T_FILE && rb_defout != rb_stdout) {
rb_io_close(rb_defout);
}
fstat(fileno(fr), &st);
if (*ruby_inplace_mode) {
str = rb_str_new2(fn);
#ifdef NO_LONG_FNAME
ruby_add_suffix(str, ruby_inplace_mode);
#else
rb_str_cat2(str, ruby_inplace_mode);
#endif
#ifdef NO_SAFE_RENAME
(void)fclose(fr);
(void)unlink(RSTRING(str)->ptr);
(void)rename(fn, RSTRING(str)->ptr);
fr = rb_fopen(RSTRING(str)->ptr, "r");
#else
if (rename(fn, RSTRING(str)->ptr) < 0) {
rb_warn("Can't rename %s to %s: %s, skipping file",
fn, RSTRING(str)->ptr, strerror(errno));
fclose(fr);
goto retry;
}
#endif
}
else {
#ifdef NO_SAFE_RENAME
rb_fatal("Can't do inplace edit without backup");
#else
if (unlink(fn) < 0) {
rb_warn("Can't remove %s: %s, skipping file",
fn, strerror(errno));
fclose(fr);
goto retry;
}
#endif
}
fw = rb_fopen(fn, "w");
#ifndef NO_SAFE_RENAME
fstat(fileno(fw), &st2);
#ifdef HAVE_FCHMOD
fchmod(fileno(fw), st.st_mode);
#else
chmod(fn, st.st_mode);
#endif
if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
fchown(fileno(fw), st.st_uid, st.st_gid);
}
#endif
rb_defout = prep_stdio(fw, FMODE_WRITABLE, rb_cFile);
prep_path(rb_defout, fn);
}
current_file = prep_stdio(fr, FMODE_READABLE, rb_cFile);
prep_path(current_file, fn);
}
if (binmode) rb_io_binmode(current_file);
}
else {
init_p = 0;
return Qfalse;
}
}
return Qtrue;
}
static void
any_close(file)
VALUE file;
{
if (TYPE(file) == T_FILE)
rb_io_close(file);
else
rb_funcall3(file, rb_intern("close"), 0, 0);
}
static VALUE
argf_getline(argc, argv)
int argc;
VALUE *argv;
{
VALUE line;
retry:
if (!next_argv()) return Qnil;
if (TYPE(current_file) != T_FILE) {
line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
}
else if (argc == 0 && rb_rs == rb_default_rs) {
line = rb_io_gets(current_file);
}
else {
VALUE rs;
OpenFile *fptr;
if (argc == 0) {
rs = rb_rs;
}
else {
rb_scan_args(argc, argv, "1", &rs);
}
GetOpenFile(current_file, fptr);
rb_io_check_readable(fptr);
line = rb_io_getline(rs, fptr);
}
if (NIL_P(line) && next_p != -1) {
any_close(current_file);
next_p = 1;
goto retry;
}
gets_lineno++;
lineno = INT2FIX(gets_lineno);
return line;
}
static VALUE
rb_f_gets(argc, argv)
int argc;
VALUE *argv;
{
VALUE line = argf_getline(argc, argv);
rb_lastline_set(line);
return line;
}
VALUE
rb_gets()
{
VALUE line;
if (rb_rs != rb_default_rs) {
return rb_f_gets(0, 0);
}
retry:
if (!next_argv()) return Qnil;
line = rb_io_gets(current_file);
if (NIL_P(line) && next_p != -1) {
any_close(current_file);
next_p = 1;
goto retry;
}
rb_lastline_set(line);
if (!NIL_P(line)) {
gets_lineno++;
lineno = INT2FIX(gets_lineno);
}
return line;
}
static VALUE
rb_f_readline(argc, argv)
int argc;
VALUE *argv;
{
VALUE line = rb_f_gets(argc, argv);
if (NIL_P(line)) {
rb_eof_error();
}
return line;
}
static VALUE
rb_f_getc()
{
rb_warn("getc is obsolete; use STDIN.getc instead");
return rb_io_getc(rb_stdin);
}
static VALUE
rb_f_readlines(argc, argv)
int argc;
VALUE *argv;
{
VALUE line, ary;
ary = rb_ary_new();
while (!NIL_P(line = argf_getline(argc, argv))) {
rb_ary_push(ary, line);
}
return ary;
}
static VALUE
rb_f_backquote(obj, str)
VALUE obj, str;
{
VALUE port, result;
OpenFile *fptr;
SafeStringValue(str);
port = pipe_open(RSTRING(str)->ptr, "r");
if (NIL_P(port)) return rb_str_new(0,0);
GetOpenFile(port, fptr);
result = read_all(fptr, remain_size(fptr));
rb_io_close(port);
if (NIL_P(result)) return rb_str_new(0,0);
return result;
}
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
static VALUE
rb_f_select(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE read, write, except, timeout, res, list;
fd_set rset, wset, eset, pset;
fd_set *rp, *wp, *ep;
struct timeval *tp, timerec;
OpenFile *fptr;
long i;
int max = 0, n;
int interrupt_flag = 0;
int pending = 0;
rb_scan_args(argc, argv, "13", &read, &write, &except, &timeout);
if (NIL_P(timeout)) {
tp = 0;
}
else {
timerec = rb_time_interval(timeout);
tp = &timerec;
}
FD_ZERO(&pset);
if (!NIL_P(read)) {
Check_Type(read, T_ARRAY);
rp = &rset;
FD_ZERO(rp);
for (i=0; i<RARRAY(read)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
FD_SET(fileno(fptr->f), rp);
if (READ_DATA_PENDING(fptr->f)) { /* check for buffered data */
pending++;
FD_SET(fileno(fptr->f), &pset);
}
if (max < fileno(fptr->f)) max = fileno(fptr->f);
}
if (pending) { /* no blocking if there's buffered data */
timerec.tv_sec = timerec.tv_usec = 0;
tp = &timerec;
}
}
else
rp = 0;
if (!NIL_P(write)) {
Check_Type(write, T_ARRAY);
wp = &wset;
FD_ZERO(wp);
for (i=0; i<RARRAY(write)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
FD_SET(fileno(fptr->f), wp);
if (max < fileno(fptr->f)) max = fileno(fptr->f);
if (fptr->f2) {
FD_SET(fileno(fptr->f2), wp);
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
}
}
}
else
wp = 0;
if (!NIL_P(except)) {
Check_Type(except, T_ARRAY);
ep = &eset;
FD_ZERO(ep);
for (i=0; i<RARRAY(except)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
FD_SET(fileno(fptr->f), ep);
if (max < fileno(fptr->f)) max = fileno(fptr->f);
if (fptr->f2) {
FD_SET(fileno(fptr->f2), ep);
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
}
}
}
else {
ep = 0;
}
max++;
n = rb_thread_select(max, rp, wp, ep, tp);
if (n < 0) {
rb_sys_fail(0);
}
if (!pending && n == 0) return Qnil; /* returns nil on timeout */
res = rb_ary_new2(3);
rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
if (interrupt_flag == 0) {
if (rp) {
list = RARRAY(res)->ptr[0];
for (i=0; i< RARRAY(read)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
if (FD_ISSET(fileno(fptr->f), rp)
|| FD_ISSET(fileno(fptr->f), &pset)) {
rb_ary_push(list, RARRAY(read)->ptr[i]);
}
}
}
if (wp) {
list = RARRAY(res)->ptr[1];
for (i=0; i< RARRAY(write)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
if (FD_ISSET(fileno(fptr->f), wp)) {
rb_ary_push(list, RARRAY(write)->ptr[i]);
}
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), wp)) {
rb_ary_push(list, RARRAY(write)->ptr[i]);
}
}
}
if (ep) {
list = RARRAY(res)->ptr[2];
for (i=0; i< RARRAY(except)->len; i++) {
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
if (FD_ISSET(fileno(fptr->f), ep)) {
rb_ary_push(list, RARRAY(except)->ptr[i]);
}
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), ep)) {
rb_ary_push(list, RARRAY(except)->ptr[i]);
}
}
}
}
return res; /* returns an empty array on interrupt */
}
#if !defined(MSDOS) && !defined(__human68k__)
static int
io_cntl(fd, cmd, narg, io_p)
int fd, cmd, io_p;
long narg;
{
int retval;
#ifdef HAVE_FCNTL
TRAP_BEG;
# if defined(__CYGWIN__)
retval = io_p?ioctl(fd, cmd, (void*)narg):fcntl(fd, cmd, narg);
# else
retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
# endif
TRAP_END;
#else
if (!io_p) {
rb_notimplement();
}
TRAP_BEG;
retval = ioctl(fd, cmd, narg);
TRAP_END;
#endif
return retval;
}
#endif
static VALUE
rb_io_ctl(io, req, arg, io_p)
VALUE io, req, arg;
int io_p;
{
#if !defined(MSDOS) && !defined(__human68k__)
int cmd = NUM2ULONG(req);
OpenFile *fptr;
long len = 0;
long narg = 0;
int retval;
rb_secure(2);
GetOpenFile(io, fptr);
if (NIL_P(arg) || arg == Qfalse) {
narg = 0;
}
else if (FIXNUM_P(arg)) {
narg = FIX2LONG(arg);
}
else if (arg == Qtrue) {
narg = 1;
}
else if (rb_obj_is_kind_of(arg, rb_cInteger)) {
narg = NUM2LONG(arg);
}
else {
StringValue(arg);
#ifdef IOCPARM_MASK
#ifndef IOCPARM_LEN
#define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
#endif
#endif
#ifdef IOCPARM_LEN
len = IOCPARM_LEN(cmd); /* on BSDish systems we're safe */
#else
len = 256; /* otherwise guess at what's safe */
#endif
rb_str_modify(arg);
if (len <= RSTRING(arg)->len) {
len = RSTRING(arg)->len;
}
if (RSTRING(arg)->len < len) {
rb_str_resize(arg, len+1);
}
RSTRING(arg)->ptr[len] = 17; /* a little sanity check here */
narg = (long)RSTRING(arg)->ptr;
}
retval = io_cntl(fileno(fptr->f), cmd, narg, io_p);
if (retval < 0) rb_sys_fail(fptr->path);
if (TYPE(arg) == T_STRING && RSTRING(arg)->ptr[len] != 17) {
rb_raise(rb_eArgError, "return value overflowed string");
}
if (fptr->f2 && fileno(fptr->f) != fileno(fptr->f2)) {
/* call on f2 too; ignore result */
io_cntl(fileno(fptr->f2), cmd, narg, io_p);
}
return INT2NUM(retval);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
static VALUE
rb_io_ioctl(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE req, arg;
rb_scan_args(argc, argv, "11", &req, &arg);
return rb_io_ctl(io, req, arg, 1);
}
static VALUE
rb_io_fcntl(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
#ifdef HAVE_FCNTL
VALUE req, arg;
rb_scan_args(argc, argv, "11", &req, &arg);
return rb_io_ctl(io, req, arg, 0);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
static VALUE
rb_f_syscall(argc, argv)
int argc;
VALUE *argv;
{
#if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
#ifdef atarist
unsigned long arg[14]; /* yes, we really need that many ! */
#else
unsigned long arg[8];
#endif
int retval = -1;
int i = 1;
int items = argc - 1;
/* This probably won't work on machines where sizeof(long) != sizeof(int)
* or where sizeof(long) != sizeof(char*). But such machines will
* not likely have syscall implemented either, so who cares?
*/
rb_secure(2);
if (argc == 0)
rb_raise(rb_eArgError, "too few arguments for syscall");
arg[0] = NUM2LONG(argv[0]); argv++;
while (items--) {
if (FIXNUM_P(*argv)) {
arg[i] = (unsigned long)NUM2LONG(*argv);
}
else {
VALUE v = *argv;
StringValue(v);
rb_str_modify(v);
arg[i] = (unsigned long)RSTRING(v)->ptr;
}
argv++;
i++;
}
TRAP_BEG;
switch (argc) {
case 1:
retval = syscall(arg[0]);
break;
case 2:
retval = syscall(arg[0],arg[1]);
break;
case 3:
retval = syscall(arg[0],arg[1],arg[2]);
break;
case 4:
retval = syscall(arg[0],arg[1],arg[2],arg[3]);
break;
case 5:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4]);
break;
case 6:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
break;
case 7:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
break;
case 8:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7]);
break;
#ifdef atarist
case 9:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8]);
break;
case 10:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8], arg[9]);
break;
case 11:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8], arg[9], arg[10]);
break;
case 12:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8], arg[9], arg[10], arg[11]);
break;
case 13:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
break;
case 14:
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
arg[7], arg[8], arg[9], arg[10], arg[11], arg[12], arg[13]);
break;
#endif /* atarist */
}
TRAP_END;
if (retval < 0) rb_sys_fail(0);
return INT2NUM(retval);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
static VALUE
rb_io_s_pipe()
{
#ifndef __human68k__
int pipes[2];
VALUE r, w;
#ifdef NT
if (_pipe(pipes, 1024, O_BINARY) == -1)
#else
if (pipe(pipes) == -1)
#endif
rb_sys_fail(0);
r = prep_stdio(rb_fdopen(pipes[0], "r"), FMODE_READABLE, rb_cIO);
w = prep_stdio(rb_fdopen(pipes[1], "w"), FMODE_WRITABLE|FMODE_SYNC, rb_cIO);
return rb_assoc_new(r, w);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
struct foreach_arg {
int argc;
VALUE sep;
VALUE io;
OpenFile *fptr;
};
static VALUE
io_s_foreach(arg)
struct foreach_arg *arg;
{
VALUE str;
while (!NIL_P(str = rb_io_getline(arg->sep, arg->fptr))) {
rb_yield(str);
}
return Qnil;
}
static VALUE
rb_io_s_foreach(argc, argv)
int argc;
VALUE *argv;
{
VALUE fname, io;
OpenFile *fptr;
struct foreach_arg arg;
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
SafeStringValue(fname);
if (argc == 1) {
arg.sep = rb_default_rs;
}
io = rb_io_open(RSTRING(fname)->ptr, "r");
if (NIL_P(io)) return Qnil;
GetOpenFile(io, fptr);
arg.fptr = fptr;
return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, io);
}
static VALUE
io_s_readlines(arg)
struct foreach_arg *arg;
{
return rb_io_readlines(arg->argc, &arg->sep, arg->io);
}
static VALUE
rb_io_s_readlines(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE fname;
struct foreach_arg arg;
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
SafeStringValue(fname);
arg.argc = argc - 1;
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
if (NIL_P(arg.io)) return Qnil;
return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
}
static VALUE
io_s_read(arg)
struct foreach_arg *arg;
{
return io_read(arg->argc, &arg->sep, arg->io);
}
static VALUE
rb_io_s_read(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE fname, offset;
struct foreach_arg arg;
rb_scan_args(argc, argv, "12", &fname, &arg.sep, &offset);
SafeStringValue(fname);
arg.argc = argc ? 1 : 0;
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
if (NIL_P(arg.io)) return Qnil;
if (!NIL_P(offset)) {
rb_io_seek(arg.io, offset, SEEK_SET);
}
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
}
static VALUE
argf_tell()
{
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream to tell");
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_tell(current_file);
}
static VALUE
argf_seek_m(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream to seek");
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_seek_m(argc, argv, current_file);
}
static VALUE
argf_set_pos(self, offset)
VALUE self, offset;
{
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream to set position");
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_set_pos(current_file, offset);
}
static VALUE
argf_rewind()
{
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream to rewind");
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_rewind(current_file);
}
static VALUE
argf_fileno()
{
if (!next_argv()) {
rb_raise(rb_eArgError, "no stream");
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_fileno(current_file);
}
static VALUE
argf_to_io()
{
return current_file;
}
static VALUE
argf_read(argc, argv)
int argc;
VALUE *argv;
{
VALUE tmp, str;
long len = 0;
if (argc == 1) len = NUM2LONG(argv[0]);
str = Qnil;
retry:
if (!next_argv()) return str;
if (TYPE(current_file) != T_FILE) {
tmp = argf_forward();
StringValue(tmp);
}
else {
tmp = io_read(argc, argv, current_file);
}
if (NIL_P(tmp) && next_p != -1) {
any_close(current_file);
next_p = 1;
goto retry;
}
if (NIL_P(tmp) || RSTRING(tmp)->len == 0) return str;
else if (NIL_P(str)) str = tmp;
else rb_str_append(str, tmp);
if (argc == 0) {
goto retry;
}
if (RSTRING(tmp)->len < len) {
len -= RSTRING(tmp)->len;
argv[0] = LONG2FIX(len);
goto retry;
}
return str;
}
static VALUE
argf_getc()
{
VALUE byte;
retry:
if (!next_argv()) return Qnil;
if (TYPE(current_file) != T_FILE) {
byte = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
}
else {
byte = rb_io_getc(current_file);
}
if (NIL_P(byte) && next_p != -1) {
any_close(current_file);
next_p = 1;
goto retry;
}
return byte;
}
static VALUE
argf_readchar()
{
VALUE c = argf_getc();
if (NIL_P(c)) {
rb_eof_error();
}
return c;
}
static VALUE
argf_eof()
{
if (!next_argv()) return Qtrue;
if (next_p == 1) {
return Qtrue;
}
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
if (rb_io_eof(current_file)) {
next_p = 1;
return Qtrue;
}
return Qfalse;
}
static VALUE
argf_each_line(argc, argv)
int argc;
VALUE *argv;
{
VALUE str;
while (RTEST(str = argf_getline(argc, argv))) {
rb_yield(str);
}
return argf;
}
static VALUE
argf_each_byte()
{
VALUE byte;
while (!NIL_P(byte = argf_getc())) {
rb_yield(byte);
}
return Qnil;
}
static VALUE
argf_filename()
{
next_argv();
return filename;
}
static VALUE
argf_file()
{
next_argv();
return current_file;
}
static VALUE
argf_binmode()
{
binmode = 1;
next_argv();
if (TYPE(current_file) != T_FILE) {
argf_forward();
}
else {
rb_io_binmode(current_file);
}
return argf;
}
static VALUE
argf_skip()
{
if (next_p != -1) {
any_close(current_file);
next_p = 1;
}
return argf;
}
static VALUE
argf_close()
{
any_close(current_file);
if (next_p != -1) {
next_p = 1;
}
gets_lineno = 0;
return argf;
}
static VALUE
argf_closed()
{
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
return rb_io_closed(current_file);
}
static VALUE
opt_i_get()
{
if (!ruby_inplace_mode) return Qnil;
return rb_str_new2(ruby_inplace_mode);
}
static void
opt_i_set(val)
VALUE val;
{
if (!RTEST(val)) {
if (ruby_inplace_mode) free(ruby_inplace_mode);
ruby_inplace_mode = 0;
return;
}
StringValue(val);
if (ruby_inplace_mode) free(ruby_inplace_mode);
ruby_inplace_mode = 0;
ruby_inplace_mode = strdup(RSTRING(val)->ptr);
}
void
Init_IO()
{
rb_eIOError = rb_define_class("IOError", rb_eStandardError);
rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
id_write = rb_intern("write");
rb_define_global_function("syscall", rb_f_syscall, -1);
rb_define_global_function("open", rb_f_open, -1);
rb_define_global_function("printf", rb_f_printf, -1);
rb_define_global_function("print", rb_f_print, -1);
rb_define_global_function("putc", rb_f_putc, 1);
rb_define_global_function("puts", rb_f_puts, -1);
rb_define_global_function("gets", rb_f_gets, -1);
rb_define_global_function("readline", rb_f_readline, -1);
rb_define_global_function("getc", rb_f_getc, 0);
rb_define_global_function("select", rb_f_select, -1);
rb_define_global_function("readlines", rb_f_readlines, -1);
rb_define_global_function("`", rb_f_backquote, 1);
rb_define_global_function("p", rb_f_p, -1);
rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
rb_cIO = rb_define_class("IO", rb_cObject);
rb_include_module(rb_cIO, rb_mEnumerable);
rb_define_singleton_method(rb_cIO, "allocate", rb_io_s_alloc, 0);
rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1);
rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
rb_define_singleton_method(rb_cIO, "for_fd", rb_class_new_instance, -1);
rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0);
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
rb_output_fs = Qnil;
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
rb_rs = rb_default_rs = rb_str_new2("\n");
rb_output_rs = Qnil;
rb_global_variable(&rb_default_rs);
OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
rb_define_variable("$/", &rb_rs);
rb_define_variable("$-0", &rb_rs);
rb_define_variable("$\\", &rb_output_rs);
rb_define_hooked_variable("$.", &lineno, 0, lineno_setter);
rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
rb_define_method(rb_cIO, "become", rb_io_become, 1);
rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
rb_define_method(rb_cIO, "print", rb_io_print, -1);
rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
rb_define_method(rb_cIO, "sysread", rb_io_sysread, 1);
rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
rb_define_alias(rb_cIO, "to_i", "fileno");
rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
rb_define_method(rb_cIO, "read", io_read, -1);
rb_define_method(rb_cIO, "write", io_write, 1);
rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
rb_define_method(rb_cIO, "<<", rb_io_addstr, 1);
rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
rb_define_method(rb_cIO, "binmode", rb_io_binmode, 0);
rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
rb_stdin = orig_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO);
rb_define_hooked_variable("$stdin", &rb_stdin, 0, set_stdin);
rb_stdout = orig_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO);
rb_define_hooked_variable("$stdout", &rb_stdout, 0, set_stdout);
rb_stderr = orig_stderr = prep_stdio(stderr, FMODE_WRITABLE, rb_cIO);
rb_define_hooked_variable("$stderr", &rb_stderr, 0, set_stderr);
rb_defout = rb_stdout;
rb_define_hooked_variable("$>", &rb_defout, 0, rb_io_defset);
rb_define_hooked_variable("$defout", &rb_defout, 0, rb_io_defset);
rb_define_global_const("STDIN", rb_stdin);
rb_define_global_const("STDOUT", rb_stdout);
rb_define_global_const("STDERR", rb_stderr);
argf = rb_obj_alloc(rb_cObject);
rb_extend_object(argf, rb_mEnumerable);
rb_define_readonly_variable("$<", &argf);
rb_define_global_const("ARGF", argf);
rb_define_singleton_method(argf, "fileno", argf_fileno, 0);
rb_define_singleton_method(argf, "to_i", argf_fileno, 0);
rb_define_singleton_method(argf, "to_io", argf_to_io, 0);
rb_define_singleton_method(argf, "each", argf_each_line, -1);
rb_define_singleton_method(argf, "each_line", argf_each_line, -1);
rb_define_singleton_method(argf, "each_byte", argf_each_byte, 0);
rb_define_singleton_method(argf, "read", argf_read, -1);
rb_define_singleton_method(argf, "readlines", rb_f_readlines, -1);
rb_define_singleton_method(argf, "to_a", rb_f_readlines, -1);
rb_define_singleton_method(argf, "gets", rb_f_gets, -1);
rb_define_singleton_method(argf, "readline", rb_f_readline, -1);
rb_define_singleton_method(argf, "getc", argf_getc, 0);
rb_define_singleton_method(argf, "readchar", argf_readchar, 0);
rb_define_singleton_method(argf, "tell", argf_tell, 0);
rb_define_singleton_method(argf, "seek", argf_seek_m, -1);
rb_define_singleton_method(argf, "rewind", argf_rewind, 0);
rb_define_singleton_method(argf, "pos", argf_tell, 0);
rb_define_singleton_method(argf, "pos=", argf_set_pos, 1);
rb_define_singleton_method(argf, "eof", argf_eof, 0);
rb_define_singleton_method(argf, "eof?", argf_eof, 0);
rb_define_singleton_method(argf, "binmode", argf_binmode, 0);
rb_define_singleton_method(argf, "to_s", argf_filename, 0);
rb_define_singleton_method(argf, "filename", argf_filename, 0);
rb_define_singleton_method(argf, "file", argf_file, 0);
rb_define_singleton_method(argf, "skip", argf_skip, 0);
rb_define_singleton_method(argf, "close", argf_close, 0);
rb_define_singleton_method(argf, "closed?", argf_closed, 0);
rb_define_singleton_method(argf, "lineno", argf_lineno, 0);
rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1);
current_file = rb_stdin;
rb_global_variable(¤t_file);
filename = rb_str_new2("-");
rb_define_readonly_variable("$FILENAME", &filename);
rb_define_virtual_variable("$-i", opt_i_get, opt_i_set);
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
atexit(pipe_atexit);
#endif
Init_File();
rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
rb_file_const("RDONLY", INT2FIX(O_RDONLY));
rb_file_const("WRONLY", INT2FIX(O_WRONLY));
rb_file_const("RDWR", INT2FIX(O_RDWR));
rb_file_const("APPEND", INT2FIX(O_APPEND));
rb_file_const("CREAT", INT2FIX(O_CREAT));
rb_file_const("EXCL", INT2FIX(O_EXCL));
#if defined(O_NDELAY) || defined(O_NONBLOCK)
# ifdef O_NONBLOCK
rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK));
# else
rb_file_const("NONBLOCK", INT2FIX(O_NDELAY));
# endif
#endif
rb_file_const("TRUNC", INT2FIX(O_TRUNC));
#ifdef O_NOCTTY
rb_file_const("NOCTTY", INT2FIX(O_NOCTTY));
#endif
#ifdef O_BINARY
rb_file_const("BINARY", INT2FIX(O_BINARY));
#endif
#ifdef O_SYNC
rb_file_const("SYNC", INT2FIX(O_SYNC));
#endif
}
|