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
|
/* $EPIC: ircaux.c,v 1.109 2006/10/19 22:25:29 jnelson Exp $ */
/*
* ircaux.c: some extra routines... not specific to irc... that I needed
*
* Copyright (c) 1990 Michael Sandroff.
* Copyright (c) 1991, 1992 Troy Rollo.
* Copyright (c) 1992-1996 Matthew Green.
* Copyright 1994 Jake Khuon.
* Copyright 1993, 2003 EPIC Software Labs.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notices, the above paragraph (the one permitting redistribution),
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "irc.h"
#include "screen.h"
#include <pwd.h>
#include <sys/wait.h>
#include <math.h>
#include "ircaux.h"
#include "output.h"
#include "term.h"
#include "vars.h"
#include "alias.h"
#include "if.h"
#include "words.h"
#include "ctcp.h"
/*
* This is the basic overhead for every malloc allocation (8 bytes).
* The size of the allocation and a "magic" sequence are retained here.
* This is in addition to any overhead the underlying malloc package
* may impose -- this may mean that malloc()s may be actually up to
* 20 to 30 bytes larger than you request.
*/
typedef struct _mo_money
{
unsigned magic;
int size;
#ifdef ALLOC_DEBUG
unsigned entry;
char* fn;
int line;
#endif
} MO;
#ifdef ALLOC_DEBUG
struct {
int size;
void** entries;
} alloc_table = { 0, NULL };
#endif
#define mo_ptr(ptr) ((MO *)( (char *)(ptr) - sizeof(MO) ))
#define alloc_size(ptr) ((mo_ptr(ptr))->size)
#define magic(ptr) ((mo_ptr(ptr))->magic)
#define FREED_VAL -3
#define ALLOC_MAGIC (unsigned long)0x7fbdce70
/*
* This was all imported by pre3 for no reason other than to track down
* that blasted bug that splitfire is tickling.
*/
#define NO_ERROR 0
#define ALLOC_MAGIC_FAILED 1
#define ALREADY_FREED 2
static int malloc_check (void *ptr)
{
if (!ptr)
return ALLOC_MAGIC_FAILED;
if (magic(ptr) != ALLOC_MAGIC)
return ALLOC_MAGIC_FAILED;
if (alloc_size(ptr) == FREED_VAL)
return ALREADY_FREED;
#ifdef ALLOC_DEBUG
if (ptr != alloc_table.entries[mo_ptr(ptr)->entry])
return ALREADY_FREED;
#endif
return NO_ERROR;
}
void fatal_malloc_check (void *ptr, const char *special, const char *fn, int line)
{
static int recursion = 0;
switch (malloc_check(ptr))
{
case ALLOC_MAGIC_FAILED:
{
if (recursion)
abort();
recursion++;
privileged_yell("IMPORTANT! MAKE SURE TO INCLUDE ALL OF THIS INFORMATION"
" IN YOUR BUG REPORT!");
privileged_yell("MAGIC CHECK OF MALLOCED MEMORY FAILED!");
if (special)
privileged_yell("Because of: [%s]", special);
if (ptr)
{
privileged_yell("Address: [%p] Size: [%d] Magic: [%x] "
"(should be [%lx])",
ptr, alloc_size(ptr), magic(ptr), ALLOC_MAGIC);
privileged_yell("Dump: [%s]", prntdump(ptr, alloc_size(ptr)));
}
else
privileged_yell("Address: [NULL]");
privileged_yell("IMPORTANT! MAKE SURE TO INCLUDE ALL OF THIS INFORMATION"
" IN YOUR BUG REPORT!");
panic("BE SURE TO INCLUDE THE ABOVE IMPORTANT INFORMATION! "
"-- new_free()'s magic check failed from [%s/%d].",
fn, line);
}
case ALREADY_FREED:
{
if (recursion)
abort();
recursion++;
panic("free()d the same address twice from [%s/%d].",
fn, line);
}
case NO_ERROR:
return;
}
}
/*
* really_new_malloc is the general interface to the malloc(3) call.
* It is only called by way of the ``new_malloc'' #define.
* It wont ever return NULL.
*/
void * really_new_malloc (size_t size, const char *fn, int line)
{
char *ptr;
if (!(ptr = (char *)malloc(size + sizeof(MO))))
panic("Malloc() failed from [%s/%d], giving up!", fn, line);
/* Store the size of the allocation in the buffer. */
ptr += sizeof(MO);
magic(ptr) = ALLOC_MAGIC;
alloc_size(ptr) = size;
#ifdef ALLOC_DEBUG
mo_ptr(ptr)->entry = alloc_table.size;
mo_ptr(ptr)->fn = fn;
mo_ptr(ptr)->line = line;
alloc_table.size++;
alloc_table.entries = realloc(alloc_table.entries, (alloc_table.size) * sizeof(void**));
alloc_table.entries[alloc_table.size-1] = ptr;
#endif
return ptr;
}
#ifdef DELAYED_FREES
/*
* Instead of calling free() directly in really_new_free(), we instead
* delay that until the stack has been unwound completely. This masks the
* many bugs in epic where we hold a pointer to some object (such as a DCC
* item) and then invoke a sequence point. When that sequence point returns
* we cannot assume that pointer is still valid. But regretably we assume
* that so often that we can't just sweep this problem away. The rules
* regarding double and invalid frees stays because that is all done at a
* higher level. The only thing this changes is that we do not release
* memory until it is impossible that we could be holding a pointer to it.
* This does not fix those bugs, only mitigates their damaging effect.
*/
int need_delayed_free = 0;
static void ** delayed_free_table;
static int delayed_free_table_size = 0;
static int delayed_frees = 0;
void do_delayed_frees (void)
{
int i;
for (i = 0; i < delayed_frees; i++)
{
free((void *)delayed_free_table[i]);
delayed_free_table[i] = NULL;
}
delayed_frees = 0;
need_delayed_free = 0;
}
static void delay_free (void *ptr)
{
need_delayed_free = 1;
if (delayed_frees >= delayed_free_table_size - 2)
{
int i = delayed_free_table_size;
if (delayed_free_table_size)
delayed_free_table_size *= 2;
else
delayed_free_table_size = 128;
RESIZE(delayed_free_table, void *, delayed_free_table_size);
for (; i < delayed_free_table_size; i++)
delayed_free_table[i] = NULL;
}
delayed_free_table[delayed_frees++] = ptr;
}
#endif
/*
* really_new_free is the general interface to the free(3) call.
* It is only called by way of the ``new_free'' #define.
* You must always use new_free to free anything youve allocated
* with new_malloc, or all heck will break loose.
*/
void * really_new_free (void **ptr, const char *fn, int line)
{
if (*ptr)
{
fatal_malloc_check(*ptr, NULL, fn, line);
alloc_size(*ptr) = FREED_VAL;
#ifdef ALLOC_DEBUG
alloc_table.entries[mo_ptr(*ptr)->entry]
= alloc_table.entries[--alloc_table.size];
mo_ptr(alloc_table.entries[mo_ptr(*ptr)->entry])->entry = mo_ptr(*ptr)->entry;
#endif
#ifdef DELAYED_FREES
delay_free((void *)(mo_ptr(*ptr)));
#else
free((void *)(mo_ptr(*ptr)));
#endif
}
return ((*ptr = NULL));
}
#if 1
/* really_new_malloc in disguise */
void * really_new_realloc (void **ptr, size_t size, const char *fn, int line)
{
void *newptr = NULL;
if (!size)
*ptr = really_new_free(ptr, fn, line);
else if (!*ptr)
*ptr = really_new_malloc(size, fn, line);
else
{
/* Make sure this is safe for realloc. */
fatal_malloc_check(*ptr, NULL, fn, line);
/* Copy everything, including the MO buffer */
if ((newptr = (char *)realloc(mo_ptr(*ptr), size + sizeof(MO))))
{
*ptr = newptr;
} else {
new_free(ptr);
panic("realloc() failed from [%s/%d], giving up!", fn, line);
}
/* Re-initalize the MO buffer; magic(*ptr) is already set. */
*ptr = (void *)((char *)*ptr + sizeof(MO));
alloc_size(*ptr) = size;
#ifdef ALLOC_DEBUG
alloc_table.entries[mo_ptr(*ptr)->entry] = *ptr;
#endif
}
return *ptr;
}
#else
void * new_realloc (void **ptr, size_t size)
{
char *ptr2 = NULL;
size_t foo,bar;
/* Yes the function is ifdefed out, but this serves as a proof of concept. */
for (foo=1, bar=size+sizeof(MO); bar; foo<<=1, bar>>=1) /* Nothing */ ;
if (foo) {size=foo;}
if (*ptr)
{
if (size)
{
size_t msize = alloc_size(*ptr);
if (msize >= size)
return *ptr;
ptr2 = new_malloc(size);
memmove(ptr2, *ptr, msize);
}
new_free(ptr);
}
else if (size)
ptr2 = new_malloc(size);
return ((*ptr = ptr2));
}
#endif
void malloc_dump (const char *file) {
#ifdef ALLOC_DEBUG
int foo, bar;
FILE *fd;
if (!(file && *file && (fd=fopen(file, "a"))))
fd=stdout;
for (foo = alloc_table.size; foo--;) {
fprintf(fd, "%s/%d\t%d\t", mo_ptr(alloc_table.entries[foo])->fn, mo_ptr(alloc_table.entries[foo])->line, mo_ptr(alloc_table.entries[foo])->size);
for (bar = 0; bar<mo_ptr(alloc_table.entries[foo])->size; bar++)
fprintf(fd, " %x", (unsigned char)(((char*)(alloc_table.entries[foo]))[bar]));
fprintf(fd, "\n");
}
fclose(fd);
#endif
}
char * upper (char *str)
{
char *ptr = (char *) 0;
if (str)
{
ptr = str;
for (; *str; str++)
{
if (islower(*str))
*str = toupper(*str);
}
}
return (ptr);
}
char * lower (char *str)
{
char *ptr = NULL;
if (str)
{
ptr = str;
for (; *str; str++)
{
if (isupper(*str))
*str = tolower(*str);
}
}
return ptr;
}
/* case insensitive string searching */
ssize_t stristr (const char *start, const char *srch)
{
int x = 0;
const char *source = start;
if (!source || !*source || !srch || !*srch || strlen(source) < strlen(srch))
return -1;
while (*source)
{
if (source[x] && toupper(source[x]) == toupper(srch[x]))
x++;
else if (srch[x])
source++, x = 0;
else
return source - start;
}
return -1;
}
/* case insensitive string searching from the end */
ssize_t rstristr (const char *start, const char *srch)
{
const char *ptr;
int x = 0;
if (!start || !*start || !srch || !*srch || strlen(start) < strlen(srch))
return -1;
ptr = start + strlen(start) - strlen(srch);
while (ptr >= start)
{
if (!srch[x])
return ptr - start;
if (toupper(ptr[x]) == toupper(srch[x]))
x++;
else
ptr--, x = 0;
}
return -1;
}
char * remove_trailing_spaces (char *foo, size_t *cluep)
{
char *end;
size_t clue = cluep?*cluep:0;
if (!*foo)
return foo;
end = clue + foo + strlen(clue + foo) - 1;
while (end > foo && my_isspace(*end))
end--;
/* If this is a \, then it was a \ before a space. Go forward */
if (end[0] == '\\' && my_isspace(end[1]))
end++;
end[1] = 0;
if (cluep)
*cluep = end - foo;
return foo;
}
char * forcibly_remove_trailing_spaces (char *foo, size_t *cluep)
{
char *end;
size_t clue = cluep?*cluep:0;
if (!*foo)
return foo;
end = clue + foo + strlen(clue + foo) - 1;
while (end > foo && my_isspace(*end))
end--;
/* Do not save spaces after \ at end of words! */
end[1] = 0;
if (cluep)
*cluep = end - foo;
return foo;
}
char * next_in_comma_list (char *str, char **after)
{
return next_in_div_list(str, after, ',');
}
char * next_in_div_list (char *str, char **after, char delim)
{
*after = str;
while (*after && **after && **after != delim)
(*after)++;
if (*after && **after == delim)
{
**after = 0;
(*after)++;
}
return str;
}
/* Find the next instance of 'what' that isn't backslashed. */
char * findchar (char *str, int what)
{
char *p;
for (p = str; *p; p++)
{
if (p[0] == '\\' && p[1])
p++;
else if (p[0] == what)
break;
}
if (*p == what)
return p;
else
return NULL;
}
unsigned char stricmp_table [] =
{
0, 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, 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, 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
};
/* my_stricmp: case insensitive version of strcmp */
int my_stricmp (const unsigned char *str1, const unsigned char *str2)
{
while (*str1 && *str2 && (stricmp_table[(unsigned short)*str1] == stricmp_table[(unsigned short)*str2]))
str1++, str2++;
return (stricmp_table[(unsigned short)*str1] -
stricmp_table[(unsigned short)*str2]);
}
/* my_strnicmp: case insensitive version of strncmp */
int my_strnicmp (const unsigned char *str1, const unsigned char *str2, size_t n)
{
while (n && *str1 && *str2 && (stricmp_table[(unsigned short)*str1] == stricmp_table[(unsigned short)*str2]))
str1++, str2++, n--;
return (n ?
(stricmp_table[(unsigned short)*str1] -
stricmp_table[(unsigned short)*str2]) : 0);
}
/* chop -- chops off the last character. capiche? */
char * chop (char *stuff, size_t nchar)
{
size_t sl = strlen(stuff);
if (nchar > 0 && sl > 0 && nchar <= sl)
stuff[sl - nchar] = 0;
else if (nchar > sl)
stuff[0] = 0;
return stuff;
}
/*
* strext: Makes a copy of the string delmited by two char pointers and
* returns it in malloced memory. Useful when you dont want to munge up
* the original string with a null. end must be one place beyond where
* you want to copy, ie, its the first character you dont want to copy.
*/
char * strext (const char *start, const char *end)
{
char *ptr, *retval;
ptr = retval = (char *)new_malloc(end-start+1);
while (start < end)
*ptr++ = *start++;
*ptr = 0;
return retval;
}
char * strlopencat (char *dest, size_t maxlen, ...)
{
va_list args;
int size;
char * this_arg = NULL;
size_t this_len;
char * endp;
char * p;
size_t worklen;
endp = dest + maxlen; /* This better not be an error */
size = strlen(dest); /* Find the end of the string */
p = dest + size; /* We will start catting there */
va_start(args, maxlen);
for (;;)
{
/* Grab the next string, stop if no more */
if (!(this_arg = va_arg(args, char *)))
break;
this_len = strlen(this_arg); /* How much do we need? */
worklen = endp - p; /* How much do we have? */
/* If not enough space, copy what we can and stop */
if (this_len > worklen)
{
strlcpy(p, this_arg, worklen);
break; /* No point in continuing */
}
/* Otherwise, we have enough space, copy it */
p += strlcpy(p, this_arg, endp - p);
}
va_end(args);
return dest;
}
/*
* malloc_strcat_ues_c: Just as with malloc_strcat, append 'src' to the end
* of '*dest', optionally dequoting (not copying backslashes from) 'src'
* pursuant to the following rules:
*
* special == empty_string De-quote all characters (remove all \'s)
* special == NULL De-quote nothing ('src' is literal text)
* special is anything else De-quote only \X where X is one of the
* characters in 'special'.
*
* Examples where: dest == "one" and src == "t\w\o"
* special == empty_string result is "one two"
* (remove all \'s)
* special == NULL result is "one t\w\o"
* (remove no \'s)
* special == "lmnop" result is "one t\wo"
* (remove the \ before o, but not w,
* because "o" is in "lmnop")
*
* "ues" stands for "UnEscape Special" and was written to replace the old
* ``strmcat_ue'' which had string length limit problems. "_c" of course
* means this function takes a string length clue. The previous name,
* 'm_strcat_ues_c' was changed becuase ISO C does not allow user symbols
* to start with ``m_''.
*
* Just as with 'malloc_strcat', 'src' may be NULL and this function will
* no-op (as opposed to crashing)
*
* The technique we use here is a hack, and it's expensive, but it works.
* 1) Copy 'src' into a temporary buffer, removing any \'s as proscribed
* 2) Append the temporary buffer to '*dest'
*
* NOTES: This is the "dequoter", also known as "Quoting Hell". Everything
* that removes \'s uses this function to do it.
*/
char * malloc_strcat_ues_c (char **dest, const char *src, const char *special, size_t *cluep)
{
char *workbuf, *p;
const char *s;
/* If there is nothing to copy, just stop right here. */
if (!src || !*src)
return *dest;
/* If we're not dequoting, cut it short and return. */
if (special == NULL)
{
malloc_strcat_c(dest, src, cluep);
return *dest;
}
/*
* Set up a working buffer for our copy.
* Reserve two extra spaces because the algorithm below
* may copy two nuls to 'workbuf', and we need the space
* for the second nul.
*/
workbuf = alloca(strlen(src) + 2);
/* Walk 'src' looking for characters to dequote */
for (s = src, p = workbuf; ; s++, p++)
{
/*
* If we see a backslash, it is not at the end of the
* string, and the character after it is contained in
* 'special', then skip the backslash.
*/
if (*s == '\\')
{
/*
* If we are doing special dequote handling,
* and the \ is not at the end of the string,
* and the character after it is contained
* within ``special'', skip the \.
*/
if (special != empty_string)
{
/*
* If this character is handled by 'special', then
* copy the next character and either continue to
* the next character or stop if we're done.
*/
if (s[1] && strchr(special, s[1]))
{
if ((*p = *++s) == 0)
break;
else
continue;
/* NOTREACHED */
}
}
/*
* BACKWARDS COMPATABILITY:
* In any case where \n, \p, \r, and \0 are not
* explicitly caught by 'special', we have to
* convert them to \020 (dle) to maintain backwards
* compatability.
*/
if (s[1] == 'n' || s[1] == 'p' ||
s[1] == 'r' || s[1] == '0')
{
s++; /* Skip the \ */
*p = '\020'; /* Copy a \n */
continue;
}
/*
* So it is not handled by 'special' and it is not
* a legacy escape. So we either need to copy or ignore
* this \ based on the value of special. If "special"
* is empty_string, we remove it. Otherwise, we keep it.
*/
if (special == empty_string)
s++;
/*
* Copy this character (or in the above case, the character
* after the \). If we copy a nul, then immediately stop the
* process here!
*/
if ((*p = *s) == 0)
break;
}
/*
* Always copy any non-slash character.
* Stop when we reach the nul.
*/
else
if ((*p = *s) == 0)
break;
}
/*
* We're done! Append 'workbuf' to 'dest'.
*/
malloc_strcat_c(dest, workbuf, cluep);
return *dest;
}
/*
* normalize_filename: replacement for expand_twiddle
*
* This is a front end to realpath(), has the same signature, except
* it expands twiddles for me, and it returns 0 or -1 instead of (char *).
*/
int normalize_filename (const char *str, Filename result)
{
Filename workpath;
char * pathname;
char * rest;
struct passwd *entry;
if (*str == '~')
{
/* make a copy of the path we can make changes to */
pathname = LOCAL_COPY(str + 1);
/* Stop the tilde-expansion at the first / (or nul) */
if ((rest = strchr(pathname, '/')))
*rest++ = 0;
/* Expand ~ to our homedir, ~user to user's homedir */
if (*pathname) {
if ((entry = getpwnam(pathname)) == NULL) {
snprintf(result, MAXPATHLEN + 1, "~%s", pathname);
return -1;
}
strlcpy(workpath, entry->pw_dir, sizeof(workpath));
} else
strlcpy(workpath, my_path, sizeof(workpath));
/* And tack on whatever is after the first / */
if (rest)
{
strlcat(workpath, "/", sizeof(workpath));
strlcat(workpath, rest, sizeof(workpath));
}
str = workpath;
}
if (realpath(str, result) == NULL)
return -1;
return 0;
}
/*
* expand_twiddle: expands ~ in pathnames.
*
* XXX WARNING XXX
*
* It is perfectly valid for (str == *result)! You must NOT change
* '*result' until you have first copied 'str' to 'buffer'! If you
* do not do this, you will corrupt the result! You have been warned!
*/
int expand_twiddle (const char *str, Filename result)
{
Filename buffer;
char *rest;
struct passwd *entry;
/* Handle filenames without twiddles to expand */
if (*str != '~')
{
/* Only do the copy if the destination is not the source */
if (str != result)
strlcpy(result, str, MAXPATHLEN + 1);
return 0;
}
/* Handle filenames that are just ~ or ~/... */
str++;
if (!*str || *str == '/')
{
strlcpy(buffer, my_path, sizeof(buffer));
strlcat(buffer, str, sizeof(buffer));
strlcpy(result, buffer, MAXPATHLEN + 1);
return 0;
}
/* Handle filenames that are ~user or ~user/... */
if ((rest = strchr(str, '/')))
*rest++ = 0;
if ((entry = getpwnam(str)))
{
strlcpy(buffer, entry->pw_dir, sizeof(buffer));
if (rest)
{
strlcat(buffer, "/", sizeof(buffer));
strlcat(buffer, rest, sizeof(buffer));
}
strlcpy(result, buffer, MAXPATHLEN + 1);
return 0;
}
return -1;
}
/* islegal: true if c is a legal nickname char anywhere but first char */
#define islegal(c) ((((c) >= 'A') && ((c) <= '}')) || \
(((c) >= '0') && ((c) <= '9')) || \
((c) == '-') || ((c) == '_'))
/*
* check_nickname: checks is a nickname is legal. If the first character is
* bad, null is returned. If the first character is not bad, the string is
* truncated down to only legal characters and returned
*
* rewritten, with help from do_nick_name() from the server code (2.8.5),
* phone, april 1993.
*/
char * check_nickname (char *nick, int unused)
{
char *s;
if (!nick || *nick == '-' || isdigit(*nick))
return NULL;
for (s = nick; *s && (s - nick) < NICKNAME_LEN; s++)
if (!islegal(*s) || my_isspace(*s))
break;
*s = '\0';
return *nick ? nick : NULL;
}
/*
* sindex: much like index(), but it looks for a match of any character in
* the group, and returns that position. If the first character is a ^, then
* this will match the first occurence not in that group.
*
* XXXX - sindex is a lot like strpbrk(), which is standard
*/
char * sindex (char *string, const char *group)
{
const char *ptr;
if (!string || !group)
return (char *) NULL;
if (*group == '^')
{
group++;
for (; *string; string++)
{
for (ptr = group; *ptr; ptr++)
{
if (*ptr == *string)
break;
}
if (*ptr == '\0')
return string;
}
}
else
{
for (; *string; string++)
{
for (ptr = group; *ptr; ptr++)
{
if (*ptr == *string)
return string;
}
}
}
return (char *) NULL;
}
/*
* rsindex: much like rindex(), but it looks for a match of any character in
* the group, and returns that position. If the first character is a ^, then
* this will match the first occurence not in that group.
*/
char * rsindex (char *string, char *start, char *group, int howmany)
{
char *ptr;
if (howmany && string && start && group && start <= string)
{
if (*group == '^')
{
group++;
for (ptr = string; (ptr >= start) && howmany; ptr--)
{
if (!strchr(group, *ptr))
{
if (--howmany == 0)
return ptr;
}
}
}
else
{
for (ptr = string; (ptr >= start) && howmany; ptr--)
{
if (strchr(group, *ptr))
{
if (--howmany == 0)
return ptr;
}
}
}
}
return NULL;
}
/* is_number: returns true if the given string is a number, false otherwise */
int is_number (const char *str)
{
if (!str || !*str)
return 0;
while (*str && isspace(*str))
str++;
if (*str == '-')
str++;
if (*str)
{
for (; *str; str++)
{
if (!isdigit((*str)))
return (0);
}
return 1;
}
else
return 0;
}
/* is_number: returns 1 if the given string is a real number, 0 otherwise */
int is_real_number (const char *str)
{
int period = 0;
if (!str || !*str)
return 0;
while (*str && isspace(*str))
str++;
if (*str == '-')
str++;
if (!*str)
return 0;
for (; *str; str++)
{
if (isdigit((*str)))
continue;
if (*str == '.' && period == 0)
{
period = 1;
continue;
}
return 0;
}
return 1;
}
/*
* path_search: given a file called name, this will search each element of
* the given path to locate the file. If found in an element of path, the
* full path name of the file is returned in a static string. If not, null
* is returned. Path is a colon separated list of directories
*/
int path_search (const char *name, const char *xpath, Filename result)
{
Filename buffer;
Filename candidate;
char *path;
char *ptr;
/* No Path -> Error */
if (!xpath)
return -1; /* Take THAT! */
/*
* A "relative" path is valid if the file exists
* based on the current directory. If it does not
* exist from the current directory, then we will
* search the path for it.
*
* PLEASE NOTE this catches things like ~foo/bar too!
*/
if (strchr(name, '/'))
{
if (!normalize_filename(name, candidate))
{
if (file_exists(candidate))
{
strlcpy(result, candidate, MAXPATHLEN + 1);
return 0;
}
}
}
/*
* The previous check already took care of absolute paths
* that exist, so we need to check for absolute paths here
* that DON'T exist. (is this cheating?). Also, ~user/foo
* is considered an "absolute path".
*/
if (*name == '/' || *name == '~')
return -1;
*result = 0;
for (path = LOCAL_COPY(xpath); path; path = ptr)
{
if ((ptr = strchr(path, ':')))
*ptr++ = 0;
snprintf(buffer, sizeof(buffer), "%s/%s", path, name);
if (normalize_filename(buffer, candidate))
continue;
if (file_exists(candidate)) {
strlcpy(result, candidate, MAXPATHLEN + 1);
return 0;
}
}
return -1;
}
/*
* double_quote: Given a str of text, this will quote any character in the
* set stuff with the QUOTE_CHAR. You have to pass in a buffer thats at
* least twice the size of 'str' (in case every character is quoted.)
* "output" is returned for your convenience.
*/
char * double_quote (const char *str, const char *stuff, char *buffer)
{
char c;
int pos;
*buffer = 0; /* Whatever */
if (!stuff)
return buffer; /* Whatever */
for (pos = 0; (c = *str); str++)
{
if (strchr(stuff, c))
{
if (c == '$')
buffer[pos++] = '$';
else
buffer[pos++] = '\\';
}
buffer[pos++] = c;
}
buffer[pos] = '\0';
return buffer;
}
void panic (const char *format, ...)
{
char buffer[BIG_BUFFER_SIZE * 10 + 1];
static int recursion = 0; /* Recursion is bad */
if (recursion)
abort();
recursion = 1;
if (format)
{
va_list arglist;
va_start(arglist, format);
vsnprintf(buffer, BIG_BUFFER_SIZE * 10, format, arglist);
va_end(arglist);
}
term_reset();
fprintf(stderr, "An unrecoverable logic error has occurred.\n");
fprintf(stderr, "Please fill out the BUG_FORM file, and include the following message:\n");
fprintf(stderr, "Panic: [%s (%lu):%s]\n", irc_version, commit_id, buffer);
panic_dump_call_stack();
if (x_debug & DEBUG_CRASH)
irc_exit(0, "EPIC Panic: %s (%lu):%s", irc_version, commit_id, buffer);
else
irc_exit(1, "EPIC Panic: %s (%lu):%s", irc_version, commit_id, buffer);
}
/* beep_em: Not hard to figure this one out */
void beep_em (int beeps)
{
int cnt,
i;
for (cnt = beeps, i = 0; i < cnt; i++)
term_beep();
}
/* Not really complicated, but a handy function to have */
int end_strcmp (const char *val1, const char *val2, size_t bytes)
{
if (bytes < strlen(val1))
return (strcmp(val1 + strlen(val1) - (size_t) bytes, val2));
else
return -1;
}
/*
* exec a program, given its arguments and input, return its entire output.
* on call, *len is the length of input, and on return, it is set to the
* length of the data returned.
*
* Reading is done more agressively than writing to keep the buffers
* clean, and the data flowing.
*
* Potential Bugs:
* - If the program in question locks up for any reason, so will epic.
* This can be fixed with an appropriate timeout in the select call.
* - If the program in question outputs enough data, epic will run out
* of memory and dump core.
* - Although input and the return values are char*'s, they are only
* treated as blocks of data, the size of *len.
*
* Special Note: stdin and stdout are not expected to be textual.
*/
char* exec_pipe (const char *executable, char *input, size_t *len, char * const *args)
{
int pipe0[2] = {-1, -1};
int pipe1[2] = {-1, -1};
pid_t pid;
char * ret = NULL;
size_t retlen = 0, rdpos = 0, wrpos = 0;
fd_set rdfds, wrfds;
int fdmax;
if (pipe(pipe0) || pipe(pipe1))
{
yell("Cannot open pipes for %s: %s",
executable, strerror(errno));
close(pipe0[0]);
close(pipe0[1]);
close(pipe1[0]);
close(pipe1[1]);
return ret;
}
switch (pid = fork())
{
case -1:
yell("Cannot fork for %s: %s",
executable, strerror(errno));
close(pipe0[0]);
close(pipe0[1]);
close(pipe1[0]);
close(pipe1[1]);
return ret;
case 0:
dup2(pipe0[0], 0);
dup2(pipe1[1], 1);
close(pipe0[1]);
close(pipe1[0]);
close(2); /* we dont want to see errors yet */
setuid(getuid());
setgid(getgid());
execvp(executable, args);
#if 0
/*
* OK, well the problem with this is that the message
* is going to go out on stdout right, and where does
* that end up?
*/
yell("Cannot exec %s: %s",
executable, strerror(errno));
#endif
_exit(0);
default :
close(pipe0[0]);
close(pipe1[1]);
FD_ZERO(&rdfds);
FD_ZERO(&wrfds);
FD_SET(pipe1[0], &rdfds);
FD_SET(pipe0[1], &wrfds);
fdmax = 1 + MAX(pipe1[0], pipe0[1]);
for (;;) {
fd_set RDFDS = rdfds;
fd_set WRFDS = wrfds;
int foo;
foo = select(fdmax, &RDFDS, &WRFDS, NULL, NULL);
if (-1 == foo) {
yell("Broken select call: %s", strerror(errno));
if (EINTR == errno)
continue;
break;
} else if (0 == foo) {
break;
}
if (FD_ISSET(pipe1[0], &RDFDS)) {
retlen = rdpos + 4096;
new_realloc((void**)&ret, retlen);
foo = read(pipe1[0], ret+rdpos, retlen-rdpos);
if (0 == foo)
break;
else if (0 < foo)
rdpos += foo;
} else if (FD_ISSET(pipe0[1], &WRFDS)) {
if (input && wrpos < *len)
foo = write(pipe0[1], input+wrpos, MIN(512, *len-wrpos));
else {
FD_CLR(pipe0[1], &wrfds);
close(pipe0[1]);
}
if (0 < foo)
wrpos += foo;
}
}
close(pipe0[1]);
close(pipe1[0]);
waitpid(pid, NULL, WNOHANG);
new_realloc((void**)&ret, rdpos);
break;
}
*len = rdpos;
return ret;
}
/*
* exec() something and return three FILE's.
*
* On failure, close everything and return NULL.
*/
FILE ** open_exec (const char *executable, char * const *args)
{
static FILE * file_pointers[3];
int pipe0[2] = {-1, -1};
int pipe1[2] = {-1, -1};
int pipe2[2] = {-1, -1};
if (pipe(pipe0) == -1 || pipe(pipe1) == -1 || pipe(pipe2) == -1)
{
yell("Cannot open exec pipes: %s\n", strerror(errno));
close(pipe0[0]);
close(pipe0[1]);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
close(pipe2[1]);
return NULL;
}
switch (fork())
{
case -1:
{
yell("Cannot fork for exec: %s\n",
strerror(errno));
close(pipe0[0]);
close(pipe0[1]);
close(pipe1[0]);
close(pipe1[1]);
close(pipe2[0]);
close(pipe2[1]);
return NULL;
}
case 0:
{
dup2(pipe0[0], 0);
dup2(pipe1[1], 1);
dup2(pipe2[1], 2);
close(pipe0[1]);
close(pipe1[0]);
close(pipe2[0]);
setuid(getuid());
setgid(getgid());
execvp(executable, args);
_exit(0);
}
default :
{
close(pipe0[0]);
close(pipe1[1]);
close(pipe2[1]);
if (!(file_pointers[0] = fdopen(pipe0[1], "w")))
{
yell("Cannot open exec STDIN: %s\n",
strerror(errno));
close(pipe0[1]);
close(pipe1[0]);
close(pipe2[0]);
return NULL;
}
if (!(file_pointers[1] = fdopen(pipe1[0], "r")))
{
yell("Cannot open exec STDOUT: %s\n",
strerror(errno));
fclose(file_pointers[0]);
close(pipe1[0]);
close(pipe2[0]);
return NULL;
}
if (!(file_pointers[2] = fdopen(pipe2[0], "r")))
{
yell("Cannot open exec STDERR: %s\n",
strerror(errno));
fclose(file_pointers[0]);
fclose(file_pointers[1]);
close(pipe2[0]);
return NULL;
}
break;
}
}
return file_pointers;
}
static FILE * open_compression (char *executable, char *filename)
{
FILE * file_pointer;
int pipes[2] = {-1, -1};
if (pipe(pipes) == -1)
{
yell("Cannot start decompression: %s\n", strerror(errno));
if (pipes[0] != -1)
{
close(pipes[0]);
close(pipes[1]);
}
return NULL;
}
switch (fork())
{
case -1:
{
yell("Cannot start decompression: %s\n",
strerror(errno));
return NULL;
}
case 0:
{
dup2(pipes[1], 1);
close(pipes[0]);
close(2); /* we dont want to see errors */
setuid(getuid());
setgid(getgid());
/*
* 'compress', 'uncompress, 'gzip', 'gunzip',
* 'bzip2' and 'bunzip'2 on my system all support
* the -d option reasonably. I hope they do
* elsewhere. :d
*/
execl(executable, executable, "-d", "-c", filename, NULL);
_exit(0);
}
default :
{
close(pipes[1]);
if (!(file_pointer = fdopen(pipes[0], "r")))
{
yell("Cannot start decompression: %s\n",
strerror(errno));
return NULL;
}
break;
}
}
return file_pointer;
}
/*
* Front end to fopen() that will open ANY file, compressed or not, and
* is relatively smart about looking for the possibilities, and even
* searches a path for you! ;-)
*
* NOTICE -- 'filename' is an input/output parameter. On input, it must
* be a malloc()ed string containing a file to open. On output, it will
* be changed to point to the actual filename that was opened. THE ORIGINAL
* INPUT VALUE IS ALWAYS FREE()D IN EVERY CIRCUMSTANCE. IT WILL BE REPLACED
* WITH A NEW VALUE (ie, the variable will be changed) UPON RETURN. You must
* not save the original value of '*filename' and use it after calling uzfopen.
*/
FILE * uzfopen (char **filename, const char *path, int do_error)
{
static int setup = 0;
static Filename path_to_gunzip;
static Filename path_to_uncompress;
static Filename path_to_bunzip2;
int ok_to_decompress = 0;
Filename fullname;
Filename candidate;
Stat file_info;
FILE * doh;
if (!setup)
{
*path_to_gunzip = 0;
path_search("gunzip", getenv("PATH"), path_to_gunzip);
*path_to_uncompress = 0;
path_search("uncompress", getenv("PATH"), path_to_uncompress);
*path_to_bunzip2 = 0;
if (path_search("bunzip2", getenv("PATH"), path_to_bunzip2))
path_search("bunzip", getenv("PATH"), path_to_bunzip2);
setup = 1;
}
/*
* It is allowed to pass to this function either a true filename
* with the compression extention, or to pass it the base name of
* the filename, and this will look to see if there is a compressed
* file that matches the base name
*/
/*
* Start with what we were given as an initial guess
* kev asked me to call expand_twiddle here once,
* Now that path_search() does ~'s, we don't need to do
* so here any more.
*/
/*
* Look to see if the passed filename is a full compressed filename
*/
if ((!end_strcmp(*filename, ".gz", 3)) ||
(!end_strcmp(*filename, ".z", 2)))
{
if (!*path_to_gunzip)
{
if (do_error)
yell("Cannot open file %s because gunzip "
"was not found", *filename);
goto error_cleanup;
}
ok_to_decompress = 2;
if (path_search(*filename, path, fullname))
goto file_not_found;
}
else if (!end_strcmp(*filename, ".Z", 2))
{
if (!*path_to_gunzip && !*path_to_uncompress)
{
if (do_error)
yell("Cannot open file %s becuase uncompress "
"was not found", *filename);
goto error_cleanup;
}
ok_to_decompress = 1;
if (path_search(*filename, path, fullname))
goto file_not_found;
}
else if (!end_strcmp(*filename, ".bz2", 4))
{
if (!*path_to_bunzip2)
{
if (do_error)
yell("Cannot open file %s because bunzip "
"was not found", *filename);
goto error_cleanup;
}
ok_to_decompress = 3;
if (path_search(*filename, path, fullname))
goto file_not_found;
}
/* Right now it doesnt look like the file is a full compressed fn */
else
{
do
{
/* Trivially, see if the file we were passed exists */
if (!path_search(*filename, path, fullname)) {
ok_to_decompress = 0;
break;
}
/* Is there a "filename.gz"? */
snprintf(candidate, sizeof(candidate), "%s.gz", *filename);
if (!path_search(candidate, path, fullname)) {
ok_to_decompress = 2;
break;
}
/* Is there a "filename.Z"? */
snprintf(candidate, sizeof(candidate), "%s.Z", *filename);
if (!path_search(candidate, path, fullname)) {
ok_to_decompress = 1;
break;
}
/* Is there a "filename.z"? */
snprintf(candidate, sizeof(candidate), "%s.z", *filename);
if (!path_search(candidate, path, fullname)) {
ok_to_decompress = 2;
break;
}
/* Is there a "filename.bz2"? */
snprintf(candidate, sizeof(candidate), "%s.bz2", *filename);
if (!path_search(candidate, path, fullname)) {
ok_to_decompress = 3;
break;
}
goto file_not_found;
}
while (0);
stat(fullname, &file_info);
if (S_ISDIR(file_info.st_mode))
{
if (do_error)
yell("%s is a directory", fullname);
goto error_cleanup;
}
if (file_info.st_mode & 0111)
{
if (do_error)
yell("Cannot open %s -- executable file", fullname);
goto error_cleanup;
}
}
/*
* At this point, we should have a filename in the variable
* *filename, and it should exist. If ok_to_decompress is one, then
* we can gunzip the file if gunzip is available. else we
* uncompress the file.
*/
malloc_strcpy(filename, fullname);
if (ok_to_decompress)
{
if ((ok_to_decompress <= 2) && *path_to_gunzip)
return open_compression(path_to_gunzip, *filename);
else if ((ok_to_decompress == 1) && *path_to_uncompress)
return open_compression(path_to_uncompress, *filename);
else if ((ok_to_decompress == 3) && *path_to_bunzip2)
return open_compression(path_to_bunzip2, *filename);
if (do_error)
yell("Cannot open compressed file %s becuase no "
"uncompressor was found", *filename);
goto error_cleanup;
}
/* Its not a compressed file... Try to open it regular-like. */
else if ((doh = fopen(*filename, "r")))
return doh;
/* nope.. we just cant seem to open this file... */
else if (do_error)
yell("Cannot open file %s: %s", *filename, strerror(errno));
goto error_cleanup;
file_not_found:
if (do_error)
yell("File not found: %s", *filename);
error_cleanup:
new_free(filename);
return NULL;
}
/*
* slurp_file opens up a file and puts the contents into 'buffer'.
* The size of 'buffer' is returned.
*/
int slurp_file (char **buffer, char *filename)
{
char * local_buffer;
size_t offset;
off_t local_buffer_size;
off_t filesize;
Stat s;
FILE * file;
size_t count;
file = uzfopen(&filename, get_string_var(LOAD_PATH_VAR), 1);
if (stat(filename, &s) < 0)
{
fclose(file);
new_free(&filename);
return -1; /* Whatever. */
}
filesize = s.st_size;
if (!end_strcmp(filename, ".gz", 3))
filesize *= 7;
else if (!end_strcmp(filename, ".bz2", 4))
filesize *= 10;
else if (!end_strcmp(filename, ".Z", 2))
filesize *= 5;
local_buffer = new_malloc(filesize);
local_buffer_size = filesize;
offset = 0;
do
{
count = fread(local_buffer + offset,
local_buffer_size - offset, 1, file);
offset += count;
if (!feof(file))
{
local_buffer_size += (filesize * 3);
new_realloc((void **)&local_buffer, local_buffer_size);
continue;
}
}
while (0);
*buffer = local_buffer;
return offset;
}
int fw_strcmp (comp_len_func *compar, char *v1, char *v2)
{
int len = 0;
char *pos = one;
while (!my_isspace(*pos))
pos++, len++;
return compar(v1, v2, len);
}
/*
* Compares the last word in 'one' to the string 'two'. You must provide
* the compar function. my_stricmp is a good default.
*/
int lw_strcmp(comp_func *compar, char *val1, char *val2)
{
char *pos = val1 + strlen(val1) - 1;
if (pos > val1) /* cant do pos[-1] if pos == val1 */
while (!my_isspace(pos[-1]) && (pos > val1))
pos--;
else
pos = val1;
if (compar)
return compar(pos, val2);
else
return my_stricmp(pos, val2);
}
/*
* you give it a filename, some flags, and a position, and it gives you an
* fd with the file pointed at the 'position'th byte.
*/
int opento(const char *filename, int flags, off_t position)
{
int file;
file = open(filename, flags, 777);
lseek(file, position, SEEK_SET);
return file;
}
/* swift and easy -- returns the size of the file */
off_t file_size (const char *filename)
{
Stat statbuf;
if (!stat(filename, &statbuf))
return (off_t)(statbuf.st_size);
else
return -1;
}
int file_exists (const char *filename)
{
if (file_size(filename) == -1)
return 0;
else
return 1;
}
int isdir (const char *filename)
{
Stat statbuf;
if (!stat(filename, &statbuf))
{
if (S_ISDIR(statbuf.st_mode))
return 1;
}
return 0;
}
struct metric_time timeval_to_metric (const Timeval *tv)
{
struct metric_time retval;
double my_timer;
long sec;
retval.mt_days = tv->tv_sec / 86400;
sec = tv->tv_sec % 86400; /* Seconds after midnight */
sec = sec * 1000; /* Convert to ms */
sec += (tv->tv_usec / 1000); /* Add ms fraction */
my_timer = (double)sec / 86400.0; /* Convert to millidays */
retval.mt_mdays = my_timer;
return retval;
}
struct metric_time get_metric_time (double *timer)
{
Timeval tv;
struct metric_time mt;
get_time(&tv);
mt = timeval_to_metric(&tv);
if (timer)
*timer = mt.mt_mdays;
return mt;
}
/* Gets the time in second/usecond if you can, second/0 if you cant. */
Timeval get_time (Timeval *timer)
{
static Timeval retval;
/* Substitute a dummy timeval if we need one. */
if (!timer)
timer = &retval;
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
timer->tv_sec = ts.tv_sec;
timer->tv_usec = ts.tv_nsec / 1000;
#else
# ifdef HAVE_GETTIMEOFDAY
gettimeofday(timer, NULL);
# else
timer->tv_sec = time(NULL);
timer->tv_usec = 0;
# endif
#endif
}
return *timer;
}
/*
* calculates the time elapsed between 't1' and 't2' where they were
* gotten probably with a call to get_time. 't1' should be the older
* timer and 't2' should be the most recent timer.
*/
double time_diff (const Timeval t1, const Timeval t2)
{
Timeval td;
td.tv_sec = t2.tv_sec - t1.tv_sec;
td.tv_usec = t2.tv_usec - t1.tv_usec;
return (double)td.tv_sec + ((double)td.tv_usec / 1000000.0);
}
Timeval double_to_timeval (double x)
{
Timeval td;
time_t s;
s = (time_t) x;
x = x - s;
x = x * 1000000;
td.tv_sec = s;
td.tv_usec = (long) x;
return td;
}
/*
* calculates the time elapsed between 'one' and 'two' where they were
* gotten probably with a call to get_time. 'one' should be the older
* timer and 'two' should be the most recent timer.
*/
Timeval time_subtract (const Timeval t1, const Timeval t2)
{
Timeval td;
td.tv_sec = t2.tv_sec - t1.tv_sec;
td.tv_usec = t2.tv_usec - t1.tv_usec;
if (td.tv_usec < 0)
{
td.tv_usec += 1000000;
td.tv_sec--;
}
return td;
}
/*
* Adds the interval "two" to the base time "one" and returns it.
*/
Timeval time_add (const Timeval t1, const Timeval t2)
{
Timeval td;
td.tv_usec = t1.tv_usec + t2.tv_usec;
td.tv_sec = t1.tv_sec + t2.tv_sec;
if (td.tv_usec >= 1000000)
{
td.tv_usec -= 1000000;
td.tv_sec++;
}
return td;
}
const char * plural (int number)
{
return (number != 1) ? "s" : empty_string;
}
char * my_ctime (time_t when)
{
return chop(ctime(&when), 1);
}
char * ltoa (long foo)
{
static char buffer[BIG_BUFFER_SIZE + 1];
char *pos = buffer + BIG_BUFFER_SIZE - 1;
unsigned long absv;
int negative;
absv = (foo < 0) ? (unsigned long)-foo : (unsigned long)foo;
negative = (foo < 0) ? 1 : 0;
buffer[BIG_BUFFER_SIZE] = 0;
for (; absv > 9; absv /= 10)
*pos-- = (absv % 10) + '0';
*pos = (absv) + '0';
if (negative)
*--pos = '-';
return pos;
}
char * ftoa (double foo)
{
static char buffer [BIG_BUFFER_SIZE + 1];
extern double fmod (double, double);
if (get_int_var(FLOATING_POINT_MATH_VAR)) {
snprintf(buffer, sizeof buffer, "%.*g",
get_int_var(FLOATING_POINT_PRECISION_VAR), foo);
} else {
foo -= fmod(foo, 1);
snprintf(buffer, sizeof buffer, "%.0f", foo);
}
return buffer;
}
/*
* Formats "src" into "dest" using the given length. If "length" is
* negative, then the string is right-justified. If "length" is
* zero, nothing happens. Sure, i cheat, but its cheaper then doing
* two snprintf's.
*
* Changed to use the PAD_CHAR variable, which allows the user to specify
* what character should be used to "fill out" the padding.
*/
char * strformat (char *dest, const char *src, ssize_t length, int pad)
{
char * ptr1 = dest;
const char * ptr2 = src;
int tmplen = length;
int abslen;
char padc;
abslen = (length >= 0 ? length : -length);
if ((padc = (char)pad) == 0)
padc = ' ';
/* Cheat by spacing out 'dest' */
for (tmplen = abslen - 1; tmplen >= 0; tmplen--)
dest[tmplen] = padc;
dest[abslen] = 0;
/* Then cheat further by deciding where the string should go. */
if (length > 0) /* left justified */
{
while ((length-- > 0) && *ptr2)
*ptr1++ = *ptr2++;
}
else if (length < 0) /* right justified */
{
length = -length;
ptr1 = dest;
ptr2 = src;
if ((int)strlen(src) < length)
ptr1 += length - strlen(src);
while ((length-- > 0) && *ptr2)
*ptr1++ = *ptr2++;
}
return dest;
}
/*
* MatchingBracket returns the next unescaped bracket of the given type
* This used to be real simple (see the final else clause), but very
* expensive. Since its called a lot, i unrolled the two most common cases
* (parens and brackets) and parsed them out with switches, which should
* really help the cpu usage. I hope. Everything else just falls through
* and uses the old tried and true method.
*/
ssize_t MatchingBracket (const char *start, char left, char right)
{
int bracket_count = 1;
const char *string = start;
if (left == '(')
{
for (; *string; string++)
{
switch (*string)
{
case '(':
bracket_count++;
break;
case ')':
bracket_count--;
if (bracket_count == 0)
return string - start;
break;
case '\\':
if (string[1])
string++;
break;
}
}
}
else if (left == '[')
{
for (; *string; string++)
{
switch (*string)
{
case '[':
bracket_count++;
break;
case ']':
bracket_count--;
if (bracket_count == 0)
return string - start;
break;
case '\\':
if (string[1])
string++;
break;
}
}
}
else /* Fallback for everyone else */
{
while (*string && bracket_count)
{
if (*string == '\\' && string[1])
string++;
else if (*string == left)
bracket_count++;
else if (*string == right)
{
if (--bracket_count == 0)
return string - start;
}
string++;
}
}
return -1;
}
/*
* parse_number: returns the next number found in a string and moves the
* string pointer beyond that point in the string. Here's some examples:
*
* "123harhar" returns 123 and str as "harhar"
*
* while:
*
* "hoohar" returns -1 and str as "hoohar"
*/
int parse_number (char **str)
{
long ret;
char *ptr = *str; /* sigh */
ret = strtol(ptr, str, 10);
if (*str == ptr)
ret = -1;
return (int)ret;
}
char * chop_word (char *str)
{
char *end = str + strlen(str) - 1;
while (my_isspace(*end) && (end > str))
end--;
while (!my_isspace(*end) && (end > str))
end--;
if (end >= str)
*end = 0;
return str;
}
char * skip_spaces (char *str)
{
while (str && *str && isspace(*str))
str++;
return str;
}
int split_args (char *str, char **to, size_t maxargs)
{
size_t counter;
char * ptr;
ptr = str;
for (counter = 0; counter < maxargs; counter++)
{
if (!ptr || !*ptr)
break;
ptr = skip_spaces(ptr);
if (*ptr == '{' || *ptr == '(')
{
if (counter > 0)
ptr[-1] = 0;
to[counter] = next_expr_with_type(&ptr, *ptr);
}
else
to[counter] = new_next_arg(ptr, &ptr);
/* Syntax error? abort immediately. */
if (to[counter] == NULL)
break;
}
to[counter] = NULL;
return counter;
}
int splitw (char *str, char ***to)
{
int numwords = count_words(str, DWORD_YES, "\"");
int counter;
if (numwords)
{
*to = (char **)new_malloc(sizeof(char *) * numwords);
for (counter = 0; counter < numwords; counter++)
(*to)[counter] = safe_new_next_arg(str, &str);
}
else
*to = NULL;
return numwords;
}
char * unsplitw (char ***container, int howmany)
{
char *retval = NULL;
char **str = *container;
size_t clue = 0;
if (!str || !*str)
return NULL;
while (howmany)
{
if (*str && **str)
malloc_strcat_word_c(&retval, space, *str, &clue);
str++, howmany--;
}
new_free((char **)container);
return retval;
}
double strtod(); /* sunos must die. */
int check_val (const char *sub)
{
double sval;
char *endptr;
if (!*sub)
return 0;
/* get the numeric value (if any). */
errno = 0;
sval = strtod(sub, &endptr);
/* Numbers that cause exceptional conditions in strtod() are true */
if (errno == ERANGE
#if defined(HAVE_FINITE)
|| finite(sval) == 0
#elif defined(HAVE_INFINITE)
|| isfinite(sval) == 0
#endif
)
return 1;
/*
* - Any string with no leading number
* - Any string containing anything after a leading number
* - Any string wholly containing a non-zero number
* are all true.
*/
if (sub == endptr || *endptr || sval != 0.0)
return 1;
/* Basically that leaves empty strings and the number 0 as false. */
return 0;
}
/*
* Appends 'num' copies of 'app' to the end of 'str'.
*/
char *strextend (char *str, char app, int num)
{
char *ptr = str + strlen(str);
for (;num;num--)
*ptr++ = app;
*ptr = (char) 0;
return str;
}
int empty (const char *str)
{
#if 0
while (str && *str && *str == ' ')
str++;
#endif
if (str && *str)
return 0;
return 1;
}
/* makes foo[one][two] look like tmp.one.two -- got it? */
char * remove_brackets (const char *name, const char *args, int *arg_flag)
{
char *ptr,
*right,
*result1,
*rptr,
*retval = NULL;
ssize_t span;
/* XXXX - ugh. */
rptr = malloc_strdup(name);
while ((ptr = strchr(rptr, '[')))
{
*ptr++ = 0;
right = ptr;
if ((span = MatchingBracket(right, '[', ']')) >= 0)
{
ptr = right + span;
*ptr++ = 0;
}
else
ptr = NULL;
if (args)
result1 = expand_alias(right, args, arg_flag, NULL);
else
result1 = right;
retval = malloc_strdup3(rptr, ".", result1);
if (ptr)
malloc_strcat(&retval, ptr);
if (args)
new_free(&result1);
if (rptr)
new_free(&rptr);
rptr = retval;
}
return upper(rptr);
}
long my_atol (const char *str)
{
if (str)
return (long) strtol(str, NULL, 0);
else
return 0L;
}
char * malloc_dupchar (int i)
{
char c = (char) i; /* blah */
char * ret = (char *)new_malloc(2);
ret[0] = c;
ret[1] = 0;
return ret;
}
/*
* This checks to see if ``root'' is a proper subname for ``var''.
*/
int is_root (const char *root, const char *var, int descend)
{
int rootl, varl;
/* ``root'' must end in a dot */
rootl = strlen(root);
if (rootl == 0)
return 0;
if (root[rootl - 1] != '.')
return 0;
/* ``root'' must be shorter than ``var'' */
varl = strlen(var);
if (varl <= rootl)
return 0;
/* ``var'' must contain ``root'' as a leading subset */
if (my_strnicmp(root, var, rootl))
return 0;
/*
* ``var'' must not contain any additional dots
* if we are checking for the current level only
*/
if (!descend && strchr(var + rootl, '.'))
return 0;
/* Looks like its ok */
return 1;
}
/* Returns the number of characters they are equal at. */
size_t streq (const char *str1, const char *str2)
{
size_t cnt = 0;
while (*str1 && *str2 && *str1 == *str2)
cnt++, str1++, str2++;
return cnt;
}
char * malloc_strndup (const char *str, size_t len)
{
char *retval = (char *)new_malloc(len + 1);
strlcpy(retval, str, len + 1);
return retval;
}
char * prntdump(const char *ptr, size_t size)
{
size_t i;
static char dump[65];
strlcat(dump, ptr, sizeof dump);
for (i = 0; i < size && i < 64; i++)
{
if (!isgraph(dump[i]) && !isspace(dump[i]))
dump[i] = '.';
}
if (i == 64)
dump[63] = '>';
dump[i] = 0;
return dump;
}
/* XXXX this doesnt belong here. im not sure where it goes, though. */
char * get_userhost (void)
{
strlcpy(userhost, username, sizeof userhost);
strlcat(userhost, "@", sizeof userhost);
strlcat(userhost, hostname, sizeof userhost);
return userhost;
}
double time_to_next_interval (int interval)
{
Timeval right_now, then;
get_time(&right_now);
then.tv_usec = 1000000 - right_now.tv_usec;
if (interval == 1)
then.tv_sec = 0;
else
then.tv_sec = interval - (right_now.tv_sec + 1) % interval;
return (double)then.tv_sec + (double)then.tv_usec / 1000000;
}
/* Fancy attempt to compensate for broken time_t's */
double time_to_next_minute (void)
{
static int which = 0;
Timeval right_now, then;
get_time(&right_now);
/*
* The first time called, try to determine if the system clock
* is an exact multiple of 60 at the top of every minute. If it
* is, then we will use the "60 trick" to optimize calculations.
* If it is not, then we will do it the hard time every time.
*/
if (which == 0)
{
time_t blargh;
struct tm *now_tm;
blargh = right_now.tv_sec;
now_tm = gmtime(&blargh);
if (!which)
{
if (now_tm->tm_sec == right_now.tv_sec % 60)
which = 1;
else
which = 2;
}
}
then.tv_usec = 1000000 - right_now.tv_usec;
if (which == 1)
then.tv_sec = 60 - (right_now.tv_sec + 1) % 60;
else /* which == 2 */
{
time_t blargh;
struct tm *now_tm;
blargh = right_now.tv_sec;
now_tm = gmtime(&blargh);
then.tv_sec = 60 - (now_tm->tm_sec + 1) % 60;
}
return (double)then.tv_sec + (double)then.tv_usec / 1000000;
}
/*
* An strcpy that is guaranteed to be safe for overlaps.
* Warning: This may _only_ be called when one and two overlap!
*/
char * ov_strcpy (char *str1, const char *str2)
{
if (str2 > str1)
{
while (str2 && *str2)
*str1++ = *str2++;
*str1 = 0;
}
return str1;
}
/*
* Its like strcspn, except the second arg is NOT a string.
*/
size_t ccspan (const char *string, int s)
{
size_t count = 0;
char c = (char) s;
while (string && *string && *string != c)
string++, count++;
return count;
}
int last_char (const char *string)
{
while (string && string[0] && string[1])
string++;
return (int)*string;
}
int charcount (const char *string, char what)
{
int x = 0;
const char *place = string;
while (*place)
if (*place++ == what)
x++;
return x;
}
/* Dest should be big enough to hold "src" */
void strip_control (const char *src, char *dest)
{
for (; *src; src++)
{
if (isgraph(*src) || isspace(*src))
*dest++ = *src;
}
*dest++ = 0;
}
const char * strfill (char c, int num)
{
static char buffer[BIG_BUFFER_SIZE / 4 + 1];
int i;
if (num > BIG_BUFFER_SIZE / 4)
num = BIG_BUFFER_SIZE / 4;
for (i = 0; i < num; i++)
buffer[i] = c;
buffer[i] = 0;
return buffer;
}
char * encode (const char *str, size_t len)
{
char *retval;
char *ptr;
if ((int)len < 0)
len = strlen(str);
ptr = retval = new_malloc(len * 2 + 1);
while (len)
{
*ptr++ = ((unsigned char)*str >> 4) + 0x41;
*ptr++ = ((unsigned char)*str & 0x0f) + 0x41;
str++;
len--;
}
*ptr = 0;
return retval;
}
char * decode (const char *str)
{
char *retval;
char *ptr;
int len = strlen(str);
ptr = retval = new_malloc(len / 2 + 1);
while (len >= 2)
{
*ptr++ = ((str[0] - 0x41) << 4) | (str[1] - 0x41);
str += 2;
len -= 2;
}
*ptr = 0;
return retval;
}
char * chomp (char *s)
{
char *e = s + strlen(s);
if (e == s)
return s;
while (*--e == '\n')
{
*e = 0;
if (e == s)
break;
}
return s;
}
/*
* figure_out_address -- lets try this one more time.
*/
int figure_out_address (const char *nuh, char **nick, char **user, char **host)
{
static char *mystuff = NULL;
char *bang,
*at,
*adot = NULL;
/* Dont bother with channels, theyre ok. */
if (*nuh == '#' || *nuh == '&')
return -1;
malloc_strcpy(&mystuff, nuh);
*host = star;
/*
* Find and identify each of the three context clues
* (A bang, an at, and a dot).
*/
if ((bang = strchr(mystuff, '!')))
{
*bang = 0;
if ((at = strchr(bang + 1, '@')))
{
*at = 0;
adot = strchr(at + 1, '.');
}
}
else if ((at = strchr(mystuff, '@')))
{
*at = 0;
adot = strchr(at + 1, '.');
}
else
adot = strchr(mystuff, '.');
/*
* Hrm. How many cases are there? There are three context clues
* (A bang, an at, and a dot.) So that makes 8 different cases.
* Let us enumerate them:
*
* nick (no !, no @, no .)
* nick!user (a !, no @, no .)
* nick!user@host (a !, a @, no .)
* nick!user@host.domain (a !, a @, a .)
* nick!host.domain (a !, no @, a .)
* user@host (no !, a @, no .)
* user@host.domain (no !, a @, a .)
* host.domain (no !, no @, yes .)
*/
/*
* STAGE ONE -- EXTRACT THE NICK, USER, AND HOST PORTIONS.
*
* stage two is now in 'figure_out_domain', so functions which want it
* that way can have it that way.
*/
/*
* Now let us handle each of these eight cases in a reasonable way.
*/
if (bang)
{
if (at)
{
/* nick!user@host */
*nick = mystuff;
*user = bang + 1;
*host = at + 1;
}
else
{
if (adot) /* nick!host.domain */
{
*nick = mystuff;
*user = star;
*host = at + 1;
}
else /* nick!user */
{
*nick = mystuff;
*user = star;
*host = star;
}
}
}
else
{
if (at)
{
/* user@host.domain */
*nick = star;
*user = mystuff;
*host = at + 1;
}
else
{
if (adot) /* host.domain */
{
*nick = star;
*user = star;
*host = mystuff;
}
else /* nick */
{
*nick = mystuff;
*user = star;
*host = star;
}
}
}
return 0;
}
int figure_out_domain (char *fqdn, char **host, char **domain, int *ip)
{
char *firstback,
*secondback,
*thirdback,
*fourthback;
char *endstring;
char *adot;
int number;
/* determine if we have an IP, use dot to hold this */
/* is_number is better than my_atol since floating point
* base 36 numbers are pretty much invalid as IPs.
*/
if ((adot = strrchr(fqdn, '.')) && is_number(adot + 1))
*ip = 1;
else
*ip = 0;
/*
* STAGE TWO -- EXTRACT THE HOST AND DOMAIN FROM FQDN
*/
/*
* At this point, 'fqdn' points what what we think the hostname
* is. We chop it up into discrete parts and see what we end up with.
*/
endstring = fqdn + strlen(fqdn);
firstback = strnrchr(fqdn, '.', 1);
secondback = strnrchr(fqdn, '.', 2);
thirdback = strnrchr(fqdn, '.', 3);
fourthback = strnrchr(fqdn, '.', 4);
/* Track foo@bar or some such thing. */
if (!firstback)
{
*host = fqdn;
return 0;
}
/*
* IP address (A.B.C.D)
*/
if (my_atol(firstback + 1))
{
*domain = fqdn;
number = my_atol(fqdn);
if (number < 128)
*host = thirdback;
else if (number < 192)
*host = secondback;
else
*host = firstback;
if (!*host)
return -1; /* Invalid hostname */
**host = 0;
(*host)++;
}
/*
* (*).(*.???)
* Handles *.com, *.net, *.edu, etc
*/
else if (secondback && (endstring - firstback == 4))
{
*host = fqdn;
*domain = secondback;
**domain = 0;
(*domain)++;
}
/*
* (*).(*.k12.??.us)
* Handles host.school.k12.state.us
*/
else if (fourthback &&
(firstback - secondback == 3) &&
!strncmp(thirdback, ".k12.", 5) &&
!strncmp(firstback, ".us", 3))
{
*host = fqdn;
*domain = fourthback;
**domain = 0;
(*domain)++;
}
/*
* ()(*.k12.??.us)
* Handles school.k12.state.us
*/
else if (thirdback && !fourthback &&
(firstback - secondback == 3) &&
!strncmp(thirdback, ".k12.", 5) &&
!strncmp(firstback, ".us", 3))
{
*host = empty_string;
*domain = fqdn;
}
/*
* (*).(*.???.??)
* Handles host.domain.com.au
*/
else if (thirdback &&
(endstring - firstback == 3) &&
(firstback - secondback == 4))
{
*host = fqdn;
*domain = thirdback;
**domain = 0;
(*domain)++;
}
/*
* ()(*.???.??)
* Handles domain.com.au
*/
else if (secondback && !thirdback &&
(endstring - firstback == 3) &&
(firstback - secondback == 4))
{
*host = empty_string;
*domain = fqdn;
}
/*
* (*).(*.??.??)
* Handles host.domain.co.uk
*/
else if (thirdback &&
(endstring - firstback == 3) &&
(firstback - secondback == 3))
{
*host = fqdn;
*domain = thirdback;
**domain = 0;
(*domain)++;
}
/*
* ()(*.??.??)
* Handles domain.co.uk
*/
else if (secondback && !thirdback &&
(endstring - firstback == 3) &&
(firstback - secondback == 3))
{
*host = empty_string;
*domain = fqdn;
}
/*
* (*).(*.??)
* Handles domain.de
*/
else if (secondback && (endstring - firstback == 3))
{
*host = fqdn;
*domain = secondback;
**domain = 0;
(*domain)++;
}
/*
* Everything else...
*/
else
{
*host = empty_string;
*domain = fqdn;
}
return 0;
}
int count_char (const unsigned char *src, const unsigned char look)
{
const unsigned char *t;
int cnt = 0;
while ((t = strchr(src, look)))
cnt++, src = t + 1;
return cnt;
}
char * strnrchr(char *start, char which, int howmany)
{
char *ends = start + strlen(start);
while (ends > start && howmany)
{
if (*--ends == which)
howmany--;
}
if (ends == start)
return NULL;
else
return ends;
}
/*
* This replaces some number of numbers (1 or more) with a single asterisk.
*/
void mask_digits (char **host)
{
char *src_ptr;
char *retval, *retval_ptr;
size_t size;
size = strlen(*host) + 1;
retval = retval_ptr = alloca(size);
src_ptr = *host;
while (*src_ptr)
{
if (isdigit(*src_ptr))
{
while (*src_ptr && isdigit(*src_ptr))
src_ptr++;
*retval_ptr++ = '*';
}
else
*retval_ptr++ = *src_ptr++;
}
*retval_ptr = 0;
strlcpy(*host, retval, size);
}
char * strlpcat (char *source, size_t size, const char *format, ...)
{
va_list args;
char buffer[BIG_BUFFER_SIZE + 1];
va_start(args, format);
vsnprintf(buffer, sizeof buffer, format, args);
va_end(args);
strlcat(source, buffer, size);
return source;
}
u_char *strcpy_nocolorcodes (u_char *dest, const u_char *source)
{
u_char *save = dest;
ssize_t span;
do
{
while (*source == 3)
{
span = skip_ctl_c_seq(source, NULL, NULL);
source += span;
}
*dest++ = *source;
}
while (*source++);
return save;
}
/*
* This mangles up 'incoming' corresponding to the current values of
* /set mangle_inbound or /set mangle_outbound.
* 'incoming' needs to be at least _ELEVEN_ as big as neccesary
* (ie, sizeof(incoming) >= strlen(incoming) * 11 + 1)
*/
size_t mangle_line (char *incoming, int how, size_t how_much)
{
int stuff;
char *buffer;
size_t i;
char *s;
if (how_much == 0)
panic("mangle_line passed a zero-size buffer [%s] [%d]", incoming, how);
stuff = how;
buffer = alloca(how_much + 1); /* Absurdly large */
#if notyet
if (stuff & STRIP_CTCP2)
{
char *output;
output = strip_ctcp2(incoming);
strlcpy(incoming, output, how_much);
new_free(&output);
}
else if (stuff & MANGLE_INBOUND_CTCP2)
{
char *output;
output = ctcp2_to_ircII(incoming);
strlcpy(incoming, output, how_much);
new_free(&output);
}
else if (stuff & MANGLE_OUTBOUND_CTCP2)
{
char *output;
output = ircII_to_ctcp2(incoming);
strlcpy(incoming, output, how_much);
new_free(&output);
}
#endif
if (stuff & MANGLE_ESCAPES)
{
for (i = 0; incoming[i]; i++)
{
if (incoming[i] == 0x1b)
incoming[i] = 0x5b;
}
}
if (stuff & MANGLE_ANSI_CODES)
{
/* normalize_string can expand up to three times */
char *output;
normalize_never_xlate = 1; /* XXXXX */
output = normalize_string(incoming, 1); /* Should be ok */
normalize_never_xlate = 0; /* XXXXX */
if (strlcpy(incoming, output, how_much) > how_much)
say("Mangle_line truncating results. #1 -- "
"Email jnelson@acronet.net [%d] [%d]",
strlen(output), how_much);
new_free(&output);
/*
* Turn off ALL_OFF if ANSI is used, unless all of the
* "ATTRIBUTES" are being stripped.
*/
#define ATTRIBUTES (STRIP_COLOR | STRIP_REVERSE | STRIP_UNDERLINE | \
STRIP_BOLD | STRIP_BLINK | STRIP_ALT_CHAR)
if (stuff & STRIP_ALL_OFF)
{
if ((stuff & ATTRIBUTES) != ATTRIBUTES)
stuff &= ~(STRIP_ALL_OFF);
}
}
/*
* Now we mangle the individual codes
*/
for (i = 0, s = incoming; *s; s++)
{
/* If buffer[i] is past the end, then stop right here! */
if (i >= how_much)
break;
switch (*s)
{
case 003: /* color codes */
{
int lhs = 0,
rhs = 0;
char * end;
ssize_t span;
span = skip_ctl_c_seq(s, &lhs, &rhs);
end = s + span;
if (!(stuff & STRIP_COLOR))
{
/*
* Copy the string one byte at a time
* but only as much as we have space for.
*/
while (s < end && i < how_much)
buffer[i++] = *s++;
}
s = end - 1;
break;
}
case REV_TOG: /* Reverse */
{
if (!(stuff & STRIP_REVERSE))
buffer[i++] = REV_TOG;
break;
}
case UND_TOG: /* Underline */
{
if (!(stuff & STRIP_UNDERLINE))
buffer[i++] = UND_TOG;
break;
}
case BOLD_TOG: /* Bold */
{
if (!(stuff & STRIP_BOLD))
buffer[i++] = BOLD_TOG;
break;
}
case BLINK_TOG: /* Flashing */
{
if (!(stuff & STRIP_BLINK))
buffer[i++] = BLINK_TOG;
break;
}
case ROM_CHAR: /* Special rom-chars */
{
if (!(stuff & STRIP_ROM_CHAR))
buffer[i++] = ROM_CHAR;
break;
}
case ND_SPACE: /* Nondestructive spaces */
{
if (!(stuff & STRIP_ND_SPACE))
buffer[i++] = ND_SPACE;
break;
}
case ALT_TOG: /* Alternate character set */
{
if (!(stuff & STRIP_ALT_CHAR))
buffer[i++] = ALT_TOG;
break;
}
case ALL_OFF: /* ALL OFF attribute */
{
if (!(stuff & STRIP_ALL_OFF))
buffer[i++] = ALL_OFF;
break;
}
default: /* Everything else */
if (!(stuff & STRIP_OTHER))
buffer[i++] = *s;
}
}
/* If buffer[i] is off the end of the string, bring it back in. */
if (i >= how_much)
i = how_much - 1;
/* Terminate the mangled copy, and return to sender. */
buffer[i] = 0;
return strlcpy(incoming, buffer, how_much);
}
/* RANDOM NUMBERS */
/*
* Random number generator #1 -- psuedo-random sequence
* If you do not have /dev/random and do not want to use gettimeofday(), then
* you can use the psuedo-random number generator. Its performance varies
* from weak to moderate. It is a predictable mathematical sequence that
* varies depending on the seed, and it provides very little repetition,
* but with 4 or 5 samples, it should be trivial for an outside person to
* find the next numbers in your sequence.
*
* If 'l' is not zero, then it is considered a "seed" value. You want
* to call it once to set the seed. Subsequent calls should use 'l'
* as 0, and it will return a value.
*/
static unsigned long randm (unsigned long l)
{
/* patch from Sarayan to make $rand() better */
static const long RAND_A = 16807L;
static const long RAND_M = 2147483647L;
static const long RAND_Q = 127773L;
static const long RAND_R = 2836L;
static unsigned long z = 0;
long t;
if (z == 0)
z = (unsigned long) getuid();
if (l == 0)
{
t = RAND_A * (z % RAND_Q) - RAND_R * (z / RAND_Q);
if (t > 0)
z = t;
else
z = t + RAND_M;
return (z >> 8) | ((z & 255) << 23);
}
else
{
if ((long) l < 0)
z = (unsigned long) getuid();
else
z = l;
return 0;
}
}
/*
* Random number generator #2 -- gettimeofday().
* If you have gettimeofday(), then we could use it. Its performance varies
* from weak to moderate. At best, it is a source of modest entropy, with
* distinct linear qualities. At worst, it is a linear sequence. If you do
* not have gettimeofday(), then it uses randm() instead.
*/
static unsigned long randt_2 (void)
{
Timeval tp1;
get_time(&tp1);
return (unsigned long) tp1.tv_usec;
}
static unsigned long randt (unsigned long l)
{
#ifdef HAVE_GETTIMEOFDAY
unsigned long t1, t2, t;
if (l != 0)
return 0;
t1 = randt_2();
t2 = randt_2();
t = (t1 & 65535) * 65536 + (t2 & 65535);
return t;
#else
return randm(l);
#endif
}
/*
* Random number generator #3 -- /dev/urandom.
* If you have the /dev/urandom device, then we will use it. Its performance
* varies from moderate to very strong. At best, it is a source of pretty
* substantial unpredictable numbers. At worst, it is mathematical psuedo-
* random sequence (which randm() is).
*/
static unsigned long randd (unsigned long l)
{
unsigned long value;
static int random_fd = -1;
if (l != 0)
return 0; /* No seeding appropriate */
if (random_fd == -2)
return randm(l);
else if (random_fd == -1)
{
if ((random_fd = open("/dev/urandom", O_RDONLY)) == -1)
{
random_fd = -2;
return randm(l); /* Fall back to randm */
}
}
read(random_fd, (void *)&value, sizeof(value));
return value;
}
/*
* Random number generator #4 -- Arc4random.
* If you have the /dev/urandom device, this this may very well be the best
* random number generator for you. It spits out relatively good entropic
* numbers, while not severely depleting the entropy pool (as reading directly
* from /dev/random does). If you do not have the /dev/urandom device, then
* this function uses the stack for its entropy, which may or may not be
* suitable, but what the heck. This generator is always available.
*/
static unsigned long randa (unsigned long l)
{
if (l != 0)
return 0; /* No seeding appropriate */
return (unsigned long)bsd_arc4random();
}
unsigned long random_number (unsigned long l)
{
switch (get_int_var(RANDOM_SOURCE_VAR))
{
case 0:
return randd(l);
case 1:
return randm(l);
case 2:
return randt(l);
case 3:
default:
return randa(l);
}
}
/*
* urlencode: converts non-alphanumeric characters to hexidecimal codes
* Contributed by SrFrog
*/
char * urlencode (const char *s)
{
static const char unsafe[] = "`'!@#$%^&*(){}<>~|\\\";? ,/";
static const char hexnum[] = "0123456789ABCDEF";
const char *p1;
char * p2;
size_t len;
char * retval;
if (!s || !*s)
return NULL;
len = strlen(s);
retval = new_malloc(len * 3 + 1);
for (p1 = s, p2 = retval; *p1; p1++)
{
if (*p1 <= 0x20 || strchr(unsafe, *p1))
{
unsigned c = (unsigned)(unsigned char)*p1;
*p2++ = '%';
*p2++ = hexnum[c >> 4];
*p2++ = hexnum[c & 0x0f];
}
else
*p2++ = *p1;
}
*p2 = 0;
return retval;
}
#define XTOI(x) \
( \
((x) >= '0' && (x) <= '9') \
? ((x) - '0') \
: ( ((x) >= 'A' && (x) <= 'F') \
? (((x) - 'A') + 10) \
: ( ((x) >= 'a' && (x) <= 'f') \
? (((x) - 'a') + 10) \
: -1 \
) \
) \
)
char * urldecode (char *s, size_t *length)
{
const char *p1;
char * p2;
size_t len;
char * retval;
int val1;
int val2;
if (!s || !*s)
return NULL;
len = length ? *length : strlen(s);
retval = alloca(len + 1);
for (p1 = s, p2 = retval; len--; p1++, p2++)
{
if (*p1 == '%' && len >= 2 &&
(((val1 = XTOI(p1[1])) != -1) &&
((val2 = XTOI(p1[2])) != -1)))
{
p1++, p1++;
len--, len--;
*p2 = (val1 << 4) | val2;
}
else
*p2 = *p1;
}
*p2 = 0;
if (length)
*length = p2 - retval;
return memcpy(s, retval, p2 - retval + 1);
}
/*
* quote_it: This quotes the given string making it sendable via irc. A
* pointer to the length of the data is required and the data need not be
* null terminated (it can contain nulls). Returned is a malloced, null
* terminated string.
*/
char *enquote_it (char *str, size_t len)
{
char *buffer = new_malloc(len + 5);
char *ptr = buffer;
size_t i;
int size = len;
for (i = 0; i < len; i++)
{
if (ptr-buffer >= size)
{
int j = ptr-buffer;
size += 256;
RESIZE(buffer, char, size + 5);
ptr = buffer + j;
}
switch (str[i])
{
case CTCP_DELIM_CHAR: *ptr++ = CTCP_QUOTE_CHAR;
*ptr++ = 'a';
break;
case '\n': *ptr++ = CTCP_QUOTE_CHAR;
*ptr++ = 'n';
break;
case '\r': *ptr++ = CTCP_QUOTE_CHAR;
*ptr++ = 'r';
break;
case CTCP_QUOTE_CHAR: *ptr++ = CTCP_QUOTE_CHAR;
*ptr++ = CTCP_QUOTE_CHAR;
break;
case '\0': *ptr++ = CTCP_QUOTE_CHAR;
*ptr++ = '0';
break;
default: *ptr++ = str[i];
break;
}
}
*ptr = '\0';
return buffer;
}
/*
* ctcp_unquote_it: This takes a null terminated string that had previously
* been quoted using ctcp_quote_it and unquotes it. Returned is a malloced
* space pointing to the unquoted string. NOTE: a trailing null is added for
* convenied, but the returned data may contain nulls!. The len is modified
* to contain the size of the data returned.
*/
char *dequote_it (char *str, size_t *len)
{
char *buffer;
char *ptr;
char c;
size_t i, new_size = 0;
buffer = (char *) new_malloc(sizeof(char) * *len + 1);
ptr = buffer;
i = 0;
while (i < *len)
{
if ((c = str[i++]) == CTCP_QUOTE_CHAR)
{
switch (c = str[i++])
{
case CTCP_QUOTE_CHAR:
*ptr++ = CTCP_QUOTE_CHAR;
break;
case 'a':
*ptr++ = CTCP_DELIM_CHAR;
break;
case 'n':
*ptr++ = '\n';
break;
case 'r':
*ptr++ = '\r';
break;
case '0':
*ptr++ = '\0';
break;
default:
*ptr++ = c;
break;
}
}
else
*ptr++ = c;
new_size++;
}
*ptr = '\0';
*len = new_size;
return (buffer);
}
const char * my_strerror (int err1, int err2)
{
static char buffer[1024];
if (err1 == -1)
{
if (err2 < 0)
{
#ifdef HAVE_HSTRERROR
return hstrerror(h_errno);
#else
return "Hostname lookup failure";
#endif
}
else
return strerror(errno);
}
else if (err1 == -2)
return "The operation is not supported for the protocol family";
else if (err1 == -3)
return "The hostname has no address in the protocol family";
else if (err1 == -4)
return "The presentation internet address was invalid";
else if (err1 == -5)
return "The hostname does not resolve";
else if (err1 == -6)
return "There is no virtual host for the protocol family";
else if (err1 == -7)
return "The remote peer to connect to was not provided";
else if (err1 == -8)
return "The local and remote address are in different protocol families.";
else if (err1 == -9)
return "Connection was not successful (may have timed out)";
else if (err1 == -10)
return "Requested local port is not available.";
else if (err1 == -11)
{
snprintf(buffer, 1024, "Connect failed: %s", strerror(err2));
return buffer;
}
else if (err1 == -12)
return "Connection was not successful (may have been reset)";
else if (err1 == -13)
return "The local address to bind to was not provided";
else if (err1 == -14)
return "The protocol family does not make sense";
else
{
snprintf(buffer, 1024, "EPIC Network Error %d", err1);
return buffer;
}
}
/*
* Should I switch over to using getaddrinfo() directly or is using
* inet_strton() sufficient?
*/
char * switch_hostname (const char *new_hostname)
{
char * retval = NULL;
ISA new_4;
char v4_name[1024];
int accept4 = 0;
#ifdef INET6
ISA6 new_6;
#endif
char v6_name[1024];
int accept6 = 0;
if (new_hostname == NULL)
{
new_free(&LocalIPv4Addr);
#ifdef INET6
new_free(&LocalIPv6Addr);
#endif
if (LocalHostName)
malloc_sprintf(&retval, "Virtual Hostname [%s] will no longer be used", LocalHostName);
else
malloc_sprintf(&retval, "Virtual Hostname support is not activated");
new_free(&LocalHostName);
return retval;
}
strlcpy(v4_name, "<none>", sizeof v4_name);
new_4.sin_family = AF_INET;
if (!inet_strton(new_hostname, zero, (SA *)&new_4, 0)) {
inet_ntostr((SA *)&new_4, v4_name, 1024, NULL, 0, NI_NUMERICHOST);
accept4 = 1;
}
strlcpy(v6_name, "<none>", sizeof v6_name);
#ifdef INET6
new_6.sin6_family = AF_INET6;
if (!inet_strton(new_hostname, zero, (SA *)&new_6, 0)) {
inet_ntostr((SA *)&new_6, v6_name, 1024, NULL, 0, NI_NUMERICHOST);
accept6 = 1;
}
#endif
if (accept4 || accept6)
{
new_free(&LocalIPv4Addr);
#ifdef INET6
new_free(&LocalIPv6Addr);
#endif
if (accept4)
{
LocalIPv4Addr = (ISA *)new_malloc(sizeof(*LocalIPv4Addr));
*LocalIPv4Addr = new_4;
}
#ifdef INET6
if (accept6)
{
LocalIPv6Addr = (ISA6 *)new_malloc(sizeof(*LocalIPv6Addr));
*LocalIPv6Addr = new_6;
}
#endif
malloc_strcpy(&LocalHostName, new_hostname);
malloc_sprintf(&retval, "Local address changed to [%s] (%s) (%s)",
LocalHostName, v4_name, v6_name);
}
else
malloc_sprintf(&retval, "I cannot configure [%s] -- local address not changed.", new_hostname);
return retval;
}
size_t strlcpy_c (char *dst, const char *src, size_t size, size_t *cluep)
{
size_t retval;
retval = strlcpy(dst, src, size);
if (retval > size - 1)
*cluep = size - 1;
else
*cluep = retval;
return retval;
}
size_t strlcat_c (char *dst, const char *src, size_t size, size_t *cluep)
{
size_t retval;
size_t real_size;
char * real_dst;
real_dst = dst + *cluep;
real_size = size - *cluep;
retval = strlcat(real_dst, src, real_size);
if (retval + *cluep > size - 1)
*cluep = size - 1;
else
*cluep = retval + *cluep;
return retval;
}
char * strlopencat_c (char *dest, size_t maxlen, size_t *cluep, ...)
{
va_list args;
char * this_arg = NULL;
size_t spare_clue;
if (cluep == NULL)
{
spare_clue = strlen(dest);
cluep = &spare_clue;
}
va_start(args, cluep);
for (;;)
{
/* Grab the next string, stop if no more */
if (!(this_arg = va_arg(args, char *)))
break;
/* Add this string to the end, adjusting the clue */
strlcat_c(dest, this_arg, maxlen, cluep);
/* If we reached the end of our space, stop here. */
if (*cluep >= maxlen - 1)
break;
}
va_end(args);
return dest;
}
int is_string_empty (const char *str)
{
while (str && *str && isspace(*str))
str++;
if (str && *str)
return 0;
return 1;
}
/*
* malloc_strcpy_c: Make a copy of a string into heap space, which may
* optionally be provided, with an optional clue.
*
* Arguments:
* 'ptr' - A pointer to a variable pointer that is either:
* 1) The value NULL or a valid heap pointer to space which is not
* large enough to hold 'src', in which case heap space will be
* allocated, and the original value of (*ptr) will be invalidated.
* 2) A valid heap pointer to space which is large enough to hold 'src'
* in which case 'src' will be copied to the heap space.
* 'src' - The string to be copied. If NULL, (*ptr) is invalidated (freed).
*
* Return value:
* If 'src' is NULL, an invalid heap pointer.
* If 'src' is not NULL, a valid heap pointer that contains a copy of 'src'.
* (*ptr) is set to the return value.
* This function will not return (panic) if (*ptr) is not NULL and is
* not a valid heap pointer.
*
* Notes:
* If (*ptr) is not big enough to hold 'src' then the original value (*ptr)
* will be invalidated and must not be used after this function returns.
* You must deallocate the space later by passing (ptr) to the new_free()
* function.
*/
char * malloc_strcpy_c (char **ptr, const char *src, size_t *clue)
{
size_t size, size_src;
if (!src)
{
if (clue)
*clue = 0;
return new_free(ptr); /* shrug */
}
if (*ptr)
{
size = alloc_size(*ptr);
if (size == (size_t) FREED_VAL)
panic("free()d pointer passed to malloc_strcpy");
/* No copy neccesary! */
if (*ptr == src)
return *ptr;
size_src = strlen(src);
if (size > size_src)
{
strlcpy(*ptr, src, size);
if (clue)
*clue = size_src;
return *ptr;
}
new_free(ptr);
}
size = strlen(src);
*ptr = new_malloc(size + 1);
strlcpy(*ptr, src, size + 1);
if (clue)
*clue = size;
return *ptr;
}
/*
* malloc_strcat_c: Append a copy of 'src' to the end of '*ptr', with an
* optional "clue" (length of (*ptr))
*
* Arguments:
* 'ptr' - A pointer to a variable pointer that is either:
* 1) The value NULL or a valid heap pointer to space which is not
* large enough to hold 'src', in which case heap space will be
* allocated, and the original value of (*ptr) will be invalidated.
* 2) A valid heap pointer to space which shall contain a valid
* nul-terminated C string.
* 'src' - The string to be copied. If NULL, this function is a no-op.
* 'cluep' - A pointer to an integer holding the string length of (*ptr).
* may be NULL.
*
* Return value:
* If 'src' is NULL, the original value of (*ptr) is returned.
* If 'src' is not NULL, a valid heap pointer that contains the catenation
* of the string originally contained in (*ptr) and 'src'.
* (*ptr) is set to the return value.
* This function will not return (panic) if (*ptr) is not NULL and is
* not a valid heap pointer.
* If 'cluep' is not NULL, it will be set to the string length of the
* new value of (*ptr).
*
* Notes:
* If (*ptr) is not big enough to take on the catenated string, then the
* original value (*ptr) will be invalidated and must not be used after
* this function returns.
* This function needs to determine how long (*ptr) is, and unless you
* provide 'cluep' it will do a strlen(*ptr). If (*ptr) is quite
* large, this could be an expensive operation. The use of a clue
* is an optimization option.
* If you don't want to bother with the 'clue', use the malloc_strcat macro.
* You must deallocate the space later by passing (ptr) to the new_free()
* function.
*/
char * malloc_strcat_c (char **ptr, const char *src, size_t *cluep)
{
size_t msize;
size_t psize;
size_t ssize;
size_t clue = cluep ? *cluep : 0;
if (*ptr)
{
if (alloc_size(*ptr) == FREED_VAL)
panic("free()d pointer passed to malloc_strcat");
if (!src)
return *ptr;
psize = clue + strlen(clue + *ptr);
ssize = strlen(src);
msize = psize + ssize + 1;
RESIZE(*ptr, char, msize);
if (cluep)
*cluep = psize + ssize;
strlcat(psize + *ptr, src, msize - psize);
return (*ptr);
}
return (*ptr = malloc_strdup(src));
}
/*
* malloc_strdup: Allocate and return a pointer to valid heap space into
* which a copy of 'str' has been placed.
*
* Arguments:
* 'str' - The string to be copied. If NULL, a zero length string will
* be copied in its place.
*
* Return value:
* If 'str' is not NULL, then a valid heap pointer containing a copy of 'str'.
* If 'str' is NULL, then a valid heap pointer containing a 0-length string.
*
* Notes:
* You must deallocate the space later by passing a pointer to the return
* value to the new_free() function.
*/
char * malloc_strdup (const char *str)
{
char *ptr;
size_t size;
if (!str)
str = empty_string;
size = strlen(str) + 1;
ptr = (char *)new_malloc(size);
strlcpy(ptr, str, size);
return ptr;
}
/*
* malloc_strdup2: Allocate and return a pointer to valid heap space into
* which a catenation of 'str1' and 'str2' have been placed.
*
* Arguments:
* 'str1' - The first string to be copied. If NULL, a zero-length string
* will be used in its place.
* 'str2' - The second string to be copied. If NULL, a zero-length string
* will be used in its place.
*
* Return value:
* A valid heap pointer containing a copy of 'str1' and 'str2' catenated
* together. 'str1' and 'str2' may be substituted as indicated above.
*
* Notes:
* You must deallocate the space later by passing a pointer to the return
* value to the new_free() function.
*/
char * malloc_strdup2 (const char *str1, const char *str2)
{
size_t msize;
char * buffer;
/* Prevent a crash. */
if (str1 == NULL)
str1 = empty_string;
if (str2 == NULL)
str2 = empty_string;
msize = strlen(str1) + strlen(str2) + 1;
buffer = (char *)new_malloc(msize);
*buffer = 0;
strlopencat_c(buffer, msize, NULL, str1, str2, NULL);
return buffer;
}
/*
* malloc_strdup3: Allocate and return a pointer to valid heap space into
* which a catenation of 'str1', 'str2', and 'str3' have been placed.
*
* Arguments:
* 'str1' - The first string to be copied. If NULL, a zero-length string
* will be used in its place.
* 'str2' - The second string to be copied. If NULL, a zero-length string
* will be used in its place.
* 'str3' - The third string to be copied. If NULL, a zero-length string
* will be used in its place.
*
* Return value:
* A valid heap pointer containing a copy of 'str1', 'str2', and 'str3'
* catenated together. 'str1', 'str2', and 'str3' may be substituted
* as indicated above.
*
* Notes:
* You must deallocate the space later by passing a pointer to the return
* value to the new_free() function.
*/
char * malloc_strdup3 (const char *str1, const char *str2, const char *str3)
{
size_t msize;
char *buffer;
if (!str1)
str1 = empty_string;
if (!str2)
str2 = empty_string;
if (!str3)
str3 = empty_string;
msize = strlen(str1) + strlen(str2) + strlen(str3) + 1;
buffer = (char *)new_malloc(msize);
*buffer = 0;
strlopencat_c(buffer, msize, NULL, str1, str2, str3, NULL);
return buffer;
}
/*
* malloc_strcat2_c: Append a copy of 'str1' and 'str2'' to the end of
* '*ptr', with an optional "clue" (length of (*ptr))
*
* Arguments:
* 'ptr' - A pointer to a variable pointer that is either NULL or a valid
* heap pointer which shall contain a valid C string.
* 'str1' - The first of two strings to be appended to '*ptr'.
* May be NULL.
* 'str2' - The second of two strings to be appended to '*ptr'.
* May be NULL.
* 'cluep' - A pointer to an integer holding the string length of (*ptr).
* May be NULL.
*
* Return value:
* The catenation of the three strings '*ptr', 'str1', and 'str2', except
* if either 'str1' or 'str2' are NULL, those values are ignored.
* (*ptr) is set to the return value.
* This function will not return (panic) if (*ptr) is not NULL and is
* not a valid heap pointer.
* If 'cluep' is not NULL, it will be set to the string length of the
* new value of (*ptr).
*
* Notes:
* The original value of (*ptr) is always invalidated by this function and
* may not be used after this function returns.
* This function needs to determine how long (*ptr) is, and unless you
* provide 'cluep' it will do a strlen(*ptr). If (*ptr) is quite
* large, this could be an expensive operation. The use of a clue
* is an optimization option.
* If you don't want to bother with the 'clue', use the malloc_strcat2 macro.
* You must deallocate the space later by passing (ptr) to the new_free()
* function.
*/
char * malloc_strcat2_c (char **ptr, const char *str1, const char *str2, size_t *clue)
{
size_t csize;
int msize;
csize = clue ? *clue : 0;
msize = csize;
if (*ptr)
{
if (alloc_size(*ptr) == FREED_VAL)
panic("free()d pointer passed to malloc_strcat2");
msize += strlen(csize + *ptr);
}
if (str1)
msize += strlen(str1);
if (str2)
msize += strlen(str2);
if (!*ptr)
{
*ptr = new_malloc(msize + 1);
**ptr = 0;
}
else
RESIZE(*ptr, char, msize + 1);
if (str1)
strlcat(csize + *ptr, str1, msize + 1 - csize);
if (str2)
strlcat(csize + *ptr, str2, msize + 1 - csize);
if (clue)
*clue = msize;
return *ptr;
}
/*
* malloc_strcat_wordlist_c: Append a word list to another word list using
* a delimiter, with an optional "clue" (length of (*ptr))
*
* Arguments:
* 'ptr' - A pointer to a variable pointer that is either NULL or a valid
* heap pointer which shall contain a valid C string which
* represents a word list (words separated by delimiters)
* 'word_delim' - The delimiter to use to separate (*ptr) from 'word_list'.
* May be NULL if no delimiter is desired.
* 'word_list' - The word list to append to (*ptr).
* May be NULL.
* 'cluep' - A pointer to an integer holding the string length of (*ptr).
* May be NULL.
*
* Return value:
* If "wordlist" is either NULL or a zero-length string, this function
* does nothing, and returns the original value of (*ptr).
* If "wordlist" is not NULL and not a zero-length string, and (*ptr) is
* either NULL or a zero-length string, (*ptr) is set to "wordlist",
* and the new value of (*ptr) is returned.
* If "wordlist" is not NULL and not a zero-length string, and (*ptr) is
* not NULL and not a zero-length string, (*ptr) is set to the
* catenation of (*ptr), 'word_delim', and 'wordlist' and is the
* return value.
* This function will not return (panic) if (*ptr) is not NULL and is
* not a valid heap pointer.
* If 'cluep' is not NULL, it will be set to the string length of the
* new value of (*ptr).
*
* Notes:
* The idea of this function is given two word lists, either of which
* may contain zero or more words, paste them together using a
* delimiter, which for word lists, is usually a space, but could
* be any character.
* Unless "wordlist" is NULL or a zero-length string, the original value
* of (*ptr) is invalidated and may not be used after this function
* returns.
* This function needs to determine how long (*ptr) is, and unless you
* provide 'cluep' it will do a strlen(*ptr). If (*ptr) is quite
* large, this could be an expensive operation. The use of a clue
* is an optimization option.
* If you don't want to bother with the 'clue', use the
* malloc_strcat_wordlist macro.
* You must deallocate the space later by passing (ptr) to the new_free()
* function.
* A WORD LIST IS CONSIDERED TO HAVE ONE ELEMENT IF IT HAS ANY CHARACTERS
* EVEN IF THAT CHARACTER IS A DELIMITER (ie, a space).
*/
char * malloc_strcat_wordlist_c (char **ptr, const char *word_delim, const char *wordlist, size_t *clue)
{
if (wordlist && *wordlist)
{
if (*ptr && **ptr)
return malloc_strcat2_c(ptr, word_delim, wordlist, clue);
else
return malloc_strcpy_c(ptr, wordlist, clue);
}
else
return *ptr;
}
char * malloc_strcat_word_c (char **ptr, const char *word_delim, const char *word, size_t *clue)
{
/* You MUST turn on extractw to get double quoted words */
if (!(x_debug & DEBUG_EXTRACTW))
return malloc_strcat_wordlist_c(ptr, word_delim, word, clue);
if (word && *word)
{
int quote_word = strpbrk(word, word_delim) ? 1 : 0;
#if 0
if (!*ptr || !**ptr)
malloc_strcpy_c(ptr, empty_string, clue);
#endif
if (*ptr && **ptr)
malloc_strcat_c(ptr, word_delim, clue);
if (quote_word)
malloc_strcat_c(ptr, "\"", clue);
malloc_strcat_c(ptr, word, clue);
if (quote_word)
malloc_strcat_c(ptr, "\"", clue);
}
return *ptr;
}
/*
* malloc_sprintf: write a formatted string to heap memory
*
* Arguments:
* 'ptr' - A NULL pointer, or a pointer to a variable pointer that is
* either NULL or a valid heap pointer which shall contain
* a valid C string which represents a word list (words
* separated by delimiters)
* 'format' - A *printf() format string
* ... - The rest of the arguments map to 'format' in the normal way for
* *printf() functions.
*
* Return value:
* If 'format' is NULL, The return value will be set to a valid heap pointer
* to a zero-length C string.
* If 'format' is not NULL, The return value will be set to a valid heap
* pointer sufficiently large to contain a C string of the form 'format',
* filled in with the rest of the arguments in accordance with sprintf().
* In either case, if ptr is not NULL, (*ptr) is set to the return value.
* This function will not return (panic) if ptr is not NULL, *ptr is not NULL,
* and is (*ptr) is not a valid heap pointer.
*
* Notes:
* This function has an arbitrarily limit of 20k on the return value.
* If the arguments passed do not match up with 'format', chaos may result.
* If ptr is not NULL then the original value of (*ptr) is invalidated and
* may not be used after this function returns.
* You must deallocate the space later by passing a pointer to the return
* value to the new_free() function.
*/
char * malloc_sprintf (char **ptr, const char *format, ...)
{
char booya[BIG_BUFFER_SIZE * 10 + 1];
*booya = 0;
if (format)
{
va_list args;
va_start(args, format);
vsnprintf(booya, sizeof booya, format, args);
va_end(args);
}
if (ptr)
{
malloc_strcpy(ptr, booya);
return *ptr;
}
else
return malloc_strdup(booya);
}
/*
* universal_next_arg_count: Remove the first "count" words from "str",
* where ``word'' is defined by all this scary text below here...
*
* Arguments:
* 'str' - A standard word list (words are separated by spaces only)
* This string will be modified!
* 'new_ptr' - A pointer to a variable pointer into which shall be placed
* the new start of 'str' after the words have been removed.
* It's customary to pass a pointer to "str" for this param.
* 'count' - The number of words to remove. If you want to remove one
* word, use the next_arg() macro.
* 'extended' - One of the three "DWORD" macros:
* DWORD_NEVER - Do not honor double-quoted words in 'str'
* DWORD_YES - Honor them if /xdebug extractw is on.
* DWORD_ALWAYS - Always honor double-quoted words in 'str'.
* 'dequote' - The double quotes that surround double-quoted words
* should be stripped from the return value.
*
* Definition:
* A "word" is either a "standard word" or a "double quoted word".
* A "standard word" is one or more characters that are not spaces, as
* defined by your locale's "isspace(3)" rules. A "standard word"
* is separated from other "standard words" by spaces, as defined
* by "isspace(3)". "Standard words" do not contain any spaces.
* A "double quoted word" is one or more characters that are surrounded
* by double quotes ("). A word is considered "double quoted" if it
* begins with a double quote that occurs at the start of the string,
* or immediately after one or more spaces as defined by isspace(3);
* and if it ends with a double quote that occurs at the end of the
* string or immediately before one or more spaces. Every word that
* is not a "double quoted word" as defined here is a Standard Word.
*
* If "extended" is DWORD_NEVER, or DWORD_YES and /xdebug extractw is off,
* then all words shall be treated as Standard Words, even if they
* are surrounded in double quotes.
*
* If "dequote" is 0, then any Double Quoted Words shall be modified to
* remove the double quotes that surround them. Dequoting multiple
* words is expensive.
*
* Return value:
* The first position of the first word in 'str'; the start of a string
* that includes the first 'count' words in 'str', perhaps modified
* by the above rules. Spaces between words are retained, but words
* before the first word and after the last word will be trimmed.
* Furthermore, because the whitespace character after the last word in
* the return value shall be filled in with a NUL character, the
* '*new_ptr' value will be set to the position after this NUL.
* Subject to the following conditions:
* If "str" is NULL or a zero-length string, NULL is returned.
* If "str" contains only spaces the return value shall be a
* zero-length string.
* If "str" does not contain more words than requested, The return
* value shall be as normal, but '*new_ptr' shall be set to
* a zero-length string.
*
* Notes:
* "Contain more words than requested" means if you request 1 word
* and there is only one word in 'str', then the return value
* will point to that word, but '*new_ptr' will be set to a zero
* length string.
* You can loop over a string, pulling each word off doing something
* like the following:
* while ((ptr = universal_next_arg_count(str, &str, 1, 1, 0))) {
* ... operate on ptr ...
* }
* 'str' will be modified, so if you need to remove words from a
* (const char *), make a LOCAL_COPY() of it first.
*
* There are some shorthand macros available:
* next_arg(char *str, char **new_ptr);
* next_arg_count(char *str, char **new_ptr, int count);
* new_next_arg(char *str, char **new_ptr);
* new_next_arg_count(char *str, char **new_ptr, int count);
*/
char * universal_next_arg_count (char *str, char **new_ptr, int count, int extended, int dequote, const char *delims)
{
size_t clue;
if (!str || !*str)
return NULL;
while (str && *str && my_isspace(*str))
str++;
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell(">>>> universal_next_arg_count: Start: [%s], count [%d], extended [%d], dequote [%d], delims [%s]", str, count, extended, dequote, delims);
real_move_to_abs_word(str, (const char **)new_ptr, count, extended, delims);
if (**new_ptr && *new_ptr > str)
(*new_ptr)[-1] = 0;
clue = (*new_ptr) - str - 1;
remove_trailing_spaces(str, &clue);
if (dequote)
dequoter(&str, &clue, count == 1 ? 0 : 1, extended, delims);
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell("<<<< universal_next_arg_count: End: [%s] [%s]",
str, *new_ptr);
#if 0 /* This can't possibly be right! */
if (!str || !*str)
return NULL;
#endif
return str;
}
/*
* dequoter: Remove double quotes around Double Quoted Words
*
* Arguments:
* 'str' - A pointer to a string that contains Double Quoted Words.
* Double quotes around Double Quoted Words in (*str) will be
* removed.
* 'clue' - A pointer to an integer holding the size of '*str'. You must
* provide this correct value. If '*str' is shortened, this
* value will be changed to reflect the new length of '*str'.
* 'full' - Assume '*str' contains more than one word and an exhaustive
* dequoting is neccessary. THIS IS VERY EXPENSIVE. If '*str'
* contains one word, this should be 0.
* 'extended' - The extended word policy for this string. This should
* usually be DWORD_ALWAYS unless you're doing something fancy.
*
* Return value:
* There is no return value, but '*str' and '*clue' may be modified as
* described in the above notes.
*
* Notes:
* None.
*/
void dequoter (char **str, size_t *clue, int full, int extended, const char *delims)
{
int simple;
char what;
size_t orig_size;
orig_size = *clue + 1;
/*
* Solve the problem of a string with one word...
*/
if (full == 0)
{
if (delims && delims[0] && delims[1] == 0)
{
simple = 1;
what = delims[0];
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell("#### dequoter: Dequoting [%s] simply with delim [%c]", *str, what);
}
else
{
simple = 0;
what = 255;
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell("#### dequoter: Dequoting [%s] fully with delims [%s]", *str, delims);
}
if (str && *str && ((simple == 1 && **str == what) ||
(simple == 0 && strchr(delims, **str))))
{
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell("#### dequoter: simple string starts with delim...");
if (*clue > 0 && ((simple == 1 && (*str)[*clue] == what) ||
(simple == 0 && strchr(delims, (*str)[*clue]))))
{
if (x_debug & DEBUG_EXTRACTW_DEBUG)
yell("#### dequoter: simple string ends with delim...");
/* Kill the closing quote. */
(*str)[*clue] = 0;
(*clue)--;
/* Kill the opening quote. */
(*str)++;
(*clue)--;
}
}
return;
}
/*
* I'm going to perdition for writing this, aren't I...
*/
else
{
char *orig_str; /* Where to start the dest string */
char *retval; /* our temp working buffer */
size_t rclue; /* A working clue for 'retval' */
char *this_word; /* The start of each word */
size_t this_len; /* How long the word is */
orig_str = *str; /* Keep this for later use */
retval = alloca(orig_size); /* Reserve space for a full copy */
*retval = 0; /* Prep retval for use... */
rclue = 0;
/*
* Solve the problem of dequoting N words iteratively by
* solving the problem for the first word and repeating
* until we've worked through the entire list. Then copy
* the results back to the original string.
*/
while ((this_word = universal_next_arg_count(*str, str, 1, extended, 0, delims)))
{
this_len = strlen(this_word) - 1;
dequoter(&this_word, &this_len, 0, extended, delims);
if (rclue > 0)
strlcat_c(retval, space, orig_size, &rclue);
strlcat_c(retval, this_word, orig_size, &rclue);
}
*orig_str = 0;
*clue = 0;
strlcpy_c(orig_str, retval, orig_size, clue);
*str = orig_str;
}
}
char * new_new_next_arg_count (char *str, char **new_ptr, char *type, int count)
{
char kludge[2];
/* Skip leading spaces, blah blah blah */
while (str && *str && my_isspace(*str))
str++;
if (!str || !*str)
return NULL;
if (*str == '\'')
*type = '\'';
else
*type = '"';
kludge[0] = *type;
kludge[1] = 0;
return universal_next_arg_count(str, new_ptr, 1, DWORD_ALWAYS, 1, kludge);
}
/*
* Note that the old version is now out of sync with epics word philosophy.
*/
char * safe_new_next_arg (char *str, char **new_ptr)
{
char * ret;
if (!(ret = new_next_arg(str, new_ptr)))
ret = empty_string;
return ret;
}
/*
* yanks off the last word from 'src'
* kinda the opposite of next_arg
*/
char * last_arg (char **src, size_t *cluep)
{
char *mark, *start, *end;
start = *src;
end = start + *cluep;
mark = end + strlen(end);
/* Always support double-quoted words. */
move_word_rel(start, (const char **)&mark, -1, DWORD_ALWAYS, "\"");
*cluep = (mark - *src - 1);
if (mark > start)
mark[-1] = 0;
else
*src = NULL; /* We're done, natch! */
return mark;
}
|