[r6736]: sandbox / bruce / trunk / extensions / rexxutil / platform / unix / rexxutil.cpp Maximize Restore History

Download this file

rexxutil.cpp    4901 lines (4349 with data), 224.4 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
/*----------------------------------------------------------------------------*/
/* */
/* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
/* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the Common Public License v1.0 which accompanies this */
/* distribution. A copy is also available at the following address: */
/* http://www.oorexx.org/license.html */
/* */
/* Redistribution and use in source and binary forms, with or */
/* without modification, are permitted provided that the following */
/* conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright */
/* notice, this list of conditions and the following disclaimer. */
/* Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in */
/* the documentation and/or other materials provided with the distribution. */
/* */
/* Neither the name of Rexx Language Association nor the names */
/* of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
/* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/*----------------------------------------------------------------------------*/
/******************************************************************************/
/* REXX AIX/LINUX Support */
/* */
/* AIX system utility functions */
/* */
/******************************************************************************/
/**********************************************************************
* LRXUTIL.C *
* *
* This program extends the REXX language by providing many *
* REXX external functions. *
* These functions are: *
* SysCls -- Clear the screen in an OS/2 fullscreen *
* or windowed command prompt session. *
* SysCurPos -- Set and/or Query the cursor position *
* in an OS/2 fullscreen or windowed *
* command prompt session. *
* SysCurState -- Make the cursor visible or invisible *
* in an OS/2 fullscreen or windowed *
* command prompt session. *
* SysDriveInfo -- Returns information about a specific *
* drive. *
* SysDriveMap -- Returns a list of the drives on the *
* machine *
* SysDropFuncs -- Makes all functions in this package *
* unknown to REXX. *
* SysFileDelete -- Deletes a file *
* SysFileSearch -- Searches for a file matching a given *
* filespec. *
* SysFileTree -- Searches for files matching a given *
* filespec, including files in *
* subdirectories. *
* SysGetKey -- Returns one by of data indicating the *
* key which was pressed, *
* in an OS/2 fullscreen or windowed *
* command prompt session. *
* SysGetMessage -- Retrieves a message text from an OS/2 *
* message file, substituting values *
* provided. *
* SysIni -- Reads and/or updates entries in .INI *
* files. *
* SysLoadFuncs -- Makes all functions in this package *
* known to REXX so REXX programs may *
* call them. *
* SysMkDir -- Creates a directory *
* SysVersion -- Returns the AIX Version number *
* SysLinVer -- Returns the OS/2 Version number *
* SysRmDir -- Removes a directory *
* SysSearchPath -- Searches throught a specified path *
* for a file. *
* SysSleep -- Suspends execution for a number of *
* seconds. *
* SysTempFilename -- Creates a unique filename *
* SysTextScreenRead -- Reads characters from the screen, *
* in an OS/2 fullscreen or windowed *
* command prompt session. *
* SysTextScreenSize -- Returns the size of the window in *
* rows and columns, *
* in an OS/2 fullscreen or windowed *
* command prompt session. *
*EX SysGetEA -- Reads an extended attribute *
* for a file. *
*EX SysPutEA -- Writes an extended attribute *
* for a file. *
*EX SysWaitNamedPipe -- Wait on a named pipe. *
*EX SysSetIcon -- Set a file icon *
*EX SysRegisterObjectClass -- Register a new object class *
*EX SysDeregisterObjectClass -- Remove class registration *
*EX SysQueryClassList -- Get list of registered classes *
*EX SysCreateObject -- Create an object instance *
*EX SysDestroyObject -- Delete an object instance *
*EX SysSetObjectData -- Change object settings data *
*EX SysElapsedTime -- Return the OS/2 elapsed time count *
*EX SysBootDrive -- Return the OS/2 boot drive *
*EX SysQueryEAList -- Return list of file EA names *
*EX SysWildCard -- Perform file wild card editting *
*EX SysFileSystemType -- Return drive file system type *
*EX SysAddFileHandle -- Add file handles to a process *
*EX SysSetFileHandle -- Set file handles for a process *
* SysCreateMutexSem -- Create a Mutex semaphore *
* SysOpenMutexSem -- Open a Mutex semaphore *
* SysCloseMutexSem -- Close a Mutex semaphore *
* SysRequestMutexSem -- Request a Mutex semaphore *
* SysReleaseMutexSem -- Release a Mutex semaphore *
* SysCreateEventSem -- Create an Event semaphore *
* SysOpenEventSem -- Open an Event semaphore *
* SysCloseEventSem -- Close an Event semaphore *
* SysPostEventSem -- Post an Event semaphore *
* SysResetEventSem -- Reset an Event semaphore *
* SysWaitEventSem -- Wait on an Event semaphore *
*EX SysProcessType -- Return type of process *
*EX SysSetPriority -- Set current thread priority *
*EX SysGetCollate -- Get country/codepage collating sequence*
*EX SysNationalLanguageCompare -- NLS strict compare *
*EX SysMapCase -- NLS uppercasing *
*EX SysSetProcessCodePage -- Set current code page *
*EX SysQueryProcessCodePage -- Get current code page *
* SysAddRexxMacro -- Load program into macro space *
* SysDropRexxMacro -- Drop program from macro space *
* SysReorderRexxMacro -- Reorder program in macro space *
* SysQueryRexxMacro -- Query ordering of macro space program *
* SysClearRexxMacroSpace -- Remove all programs from macro space*
* SysLoadRexxMacroSpace -- Load a Rexx macro space *
* SysSaveRexxMacroSpace -- Save a Rexx macro space *
*EX SysShutDownSystem -- Shutdown the system *
*EX SysSwitchSession -- Switch to a named session *
*EX SysDropLibrary -- Drop a function package *
* EX SysPi -- Return Pi to given precision *
* EX SysSqrt -- Calculate a square root *
* EX SysExp -- Calculate an exponent *
* EX SysLog -- Return natural log of a number *
* EX SysLog10 -- Return log base 10 of a number *
* EX SysSinh -- Hyperbolic sine function *
* EX SysCosh -- Hyperbolic cosine function *
* EX SysTanh -- Hyperbolic tangent function *
* EX SysPower -- raise number to non-integer power *
* EX SysSin -- Sine function *
* EX SysCos -- Cosine function *
* EX SysTan -- Tangent function *
* EX SysCotan -- Cotangent function *
* EX SysArcSin -- ArcSine function *
* EX SysArcCos -- ArcCosine function *
* EX SysArcTan -- ArcTangent function *
*EX SysSaveObject -- Save an object either Async or Sync *
*EX SysOpenObject -- Open a view of an object *
*EX SysMoveObject -- Move an object from the existing folder*
* to the destination folder *
*EX SysCopyObject -- Copy an object from the existing folder*
* to the destination folder *
*EX SysCreateShadow -- Create a shadow of an object *
*EX SysWaitForShell -- Wait for WPS initialization *
* (Merlin only). *
*EX SysQuerySwitchList -- query the entries of the switch list *
* (for SysSwitchSession) *
* SysDumpVariables -- Dump current variables to a file *
* SysSetFileDateTime -- Set the last modified date of a file *
* SysGetFileDateTime -- Get the last modified date of a file *
* SysStemSort -- sort a stem array *
* SysStemDelete -- delete items in a stem array *
* SysStemInsert -- insert items into a stem array *
* SysStemCopy -- copy items from one stem array to other*
* SysGetErrortext -- Retrieve textual desc. of error number *
* SysQueryProcess -- Get information on current proc/thread *
* SysUtilVersion -- query version of REXXUTIL.DLL *
* *
* SysAddFuncPkg -- CREXX for AIX function support *
* SysAddCmdPkg -- CREXX for AIX function support *
* SysDropFuncPkg -- CREXX for AIX function support *
* SysDropCmdPkg -- CREXX for AIX function support *
* SysGetpid -- CREXX for AIX function support *
* SysFork -- CREXX for AIX function support *
* SysWait -- CREXX for AIX function support *
* SysCreatePipe -- CREXX for AIX function support *
* *
* SysIsFile -- does file exist? *
* SysIsFileDirectory -- is file a subdirectory? *
* SysIsFileLink -- is file a link? *
* *
**********************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined( HAVE_LOCALE_H )
# include <locale.h>
#endif
#include "oorexxapi.h"
#if defined( HAVE_SYS_WAIT_H )
# include <sys/wait.h>
#endif
#include <sys/ipc.h>
#include <memory.h>
#if defined( HAVE_MALLOC_H )
# include <malloc.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <math.h>
#include <limits.h>
#include <sys/stat.h> /* mkdir() function */
#include <errno.h> /* get the errno variable */
#include <stddef.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <pthread.h>
#include <semaphore.h>
#if defined(__APPLE__) && defined(__MACH__)
# define fstat64 fstat
# define lseek64 lseek
# define lstat64 lstat
# define open64 open
# define stat64 stat
#endif
#if defined( HAVE_SYS_SEM_H )
# include <sys/sem.h>
#endif
#include <dirent.h> /* directory functions */
#include <sys/time.h> /* needed for the select func */
#include <time.h> /* needed for the select func */
#if defined( HAVE_SYS_SELECT_H )
# include <sys/select.h> /* needed for the select func */
#endif
#if defined( HAVE_SYS_LDR_H )
# include <sys/ldr.h> /* needed for the load func */
#endif
#if defined( HAVE_STRINGS_H )
# include <strings.h>
#endif
#include <utime.h> /* moved, used by AIX & Linux */
#if defined( HAVE_SYS_UTSNAME_H )
# include <sys/utsname.h> /* get the uname() function */
#endif
#include <signal.h>
#if defined( HAVE_SYS_RESOURCE_H )
# include <sys/resource.h> /* get the getpriority() func */
#endif
#if defined( HAVE_FEATURES_H )
# include <features.h> /* catopen() */
#endif
#if defined( HAVE_NL_TYPES_H )
# include <nl_types.h> /* catopen() */
#endif
#include <termios.h> /* needed for SysGetKey */
#include <fnmatch.h> /* fnmatch() */
#if !defined( HAVE_UNION_SEMUN )
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
#endif
extern char *resolve_tilde(const char *);
#define INVALID_ROUTINE 40
#define MAX_DIGITS 9
#define NO_UTIL_ERROR "0" /* No error whatsoever */
#define VALID_ROUTINE 0 /* Successful completion */
//#define MAX_LINE_LEN 2048 /* max line length */
#define MAX_LINE_LEN 4096 /* max line length */
#define MAX_READ 0x10000 /* full segment of buffer */
#define CH_EOF 0x1A /* end of file marker */
#define CH_CR '\r' /* carriage return character */
#define CH_NL '\n' /* new line character */
#define MAX 256 /* temporary buffer length */
#define IBUF_LEN 4096 /* Input buffer length */
#define CURRENT_DIR_FIRST 0 /* search flag 'C' */
#define ENVIRONMENT_ONLY 1 /* search flag 'N' */
#define OFFSET 1000 /* needed to prevent collision*/
/* with the return codes */
#define MAXUSECOUNT 65535 /* max semaphore usecount */
#define REXXMESSAGEFILE "rexx.cat"
/*********************************************************************/
/* Various definitions used by the math functions */
/*********************************************************************/
#define SINE 0 /* trig function defines... */
#define COSINE 3 /* the ordering is important, */
#define TANGENT 1 /* as these get transformed */
#define COTANGENT 2 /* depending on the angle */
#define MAXTRIG 3 /* value */
#define ARCSINE 0 /* defines for arc trig */
#define ARCCOSINE 1 /* functions. Ordering is */
#define ARCTANGENT 2 /* not as important here */
#define pi 3.14159265358979323846l /* pi value */
#define DEGREES 'D' /* degrees option */
#define RADIANS 'R' /* radians option */
#define GRADES 'G' /* grades option */
#define DEFAULT_PRECISION 9 /* default precision to use */
#define MAX_PRECISION 16 /* maximum available precision*/
/*********************************************************************/
/* Numeric Error Return Strings */
/*********************************************************************/
#define NO_UTIL_ERROR "0" /* No error whatsoever */
#define ERROR_NOMEM "2" /* Insufficient memory */
#define ERROR_FILEOPEN "3" /* Error opening text file */
/*********************************************************************/
/* Alpha Numeric Return Strings */
/*********************************************************************/
#define ERROR_RETSTR "ERROR:"
/*********************************************************************/
/* Numeric Return calls */
/*********************************************************************/
#define INVALID_ROUTINE 40 /* Raise Rexx error */
#define VALID_ROUTINE 0 /* Successful completion */
/*********************************************************************/
/* Defines used by SysStemSort */
/*********************************************************************/
#define SORT_CASESENSITIVE 0
#define SORT_CASEIGNORE 1
#define SORT_ASCENDING 0
#define SORT_DECENDING 1
#define SORT_NUMERIC 3
#define SORT_DEF_AVG_SIZE 20
/*********************************************************************/
/* Some useful macros */
/*********************************************************************/
#define BUILDRXSTRING(t, s) { \
strcpy((t)->strptr,(s));\
(t)->strlength = strlen((s)); \
}
#define RETVAL(retc) { \
sprintf(retstr->strptr, "%d", retc); \
retstr->strlength = strlen(retstr->strptr); \
return VALID_ROUTINE; \
}
/*********************************************************************/
/* Defines uses by SysTree */
/*********************************************************************/
#define RECURSE 0x0002
#define DO_DIRS 0x0004
#define DO_FILES 0x0008
#define NAME_ONLY 0x0010
#define EDITABLE_TIME 0x0020
#define LONG_TIME 0x0040
#define CASELESS 0x0080
#define IGNORE 2 /* Ignore attributes entirely */
/******************************************************************************/
/* Defines used by SysGetKey */
/******************************************************************************/
#define stty(a,b) (void)tcsetattr(a,TCSANOW,b) /* simple set attr. */
#define gtty(a,b) (void)tcgetattr(a,b) /* simple get attr. */
#define discard_input(a) tcflush(a,TCIFLUSH) /* simple flush */
#define restore_tty(a) stty(ttyfd,a) /* new settings STDIN */
/* original terminal settings */
struct termios in_orig; /* original settings (important!!) */
/*********************************************************************/
/* RxTree Structure used by SysTree. */
/*********************************************************************/
typedef struct RxTreeData {
size_t count; /* Number of lines processed */
SHVBLOCK shvb; /* Request block for RxVar */
size_t stemlen; /* Length of stem */
size_t vlen; /* Length of variable value */
char TargetSpec[MAX+1]; /* Target filespec */
char truefile[MAX+1]; /* expanded file name */
char Temp[MAX+80]; /* buffer for returned values */
char varname[MAX]; /* Buffer for variable name */
size_t nattrib; /* New attrib, diff for each */
} RXTREEDATA;
/*********************************************************************/
/* RxTree Structure used by GetLine, OpenFile and CloseFile */
/*********************************************************************/
typedef struct _GetFileData {
char * buffer; /* file read buffer */
size_t size; /* file size */
size_t data; /* data left in buffer */
size_t residual; /* size left to read */
const char *scan; /* current scan position */
FILE *handle; /* file handle */
} GetFileData;
/*********************************************************************/
/* RxStemData */
/* Structure which describes as generic */
/* stem variable. */
/*********************************************************************/
typedef struct RxStemData {
SHVBLOCK shvb; /* Request block for RxVar */
char ibuf[IBUF_LEN]; /* Input buffer */
char varname[MAX]; /* Buffer for the variable */
/* name */
char stemname[MAX]; /* Buffer for the variable */
/* name */
size_t stemlen; /* Length of stem. */
size_t vlen; /* Length of variable value */
size_t j; /* Temp counter */
size_t tlong; /* Temp counter */
size_t count; /* Number of elements */
/* processed */
} RXSTEMDATA;
/*********************************************************************/
/* SORTMEM used by SysStemSort */
/*********************************************************************/
typedef struct _SORT_MEM {
size_t ulSize;
size_t ulRemaining;
size_t ulItems;
char * pNextBlock;
struct _SORT_MEM *pNext;
char pData;
} SORTMEM, *PSORTMEM;
#ifdef XX__cplusplus
extern "C" {
#endif
/********************************************************************
* Function: string2size_t(string, number) *
* *
* Purpose: Validates and converts an ASCII-Z string from string *
* form to an unsigned long. Returns false if the number *
* is not valid, true if the number was successfully *
* converted. *
* *
* RC: true - Good number converted *
* false - Invalid number supplied. *
*********************************************************************/
bool string2size_t(
const char *string, /* string to convert */
size_t *number) /* converted number */
{
size_t accumulator; /* converted number */
size_t length; /* length of number */
length = strlen(string); /* get length of string */
if (length == 0 || /* if null string */
length > MAX_DIGITS + 1) /* or too long */
{
return false; /* not valid */
}
accumulator = 0; /* start with zero */
while (length) /* while more digits */
{
if (!isdigit(*string)) /* not a digit? */
{
return false; /* tell caller */
}
/* add to accumulator */
accumulator = accumulator * 10 + (*string - '0');
length--; /* reduce length */
string++; /* step pointer */
}
*number = accumulator; /* return the value */
return true; /* good number */
}
/*********************************************************************/
/**************** REXXUTIL Supporting Functions ********************/
/**************** REXXUTIL Supporting Functions ********************/
/**************** REXXUTIL Supporting Functions ********************/
/*********************************************************************/
/********************************************************************
* Function: ReadNextBuffer(filedata) *
* *
* Purpose: Reads the next buffer of data. *
* *
* RC: 0 buffer was read *
* 1 - error occurred reading buffer *
*********************************************************************/
int ReadNextBuffer(
GetFileData *filedata ) /* global file information */
{
size_t size; /* size to read */
char *endptr; /* end of file pointer */
/* get size of this read */
if(filedata->residual >= MAX_READ)
size = MAX_READ; /* read as much as possible */
else
size = filedata->residual; /* take the rest */
/* read the file in */
filedata->data = fread(filedata->buffer, 1, size, filedata->handle);
if(!filedata->data) /* problems ? */
return (1); /* get out */
if (filedata->data != size) /* not get all of it? */
filedata->residual = 0; /* no residual */
else /* residual is remainder */
filedata->residual = filedata->residual - size;
/* look for a EOF mark */
endptr = (char *)memchr(filedata->buffer, CH_EOF, filedata->data);
if (endptr) { /* found an EOF mark */
/* set new length */
filedata->data = (size_t)(endptr - filedata->buffer);
filedata->residual = 0; /* no residual */
}
filedata->scan = filedata->buffer; /* set position to beginning */
return 0;
}
/***********************************************************************/
/* Function: strupr(string) */
/* Purpose: Uppercas the given string */
/***********************************************************************/
void strupr(char * string){
for(;*string != '\0';string++){ /* while not at the end */
*string = toupper(*string);
}
}
/********************************************************************
* Function: OpenFile(file, filedata) *
* *
* Purpose: Prepares a file for reading. *
* *
* RC: 0 - file was opened successfully *
* 1 - file open error occurred *
*********************************************************************/
int OpenFile(
const char *file, /* file name */
GetFileData *filedata ) /* global file information */
{
struct stat64 finfo; /* file information */
char * endptr = NULL; /* end of buffer pointer */
/* try to open the file */
if((filedata->handle = fopen(file,"r")) == NULL)
return (1); /* return failure */
/* retrieve the file size */
if((stat64(file,&finfo) == -1)||(!finfo.st_size)){
fclose(filedata->handle); /* close the file */
return (1); /* and quit */
}
if (finfo.st_size <= MAX_READ) { /* less than a single buffer */
/* allocate buffer for file */
if((filedata->buffer = (char *)malloc(finfo.st_size)) == NULL ){
fclose(filedata->handle); /* close the file */
return (1); /* and quit */
}
filedata->size = finfo.st_size; /* save file size */
filedata->residual = 0; /* no left over information */
/* read the file in */
filedata->data = fread(filedata->buffer, 1,
finfo.st_size, filedata->handle);
if(filedata->data != (size_t)finfo.st_size){/* problems ? */
free(filedata->buffer); /* free the buffer */
fclose(filedata->handle); /* close the file */
return (1); /* and quit */
} /* look for a EOF mark */
endptr = (char *)memchr(filedata->buffer, CH_EOF, filedata->data);
if (endptr) /* found an EOF mark */
/* set new length */
filedata->data = (size_t)(endptr - filedata->buffer);
filedata->scan = filedata->buffer; /* set position to beginning */
}
else { /* need to read partial */
/* allocate buffer for read */
if((filedata->buffer = (char *)malloc(MAX_READ)) == NULL ){
fclose(filedata->handle); /* close the file */
return (1); /* and quit */
}
filedata->size = finfo.st_size; /* save file size */
/* and set remainder */
filedata->residual = filedata->size;
/* read the file in */
if (ReadNextBuffer(filedata)) {
free(filedata->buffer); /* free the buffer */
fclose(filedata->handle); /* close the file */
return 1;
}
}
return 0; /* file is opened */
}
/********************************************************************
* Function: CloseFile(filedata) *
* *
* Purpose: Close a file *
*********************************************************************/
void CloseFile(
GetFileData *filedata ) /* global file information */
{
fclose(filedata->handle); /* close the file */
free(filedata->buffer); /* release the file buffer */
}
/*********************************************************************
* Function: GetLine(line, size, filedata) *
* *
* Purpose: Reads a line of data using buffered reads. At end of *
* file, zero is returned to indicate nothing left. *
* *
* RC: true - line was read successfully *
* false - end of file was reached *
*********************************************************************/
int GetLine(
char *line, /* returned line */
size_t size, /* size of line buffer */
GetFileData *filedata ) /* file handle */
{
const char *scan; /* current scan pointer */
size_t length; /* line length */
size_t copylength; /* copied length */
if (!(filedata->data)) { /* if out of current buffer */
if (filedata->residual) { /* may be another buffer */
ReadNextBuffer(filedata); /* try to read one */
if (!filedata->data) /* nothing more? */
return 1; /* all done */
}
else
return (1); /* return EOF condition */
}
/* look for a line feed */
scan = (const char *)memchr(filedata->scan, CH_NL, filedata->data);
if (scan) { /* found one */
/* calculate the length */
length = scan - filedata->scan;
copylength = length;
if (copylength > size)
{
copylength = size;
}
/* copy over the data */
memcpy(line, filedata->scan, copylength);
line[copylength] = '\0'; /* make into ASCIIZ string */
/* we don't want the CR character in the result string*/
if ( line[copylength - 1] == CH_CR ) {
line[copylength - 1] = '\0';
}
filedata->data -= length + 1; /* reduce the length */
filedata->scan = scan + 1; /* set new scan point */
if (!filedata->data) { /* all used up */
if (filedata->residual) /* more to read */
ReadNextBuffer(filedata); /* read the next buffer */
}
return 0; /* this worked ok */
}
else /* ran off the end */
{
/* now we have scanned the whole buffer, but didn't find LF. */
/* we have two situation that can appear: */
/* 1.) size > filedata->data ==> there is still room in the working */
/* buffer, we can see whether we have scanned the whole file */
/* --> ReadNextBuffer, or this was it, and we return */
/* 2.) size < filedata->buffer ==> we have scanned to the end of the */
/* buffer, more than what would fit into it, but still we */
/* haven't had a hit. So copy all elements into the buffer */
/* read the next buffer, GetLine to get the next LF */
/* and return what was put into buffer. Be ALWAYS AWARE that */
/* that buffer limits to 2047 bytes, and that we only return up */
/* to 2047 bytes of a line. The rest of the line is not returned */
/* and not checked for search argument. Nevertheless, this */
/* garantees, that the line counter (argument 'N') corresponds */
/* with the input file */
/* get length to copy */
if (size > filedata->data)
{
copylength = filedata->data; /* copy the rest into linebuffer */
/* copy over the data */
memcpy(line, filedata->scan, copylength);
line[copylength] = '\0'; /* make into ASCIIZ string */
/* all data should be read, filedata->data must be zero */
filedata->data -= copylength;
/* scan should be at the end */
filedata->scan += copylength; /* set new scan point */
/* if no more data to read in the file, return OK */
if (!filedata->residual)
return 0;
else
return GetLine(line + copylength, size - copylength, filedata);
}
else /* the line is full, scan until LF found but no copy */
{
copylength = filedata->data;
if (size < copylength)
{
copylength = size;
}
/* copy over the data */
memcpy(line, filedata->scan, copylength);
line[copylength] = '\0'; /* make into ASCIIZ string */
/* we don't want the CR character in the result string*/
filedata->data = 0; /* no data in buffer */
filedata->scan += filedata->data; /* set scan point to end */
if (filedata->residual) /* more to read */
{
ReadNextBuffer(filedata); /* read the next buffer */
return GetLine(line + copylength, 0, filedata);
}
else
return 0;
}
}
}
/********************************************************************
* Function: mystrstr(haystack, needle, hlen, nlen, sensitive) *
* *
* Purpose: Determines if the string 'needle' is in the *
* string 'haystack' by returning it's position or *
* a NULL if not found. The length of haystack and *
* needle are given by 'hlen' and 'nlen' respectively. *
* *
* If 'sensitive' is true, then the search is case *
* sensitive, else it is case insensitive. *
* *
* RC: num - The pos where needle was found. *
* NULL - needle not found. *
* *
* Used By: SysFileSearch() *
*********************************************************************/
const char *mystrstr(
const char *haystack,
const char *needle,
size_t hlen,
size_t nlen,
bool sensitive)
{
char line[MAX_LINE_LEN];
char target[MAX_LINE_LEN];
size_t p;
/* Copy line - Change nulls to spaces and uppercase if needed */
for (p = 0; p < hlen; p++) {
if (haystack[p] == '\0')
line[p] = ' ';
else if (sensitive)
line[p] = haystack[p];
else line[p] = toupper(haystack[p]);
}
line[p] = '\0';
/* Copy target - Change nulls to spaces and uppercase if needed */
for (p = 0; p < nlen; p++) {
if (needle[p] == '\0')
target[p] = ' ';
else if (sensitive)
target[p] = needle[p];
else target[p] = toupper(needle[p]);
}
target[p] = '\0';
const char *hit = strstr(line, target);
if (hit == NULL)
{
return NULL;
}
// adjust original pointer to offset
return haystack + (hit - line);
}
/*************************************************************************
* Function: get_next_path *
* *
* Purpose: Read one path out of an environment value pointed to by *
* ppenv. *
* Used by the 'SearchPath' func. *
* Return: 0 - read successful *
* 1 - end of environment entry *
*************************************************************************/
int get_next_path(
char * *ppenv, /* environment pointer */
char * path_buf) /* path buffer */
{
int i; /* counter */
if(*ppenv == NULL) /* environment entry available ? */
return (1); /* return end of envrionment */
if(**ppenv == ':') /* if we point to the seperator */
(*ppenv)++; /* jump over */
if(**ppenv == '\0') /* if end of environment entry */
return (1); /* return so */
/* copy the path out of the */
/* environment entry */
for(i=0;(**ppenv != ':')&&(**ppenv != '\0');(*ppenv)++){
if(i>MAX_LINE_LEN) /* if buffer to short */
return (1); /* get out */
path_buf[i++] = **ppenv; /* copy this character */
}
path_buf[i] = '\0'; /* terminate the string */
return (0); /* found another path */
}
/*************************************************************************
* Function: SearchPath *
* *
* Purpose: Search a file along the given environment entry and return *
* the full filespec if found. *
* *
* Return: 0 - found the file(=>buf is modified) *
* 1 - not found *
*************************************************************************/
int SearchPath(
int SearchFlag, /* search curr dir first ? */
const char * path, /* environment variable name */
const char * filename, /* searched file */
char * buf, /* returned filespec if found */
size_t buf_size) /* size of the return buffer */
{
int rc = 1; /* init rc to not found */
DIR *dp; /* directory pointer */
struct dirent *ep; /* directory entry pointer */
int length; /* path length */
char path_buf[IBUF_LEN]; /* current search path */
char * penv; /* ptr to the environment */
if(!SearchFlag){ /* search curr dir first ? */
dp = opendir("./"); /* open current directory */
if(dp != NULL){ /* worked well ? */
while((ep = readdir(dp))){ /* while entries in the dir */
/* if we have a match */
if(!strcmp(ep->d_name,filename)){
if(!getcwd(buf,buf_size)) /* copy the cwd to return buf */
return rc; /* failed, get out */
length = strlen(buf); /* get the length of the path */
if((length+2+strlen(filename))>buf_size)/* check buf space */
return rc; /* not enough, get out */
buf[length] = '/'; /* add a slash */
buf[length+1] = '\0'; /* and update the terminater */
strcat(buf,filename); /* now complete the filespec */
rc = 0; /* Yep,found ! */
}
}
(void)closedir(dp); /* close the directory */
}
}
if(!rc) /* if we already found it */
return rc; /* return to caller */
/* Now search along the environment entry */
penv = getenv(path); /* get the environment entry */
if(!penv) /* if couldn't get the env */
return rc; /* get out */
/* while we have another path */
/* to search for the file */
while(!get_next_path(&penv,path_buf)){
dp = opendir(path_buf); /* open the directory */
if(dp != NULL){ /* worked well ? */
while((ep = readdir(dp))){ /* while entries in the dir */
/* if we have a match */
if(!strcmp(ep->d_name,filename)){
if(strlen(path_buf)>buf_size)/* check the size */
return rc; /* get out */
strcpy(buf,path_buf); /* copy path to the return buf*/
length = strlen(buf); /* get the length of the path */
if((length+2+strlen(filename))>buf_size)/* check buf space */
return rc; /* not enough, get out */
buf[length] = '/'; /* add a slash */
buf[length+1] = '\0'; /* and update the terminater */
strcat(buf,filename); /* now complete the filespec */
(void)closedir(dp); /* close the directory */
return 0; /* Yep,found ! */
}
}
(void)closedir(dp); /* close the directory */
}
}
return rc; /* return not found */
}
/*****************************************************************
* Function: getpath(string, path, filename) *
* *
* Purpose: This function gets the PATH and FILENAME of the file *
* target contained in STRING. The path will end with *
* the '/' char if a path is supplied. *
* *
*****************************************************************/
void getpath(
char *string,
char *path,
char *filename )
{
int iLen; /* length of filespec */
int LastSlashPos; /* position of last slash */
char szSavePath[IBUF_LEN]; /* Keep path to get back to */
if (!strcmp(string, ".")) /* period case? */
strcpy(string, "./*"); /* make it a * request */
else if (!strcmp(string, "..")) /* double period case? */
strcpy(string, "../*"); /* make it a ../ * request */
iLen = strlen(string); /* Get length of full file */
/* spec */
LastSlashPos = iLen-1; /* Get max pos of last '/' */
/* Step back through string until it begins or at last '/' char */
do
LastSlashPos--;
while((string[LastSlashPos] != '/') && (LastSlashPos>=0));
if (string[LastSlashPos] == '/'){ /* if we have a slash */
/* Get file name from filespec (just after last '/') */
if (string[LastSlashPos+1]) /* have a real name? */
/* copy it over */
strcpy(filename, &string[LastSlashPos+1]);
else
strcpy(filename, "*"); /* just use wildcards */
}
else { /* no '/' just filename */
strcpy(filename, &string[LastSlashPos+1]);
}
/* Now resolve to fully qualified path ---------------------------- */
iLen = strlen(filename); /* Get file name length */
if (string[LastSlashPos] != '/') /* if we have no slash */
{ /* resolve current dir */
if (!getcwd(path, (IBUF_LEN - iLen - 2)))
strcpy(path, "./"); /* if no cwd set current dir */
else
strcat(path, "/");
}
else { /* there is path info */
strncpy(path, string, LastSlashPos+1);/* copy the path out */
*(path+LastSlashPos+1) = '\0'; /* make ASCII-Z string */
if (getcwd(szSavePath, (IBUF_LEN - 1 )))
{
if (!chdir(path)) /* If successful get */
{ /* reolved path name */
if ((getcwd(path, (IBUF_LEN - iLen - 2 ))) &&
( LastSlashPos > 0 ))
strcat(path, "/"); /* Add if not root dir */
chdir(szSavePath); /* Back to current dir */
}
}
}
}
/*****************************************************************************
* Function: LinFindNextFile(path, dir_handle, finfo, d_name, caseless) *
* *
* Purpose: This function finds the next file in the directory PATH pointed *
* by DIR_HANDLE which matchs the filespec. All needed info is *
* returned via the FINFO struct and the D_NAME pointer. *
* *
* Note: '?' is currently not supported. Add the impletmentation here ! *
******************************************************************************/
int LinFindNextFile(
const char * filespec, /* filespec to search for */
const char * path, /* current path */
DIR *dir_handle, /* directory handle */
struct stat *finfo, /* return buf for the finfo */
char * *d_name, /* name of the file found */
size_t caseless) /* case insensitive matching */
{
struct dirent *dir_entry; /* Directory entry */
char full_path[IBUF_LEN+1];
/* strtok routine */
if(!(dir_entry = readdir(dir_handle)))/* get first entry */
return 0; /* no entry or EOF */
do{
/* make full spec */
sprintf(full_path, "%s%s", path, dir_entry->d_name);
lstat(full_path, finfo); /* read the info about it */
if(S_ISREG(finfo->st_mode) || /* if it is a file */
S_ISCHR(finfo->st_mode) || /* or a device special */
S_ISBLK(finfo->st_mode) || /* file */
S_ISSOCK(finfo->st_mode) || /* or a socket */
S_ISLNK(finfo->st_mode) || /* or a symbolic link */
S_ISFIFO(finfo->st_mode)){ /* or a FIFO */
if (caseless) { /* if caseless search */
char dup_d_name[IBUF_LEN+1]; /* compare upper cased copy */
char * pDest = dup_d_name; /* of the entry name */
char * pSrc = dir_entry->d_name;
for ( ; *pSrc; pDest++, pSrc++ )
*pDest = toupper(*pSrc);
*pDest = '\x0';
if(fnmatch(filespec,dup_d_name,FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)==0){
*d_name = dir_entry->d_name; /* retptr to the name location*/
return 1; /* return success */
}
}
else { /* else compare normally */
if(fnmatch(filespec,dir_entry->d_name,FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)==0){
*d_name = dir_entry->d_name; /* retptr to the name location*/
return 1; /* return success */
}
}
}
}
while((dir_entry = readdir(dir_handle)));/* while have entries */
return 0; /* no file found or EOF */
}
/*****************************************************************
* Function: LinFindNextDir(path, dir_handle, finfo, d_name) *
* *
* Purpose: This function finds the next dir in the directory *
* PATH pointed by DIR_HANDLE. All needed info is *
* returned via the FINFO struct and the D_NAME pointer.*
* *
* Note: '?' is currently not supported. Add the impletmentation *
* here ! *
*****************************************************************/
int LinFindNextDir(
const char * filespec, /* filespec to search for */
const char * path, /* current path */
DIR *dir_handle, /* directory handle */
struct stat *finfo, /* return buf for the finfo */
char * *d_name, /* name of the file found */
size_t caseless) /* case insensitive matching */
{
struct dirent *dir_entry; /* Directory entry */
char full_path[IBUF_LEN+1];
/* strtok routine */
if(!(dir_entry = readdir(dir_handle)))/* get first entry */
return 0; /* no entry or EOF */
do{
/* make full spec */
sprintf(full_path, "%s%s", path, dir_entry->d_name);
lstat(full_path, finfo); /* read the info about it */
if(S_ISDIR(finfo->st_mode)){ /* if it is a directory */
if (caseless) { /* if caseless search */
char dup_d_name[IBUF_LEN+1]; /* compare upper cased copy */
char * pDest = dup_d_name; /* of the entry name */
char * pSrc = dir_entry->d_name;
for ( ; *pSrc; pDest++, pSrc++ )
*pDest = toupper(*pSrc);
*pDest = '\x0';
if(fnmatch(filespec,dup_d_name,FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)==0){
*d_name = dir_entry->d_name; /* retptr to the name location*/
return 1; /* return success */
}
}
else { /* else compare normally */
if(fnmatch(filespec,dir_entry->d_name,FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD)==0){
*d_name = dir_entry->d_name; /* retptr to the name location*/
return 1; /* return success */
}
}
}
}
while((dir_entry = readdir(dir_handle)));/* while have entries */
return 0; /* no file found or EOF */
}
/*********************************************************************/
/* Function: size_t FormatFile( */
/* */
/* Purpose: Returns the new file attribute, given the mask of */
/* attributes to be cleared/set and the current attribute */
/* settings. */
/* */
/*********************************************************************/
size_t FormatFile(
RXTREEDATA *ldp, /* Pointer to local data */
int *smask, /* Mask of attributes to */
/* search for */
int *dmask, /* Mask of attributes to set */
size_t options, /* Search and output format */
struct stat *finfo ) /* File info sturcture */
{
struct tm *timestamp; /* Time info about the file */
char tp; /* type of the entry */
if (options&NAME_ONLY) /* name only? */
strcpy(ldp->Temp, ldp->truefile); /* just copy it over */
else {
#ifdef AIX
struct tm stTimestamp; /* Time info about the file */
timestamp = localtime_r(&(finfo->st_mtime),&stTimestamp); /* get the time info */
#else
timestamp = localtime(&(finfo->st_mtime));/* get the time info */
#endif
/* add long time format */
if (options&LONG_TIME) /* need the long time format? */
/* format as such */
sprintf(ldp->Temp, "%4d-%02d-%02d %02d:%02d:%02d %10lu ",
timestamp->tm_year+1900,
timestamp->tm_mon+1,
timestamp->tm_mday,
timestamp->tm_hour,
timestamp->tm_min,
timestamp->tm_sec,
finfo->st_size);
else
{
if (options&EDITABLE_TIME) /* need the "smushed" form? */
/* format as such */
sprintf(ldp->Temp, "%02d/%02d/%02d/%02d/%02d %10lu ",
(timestamp->tm_year)%100,
timestamp->tm_mon+1,
timestamp->tm_mday,
timestamp->tm_hour,
timestamp->tm_min,
finfo->st_size);
else /* give the pretty form */
sprintf(ldp->Temp, "%2d/%02d/%02d %2d:%02d%c %10lu ",
timestamp->tm_mon+1,
timestamp->tm_mday,
(timestamp->tm_year)%100,
(timestamp->tm_hour < 13?
timestamp->tm_hour:
(timestamp->tm_hour-12)),
timestamp->tm_min,
((timestamp->tm_hour < 12 ||
timestamp->tm_hour == 24)?'a':'p'),
finfo->st_size);
}
/* find the type of the entry */
if(S_ISLNK(finfo->st_mode))
tp = 'l'; /* symbolic link */
else if(S_ISBLK(finfo->st_mode))
tp = 'b'; /* block device */
else if(S_ISCHR(finfo->st_mode))
tp = 'c'; /* character device */
else if(S_ISDIR(finfo->st_mode))
tp = 'd'; /* directory */
else if(S_ISFIFO(finfo->st_mode))
tp = 'p'; /* FIFO */
else if(S_ISSOCK(finfo->st_mode))
tp = 's'; /* socket */
else
tp = '-'; /* regular file */
/* format the attributes now */
sprintf(ldp->Temp, "%s%c%c%c%c%c%c%c%c%c%c %s", ldp->Temp,
tp,
(((finfo->st_mode)&S_IREAD)?'r':'-'),
(((finfo->st_mode)&S_IWRITE)?'w':'-'),
(((finfo->st_mode)&S_IEXEC)?'x':'-'),
(((finfo->st_mode)&S_IRGRP)?'r':'-'),
(((finfo->st_mode)&S_IWGRP)?'w':'-'),
(((finfo->st_mode)&S_IXGRP)?'x':'-'),
(((finfo->st_mode)&S_IROTH)?'r':'-'),
(((finfo->st_mode)&S_IWOTH)?'w':'-'),
(((finfo->st_mode)&S_IXOTH)?'x':'-'),
ldp->truefile);
}
/* Place new string in Stem */
ldp->vlen = strlen(ldp->Temp);
ldp->count++;
sprintf(ldp->varname+ldp->stemlen, "%d", ldp->count);
ldp->shvb.shvnext = NULL;
ldp->shvb.shvname.strptr = ldp->varname;
ldp->shvb.shvname.strlength = strlen(ldp->varname);
ldp->shvb.shvvalue.strptr = ldp->Temp;
ldp->shvb.shvvalue.strlength = ldp->vlen;
ldp->shvb.shvnamelen = ldp->shvb.shvname.strlength;
ldp->shvb.shvvaluelen = ldp->vlen;
ldp->shvb.shvcode = RXSHV_SET;
ldp->shvb.shvret = 0;
int rc = RexxVariablePool(&ldp->shvb);
if (rc & (RXSHV_BADN | RXSHV_MEMFL))
{
return INVALID_ROUTINE;
}
return 0; /* good return */
}
/*****************************************************************************
* Function: RecursiveFindFile( FileSpec, path, lpd, smask, dmask, options ) *
* *
* Purpose: Finds all files starting with FileSpec, and will look down the *
* directory tree if required. *
* *
* Params: FileSpec - ASCIIZ string which designates filespec to search *
* for. *
* path - ASCIIZ string for current path *
* *
* ldp - Pointer to local data structure. *
* *
* smask - Array of integers which describe the source attribute *
* mask. Only files with attributes matching this mask *
* will be found. *
* *
* dmask - Array of integers which describe the target attribute *
* mask. Attributes of all found files will be set *
* using this mask. *
* *
* Note: Both source and targets mask are really arrays of *
* integers. Each index of the mask corresponds *
* to a different file attribute. Each indexe and *
* its associated attribute follows: *
* *
* mask[0] = FILE_ARCHIVED *
* mask[1] = FILE_DIRECTORY *
* mask[2] = FILE_HIDDEN *
* mask[3] = FILE_READONLY *
* mask[4] = FILE_SYSTEM *
* *
* A negative value at a given index indicates that *
* the attribute bit of the file is not set. A positive *
* number indicates that the attribute should be set. *
* A value of 0 indicates a "Don't Care" setting. *
* *
* options - The search/output options. The following options *
* may be ORed together when calling this function: *
* *
* RECURSE - Indicates that function should search *
* all child subdirectories recursively. *
* DO_DIRS - Indicates that directories should be *
* included in the search. *
* DO_FILES - Indicates that files should be included *
* in the search. *
* NAME_ONLY - Indicates that the output should be *
* restricted to filespecs only. *
* EDITABLE_TIME - Indicates time and date fields should *
* be output as one timestamp. *
* LONG_TIME - Indicates time and date fields should *
* be output as one long formatted timestamp*
* CASELESS - Indicates do a case insensitive check for *
* file names. *
* *
*****************************************************************************/
int RecursiveFindFile(
const char *FileSpec, /* Filespecs to search for */
const char *path, /* current directory */
RXTREEDATA *ldp, /* Pointer to local data */
int *smask, /* Mask of attributes to */
/* search for */
int *dmask, /* Mask of attributes to set */
size_t options ) /* Search and output format */
/* options */
{
char tempfile[MAX+1]; /* Used to hold temp file name*/
DIR *dir_handle; /* Directory handle */
struct stat finfo; /* file information */
char * filename;
size_t caseless = options&CASELESS;
/********************************************************************
* First, process all of the normal files, saving directories for *
* last. *
********************************************************************/
if(!(dir_handle = opendir(path))){ /* get the directory handle */
return VALID_ROUTINE; /* return on non-zero */
}
if (options&DO_FILES && /* if processing files */
/* and have some */
(LinFindNextFile(ldp->TargetSpec,path ,dir_handle,&finfo, &filename, caseless))) {
/* Get the rest of the files */
do {
/* build the full name */
sprintf(ldp->truefile, "%s%s", path, filename);
/* passed back ok? */
if (FormatFile(ldp, smask, dmask, options, &finfo)) {
closedir(dir_handle); /* close the search */
return INVALID_ROUTINE; /* error on non-zero */
}
} while (LinFindNextFile(ldp->TargetSpec,path, dir_handle, &finfo, &filename, caseless));
}
closedir(dir_handle); /* reset the */
dir_handle = opendir(path); /* directory handle (rewinddir*/
/* doesn't work!) */
if (options&DO_DIRS && /* need directories? */
/* and have some */
(LinFindNextDir(ldp->TargetSpec,path,dir_handle,&finfo,&filename, caseless))) {
do {
/* dot directory? */
if (!strcmp(filename, ".") || !strcmp(filename, ".."))
continue; /* skip this one */
/* build the full name */
sprintf(ldp->truefile, "%s%s", path, filename);
/* passed back ok? */
if (FormatFile(ldp, smask, dmask, options, &finfo)) {
closedir(dir_handle); /* close the search */
return INVALID_ROUTINE; /* error on non-zero */
}
} while (LinFindNextDir(ldp->TargetSpec,path, dir_handle, &finfo, &filename, caseless));
}
closedir(dir_handle); /* reset the directory handle */
dir_handle = opendir(path); /* (rewinddir doesn't work!) */
/* need to recurse? and have */
if (options&RECURSE) { /* some? no need for caseless */
/* matching star */
if (LinFindNextDir("*",path,dir_handle,&finfo,&filename, 0)) {
do {
/* dot directory? */
if (!strcmp(filename, ".") ||
!strcmp(filename, ".."))
continue; /* skip this one */
/* build the new directory */
sprintf(tempfile, "%s%s/", path, filename);
/* search the next level */
if (RecursiveFindFile(ldp->TargetSpec, tempfile, ldp,
smask, dmask, options)) {
closedir(dir_handle); /* close the search */
return INVALID_ROUTINE; /* error on non-zero */
}
} while (LinFindNextDir("*",path,dir_handle,&finfo,&filename, 0));
}
}
closedir(dir_handle);
return VALID_ROUTINE; /* finished */
}
/********************************************************************
* Function: string2long(string, number) *
* *
* Purpose: Validates and converts an ASCII-Z string from string *
* form to an unsigned long. Returns false if the number *
* is not valid, true if the number was successfully *
* converted. *
* *
* RC: true - Good number converted *
* false - Invalid number supplied. *
*********************************************************************/
bool string2int(
const char * string,
int *number)
{
int accumulator; /* converted number */
int length; /* length of number */
int sign; /* sign of number */
sign = 1; /* set default sign */
if (*string == '-') { /* negative? */
sign = -1; /* change sign */
string++; /* step past sign */
}
length = strlen(string); /* get length of string */
if (length == 0 || /* if null string */
length > MAX_DIGITS) /* or too long */
return false; /* not valid */
accumulator = 0; /* start with zero */
while (length) { /* while more digits */
if (!isdigit(*string)) /* not a digit? */
return false; /* tell caller */
/* add to accumulator */
accumulator = accumulator * 10 + (*string - '0');
length--; /* reduce length */
string++; /* step pointer */
}
*number = accumulator * sign; /* return the value */
return true; /* good number */
}
void restore_terminal(int signal){
stty(STDIN_FILENO,&in_orig); /* restore the terminal settings */
raise(signal); /* propagate signal */
}
/******************************************************************************/
/* getkey */
/******************************************************************************/
/* echo == false => no echo */
/* echo == true => echo */
/******************************************************************************/
int getkey(char *ret, bool echo){
/* restore original TTY settings on exit */
int ttyfd = STDIN_FILENO; /* standard tty filedescriptor */
/* Set the cleanup handler for unconditional process termination */
struct sigaction new_action;
/* Set up the structure to specify the new action */
new_action.sa_handler = restore_terminal;
sigfillset(&new_action.sa_mask);
new_action.sa_flags = SA_RESTART;
/* Termination signals */
sigaction(SIGINT, &new_action, NULL); /* exitClear on SIGINT signal */
sigaction(SIGTERM, &new_action, NULL); /* exitClear on SIGTERM signal */
sigaction(SIGQUIT, &new_action, NULL); /* exitClear on SIGQUIT signal */
sigaction(SIGHUP, &new_action, NULL); /* exitClear on SIGHUP signal */
sigaction(SIGTSTP, &new_action, NULL); /* exitClear on SIGTSTP signal */
sigaction(SIGTTIN, &new_action, NULL); /* exitClear on SIGTTIN signal */
sigaction(SIGTTOU, &new_action, NULL); /* exitClear on SIGTTOU signal */
/* Error signals */
sigaction(SIGSEGV, &new_action, NULL); /* exitClear on SIGSEGV signal */
sigaction(SIGFPE, &new_action, NULL); /* exitClear on SIGFPE signal */
sigaction(SIGILL, &new_action, NULL); /* exitClear on SIGILL signal */
sigaction(SIGBUS, &new_action, NULL); /* exitClear on SIGBUS signal */
sigaction(SIGPIPE, &new_action, NULL); /* exitClear on broken pipe */
if( !isatty(ttyfd) ){ /* connected to a terminal ? */
ret[0] = '\0';
return 0;
}
ttyfd = STDIN_FILENO; /* STDIN_FILENO is out default fd */
/* open tty */
ttyfd = open("/dev/tty", O_RDONLY); /* get filedescriptor (fd) for tty */
struct termios in_raw; /* global for save reset after SIGNAL */
gtty(ttyfd, &in_orig); /* determine existing tty settings */
/* restore original TTY settings on exit */
/* change STDIN settings to raw */
gtty(ttyfd, &in_raw); /* save old settings */
in_raw.c_lflag &= ~ICANON; /* no canonical mode */
if (!echo) /* no echo flag set */
in_raw.c_lflag &= ~ECHO; /* no echo */
in_raw.c_cc[VMIN] = 1; /* read 1 byte before returning */
in_raw.c_cc[VTIME] = 0; /* return immediatly (no timeout) */
stty(ttyfd, &in_raw); /* execute settings now! */
ret[0] = getchar(); /* read the char */
ret[1] = '\0'; /* terminate string */
restore_tty(&in_orig); /* for standard I/O behavior */
close(ttyfd); /* close the terminal */
return 0; /* everything is fine */
}
/*************************************************************************
* Function: SysSleep *
* *
* Syntax: call SysSleep secs *
* *
* Params: secs - Number of seconds to sleep. *
* *
* Return: NO_UTIL_ERROR *
*************************************************************************/
size_t RexxEntry SysSleep(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int secs; /* Time to sleep in secs */
size_t length; /* length of the count */
const char * string; /* input sleep time */
int nanoseconds = 0; /* decimals value */
int digits; /* number of decimal digits */
#if defined( HAVE_NANOSLEEP )
struct timespec Rqtp, Rmtp;
#elif defined( HAVE_NSLEEP )
struct timestruc_t Rqtp, Rmtp;
#endif
int nano;
if (numargs != 1) /* Must have one argument */
return INVALID_ROUTINE;
string = args[0].strptr; /* point to the string */
length = args[0].strlength; /* get length of string */
if (length == 0 || /* if null string */
length > MAX_DIGITS) /* or too long */
return INVALID_ROUTINE; /* not valid */
secs = 0; /* start with zero */
while (length) { /* while more digits */
if (!isdigit(*string)) /* not a digit? */
break; /* get out of this loop */
secs = secs * 10 + (*string - '0');/* add to accumulator */
length--; /* reduce length */
string++; /* step pointer */
}
if (*string == '.') { /* have a decimal number? */
string++; /* step over the decimal */
length--; /* reduce the length */
nanoseconds = 0; /* no nanoseconds yet */
digits = 0; /* and no digits */
nano = 9;
while(nano)
{
while (length)
{ /* while more digits */
if (!isdigit(*string)) /* not a digit? */
return INVALID_ROUTINE; /* not a valid number */
/* add to accumulator */
nanoseconds = nanoseconds * 10 + (*string - '0');
length--; /* reduce length */
string++; /* step pointer */
nano--;
}
while(nano)
{
nanoseconds = nanoseconds * 10;
nano--;
}
}
}
else if (length != 0) /* invalid character found? */
return INVALID_ROUTINE; /* this is invalid */
#if defined( HAVE_NANOSLEEP )
Rqtp.tv_sec = secs;
Rqtp.tv_nsec = nanoseconds;
nanosleep(&Rqtp, &Rmtp);
#elif defined( HAVE_NSLEEP )
Rqtp.tv_sec = secs;
Rqtp.tv_nsec = nanoseconds;
nsleep(&Rqtp, &Rmtp);
#else
sleep( secs );
#endif
BUILDRXSTRING(retstr, NO_UTIL_ERROR);/* return no error */
return VALID_ROUTINE; /* this worked ok */
}
/*************************************************************************
* Function: SysLoadFuncs *
* *
* Syntax: call SysLoadFuncs [option] *
* *
* Params: none *
* *
* Return: null string *
*************************************************************************/
size_t RexxEntry SysLoadFuncs(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
// this is a NOP now
retstr->strlength = 0; /* set return value */
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysDropFuncs *
* *
* Syntax: call SysDropFuncs *
* *
* Return: NO_UTIL_ERROR - Successful. *
*************************************************************************/
size_t RexxEntry SysDropFuncs(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
// this is a NOP now
retstr->strlength = 0; /* set return value */
return VALID_ROUTINE;
}
/**********************************************************************
* Function: SysCls *
* *
* Syntax: call SysCls *
* *
* Return: NO_UTIL_ERROR - Successful. *
**********************************************************************/
size_t RexxEntry SysCls(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
if (numargs) /* arguments specified? */
return INVALID_ROUTINE; /* raise the error */
system("clear"); /* do the clear */ // think about the use of 'execve', Weigold
BUILDRXSTRING(retstr, NO_UTIL_ERROR);/* pass back result */
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysAddRexxMacro *
* *
* Syntax: result = SysAddRexxMacro(name, file, <order>) *
* *
* Params: name - loaded name of the macro file *
* file - file containing the loaded macro *
* order - Either 'B'efore or 'A'fter. The default is 'B' *
* *
* Return: return code from RexxAddMacro *
*************************************************************************/
size_t RexxEntry SysAddRexxMacro(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
size_t position; /* added position */
if (numargs < 2 || numargs > 3 || /* wrong number? */
!RXVALIDSTRING(args[0]) || /* first is omitted */
!RXVALIDSTRING(args[1])) /* second is omitted */
return INVALID_ROUTINE; /* raise error condition */
position = RXMACRO_SEARCH_BEFORE; /* set default search position*/
if (numargs == 3) { /* have an option? */
if (RXZEROLENSTRING(args[2])) /* null string? */
return INVALID_ROUTINE; /* this is an error */
/* 'B'efore? */
else if (toupper(args[2].strptr[0]) == 'B')
position = RXMACRO_SEARCH_BEFORE;/* place before */
/* 'A'fter? */
else if (toupper(args[2].strptr[0]) == 'A')
position = RXMACRO_SEARCH_AFTER; /* place after */
else /* parm given was bad */
return INVALID_ROUTINE; /* raise an error */
}
/* try to add the macro */
rc = RexxAddMacro(args[0].strptr, args[1].strptr, position);
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysDropRexxMacro *
* *
* Syntax: result = SysDropRexxMacro(name) *
* *
* Params: name - name of the macro space function *
* *
* Return: return code from RexxDropMacro *
*************************************************************************/
size_t RexxEntry SysDropRexxMacro(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
if (numargs != 1) /* wrong number? */
return INVALID_ROUTINE; /* raise error condition */
rc = RexxDropMacro(args[0].strptr); /* try to drop the macro */
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysClearRexxMacroSpace *
* *
* Syntax: result = SysClearRexxMacroSpace() *
* *
* Params: none *
* *
* Return: return code from RexxClearMacroSpace() *
*************************************************************************/
size_t RexxEntry SysClearRexxMacroSpace(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
if (numargs) /* wrong number? */
return INVALID_ROUTINE; /* raise error condition */
rc = RexxClearMacroSpace(); /* clear the macro space */
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysSaveRexxMacroSpace *
* *
* Syntax: result = SysSaveRexxMacroSpace(file) *
* *
* Params: file - name of the saved macro space file *
* *
* Return: return code from RexxSaveMacroSpace() *
*************************************************************************/
size_t RexxEntry SysSaveRexxMacroSpace(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
if (numargs != 1) /* wrong number? */
return INVALID_ROUTINE; /* raise error condition */
/* clear the macro space */
rc = RexxSaveMacroSpace(0, NULL, args[0].strptr);
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysLoadRexxMacroSpace *
* *
* Syntax: result = SysLoadRexxMacroSpace(file) *
* *
* Params: file - name of the saved macro space file *
* *
* Return: return code from RexxLoadMacroSpace() *
*************************************************************************/
size_t RexxEntry SysLoadRexxMacroSpace(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
if (numargs != 1) /* wrong number? */
return INVALID_ROUTINE; /* raise error condition */
/* clear the macro space */
rc = RexxLoadMacroSpace(0, NULL, args[0].strptr);
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysQueryRexxMacro *
* *
* Syntax: result = SysQueryRexxMacro(name) *
* *
* Params: name - name of the macro space function *
* *
* Return: position of the macro ('B' or 'A'), returns null for errors.*
*************************************************************************/
size_t RexxEntry SysQueryRexxMacro(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
unsigned short position; /* returned position */
if (numargs != 1) /* wrong number? */
return INVALID_ROUTINE; /* raise error condition */
/* query the macro position */
if (RexxQueryMacro(args[0].strptr, &position))
retstr->strlength = 0; /* return a null string */
else {
/* before? */
if (position == RXMACRO_SEARCH_BEFORE)
retstr->strptr[0] = 'B'; /* return a 'B' */
else
retstr->strptr[0] = 'A'; /* must be 'A'fter */
retstr->strlength = 1; /* returning one character */
}
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysReorderRexxMacro *
* *
* Syntax: result = SysReorderRexxMacro(name, order) *
* *
* Params: name - loaded name of the macro file *
* order - Either 'B'efore or 'A'fter. *
* *
* Return: return code from RexxReorderMacro *
*************************************************************************/
size_t RexxEntry SysReorderRexxMacro(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* creation return code */
size_t position; /* added position */
if (numargs != 2 || /* wrong number? */
!RXVALIDSTRING(args[0]) || /* first is omitted */
RXZEROLENSTRING(args[1])) /* null string? */
return INVALID_ROUTINE; /* raise error condition */
/* 'B'efore? */
if (toupper(args[1].strptr[0]) == 'B')
position = RXMACRO_SEARCH_BEFORE; /* place before */
/* 'A'fter? */
else if (toupper(args[1].strptr[0]) == 'A')
position = RXMACRO_SEARCH_AFTER; /* place after */
else /* parm given was bad */
return INVALID_ROUTINE; /* raise an error */
/* try to add the macro */
rc = RexxReorderMacro(args[0].strptr, position);
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysMkDir *
* *
* Syntax: call SysMkDir dir *
* *
* Params: dir - Directory to be created. *
* *
* Return: NO_UTIL_ERROR *
* *
*************************************************************************/
size_t RexxEntry SysMkDir(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
size_t rc; /* Ret code of func */
const char * path; /* given path */
char * dir_buf = NULL; /* full directory path */
if (numargs != 1)
/* If no args, then its an */
/* incorrect call */
return INVALID_ROUTINE;
path = args[0].strptr; /* directory to make */
/* check for using '~/' */
if(*(path) == '~'){
dir_buf = resolve_tilde(path); /* get absolut path */
path = dir_buf; /* directory to make */
}
/* Make the dir; standard permissions are rwxr-xr-x */
/* we do not restrict permission, this is done by root in the file */
/* /etc/security/user. We allow anything. System restricts */
/* according to the user settings --> smitty/user */
//rc = mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
rc = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
if(!rc){ /* if worked well */
sprintf(retstr->strptr, "%d", rc); /* result is return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
else{ /* if there was an error */
switch (errno) {
case EACCES: {
sprintf(retstr->strptr, "%d", 5); /* result: Access denied */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EEXIST: {
sprintf(retstr->strptr, "%d", 87);/*result: already exists */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EMLINK: {
sprintf(retstr->strptr, "%d", 206);/* result: exceeds range */
retstr->strlength = strlen(retstr->strptr);
break;
}
case ENOSPC: {
sprintf(retstr->strptr, "%d", 206);/* result: exceeds range */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EROFS: {
sprintf(retstr->strptr, "%d", 108);/* result:read only system*/
retstr->strlength = strlen(retstr->strptr);
break;
}
default:
sprintf(retstr->strptr, "%d", 2); /* result: file not found */
retstr->strlength = strlen(retstr->strptr);
break;
}
}
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free the buffer memory */
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysRmDir *
* *
* Syntax: call SysRmDir dir *
* *
* Params: dir - Directory to be removed. *
* *
* Return: NO_UTIL_ERROR *
* *
*************************************************************************/
size_t RexxEntry SysRmDir(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
size_t rc; /* Ret code of func */
const char * path; /* given path */
char * dir_buf = NULL; /* full directory path */
if (numargs != 1)
/* If no args, then its an */
/* incorrect call */
return INVALID_ROUTINE;
path = args[0].strptr; /* directory to remove */
/* check for using '~/' */
if(*(path) == '~'){
dir_buf = resolve_tilde(path); /* get absolut path */
path = dir_buf; /* directory to remove */
}
rc = rmdir(path); /* Remove the directory */
if(!rc){ /* if worked well */
sprintf(retstr->strptr, "%d", rc); /* result is return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
else{ /* if there was an error */
switch (errno) {
case EACCES: {
sprintf(retstr->strptr, "%d", 5); /* result: Access denied */
retstr->strlength = strlen(retstr->strptr);
break;
}
#if defined( ENOTEMPTY ) && defined( EEXIST )
case EEXIST: {
sprintf(retstr->strptr, "%d", 87);/*result: already exists */
retstr->strlength = strlen(retstr->strptr);
break;
}
#elif defined( ENOTEMPTY )
case ENOTEMPTY: { /* sometimes used for EEXITST */
sprintf(retstr->strptr, "%d", 87);/*result: already exists */
retstr->strlength = strlen(retstr->strptr);
break;
}
#elif defined( EEXIST )
case EEXIST: {
sprintf(retstr->strptr, "%d", 87);/*result: already exists */
retstr->strlength = strlen(retstr->strptr);
break;
}
#endif
case EBUSY: {
sprintf(retstr->strptr, "%d", 5);/* result: currently in use */
retstr->strlength = strlen(retstr->strptr);
break;
}
case ENOENT: {
sprintf(retstr->strptr, "%d", 87);/* result: doesn't exitst */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EROFS: {
sprintf(retstr->strptr, "%d", 108);/* result:read only system*/
retstr->strlength = strlen(retstr->strptr);
break;
}
default:
sprintf(retstr->strptr, "%d", 2); /* result: dir not found */
retstr->strlength = strlen(retstr->strptr);
break;
}
}
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free the buffer memory */
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysFileDelete *
* *
* Syntax: call SysFileDelete file *
* *
* Params: file - file to be deleted. *
* *
* Return: Return code from DosDelete() function. *
*************************************************************************/
size_t RexxEntry SysFileDelete(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
size_t rc; /* Ret code of func */
const char * path; /* given path */
char * dir_buf = NULL; /* full directory path */
if (numargs != 1)
/* If no args, then its an */
/* incorrect call */
return INVALID_ROUTINE;
path = args[0].strptr; /* file to remove */
/* check for using '~/' */
if(*(path) == '~'){
dir_buf = resolve_tilde(path); /* get absolut path */
path = dir_buf; /* file to remove */
}
rc = remove(path); /* Remove the file */
if(!rc){ /* if worked well */
sprintf(retstr->strptr, "%d", rc); /* result is return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
else{ /* if there was an error */
switch (errno) {
case EACCES: {
sprintf(retstr->strptr, "%d", 5); /* result: Access denied */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EBUSY: {
sprintf(retstr->strptr, "%d", 5);/* result: currently in use */
retstr->strlength = strlen(retstr->strptr);
break;
}
case ENOENT: {
sprintf(retstr->strptr, "%d", 87);/* result: doesn't exist */
retstr->strlength = strlen(retstr->strptr);
break;
}
case EROFS: {
sprintf(retstr->strptr, "%d", 108);/* result:read only system*/
retstr->strlength = strlen(retstr->strptr);
break;
}
default:
sprintf(retstr->strptr, "%d", 2); /* result: file not found */
retstr->strlength = strlen(retstr->strptr);
break;
}
}
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free the buffer memory */
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysFileSearch *
* *
* Syntax: call SysFileSearch target, file, stem [, options] *
* *
* Params: target - String to search for. *
* file - Filespec to search. *
* stem - Stem variable name to place results in. *
* options - Any combo of the following: *
* 'C' - Case sensitive search (non-default). *
* 'N' - Preceed each found string in result stem *
* with it line number in file (non-default).*
* *
* Return: NO_UTIL_ERROR - Successful. *
* ERROR_NOMEM - Out of memory. *
*************************************************************************/
size_t RexxEntry SysFileSearch(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
const char *target; /* search string */
const char *file; /* search file */
const char *opts; /* option string */
const char *ptr; /* Pointer to char str found */
size_t num = 0; /* Line number */
size_t len; /* Length of string */
size_t len2; /* Length of string */
bool linenums = false; /* Set true for linenums in */
/* output */
bool sensitive = false; /* Set true for case-sens */
/* search */
RXSTEMDATA ldp; /* stem data */
GetFileData filedata; /* file read information */
char * dir_buf = NULL; /* directory buffer */
char * line = NULL; /* Line read from file */
BUILDRXSTRING(retstr, NO_UTIL_ERROR);/* pass back result */
/* validate arguments */
if (numargs < 3 || numargs > 4 ||
!RXVALIDSTRING(args[0]) ||
!RXVALIDSTRING(args[1]) ||
!RXVALIDSTRING(args[2]))
return INVALID_ROUTINE; /* raise an error */
target = args[0].strptr; /* get target pointer */
file = args[1].strptr; /* get file name */
if(*(file) == '~'){ /* check for using '~/' */
dir_buf = resolve_tilde(file); /* get absolut path */
file = dir_buf; /* full path to the file */
}
if (numargs == 4) { /* process options */
opts = args[3].strptr; /* point to the options */
if (strstr(opts, "N") || strstr(opts, "n"))
linenums = true;
if (strstr(opts, "C") || strstr(opts, "c"))
sensitive = true;
}
/* Initialize data area */
ldp.count = 0;
strcpy(ldp.varname, args[2].strptr);
ldp.stemlen = args[2].strlength;
strupr(ldp.varname); /* uppercase the name */
if (ldp.varname[ldp.stemlen-1] != '.')
ldp.varname[ldp.stemlen++] = '.';
if (OpenFile(file, &filedata)) { /* open the file */
BUILDRXSTRING(retstr, ERROR_FILEOPEN);
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free it */
return VALID_ROUTINE; /* finished */
}
line = (char *) malloc(4096 * sizeof(char));
/* do the search...found lines*/
/* are saved in stem vars */
while (!GetLine(line, MAX_LINE_LEN - 1, &filedata)) {
len = strlen(line);
num++;
ptr = mystrstr(line, target, len, args[0].strlength, sensitive);
if (ptr != '\0') {
if (linenums) {
sprintf(ldp.ibuf, "%d ", num);
len2 = strlen(ldp.ibuf);
memcpy(ldp.ibuf+len2, line, len < IBUF_LEN-len2 ? len : IBUF_LEN-len2);
ldp.vlen = IBUF_LEN < len+len2 ? IBUF_LEN : len + len2;
}
else {
memcpy(ldp.ibuf, line, len);
ldp.vlen = len;
}
ldp.count++;
sprintf(ldp.varname+ldp.stemlen, "%d", ldp.count);
if (ldp.ibuf[ldp.vlen-1] == '\n')
ldp.vlen--;
ldp.shvb.shvnext = NULL;
ldp.shvb.shvname.strptr = ldp.varname;
ldp.shvb.shvname.strlength = strlen(ldp.varname);
ldp.shvb.shvnamelen = ldp.shvb.shvname.strlength;
ldp.shvb.shvvalue.strptr = ldp.ibuf;
ldp.shvb.shvvalue.strlength = ldp.vlen;
ldp.shvb.shvvaluelen = ldp.vlen;
ldp.shvb.shvcode = RXSHV_SET;
ldp.shvb.shvret = 0;
if (RexxVariablePool(&ldp.shvb) == RXSHV_BADN) {
CloseFile(&filedata); /* close the file */
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free it */
free(line);
return INVALID_ROUTINE; /* error on non-zero */
}
}
}
free(line);
CloseFile(&filedata); /* Close that file */
/* set stem.0 to lines read */
sprintf(ldp.ibuf, "%d", ldp.count);
ldp.varname[ldp.stemlen] = '0';
ldp.varname[ldp.stemlen+1] = 0;
ldp.shvb.shvnext = NULL;
ldp.shvb.shvname.strptr = ldp.varname;
ldp.shvb.shvname.strlength = ldp.stemlen+1;
ldp.shvb.shvnamelen = ldp.stemlen+1;
ldp.shvb.shvvalue.strptr = ldp.ibuf;
ldp.shvb.shvvalue.strlength = strlen(ldp.ibuf);
ldp.shvb.shvvaluelen = ldp.shvb.shvvalue.strlength;
ldp.shvb.shvcode = RXSHV_SET;
ldp.shvb.shvret = 0;
if (RexxVariablePool(&ldp.shvb) == RXSHV_BADN){
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free it */
return INVALID_ROUTINE; /* error on non-zero */
}
if(dir_buf) /* did we allocate ? */
free(dir_buf); /* free it */
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysSearchPath *
* *
* Syntax: call SysSearchPath path, file [, options] *
* *
* Params: path - Environment variable name which specifies a path *
* to be searched (ie 'PATH', 'DPATH', etc). *
* file - The file to search for. *
* options - 'C' - Current directory search first (default). *
* 'N' - No Current directory search. Only searches *
* the path as specified. *
* *
* Return: other - Full path and filespec of found file. *
* '' - Specified file not found along path. *
*************************************************************************/
size_t RexxEntry SysSearchPath(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
char buf[IBUF_LEN]={0}; /* returned file name */
const char * opts; /* option string */
int SearchFlag; /* Search control variable */
/* default=search current 1st */
int ulRc; /* return value of SearchPath */
SearchFlag = CURRENT_DIR_FIRST; /* set default search flag */
/* validate arguments */
if (numargs < 2 || numargs > 3 ||
!RXVALIDSTRING(args[0]) ||
!RXVALIDSTRING(args[1]))
return INVALID_ROUTINE;
if (numargs == 3) { /* process options */
opts = args[2].strptr; /* point to the options */
if ((*opts == 'N') || (*opts == 'n'))
SearchFlag = ENVIRONMENT_ONLY ;
/* do not search current dir */
else if ((*opts == 'C') || (*opts == 'c'))
SearchFlag = CURRENT_DIR_FIRST;
/* search current 1st(default)*/
else
return INVALID_ROUTINE; /* Invalid option */
}
ulRc = SearchPath(SearchFlag, args[0].strptr, args[1].strptr,
(char *)buf, sizeof(buf));
/* if environment variable could not be found, try again with */
/* uppercase name. */
if (ulRc)
{
char *temp = strdup(args[0].strptr);
strupr(temp);
ulRc = SearchPath(SearchFlag, temp, args[1].strptr,
(char *)buf, sizeof(buf));
free(temp);
}
BUILDRXSTRING(retstr, (const char *)buf); /* pass back result */
return VALID_ROUTINE;
}
#ifdef LINUX
/*************************************************************************
* Function: SysLinVer *
* *
* Syntax: call SysLinVer *
* *
* Return: Linux Version *
*************************************************************************/
size_t RexxEntry SysLinVer(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
struct utsname info; /* info structur */
if (numargs != 0) /* validate arg count */
return INVALID_ROUTINE;
if(uname(&info) < 0) /* if no info stored */
return INVALID_ROUTINE; /* get out */
sprintf(retstr->strptr, "%s %s",info.sysname,info.release);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
#endif
/*************************************************************************
* Function: SysVersion *
* *
* Syntax: call SysVersion *
* *
* Return: Operating System name (LINUX/AIX/WINDOWS) and Version *
*************************************************************************/
size_t RexxEntry SysVersion(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
struct utsname info; /* info structur */
if (numargs != 0) /* validate arg count */
return INVALID_ROUTINE;
if(uname(&info) < 0) /* if no info stored */
return INVALID_ROUTINE; /* get out */
sprintf(retstr->strptr, "%s %s.%s",info.sysname, info.version, info.release);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
/*************************************************************************
* Semaphore data struct *
*************************************************************************/
typedef struct RxSemData {
bool named; /* Named semaphore? */
sem_t * handle; /* Semaphore pointer */
} RXSEMDATA;
/*************************************************************************
* Function: SysCreateEventSem *
* *
* Syntax: handle = SysCreateEventSem(<name>) *
* *
* Params: name - optional name for a event semaphore *
* *
* Return: handle - token used as a event sem handle for *
* SysPostEventSem, SysClearEventSem, *
* SysCloseEventSem, and SysOpenEventSem *
* '' - Empty string in case of any error *
*************************************************************************/
RexxRoutine2(RexxObjectPtr, SysCreateEventSem, OPTIONAL_CSTRING, name, OPTIONAL_CSTRING, reset)
{
RXSEMDATA *semdata;
int rc;
// Note that the reset arg has no meaning on Unix/Linux and is unused.
semdata = (RXSEMDATA *)malloc(sizeof(RXSEMDATA));
if (semdata == NULL) {
return context->String("");
}
if (name == NULL) {
/* this is an unnamed semaphore */
semdata->handle = (sem_t *)malloc(sizeof(sem_t));
rc = sem_init(semdata->handle, 0, 0);
if (rc == -1) {
free(semdata);
return context->String("");
}
semdata->named = false;
}
else {
/* this is a named semaphore */
sem_unlink(name);
semdata->handle = sem_open(name, (O_CREAT | O_EXCL), (S_IRWXU | S_IRWXG), 0);
if (semdata->handle == SEM_FAILED ) {
free(semdata);
return context->String("");
}
semdata->named = true;
}
return context->Uintptr((uintptr_t)semdata);
}
/*************************************************************************
* Function: SysOpenEventSem *
* *
* Syntax: result = SysOpenEventSem(handle) *
* *
* Params: handle - token returned from SysCreateEventSem *
* *
* Return: result - return code from DosOpenEventSem *
*************************************************************************/
RexxMethod1(uintptr_t, SysOpenEventSem, CSTRING, name)
{
RXSEMDATA *semdata;
semdata = (RXSEMDATA *)malloc(sizeof(RXSEMDATA));
if (semdata == NULL) {
return 0;
}
semdata->handle = sem_open(name, 0);
if (semdata->handle == SEM_FAILED ) {
return 0;
}
semdata->named = true;
return (uintptr_t)semdata->handle;
}
/*************************************************************************
* Function: SysResetEventSem *
* *
* Syntax: result = SysResetEventSem(handle) *
* *
* Params: handle - token returned from SysCreateEventSem *
* *
* Return: result - return code from DosResetEventSem *
*************************************************************************/
RexxMethod1(int, SysResetEventSem, uintptr_t, vhandle)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
sem_init(semdata->handle, 1, 0);
return 0;
}
/*************************************************************************
* Function: SysPostEventSem *
* *
* Syntax: result = SysPostEventSem(handle) *
* *
* Params: handle - token returned from SysCreateEventSem *
* *
* Return: result - return code from DosPostEventSem *
*************************************************************************/
RexxMethod1(int, SysPostEventSem, uintptr_t, vhandle)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
int rc;
rc = sem_post(semdata->handle);
if (rc) {
return 6;
}
return 0;
}
/*************************************************************************
* Function: SysCloseEventSem *
* *
* Syntax: result = SysCloseEventSem(handle) *
* *
* Params: handle - token returned from SysCreateEventSem *
* *
* Return: result - return code from DosCloseEventSem *
*************************************************************************/
RexxMethod1(int, SysCloseEventSem, uintptr_t, vhandle)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
if (semdata->named == false) {
/* this is an unnamed semaphore so we must free the target */
if (sem_destroy(semdata->handle)) {
if (errno == EINVAL) {
return 6;
}
else if (errno) {
return 102;
}
}
}
else {
/* this is a named semaphore */
if (sem_close(semdata->handle)) {
if (errno == EINVAL) {
return 6;
}
else if (errno) {
return 102;
}
}
}
free(semdata);
return 0;
}
#define SEM_WAIT_PERIOD 100 /* POSIX says this should be 10ms */
/*************************************************************************
* Function: SysWaitEventSem *
* *
* Syntax: result = SysWaitEventSem(handle, <timeout>) *
* *
* Params: handle - token returned from SysWaitEventSem *
* *
* Return: result - return code from DosWaitEventSem *
*************************************************************************/
RexxRoutine2(int, SysWaitEventSem, uintptr_t, vhandle, OPTIONAL_int, timeout)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
int rc = 0;
if (timeout != 0) {
/* this looping construct will cause us to wait longer than the */
/* specified timeout due to the latency involved in the loop, */
/* but that cannot be helped */
while (timeout > 0) {
rc = sem_trywait(semdata->handle);
if (rc == 0) {
break;
}
if (usleep(SEM_WAIT_PERIOD * 1000)) {
timeout -= SEM_WAIT_PERIOD;
}
}
}
else {
rc = sem_wait(semdata->handle);
}
if (rc) {
if (errno == EAGAIN) {
return 121;
}
else if (errno == EINVAL) {
return 6;
}
}
return 0;
}
/*************************************************************************
* Function: SysCreateMutexSem *
* *
* Syntax: handle = SysCreateMutexSem(<name>) *
* *
* Params: name - optional name for a event semaphore *
* *
* Return: handle - token used as a event sem handle for *
* SysPostEventSem, SysClearEventSem, *
* SysCloseEventSem, and SysOpenEventSem *
* '' - Empty string in case of any error *
*************************************************************************/
RexxRoutine1(RexxObjectPtr, SysCreateMutexSem, OPTIONAL_CSTRING, name)
{
RXSEMDATA *semdata;
int rc;
semdata = (RXSEMDATA *)malloc(sizeof(RXSEMDATA));
if (semdata == NULL) {
return context->String("");
}
if (strlen(name) == 0) {
/* this is an unnamed semaphore */
semdata->handle = (sem_t *)malloc(sizeof(sem_t));
rc = sem_init(semdata->handle, 0, 0);
if (rc == -1) {
free(semdata);
return context->String("");
}
semdata->named = false;
}
else {
/* this is a named semaphore */
sem_unlink(name);
semdata->handle = sem_open(name, (O_CREAT | O_EXCL), (S_IRWXU | S_IRWXG), 0);
if (semdata->handle == SEM_FAILED ) {
free(semdata);
return context->String("");
}
semdata->named = true;
}
rc = sem_post(semdata->handle);
return context->Uintptr((uintptr_t)semdata);
}
/*************************************************************************
* Function: SysOpenMutexSem *
* *
* Syntax: result = SysOpenMutexSem(handle) *
* *
* Params: handle - token returned from SysCreateMutexSem *
* *
* Return: result - return code from DosOpenEventSem *
*************************************************************************/
RexxRoutine1(uintptr_t, SysOpenMutexSem, CSTRING, name)
{
RXSEMDATA *semdata;
semdata = (RXSEMDATA *)malloc(sizeof(RXSEMDATA));
if (semdata == NULL) {
return 0;
}
semdata->handle = sem_open(name, 0);
if (semdata->handle == SEM_FAILED ) {
return 0;
}
semdata->named = true;
return (uintptr_t)semdata->handle;
}
/*************************************************************************
* Function: SysRequestMutexSem *
* *
* Syntax: result = SysRequestMutexSem(handle, <timeout>) *
* *
* Params: handle - token returned from SysRequestMutexSem *
* *
* Return: result - return code from DosWaitEventSem *
*************************************************************************/
RexxRoutine2(int, SysRequestMutexSem, uintptr_t, vhandle, OPTIONAL_int, timeout)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
int rc = 0;
if (timeout != 0) {
/* this looping construct will cause us to wait longer than the */
/* specified timeout due to the latency involved in the loop, */
/* but that cannot be helped */
while (timeout > 0) {
rc = sem_trywait(semdata->handle);
if (rc == 0) {
break;
}
if (usleep(SEM_WAIT_PERIOD * 1000)) {
timeout -= SEM_WAIT_PERIOD;
}
}
}
else {
rc = sem_wait(semdata->handle);
}
if (rc) {
if (errno == EAGAIN) {
return 121;
}
else if (errno == EINVAL) {
return 6;
}
}
return 0;
}
/*************************************************************************
* Function: SysReleaseMutexSem *
* *
* Syntax: result = SysReleaseMutexSem(handle) *
* *
* Params: handle - token returned from SysCreateMutexSem *
* *
* Return: result - return code from DosCloseEventSem *
*************************************************************************/
RexxRoutine1(int, SysReleaseMutexSem, uintptr_t, vhandle)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
int rc;
int val;
rc = sem_getvalue(semdata->handle, &val);
if (rc) {
if (errno == EINVAL) {
return 6;
}
else {
return 288;
}
}
if (val == 0) {
rc = sem_post(semdata->handle);
if (rc) {
return 6;
}
}
return 0;
}
/*************************************************************************
* Function: SysCloseMutexSem *
* *
* Syntax: result = SysCloseMutexSem(handle) *
* *
* Params: handle - token returned from SysCreateMutexSem *
* *
* Return: result - return code from DosCloseEventSem *
*************************************************************************/
RexxRoutine1(int, SysCloseMutexSem, uintptr_t, vhandle)
{
RXSEMDATA *semdata = (RXSEMDATA *)vhandle;
if (semdata->named == false) {
/* this is an unnamed semaphore so we must free the target */
if (sem_destroy(semdata->handle)) {
if (errno == EINVAL) {
return 6;
}
else if (errno) {
return 102;
}
}
}
else {
/* this is a named semaphore */
if (sem_close(semdata->handle)) {
if (errno == EINVAL) {
return 6;
}
else if (errno) {
return 102;
}
}
}
free(semdata);
return 0;
}
/*************************************************************************
* Function: SysFileTree *
* *
* Syntax: call SysFileTree filespec, stem [, options] *
* *
* Params: filespec - Filespec to search for (may include * and ?). *
* stem - Name of stem var to store results in. *
* options - Any combo of the following: *
* 'B' - Search for files and directories. *
* 'D' - Search for directories only. *
* 'F' - Search for files only. *
* 'O' - Only output file names. *
* 'S' - Recursively scan subdirectories. *
* 'T' - Combine time & date fields into one. *
* 'L' - Long time format *
* 'I' - Case Insensitive search. *
* *
* Return: NO_UTIL_ERROR - Successful. *
* ERROR_NOMEM - Out of memory. *
*************************************************************************/
size_t RexxEntry SysFileTree(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
char FileSpec[IBUF_LEN]; /* File spec to look for */
char path[IBUF_LEN]; /* path to search along */
const char *optptr; /* option scan pointer */
size_t options; /* Mask of options */
int smask[5]; /* Source attribute mask */
int dmask[5]; /* Target attribute mask */
RXTREEDATA ldp; /* local data */
char * temp;
options = DO_FILES|DO_DIRS; /* Clear if we should not */
/* display files */
smask[0] = IGNORE; /* No mask unless specified */
dmask[0] = IGNORE; /* No mask unless specified */
path[0] = '\0'; /* no path yet */
BUILDRXSTRING(retstr, NO_UTIL_ERROR);/* pass back result */
/* validate arguments */
if (numargs < 2 || numargs > 3 ||
!RXVALIDSTRING(args[0]) ||
!RXVALIDSTRING(args[1]) ||
args[0].strlength > 255)
return INVALID_ROUTINE; /* Invalid call to routine */
/* initialize data area */
ldp.count = 0;
strcpy(ldp.varname, args[1].strptr);
ldp.stemlen = args[1].strlength;
/* uppercase the name */
strupr(ldp.varname);
if (ldp.varname[ldp.stemlen-1] != '.')
ldp.varname[ldp.stemlen++] = '.';
strcpy(FileSpec, args[0].strptr); /* get file spec */
/* if filespec is '*' then use './ *' */
if((strlen(FileSpec)==1)&&(FileSpec[0] == '*'))
strcpy(FileSpec, "./*");
/** If FileSpec ends in / then append * */
if (FileSpec[strlen(FileSpec)-1] == '/')
strcat(FileSpec, "*");
/* if filespec begins with '~' generate absolute path */
if(FileSpec[0] == '~'){
if(!(temp = resolve_tilde(FileSpec)))/* get the absolute path */
return INVALID_ROUTINE;
strcpy(FileSpec,temp); /* work with the new one */
free(temp); /* free temp space */
}
if (numargs >= 3 && /* check third option */
!RXNULLSTRING(args[2])) {
if (!args[2].strlength) /* a zero length string isn't */
return INVALID_ROUTINE; /* valid */
optptr = args[2].strptr; /* copy the pointer */
while (*optptr) { /* while more characters */
switch(toupper(*optptr)) { /* process each option */
case 'S': /* recurse on subdirectories */
options |= RECURSE; /* Should we recurse */
break;
case 'O': /* only return names */
options |= NAME_ONLY; /* Should include names only */
break;
case 'T': /* editable time? */
options |= EDITABLE_TIME; /* create editable timestamp */
break;
case 'L': /* long time format? */
options |= LONG_TIME; /* create timestamp */
break;
case 'F': /* include only files? */
options &= ~DO_DIRS; /* Should not include dirs ! */
options |= DO_FILES; /* Should include files ! */
break;
case 'D': /* include only directories? */
options |= DO_DIRS; /* Should include dirs ! */
options &= ~DO_FILES; /* Should not include files ! */
break;
case 'B': /* include both files and dirs*/
options |= DO_DIRS; /* Should include dirs ! */
options |= DO_FILES; /* Should include files ! */
break;
case 'I': /* case insensitive? */
options |= CASELESS; /* do caseless check */
break;
default: /* unknown option */
return INVALID_ROUTINE; /* raise an error */
}
optptr++; /* step the option pointer */
}
}
/* get path and name */
getpath(FileSpec, path, ldp.TargetSpec);
if ( options & CASELESS ) { /* if caseless upper case */
char * p = ldp.TargetSpec; /* file name portion now */
for ( ; *p; ++p ) {
*p = toupper(*p);
}
}
/* recursively search */
if (RecursiveFindFile(FileSpec, path, &ldp, smask, dmask, options))
return INVALID_ROUTINE;
/* return lines read */
sprintf(ldp.Temp, "%d", ldp.count);
ldp.varname[ldp.stemlen] = '0';
ldp.varname[ldp.stemlen+1] = 0;
ldp.shvb.shvnext = NULL;
ldp.shvb.shvname.strptr = ldp.varname;
ldp.shvb.shvname.strlength = ldp.stemlen+1;
ldp.shvb.shvnamelen = ldp.stemlen+1;
ldp.shvb.shvvalue.strptr = ldp.Temp;
ldp.shvb.shvvalue.strlength = strlen(ldp.Temp);
ldp.shvb.shvvaluelen = ldp.shvb.shvvalue.strlength;
ldp.shvb.shvcode = RXSHV_SET;
ldp.shvb.shvret = 0;
if (RexxVariablePool(&ldp.shvb) == RXSHV_BADN)
return INVALID_ROUTINE; /* error on non-zero */
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysTempFileName *
* *
* Syntax: call SysTempFileName template [,filler] *
* *
* Params: template - Description of filespec desired. For example: *
* C:\TEMP\FILE.??? *
* filler - A character which when found in template will be *
* replaced with random digits until a unique file *
* or directory is found. The default character *
* is '?'. *
* *
* Return: other - Unique file/directory name. *
* '' - No more files exist given specified template. *
*************************************************************************/
size_t RexxEntry SysTempFileName(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
char filler; /* filler character */
char numstr[6];
size_t num, max;
char *array;
char *dir; /* the directory */
char *file; /* the file prefix */
char *tmp; /* temporary */
int x, j, i;
j = 0; /* get a copy of the argument */
i = 0;
max = 1;
if (numargs < 1 || /* validate arguments */
numargs > 2 ||
!RXVALIDSTRING(args[0]) ||
args[0].strlength > 512)
return INVALID_ROUTINE;
if (numargs == 2 && /* get filler character */
!RXNULLSTRING(args[1])) {
if (args[1].strlength != 1) /* must be one character */
return INVALID_ROUTINE;
filler = args[1].strptr[0];
}
else
filler = '?';
/* get the file id */
dir = (char*) malloc(args[0].strlength+1);
if (dir == NULL){ /* if something went wrong */
BUILDRXSTRING(retstr, ERROR_NOMEM);
return VALID_ROUTINE;
}
strcpy(dir, args[0].strptr); /* copy the string */
tmp = dir; /* set temporary */
/* search for filter *********/
for (x = 0; tmp[x] != 0; x++)
{
if (tmp[x] == filler)
{
max = max *10;
j++;
}
}
if (j)
{
srand(time(0));
num = rand();
num = num % max;
switch (j)
{
case 1:
sprintf(numstr, "%01u", num);
break;
case 2:
sprintf(numstr, "%02u", num);
break;
case 3:
sprintf(numstr, "%03u", num);
break;
case 4:
sprintf(numstr, "%04u", num);
break;
case 5:
sprintf(numstr, "%05u", num);
break;
default:
return INVALID_ROUTINE; /* raise error condition */
} /* for compatibility */
for (x = 0; tmp[x] !=0; x++)
{
if (tmp[x] == filler)
{
tmp[x] = numstr[i++];
}
}
} /* if we need the filler */
while(*tmp != 0 ) /* lets start at the end */
{
tmp++;
}
while((*tmp != '/') && (*tmp != '\\') && (tmp > dir))
{
--tmp;
}
if(tmp == dir)
{ /* directory string is '' or '/' */
if(*dir == '\\'){
file = dir+1;
array = tempnam(NULL,file); /* call system routine */
}
else if(*dir == '/'){
file = dir+1;
array = tempnam("/",file); /* call system routine */
}
else{
file = dir;
array = tempnam(NULL,file);/* call system routine */
}
}
else
{ /* directory string exists */
file = tmp+1; /* set filename prefix */
*tmp = '\0'; /* terminate directory string */
array = tempnam(dir,file);/* call system routine */
}
if(strlen(array) > 255)
{
free((char *)retstr->strptr);
retstr->strptr = (char *) malloc(strlen(array) + 1);
}
strcpy(retstr->strptr, array);
/* correct string length */
retstr->strlength = strlen(retstr->strptr);
free (array);
free(dir); /* free local string */
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysSetPriority *
* *
* Syntax: result = SysSetPriority(Class, Level) *
* *
* Params: Class - The priority class (0-4) *
* Level - Amount to change (-31 to +31) *
* (lower to higher priority) *
* Return: 0 for correct execution *
* 304 for ERROR_INVALID_PDELTA *
* 307 for ERROR_INVALID_PCLASS *
* derived from: *
* result - return code from DosSetPriority *
* *
*************************************************************************/
size_t RexxEntry SysSetPriority(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int pclass; /* priority class */
int level; /* priority level */
RexxReturnCode rc; /* creation return code */
if (numargs != 2 || /* must have two */
!RXVALIDSTRING(args[0])) /* first is omitted */
return INVALID_ROUTINE; /* raise error condition */
/* get class of change */
if(!string2int(args[0].strptr,&pclass) || /* set the value for pclass */
!string2int(args[1].strptr,&level)) /* set the value for level */
return INVALID_ROUTINE;
if (pclass == 0){ /* class 0 -> no change */
rc = 0; /* no error */
}
/* change the priority */
/* change according to delta */
else if (((size_t)pclass > 0) && ((size_t)pclass <= 4)){
int pid; /* PID */
pid = getpid(); /* current PID */
int priority; /* Priority */
/* current priority */
priority = getpriority(PRIO_PROCESS, getpid());
/* Set new priority */
setpriority(PRIO_PROCESS, getpid(),-level);
rc = 0;
}
else{
rc = 307;
return INVALID_ROUTINE; /* raise error condition */
}
sprintf(retstr->strptr, "%d", rc); /* format the return code */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysGetMessage *
* *
* Syntax: call SysGetMessage msgnum [,file] [,str1]...[,str9] *
* *
* Params: file - Name of message file to get message from. *
* Default is OSO001.MSG. *
* msgnum - Number of message being queried. *
* str1 ... str9 - Insertion strings. For messages which *
* contain %1, %2, etc, the str1, str2, etc *
* strings will be inserted (if given). *
* *
* Return: The message with the inserted strings (if given). *
* Note: The set number ist always 1. Therefore the interface *
* remains the same as in OS/2 and Win. *
* Reason: keep portability *
*************************************************************************/
size_t RexxEntry SysGetMessage(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int msgnum; /* Message number to get */
int setnum = 1; /* Set number (const 1) */
#if defined( HAVE_CATOPEN )
nl_catd catalog; /* catalog handle */
#endif
/* default error msg */
const char default_message[] = {"Error: Message catalog not open !\0"};
/* msg not found msg */
const char not_found_message[] = {"Error: Message not found !\0"};
/* insertion error msg */
const char error_insertions[] = {"Error: Unable to generate message \
(wrong insertions)\0"};
/* cat not found msg */
const char cat_not_found_message[] = {"Error: Message catalog not found !\0"};
const char * msg; /* msg retrieved from catalog */
int icount; /* number of insertions */
int msg_length = 0; /* length of the return msg */
const char * msgfile; /* name of the message file */
char * temp;
int count = 0; /* number of '%s' in the msg */
if (numargs < 1 || numargs > 11 || /* validate arguments */
!RXVALIDSTRING(args[0]))
return INVALID_ROUTINE; /* exit with error */
/* get message number */
if (!string2int(args[0].strptr, &msgnum) || msgnum < 0)
return INVALID_ROUTINE; /* exit with error */
/* Get message file name. */
/* Use "rexx.cat if not */
/* given */
if (numargs >= 2 && RXVALIDSTRING(args[1]))
msgfile = args[1].strptr; /* use provided message file */
else
msgfile = REXXMESSAGEFILE;
#if defined( HAVE_SETLOCALE )
setlocale(LC_ALL, "en_US");
#endif
#if defined( HAVE_CATOPEN )
/* open the catalog */
if((catalog = catopen(msgfile, NL_CAT_LOCALE)) == (nl_catd)-1){
retstr->strptr = (char *)malloc(strlen(cat_not_found_message)+1);
strcpy(retstr->strptr, cat_not_found_message);
retstr->strlength = strlen(cat_not_found_message);
return VALID_ROUTINE;
}
/* retrieve msg from catalog */
msg = catgets(catalog, setnum, (int)msgnum, default_message);
if(*msg == '\0') /* if empty string returned */
msg = not_found_message; /* it means msg not found */
/* set number of insertions */
if (numargs >= 2)
icount = numargs-2;
else
icount = 0;
/* calculate length of the return message */
for(int j=2; j < icount+2; j++)
msg_length += args[j].strlength;
msg_length += strlen(msg);
msg_length -= icount*2;
/* alloc needed space for the return message (add 100 for default msgs) */
if(!(retstr->strptr = (char *)malloc(msg_length+100))){
BUILDRXSTRING(retstr, ERROR_NOMEM);
catclose(catalog);
return VALID_ROUTINE;
}
/* check for too much '%s' in the message */
temp = const_cast<char *>(msg);
/* replace all &1..&9 with %s */
while((temp = strstr(temp, "&"))){
if(isdigit(*(temp+1))){ /* replace &1..&9 ? */
*(temp++) = '%';
*(temp++) = 's'; /* %s expected */
// count++;
}
else
temp++;
}
/* now look for number of replacement variables */
temp = const_cast<char *>(msg); /* reset temp pointer */
while((temp = strstr(temp,"%s"))){ /* search for the %s */
count ++; /* increment counter */
temp += 2; /* jump over %s */
}
if(count > icount)
icount = 10; /* go to error case */
/* generate full message with insertions */
switch(icount){
case(1):{
if(sprintf(retstr->strptr, msg,args[2].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(2):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(3):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(4):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(5):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(6):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(7):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(8):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr,
args[9].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(9):{
if(sprintf(retstr->strptr, msg,args[2].strptr,
args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr,
args[9].strptr,
args[10].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(10):{
strcpy(retstr->strptr, error_insertions); /* error case */
break;
}
default:{
strcpy(retstr->strptr, msg);
break;
}
}
retstr->strlength = strlen(retstr->strptr);/* insert the length */
catclose(catalog); /* close the catalog */
return VALID_ROUTINE; /* no error on call */
#else
/* catopen not supported */
const char cat_not_supported_message[] = {"Error: Message catalog (catopen) not supported !\0"};
retstr->strptr = (char *)malloc(strlen(cat_not_supported_message)+1);
strcpy(retstr->strptr, cat_not_supported_message);
retstr->strlength = strlen(cat_not_supported_message);
return VALID_ROUTINE;
#endif
}
/*************************************************************************
* Function: SysGetMessageX *
* *
* Syntax: call SysGetMessageX setnum, msgnum [,file] [,str1]...[,str9]*
* *
* Params: file - Name of message file to get message from. *
* Default is OSO001.MSG. *
* msgnum - Number of message being queried. *
* str1 ... str9 - Insertion strings. For messages which *
* contain %1, %2, etc, the str1, str2, etc *
* strings will be inserted (if given). *
* setnum - set number in the catalog *
* *
* Return: The message with the inserted strings (if given). *
* Note: This is a special Unix only version of SysGetMessage which *
* supports the selection of a set in the msg catalog. *
*************************************************************************/
size_t RexxEntry SysGetMessageX(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int msgnum; /* Message number to get */
int setnum; /* Set number */
#if defined( HAVE_CATOPEN )
nl_catd catalog; /* catalog handle */
#endif
/* default error msg */
const char default_message[] = {"Error: Message catalog not open !\0"};
/* msg not found msg */
const char not_found_message[] = {"Error: Message not found !\0"};
/* insertion error msg */
const char error_insertions[] = {"Error: Unable to generate message \
(wrong insertions)\0"};
/* cat not found msg */
const char cat_not_found_message[] = {"Error: Message catalog not found !\0"};
char * msg; /* msg retrieved from catalog */
int icount; /* number of insertions */
int msg_length = 0; /* length of the return msg */
const char * msgfile; /* name of the message file */
char * temp;
int count = 0; /* number of '%s' in the msg */
if (numargs < 1 || numargs > 12 || /* validate arguments */
!RXVALIDSTRING(args[0]) ||
!RXVALIDSTRING(args[1]))
return INVALID_ROUTINE; /* exit with error */
/* get set number */
if (!string2int(args[0].strptr, &setnum) || setnum < 0)
return INVALID_ROUTINE; /* exit with error */
/* get message number */
if (!string2int(args[1].strptr, &msgnum) || msgnum < 0)
return INVALID_ROUTINE; /* exit with error */
/* Get message file name. */
/* Use "rexx.cat if not */
/* given */
if (numargs >= 3 && RXVALIDSTRING(args[1]))
msgfile = args[2].strptr; /* use provided message file */
else
msgfile = REXXMESSAGEFILE;
#if defined( HAVE_CATOPEN )
/* open the catalog */
if((catalog = catopen(msgfile, NL_CAT_LOCALE)) == (nl_catd)-1){
retstr->strptr = (char *)malloc(strlen(cat_not_found_message)+1);
strcpy(retstr->strptr, cat_not_found_message);
retstr->strlength = strlen(cat_not_found_message);
return VALID_ROUTINE;
}
/* retrieve msg from catalog */
msg = catgets(catalog, setnum, msgnum, default_message);
if(*msg == '\0') /* if empty string returned */
msg = const_cast<char *>(not_found_message); /* it means msg not found */
/* set number of insertions */
if (numargs >= 3)
icount = numargs-3;
else
icount = 0;
/* calculate length of the return message */
for(int j=3; j < icount+3; j++)
msg_length += args[j].strlength;
msg_length += strlen(msg);
msg_length -= icount*2;
/* alloc needed space for the return message (add 100 for default msgs) */
if(!(retstr->strptr = (char *)malloc(msg_length+100))){
BUILDRXSTRING(retstr, ERROR_NOMEM);
catclose(catalog);
return VALID_ROUTINE;
}
/* check for to much '%s' in the message */
temp = msg;
/* replace all &1..&9 with %s */
while((temp = strstr(temp, "&"))){
if(isdigit(*(temp+1))){ /* replace &1..&9 ? */
*(temp++) = '%';
*(temp++) = 's'; /* %s expected */
}
else
temp++;
}
/* now look for number of replacement variables */
temp = msg; /* reset temp pointer */
while((temp = strstr(temp,"%s"))){ /* search for the %s */
count++; /* increment counter */
temp += 2; /* jump over %s */
}
if(count > icount)
icount = 10; /* go to error case */
/* generate full message with insertions */
switch(icount){
case(1):{
if(sprintf(retstr->strptr, msg,args[3].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(2):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(3):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(4):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(5):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(6):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(7):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr,
args[9].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(8):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr,
args[9].strptr,
args[10].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(9):{
if(sprintf(retstr->strptr, msg,args[3].strptr,
args[4].strptr,
args[5].strptr,
args[6].strptr,
args[7].strptr,
args[8].strptr,
args[9].strptr,
args[10].strptr,
args[11].strptr) != msg_length)
strcpy(retstr->strptr, error_insertions);
break;
}
case(10):{
strcpy(retstr->strptr, error_insertions); /* error case */
break;
}
default:{
strcpy(retstr->strptr, msg);
break;
}
}
retstr->strlength = strlen(retstr->strptr);/* insert the length */
catclose(catalog); /* close the catalog */
return VALID_ROUTINE; /* no error on call */
#else
/* catopen not supported */
const char cat_not_supported_message[] = {"Error: Message catalog (catopen) not supported !\0"};
retstr->strptr = (char *)malloc(strlen(cat_not_supported_message)+1);
strcpy(retstr->strptr, cat_not_supported_message);
retstr->strlength = strlen(cat_not_supported_message);
return VALID_ROUTINE;
#endif
}
/*************************************************************************
* Function: SysGetKey *
* *
* Syntax: call SysGetKey [echo] *
* *
* Params: echo - Either of the following: *
* 'ECHO' - Echo the inputted key (default). *
* 'NOECHO' - Do not echo the inputted key. *
* *
* Return: The key striked. *
*************************************************************************/
size_t RexxEntry SysGetKey(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
bool echo = true; /* Set to false if we */
/* shouldn't echo */
if (numargs > 1) /* too many arguments */
return INVALID_ROUTINE; /* raise an error */
if (numargs == 1) { /* validate arguments */
if (!strcasecmp(args[0].strptr, "NOECHO"))
echo = false;
else if (strcasecmp(args[0].strptr, "ECHO"))
return INVALID_ROUTINE; /* Invalid option */
}
getkey(retstr->strptr,echo); /* call the complicated part */
retstr->strlength = strlen(retstr->strptr); /* format string */
return VALID_ROUTINE; /* no error on call */
}
#ifdef AIX_DISABLED
/*************************************************************************
* Function: SysAddFuncPkg *
* *
* Description: Function to enable load of old CREXX libraries for AIX. *
* The Path must be fully qualified in REXX procedure, *
* it is used to select the old >load< function call. *
* *
* Syntax: call SysAddFuncPkg ( /Fully_qualified_path/Package_name ) *
* *
* Params: none *
* *
* Return: null string *
*************************************************************************/
size_t RexxEntry SysAddFuncPkg(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc = NULL;
int j; /* Counter */
size_t arglength; /* length of the count */
char * argstring; /* input sleep time */
RXFUNCBLOCK *funcblock; /* Base for function blocks */
PRXINITFUNCPKG InitFunc; /* Pointer returned from load */
retstr->strlength = 0; /* set return value */
/* check arguments */
if (numargs != 1) /* Must have one argument */
return INVALID_ROUTINE;
argstring = args[0].strptr; /* point to the string */
arglength = args[0].strlength; /* get length of string */
if ( (arglength == 0 ) || /* if null string */
(arglength >= MAXNAME) || /* or too long */
(!strchr( argstring, '/' )) )
return INVALID_ROUTINE; /* not valid */
if (!(InitFunc = (PRXINITFUNCPKG)load(argstring,0,NULL)))
{ /* loadAndInit? for load */
if ( InitFunc == NULL ) {
fprintf(stderr, " *** Unable to load library %s !\nError message: errno = %d;",\
argstring, errno);
perror(" REXXUTIL");
rc = 1;
}
}
if ( rc == NULL ) {
/* Call the initialization routine for the library (which should */
/* be the function pointer returned to us by load). */
rc = (*InitFunc)(&funcblock);
if (rc) { /* If routine indicates error,*/
/* tell the user. */
fprintf(stderr,"*** Library load routine gave error %d.\n",rc);
return(rc); /* don't load anything */
} /* endif */
/* Now run through the array of func blocks, adding them to the */
/* list of external functions or subcommand handlers. Note that */
/* we use the external function types in all cases, but since */
/* the only thing affected is the function pointers, there's no */
/* problem - the RXFUNCBLOCK and RXSUBCOMBLOCK types are */
/* otherwise identical. */
for (j=0; funcblock[j].name != NULL; j++) {
#ifdef ORXLD_DEBUG
fprintf(stderr,"REXXUTIL: PKGFunction %s \n", funcblock[j].name);
#endif
if ( funcblock[j].function && (rc == NULL) ) {
RexxRegisterFunctionDll( funcblock[j].name,
argstring, funcblock[j].name);
}
}
}
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysAddCmdPkg like SysAddFuncPkg *
* *
*************************************************************************/
size_t RexxEntry SysAddCmdPkg(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
return SysAddFuncPkg(
name, /* Function name */
numargs, /* Number of arguments */
args, /* Argument array */
queuename, /* Current queue */
retstr ); /* Return RXSTRING */
}
/*************************************************************************
* Function: SysDropFuncPkg *
* *
* Description: Function to enable load of old CREXX libraries for AIX. *
* It is used for the old >load< function call for the *
* list of function to be dropped from REXX. *
* The Path must not be fully qualified in REXX procedure. *
* *
* Syntax: call SysDropFuncPkg *
* *
* Return: NO_UTIL_ERROR - Successful. *
*************************************************************************/
size_t RexxEntry SysDropFuncPkg(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc = NULL;
int j; /* Counter */
size_t arglength; /* length of the count */
char * argstring; /* input sleep time */
RXFUNCBLOCK *funcblock; /* Base for function blocks */
PRXINITFUNCPKG InitFunc; /* Pointer returned from load */
retstr->strlength = 0; /* set return value */
/* check arguments */
if (numargs != 1) /* Must have one argument */
return INVALID_ROUTINE;
argstring = args[0].strptr; /* point to the string */
arglength = args[0].strlength; /* get length of string */
if ( (arglength == 0 ) || /* if null string */
(arglength >= MAXNAME) ) /* or too long */
return INVALID_ROUTINE; /* not valid */
if (!(InitFunc = (PRXINITFUNCPKG)load(argstring,0,NULL)))
{ /* loadAndInit? for load */
if ( InitFunc == NULL ) {
fprintf(stderr, " *** Unable to drop library %s !\nError message: errno = %d;",\
argstring, errno);
perror(" REXXUTIL");
rc = 1;
}
}
if ( rc == NULL ) {
/* Call the initialization routine for the library (which should */
/* be the function pointer returned to us by load). */
rc = (*InitFunc)(&funcblock);
if (rc) { /* If routine indicates error,*/
/* tell the user. */
fprintf(stderr," *** Library drop routine gave error %d.\n",rc);
return(rc); /* don't load anything */
} /* endif */
/* Now run through the array of func blocks, adding them to the */
/* list of external functions or subcommand handlers. Note that */
/* we use the external function types in all cases, but since */
/* the only thing affected is the function pointers, there's no */
/* problem - the RXFUNCBLOCK and RXSUBCOMBLOCK types are */
/* otherwise identical. */
for (j=0; funcblock[j].name != NULL; j++) {
#ifdef ORXLD_DEBUG
fprintf(stderr,"REXXUTIL: PKGFunction %s \n", funcblock[j].name);
#endif
if ( funcblock[j].function && (rc == NULL) ) {
RexxDeregisterFunction( funcblock[j].name );
}
} /* endfor */
}
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysDropCmdPkg like SysDropFuncPkg *
* *
*************************************************************************/
size_t RexxEntry SysDropCmdPkg(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
return SysDropFuncPkg(
name, /* Function name */
numargs, /* Number of arguments */
args, /* Argument array */
queuename, /* Current queue */
retstr ); /* Return RXSTRING */
}
#endif /* CREXX funcs */
#ifdef AIX
/*************************************************************************
* Function: SysGetpid *
* *
* Description: Function to migrate CREXX for AIX procedures. *
* *
* Syntax: call SysGetpid() *
* *
* Return: Process_ID *
*************************************************************************/
size_t RexxEntry SysGetpid(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
retstr->strlength = 0; /* set return value */
/* check arguments */
if (numargs != 0) /* Must have no argument */
return INVALID_ROUTINE;
else
{
sprintf(retstr->strptr, "%d", getpid());
retstr->strlength = strlen(retstr->strptr);
}
return VALID_ROUTINE; /* no error on call */
}
#endif /* CREXX funcs */
/*************************************************************************
* Function: SysFork *
* *
* Description: Function to migrate CREXX for AIX procedures. *
* *
* Syntax: call SysFork() *
* *
* Return: Process_ID ( to parent child''s ID / to child the ID 0 ) *
*************************************************************************/
size_t RexxEntry SysFork(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
retstr->strlength = 0; /* set return value */
/* check arguments */
if (numargs != 0) /* Must have no argument */
return INVALID_ROUTINE;
else
{
sprintf(retstr->strptr, "%d", fork());
retstr->strlength = strlen(retstr->strptr);
}
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysWait *
* *
* Description: Function to migrate CREXX for AIX procedures. *
* *
* Syntax: call SysWait() *
* *
* Return: exit code of child *
*************************************************************************/
size_t RexxEntry SysWait(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int iStatus;
retstr->strlength = 0; /* set return value */
/* check arguments */
if (numargs != 0) /* Must have no argument */
return INVALID_ROUTINE;
else
{
wait( &iStatus );
sprintf(retstr->strptr, "%d", iStatus);
retstr->strlength = strlen(retstr->strptr);
}
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysCreatePipe *
* *
* Description: Function to migrate CREXX for AIX procedures. *
* Function creates an unnamed pipe *
* *
* Syntax: call SysCreatePipe( Blocking | Nonblocking ) *
* *
* Return: 'handle handle' ( handle for read and handle for write )*
*************************************************************************/
size_t RexxEntry SysCreatePipe(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int iStatus;
int iaH[2];
char cBlocking = 0;
retstr->strlength = 0; /* set return value */
/* check arguments ---------- */
if (numargs > 1) /* More than one arg? */
return INVALID_ROUTINE;
if (numargs == 0 ) /* No arg? */
cBlocking = 1; /* Default is blocking */
/* One arg, first char is 'b'? */
else if (args[0].strptr[0] == 'b' || args[0].strptr[0] == 'B')
cBlocking = 1; /* Wants blocking */
/* One arg, first char is 'n'? */
else if (args[0].strptr[0] == 'n' || args[0].strptr[0] == 'N')
cBlocking = 0; /* Wants non-blocking */
if (pipe(iaH)) /* Create the pipe */
{
perror("*** ERROR: Creating pipe"); /* pipe creation failed */
return VALID_ROUTINE; /* no error on call */
}
if (!cBlocking) /* Non-blocking? */
{
/* Get file status flags ---------- */
iStatus = fcntl(iaH[0], F_GETFL, NULL);
iStatus |= O_NONBLOCK; /* Turn on NONBLOCK flag */
/* Does set work? ----------------- */
if (fcntl(iaH[0], F_SETFL, iStatus) == -1) {
perror("*** ERROR: Setting NONBLOCK flag"); /* No, tell user */
close(iaH[0]); close(iaH[1]); /* Close pipes */
return VALID_ROUTINE; /* no error on call */
}
}
sprintf(retstr->strptr, "%d %d", iaH[0], iaH[1]);/* Create return string */
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
/*************************************************************************
* Function: SysDumpVariables *
* *
* Syntax: result = SysDumpVariables([filename]) *
* *
* Params: filename - name of the file where variables are appended to *
* (dump is written to stdout if omitted) *
* *
* Return: 0 - dump completed OK *
* -1 - failure during dump *
*************************************************************************/
size_t RexxEntry SysDumpVariables(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
RexxReturnCode rc; /* Ret code */
SHVBLOCK shvb;
int handle;
bool fCloseFile = false;
if ( (numargs > 1) || /* wrong number of arguments? */
((numargs > 0) && !RXVALIDSTRING(args[0])) )
return INVALID_ROUTINE; /* raise error condition */
if (numargs > 0)
{
/* open output file for append */
fCloseFile = true;
handle = open(args[0].strptr, O_WRONLY | O_APPEND | O_CREAT, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH);
if(handle < 0)
return INVALID_ROUTINE; /* raise error condition */
}
else
handle = STDOUT_FILENO;
do
{
/* prepare request block */
shvb.shvnext = NULL;
shvb.shvname.strptr = NULL; /* let REXX allocate the memory */
shvb.shvname.strlength = 0;
shvb.shvnamelen = 0;
shvb.shvvalue.strptr = NULL; /* let REXX allocate the memory */
shvb.shvvalue.strlength = 0;
shvb.shvvaluelen = 0;
shvb.shvcode = RXSHV_NEXTV;
shvb.shvret = 0;
rc = RexxVariablePool(&shvb);
if (rc == RXSHV_OK)
{
write(handle, "Name=", strlen("Name="));
write(handle, shvb.shvname.strptr, shvb.shvname.strlength);
write(handle, ", Value='", 9);
write(handle, shvb.shvvalue.strptr,shvb.shvvalue.strlength);
write(handle, "'\n", 2);
/* free memory allocated by REXX */
RexxFreeMemory((void *)shvb.shvname.strptr);
RexxFreeMemory((void *)shvb.shvvalue.strptr);
/* leave loop if this was the last var */
if (shvb.shvret & RXSHV_LVAR)
break;
}
} while (rc == RXSHV_OK);
if (fCloseFile)
close(handle); /* close the file */
if (rc != RXSHV_LVAR)
RETVAL(-1)
else
RETVAL(0)
}
/*************************************************************************
* Function: SysGetFileDateTime *
* *
* Syntax: result = SysGetFileDateTime(filename [,timesel]) *
* Params: filename - name of the file to query *
* timesel - What filetime to query: Access/Write *
* *
* Return: -1 - file date/time query failed *
* other - date and time as YYYY-MM-DD HH:MM:SS *
*************************************************************************/
size_t RexxEntry SysGetFileDateTime(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
struct stat64 buf;
struct tm *newtime;
const char *dir_buf = NULL; /* full directory path */
bool fOk = true;
bool alloc_Flag = false;
if ( (numargs < 1) || (numargs > 2) ||
((numargs == 2) && !RXVALIDSTRING(args[1])) )
return INVALID_ROUTINE; /* raise error condition */
if(*(args[0].strptr) == '~')
{
dir_buf = resolve_tilde(args[0].strptr);
alloc_Flag = true;
}
else
{
dir_buf = args[0].strptr;
}
if (stat64(dir_buf, &buf) < 0)
{
fOk = false;
}
if(fOk)
{
if (numargs > 1)
{
switch (args[1].strptr[0])
{
case 'a':
case 'A':
newtime = localtime(&(buf.st_atime));
break;
case 'w':
case 'W':
newtime = localtime(&(buf.st_mtime));
break;
default:
return INVALID_ROUTINE;
}
}
else
newtime = localtime(&(buf.st_mtime));
newtime->tm_year += 1900;
newtime->tm_mon += 1;
sprintf(retstr->strptr, "%4d-%02d-%02d %02d:%02d:%02d",
newtime->tm_year,
newtime->tm_mon,
newtime->tm_mday,
newtime->tm_hour,
newtime->tm_min,
newtime->tm_sec);
retstr->strlength = strlen(retstr->strptr);
}
if( (dir_buf) && (alloc_Flag == true) )
free((void *)dir_buf); /* free the buffer memory */
if (!fOk)
RETVAL(-1)
else
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysSetFileDateTime *
* *
* Syntax: result = SysSetFileDateTime(filename [,newdate] [,newtime]) *
* *
* Params: filename - name of the file to update *
* newdate - new date to set in format YYYY-MM-DD (YYYY>1800) *
* newtime - new time to set in format HH:MM:SS *
* *
* Return: 0 - file date/time was updated correctly *
* -1 - failure attribute update *
*************************************************************************/
size_t RexxEntry SysSetFileDateTime(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
bool fOk = true;
struct utimbuf timebuf;
struct tm *newtime;
time_t ltime;
struct stat64 buf;
const char *dir_buf = NULL;
bool alloc_Flag = false;
/* we expect one to three parameters, if three parameters are */
/* specified then the second may be omitted to set only a new time,*/
/* if only one is specified then the file is set to current time */
if ( (numargs < 1) || (numargs > 3) ||
((numargs == 2) && !RXVALIDSTRING(args[1])) ||
((numargs == 3) && !RXVALIDSTRING(args[2])) )
return INVALID_ROUTINE; /* raise error condition */
if(*(args[0].strptr) == '~')
{
dir_buf = resolve_tilde(args[0].strptr);
alloc_Flag = true;
}
else
{
dir_buf = args[0].strptr;
}
if (stat64(dir_buf, &buf) < 0)
{
fOk = false;
}
if (numargs == 1)
{
time(&ltime);
timebuf.modtime = ltime;
if (utime(dir_buf, &timebuf) < 0)
{
fOk = false;
}
}
else
{
newtime = localtime(&(buf.st_mtime));
if ((numargs >= 2) && RXVALIDSTRING(args[1]))
{
/* parse new date */
if (sscanf(args[1].strptr, "%4d-%2d-%2d", &newtime->tm_year,
&newtime->tm_mon, &newtime->tm_mday) != 3)
fOk = false;
newtime->tm_year -= 1900;
newtime->tm_mon -= 1;
}
if ((numargs == 3) && RXVALIDSTRING(args[2]))
{
/* parse new time */
if (sscanf(args[2].strptr, "%2d:%2d:%2d", &newtime->tm_hour,
&newtime->tm_min, &newtime->tm_sec) != 3)
fOk = false;
}
ltime = mktime(newtime);
timebuf.modtime = ltime;
if (utime(dir_buf, &timebuf) < 0)
{
fOk = false;
}
}
if( (dir_buf) && (alloc_Flag == true) )
free((void *)dir_buf); /* free the buffer memory */
if (fOk)
RETVAL(0)
else
RETVAL(-1)
}
size_t RexxEntry RexxStemSort(const char *stemname, int order, int type,
size_t start, size_t end, size_t firstcol, size_t lastcol);
/*************************************************************************
* Function: SysStemSort *
* *
* Syntax: result = SysStemSort(stem, order, type, start, end, *
* firstcol, lastcol) *
* *
* Params: stem - name of stem to sort *
* order - 'A' or 'D' for sort order *
* type - 'C', 'I', 'N' for comparision type *
* start - first index to sort *
* end - last index to sort *
* firstcol - first column to use as sort key *
* lastcol - last column to use as sort key *
* *
* Return: 0 - sort was successful *
* -1 - sort failed *
*************************************************************************/
size_t RexxEntry SysStemSort(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
char stemName[255];
size_t first = 1;
size_t last = SIZE_MAX;
size_t firstCol = 0;
size_t lastCol = SIZE_MAX;
int sortType = SORT_CASESENSITIVE;
int sortOrder = SORT_ASCENDING;
if ( (numargs < 1) || (numargs > 7) || /* validate arguments */
!RXVALIDSTRING(args[0]))
return INVALID_ROUTINE;
/* remember stem name */
memset(stemName, 0, sizeof(stemName));
strcpy(stemName, args[0].strptr);
if (stemName[args[0].strlength-1] != '.')
stemName[args[0].strlength] = '.';
/* check other parameters */
if ((numargs >= 2) && RXVALIDSTRING(args[1])) /* sort order */
{
switch (args[1].strptr[0])
{
case 'A':
case 'a':
sortOrder = SORT_ASCENDING;
break;
case 'D':
case 'd':
sortOrder = SORT_DECENDING;
break;
default:
return INVALID_ROUTINE;
} /* endswitch */
} /* endif */
if ((numargs >= 3) && RXVALIDSTRING(args[2])) /* sort type */
{
switch (args[2].strptr[0])
{
case 'C':
case 'c':
sortType = SORT_CASESENSITIVE;
break;
case 'I':
case 'i':
sortType = SORT_CASEIGNORE;
break;
default:
return INVALID_ROUTINE;
} /* endswitch */
} /* endif */
if ((numargs >= 4) && RXVALIDSTRING(args[3])) /* first element to sort */
{
if (!string2size_t(args[3].strptr, &first))
return INVALID_ROUTINE;
if (first == 0)
return INVALID_ROUTINE;
} /* endif */
if ((numargs >= 5) && RXVALIDSTRING(args[4])) /* last element to sort */
{
if (!string2size_t(args[4].strptr, &last))
return INVALID_ROUTINE;
if (last < first)
return INVALID_ROUTINE;
} /* endif */
if ((numargs >= 6) && RXVALIDSTRING(args[5])) /* first column to sort */
{
if (!string2size_t(args[5].strptr, &firstCol))
return INVALID_ROUTINE;
firstCol--;
} /* endif */
if ((numargs == 7) && RXVALIDSTRING(args[6])) /* last column to sort */
{
if (!string2size_t(args[6].strptr, &lastCol))
return INVALID_ROUTINE;
lastCol--;
if (lastCol < firstCol)
return INVALID_ROUTINE;
} /* endif */
/* the sorting is done in the interpreter */
if (!RexxStemSort(stemName, sortOrder, sortType, first, last, firstCol, lastCol)) {
sprintf(retstr->strptr, "-1");
retstr->strlength = 2;
return INVALID_ROUTINE;
}
sprintf(retstr->strptr, "0");
retstr->strlength = 1;
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysStemDelete *
* *
* Syntax: result = SysStemDelete(stem, startitem [,itemcount]) *
* *
* Params: stem - name of stem where item will be deleted *
* startitem - index of item to delete *
* itemcount - number of items to delete if more than 1 *
* *
* Return: 0 - delete was successful *
* -1 - delete failed *
*************************************************************************/
RexxRoutine3(int, SysStemDelete, RexxStemObject, toStem, stringsize_t, start, OPTIONAL_stringsize_t, count)
{
if (argumentOmitted(3))
{
count = 1;
}
stringsize_t items;
RexxObjectPtr temp = context->GetStemArrayElement(toStem, 0);
if (temp == NULLOBJECT || !context->StringSize(temp, &items))
{
context->InvalidRoutine();
return 0;
}
// make sure the deletion site is within the bounds
if (start + count - 1 > items)
{
context->InvalidRoutine();
return 0;
}
stringsize_t index;
/* now copy the remaining indices up front */
for ( index = start; index + count <= items; index++)
{
// copy from the old index to the new index
RexxObjectPtr value = context->GetStemArrayElement(toStem, index + count);
// is this a sparse array?
if (value == NULLOBJECT)
{
// return this as a failure
return -1;
}
context->SetStemArrayElement(toStem, index, value);
}
/* now delete the items at the end */
for (index = items - count + 1; index <= items; index++)
{
context->DropStemArrayElement(toStem, index);
}
context->SetStemArrayElement(toStem, 0, context->StringSize(items - count));
return 0;
}
/*************************************************************************
* Function: SysStemInsert *
* *
* Syntax: result = SysStemInsert(stem, position, value) *
* *
* Params: stem - name of stem where item will be inserted *
* position - index where new item will be inserted *
* value - new item value *
* *
* Return: 0 - insert was successful *
* -1 - insert failed *
*************************************************************************/
RexxRoutine3(int, SysStemInsert, RexxStemObject, toStem, stringsize_t, position, RexxObjectPtr, newValue)
{
stringsize_t count;
RexxObjectPtr temp = context->GetStemArrayElement(toStem, 0);
if (temp == NULLOBJECT || !context->StringSize(temp, &count))
{
context->InvalidRoutine();
return 0;
}
/* check wether new position is within limits */
if (position == 0 || (position > count + 1))
{
context->InvalidRoutine();
return 0;
}
for (size_t index = count; index >= position; index--)
{
// copy from the old index to the new index
RexxObjectPtr value = context->GetStemArrayElement(toStem, index);
// is this a sparse array?
if (value == NULLOBJECT)
{
// return this as a failure
return -1;
}
context->SetStemArrayElement(toStem, index + 1, value);
}
// now set the new value
context->SetStemArrayElement(toStem, position, newValue);
return 0;
}
/*************************************************************************
* Function: SysStemCopy *
* *
* Syntax: result = SysStemCopy(fromstem, tostem, from, to, count *
* [,insert]) *
* *
* Params: fromstem - name of source stem *
* tostem - - name of target stem *
* from - first index in source stem to copy *
* to - position where items are copied/inserted in target stem*
* count - number of items to copy/insert *
* insert - 'I' to indicate insert instead of 'O' overwrite *
* *
* Return: 0 - stem copy was successful *
* -1 - stem copy failed *
*************************************************************************/
RexxRoutine6(int, SysStemCopy, RexxStemObject, fromStem, RexxStemObject, toStem,
OPTIONAL_stringsize_t, from, OPTIONAL_stringsize_t, to, OPTIONAL_stringsize_t, count,
OPTIONAL_CSTRING, option)
{
bool inserting = false;
/* get copy type */
if (option != NULL)
{
switch (*option)
{
case 'I':
case 'i':
inserting = true;
break;
case 'O':
case 'o':
inserting = false;
break;
default:
{
context->InvalidRoutine();
return 0;
}
}
}
stringsize_t fromCount;
RexxObjectPtr temp = context->GetStemArrayElement(fromStem, 0);
if (temp == NULLOBJECT || !context->StringSize(temp, &fromCount))
{
context->InvalidRoutine();
return 0;
}
// default from location is the first element
if (argumentOmitted(3))
{
from = 1;
}
if (argumentOmitted(4))
{
to = 1;
}
// was a count explicitly specified?
if (argumentExists(5))
{
// this must be in range
if ((count > (fromCount - from + 1)) || (fromCount == 0))
{
context->InvalidRoutine();
return 0;
}
}
else
{
// default is to copy everything from the starting position.
count = fromCount - from + 1;
}
stringsize_t toCount = 0;
// but if it is set, then use that value
temp = context->GetStemArrayElement(toStem, 0);
if (temp != NULLOBJECT && !context->StringSize(temp, &toCount))
{
context->InvalidRoutine();
return 0;
}
// copying out of range? Error
if (to > toCount + 1)
{
context->InvalidRoutine();
return 0;
}
if (inserting)
{
/* if we are about to insert the items we have to make room */
for (size_t index = toCount; index >= to; index--)
{
// copy from the old index to the new index
RexxObjectPtr value = context->GetStemArrayElement(toStem, index);
// is this a sparse array?
if (value == NULLOBJECT)
{
// return this as a failure
return -1;
}
context->SetStemArrayElement(toStem, index, value);
}
// set the new count value in the target
toCount += count;
context->SetStemArrayElement(toStem, 0, context->StringSize(toCount));
}
/* now do the actual copying from the source to target */
for (size_t index = 0; index < count; index++)
{
// just retrieve and copy
RexxObjectPtr value = context->GetStemArrayElement(fromStem, from + index);
// is this a sparse array?
if (value == NULLOBJECT)
{
// return this as a failure
return -1;
}
context->SetStemArrayElement(toStem, to + index, value);
}
// do we need to update the size?
if (to + count - 1 > toCount)
{
context->SetStemArrayElement(toStem, 0, context->StringSize(to + count - 1));
}
return 0;
}
/**************************************************************************
* Function: SysQueryProcess *
* *
* Params: "PID" - (default) returns current process ID *
* NEW: "PPID" - returns parent of current process ID *
* NEW: "PGID" - returns group ID of current process *
* NO "TID" - returns current thread ID *
* YES "PPRIO" - returns current process priority *
* NO "TPRIO" - returns current thread priority *
* YES "PTIME" - returns current process times *
* NO "TTIME" - returns current thread times *
* NEW: "PMEM" - returns current process max memory size RSS *
* NEW: "PSWAPS" - returns current process number of swaps out *
* NEW: "PRCVDSIG" - returns current process received signals *
***************************************************************************/
size_t RexxEntry SysQueryProcess(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
unsigned int uiUsedCPUTime = 0;
unsigned int uiUsedCPUmsec = 0;
unsigned int uiUsedHours = 0;
unsigned int uiUsedMinutes = 0;
unsigned int uiUsedSeconds = 0;
char timebuf[40];
int iRc = 0;
struct rusage struResUse;
retstr->strlength = 0; /* set return value */
if (numargs > 1) /* none or one argument accepted */
return INVALID_ROUTINE; /* raise error condition */
if ((numargs == 0) || (!strcasecmp(args[0].strptr, "PID")))
{
sprintf(retstr->strptr, "%d", getpid());
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PPID"))
{
sprintf(retstr->strptr, "%d", getppid());
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PGID"))
{
sprintf(retstr->strptr, "%d", getpgid(getpid()));
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PPRIO"))
{
sprintf(retstr->strptr, "%d", getpriority(PRIO_PROCESS, 0));
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
/* ----------------------------------------------------------------- */
/* Get process usage data and keep calls together at the end of */
/* the function SysQueryProcess. */
iRc = getrusage ( RUSAGE_SELF, &struResUse);
if ( iRc )
{
sprintf(retstr->strptr, " System error; errno = %d", errno);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
if (!strcasecmp(args[0].strptr, "PTIME")) /* Calculate the used CPU time*/
{
uiUsedCPUmsec = (unsigned int) struResUse.ru_utime.tv_usec/1000;
uiUsedCPUmsec += (unsigned int) struResUse.ru_stime.tv_usec/1000;
if (uiUsedCPUmsec >= 1000 )
{
uiUsedCPUTime = uiUsedCPUmsec / 1000;
uiUsedCPUmsec = uiUsedCPUmsec % 1000;
}
uiUsedCPUTime += (unsigned int) struResUse.ru_utime.tv_sec;
uiUsedCPUTime += (unsigned int) struResUse.ru_stime.tv_sec;
uiUsedHours = uiUsedCPUTime / 3600;
uiUsedMinutes = uiUsedCPUTime / 60;
if (uiUsedMinutes >= 60 ) uiUsedMinutes = uiUsedMinutes % 60;
if (uiUsedCPUTime >= 60 ) uiUsedSeconds = uiUsedCPUTime % 60;
else uiUsedSeconds = uiUsedCPUTime;
sprintf(retstr->strptr, "CPU_Time Summary: %2d:%.2d:%.2d:%.3d Kernel:",
uiUsedHours, uiUsedMinutes, uiUsedSeconds, uiUsedCPUmsec );
uiUsedCPUmsec = (unsigned int) struResUse.ru_stime.tv_usec/1000;
uiUsedCPUTime = (unsigned int) struResUse.ru_stime.tv_sec;
uiUsedHours = uiUsedCPUTime / 3600;
uiUsedMinutes = uiUsedCPUTime / 60;
if (uiUsedMinutes >= 60 ) uiUsedMinutes = uiUsedMinutes % 60;
if (uiUsedCPUTime >= 60 ) uiUsedSeconds = uiUsedCPUTime % 60;
else uiUsedSeconds = uiUsedCPUTime;
sprintf(timebuf, " %2d:%.2d:%.2d:%.3d User:", uiUsedHours,
uiUsedMinutes, uiUsedSeconds, uiUsedCPUmsec );
strcat(retstr->strptr, timebuf);
uiUsedCPUmsec = (unsigned int) struResUse.ru_utime.tv_usec/1000;
uiUsedCPUTime = (unsigned int) struResUse.ru_utime.tv_sec;
uiUsedHours = uiUsedCPUTime / 3600;
uiUsedMinutes = uiUsedCPUTime / 60;
if (uiUsedMinutes >= 60 ) uiUsedMinutes = uiUsedMinutes % 60;
if (uiUsedCPUTime >= 60 ) uiUsedSeconds = uiUsedCPUTime % 60;
else uiUsedSeconds = uiUsedCPUTime;
sprintf(timebuf, " %2d:%.2d:%.2d:%.3d", uiUsedHours,
uiUsedMinutes, uiUsedSeconds, uiUsedCPUmsec );
strcat(retstr->strptr, timebuf);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PMEM")) /* Show max memory RSS used */
{
sprintf(retstr->strptr, "Max_Memory_RSS: %ld", struResUse.ru_maxrss);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PSWAPS")) /* Memory has been swapped */
{
sprintf(retstr->strptr, "Memory_swaps: %ld", struResUse.ru_nswap);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
else
if (!strcasecmp(args[0].strptr, "PRCVDSIG")) /* Process received signals*/
{
sprintf(retstr->strptr, "Received_signals: %ld", struResUse.ru_nsignals);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE; /* no error on call */
}
return INVALID_ROUTINE; /* good completion */
}
/*************************************************************************
* Function: SysGetErrortext *
* *
* Syntax: call SysGetErrortext errnumber *
* *
* Params: errnumber - error number to be described *
* *
* Return: Description or empty string *
*************************************************************************/
size_t RexxEntry SysGetErrortext(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
int errnum = 0;
char *errmsg = NULL;
if (numargs != 1)
/* If no args, then its an */
/* incorrect call */
return INVALID_ROUTINE;
errnum = atoi(args[0].strptr);
errmsg = strerror( errnum );
if (errmsg == NULL )
retstr->strptr[0] = '\0';
else
{
if (strlen(errmsg) >= retstr->strlength)
retstr->strptr = (char *) malloc(strlen(errmsg + 1));
strcpy(retstr->strptr,errmsg);
}
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysUtilVersion *
* *
* Syntax: Say SysUtilVersion *
* *
* Return: REXXUTIL.DLL Version *
*************************************************************************/
size_t RexxEntry SysUtilVersion(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
{
if (numargs != 0) /* validate arg count */
return INVALID_ROUTINE;
/* format into the buffer */
sprintf(retstr->strptr, "%d.%d.%d", ORX_VER, ORX_REL, ORX_MOD);
retstr->strlength = strlen(retstr->strptr);
return VALID_ROUTINE;
}
/*************************************************************************
* Function: SysIsFile *
* *
* Syntax: call SysIsFile file *
* *
* Params: file - file to check existance of. *
* *
* Return: Logical. *
*************************************************************************/
RexxRoutine1(logical_t, SysIsFile, CSTRING, filename)
{
struct stat64 finfo; /* return buf for the finfo */
int rc = stat64(filename, &finfo); /* read the info about it */
// check the flag settings for a regular file
return rc == 0 && (S_ISREG(finfo.st_mode) || S_ISBLK(finfo.st_mode));
}
/*************************************************************************
* Function: SysIsFileDirectory *
* *
* Syntax: call SysIsFileDirectory dir *
* *
* Params: dir - dir to check existance of. *
* *
* Return: Logical. *
*************************************************************************/
RexxRoutine1(logical_t, SysIsFileDirectory, CSTRING, filename)
{
struct stat64 finfo; /* return buf for the finfo */
int rc = stat64(filename, &finfo); /* read the info about it */
return rc == 0 && S_ISDIR(finfo.st_mode);
}
/*************************************************************************
* Function: SysIsFileLink *
* *
* Syntax: call SysIsFileLink file *
* *
* Params: file - file to check if it is a Link (Alias). *
* *
* Return: Logical. *
*************************************************************************/
RexxRoutine1(logical_t, SysIsFileLink, CSTRING, filename)
{
struct stat64 finfo; /* return buf for the finfo */
int rc = lstat64(filename, &finfo); /* read the info about it */
return rc == 0 && S_ISLNK(finfo.st_mode);
}
/*************************************************************************
* Function: SysFileExists *
* *
* Syntax: call SysFileExists *
* *
* Params: dir - file or dir to check existance of *
* *
* Return: Logical. *
*************************************************************************/
RexxRoutine1(logical_t, SysFileExists, CSTRING, filename)
{
struct stat64 finfo; /* return buf for the finfo */
return stat64(filename, &finfo) == 0; /* read the info about it */
}
#ifdef XX__cplusplus
}
#endif
// now build the actual entry list
RexxRoutineEntry rexxutil_routines[] =
{
REXX_TYPED_ROUTINE(SysCreateMutexSem, SysCreateMutexSem),
REXX_TYPED_ROUTINE(SysOpenMutexSem, SysOpenMutexSem),
REXX_TYPED_ROUTINE(SysCloseMutexSem, SysCloseMutexSem),
REXX_TYPED_ROUTINE(SysRequestMutexSem, SysRequestMutexSem),
REXX_TYPED_ROUTINE(SysReleaseMutexSem, SysReleaseMutexSem),
REXX_TYPED_ROUTINE(SysCreateEventSem, SysCreateEventSem),
REXX_TYPED_ROUTINE(SysOpenEventSem, SysOpenEventSem),
REXX_TYPED_ROUTINE(SysCloseEventSem, SysCloseEventSem),
REXX_TYPED_ROUTINE(SysResetEventSem, SysResetEventSem),
REXX_TYPED_ROUTINE(SysPostEventSem, SysPostEventSem),
REXX_TYPED_ROUTINE(SysWaitEventSem, SysWaitEventSem),
REXX_CLASSIC_ROUTINE(SysSetPriority, SysSetPriority),
REXX_CLASSIC_ROUTINE(SysAddRexxMacro, SysAddRexxMacro),
REXX_CLASSIC_ROUTINE(SysDropRexxMacro, SysDropRexxMacro),
REXX_CLASSIC_ROUTINE(SysReorderRexxMacro, SysReorderRexxMacro),
REXX_CLASSIC_ROUTINE(SysQueryRexxMacro, SysQueryRexxMacro),
REXX_CLASSIC_ROUTINE(SysClearRexxMacroSpace, SysClearRexxMacroSpace),
REXX_CLASSIC_ROUTINE(SysLoadRexxMacroSpace, SysLoadRexxMacroSpace),
REXX_CLASSIC_ROUTINE(SysSaveRexxMacroSpace, SysSaveRexxMacroSpace),
#if defined(AIX_DISABLED)
REXX_CLASSIC_ROUTINE(SysAddFuncPkg, SysAddFuncPkg),
REXX_CLASSIC_ROUTINE(SysAddCmdPkg, SysAddCmdPkg),
REXX_CLASSIC_ROUTINE(SysDropFuncPkg, SysDropFuncPkg),
REXX_CLASSIC_ROUTINE(SysDropCmdPkg, SysDropCmdPkg),
#endif
#if defined(AIX)
REXX_CLASSIC_ROUTINE(SysGetpid, SysGetpid),
#endif
REXX_CLASSIC_ROUTINE(SysFork, SysFork),
REXX_CLASSIC_ROUTINE(SysWait, SysWait),
REXX_CLASSIC_ROUTINE(SysCreatePipe, SysCreatePipe),
REXX_CLASSIC_ROUTINE(SysCls, SysCls),
REXX_CLASSIC_ROUTINE(SysDropFuncs, SysDropFuncs),
REXX_CLASSIC_ROUTINE(SysFileDelete, SysFileDelete),
REXX_CLASSIC_ROUTINE(SysFileSearch, SysFileSearch),
REXX_CLASSIC_ROUTINE(SysFileTree, SysFileTree),
REXX_CLASSIC_ROUTINE(SysGetKey, SysGetKey),
REXX_CLASSIC_ROUTINE(SysGetMessage, SysGetMessage),
REXX_CLASSIC_ROUTINE(SysGetMessageX, SysGetMessageX),
REXX_CLASSIC_ROUTINE(SysLoadFuncs, SysLoadFuncs),
REXX_CLASSIC_ROUTINE(SysMkDir, SysMkDir),
#ifdef LINUX
REXX_CLASSIC_ROUTINE(SysLinVer, SysLinVer),
#endif
REXX_CLASSIC_ROUTINE(SysVersion, SysVersion),
REXX_CLASSIC_ROUTINE(SysRmDir, SysRmDir),
REXX_CLASSIC_ROUTINE(SysSearchPath, SysSearchPath),
REXX_CLASSIC_ROUTINE(SysSleep, SysSleep),
REXX_CLASSIC_ROUTINE(SysTempFileName, SysTempFileName),
REXX_CLASSIC_ROUTINE(SysDumpVariables, SysDumpVariables),
REXX_CLASSIC_ROUTINE(SysSetFileDateTime, SysSetFileDateTime),
REXX_CLASSIC_ROUTINE(SysGetFileDateTime, SysGetFileDateTime),
REXX_CLASSIC_ROUTINE(SysStemSort, SysStemSort),
REXX_TYPED_ROUTINE(SysStemDelete, SysStemDelete),
REXX_TYPED_ROUTINE(SysStemInsert, SysStemInsert),
REXX_TYPED_ROUTINE(SysStemCopy, SysStemCopy),
REXX_CLASSIC_ROUTINE(SysQueryProcess, SysQueryProcess),
REXX_CLASSIC_ROUTINE(SysGetErrortext, SysGetErrortext),
REXX_CLASSIC_ROUTINE(SysUtilVersion, SysUtilVersion),
REXX_TYPED_ROUTINE(SysIsFile, SysIsFile),
REXX_TYPED_ROUTINE(SysIsFileDirectory, SysIsFileDirectory),
REXX_TYPED_ROUTINE(SysIsFileLink, SysIsFileLink),
REXX_TYPED_ROUTINE(SysFileExists, SysFileExists),
REXX_LAST_ROUTINE()
};
RexxPackageEntry rexxutil_package_entry =
{
STANDARD_PACKAGE_HEADER
REXX_INTERPRETER_4_0_0, // anything after 4.0.0 will work
"REXXUTIL", // name of the package
"4.0", // package information
NULL, // no load/unload functions
NULL,
rexxutil_routines, // the exported functions
NULL // no methods in this package
};
// package loading stub.
OOREXX_GET_PACKAGE(rexxutil);