/usr/share/doc/glibc-doc/html/libc_25.html is in glibc-doc 2.15-0ubuntu10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.
This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10) .
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
-->
<!-- Created on April 20, 2012 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 25. The Basic Program/System Interface</title>
<meta name="description" content="The GNU C Library: 25. The Basic Program/System Interface">
<meta name="keywords" content="The GNU C Library: 25. The Basic Program/System Interface">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Program-Basics"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_24.html#Blocking-in-BSD" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Arguments" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc_24.html#Signal-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-Basic-Program_002fSystem-Interface"></a>
<h1 class="chapter">25. The Basic Program/System Interface</h1>
<a name="index-process"></a>
<a name="index-program"></a>
<a name="index-address-space-1"></a>
<a name="index-thread-of-control"></a>
<p><em>Processes</em> are the primitive units for allocation of system
resources. Each process has its own address space and (usually) one
thread of control. A process executes a program; you can have multiple
processes executing the same program, but each process has its own copy
of the program within its own address space and executes it
independently of the other copies. Though it may have multiple threads
of control within the same program and a program may be composed of
multiple logically separate modules, a process always executes exactly
one program.
</p>
<p>Note that we are using a specific definition of “program” for the
purposes of this manual, which corresponds to a common definition in the
context of Unix system. In popular usage, “program” enjoys a much
broader definition; it can refer for example to a system’s kernel, an
editor macro, a complex package of software, or a discrete section of
code executing within a process.
</p>
<p>Writing the program is what this manual is all about. This chapter
explains the most basic interface between your program and the system
that runs, or calls, it. This includes passing of parameters (arguments
and environment) from the system, requesting basic services from the
system, and telling the system the program is done.
</p>
<p>A program starts another program with the <code>exec</code> family of system calls.
This chapter looks at program startup from the execee’s point of view. To
see the event from the execor’s point of view, see <a href="libc_26.html#Executing-a-File">Executing a File</a>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Program-Arguments">25.1 Program Arguments</a></td><td> </td><td align="left" valign="top"> Parsing your program’s command-line arguments.
</td></tr>
<tr><td align="left" valign="top"><a href="#Environment-Variables">25.4 Environment Variables</a></td><td> </td><td align="left" valign="top"> Less direct parameters affecting your program
</td></tr>
<tr><td align="left" valign="top"><a href="#System-Calls">25.5 System Calls</a></td><td> </td><td align="left" valign="top"> Requesting service from the system
</td></tr>
<tr><td align="left" valign="top"><a href="#Program-Termination">25.6 Program Termination</a></td><td> </td><td align="left" valign="top"> Telling the system you’re done; return status
</td></tr>
</table>
<hr size="6">
<a name="Program-Arguments"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Program-Basics" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argument-Syntax" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Program-Arguments-1"></a>
<h2 class="section">25.1 Program Arguments</h2>
<a name="index-program-arguments"></a>
<a name="index-command-line-arguments"></a>
<a name="index-arguments_002c-to-program"></a>
<a name="index-program-startup"></a>
<a name="index-startup-of-program"></a>
<a name="index-invocation-of-program"></a>
<a name="index-main-function"></a>
<a name="index-main"></a>
<p>The system starts a C program by calling the function <code>main</code>. It
is up to you to write a function named <code>main</code>—otherwise, you
won’t even be able to link your program without errors.
</p>
<p>In ISO C you can define <code>main</code> either to take no arguments, or to
take two arguments that represent the command line arguments to the
program, like this:
</p>
<table><tr><td> </td><td><pre class="smallexample">int main (int <var>argc</var>, char *<var>argv</var>[])
</pre></td></tr></table>
<a name="index-argc-_0028program-argument-count_0029"></a>
<a name="index-argv-_0028program-argument-vector_0029"></a>
<p>The command line arguments are the whitespace-separated tokens given in
the shell command used to invoke the program; thus, in ‘<samp>cat foo
bar</samp>’, the arguments are ‘<samp>foo</samp>’ and ‘<samp>bar</samp>’. The only way a
program can look at its command line arguments is via the arguments of
<code>main</code>. If <code>main</code> doesn’t take arguments, then you cannot get
at the command line.
</p>
<p>The value of the <var>argc</var> argument is the number of command line
arguments. The <var>argv</var> argument is a vector of C strings; its
elements are the individual command line argument strings. The file
name of the program being run is also included in the vector as the
first element; the value of <var>argc</var> counts this element. A null
pointer always follows the last element: <code><var>argv</var>[<var>argc</var>]</code>
is this null pointer.
</p>
<p>For the command ‘<samp>cat foo bar</samp>’, <var>argc</var> is 3 and <var>argv</var> has
three elements, <code>"cat"</code>, <code>"foo"</code> and <code>"bar"</code>.
</p>
<p>In Unix systems you can define <code>main</code> a third way, using three arguments:
</p>
<table><tr><td> </td><td><pre class="smallexample">int main (int <var>argc</var>, char *<var>argv</var>[], char *<var>envp</var>[])
</pre></td></tr></table>
<p>The first two arguments are just the same. The third argument
<var>envp</var> gives the program’s environment; it is the same as the value
of <code>environ</code>. See section <a href="#Environment-Variables">Environment Variables</a>. POSIX.1 does not
allow this three-argument form, so to be portable it is best to write
<code>main</code> to take two arguments, and use the value of <code>environ</code>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argument-Syntax">25.1.1 Program Argument Syntax Conventions</a></td><td> </td><td align="left" valign="top"> By convention, options start with a hyphen.
</td></tr>
<tr><td align="left" valign="top"><a href="#Parsing-Program-Arguments">25.1.2 Parsing Program Arguments</a></td><td> </td><td align="left" valign="top"> Ways to parse program options and arguments.
</td></tr>
</table>
<hr size="6">
<a name="Argument-Syntax"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Program-Arguments" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Parsing-Program-Arguments" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Arguments" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Program-Argument-Syntax-Conventions"></a>
<h3 class="subsection">25.1.1 Program Argument Syntax Conventions</h3>
<a name="index-program-argument-syntax"></a>
<a name="index-syntax_002c-for-program-arguments"></a>
<a name="index-command-argument-syntax"></a>
<p>POSIX recommends these conventions for command line arguments.
<code>getopt</code> (see section <a href="#Getopt">Parsing program options using <code>getopt</code></a>) and <code>argp_parse</code> (see section <a href="#Argp">Parsing Program Options with Argp</a>) make
it easy to implement them.
</p>
<ul>
<li>
Arguments are options if they begin with a hyphen delimiter (‘<samp>-</samp>’).
</li><li>
Multiple options may follow a hyphen delimiter in a single token if
the options do not take arguments. Thus, ‘<samp>-abc</samp>’ is equivalent to
‘<samp>-a -b -c</samp>’.
</li><li>
Option names are single alphanumeric characters (as for <code>isalnum</code>;
see section <a href="libc_4.html#Classification-of-Characters">Classification of Characters</a>).
</li><li>
Certain options require an argument. For example, the ‘<samp>-o</samp>’ command
of the <code>ld</code> command requires an argument—an output file name.
</li><li>
An option and its argument may or may not appear as separate tokens. (In
other words, the whitespace separating them is optional.) Thus,
‘<samp>-o foo</samp>’ and ‘<samp>-ofoo</samp>’ are equivalent.
</li><li>
Options typically precede other non-option arguments.
<p>The implementations of <code>getopt</code> and <code>argp_parse</code> in the GNU C
library normally make it appear as if all the option arguments were
specified before all the non-option arguments for the purposes of
parsing, even if the user of your program intermixed option and
non-option arguments. They do this by reordering the elements of the
<var>argv</var> array. This behavior is nonstandard; if you want to suppress
it, define the <code>_POSIX_OPTION_ORDER</code> environment variable.
See section <a href="#Standard-Environment">Standard Environment Variables</a>.
</p>
</li><li>
The argument ‘<samp>--</samp>’ terminates all options; any following arguments
are treated as non-option arguments, even if they begin with a hyphen.
</li><li>
A token consisting of a single hyphen character is interpreted as an
ordinary non-option argument. By convention, it is used to specify
input from or output to the standard input and output streams.
</li><li>
Options may be supplied in any order, or appear multiple times. The
interpretation is left up to the particular application program.
</li></ul>
<a name="index-long_002dnamed-options"></a>
<p>GNU adds <em>long options</em> to these conventions. Long options consist
of ‘<samp>--</samp>’ followed by a name made of alphanumeric characters and
dashes. Option names are typically one to three words long, with
hyphens to separate words. Users can abbreviate the option names as
long as the abbreviations are unique.
</p>
<p>To specify an argument for a long option, write
‘<samp>--<var>name</var>=<var>value</var></samp>’. This syntax enables a long option to
accept an argument that is itself optional.
</p>
<p>Eventually, the GNU system will provide completion for long option names
in the shell.
</p>
<hr size="6">
<a name="Parsing-Program-Arguments"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argument-Syntax" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Arguments" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-Program-Arguments-1"></a>
<h3 class="subsection">25.1.2 Parsing Program Arguments</h3>
<a name="index-program-arguments_002c-parsing"></a>
<a name="index-command-arguments_002c-parsing"></a>
<a name="index-parsing-program-arguments"></a>
<p>If the syntax for the command line arguments to your program is simple
enough, you can simply pick the arguments off from <var>argv</var> by hand.
But unless your program takes a fixed number of arguments, or all of the
arguments are interpreted in the same way (as file names, for example),
you are usually better off using <code>getopt</code> (see section <a href="#Getopt">Parsing program options using <code>getopt</code></a>) or
<code>argp_parse</code> (see section <a href="#Argp">Parsing Program Options with Argp</a>) to do the parsing.
</p>
<p><code>getopt</code> is more standard (the short-option only version of it is a
part of the POSIX standard), but using <code>argp_parse</code> is often
easier, both for very simple and very complex option structures, because
it does more of the dirty work for you.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Getopt">25.2 Parsing program options using <code>getopt</code></a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Argp">25.3 Parsing Program Options with Argp</a></td><td> </td><td align="left" valign="top"> Parsing program options using <code>argp_parse</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Suboptions">25.3.12.1 Parsing of Suboptions</a></td><td> </td><td align="left" valign="top"> Some programs need more detailed options.
</td></tr>
<tr><td align="left" valign="top"><a href="#Suboptions-Example">25.3.13 Parsing of Suboptions Example</a></td><td> </td><td align="left" valign="top"> This shows how it could be done for <code>mount</code>.
</td></tr>
</table>
<hr size="6">
<a name="Getopt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Parsing-Program-Arguments" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Using-Getopt" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-program-options-using-getopt"></a>
<h2 class="section">25.2 Parsing program options using <code>getopt</code></h2>
<p>The <code>getopt</code> and <code>getopt_long</code> functions automate some of the
chore involved in parsing typical unix command line options.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Using-Getopt">25.2.1 Using the <code>getopt</code> function</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Example-of-Getopt">25.2.2 Example of Parsing Arguments with <code>getopt</code></a></td><td> </td><td align="left" valign="top"> An example of parsing options with <code>getopt</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Getopt-Long-Options">25.2.3 Parsing Long Options with <code>getopt_long</code></a></td><td> </td><td align="left" valign="top"> GNU suggests utilities accept long-named
options; here is one way to do.
</td></tr>
<tr><td align="left" valign="top"><a href="#Getopt-Long-Option-Example">25.2.4 Example of Parsing Long Options with <code>getopt_long</code></a></td><td> </td><td align="left" valign="top"> An example of using <code>getopt_long</code>.
</td></tr>
</table>
<hr size="6">
<a name="Using-Getopt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Getopt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Example-of-Getopt" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-the-getopt-function"></a>
<h3 class="subsection">25.2.1 Using the <code>getopt</code> function</h3>
<p>Here are the details about how to call the <code>getopt</code> function. To
use this facility, your program must include the header file
‘<tt>unistd.h</tt>’.
<a name="index-unistd_002eh-15"></a>
</p>
<dl>
<dt><a name="index-opterr"></a><u>Variable:</u> int <b>opterr</b></dt>
<dd><p>If the value of this variable is nonzero, then <code>getopt</code> prints an
error message to the standard error stream if it encounters an unknown
option character or an option with a missing required argument. This is
the default behavior. If you set this variable to zero, <code>getopt</code>
does not print any messages, but it still returns the character <code>?</code>
to indicate an error.
</p></dd></dl>
<dl>
<dt><a name="index-optopt"></a><u>Variable:</u> int <b>optopt</b></dt>
<dd><p>When <code>getopt</code> encounters an unknown option character or an option
with a missing required argument, it stores that option character in
this variable. You can use this for providing your own diagnostic
messages.
</p></dd></dl>
<dl>
<dt><a name="index-optind"></a><u>Variable:</u> int <b>optind</b></dt>
<dd><p>This variable is set by <code>getopt</code> to the index of the next element
of the <var>argv</var> array to be processed. Once <code>getopt</code> has found
all of the option arguments, you can use this variable to determine
where the remaining non-option arguments begin. The initial value of
this variable is <code>1</code>.
</p></dd></dl>
<dl>
<dt><a name="index-optarg"></a><u>Variable:</u> char * <b>optarg</b></dt>
<dd><p>This variable is set by <code>getopt</code> to point at the value of the
option argument, for those options that accept arguments.
</p></dd></dl>
<dl>
<dt><a name="index-getopt"></a><u>Function:</u> int <b>getopt</b><i> (int <var>argc</var>, char **<var>argv</var>, const char *<var>options</var>)</i></dt>
<dd><p>The <code>getopt</code> function gets the next option argument from the
argument list specified by the <var>argv</var> and <var>argc</var> arguments.
Normally these values come directly from the arguments received by
<code>main</code>.
</p>
<p>The <var>options</var> argument is a string that specifies the option
characters that are valid for this program. An option character in this
string can be followed by a colon (‘<samp>:</samp>’) to indicate that it takes a
required argument. If an option character is followed by two colons
(‘<samp>::</samp>’), its argument is optional; this is a GNU extension.
</p>
<p><code>getopt</code> has three ways to deal with options that follow
non-options <var>argv</var> elements. The special argument ‘<samp>--</samp>’ forces
in all cases the end of option scanning.
</p>
<ul>
<li>
The default is to permute the contents of <var>argv</var> while scanning it
so that eventually all the non-options are at the end. This allows
options to be given in any order, even with programs that were not
written to expect this.
</li><li>
If the <var>options</var> argument string begins with a hyphen (‘<samp>-</samp>’), this
is treated specially. It permits arguments that are not options to be
returned as if they were associated with option character ‘<samp>\1</samp>’.
</li><li>
POSIX demands the following behavior: The first non-option stops option
processing. This mode is selected by either setting the environment
variable <code>POSIXLY_CORRECT</code> or beginning the <var>options</var> argument
string with a plus sign (‘<samp>+</samp>’).
</li></ul>
<p>The <code>getopt</code> function returns the option character for the next
command line option. When no more option arguments are available, it
returns <code>-1</code>. There may still be more non-option arguments; you
must compare the external variable <code>optind</code> against the <var>argc</var>
parameter to check this.
</p>
<p>If the option has an argument, <code>getopt</code> returns the argument by
storing it in the variable <var>optarg</var>. You don’t ordinarily need to
copy the <code>optarg</code> string, since it is a pointer into the original
<var>argv</var> array, not into a static area that might be overwritten.
</p>
<p>If <code>getopt</code> finds an option character in <var>argv</var> that was not
included in <var>options</var>, or a missing option argument, it returns
‘<samp>?</samp>’ and sets the external variable <code>optopt</code> to the actual
option character. If the first character of <var>options</var> is a colon
(‘<samp>:</samp>’), then <code>getopt</code> returns ‘<samp>:</samp>’ instead of ‘<samp>?</samp>’ to
indicate a missing option argument. In addition, if the external
variable <code>opterr</code> is nonzero (which is the default), <code>getopt</code>
prints an error message.
</p></dd></dl>
<hr size="6">
<a name="Example-of-Getopt"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-Getopt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt-Long-Options" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Example-of-Parsing-Arguments-with-getopt"></a>
<h3 class="subsection">25.2.2 Example of Parsing Arguments with <code>getopt</code></h3>
<p>Here is an example showing how <code>getopt</code> is typically used. The
key points to notice are:
</p>
<ul>
<li>
Normally, <code>getopt</code> is called in a loop. When <code>getopt</code> returns
<code>-1</code>, indicating no more options are present, the loop terminates.
</li><li>
A <code>switch</code> statement is used to dispatch on the return value from
<code>getopt</code>. In typical use, each case just sets a variable that
is used later in the program.
</li><li>
A second loop is used to process the remaining non-option arguments.
</li></ul>
<table><tr><td> </td><td><pre class="smallexample">#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
</pre><pre class="smallexample">
</pre><pre class="smallexample"> while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
</pre><pre class="smallexample">
</pre><pre class="smallexample"> printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
</pre></td></tr></table>
<p>Here are some examples showing what this program prints with different
combinations of arguments:
</p>
<table><tr><td> </td><td><pre class="smallexample">% testopt
aflag = 0, bflag = 0, cvalue = (null)
% testopt -a -b
aflag = 1, bflag = 1, cvalue = (null)
% testopt -ab
aflag = 1, bflag = 1, cvalue = (null)
% testopt -c foo
aflag = 0, bflag = 0, cvalue = foo
% testopt -cfoo
aflag = 0, bflag = 0, cvalue = foo
% testopt arg1
aflag = 0, bflag = 0, cvalue = (null)
Non-option argument arg1
% testopt -a arg1
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument arg1
% testopt -c foo arg1
aflag = 0, bflag = 0, cvalue = foo
Non-option argument arg1
% testopt -a -- -b
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -b
% testopt -a -
aflag = 1, bflag = 0, cvalue = (null)
Non-option argument -
</pre></td></tr></table>
<hr size="6">
<a name="Getopt-Long-Options"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Example-of-Getopt" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt-Long-Option-Example" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-Long-Options-with-getopt_005flong"></a>
<h3 class="subsection">25.2.3 Parsing Long Options with <code>getopt_long</code></h3>
<p>To accept GNU-style long options as well as single-character options,
use <code>getopt_long</code> instead of <code>getopt</code>. This function is
declared in ‘<tt>getopt.h</tt>’, not ‘<tt>unistd.h</tt>’. You should make every
program accept long options if it uses any options, for this takes
little extra work and helps beginners remember how to use the program.
</p>
<dl>
<dt><a name="index-struct-option"></a><u>Data Type:</u> <b>struct option</b></dt>
<dd><p>This structure describes a single long option name for the sake of
<code>getopt_long</code>. The argument <var>longopts</var> must be an array of
these structures, one for each long option. Terminate the array with an
element containing all zeros.
</p>
<p>The <code>struct option</code> structure has these fields:
</p>
<dl compact="compact">
<dt> <code>const char *name</code></dt>
<dd><p>This field is the name of the option. It is a string.
</p>
</dd>
<dt> <code>int has_arg</code></dt>
<dd><p>This field says whether the option takes an argument. It is an integer,
and there are three legitimate values: <code>no_argument</code>,
<code>required_argument</code> and <code>optional_argument</code>.
</p>
</dd>
<dt> <code>int *flag</code></dt>
<dt> <code>int val</code></dt>
<dd><p>These fields control how to report or act on the option when it occurs.
</p>
<p>If <code>flag</code> is a null pointer, then the <code>val</code> is a value which
identifies this option. Often these values are chosen to uniquely
identify particular long options.
</p>
<p>If <code>flag</code> is not a null pointer, it should be the address of an
<code>int</code> variable which is the flag for this option. The value in
<code>val</code> is the value to store in the flag to indicate that the option
was seen.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-getopt_005flong"></a><u>Function:</u> int <b>getopt_long</b><i> (int <var>argc</var>, char *const *<var>argv</var>, const char *<var>shortopts</var>, const struct option *<var>longopts</var>, int *<var>indexptr</var>)</i></dt>
<dd><p>Decode options from the vector <var>argv</var> (whose length is <var>argc</var>).
The argument <var>shortopts</var> describes the short options to accept, just as
it does in <code>getopt</code>. The argument <var>longopts</var> describes the long
options to accept (see above).
</p>
<p>When <code>getopt_long</code> encounters a short option, it does the same
thing that <code>getopt</code> would do: it returns the character code for the
option, and stores the options argument (if it has one) in <code>optarg</code>.
</p>
<p>When <code>getopt_long</code> encounters a long option, it takes actions based
on the <code>flag</code> and <code>val</code> fields of the definition of that
option.
</p>
<p>If <code>flag</code> is a null pointer, then <code>getopt_long</code> returns the
contents of <code>val</code> to indicate which option it found. You should
arrange distinct values in the <code>val</code> field for options with
different meanings, so you can decode these values after
<code>getopt_long</code> returns. If the long option is equivalent to a short
option, you can use the short option’s character code in <code>val</code>.
</p>
<p>If <code>flag</code> is not a null pointer, that means this option should just
set a flag in the program. The flag is a variable of type <code>int</code>
that you define. Put the address of the flag in the <code>flag</code> field.
Put in the <code>val</code> field the value you would like this option to
store in the flag. In this case, <code>getopt_long</code> returns <code>0</code>.
</p>
<p>For any long option, <code>getopt_long</code> tells you the index in the array
<var>longopts</var> of the options definition, by storing it into
<code>*<var>indexptr</var></code>. You can get the name of the option with
<code><var>longopts</var>[*<var>indexptr</var>].name</code>. So you can distinguish among
long options either by the values in their <code>val</code> fields or by their
indices. You can also distinguish in this way among long options that
set flags.
</p>
<p>When a long option has an argument, <code>getopt_long</code> puts the argument
value in the variable <code>optarg</code> before returning. When the option
has no argument, the value in <code>optarg</code> is a null pointer. This is
how you can tell whether an optional argument was supplied.
</p>
<p>When <code>getopt_long</code> has no more options to handle, it returns
<code>-1</code>, and leaves in the variable <code>optind</code> the index in
<var>argv</var> of the next remaining argument.
</p></dd></dl>
<p>Since long option names were used before the <code>getopt_long</code>
options was invented there are program interfaces which require programs
to recognize options like ‘<samp>-option value</samp>’ instead of
‘<samp>--option value</samp>’. To enable these programs to use the GNU
getopt functionality there is one more function available.
</p>
<dl>
<dt><a name="index-getopt_005flong_005fonly"></a><u>Function:</u> int <b>getopt_long_only</b><i> (int <var>argc</var>, char *const *<var>argv</var>, const char *<var>shortopts</var>, const struct option *<var>longopts</var>, int *<var>indexptr</var>)</i></dt>
<dd>
<p>The <code>getopt_long_only</code> function is equivalent to the
<code>getopt_long</code> function but it allows to specify the user of the
application to pass long options with only ‘<samp>-</samp>’ instead of
‘<samp>--</samp>’. The ‘<samp>--</samp>’ prefix is still recognized but instead of
looking through the short options if a ‘<samp>-</samp>’ is seen it is first
tried whether this parameter names a long option. If not, it is parsed
as a short option.
</p>
<p>Assuming <code>getopt_long_only</code> is used starting an application with
</p>
<table><tr><td> </td><td><pre class="smallexample"> app -foo
</pre></td></tr></table>
<p>the <code>getopt_long_only</code> will first look for a long option named
‘<samp>foo</samp>’. If this is not found, the short options ‘<samp>f</samp>’, ‘<samp>o</samp>’,
and again ‘<samp>o</samp>’ are recognized.
</p></dd></dl>
<hr size="6">
<a name="Getopt-Long-Option-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Getopt-Long-Options" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Getopt" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Example-of-Parsing-Long-Options-with-getopt_005flong"></a>
<h3 class="subsection">25.2.4 Example of Parsing Long Options with <code>getopt_long</code></h3>
<table><tr><td> </td><td><pre class="smallexample">#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/* <span class="roman">Flag set by ‘<samp>--verbose</samp>’.</span> */
static int verbose_flag;
int
main (argc, argv)
int argc;
char **argv;
{
int c;
while (1)
{
static struct option long_options[] =
{
/* <span class="roman">These options set a flag.</span> */
{"verbose", no_argument, &verbose_flag, 1},
{"brief", no_argument, &verbose_flag, 0},
/* <span class="roman">These options don't set a flag.
We distinguish them by their indices.</span> */
{"add", no_argument, 0, 'a'},
{"append", no_argument, 0, 'b'},
{"delete", required_argument, 0, 'd'},
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
/* <span class="roman"><code>getopt_long</code> stores the option index here.</span> */
int option_index = 0;
c = getopt_long (argc, argv, "abc:d:f:",
long_options, &option_index);
/* <span class="roman">Detect the end of the options.</span> */
if (c == -1)
break;
switch (c)
{
case 0:
/* <span class="roman">If this option set a flag, do nothing else now.</span> */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'a':
puts ("option -a\n");
break;
case 'b':
puts ("option -b\n");
break;
case 'c':
printf ("option -c with value `%s'\n", optarg);
break;
case 'd':
printf ("option -d with value `%s'\n", optarg);
break;
case 'f':
printf ("option -f with value `%s'\n", optarg);
break;
case '?':
/* <span class="roman"><code>getopt_long</code> already printed an error message.</span> */
break;
default:
abort ();
}
}
/* <span class="roman">Instead of reporting ‘<samp>--verbose</samp>’
and ‘<samp>--brief</samp>’ as they are encountered,
we report the final status resulting from them.</span> */
if (verbose_flag)
puts ("verbose flag is set");
/* <span class="roman">Print any remaining command line arguments (not options).</span> */
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
putchar ('\n');
}
exit (0);
}
</pre></td></tr></table>
<hr size="6">
<a name="Argp"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Getopt-Long-Option-Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#The-argp_005fparse-Function" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-Program-Options-with-Argp"></a>
<h2 class="section">25.3 Parsing Program Options with Argp</h2>
<a name="index-argp-_0028program-argument-parser_0029"></a>
<a name="index-argument-parsing-with-argp"></a>
<a name="index-option-parsing-with-argp"></a>
<p><em>Argp</em> is an interface for parsing unix-style argument vectors.
See section <a href="#Program-Arguments">Program Arguments</a>.
</p>
<p>Argp provides features unavailable in the more commonly used
<code>getopt</code> interface. These features include automatically producing
output in response to the ‘<samp>--help</samp>’ and ‘<samp>--version</samp>’ options, as
described in the GNU coding standards. Using argp makes it less likely
that programmers will neglect to implement these additional options or
keep them up to date.
</p>
<p>Argp also provides the ability to merge several independently defined
option parsers into one, mediating conflicts between them and making the
result appear seamless. A library can export an argp option parser that
user programs might employ in conjunction with their own option parsers,
resulting in less work for the user programs. Some programs may use only
argument parsers exported by libraries, thereby achieving consistent and
efficient option-parsing for abstractions implemented by the libraries.
</p>
<a name="index-argp_002eh"></a>
<p>The header file ‘<tt><argp.h></tt>’ should be included to use argp.
</p>
<hr size="6">
<a name="The-argp_005fparse-Function"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Global-Variables" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">25.3.1 The <code>argp_parse</code> Function</h3>
<p>The main interface to argp is the <code>argp_parse</code> function. In many
cases, calling <code>argp_parse</code> is the only argument-parsing code
needed in <code>main</code>.
See section <a href="#Program-Arguments">Program Arguments</a>.
</p>
<dl>
<dt><a name="index-argp_005fparse"></a><u>Function:</u> error_t <b>argp_parse</b><i> (const struct argp *<var>argp</var>, int <var>argc</var>, char **<var>argv</var>, unsigned <var>flags</var>, int *<var>arg_index</var>, void *<var>input</var>)</i></dt>
<dd><p>The <code>argp_parse</code> function parses the arguments in <var>argv</var>, of
length <var>argc</var>, using the argp parser <var>argp</var>. See section <a href="#Argp-Parsers">Specifying Argp Parsers</a>.
</p>
<p>A value of zero is the same as a <code>struct argp</code>containing all
zeros. <var>flags</var> is a set of flag bits that modify the parsing
behavior. See section <a href="#Argp-Flags">Flags for <code>argp_parse</code></a>. <var>input</var> is passed through to the argp
parser <var>argp</var>, and has meaning defined by <var>argp</var>. A typical
usage is to pass a pointer to a structure which is used for specifying
parameters to the parser and passing back the results.
</p>
<p>Unless the <code>ARGP_NO_EXIT</code> or <code>ARGP_NO_HELP</code> flags are included
in <var>flags</var>, calling <code>argp_parse</code> may result in the program
exiting. This behavior is true if an error is detected, or when an
unknown option is encountered. See section <a href="#Program-Termination">Program Termination</a>.
</p>
<p>If <var>arg_index</var> is non-null, the index of the first unparsed option
in <var>argv</var> is returned as a value.
</p>
<p>The return value is zero for successful parsing, or an error code
(see section <a href="libc_2.html#Error-Codes">Error Codes</a>) if an error is detected. Different argp parsers
may return arbitrary error codes, but the standard error codes are:
<code>ENOMEM</code> if a memory allocation error occurred, or <code>EINVAL</code> if
an unknown option or option argument is encountered.
</p></dd></dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Global-Variables">25.3.2 Argp Global Variables</a></td><td> </td><td align="left" valign="top"> Global argp parameters.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Parsers">25.3.3 Specifying Argp Parsers</a></td><td> </td><td align="left" valign="top"> Defining parsers for use with <code>argp_parse</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Flags">25.3.7 Flags for <code>argp_parse</code></a></td><td> </td><td align="left" valign="top"> Flags that modify the behavior of <code>argp_parse</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Help">25.3.9 The <code>argp_help</code> Function</a></td><td> </td><td align="left" valign="top"> Printing help messages when not parsing.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Examples">25.3.11 Argp Examples</a></td><td> </td><td align="left" valign="top"> Simple examples of programs using argp.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-User-Customization">25.3.12 Argp User Customization</a></td><td> </td><td align="left" valign="top">
Users may control the ‘<samp>--help</samp>’ output format.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Global-Variables"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#The-argp_005fparse-Function" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parsers" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Argp-Global-Variables-1"></a>
<h3 class="subsection">25.3.2 Argp Global Variables</h3>
<p>These variables make it easy for user programs to implement the
‘<samp>--version</samp>’ option and provide a bug-reporting address in the
‘<samp>--help</samp>’ output. These are implemented in argp by default.
</p>
<dl>
<dt><a name="index-argp_005fprogram_005fversion"></a><u>Variable:</u> const char * <b>argp_program_version</b></dt>
<dd><p>If defined or set by the user program to a non-zero value, then a
‘<samp>--version</samp>’ option is added when parsing with <code>argp_parse</code>,
which will print the ‘<samp>--version</samp>’ string followed by a newline and
exit. The exception to this is if the <code>ARGP_NO_EXIT</code> flag is used.
</p></dd></dl>
<dl>
<dt><a name="index-argp_005fprogram_005fbug_005faddress"></a><u>Variable:</u> const char * <b>argp_program_bug_address</b></dt>
<dd><p>If defined or set by the user program to a non-zero value,
<code>argp_program_bug_address</code> should point to a string that will be
printed at the end of the standard output for the ‘<samp>--help</samp>’ option,
embedded in a sentence that says ‘<samp>Report bugs to <var>address</var>.</samp>’.
</p></dd></dl>
<dl>
<dt><a name="index-argp_005fprogram_005fversion_005fhook"></a><u>Variable:</u> <b>argp_program_version_hook</b></dt>
<dd><p>If defined or set by the user program to a non-zero value, a
‘<samp>--version</samp>’ option is added when parsing with <code>arg_parse</code>,
which prints the program version and exits with a status of zero. This
is not the case if the <code>ARGP_NO_HELP</code> flag is used. If the
<code>ARGP_NO_EXIT</code> flag is set, the exit behavior of the program is
suppressed or modified, as when the argp parser is going to be used by
other programs.
</p>
<p>It should point to a function with this type of signature:
</p>
<table><tr><td> </td><td><pre class="smallexample">void <var>print-version</var> (FILE *<var>stream</var>, struct argp_state *<var>state</var>)
</pre></td></tr></table>
<p>See section <a href="#Argp-Parsing-State">Argp Parsing State</a>, for an explanation of <var>state</var>.
</p>
<p>This variable takes precedence over <code>argp_program_version</code>, and is
useful if a program has version information not easily expressed in a
simple string.
</p></dd></dl>
<dl>
<dt><a name="index-argp_005ferr_005fexit_005fstatus"></a><u>Variable:</u> error_t <b>argp_err_exit_status</b></dt>
<dd><p>This is the exit status used when argp exits due to a parsing error. If
not defined or set by the user program, this defaults to:
<code>EX_USAGE</code> from ‘<tt><sysexits.h></tt>’.
</p></dd></dl>
<hr size="6">
<a name="Argp-Parsers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Global-Variables" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Option-Vectors" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Specifying-Argp-Parsers"></a>
<h3 class="subsection">25.3.3 Specifying Argp Parsers</h3>
<p>The first argument to the <code>argp_parse</code> function is a pointer to a
<code>struct argp</code>, which is known as an <em>argp parser</em>:
</p>
<dl>
<dt><a name="index-struct-argp"></a><u>Data Type:</u> <b>struct argp</b></dt>
<dd><p>This structure specifies how to parse a given set of options and
arguments, perhaps in conjunction with other argp parsers. It has the
following fields:
</p>
<dl compact="compact">
<dt> <code>const struct argp_option *options</code></dt>
<dd><p>A pointer to a vector of <code>argp_option</code> structures specifying which
options this argp parser understands; it may be zero if there are no
options at all. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>.
</p>
</dd>
<dt> <code>argp_parser_t parser</code></dt>
<dd><p>A pointer to a function that defines actions for this parser; it is
called for each option parsed, and at other well-defined points in the
parsing process. A value of zero is the same as a pointer to a function
that always returns <code>ARGP_ERR_UNKNOWN</code>. See section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>.
</p>
</dd>
<dt> <code>const char *args_doc</code></dt>
<dd><p>If non-zero, a string describing what non-option arguments are called by
this parser. This is only used to print the ‘<samp>Usage:</samp>’ message. If
it contains newlines, the strings separated by them are considered
alternative usage patterns and printed on separate lines. Lines after
the first are prefixed by ‘<samp> or: </samp>’ instead of ‘<samp>Usage:</samp>’.
</p>
</dd>
<dt> <code>const char *doc</code></dt>
<dd><p>If non-zero, a string containing extra text to be printed before and
after the options in a long help message, with the two sections
separated by a vertical tab (<code>'\v'</code>, <code>'\013'</code>) character. By
convention, the documentation before the options is just a short string
explaining what the program does. Documentation printed after the
options describe behavior in more detail.
</p>
</dd>
<dt> <code>const struct argp_child *children</code></dt>
<dd><p>A pointer to a vector of <code>argp_children</code> structures. This pointer
specifies which additional argp parsers should be combined with this
one. See section <a href="#Argp-Children">Combining Multiple Argp Parsers</a>.
</p>
</dd>
<dt> <code>char *(*help_filter)(int <var>key</var>, const char *<var>text</var>, void *<var>input</var>)</code></dt>
<dd><p>If non-zero, a pointer to a function that filters the output of help
messages. See section <a href="#Argp-Help-Filtering">Customizing Argp Help Output</a>.
</p>
</dd>
<dt> <code>const char *argp_domain</code></dt>
<dd><p>If non-zero, the strings used in the argp library are translated using
the domain described by this string. If zero, the current default domain
is used.
</p>
</dd>
</dl>
</dd></dl>
<p>Of the above group, <code>options</code>, <code>parser</code>, <code>args_doc</code>, and
the <code>doc</code> fields are usually all that are needed. If an argp
parser is defined as an initialized C variable, only the fields used
need be specified in the initializer. The rest will default to zero due
to the way C structure initialization works. This design is exploited in
most argp structures; the most-used fields are grouped near the
beginning, the unused fields left unspecified.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Option-Vectors">25.3.4 Specifying Options in an Argp Parser</a></td><td> </td><td align="left" valign="top"> Specifying options in an argp parser.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Parser-Functions">25.3.5 Argp Parser Functions</a></td><td> </td><td align="left" valign="top"> Defining actions for an argp parser.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Children">25.3.6 Combining Multiple Argp Parsers</a></td><td> </td><td align="left" valign="top"> Combining multiple argp parsers.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Help-Filtering">25.3.8 Customizing Argp Help Output</a></td><td> </td><td align="left" valign="top"> Customizing help output for an argp parser.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Option-Vectors"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Parsers" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Option-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Specifying-Options-in-an-Argp-Parser"></a>
<h3 class="subsection">25.3.4 Specifying Options in an Argp Parser</h3>
<p>The <code>options</code> field in a <code>struct argp</code> points to a vector of
<code>struct argp_option</code> structures, each of which specifies an option
that the argp parser supports. Multiple entries may be used for a single
option provided it has multiple names. This should be terminated by an
entry with zero in all fields. Note that when using an initialized C
array for options, writing <code>{ 0 }</code> is enough to achieve this.
</p>
<dl>
<dt><a name="index-struct-argp_005foption"></a><u>Data Type:</u> <b>struct argp_option</b></dt>
<dd><p>This structure specifies a single option that an argp parser
understands, as well as how to parse and document that option. It has
the following fields:
</p>
<dl compact="compact">
<dt> <code>const char *name</code></dt>
<dd><p>The long name for this option, corresponding to the long option
‘<samp>--<var>name</var></samp>’; this field may be zero if this option <em>only</em>
has a short name. To specify multiple names for an option, additional
entries may follow this one, with the <code>OPTION_ALIAS</code> flag
set. See section <a href="#Argp-Option-Flags">Flags for Argp Options</a>.
</p>
</dd>
<dt> <code>int key</code></dt>
<dd><p>The integer key provided by the current option to the option parser. If
<var>key</var> has a value that is a printable <small>ASCII</small> character (i.e.,
<code>isascii (<var>key</var>)</code> is true), it <em>also</em> specifies a short
option ‘<samp>-<var>char</var></samp>’, where <var>char</var> is the <small>ASCII</small> character
with the code <var>key</var>.
</p>
</dd>
<dt> <code>const char *arg</code></dt>
<dd><p>If non-zero, this is the name of an argument associated with this
option, which must be provided (e.g., with the
‘<samp>--<var>name</var>=<var>value</var></samp>’ or ‘<samp>-<var>char</var> <var>value</var></samp>’
syntaxes), unless the <code>OPTION_ARG_OPTIONAL</code> flag (see section <a href="#Argp-Option-Flags">Flags for Argp Options</a>) is set, in which case it <em>may</em> be provided.
</p>
</dd>
<dt> <code>int flags</code></dt>
<dd><p>Flags associated with this option, some of which are referred to above.
See section <a href="#Argp-Option-Flags">Flags for Argp Options</a>.
</p>
</dd>
<dt> <code>const char *doc</code></dt>
<dd><p>A documentation string for this option, for printing in help messages.
</p>
<p>If both the <code>name</code> and <code>key</code> fields are zero, this string
will be printed tabbed left from the normal option column, making it
useful as a group header. This will be the first thing printed in its
group. In this usage, it’s conventional to end the string with a
‘<samp>:</samp>’ character.
</p>
</dd>
<dt> <code>int group</code></dt>
<dd><p>Group identity for this option.
</p>
<p>In a long help message, options are sorted alphabetically within each
group, and the groups presented in the order 0, 1, 2, …, <var>n</var>,
-<var>m</var>, …, -2, -1.
</p>
<p>Every entry in an options array with this field 0 will inherit the group
number of the previous entry, or zero if it’s the first one. If it’s a
group header with <code>name</code> and <code>key</code> fields both zero, the
previous entry + 1 is the default. Automagic options such as
‘<samp>--help</samp>’ are put into group -1.
</p>
<p>Note that because of C structure initialization rules, this field often
need not be specified, because 0 is the correct value.
</p></dd>
</dl>
</dd></dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Option-Flags">25.3.4.1 Flags for Argp Options</a></td><td> </td><td align="left" valign="top"> Flags for options.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Option-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Option-Vectors" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parser-Functions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Option-Vectors" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Flags-for-Argp-Options"></a>
<h4 class="subsubsection">25.3.4.1 Flags for Argp Options</h4>
<p>The following flags may be or’d together in the <code>flags</code> field of a
<code>struct argp_option</code>. These flags control various aspects of how
that option is parsed or displayed in help messages:
</p>
<dl compact="compact">
<dt> <code>OPTION_ARG_OPTIONAL</code>
<a name="index-OPTION_005fARG_005fOPTIONAL"></a>
</dt>
<dd><p>The argument associated with this option is optional.
</p>
</dd>
<dt> <code>OPTION_HIDDEN</code>
<a name="index-OPTION_005fHIDDEN"></a>
</dt>
<dd><p>This option isn’t displayed in any help messages.
</p>
</dd>
<dt> <code>OPTION_ALIAS</code>
<a name="index-OPTION_005fALIAS"></a>
</dt>
<dd><p>This option is an alias for the closest previous non-alias option. This
means that it will be displayed in the same help entry, and will inherit
fields other than <code>name</code> and <code>key</code> from the option being
aliased.
</p>
</dd>
<dt> <code>OPTION_DOC</code>
<a name="index-OPTION_005fDOC"></a>
</dt>
<dd><p>This option isn’t actually an option and should be ignored by the actual
option parser. It is an arbitrary section of documentation that should
be displayed in much the same manner as the options. This is known as a
<em>documentation option</em>.
</p>
<p>If this flag is set, then the option <code>name</code> field is displayed
unmodified (e.g., no ‘<samp>--</samp>’ prefix is added) at the left-margin where
a <em>short</em> option would normally be displayed, and this
documentation string is left in it’s usual place. For purposes of
sorting, any leading whitespace and punctuation is ignored, unless the
first non-whitespace character is ‘<samp>-</samp>’. This entry is displayed
after all options, after <code>OPTION_DOC</code> entries with a leading
‘<samp>-</samp>’, in the same group.
</p>
</dd>
<dt> <code>OPTION_NO_USAGE</code>
<a name="index-OPTION_005fNO_005fUSAGE"></a>
</dt>
<dd><p>This option shouldn’t be included in ‘long’ usage messages, but should
still be included in other help messages. This is intended for options
that are completely documented in an argp’s <code>args_doc</code>
field. See section <a href="#Argp-Parsers">Specifying Argp Parsers</a>. Including this option in the generic usage
list would be redundant, and should be avoided.
</p>
<p>For instance, if <code>args_doc</code> is <code>"FOO BAR\n-x BLAH"</code>, and the
‘<samp>-x</samp>’ option’s purpose is to distinguish these two cases, ‘<samp>-x</samp>’
should probably be marked <code>OPTION_NO_USAGE</code>.
</p></dd>
</dl>
<hr size="6">
<a name="Argp-Parser-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Option-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Special-Keys" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Argp-Parser-Functions-1"></a>
<h3 class="subsection">25.3.5 Argp Parser Functions</h3>
<p>The function pointed to by the <code>parser</code> field in a <code>struct
argp</code> (see section <a href="#Argp-Parsers">Specifying Argp Parsers</a>) defines what actions take place in response
to each option or argument parsed. It is also used as a hook, allowing a
parser to perform tasks at certain other points during parsing.
</p>
<p>Argp parser functions have the following type signature:
</p>
<a name="index-argp-parser-functions"></a>
<table><tr><td> </td><td><pre class="smallexample">error_t <var>parser</var> (int <var>key</var>, char *<var>arg</var>, struct argp_state *<var>state</var>)
</pre></td></tr></table>
<p>where the arguments are as follows:
</p>
<dl compact="compact">
<dt> <var>key</var></dt>
<dd><p>For each option that is parsed, <var>parser</var> is called with a value of
<var>key</var> from that option’s <code>key</code> field in the option
vector. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>. <var>parser</var> is also called at
other times with special reserved keys, such as <code>ARGP_KEY_ARG</code> for
non-option arguments. See section <a href="#Argp-Special-Keys">Special Keys for Argp Parser Functions</a>.
</p>
</dd>
<dt> <var>arg</var></dt>
<dd><p>If <var>key</var> is an option, <var>arg</var> is its given value. This defaults
to zero if no value is specified. Only options that have a non-zero
<code>arg</code> field can ever have a value. These must <em>always</em> have a
value unless the <code>OPTION_ARG_OPTIONAL</code> flag is specified. If the
input being parsed specifies a value for an option that doesn’t allow
one, an error results before <var>parser</var> ever gets called.
</p>
<p>If <var>key</var> is <code>ARGP_KEY_ARG</code>, <var>arg</var> is a non-option
argument. Other special keys always have a zero <var>arg</var>.
</p>
</dd>
<dt> <var>state</var></dt>
<dd><p><var>state</var> points to a <code>struct argp_state</code>, containing useful
information about the current parsing state for use by
<var>parser</var>. See section <a href="#Argp-Parsing-State">Argp Parsing State</a>.
</p></dd>
</dl>
<p>When <var>parser</var> is called, it should perform whatever action is
appropriate for <var>key</var>, and return <code>0</code> for success,
<code>ARGP_ERR_UNKNOWN</code> if the value of <var>key</var> is not handled by this
parser function, or a unix error code if a real error
occurred. See section <a href="libc_2.html#Error-Codes">Error Codes</a>.
</p>
<dl>
<dt><a name="index-ARGP_005fERR_005fUNKNOWN"></a><u>Macro:</u> int <b>ARGP_ERR_UNKNOWN</b></dt>
<dd><p>Argp parser functions should return <code>ARGP_ERR_UNKNOWN</code> for any
<var>key</var> value they do not recognize, or for non-option arguments
(<code><var>key</var> == ARGP_KEY_ARG</code>) that they are not equipped to handle.
</p></dd></dl>
<p>A typical parser function uses a switch statement on <var>key</var>:
</p>
<table><tr><td> </td><td><pre class="smallexample">error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case <var>option_key</var>:
<var>action</var>
break;
…
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
</pre></td></tr></table>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Special-Keys">25.3.5.1 Special Keys for Argp Parser Functions</a></td><td> </td><td align="left" valign="top"> Special values for the <var>key</var> argument.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Parsing-State">25.3.5.3 Argp Parsing State</a></td><td> </td><td align="left" valign="top"> What the <var>state</var> argument refers to.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Helper-Functions">25.3.5.2 Functions For Use in Argp Parsers</a></td><td> </td><td align="left" valign="top"> Functions to help during argp parsing.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Special-Keys"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Parser-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Helper-Functions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parser-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Special-Keys-for-Argp-Parser-Functions"></a>
<h4 class="subsubsection">25.3.5.1 Special Keys for Argp Parser Functions</h4>
<p>In addition to key values corresponding to user options, the <var>key</var>
argument to argp parser functions may have a number of other special
values. In the following example <var>arg</var> and <var>state</var> refer to
parser function arguments. See section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>.
</p>
<dl compact="compact">
<dt> <code>ARGP_KEY_ARG</code>
<a name="index-ARGP_005fKEY_005fARG"></a>
</dt>
<dd><p>This is not an option at all, but rather a command line argument, whose
value is pointed to by <var>arg</var>.
</p>
<p>When there are multiple parser functions in play due to argp parsers
being combined, it’s impossible to know which one will handle a specific
argument. Each is called until one returns 0 or an error other than
<code>ARGP_ERR_UNKNOWN</code>; if an argument is not handled,
<code>argp_parse</code> immediately returns success, without parsing any more
arguments.
</p>
<p>Once a parser function returns success for this key, that fact is
recorded, and the <code>ARGP_KEY_NO_ARGS</code> case won’t be
used. <em>However</em>, if while processing the argument a parser function
decrements the <code>next</code> field of its <var>state</var> argument, the option
won’t be considered processed; this is to allow you to actually modify
the argument, perhaps into an option, and have it processed again.
</p>
</dd>
<dt> <code>ARGP_KEY_ARGS</code>
<a name="index-ARGP_005fKEY_005fARGS"></a>
</dt>
<dd><p>If a parser function returns <code>ARGP_ERR_UNKNOWN</code> for
<code>ARGP_KEY_ARG</code>, it is immediately called again with the key
<code>ARGP_KEY_ARGS</code>, which has a similar meaning, but is slightly more
convenient for consuming all remaining arguments. <var>arg</var> is 0, and
the tail of the argument vector may be found at <code><var>state</var>->argv
+ <var>state</var>->next</code>. If success is returned for this key, and
<code><var>state</var>->next</code> is unchanged, all remaining arguments are
considered to have been consumed. Otherwise, the amount by which
<code><var>state</var>->next</code> has been adjusted indicates how many were used.
Here’s an example that uses both, for different args:
</p>
<table><tr><td> </td><td><pre class="smallexample">…
case ARGP_KEY_ARG:
if (<var>state</var>->arg_num == 0)
/* First argument */
first_arg = <var>arg</var>;
else
/* Let the next case parse it. */
return ARGP_KEY_UNKNOWN;
break;
case ARGP_KEY_ARGS:
remaining_args = <var>state</var>->argv + <var>state</var>->next;
num_remaining_args = <var>state</var>->argc - <var>state</var>->next;
break;
</pre></td></tr></table>
</dd>
<dt> <code>ARGP_KEY_END</code>
<a name="index-ARGP_005fKEY_005fEND"></a>
</dt>
<dd><p>This indicates that there are no more command line arguments. Parser
functions are called in a different order, children first. This allows
each parser to clean up its state for the parent.
</p>
</dd>
<dt> <code>ARGP_KEY_NO_ARGS</code>
<a name="index-ARGP_005fKEY_005fNO_005fARGS"></a>
</dt>
<dd><p>Because it’s common to do some special processing if there aren’t any
non-option args, parser functions are called with this key if they
didn’t successfully process any non-option arguments. This is called
just before <code>ARGP_KEY_END</code>, where more general validity checks on
previously parsed arguments take place.
</p>
</dd>
<dt> <code>ARGP_KEY_INIT</code>
<a name="index-ARGP_005fKEY_005fINIT"></a>
</dt>
<dd><p>This is passed in before any parsing is done. Afterwards, the values of
each element of the <code>child_input</code> field of <var>state</var>, if any, are
copied to each child’s state to be the initial value of the <code>input</code>
when <em>their</em> parsers are called.
</p>
</dd>
<dt> <code>ARGP_KEY_SUCCESS</code>
<a name="index-ARGP_005fKEY_005fSUCCESS"></a>
</dt>
<dd><p>Passed in when parsing has successfully been completed, even if
arguments remain.
</p>
</dd>
<dt> <code>ARGP_KEY_ERROR</code>
<a name="index-ARGP_005fKEY_005fERROR"></a>
</dt>
<dd><p>Passed in if an error has occurred and parsing is terminated. In this
case a call with a key of <code>ARGP_KEY_SUCCESS</code> is never made.
</p>
</dd>
<dt> <code>ARGP_KEY_FINI</code>
<a name="index-ARGP_005fKEY_005fFINI"></a>
</dt>
<dd><p>The final key ever seen by any parser, even after
<code>ARGP_KEY_SUCCESS</code> and <code>ARGP_KEY_ERROR</code>. Any resources
allocated by <code>ARGP_KEY_INIT</code> may be freed here. At times, certain
resources allocated are to be returned to the caller after a successful
parse. In that case, those particular resources can be freed in the
<code>ARGP_KEY_ERROR</code> case.
</p></dd>
</dl>
<p>In all cases, <code>ARGP_KEY_INIT</code> is the first key seen by parser
functions, and <code>ARGP_KEY_FINI</code> the last, unless an error was
returned by the parser for <code>ARGP_KEY_INIT</code>. Other keys can occur
in one the following orders. <var>opt</var> refers to an arbitrary option
key:
</p>
<dl compact="compact">
<dt> <var>opt</var>… <code>ARGP_KEY_NO_ARGS</code> <code>ARGP_KEY_END</code> <code>ARGP_KEY_SUCCESS</code></dt>
<dd><p>The arguments being parsed did not contain any non-option arguments.
</p>
</dd>
<dt> ( <var>opt</var> | <code>ARGP_KEY_ARG</code> )… <code>ARGP_KEY_END</code> <code>ARGP_KEY_SUCCESS</code></dt>
<dd><p>All non-option arguments were successfully handled by a parser
function. There may be multiple parser functions if multiple argp
parsers were combined.
</p>
</dd>
<dt> ( <var>opt</var> | <code>ARGP_KEY_ARG</code> )… <code>ARGP_KEY_SUCCESS</code></dt>
<dd><p>Some non-option argument went unrecognized.
</p>
<p>This occurs when every parser function returns <code>ARGP_KEY_UNKNOWN</code>
for an argument, in which case parsing stops at that argument if
<var>arg_index</var> is a null pointer. Otherwise an error occurs.
</p></dd>
</dl>
<p>In all cases, if a non-null value for <var>arg_index</var> gets passed to
<code>argp_parse</code>, the index of the first unparsed command-line argument
is passed back in that value.
</p>
<p>If an error occurs and is either detected by argp or because a parser
function returned an error value, each parser is called with
<code>ARGP_KEY_ERROR</code>. No further calls are made, except the final call
with <code>ARGP_KEY_FINI</code>.
</p>
<hr size="6">
<a name="Argp-Helper-Functions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Special-Keys" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parsing-State" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parser-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Functions-For-Use-in-Argp-Parsers"></a>
<h4 class="subsubsection">25.3.5.2 Functions For Use in Argp Parsers</h4>
<p>Argp provides a number of functions available to the user of argp
(see section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>), mostly for producing error messages.
These take as their first argument the <var>state</var> argument to the
parser function. See section <a href="#Argp-Parsing-State">Argp Parsing State</a>.
</p>
<a name="index-usage-messages_002c-in-argp"></a>
<dl>
<dt><a name="index-argp_005fusage"></a><u>Function:</u> void <b>argp_usage</b><i> (const struct argp_state *<var>state</var>)</i></dt>
<dd><p>Outputs the standard usage message for the argp parser referred to by
<var>state</var> to <code><var>state</var>->err_stream</code> and terminate the program
with <code>exit (argp_err_exit_status)</code>. See section <a href="#Argp-Global-Variables">Argp Global Variables</a>.
</p></dd></dl>
<a name="index-syntax-error-messages_002c-in-argp"></a>
<dl>
<dt><a name="index-argp_005ferror"></a><u>Function:</u> void <b>argp_error</b><i> (const struct argp_state *<var>state</var>, const char *<var>fmt</var>, …)</i></dt>
<dd><p>Prints the printf format string <var>fmt</var> and following args, preceded
by the program name and ‘<samp>:</samp>’, and followed by a ‘<samp>Try …
--help</samp>’ message, and terminates the program with an exit status of
<code>argp_err_exit_status</code>. See section <a href="#Argp-Global-Variables">Argp Global Variables</a>.
</p></dd></dl>
<a name="index-error-messages_002c-in-argp"></a>
<dl>
<dt><a name="index-argp_005ffailure"></a><u>Function:</u> void <b>argp_failure</b><i> (const struct argp_state *<var>state</var>, int <var>status</var>, int <var>errnum</var>, const char *<var>fmt</var>, …)</i></dt>
<dd><p>Similar to the standard gnu error-reporting function <code>error</code>, this
prints the program name and ‘<samp>:</samp>’, the printf format string
<var>fmt</var>, and the appropriate following args. If it is non-zero, the
standard unix error text for <var>errnum</var> is printed. If <var>status</var> is
non-zero, it terminates the program with that value as its exit status.
</p>
<p>The difference between <code>argp_failure</code> and <code>argp_error</code> is that
<code>argp_error</code> is for <em>parsing errors</em>, whereas
<code>argp_failure</code> is for other problems that occur during parsing but
don’t reflect a syntactic problem with the input, such as illegal values
for options, bad phase of the moon, etc.
</p></dd></dl>
<dl>
<dt><a name="index-argp_005fstate_005fhelp"></a><u>Function:</u> void <b>argp_state_help</b><i> (const struct argp_state *<var>state</var>, FILE *<var>stream</var>, unsigned <var>flags</var>)</i></dt>
<dd><p>Outputs a help message for the argp parser referred to by <var>state</var>,
to <var>stream</var>. The <var>flags</var> argument determines what sort of help
message is produced. See section <a href="#Argp-Help-Flags">Flags for the <code>argp_help</code> Function</a>.
</p></dd></dl>
<p>Error output is sent to <code><var>state</var>->err_stream</code>, and the program
name printed is <code><var>state</var>->name</code>.
</p>
<p>The output or program termination behavior of these functions may be
suppressed if the <code>ARGP_NO_EXIT</code> or <code>ARGP_NO_ERRS</code> flags are
passed to <code>argp_parse</code>. See section <a href="#Argp-Flags">Flags for <code>argp_parse</code></a>.
</p>
<p>This behavior is useful if an argp parser is exported for use by other
programs (e.g., by a library), and may be used in a context where it is
not desirable to terminate the program in response to parsing errors. In
argp parsers intended for such general use, and for the case where the
program <em>doesn’t</em> terminate, calls to any of these functions should
be followed by code that returns the appropriate error code:
</p>
<table><tr><td> </td><td><pre class="smallexample">if (<var>bad argument syntax</var>)
{
argp_usage (<var>state</var>);
return EINVAL;
}
</pre></td></tr></table>
<p>If a parser function will <em>only</em> be used when <code>ARGP_NO_EXIT</code>
is not set, the return may be omitted.
</p>
<hr size="6">
<a name="Argp-Parsing-State"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Helper-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Children" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Parser-Functions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Argp-Parsing-State-1"></a>
<h4 class="subsubsection">25.3.5.3 Argp Parsing State</h4>
<p>The third argument to argp parser functions (see section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>) is a pointer to a <code>struct argp_state</code>, which contains
information about the state of the option parsing.
</p>
<dl>
<dt><a name="index-struct-argp_005fstate"></a><u>Data Type:</u> <b>struct argp_state</b></dt>
<dd><p>This structure has the following fields, which may be modified as noted:
</p>
<dl compact="compact">
<dt> <code>const struct argp *const root_argp</code></dt>
<dd><p>The top level argp parser being parsed. Note that this is often
<em>not</em> the same <code>struct argp</code> passed into <code>argp_parse</code> by
the invoking program. See section <a href="#Argp">Parsing Program Options with Argp</a>. It is an internal argp parser that
contains options implemented by <code>argp_parse</code> itself, such as
‘<samp>--help</samp>’.
</p>
</dd>
<dt> <code>int argc</code></dt>
<dt> <code>char **argv</code></dt>
<dd><p>The argument vector being parsed. This may be modified.
</p>
</dd>
<dt> <code>int next</code></dt>
<dd><p>The index in <code>argv</code> of the next argument to be parsed. This may be
modified.
</p>
<p>One way to consume all remaining arguments in the input is to set
<code><var>state</var>->next = <var>state</var>->argc</code>, perhaps after recording
the value of the <code>next</code> field to find the consumed arguments. The
current option can be re-parsed immediately by decrementing this field,
then modifying <code><var>state</var>->argv[<var>state</var>->next]</code> to reflect
the option that should be reexamined.
</p>
</dd>
<dt> <code>unsigned flags</code></dt>
<dd><p>The flags supplied to <code>argp_parse</code>. These may be modified, although
some flags may only take effect when <code>argp_parse</code> is first
invoked. See section <a href="#Argp-Flags">Flags for <code>argp_parse</code></a>.
</p>
</dd>
<dt> <code>unsigned arg_num</code></dt>
<dd><p>While calling a parsing function with the <var>key</var> argument
<code>ARGP_KEY_ARG</code>, this represents the number of the current arg,
starting at 0. It is incremented after each <code>ARGP_KEY_ARG</code> call
returns. At all other times, this is the number of <code>ARGP_KEY_ARG</code>
arguments that have been processed.
</p>
</dd>
<dt> <code>int quoted</code></dt>
<dd><p>If non-zero, the index in <code>argv</code> of the first argument following a
special ‘<samp>--</samp>’ argument. This prevents anything that follows from
being interpreted as an option. It is only set after argument parsing
has proceeded past this point.
</p>
</dd>
<dt> <code>void *input</code></dt>
<dd><p>An arbitrary pointer passed in from the caller of <code>argp_parse</code>, in
the <var>input</var> argument.
</p>
</dd>
<dt> <code>void **child_inputs</code></dt>
<dd><p>These are values that will be passed to child parsers. This vector will
be the same length as the number of children in the current parser. Each
child parser will be given the value of
<code><var>state</var>->child_inputs[<var>i</var>]</code> as <em>its</em>
<code><var>state</var>->input</code> field, where <var>i</var> is the index of the child
in the this parser’s <code>children</code> field. See section <a href="#Argp-Children">Combining Multiple Argp Parsers</a>.
</p>
</dd>
<dt> <code>void *hook</code></dt>
<dd><p>For the parser function’s use. Initialized to 0, but otherwise ignored
by argp.
</p>
</dd>
<dt> <code>char *name</code></dt>
<dd><p>The name used when printing messages. This is initialized to
<code>argv[0]</code>, or <code>program_invocation_name</code> if <code>argv[0]</code> is
unavailable.
</p>
</dd>
<dt> <code>FILE *err_stream</code></dt>
<dt> <code>FILE *out_stream</code></dt>
<dd><p>The stdio streams used when argp prints. Error messages are printed to
<code>err_stream</code>, all other output, such as ‘<samp>--help</samp>’ output) to
<code>out_stream</code>. These are initialized to <code>stderr</code> and
<code>stdout</code> respectively. See section <a href="libc_12.html#Standard-Streams">Standard Streams</a>.
</p>
</dd>
<dt> <code>void *pstate</code></dt>
<dd><p>Private, for use by the argp implementation.
</p></dd>
</dl>
</dd></dl>
<hr size="6">
<a name="Argp-Children"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Parsing-State" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Combining-Multiple-Argp-Parsers"></a>
<h3 class="subsection">25.3.6 Combining Multiple Argp Parsers</h3>
<p>The <code>children</code> field in a <code>struct argp</code> enables other argp
parsers to be combined with the referencing one for the parsing of a
single set of arguments. This field should point to a vector of
<code>struct argp_child</code>, which is terminated by an entry having a value
of zero in the <code>argp</code> field.
</p>
<p>Where conflicts between combined parsers arise, as when two specify an
option with the same name, the parser conflicts are resolved in favor of
the parent argp parser(s), or the earlier of the argp parsers in the
list of children.
</p>
<dl>
<dt><a name="index-struct-argp_005fchild"></a><u>Data Type:</u> <b>struct argp_child</b></dt>
<dd><p>An entry in the list of subsidiary argp parsers pointed to by the
<code>children</code> field in a <code>struct argp</code>. The fields are as
follows:
</p>
<dl compact="compact">
<dt> <code>const struct argp *argp</code></dt>
<dd><p>The child argp parser, or zero to end of the list.
</p>
</dd>
<dt> <code>int flags</code></dt>
<dd><p>Flags for this child.
</p>
</dd>
<dt> <code>const char *header</code></dt>
<dd><p>If non-zero, this is an optional header to be printed within help output
before the child options. As a side-effect, a non-zero value forces the
child options to be grouped together. To achieve this effect without
actually printing a header string, use a value of <code>""</code>. As with
header strings specified in an option entry, the conventional value of
the last character is ‘<samp>:</samp>’. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>.
</p>
</dd>
<dt> <code>int group</code></dt>
<dd><p>This is where the child options are grouped relative to the other
‘consolidated’ options in the parent argp parser. The values are the
same as the <code>group</code> field in <code>struct argp_option</code>. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>. All child-groupings follow parent options at a
particular group level. If both this field and <code>header</code> are zero,
then the child’s options aren’t grouped together, they are merged with
parent options at the parent option group level.
</p>
</dd>
</dl>
</dd></dl>
<hr size="6">
<a name="Argp-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Children" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Help-Filtering" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Flags-for-argp_005fparse"></a>
<h3 class="subsection">25.3.7 Flags for <code>argp_parse</code></h3>
<p>The default behavior of <code>argp_parse</code> is designed to be convenient
for the most common case of parsing program command line argument. To
modify these defaults, the following flags may be or’d together in the
<var>flags</var> argument to <code>argp_parse</code>:
</p>
<dl compact="compact">
<dt> <code>ARGP_PARSE_ARGV0</code>
<a name="index-ARGP_005fPARSE_005fARGV0"></a>
</dt>
<dd><p>Don’t ignore the first element of the <var>argv</var> argument to
<code>argp_parse</code>. Unless <code>ARGP_NO_ERRS</code> is set, the first element
of the argument vector is skipped for option parsing purposes, as it
corresponds to the program name in a command line.
</p>
</dd>
<dt> <code>ARGP_NO_ERRS</code>
<a name="index-ARGP_005fNO_005fERRS"></a>
</dt>
<dd><p>Don’t print error messages for unknown options to <code>stderr</code>; unless
this flag is set, <code>ARGP_PARSE_ARGV0</code> is ignored, as <code>argv[0]</code>
is used as the program name in the error messages. This flag implies
<code>ARGP_NO_EXIT</code>. This is based on the assumption that silent exiting
upon errors is bad behavior.
</p>
</dd>
<dt> <code>ARGP_NO_ARGS</code>
<a name="index-ARGP_005fNO_005fARGS"></a>
</dt>
<dd><p>Don’t parse any non-option args. Normally these are parsed by calling
the parse functions with a key of <code>ARGP_KEY_ARG</code>, the actual
argument being the value. This flag needn’t normally be set, as the
default behavior is to stop parsing as soon as an argument fails to be
parsed. See section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>.
</p>
</dd>
<dt> <code>ARGP_IN_ORDER</code>
<a name="index-ARGP_005fIN_005fORDER"></a>
</dt>
<dd><p>Parse options and arguments in the same order they occur on the command
line. Normally they’re rearranged so that all options come first.
</p>
</dd>
<dt> <code>ARGP_NO_HELP</code>
<a name="index-ARGP_005fNO_005fHELP"></a>
</dt>
<dd><p>Don’t provide the standard long option ‘<samp>--help</samp>’, which ordinarily
causes usage and option help information to be output to <code>stdout</code>
and <code>exit (0)</code>.
</p>
</dd>
<dt> <code>ARGP_NO_EXIT</code>
<a name="index-ARGP_005fNO_005fEXIT"></a>
</dt>
<dd><p>Don’t exit on errors, although they may still result in error messages.
</p>
</dd>
<dt> <code>ARGP_LONG_ONLY</code>
<a name="index-ARGP_005fLONG_005fONLY"></a>
</dt>
<dd><p>Use the gnu getopt ‘long-only’ rules for parsing arguments. This allows
long-options to be recognized with only a single ‘<samp>-</samp>’
(i.e., ‘<samp>-help</samp>’). This results in a less useful interface, and its
use is discouraged as it conflicts with the way most GNU programs work
as well as the GNU coding standards.
</p>
</dd>
<dt> <code>ARGP_SILENT</code>
<a name="index-ARGP_005fSILENT"></a>
</dt>
<dd><p>Turns off any message-printing/exiting options, specifically
<code>ARGP_NO_EXIT</code>, <code>ARGP_NO_ERRS</code>, and <code>ARGP_NO_HELP</code>.
</p></dd>
</dl>
<hr size="6">
<a name="Argp-Help-Filtering"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Help-Filter-Keys" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Customizing-Argp-Help-Output"></a>
<h3 class="subsection">25.3.8 Customizing Argp Help Output</h3>
<p>The <code>help_filter</code> field in a <code>struct argp</code> is a pointer to a
function that filters the text of help messages before displaying
them. They have a function signature like:
</p>
<table><tr><td> </td><td><pre class="smallexample">char *<var>help-filter</var> (int <var>key</var>, const char *<var>text</var>, void *<var>input</var>)
</pre></td></tr></table>
<p>Where <var>key</var> is either a key from an option, in which case <var>text</var>
is that option’s help text. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>. Alternately, one
of the special keys with names beginning with ‘<samp>ARGP_KEY_HELP_</samp>’
might be used, describing which other help text <var>text</var> will contain.
See section <a href="#Argp-Help-Filter-Keys">Special Keys for Argp Help Filter Functions</a>.
</p>
<p>The function should return either <var>text</var> if it remains as-is, or a
replacement string allocated using <code>malloc</code>. This will be either be
freed by argp or zero, which prints nothing. The value of <var>text</var> is
supplied <em>after</em> any translation has been done, so if any of the
replacement text needs translation, it will be done by the filter
function. <var>input</var> is either the input supplied to <code>argp_parse</code>
or it is zero, if <code>argp_help</code> was called directly by the user.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Help-Filter-Keys">25.3.8.1 Special Keys for Argp Help Filter Functions</a></td><td> </td><td align="left" valign="top"> Special <var>key</var> values for help filter functions.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Help-Filter-Keys"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Help-Filtering" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Help" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Help-Filtering" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Special-Keys-for-Argp-Help-Filter-Functions"></a>
<h4 class="subsubsection">25.3.8.1 Special Keys for Argp Help Filter Functions</h4>
<p>The following special values may be passed to an argp help filter
function as the first argument in addition to key values for user
options. They specify which help text the <var>text</var> argument contains:
</p>
<dl compact="compact">
<dt> <code>ARGP_KEY_HELP_PRE_DOC</code>
<a name="index-ARGP_005fKEY_005fHELP_005fPRE_005fDOC"></a>
</dt>
<dd><p>The help text preceding options.
</p>
</dd>
<dt> <code>ARGP_KEY_HELP_POST_DOC</code>
<a name="index-ARGP_005fKEY_005fHELP_005fPOST_005fDOC"></a>
</dt>
<dd><p>The help text following options.
</p>
</dd>
<dt> <code>ARGP_KEY_HELP_HEADER</code>
<a name="index-ARGP_005fKEY_005fHELP_005fHEADER"></a>
</dt>
<dd><p>The option header string.
</p>
</dd>
<dt> <code>ARGP_KEY_HELP_EXTRA</code>
<a name="index-ARGP_005fKEY_005fHELP_005fEXTRA"></a>
</dt>
<dd><p>This is used after all other documentation; <var>text</var> is zero for this key.
</p>
</dd>
<dt> <code>ARGP_KEY_HELP_DUP_ARGS_NOTE</code>
<a name="index-ARGP_005fKEY_005fHELP_005fDUP_005fARGS_005fNOTE"></a>
</dt>
<dd><p>The explanatory note printed when duplicate option arguments have been suppressed.
</p>
</dd>
<dt> <code>ARGP_KEY_HELP_ARGS_DOC</code>
<a name="index-ARGP_005fKEY_005fHELP_005fARGS_005fDOC"></a>
</dt>
<dd><p>The argument doc string; formally the <code>args_doc</code> field from the argp parser. See section <a href="#Argp-Parsers">Specifying Argp Parsers</a>.
</p></dd>
</dl>
<hr size="6">
<a name="Argp-Help"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Help-Filter-Keys" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Help-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-argp_005fhelp-Function"></a>
<h3 class="subsection">25.3.9 The <code>argp_help</code> Function</h3>
<p>Normally programs using argp need not be written with particular
printing argument-usage-type help messages in mind as the standard
‘<samp>--help</samp>’ option is handled automatically by argp. Typical error
cases can be handled using <code>argp_usage</code> and
<code>argp_error</code>. See section <a href="#Argp-Helper-Functions">Functions For Use in Argp Parsers</a>. However, if it’s
desirable to print a help message in some context other than parsing the
program options, argp offers the <code>argp_help</code> interface.
</p>
<dl>
<dt><a name="index-argp_005fhelp"></a><u>Function:</u> void <b>argp_help</b><i> (const struct argp *<var>argp</var>, FILE *<var>stream</var>, unsigned <var>flags</var>, char *<var>name</var>)</i></dt>
<dd><p>This outputs a help message for the argp parser <var>argp</var> to
<var>stream</var>. The type of messages printed will be determined by
<var>flags</var>.
</p>
<p>Any options such as ‘<samp>--help</samp>’ that are implemented automatically by
argp itself will <em>not</em> be present in the help output; for this
reason it is best to use <code>argp_state_help</code> if calling from within
an argp parser function. See section <a href="#Argp-Helper-Functions">Functions For Use in Argp Parsers</a>.
</p></dd></dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Help-Flags">25.3.10 Flags for the <code>argp_help</code> Function</a></td><td> </td><td align="left" valign="top"> Specifying what sort of help message to print.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Help-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Help" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Examples" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Flags-for-the-argp_005fhelp-Function"></a>
<h3 class="subsection">25.3.10 Flags for the <code>argp_help</code> Function</h3>
<p>When calling <code>argp_help</code> (see section <a href="#Argp-Help">The <code>argp_help</code> Function</a>) or
<code>argp_state_help</code> (see section <a href="#Argp-Helper-Functions">Functions For Use in Argp Parsers</a>) the exact output
is determined by the <var>flags</var> argument. This should consist of any of
the following flags, or’d together:
</p>
<dl compact="compact">
<dt> <code>ARGP_HELP_USAGE</code>
<a name="index-ARGP_005fHELP_005fUSAGE"></a>
</dt>
<dd><p>A unix ‘<samp>Usage:</samp>’ message that explicitly lists all options.
</p>
</dd>
<dt> <code>ARGP_HELP_SHORT_USAGE</code>
<a name="index-ARGP_005fHELP_005fSHORT_005fUSAGE"></a>
</dt>
<dd><p>A unix ‘<samp>Usage:</samp>’ message that displays an appropriate placeholder to
indicate where the options go; useful for showing the non-option
argument syntax.
</p>
</dd>
<dt> <code>ARGP_HELP_SEE</code>
<a name="index-ARGP_005fHELP_005fSEE"></a>
</dt>
<dd><p>A ‘<samp>Try … for more help</samp>’ message; ‘<samp>…</samp>’ contains the
program name and ‘<samp>--help</samp>’.
</p>
</dd>
<dt> <code>ARGP_HELP_LONG</code>
<a name="index-ARGP_005fHELP_005fLONG"></a>
</dt>
<dd><p>A verbose option help message that gives each option available along
with its documentation string.
</p>
</dd>
<dt> <code>ARGP_HELP_PRE_DOC</code>
<a name="index-ARGP_005fHELP_005fPRE_005fDOC"></a>
</dt>
<dd><p>The part of the argp parser doc string preceding the verbose option help.
</p>
</dd>
<dt> <code>ARGP_HELP_POST_DOC</code>
<a name="index-ARGP_005fHELP_005fPOST_005fDOC"></a>
</dt>
<dd><p>The part of the argp parser doc string that following the verbose option help.
</p>
</dd>
<dt> <code>ARGP_HELP_DOC</code>
<a name="index-ARGP_005fHELP_005fDOC"></a>
</dt>
<dd><p><code>(ARGP_HELP_PRE_DOC | ARGP_HELP_POST_DOC)</code>
</p>
</dd>
<dt> <code>ARGP_HELP_BUG_ADDR</code>
<a name="index-ARGP_005fHELP_005fBUG_005fADDR"></a>
</dt>
<dd><p>A message that prints where to report bugs for this program, if the
<code>argp_program_bug_address</code> variable contains this information.
</p>
</dd>
<dt> <code>ARGP_HELP_LONG_ONLY</code>
<a name="index-ARGP_005fHELP_005fLONG_005fONLY"></a>
</dt>
<dd><p>This will modify any output to reflect the <code>ARGP_LONG_ONLY</code> mode.
</p></dd>
</dl>
<p>The following flags are only understood when used with
<code>argp_state_help</code>. They control whether the function returns after
printing its output, or terminates the program:
</p>
<dl compact="compact">
<dt> <code>ARGP_HELP_EXIT_ERR</code>
<a name="index-ARGP_005fHELP_005fEXIT_005fERR"></a>
</dt>
<dd><p>This will terminate the program with <code>exit (argp_err_exit_status)</code>.
</p>
</dd>
<dt> <code>ARGP_HELP_EXIT_OK</code>
<a name="index-ARGP_005fHELP_005fEXIT_005fOK"></a>
</dt>
<dd><p>This will terminate the program with <code>exit (0)</code>.
</p></dd>
</dl>
<p>The following flags are combinations of the basic flags for printing
standard messages:
</p>
<dl compact="compact">
<dt> <code>ARGP_HELP_STD_ERR</code>
<a name="index-ARGP_005fHELP_005fSTD_005fERR"></a>
</dt>
<dd><p>Assuming that an error message for a parsing error has printed, this
prints a message on how to get help, and terminates the program with an
error.
</p>
</dd>
<dt> <code>ARGP_HELP_STD_USAGE</code>
<a name="index-ARGP_005fHELP_005fSTD_005fUSAGE"></a>
</dt>
<dd><p>This prints a standard usage message and terminates the program with an
error. This is used when no other specific error messages are
appropriate or available.
</p>
</dd>
<dt> <code>ARGP_HELP_STD_HELP</code>
<a name="index-ARGP_005fHELP_005fSTD_005fHELP"></a>
</dt>
<dd><p>This prints the standard response for a ‘<samp>--help</samp>’ option, and
terminates the program successfully.
</p></dd>
</dl>
<hr size="6">
<a name="Argp-Examples"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Help-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Example-1" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Argp-Examples-1"></a>
<h3 class="subsection">25.3.11 Argp Examples</h3>
<p>These example programs demonstrate the basic usage of argp.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Argp-Example-1">25.3.11.1 A Minimal Program Using Argp</a></td><td> </td><td align="left" valign="top"> A minimal program using argp.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Example-2">25.3.11.2 A Program Using Argp with Only Default Options</a></td><td> </td><td align="left" valign="top"> A program using only default options.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Example-3">25.3.11.3 A Program Using Argp with User Options</a></td><td> </td><td align="left" valign="top"> A simple program with user options.
</td></tr>
<tr><td align="left" valign="top"><a href="#Argp-Example-4">25.3.11.4 A Program Using Multiple Combined Argp Parsers</a></td><td> </td><td align="left" valign="top"> Combining multiple argp parsers.
</td></tr>
</table>
<hr size="6">
<a name="Argp-Example-1"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Examples" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Example-2" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-Minimal-Program-Using-Argp"></a>
<h4 class="subsubsection">25.3.11.1 A Minimal Program Using Argp</h4>
<p>This is perhaps the smallest program possible that uses argp. It won’t
do much except give an error messages and exit when there are any
arguments, and prints a rather pointless message for ‘<samp>--help</samp>’.
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Argp example #1 -- a minimal program using argp</span> */
/* <span class="roman">This is (probably) the smallest possible program that
uses argp. It won't do much except give an error
messages and exit when there are any arguments, and print
a (rather pointless) messages for --help.</span> */
#include <argp.h>
int main (int argc, char **argv)
{
argp_parse (0, argc, argv, 0, 0, 0);
exit (0);
}
</pre></td></tr></table>
<hr size="6">
<a name="Argp-Example-2"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Example-1" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Example-3" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-Program-Using-Argp-with-Only-Default-Options"></a>
<h4 class="subsubsection">25.3.11.2 A Program Using Argp with Only Default Options</h4>
<p>This program doesn’t use any options or arguments, it uses argp to be
compliant with the GNU standard command line format.
</p>
<p>In addition to giving no arguments and implementing a ‘<samp>--help</samp>’
option, this example has a ‘<samp>--version</samp>’ option, which will put the
given documentation string and bug address in the ‘<samp>--help</samp>’ output,
as per GNU standards.
</p>
<p>The variable <code>argp</code> contains the argument parser
specification. Adding fields to this structure is the way most
parameters are passed to <code>argp_parse</code>. The first three fields are
normally used, but they are not in this small program. There are also
two global variables that argp can use defined here,
<code>argp_program_version</code> and <code>argp_program_bug_address</code>. They
are considered global variables because they will almost always be
constant for a given program, even if they use different argument
parsers for various tasks.
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Argp example #2 -- a pretty minimal program using argp</span> */
/* <span class="roman">This program doesn't use any options or arguments, but uses
argp to be compliant with the GNU standard command line
format.
In addition to making sure no arguments are given, and
implementing a --help option, this example will have a
--version option, and will put the given documentation string
and bug address in the --help output, as per GNU standards.
The variable ARGP contains the argument parser specification;
adding fields to this structure is the way most parameters are
passed to argp_parse (the first three fields are usually used,
but not in this small program). There are also two global
variables that argp knows about defined here,
ARGP_PROGRAM_VERSION and ARGP_PROGRAM_BUG_ADDRESS (they are
global variables because they will almost always be constant
for a given program, even if it uses different argument
parsers for various tasks).</span> */
#include <argp.h>
const char *argp_program_version =
"argp-ex2 1.0";
const char *argp_program_bug_address =
"<bug-gnu-utils@gnu.org>";
/* <span class="roman">Program documentation.</span> */
static char doc[] =
"Argp example #2 -- a pretty minimal program using argp";
/* <span class="roman">Our argument parser. The <code>options</code>, <code>parser</code>, and
<code>args_doc</code> fields are zero because we have neither options or
arguments; <code>doc</code> and <code>argp_program_bug_address</code> will be
used in the output for ‘<samp>--help</samp>’, and the ‘<samp>--version</samp>’
option will print out <code>argp_program_version</code>.</span> */
static struct argp argp = { 0, 0, 0, doc };
int main (int argc, char **argv)
{
argp_parse (&argp, argc, argv, 0, 0, 0);
exit (0);
}
</pre></td></tr></table>
<hr size="6">
<a name="Argp-Example-3"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Example-2" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Example-4" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-Program-Using-Argp-with-User-Options"></a>
<h4 class="subsubsection">25.3.11.3 A Program Using Argp with User Options</h4>
<p>This program uses the same features as example 2, adding user options
and arguments.
</p>
<p>We now use the first four fields in <code>argp</code> (see section <a href="#Argp-Parsers">Specifying Argp Parsers</a>)
and specify <code>parse_opt</code> as the parser function. See section <a href="#Argp-Parser-Functions">Argp Parser Functions</a>.
</p>
<p>Note that in this example, <code>main</code> uses a structure to communicate
with the <code>parse_opt</code> function, a pointer to which it passes in the
<code>input</code> argument to <code>argp_parse</code>. See section <a href="#Argp">Parsing Program Options with Argp</a>. It is retrieved
by <code>parse_opt</code> through the <code>input</code> field in its <code>state</code>
argument. See section <a href="#Argp-Parsing-State">Argp Parsing State</a>. Of course, it’s also possible to
use global variables instead, but using a structure like this is
somewhat more flexible and clean.
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Argp example #3 -- a program with options and arguments using argp</span> */
/* <span class="roman">This program uses the same features as example 2, and uses options and
arguments.
We now use the first four fields in ARGP, so here's a description of them:
OPTIONS -- A pointer to a vector of struct argp_option (see below)
PARSER -- A function to parse a single option, called by argp
ARGS_DOC -- A string describing how the non-option arguments should look
DOC -- A descriptive string about this program; if it contains a
vertical tab character (\v), the part after it will be
printed *following* the options
The function PARSER takes the following arguments:
KEY -- An integer specifying which option this is (taken
from the KEY field in each struct argp_option), or
a special key specifying something else; the only
special keys we use here are ARGP_KEY_ARG, meaning
a non-option argument, and ARGP_KEY_END, meaning
that all arguments have been parsed
ARG -- For an option KEY, the string value of its
argument, or NULL if it has none
STATE-- A pointer to a struct argp_state, containing
various useful information about the parsing state; used here
are the INPUT field, which reflects the INPUT argument to
argp_parse, and the ARG_NUM field, which is the number of the
current non-option argument being parsed
It should return either 0, meaning success, ARGP_ERR_UNKNOWN, meaning the
given KEY wasn't recognized, or an errno value indicating some other
error.
Note that in this example, main uses a structure to communicate with the
parse_opt function, a pointer to which it passes in the INPUT argument to
argp_parse. Of course, it's also possible to use global variables
instead, but this is somewhat more flexible.
The OPTIONS field contains a pointer to a vector of struct argp_option's;
that structure has the following fields (if you assign your option
structures using array initialization like this example, unspecified
fields will be defaulted to 0, and need not be specified):
NAME -- The name of this option's long option (may be zero)
KEY -- The KEY to pass to the PARSER function when parsing this option,
*and* the name of this option's short option, if it is a
printable ascii character
ARG -- The name of this option's argument, if any
FLAGS -- Flags describing this option; some of them are:
OPTION_ARG_OPTIONAL -- The argument to this option is optional
OPTION_ALIAS -- This option is an alias for the
previous option
OPTION_HIDDEN -- Don't show this option in --help output
DOC -- A documentation string for this option, shown in --help output
An options vector should be terminated by an option with all fields zero.</span> */
#include <argp.h>
const char *argp_program_version =
"argp-ex3 1.0";
const char *argp_program_bug_address =
"<bug-gnu-utils@gnu.org>";
/* <span class="roman">Program documentation.</span> */
static char doc[] =
"Argp example #3 -- a program with options and arguments using argp";
/* <span class="roman">A description of the arguments we accept.</span> */
static char args_doc[] = "ARG1 ARG2";
/* <span class="roman">The options we understand.</span> */
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
{"silent", 's', 0, OPTION_ALIAS },
{"output", 'o', "FILE", 0,
"Output to FILE instead of standard output" },
{ 0 }
};
/* <span class="roman">Used by <code>main</code> to communicate with <code>parse_opt</code>.</span> */
struct arguments
{
char *args[2]; /* <span class="roman"><var>arg1</var> & <var>arg2</var></span> */
int silent, verbose;
char *output_file;
};
/* <span class="roman">Parse a single option.</span> */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* <span class="roman">Get the <var>input</var> argument from <code>argp_parse</code>, which we
know is a pointer to our arguments structure.</span> */
struct arguments *arguments = state->input;
switch (key)
{
case 'q': case 's':
arguments->silent = 1;
break;
case 'v':
arguments->verbose = 1;
break;
case 'o':
arguments->output_file = arg;
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 2)
/* <span class="roman">Too many arguments.</span> */
argp_usage (state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 2)
/* <span class="roman">Not enough arguments.</span> */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* <span class="roman">Our argp parser.</span> */
static struct argp argp = { options, parse_opt, args_doc, doc };
int main (int argc, char **argv)
{
struct arguments arguments;
/* <span class="roman">Default values.</span> */
arguments.silent = 0;
arguments.verbose = 0;
arguments.output_file = "-";
/* <span class="roman">Parse our arguments; every option seen by <code>parse_opt</code> will
be reflected in <code>arguments</code>.</span> */
argp_parse (&argp, argc, argv, 0, 0, &arguments);
printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
"VERBOSE = %s\nSILENT = %s\n",
arguments.args[0], arguments.args[1],
arguments.output_file,
arguments.verbose ? "yes" : "no",
arguments.silent ? "yes" : "no");
exit (0);
}
</pre></td></tr></table>
<hr size="6">
<a name="Argp-Example-4"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Example-3" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-User-Customization" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-Program-Using-Multiple-Combined-Argp-Parsers"></a>
<h4 class="subsubsection">25.3.11.4 A Program Using Multiple Combined Argp Parsers</h4>
<p>This program uses the same features as example 3, but has more options,
and presents more structure in the ‘<samp>--help</samp>’ output. It also
illustrates how you can ‘steal’ the remainder of the input arguments
past a certain point for programs that accept a list of items. It also
illustrates the <var>key</var> value <code>ARGP_KEY_NO_ARGS</code>, which is only
given if no non-option arguments were supplied to the
program. See section <a href="#Argp-Special-Keys">Special Keys for Argp Parser Functions</a>.
</p>
<p>For structuring help output, two features are used: <em>headers</em> and a
two part option string. The <em>headers</em> are entries in the options
vector. See section <a href="#Argp-Option-Vectors">Specifying Options in an Argp Parser</a>. The first four fields are zero. The
two part documentation string are in the variable <code>doc</code>, which
allows documentation both before and after the options. See section <a href="#Argp-Parsers">Specifying Argp Parsers</a>, the two parts of <code>doc</code> are separated by a vertical-tab
character (<code>'\v'</code>, or <code>'\013'</code>). By convention, the
documentation before the options is a short string stating what the
program does, and after any options it is longer, describing the
behavior in more detail. All documentation strings are automatically
filled for output, although newlines may be included to force a line
break at a particular point. In addition, documentation strings are
passed to the <code>gettext</code> function, for possible translation into the
current locale.
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Argp example #4 -- a program with somewhat more complicated options</span> */
/* <span class="roman">This program uses the same features as example 3, but has more
options, and somewhat more structure in the -help output. It
also shows how you can `steal' the remainder of the input
arguments past a certain point, for programs that accept a
list of items. It also shows the special argp KEY value
ARGP_KEY_NO_ARGS, which is only given if no non-option
arguments were supplied to the program.
For structuring the help output, two features are used,
*headers* which are entries in the options vector with the
first four fields being zero, and a two part documentation
string (in the variable DOC), which allows documentation both
before and after the options; the two parts of DOC are
separated by a vertical-tab character ('\v', or '\013'). By
convention, the documentation before the options is just a
short string saying what the program does, and that afterwards
is longer, describing the behavior in more detail. All
documentation strings are automatically filled for output,
although newlines may be included to force a line break at a
particular point. All documentation strings are also passed to
the `gettext' function, for possible translation into the
current locale.</span> */
#include <stdlib.h>
#include <error.h>
#include <argp.h>
const char *argp_program_version =
"argp-ex4 1.0";
const char *argp_program_bug_address =
"<bug-gnu-utils@prep.ai.mit.edu>";
/* <span class="roman">Program documentation.</span> */
static char doc[] =
"Argp example #4 -- a program with somewhat more complicated\
options\
\vThis part of the documentation comes *after* the options;\
note that the text is automatically filled, but it's possible\
to force a line-break, e.g.\n<-- here.";
/* <span class="roman">A description of the arguments we accept.</span> */
static char args_doc[] = "ARG1 [STRING...]";
/* <span class="roman">Keys for options without short-options.</span> */
#define OPT_ABORT 1 /* <span class="roman">--abort</span> */
/* <span class="roman">The options we understand.</span> */
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output" },
{"quiet", 'q', 0, 0, "Don't produce any output" },
{"silent", 's', 0, OPTION_ALIAS },
{"output", 'o', "FILE", 0,
"Output to FILE instead of standard output" },
{0,0,0,0, "The following options should be grouped together:" },
{"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
"Repeat the output COUNT (default 10) times"},
{"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
{ 0 }
};
/* <span class="roman">Used by <code>main</code> to communicate with <code>parse_opt</code>.</span> */
struct arguments
{
char *arg1; /* <span class="roman"><var>arg1</var></span> */
char **strings; /* <span class="roman">[<var>string</var>…]</span> */
int silent, verbose, abort; /* <span class="roman">‘<samp>-s</samp>’, ‘<samp>-v</samp>’, ‘<samp>--abort</samp>’</span> */
char *output_file; /* <span class="roman"><var>file</var> arg to ‘<samp>--output</samp>’</span> */
int repeat_count; /* <span class="roman"><var>count</var> arg to ‘<samp>--repeat</samp>’</span> */
};
/* <span class="roman">Parse a single option.</span> */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
/* <span class="roman">Get the <code>input</code> argument from <code>argp_parse</code>, which we
know is a pointer to our arguments structure.</span> */
struct arguments *arguments = state->input;
switch (key)
{
case 'q': case 's':
arguments->silent = 1;
break;
case 'v':
arguments->verbose = 1;
break;
case 'o':
arguments->output_file = arg;
break;
case 'r':
arguments->repeat_count = arg ? atoi (arg) : 10;
break;
case OPT_ABORT:
arguments->abort = 1;
break;
case ARGP_KEY_NO_ARGS:
argp_usage (state);
case ARGP_KEY_ARG:
/* <span class="roman">Here we know that <code>state->arg_num == 0</code>, since we
force argument parsing to end before any more arguments can
get here.</span> */
arguments->arg1 = arg;
/* <span class="roman">Now we consume all the rest of the arguments.
<code>state->next</code> is the index in <code>state->argv</code> of the
next argument to be parsed, which is the first <var>string</var>
we're interested in, so we can just use
<code>&state->argv[state->next]</code> as the value for
arguments->strings.
<em>In addition</em>, by setting <code>state->next</code> to the end
of the arguments, we can force argp to stop parsing here and
return.</span> */
arguments->strings = &state->argv[state->next];
state->next = state->argc;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* <span class="roman">Our argp parser.</span> */
static struct argp argp = { options, parse_opt, args_doc, doc };
int main (int argc, char **argv)
{
int i, j;
struct arguments arguments;
/* <span class="roman">Default values.</span> */
arguments.silent = 0;
arguments.verbose = 0;
arguments.output_file = "-";
arguments.repeat_count = 1;
arguments.abort = 0;
/* <span class="roman">Parse our arguments; every option seen by <code>parse_opt</code> will be
reflected in <code>arguments</code>.</span> */
argp_parse (&argp, argc, argv, 0, 0, &arguments);
if (arguments.abort)
error (10, 0, "ABORTED");
for (i = 0; i < arguments.repeat_count; i++)
{
printf ("ARG1 = %s\n", arguments.arg1);
printf ("STRINGS = ");
for (j = 0; arguments.strings[j]; j++)
printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
printf ("\n");
printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
arguments.output_file,
arguments.verbose ? "yes" : "no",
arguments.silent ? "yes" : "no");
}
exit (0);
}
</pre></td></tr></table>
<hr size="6">
<a name="Argp-User-Customization"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-Example-4" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Suboptions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Argp-User-Customization-1"></a>
<h3 class="subsection">25.3.12 Argp User Customization</h3>
<a name="index-ARGP_005fHELP_005fFMT-environment-variable"></a>
<p>The formatting of argp ‘<samp>--help</samp>’ output may be controlled to some
extent by a program’s users, by setting the <code>ARGP_HELP_FMT</code>
environment variable to a comma-separated list of tokens. Whitespace is
ignored:
</p>
<dl compact="compact">
<dt> ‘<samp>dup-args</samp>’</dt>
<dt> ‘<samp>no-dup-args</samp>’</dt>
<dd><p>These turn <em>duplicate-argument-mode</em> on or off. In duplicate
argument mode, if an option that accepts an argument has multiple names,
the argument is shown for each name. Otherwise, it is only shown for the
first long option. A note is subsequently printed so the user knows that
it applies to other names as well. The default is ‘<samp>no-dup-args</samp>’,
which is less consistent, but prettier.
</p>
</dd>
<dt> ‘<samp>dup-args-note</samp>’</dt>
<dt> ‘<samp>no-dup-args-note</samp>’</dt>
<dd><p>These will enable or disable the note informing the user of suppressed
option argument duplication. The default is ‘<samp>dup-args-note</samp>’.
</p>
</dd>
<dt> ‘<samp>short-opt-col=<var>n</var></samp>’</dt>
<dd><p>This prints the first short option in column <var>n</var>. The default is 2.
</p>
</dd>
<dt> ‘<samp>long-opt-col=<var>n</var></samp>’</dt>
<dd><p>This prints the first long option in column <var>n</var>. The default is 6.
</p>
</dd>
<dt> ‘<samp>doc-opt-col=<var>n</var></samp>’</dt>
<dd><p>This prints ‘documentation options’ (see section <a href="#Argp-Option-Flags">Flags for Argp Options</a>) in
column <var>n</var>. The default is 2.
</p>
</dd>
<dt> ‘<samp>opt-doc-col=<var>n</var></samp>’</dt>
<dd><p>This prints the documentation for options starting in column
<var>n</var>. The default is 29.
</p>
</dd>
<dt> ‘<samp>header-col=<var>n</var></samp>’</dt>
<dd><p>This will indent the group headers that document groups of options to
column <var>n</var>. The default is 1.
</p>
</dd>
<dt> ‘<samp>usage-indent=<var>n</var></samp>’</dt>
<dd><p>This will indent continuation lines in ‘<samp>Usage:</samp>’ messages to column
<var>n</var>. The default is 12.
</p>
</dd>
<dt> ‘<samp>rmargin=<var>n</var></samp>’</dt>
<dd><p>This will word wrap help output at or before column <var>n</var>. The default
is 79.
</p></dd>
</dl>
<hr size="6">
<a name="Suboptions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Argp-User-Customization" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Suboptions-Example" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp-User-Customization" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-of-Suboptions"></a>
<h4 class="subsubsection">25.3.12.1 Parsing of Suboptions</h4>
<p>Having a single level of options is sometimes not enough. There might
be too many options which have to be available or a set of options is
closely related.
</p>
<p>For this case some programs use suboptions. One of the most prominent
programs is certainly <code>mount</code>(8). The <code>-o</code> option take one
argument which itself is a comma separated list of options. To ease the
programming of code like this the function <code>getsubopt</code> is
available.
</p>
<dl>
<dt><a name="index-getsubopt"></a><u>Function:</u> int <b>getsubopt</b><i> (char **<var>optionp</var>, const char* const *<var>tokens</var>, char **<var>valuep</var>)</i></dt>
<dd>
<p>The <var>optionp</var> parameter must be a pointer to a variable containing
the address of the string to process. When the function returns the
reference is updated to point to the next suboption or to the
terminating ‘<samp>\0</samp>’ character if there is no more suboption available.
</p>
<p>The <var>tokens</var> parameter references an array of strings containing the
known suboptions. All strings must be ‘<samp>\0</samp>’ terminated and to mark
the end a null pointer must be stored. When <code>getsubopt</code> finds a
possible legal suboption it compares it with all strings available in
the <var>tokens</var> array and returns the index in the string as the
indicator.
</p>
<p>In case the suboption has an associated value introduced by a ‘<samp>=</samp>’
character, a pointer to the value is returned in <var>valuep</var>. The
string is ‘<samp>\0</samp>’ terminated. If no argument is available
<var>valuep</var> is set to the null pointer. By doing this the caller can
check whether a necessary value is given or whether no unexpected value
is present.
</p>
<p>In case the next suboption in the string is not mentioned in the
<var>tokens</var> array the starting address of the suboption including a
possible value is returned in <var>valuep</var> and the return value of the
function is ‘<samp>-1</samp>’.
</p></dd></dl>
<hr size="6">
<a name="Suboptions-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Suboptions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Environment-Variables" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Argp" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Parsing-of-Suboptions-Example"></a>
<h3 class="subsection">25.3.13 Parsing of Suboptions Example</h3>
<p>The code which might appear in the <code>mount</code>(8) program is a perfect
example of the use of <code>getsubopt</code>:
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int do_all;
const char *type;
int read_size;
int write_size;
int read_only;
enum
{
RO_OPTION = 0,
RW_OPTION,
READ_SIZE_OPTION,
WRITE_SIZE_OPTION,
THE_END
};
const char *mount_opts[] =
{
[RO_OPTION] = "ro",
[RW_OPTION] = "rw",
[READ_SIZE_OPTION] = "rsize",
[WRITE_SIZE_OPTION] = "wsize",
[THE_END] = NULL
};
int
main (int argc, char *argv[])
{
char *subopts, *value;
int opt;
while ((opt = getopt (argc, argv, "at:o:")) != -1)
switch (opt)
{
case 'a':
do_all = 1;
break;
case 't':
type = optarg;
break;
case 'o':
subopts = optarg;
while (*subopts != '\0')
switch (getsubopt (&subopts, mount_opts, &value))
{
case RO_OPTION:
read_only = 1;
break;
case RW_OPTION:
read_only = 0;
break;
case READ_SIZE_OPTION:
if (value == NULL)
abort ();
read_size = atoi (value);
break;
case WRITE_SIZE_OPTION:
if (value == NULL)
abort ();
write_size = atoi (value);
break;
default:
/* <span class="roman">Unknown suboption.</span> */
printf ("Unknown suboption `%s'\n", value);
break;
}
break;
default:
abort ();
}
/* <span class="roman">Do the real work.</span> */
return 0;
}
</pre></td></tr></table>
<hr size="6">
<a name="Environment-Variables"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Suboptions-Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Environment-Access" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Environment-Variables-1"></a>
<h2 class="section">25.4 Environment Variables</h2>
<a name="index-environment-variable"></a>
<p>When a program is executed, it receives information about the context in
which it was invoked in two ways. The first mechanism uses the
<var>argv</var> and <var>argc</var> arguments to its <code>main</code> function, and is
discussed in <a href="#Program-Arguments">Program Arguments</a>. The second mechanism uses
<em>environment variables</em> and is discussed in this section.
</p>
<p>The <var>argv</var> mechanism is typically used to pass command-line
arguments specific to the particular program being invoked. The
environment, on the other hand, keeps track of information that is
shared by many programs, changes infrequently, and that is less
frequently used.
</p>
<p>The environment variables discussed in this section are the same
environment variables that you set using assignments and the
<code>export</code> command in the shell. Programs executed from the shell
inherit all of the environment variables from the shell.
</p>
<a name="index-environment"></a>
<p>Standard environment variables are used for information about the user’s
home directory, terminal type, current locale, and so on; you can define
additional variables for other purposes. The set of all environment
variables that have values is collectively known as the
<em>environment</em>.
</p>
<p>Names of environment variables are case-sensitive and must not contain
the character ‘<samp>=</samp>’. System-defined environment variables are
invariably uppercase.
</p>
<p>The values of environment variables can be anything that can be
represented as a string. A value must not contain an embedded null
character, since this is assumed to terminate the string.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Environment-Access">25.4.1 Environment Access</a></td><td> </td><td align="left" valign="top"> How to get and set the values of
environment variables.
</td></tr>
<tr><td align="left" valign="top"><a href="#Standard-Environment">25.4.2 Standard Environment Variables</a></td><td> </td><td align="left" valign="top"> These environment variables have
standard interpretations.
</td></tr>
</table>
<hr size="6">
<a name="Environment-Access"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Environment-Variables" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Standard-Environment" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Environment-Variables" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Environment-Access-1"></a>
<h3 class="subsection">25.4.1 Environment Access</h3>
<a name="index-environment-access"></a>
<a name="index-environment-representation"></a>
<p>The value of an environment variable can be accessed with the
<code>getenv</code> function. This is declared in the header file
‘<tt>stdlib.h</tt>’. Modifications of enviroment variables are not
allowed in Multi-threaded programs. The <code>getenv</code> function
can be safely used in multi-threaded programs
<a name="index-stdlib_002eh-19"></a>
</p>
<dl>
<dt><a name="index-getenv"></a><u>Function:</u> char * <b>getenv</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>This function returns a string that is the value of the environment
variable <var>name</var>. You must not modify this string. In some non-Unix
systems not using the GNU library, it might be overwritten by subsequent
calls to <code>getenv</code> (but not by any other library function). If the
environment variable <var>name</var> is not defined, the value is a null
pointer.
</p></dd></dl>
<dl>
<dt><a name="index-putenv"></a><u>Function:</u> int <b>putenv</b><i> (char *<var>string</var>)</i></dt>
<dd><p>The <code>putenv</code> function adds or removes definitions from the environment.
If the <var>string</var> is of the form ‘<samp><var>name</var>=<var>value</var></samp>’, the
definition is added to the environment. Otherwise, the <var>string</var> is
interpreted as the name of an environment variable, and any definition
for this variable in the environment is removed.
</p>
<p>The difference to the <code>setenv</code> function is that the exact string
given as the parameter <var>string</var> is put into the environment. If the
user should change the string after the <code>putenv</code> call this will
reflect in automatically in the environment. This also requires that
<var>string</var> is no automatic variable which scope is left before the
variable is removed from the environment. The same applies of course to
dynamically allocated variables which are freed later.
</p>
<p>This function is part of the extended Unix interface. Since it was also
available in old SVID libraries you should define either
<var>_XOPEN_SOURCE</var> or <var>_SVID_SOURCE</var> before including any header.
</p></dd></dl>
<dl>
<dt><a name="index-setenv"></a><u>Function:</u> int <b>setenv</b><i> (const char *<var>name</var>, const char *<var>value</var>, int <var>replace</var>)</i></dt>
<dd><p>The <code>setenv</code> function can be used to add a new definition to the
environment. The entry with the name <var>name</var> is replaced by the
value ‘<samp><var>name</var>=<var>value</var></samp>’. Please note that this is also true
if <var>value</var> is the empty string. To do this a new string is created
and the strings <var>name</var> and <var>value</var> are copied. A null pointer
for the <var>value</var> parameter is illegal. If the environment already
contains an entry with key <var>name</var> the <var>replace</var> parameter
controls the action. If replace is zero, nothing happens. Otherwise
the old entry is replaced by the new one.
</p>
<p>Please note that you cannot remove an entry completely using this function.
</p>
<p>This function was originally part of the BSD library but is now part of
the Unix standard.
</p></dd></dl>
<dl>
<dt><a name="index-unsetenv"></a><u>Function:</u> int <b>unsetenv</b><i> (const char *<var>name</var>)</i></dt>
<dd><p>Using this function one can remove an entry completely from the
environment. If the environment contains an entry with the key
<var>name</var> this whole entry is removed. A call to this function is
equivalent to a call to <code>putenv</code> when the <var>value</var> part of the
string is empty.
</p>
<p>The function return <code>-1</code> if <var>name</var> is a null pointer, points to
an empty string, or points to a string containing a <code>=</code> character.
It returns <code>0</code> if the call succeeded.
</p>
<p>This function was originally part of the BSD library but is now part of
the Unix standard. The BSD version had no return value, though.
</p></dd></dl>
<p>There is one more function to modify the whole environment. This
function is said to be used in the POSIX.9 (POSIX bindings for Fortran
77) and so one should expect it did made it into POSIX.1. But this
never happened. But we still provide this function as a GNU extension
to enable writing standard compliant Fortran environments.
</p>
<dl>
<dt><a name="index-clearenv"></a><u>Function:</u> int <b>clearenv</b><i> (void)</i></dt>
<dd><p>The <code>clearenv</code> function removes all entries from the environment.
Using <code>putenv</code> and <code>setenv</code> new entries can be added again
later.
</p>
<p>If the function is successful it returns <code>0</code>. Otherwise the return
value is nonzero.
</p></dd></dl>
<p>You can deal directly with the underlying representation of environment
objects to add more variables to the environment (for example, to
communicate with another program you are about to execute;
see section <a href="libc_26.html#Executing-a-File">Executing a File</a>).
</p>
<dl>
<dt><a name="index-environ"></a><u>Variable:</u> char ** <b>environ</b></dt>
<dd><p>The environment is represented as an array of strings. Each string is
of the format ‘<samp><var>name</var>=<var>value</var></samp>’. The order in which
strings appear in the environment is not significant, but the same
<var>name</var> must not appear more than once. The last element of the
array is a null pointer.
</p>
<p>This variable is declared in the header file ‘<tt>unistd.h</tt>’.
</p>
<p>If you just want to get the value of an environment variable, use
<code>getenv</code>.
</p></dd></dl>
<p>Unix systems, and the GNU system, pass the initial value of
<code>environ</code> as the third argument to <code>main</code>.
See section <a href="#Program-Arguments">Program Arguments</a>.
</p>
<hr size="6">
<a name="Standard-Environment"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Environment-Access" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#System-Calls" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Environment-Variables" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Standard-Environment-Variables"></a>
<h3 class="subsection">25.4.2 Standard Environment Variables</h3>
<a name="index-standard-environment-variables"></a>
<p>These environment variables have standard meanings. This doesn’t mean
that they are always present in the environment; but if these variables
<em>are</em> present, they have these meanings. You shouldn’t try to use
these environment variable names for some other purpose.
</p>
<dl compact="compact">
<dt> <code>HOME</code></dt>
<dd><a name="index-HOME-environment-variable"></a>
<a name="index-home-directory"></a>
<p>This is a string representing the user’s <em>home directory</em>, or
initial default working directory.
</p>
<p>The user can set <code>HOME</code> to any value.
If you need to make sure to obtain the proper home directory
for a particular user, you should not use <code>HOME</code>; instead,
look up the user’s name in the user database (see section <a href="libc_29.html#User-Database">User Database</a>).
</p>
<p>For most purposes, it is better to use <code>HOME</code>, precisely because
this lets the user specify the value.
</p>
</dd>
<dt> <code>LOGNAME</code></dt>
<dd><a name="index-LOGNAME-environment-variable"></a>
<p>This is the name that the user used to log in. Since the value in the
environment can be tweaked arbitrarily, this is not a reliable way to
identify the user who is running a program; a function like
<code>getlogin</code> (see section <a href="libc_29.html#Who-Logged-In">Identifying Who Logged In</a>) is better for that purpose.
</p>
<p>For most purposes, it is better to use <code>LOGNAME</code>, precisely because
this lets the user specify the value.
</p>
</dd>
<dt> <code>PATH</code></dt>
<dd><a name="index-PATH-environment-variable"></a>
<p>A <em>path</em> is a sequence of directory names which is used for
searching for a file. The variable <code>PATH</code> holds a path used
for searching for programs to be run.
</p>
<p>The <code>execlp</code> and <code>execvp</code> functions (see section <a href="libc_26.html#Executing-a-File">Executing a File</a>)
use this environment variable, as do many shells and other utilities
which are implemented in terms of those functions.
</p>
<p>The syntax of a path is a sequence of directory names separated by
colons. An empty string instead of a directory name stands for the
current directory (see section <a href="libc_14.html#Working-Directory">Working Directory</a>).
</p>
<p>A typical value for this environment variable might be a string like:
</p>
<table><tr><td> </td><td><pre class="smallexample">:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin
</pre></td></tr></table>
<p>This means that if the user tries to execute a program named <code>foo</code>,
the system will look for files named ‘<tt>foo</tt>’, ‘<tt>/bin/foo</tt>’,
‘<tt>/etc/foo</tt>’, and so on. The first of these files that exists is
the one that is executed.
</p>
</dd>
<dt> <code>TERM</code></dt>
<dd><a name="index-TERM-environment-variable"></a>
<p>This specifies the kind of terminal that is receiving program output.
Some programs can make use of this information to take advantage of
special escape sequences or terminal modes supported by particular kinds
of terminals. Many programs which use the termcap library
(see <a href="../termcap/Finding-a-Terminal-Description.html#Finding-a-Terminal-Description">Find: (termcap)Finding a Terminal Description</a> section ‘Finding a Terminal Description’ in <cite>The Termcap Library Manual</cite>) use the <code>TERM</code> environment variable, for example.
</p>
</dd>
<dt> <code>TZ</code></dt>
<dd><a name="index-TZ-environment-variable"></a>
<p>This specifies the time zone. See section <a href="libc_21.html#TZ-Variable">Specifying the Time Zone with <code>TZ</code></a>, for information about
the format of this string and how it is used.
</p>
</dd>
<dt> <code>LANG</code></dt>
<dd><a name="index-LANG-environment-variable-1"></a>
<p>This specifies the default locale to use for attribute categories where
neither <code>LC_ALL</code> nor the specific environment variable for that
category is set. See section <a href="libc_7.html#Locales">Locales and Internationalization</a>, for more information about
locales.
</p>
</dd>
<dt> <code>LC_ALL</code></dt>
<dd><a name="index-LC_005fALL-environment-variable-1"></a>
<p>If this environment variable is set it overrides the selection for all
the locales done using the other <code>LC_*</code> environment variables. The
value of the other <code>LC_*</code> environment variables is simply ignored
in this case.
</p>
</dd>
<dt> <code>LC_COLLATE</code></dt>
<dd><a name="index-LC_005fCOLLATE-environment-variable"></a>
<p>This specifies what locale to use for string sorting.
</p>
</dd>
<dt> <code>LC_CTYPE</code></dt>
<dd><a name="index-LC_005fCTYPE-environment-variable"></a>
<p>This specifies what locale to use for character sets and character
classification.
</p>
</dd>
<dt> <code>LC_MESSAGES</code></dt>
<dd><a name="index-LC_005fMESSAGES-environment-variable-1"></a>
<p>This specifies what locale to use for printing messages and to parse
responses.
</p>
</dd>
<dt> <code>LC_MONETARY</code></dt>
<dd><a name="index-LC_005fMONETARY-environment-variable"></a>
<p>This specifies what locale to use for formatting monetary values.
</p>
</dd>
<dt> <code>LC_NUMERIC</code></dt>
<dd><a name="index-LC_005fNUMERIC-environment-variable"></a>
<p>This specifies what locale to use for formatting numbers.
</p>
</dd>
<dt> <code>LC_TIME</code></dt>
<dd><a name="index-LC_005fTIME-environment-variable"></a>
<p>This specifies what locale to use for formatting date/time values.
</p>
</dd>
<dt> <code>NLSPATH</code></dt>
<dd><a name="index-NLSPATH-environment-variable-1"></a>
<p>This specifies the directories in which the <code>catopen</code> function
looks for message translation catalogs.
</p>
</dd>
<dt> <code>_POSIX_OPTION_ORDER</code></dt>
<dd><a name="index-_005fPOSIX_005fOPTION_005fORDER-environment-variable_002e"></a>
<p>If this environment variable is defined, it suppresses the usual
reordering of command line arguments by <code>getopt</code> and
<code>argp_parse</code>. See section <a href="#Argument-Syntax">Program Argument Syntax Conventions</a>.
</p>
</dd>
</dl>
<hr size="6">
<a name="System-Calls"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Standard-Environment" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="System-Calls-1"></a>
<h2 class="section">25.5 System Calls</h2>
<a name="index-system-call"></a>
<p>A system call is a request for service that a program makes of the
kernel. The service is generally something that only the kernel has
the privilege to do, such as doing I/O. Programmers don’t normally
need to be concerned with system calls because there are functions in
the GNU C library to do virtually everything that system calls do.
These functions work by making system calls themselves. For example,
there is a system call that changes the permissions of a file, but
you don’t need to know about it because you can just use the GNU C
library’s <code>chmod</code> function.
</p>
<a name="index-kernel-call"></a>
<p>System calls are sometimes called kernel calls.
</p>
<p>However, there are times when you want to make a system call explicitly,
and for that, the GNU C library provides the <code>syscall</code> function.
<code>syscall</code> is harder to use and less portable than functions like
<code>chmod</code>, but easier and more portable than coding the system call
in assembler instructions.
</p>
<p><code>syscall</code> is most useful when you are working with a system call
which is special to your system or is newer than the GNU C library you
are using. <code>syscall</code> is implemented in an entirely generic way;
the function does not know anything about what a particular system
call does or even if it is valid.
</p>
<p>The description of <code>syscall</code> in this section assumes a certain
protocol for system calls on the various platforms on which the GNU C
library runs. That protocol is not defined by any strong authority, but
we won’t describe it here either because anyone who is coding
<code>syscall</code> probably won’t accept anything less than kernel and C
library source code as a specification of the interface between them
anyway.
</p>
<p><code>syscall</code> is declared in ‘<tt>unistd.h</tt>’.
</p>
<dl>
<dt><a name="index-syscall"></a><u>Function:</u> long int <b>syscall</b><i> (long int <var>sysno</var>, ...)</i></dt>
<dd>
<p><code>syscall</code> performs a generic system call.
</p>
<a name="index-system-call-number"></a>
<p><var>sysno</var> is the system call number. Each kind of system call is
identified by a number. Macros for all the possible system call numbers
are defined in ‘<tt>sys/syscall.h</tt>’
</p>
<p>The remaining arguments are the arguments for the system call, in
order, and their meanings depend on the kind of system call. Each kind
of system call has a definite number of arguments, from zero to five.
If you code more arguments than the system call takes, the extra ones to
the right are ignored.
</p>
<p>The return value is the return value from the system call, unless the
system call failed. In that case, <code>syscall</code> returns <code>-1</code> and
sets <code>errno</code> to an error code that the system call returned. Note
that system calls do not return <code>-1</code> when they succeed.
<a name="index-errno-1"></a>
</p>
<p>If you specify an invalid <var>sysno</var>, <code>syscall</code> returns <code>-1</code>
with <code>errno</code> = <code>ENOSYS</code>.
</p>
<p>Example:
</p>
<table><tr><td> </td><td><pre class="smallexample">
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
…
int rc;
rc = syscall(SYS_chmod, "/etc/passwd", 0444);
if (rc == -1)
fprintf(stderr, "chmod failed, errno = %d\n", errno);
</pre></td></tr></table>
<p>This, if all the compatibility stars are aligned, is equivalent to the
following preferable code:
</p>
<table><tr><td> </td><td><pre class="smallexample">
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
…
int rc;
rc = chmod("/etc/passwd", 0444);
if (rc == -1)
fprintf(stderr, "chmod failed, errno = %d\n", errno);
</pre></td></tr></table>
</dd></dl>
<hr size="6">
<a name="Program-Termination"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#System-Calls" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Normal-Termination" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Program-Termination-1"></a>
<h2 class="section">25.6 Program Termination</h2>
<a name="index-program-termination"></a>
<a name="index-process-termination"></a>
<a name="index-exit-status-value"></a>
<p>The usual way for a program to terminate is simply for its <code>main</code>
function to return. The <em>exit status value</em> returned from the
<code>main</code> function is used to report information back to the process’s
parent process or shell.
</p>
<p>A program can also terminate normally by calling the <code>exit</code>
function.
</p>
<p>In addition, programs can be terminated by signals; this is discussed in
more detail in <a href="libc_24.html#Signal-Handling">Signal Handling</a>. The <code>abort</code> function causes
a signal that kills the program.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Normal-Termination">25.6.1 Normal Termination</a></td><td> </td><td align="left" valign="top"> If a program calls <code>exit</code>, a
process terminates normally.
</td></tr>
<tr><td align="left" valign="top"><a href="#Exit-Status">25.6.2 Exit Status</a></td><td> </td><td align="left" valign="top"> The <code>exit status</code> provides information
about why the process terminated.
</td></tr>
<tr><td align="left" valign="top"><a href="#Cleanups-on-Exit">25.6.3 Cleanups on Exit</a></td><td> </td><td align="left" valign="top"> A process can run its own cleanup
functions upon normal termination.
</td></tr>
<tr><td align="left" valign="top"><a href="#Aborting-a-Program">25.6.4 Aborting a Program</a></td><td> </td><td align="left" valign="top"> The <code>abort</code> function causes
abnormal program termination.
</td></tr>
<tr><td align="left" valign="top"><a href="#Termination-Internals">25.6.5 Termination Internals</a></td><td> </td><td align="left" valign="top"> What happens when a process terminates.
</td></tr>
</table>
<hr size="6">
<a name="Normal-Termination"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Program-Termination" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Exit-Status" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Normal-Termination-1"></a>
<h3 class="subsection">25.6.1 Normal Termination</h3>
<p>A process terminates normally when its program signals it is done by
calling <code>exit</code>. Returning from <code>main</code> is equivalent to
calling <code>exit</code>, and the value that <code>main</code> returns is used as
the argument to <code>exit</code>.
</p>
<dl>
<dt><a name="index-exit"></a><u>Function:</u> void <b>exit</b><i> (int <var>status</var>)</i></dt>
<dd><p>The <code>exit</code> function tells the system that the program is done, which
causes it to terminate the process.
</p>
<p><var>status</var> is the program’s exit status, which becomes part of the
process’ termination status. This function does not return.
</p></dd></dl>
<p>Normal termination causes the following actions:
</p>
<ol>
<li>
Functions that were registered with the <code>atexit</code> or <code>on_exit</code>
functions are called in the reverse order of their registration. This
mechanism allows your application to specify its own “cleanup” actions
to be performed at program termination. Typically, this is used to do
things like saving program state information in a file, or unlocking
locks in shared data bases.
</li><li>
All open streams are closed, writing out any buffered output data. See
<a href="libc_12.html#Closing-Streams">Closing Streams</a>. In addition, temporary files opened
with the <code>tmpfile</code> function are removed; see <a href="libc_14.html#Temporary-Files">Temporary Files</a>.
</li><li>
<code>_exit</code> is called, terminating the program. See section <a href="#Termination-Internals">Termination Internals</a>.
</li></ol>
<hr size="6">
<a name="Exit-Status"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Normal-Termination" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Cleanups-on-Exit" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Exit-Status-1"></a>
<h3 class="subsection">25.6.2 Exit Status</h3>
<a name="index-exit-status"></a>
<p>When a program exits, it can return to the parent process a small
amount of information about the cause of termination, using the
<em>exit status</em>. This is a value between 0 and 255 that the exiting
process passes as an argument to <code>exit</code>.
</p>
<p>Normally you should use the exit status to report very broad information
about success or failure. You can’t provide a lot of detail about the
reasons for the failure, and most parent processes would not want much
detail anyway.
</p>
<p>There are conventions for what sorts of status values certain programs
should return. The most common convention is simply 0 for success and 1
for failure. Programs that perform comparison use a different
convention: they use status 1 to indicate a mismatch, and status 2 to
indicate an inability to compare. Your program should follow an
existing convention if an existing convention makes sense for it.
</p>
<p>A general convention reserves status values 128 and up for special
purposes. In particular, the value 128 is used to indicate failure to
execute another program in a subprocess. This convention is not
universally obeyed, but it is a good idea to follow it in your programs.
</p>
<p><strong>Warning:</strong> Don’t try to use the number of errors as the exit
status. This is actually not very useful; a parent process would
generally not care how many errors occurred. Worse than that, it does
not work, because the status value is truncated to eight bits.
Thus, if the program tried to report 256 errors, the parent would
receive a report of 0 errors—that is, success.
</p>
<p>For the same reason, it does not work to use the value of <code>errno</code>
as the exit status—these can exceed 255.
</p>
<p><strong>Portability note:</strong> Some non-POSIX systems use different
conventions for exit status values. For greater portability, you can
use the macros <code>EXIT_SUCCESS</code> and <code>EXIT_FAILURE</code> for the
conventional status value for success and failure, respectively. They
are declared in the file ‘<tt>stdlib.h</tt>’.
<a name="index-stdlib_002eh-20"></a>
</p>
<dl>
<dt><a name="index-EXIT_005fSUCCESS"></a><u>Macro:</u> int <b>EXIT_SUCCESS</b></dt>
<dd><p>This macro can be used with the <code>exit</code> function to indicate
successful program completion.
</p>
<p>On POSIX systems, the value of this macro is <code>0</code>. On other
systems, the value might be some other (possibly non-constant) integer
expression.
</p></dd></dl>
<dl>
<dt><a name="index-EXIT_005fFAILURE"></a><u>Macro:</u> int <b>EXIT_FAILURE</b></dt>
<dd><p>This macro can be used with the <code>exit</code> function to indicate
unsuccessful program completion in a general sense.
</p>
<p>On POSIX systems, the value of this macro is <code>1</code>. On other
systems, the value might be some other (possibly non-constant) integer
expression. Other nonzero status values also indicate failures. Certain
programs use different nonzero status values to indicate particular
kinds of "non-success". For example, <code>diff</code> uses status value
<code>1</code> to mean that the files are different, and <code>2</code> or more to
mean that there was difficulty in opening the files.
</p></dd></dl>
<p>Don’t confuse a program’s exit status with a process’ termination status.
There are lots of ways a process can terminate besides having it’s program
finish. In the event that the process termination <em>is</em> caused by program
termination (i.e., <code>exit</code>), though, the program’s exit status becomes
part of the process’ termination status.
</p>
<hr size="6">
<a name="Cleanups-on-Exit"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Exit-Status" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Aborting-a-Program" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Cleanups-on-Exit-1"></a>
<h3 class="subsection">25.6.3 Cleanups on Exit</h3>
<p>Your program can arrange to run its own cleanup functions if normal
termination happens. If you are writing a library for use in various
application programs, then it is unreliable to insist that all
applications call the library’s cleanup functions explicitly before
exiting. It is much more robust to make the cleanup invisible to the
application, by setting up a cleanup function in the library itself
using <code>atexit</code> or <code>on_exit</code>.
</p>
<dl>
<dt><a name="index-atexit"></a><u>Function:</u> int <b>atexit</b><i> (void (*<var>function</var>) (void))</i></dt>
<dd><p>The <code>atexit</code> function registers the function <var>function</var> to be
called at normal program termination. The <var>function</var> is called with
no arguments.
</p>
<p>The return value from <code>atexit</code> is zero on success and nonzero if
the function cannot be registered.
</p></dd></dl>
<dl>
<dt><a name="index-on_005fexit"></a><u>Function:</u> int <b>on_exit</b><i> (void (*<var>function</var>)(int <var>status</var>, void *<var>arg</var>), void *<var>arg</var>)</i></dt>
<dd><p>This function is a somewhat more powerful variant of <code>atexit</code>. It
accepts two arguments, a function <var>function</var> and an arbitrary
pointer <var>arg</var>. At normal program termination, the <var>function</var> is
called with two arguments: the <var>status</var> value passed to <code>exit</code>,
and the <var>arg</var>.
</p>
<p>This function is included in the GNU C library only for compatibility
for SunOS, and may not be supported by other implementations.
</p></dd></dl>
<p>Here’s a trivial program that illustrates the use of <code>exit</code> and
<code>atexit</code>:
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <stdio.h>
#include <stdlib.h>
void
bye (void)
{
puts ("Goodbye, cruel world....");
}
int
main (void)
{
atexit (bye);
exit (EXIT_SUCCESS);
}
</pre></td></tr></table>
<p>When this program is executed, it just prints the message and exits.
</p>
<hr size="6">
<a name="Aborting-a-Program"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Cleanups-on-Exit" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Termination-Internals" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Aborting-a-Program-1"></a>
<h3 class="subsection">25.6.4 Aborting a Program</h3>
<a name="index-aborting-a-program"></a>
<p>You can abort your program using the <code>abort</code> function. The prototype
for this function is in ‘<tt>stdlib.h</tt>’.
<a name="index-stdlib_002eh-21"></a>
</p>
<dl>
<dt><a name="index-abort"></a><u>Function:</u> void <b>abort</b><i> (void)</i></dt>
<dd><p>The <code>abort</code> function causes abnormal program termination. This
does not execute cleanup functions registered with <code>atexit</code> or
<code>on_exit</code>.
</p>
<p>This function actually terminates the process by raising a
<code>SIGABRT</code> signal, and your program can include a handler to
intercept this signal; see <a href="libc_24.html#Signal-Handling">Signal Handling</a>.
</p></dd></dl>
<table class="cartouche" border="1"><tr><td>
<p><strong>Future Change Warning:</strong> Proposed Federal censorship regulations
may prohibit us from giving you information about the possibility of
calling this function. We would be required to say that this is not an
acceptable way of terminating a program.
</p></td></tr></table>
<hr size="6">
<a name="Termination-Internals"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Aborting-a-Program" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Program-Termination" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Termination-Internals-1"></a>
<h3 class="subsection">25.6.5 Termination Internals</h3>
<p>The <code>_exit</code> function is the primitive used for process termination
by <code>exit</code>. It is declared in the header file ‘<tt>unistd.h</tt>’.
<a name="index-unistd_002eh-16"></a>
</p>
<dl>
<dt><a name="index-_005fexit"></a><u>Function:</u> void <b>_exit</b><i> (int <var>status</var>)</i></dt>
<dd><p>The <code>_exit</code> function is the primitive for causing a process to
terminate with status <var>status</var>. Calling this function does not
execute cleanup functions registered with <code>atexit</code> or
<code>on_exit</code>.
</p></dd></dl>
<dl>
<dt><a name="index-_005fExit"></a><u>Function:</u> void <b>_Exit</b><i> (int <var>status</var>)</i></dt>
<dd><p>The <code>_Exit</code> function is the ISO C equivalent to <code>_exit</code>.
The ISO C committee members were not sure whether the definitions of
<code>_exit</code> and <code>_Exit</code> were compatible so they have not used the
POSIX name.
</p>
<p>This function was introduced in ISO C99 and is declared in
‘<tt>stdlib.h</tt>’.
</p></dd></dl>
<p>When a process terminates for any reason—either because the program
terminates, or as a result of a signal—the
following things happen:
</p>
<ul>
<li>
All open file descriptors in the process are closed. See section <a href="libc_13.html#Low_002dLevel-I_002fO">Low-Level Input/Output</a>.
Note that streams are not flushed automatically when the process
terminates; see <a href="libc_12.html#I_002fO-on-Streams">Input/Output on Streams</a>.
</li><li>
A process exit status is saved to be reported back to the parent process
via <code>wait</code> or <code>waitpid</code>; see <a href="libc_26.html#Process-Completion">Process Completion</a>. If the
program exited, this status includes as its low-order 8 bits the program
exit status.
</li><li>
Any child processes of the process being terminated are assigned a new
parent process. (On most systems, including GNU, this is the <code>init</code>
process, with process ID 1.)
</li><li>
A <code>SIGCHLD</code> signal is sent to the parent process.
</li><li>
If the process is a session leader that has a controlling terminal, then
a <code>SIGHUP</code> signal is sent to each process in the foreground job,
and the controlling terminal is disassociated from that session.
See section <a href="libc_27.html#Job-Control">Job Control</a>.
</li><li>
If termination of a process causes a process group to become orphaned,
and any member of that process group is stopped, then a <code>SIGHUP</code>
signal and a <code>SIGCONT</code> signal are sent to each process in the
group. See section <a href="libc_27.html#Job-Control">Job Control</a>.
</li></ul>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Program-Basics" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc_26.html#Processes" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>root</em> on <em>April 20, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|