/usr/share/doc/glibc-doc/html/libc_10.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 | <!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: 10. Pattern Matching</title>
<meta name="description" content="The GNU C Library: 10. Pattern Matching">
<meta name="keywords" content="The GNU C Library: 10. Pattern Matching">
<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="Pattern-Matching"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_9.html#Tree-Search-Function" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Wildcard-Matching" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc_9.html#Searching-and-Sorting" 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_11.html#I_002fO-Overview" 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="Pattern-Matching-1"></a>
<h1 class="chapter">10. Pattern Matching</h1>
<p>The GNU C Library provides pattern matching facilities for two kinds of
patterns: regular expressions and file-name wildcards. The library also
provides a facility for expanding variable and command references and
parsing text into words in the way the shell does.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Wildcard-Matching">10.1 Wildcard Matching</a></td><td> </td><td align="left" valign="top"> Matching a wildcard pattern against a single string.
</td></tr>
<tr><td align="left" valign="top"><a href="#Globbing">10.2 Globbing</a></td><td> </td><td align="left" valign="top"> Finding the files that match a wildcard pattern.
</td></tr>
<tr><td align="left" valign="top"><a href="#Regular-Expressions">10.3 Regular Expression Matching</a></td><td> </td><td align="left" valign="top"> Matching regular expressions against strings.
</td></tr>
<tr><td align="left" valign="top"><a href="#Word-Expansion">10.4 Shell-Style Word Expansion</a></td><td> </td><td align="left" valign="top"> Expanding shell variables, nested commands,
arithmetic, and wildcards.
This is what the shell does with shell commands.
</td></tr>
</table>
<hr size="6">
<a name="Wildcard-Matching"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Pattern-Matching" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Globbing" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Wildcard-Matching-1"></a>
<h2 class="section">10.1 Wildcard Matching</h2>
<a name="index-fnmatch_002eh"></a>
<p>This section describes how to match a wildcard pattern against a
particular string. The result is a yes or no answer: does the
string fit the pattern or not. The symbols described here are all
declared in ‘<tt>fnmatch.h</tt>’.
</p>
<dl>
<dt><a name="index-fnmatch"></a><u>Function:</u> int <b>fnmatch</b><i> (const char *<var>pattern</var>, const char *<var>string</var>, int <var>flags</var>)</i></dt>
<dd><p>This function tests whether the string <var>string</var> matches the pattern
<var>pattern</var>. It returns <code>0</code> if they do match; otherwise, it
returns the nonzero value <code>FNM_NOMATCH</code>. The arguments
<var>pattern</var> and <var>string</var> are both strings.
</p>
<p>The argument <var>flags</var> is a combination of flag bits that alter the
details of matching. See below for a list of the defined flags.
</p>
<p>In the GNU C Library, <code>fnmatch</code> cannot experience an “error”—it
always returns an answer for whether the match succeeds. However, other
implementations of <code>fnmatch</code> might sometimes report “errors”.
They would do so by returning nonzero values that are not equal to
<code>FNM_NOMATCH</code>.
</p></dd></dl>
<p>These are the available flags for the <var>flags</var> argument:
</p>
<dl compact="compact">
<dt> <code>FNM_FILE_NAME</code></dt>
<dd><p>Treat the ‘<samp>/</samp>’ character specially, for matching file names. If
this flag is set, wildcard constructs in <var>pattern</var> cannot match
‘<samp>/</samp>’ in <var>string</var>. Thus, the only way to match ‘<samp>/</samp>’ is with
an explicit ‘<samp>/</samp>’ in <var>pattern</var>.
</p>
</dd>
<dt> <code>FNM_PATHNAME</code></dt>
<dd><p>This is an alias for <code>FNM_FILE_NAME</code>; it comes from POSIX.2. We
don’t recommend this name because we don’t use the term “pathname” for
file names.
</p>
</dd>
<dt> <code>FNM_PERIOD</code></dt>
<dd><p>Treat the ‘<samp>.</samp>’ character specially if it appears at the beginning of
<var>string</var>. If this flag is set, wildcard constructs in <var>pattern</var>
cannot match ‘<samp>.</samp>’ as the first character of <var>string</var>.
</p>
<p>If you set both <code>FNM_PERIOD</code> and <code>FNM_FILE_NAME</code>, then the
special treatment applies to ‘<samp>.</samp>’ following ‘<samp>/</samp>’ as well as to
‘<samp>.</samp>’ at the beginning of <var>string</var>. (The shell uses the
<code>FNM_PERIOD</code> and <code>FNM_FILE_NAME</code> flags together for matching
file names.)
</p>
</dd>
<dt> <code>FNM_NOESCAPE</code></dt>
<dd><p>Don’t treat the ‘<samp>\</samp>’ character specially in patterns. Normally,
‘<samp>\</samp>’ quotes the following character, turning off its special meaning
(if any) so that it matches only itself. When quoting is enabled, the
pattern ‘<samp>\?</samp>’ matches only the string ‘<samp>?</samp>’, because the question
mark in the pattern acts like an ordinary character.
</p>
<p>If you use <code>FNM_NOESCAPE</code>, then ‘<samp>\</samp>’ is an ordinary character.
</p>
</dd>
<dt> <code>FNM_LEADING_DIR</code></dt>
<dd><p>Ignore a trailing sequence of characters starting with a ‘<samp>/</samp>’ in
<var>string</var>; that is to say, test whether <var>string</var> starts with a
directory name that <var>pattern</var> matches.
</p>
<p>If this flag is set, either ‘<samp>foo*</samp>’ or ‘<samp>foobar</samp>’ as a pattern
would match the string ‘<samp>foobar/frobozz</samp>’.
</p>
</dd>
<dt> <code>FNM_CASEFOLD</code></dt>
<dd><p>Ignore case in comparing <var>string</var> to <var>pattern</var>.
</p>
</dd>
<dt> <code>FNM_EXTMATCH</code></dt>
<dd><a name="index-Korn-Shell"></a>
<a name="index-ksh"></a>
<p>Recognize beside the normal patterns also the extended patterns
introduced in ‘<tt>ksh</tt>’. The patterns are written in the form
explained in the following table where <var>pattern-list</var> is a <code>|</code>
separated list of patterns.
</p>
<dl compact="compact">
<dt> <code>?(<var>pattern-list</var>)</code></dt>
<dd><p>The pattern matches if zero or one occurrences of any of the patterns
in the <var>pattern-list</var> allow matching the input string.
</p>
</dd>
<dt> <code>*(<var>pattern-list</var>)</code></dt>
<dd><p>The pattern matches if zero or more occurrences of any of the patterns
in the <var>pattern-list</var> allow matching the input string.
</p>
</dd>
<dt> <code>+(<var>pattern-list</var>)</code></dt>
<dd><p>The pattern matches if one or more occurrences of any of the patterns
in the <var>pattern-list</var> allow matching the input string.
</p>
</dd>
<dt> <code>@(<var>pattern-list</var>)</code></dt>
<dd><p>The pattern matches if exactly one occurrence of any of the patterns in
the <var>pattern-list</var> allows matching the input string.
</p>
</dd>
<dt> <code>!(<var>pattern-list</var>)</code></dt>
<dd><p>The pattern matches if the input string cannot be matched with any of
the patterns in the <var>pattern-list</var>.
</p></dd>
</dl>
</dd>
</dl>
<hr size="6">
<a name="Globbing"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Wildcard-Matching" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Calling-Glob" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Globbing-1"></a>
<h2 class="section">10.2 Globbing</h2>
<a name="index-globbing"></a>
<p>The archetypal use of wildcards is for matching against the files in a
directory, and making a list of all the matches. This is called
<em>globbing</em>.
</p>
<p>You could do this using <code>fnmatch</code>, by reading the directory entries
one by one and testing each one with <code>fnmatch</code>. But that would be
slow (and complex, since you would have to handle subdirectories by
hand).
</p>
<p>The library provides a function <code>glob</code> to make this particular use
of wildcards convenient. <code>glob</code> and the other symbols in this
section are declared in ‘<tt>glob.h</tt>’.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Calling-Glob">10.2.1 Calling <code>glob</code></a></td><td> </td><td align="left" valign="top"> Basic use of <code>glob</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Flags-for-Globbing">10.2.2 Flags for Globbing</a></td><td> </td><td align="left" valign="top"> Flags that enable various options in <code>glob</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#More-Flags-for-Globbing">10.2.3 More Flags for Globbing</a></td><td> </td><td align="left" valign="top"> GNU specific extensions to <code>glob</code>.
</td></tr>
</table>
<hr size="6">
<a name="Calling-Glob"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Globbing" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Flags-for-Globbing" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Globbing" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Calling-glob"></a>
<h3 class="subsection">10.2.1 Calling <code>glob</code></h3>
<p>The result of globbing is a vector of file names (strings). To return
this vector, <code>glob</code> uses a special data type, <code>glob_t</code>, which
is a structure. You pass <code>glob</code> the address of the structure, and
it fills in the structure’s fields to tell you about the results.
</p>
<dl>
<dt><a name="index-glob_005ft"></a><u>Data Type:</u> <b>glob_t</b></dt>
<dd><p>This data type holds a pointer to a word vector. More precisely, it
records both the address of the word vector and its size. The GNU
implementation contains some more fields which are non-standard
extensions.
</p>
<dl compact="compact">
<dt> <code>gl_pathc</code></dt>
<dd><p>The number of elements in the vector, excluding the initial null entries
if the GLOB_DOOFFS flag is used (see gl_offs below).
</p>
</dd>
<dt> <code>gl_pathv</code></dt>
<dd><p>The address of the vector. This field has type <code>char **</code>.
</p>
</dd>
<dt> <code>gl_offs</code></dt>
<dd><p>The offset of the first real element of the vector, from its nominal
address in the <code>gl_pathv</code> field. Unlike the other fields, this
is always an input to <code>glob</code>, rather than an output from it.
</p>
<p>If you use a nonzero offset, then that many elements at the beginning of
the vector are left empty. (The <code>glob</code> function fills them with
null pointers.)
</p>
<p>The <code>gl_offs</code> field is meaningful only if you use the
<code>GLOB_DOOFFS</code> flag. Otherwise, the offset is always zero
regardless of what is in this field, and the first real element comes at
the beginning of the vector.
</p>
</dd>
<dt> <code>gl_closedir</code></dt>
<dd><p>The address of an alternative implementation of the <code>closedir</code>
function. It is used if the <code>GLOB_ALTDIRFUNC</code> bit is set in
the flag parameter. The type of this field is
<code>void (*) (void *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_readdir</code></dt>
<dd><p>The address of an alternative implementation of the <code>readdir</code>
function used to read the contents of a directory. It is used if the
<code>GLOB_ALTDIRFUNC</code> bit is set in the flag parameter. The type of
this field is <code>struct dirent *(*) (void *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_opendir</code></dt>
<dd><p>The address of an alternative implementation of the <code>opendir</code>
function. It is used if the <code>GLOB_ALTDIRFUNC</code> bit is set in
the flag parameter. The type of this field is
<code>void *(*) (const char *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_stat</code></dt>
<dd><p>The address of an alternative implementation of the <code>stat</code> function
to get information about an object in the filesystem. It is used if the
<code>GLOB_ALTDIRFUNC</code> bit is set in the flag parameter. The type of
this field is <code>int (*) (const char *, struct stat *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_lstat</code></dt>
<dd><p>The address of an alternative implementation of the <code>lstat</code>
function to get information about an object in the filesystems, not
following symbolic links. It is used if the <code>GLOB_ALTDIRFUNC</code> bit
is set in the flag parameter. The type of this field is <code>int
(*) (const char *, struct stat *)</code>.
</p>
<p>This is a GNU extension.
</p></dd>
</dl>
</dd></dl>
<p>For use in the <code>glob64</code> function ‘<tt>glob.h</tt>’ contains another
definition for a very similar type. <code>glob64_t</code> differs from
<code>glob_t</code> only in the types of the members <code>gl_readdir</code>,
<code>gl_stat</code>, and <code>gl_lstat</code>.
</p>
<dl>
<dt><a name="index-glob64_005ft"></a><u>Data Type:</u> <b>glob64_t</b></dt>
<dd><p>This data type holds a pointer to a word vector. More precisely, it
records both the address of the word vector and its size. The GNU
implementation contains some more fields which are non-standard
extensions.
</p>
<dl compact="compact">
<dt> <code>gl_pathc</code></dt>
<dd><p>The number of elements in the vector, excluding the initial null entries
if the GLOB_DOOFFS flag is used (see gl_offs below).
</p>
</dd>
<dt> <code>gl_pathv</code></dt>
<dd><p>The address of the vector. This field has type <code>char **</code>.
</p>
</dd>
<dt> <code>gl_offs</code></dt>
<dd><p>The offset of the first real element of the vector, from its nominal
address in the <code>gl_pathv</code> field. Unlike the other fields, this
is always an input to <code>glob</code>, rather than an output from it.
</p>
<p>If you use a nonzero offset, then that many elements at the beginning of
the vector are left empty. (The <code>glob</code> function fills them with
null pointers.)
</p>
<p>The <code>gl_offs</code> field is meaningful only if you use the
<code>GLOB_DOOFFS</code> flag. Otherwise, the offset is always zero
regardless of what is in this field, and the first real element comes at
the beginning of the vector.
</p>
</dd>
<dt> <code>gl_closedir</code></dt>
<dd><p>The address of an alternative implementation of the <code>closedir</code>
function. It is used if the <code>GLOB_ALTDIRFUNC</code> bit is set in
the flag parameter. The type of this field is
<code>void (*) (void *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_readdir</code></dt>
<dd><p>The address of an alternative implementation of the <code>readdir64</code>
function used to read the contents of a directory. It is used if the
<code>GLOB_ALTDIRFUNC</code> bit is set in the flag parameter. The type of
this field is <code>struct dirent64 *(*) (void *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_opendir</code></dt>
<dd><p>The address of an alternative implementation of the <code>opendir</code>
function. It is used if the <code>GLOB_ALTDIRFUNC</code> bit is set in
the flag parameter. The type of this field is
<code>void *(*) (const char *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_stat</code></dt>
<dd><p>The address of an alternative implementation of the <code>stat64</code> function
to get information about an object in the filesystem. It is used if the
<code>GLOB_ALTDIRFUNC</code> bit is set in the flag parameter. The type of
this field is <code>int (*) (const char *, struct stat64 *)</code>.
</p>
<p>This is a GNU extension.
</p>
</dd>
<dt> <code>gl_lstat</code></dt>
<dd><p>The address of an alternative implementation of the <code>lstat64</code>
function to get information about an object in the filesystems, not
following symbolic links. It is used if the <code>GLOB_ALTDIRFUNC</code> bit
is set in the flag parameter. The type of this field is <code>int
(*) (const char *, struct stat64 *)</code>.
</p>
<p>This is a GNU extension.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-glob"></a><u>Function:</u> int <b>glob</b><i> (const char *<var>pattern</var>, int <var>flags</var>, int (*<var>errfunc</var>) (const char *<var>filename</var>, int <var>error-code</var>), glob_t *<var>vector-ptr</var>)</i></dt>
<dd><p>The function <code>glob</code> does globbing using the pattern <var>pattern</var>
in the current directory. It puts the result in a newly allocated
vector, and stores the size and address of this vector into
<code>*<var>vector-ptr</var></code>. The argument <var>flags</var> is a combination of
bit flags; see <a href="#Flags-for-Globbing">Flags for Globbing</a>, for details of the flags.
</p>
<p>The result of globbing is a sequence of file names. The function
<code>glob</code> allocates a string for each resulting word, then
allocates a vector of type <code>char **</code> to store the addresses of
these strings. The last element of the vector is a null pointer.
This vector is called the <em>word vector</em>.
</p>
<p>To return this vector, <code>glob</code> stores both its address and its
length (number of elements, not counting the terminating null pointer)
into <code>*<var>vector-ptr</var></code>.
</p>
<p>Normally, <code>glob</code> sorts the file names alphabetically before
returning them. You can turn this off with the flag <code>GLOB_NOSORT</code>
if you want to get the information as fast as possible. Usually it’s
a good idea to let <code>glob</code> sort them—if you process the files in
alphabetical order, the users will have a feel for the rate of progress
that your application is making.
</p>
<p>If <code>glob</code> succeeds, it returns 0. Otherwise, it returns one
of these error codes:
</p>
<dl compact="compact">
<dt> <code>GLOB_ABORTED</code>
<a name="index-GLOB_005fABORTED"></a>
</dt>
<dd><p>There was an error opening a directory, and you used the flag
<code>GLOB_ERR</code> or your specified <var>errfunc</var> returned a nonzero
value.
for an explanation of the <code>GLOB_ERR</code> flag and <var>errfunc</var>.
</p>
</dd>
<dt> <code>GLOB_NOMATCH</code>
<a name="index-GLOB_005fNOMATCH"></a>
</dt>
<dd><p>The pattern didn’t match any existing files. If you use the
<code>GLOB_NOCHECK</code> flag, then you never get this error code, because
that flag tells <code>glob</code> to <em>pretend</em> that the pattern matched
at least one file.
</p>
</dd>
<dt> <code>GLOB_NOSPACE</code>
<a name="index-GLOB_005fNOSPACE"></a>
</dt>
<dd><p>It was impossible to allocate memory to hold the result.
</p></dd>
</dl>
<p>In the event of an error, <code>glob</code> stores information in
<code>*<var>vector-ptr</var></code> about all the matches it has found so far.
</p>
<p>It is important to notice that the <code>glob</code> function will not fail if
it encounters directories or files which cannot be handled without the
LFS interfaces. The implementation of <code>glob</code> is supposed to use
these functions internally. This at least is the assumptions made by
the Unix standard. The GNU extension of allowing the user to provide
own directory handling and <code>stat</code> functions complicates things a
bit. If these callback functions are used and a large file or directory
is encountered <code>glob</code> <em>can</em> fail.
</p></dd></dl>
<dl>
<dt><a name="index-glob64"></a><u>Function:</u> int <b>glob64</b><i> (const char *<var>pattern</var>, int <var>flags</var>, int (*<var>errfunc</var>) (const char *<var>filename</var>, int <var>error-code</var>), glob64_t *<var>vector-ptr</var>)</i></dt>
<dd><p>The <code>glob64</code> function was added as part of the Large File Summit
extensions but is not part of the original LFS proposal. The reason for
this is simple: it is not necessary. The necessity for a <code>glob64</code>
function is added by the extensions of the GNU <code>glob</code>
implementation which allows the user to provide own directory handling
and <code>stat</code> functions. The <code>readdir</code> and <code>stat</code> functions
do depend on the choice of <code>_FILE_OFFSET_BITS</code> since the definition
of the types <code>struct dirent</code> and <code>struct stat</code> will change
depending on the choice.
</p>
<p>Beside this difference the <code>glob64</code> works just like <code>glob</code> in
all aspects.
</p>
<p>This function is a GNU extension.
</p></dd></dl>
<hr size="6">
<a name="Flags-for-Globbing"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Calling-Glob" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#More-Flags-for-Globbing" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Globbing" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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-Globbing-1"></a>
<h3 class="subsection">10.2.2 Flags for Globbing</h3>
<p>This section describes the flags that you can specify in the
<var>flags</var> argument to <code>glob</code>. Choose the flags you want,
and combine them with the C bitwise OR operator <code>|</code>.
</p>
<dl compact="compact">
<dt> <code>GLOB_APPEND</code>
<a name="index-GLOB_005fAPPEND"></a>
</dt>
<dd><p>Append the words from this expansion to the vector of words produced by
previous calls to <code>glob</code>. This way you can effectively expand
several words as if they were concatenated with spaces between them.
</p>
<p>In order for appending to work, you must not modify the contents of the
word vector structure between calls to <code>glob</code>. And, if you set
<code>GLOB_DOOFFS</code> in the first call to <code>glob</code>, you must also
set it when you append to the results.
</p>
<p>Note that the pointer stored in <code>gl_pathv</code> may no longer be valid
after you call <code>glob</code> the second time, because <code>glob</code> might
have relocated the vector. So always fetch <code>gl_pathv</code> from the
<code>glob_t</code> structure after each <code>glob</code> call; <strong>never</strong> save
the pointer across calls.
</p>
</dd>
<dt> <code>GLOB_DOOFFS</code>
<a name="index-GLOB_005fDOOFFS"></a>
</dt>
<dd><p>Leave blank slots at the beginning of the vector of words.
The <code>gl_offs</code> field says how many slots to leave.
The blank slots contain null pointers.
</p>
</dd>
<dt> <code>GLOB_ERR</code>
<a name="index-GLOB_005fERR"></a>
</dt>
<dd><p>Give up right away and report an error if there is any difficulty
reading the directories that must be read in order to expand <var>pattern</var>
fully. Such difficulties might include a directory in which you don’t
have the requisite access. Normally, <code>glob</code> tries its best to keep
on going despite any errors, reading whatever directories it can.
</p>
<p>You can exercise even more control than this by specifying an
error-handler function <var>errfunc</var> when you call <code>glob</code>. If
<var>errfunc</var> is not a null pointer, then <code>glob</code> doesn’t give up
right away when it can’t read a directory; instead, it calls
<var>errfunc</var> with two arguments, like this:
</p>
<table><tr><td> </td><td><pre class="smallexample">(*<var>errfunc</var>) (<var>filename</var>, <var>error-code</var>)
</pre></td></tr></table>
<p>The argument <var>filename</var> is the name of the directory that
<code>glob</code> couldn’t open or couldn’t read, and <var>error-code</var> is the
<code>errno</code> value that was reported to <code>glob</code>.
</p>
<p>If the error handler function returns nonzero, then <code>glob</code> gives up
right away. Otherwise, it continues.
</p>
</dd>
<dt> <code>GLOB_MARK</code>
<a name="index-GLOB_005fMARK"></a>
</dt>
<dd><p>If the pattern matches the name of a directory, append ‘<samp>/</samp>’ to the
directory’s name when returning it.
</p>
</dd>
<dt> <code>GLOB_NOCHECK</code>
<a name="index-GLOB_005fNOCHECK"></a>
</dt>
<dd><p>If the pattern doesn’t match any file names, return the pattern itself
as if it were a file name that had been matched. (Normally, when the
pattern doesn’t match anything, <code>glob</code> returns that there were no
matches.)
</p>
</dd>
<dt> <code>GLOB_NOSORT</code>
<a name="index-GLOB_005fNOSORT"></a>
</dt>
<dd><p>Don’t sort the file names; return them in no particular order.
(In practice, the order will depend on the order of the entries in
the directory.) The only reason <em>not</em> to sort is to save time.
</p>
</dd>
<dt> <code>GLOB_NOESCAPE</code>
<a name="index-GLOB_005fNOESCAPE"></a>
</dt>
<dd><p>Don’t treat the ‘<samp>\</samp>’ character specially in patterns. Normally,
‘<samp>\</samp>’ quotes the following character, turning off its special meaning
(if any) so that it matches only itself. When quoting is enabled, the
pattern ‘<samp>\?</samp>’ matches only the string ‘<samp>?</samp>’, because the question
mark in the pattern acts like an ordinary character.
</p>
<p>If you use <code>GLOB_NOESCAPE</code>, then ‘<samp>\</samp>’ is an ordinary character.
</p>
<p><code>glob</code> does its work by calling the function <code>fnmatch</code>
repeatedly. It handles the flag <code>GLOB_NOESCAPE</code> by turning on the
<code>FNM_NOESCAPE</code> flag in calls to <code>fnmatch</code>.
</p></dd>
</dl>
<hr size="6">
<a name="More-Flags-for-Globbing"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Flags-for-Globbing" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Globbing" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="More-Flags-for-Globbing-1"></a>
<h3 class="subsection">10.2.3 More Flags for Globbing</h3>
<p>Beside the flags described in the last section, the GNU implementation of
<code>glob</code> allows a few more flags which are also defined in the
‘<tt>glob.h</tt>’ file. Some of the extensions implement functionality
which is available in modern shell implementations.
</p>
<dl compact="compact">
<dt> <code>GLOB_PERIOD</code>
<a name="index-GLOB_005fPERIOD"></a>
</dt>
<dd><p>The <code>.</code> character (period) is treated special. It cannot be
matched by wildcards. See section <a href="#Wildcard-Matching">Wildcard Matching</a>, <code>FNM_PERIOD</code>.
</p>
</dd>
<dt> <code>GLOB_MAGCHAR</code>
<a name="index-GLOB_005fMAGCHAR"></a>
</dt>
<dd><p>The <code>GLOB_MAGCHAR</code> value is not to be given to <code>glob</code> in the
<var>flags</var> parameter. Instead, <code>glob</code> sets this bit in the
<var>gl_flags</var> element of the <var>glob_t</var> structure provided as the
result if the pattern used for matching contains any wildcard character.
</p>
</dd>
<dt> <code>GLOB_ALTDIRFUNC</code>
<a name="index-GLOB_005fALTDIRFUNC"></a>
</dt>
<dd><p>Instead of the using the using the normal functions for accessing the
filesystem the <code>glob</code> implementation uses the user-supplied
functions specified in the structure pointed to by <var>pglob</var>
parameter. For more information about the functions refer to the
sections about directory handling see <a href="libc_14.html#Accessing-Directories">Accessing Directories</a>, and
<a href="libc_14.html#Reading-Attributes">Reading the Attributes of a File</a>.
</p>
</dd>
<dt> <code>GLOB_BRACE</code>
<a name="index-GLOB_005fBRACE"></a>
</dt>
<dd><p>If this flag is given the handling of braces in the pattern is changed.
It is now required that braces appear correctly grouped. I.e., for each
opening brace there must be a closing one. Braces can be used
recursively. So it is possible to define one brace expression in
another one. It is important to note that the range of each brace
expression is completely contained in the outer brace expression (if
there is one).
</p>
<p>The string between the matching braces is separated into single
expressions by splitting at <code>,</code> (comma) characters. The commas
themselves are discarded. Please note what we said above about recursive
brace expressions. The commas used to separate the subexpressions must
be at the same level. Commas in brace subexpressions are not matched.
They are used during expansion of the brace expression of the deeper
level. The example below shows this
</p>
<table><tr><td> </td><td><pre class="smallexample">glob ("{foo/{,bar,biz},baz}", GLOB_BRACE, NULL, &result)
</pre></td></tr></table>
<p>is equivalent to the sequence
</p>
<table><tr><td> </td><td><pre class="smallexample">glob ("foo/", GLOB_BRACE, NULL, &result)
glob ("foo/bar", GLOB_BRACE|GLOB_APPEND, NULL, &result)
glob ("foo/biz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
glob ("baz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
</pre></td></tr></table>
<p>if we leave aside error handling.
</p>
</dd>
<dt> <code>GLOB_NOMAGIC</code>
<a name="index-GLOB_005fNOMAGIC"></a>
</dt>
<dd><p>If the pattern contains no wildcard constructs (it is a literal file name),
return it as the sole “matching” word, even if no file exists by that name.
</p>
</dd>
<dt> <code>GLOB_TILDE</code>
<a name="index-GLOB_005fTILDE"></a>
</dt>
<dd><p>If this flag is used the character <code>~</code> (tilde) is handled special
if it appears at the beginning of the pattern. Instead of being taken
verbatim it is used to represent the home directory of a known user.
</p>
<p>If <code>~</code> is the only character in pattern or it is followed by a
<code>/</code> (slash), the home directory of the process owner is
substituted. Using <code>getlogin</code> and <code>getpwnam</code> the information
is read from the system databases. As an example take user <code>bart</code>
with his home directory at ‘<tt>/home/bart</tt>’. For him a call like
</p>
<table><tr><td> </td><td><pre class="smallexample">glob ("~/bin/*", GLOB_TILDE, NULL, &result)
</pre></td></tr></table>
<p>would return the contents of the directory ‘<tt>/home/bart/bin</tt>’.
Instead of referring to the own home directory it is also possible to
name the home directory of other users. To do so one has to append the
user name after the tilde character. So the contents of user
<code>homer</code>’s ‘<tt>bin</tt>’ directory can be retrieved by
</p>
<table><tr><td> </td><td><pre class="smallexample">glob ("~homer/bin/*", GLOB_TILDE, NULL, &result)
</pre></td></tr></table>
<p>If the user name is not valid or the home directory cannot be determined
for some reason the pattern is left untouched and itself used as the
result. I.e., if in the last example <code>home</code> is not available the
tilde expansion yields to <code>"~homer/bin/*"</code> and <code>glob</code> is not
looking for a directory named <code>~homer</code>.
</p>
<p>This functionality is equivalent to what is available in C-shells if the
<code>nonomatch</code> flag is set.
</p>
</dd>
<dt> <code>GLOB_TILDE_CHECK</code>
<a name="index-GLOB_005fTILDE_005fCHECK"></a>
</dt>
<dd><p>If this flag is used <code>glob</code> behaves like as if <code>GLOB_TILDE</code> is
given. The only difference is that if the user name is not available or
the home directory cannot be determined for other reasons this leads to
an error. <code>glob</code> will return <code>GLOB_NOMATCH</code> instead of using
the pattern itself as the name.
</p>
<p>This functionality is equivalent to what is available in C-shells if
<code>nonomatch</code> flag is not set.
</p>
</dd>
<dt> <code>GLOB_ONLYDIR</code>
<a name="index-GLOB_005fONLYDIR"></a>
</dt>
<dd><p>If this flag is used the globbing function takes this as a
<strong>hint</strong> that the caller is only interested in directories
matching the pattern. If the information about the type of the file
is easily available non-directories will be rejected but no extra
work will be done to determine the information for each file. I.e.,
the caller must still be able to filter directories out.
</p>
<p>This functionality is only available with the GNU <code>glob</code>
implementation. It is mainly used internally to increase the
performance but might be useful for a user as well and therefore is
documented here.
</p></dd>
</dl>
<p>Calling <code>glob</code> will in most cases allocate resources which are used
to represent the result of the function call. If the same object of
type <code>glob_t</code> is used in multiple call to <code>glob</code> the resources
are freed or reused so that no leaks appear. But this does not include
the time when all <code>glob</code> calls are done.
</p>
<dl>
<dt><a name="index-globfree"></a><u>Function:</u> void <b>globfree</b><i> (glob_t *<var>pglob</var>)</i></dt>
<dd><p>The <code>globfree</code> function frees all resources allocated by previous
calls to <code>glob</code> associated with the object pointed to by
<var>pglob</var>. This function should be called whenever the currently used
<code>glob_t</code> typed object isn’t used anymore.
</p></dd></dl>
<dl>
<dt><a name="index-globfree64"></a><u>Function:</u> void <b>globfree64</b><i> (glob64_t *<var>pglob</var>)</i></dt>
<dd><p>This function is equivalent to <code>globfree</code> but it frees records of
type <code>glob64_t</code> which were allocated by <code>glob64</code>.
</p></dd></dl>
<hr size="6">
<a name="Regular-Expressions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#More-Flags-for-Globbing" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#POSIX-Regexp-Compilation" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Regular-Expression-Matching"></a>
<h2 class="section">10.3 Regular Expression Matching</h2>
<p>The GNU C library supports two interfaces for matching regular
expressions. One is the standard POSIX.2 interface, and the other is
what the GNU system has had for many years.
</p>
<p>Both interfaces are declared in the header file ‘<tt>regex.h</tt>’.
If you define <code>_POSIX_C_SOURCE</code>, then only the POSIX.2
functions, structures, and constants are declared.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#POSIX-Regexp-Compilation">10.3.1 POSIX Regular Expression Compilation</a></td><td> </td><td align="left" valign="top"> Using <code>regcomp</code> to prepare to match.
</td></tr>
<tr><td align="left" valign="top"><a href="#Flags-for-POSIX-Regexps">10.3.2 Flags for POSIX Regular Expressions</a></td><td> </td><td align="left" valign="top"> Syntax variations for <code>regcomp</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Matching-POSIX-Regexps">10.3.3 Matching a Compiled POSIX Regular Expression</a></td><td> </td><td align="left" valign="top"> Using <code>regexec</code> to match the compiled
pattern that you get from <code>regcomp</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Regexp-Subexpressions">10.3.4 Match Results with Subexpressions</a></td><td> </td><td align="left" valign="top"> Finding which parts of the string were matched.
</td></tr>
<tr><td align="left" valign="top"><a href="#Subexpression-Complications">10.3.5 Complications in Subexpression Matching</a></td><td> </td><td align="left" valign="top"> Find points of which parts were matched.
</td></tr>
<tr><td align="left" valign="top"><a href="#Regexp-Cleanup">10.3.6 POSIX Regexp Matching Cleanup</a></td><td> </td><td align="left" valign="top"> Freeing storage; reporting errors.
</td></tr>
</table>
<hr size="6">
<a name="POSIX-Regexp-Compilation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Regular-Expressions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Flags-for-POSIX-Regexps" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="POSIX-Regular-Expression-Compilation"></a>
<h3 class="subsection">10.3.1 POSIX Regular Expression Compilation</h3>
<p>Before you can actually match a regular expression, you must
<em>compile</em> it. This is not true compilation—it produces a special
data structure, not machine instructions. But it is like ordinary
compilation in that its purpose is to enable you to “execute” the
pattern fast. (See section <a href="#Matching-POSIX-Regexps">Matching a Compiled POSIX Regular Expression</a>, for how to use the
compiled regular expression for matching.)
</p>
<p>There is a special data type for compiled regular expressions:
</p>
<dl>
<dt><a name="index-regex_005ft"></a><u>Data Type:</u> <b>regex_t</b></dt>
<dd><p>This type of object holds a compiled regular expression.
It is actually a structure. It has just one field that your programs
should look at:
</p>
<dl compact="compact">
<dt> <code>re_nsub</code></dt>
<dd><p>This field holds the number of parenthetical subexpressions in the
regular expression that was compiled.
</p></dd>
</dl>
<p>There are several other fields, but we don’t describe them here, because
only the functions in the library should use them.
</p></dd></dl>
<p>After you create a <code>regex_t</code> object, you can compile a regular
expression into it by calling <code>regcomp</code>.
</p>
<dl>
<dt><a name="index-regcomp"></a><u>Function:</u> int <b>regcomp</b><i> (regex_t *restrict <var>compiled</var>, const char *restrict <var>pattern</var>, int <var>cflags</var>)</i></dt>
<dd><p>The function <code>regcomp</code> “compiles” a regular expression into a
data structure that you can use with <code>regexec</code> to match against a
string. The compiled regular expression format is designed for
efficient matching. <code>regcomp</code> stores it into <code>*<var>compiled</var></code>.
</p>
<p>It’s up to you to allocate an object of type <code>regex_t</code> and pass its
address to <code>regcomp</code>.
</p>
<p>The argument <var>cflags</var> lets you specify various options that control
the syntax and semantics of regular expressions. See section <a href="#Flags-for-POSIX-Regexps">Flags for POSIX Regular Expressions</a>.
</p>
<p>If you use the flag <code>REG_NOSUB</code>, then <code>regcomp</code> omits from
the compiled regular expression the information necessary to record
how subexpressions actually match. In this case, you might as well
pass <code>0</code> for the <var>matchptr</var> and <var>nmatch</var> arguments when
you call <code>regexec</code>.
</p>
<p>If you don’t use <code>REG_NOSUB</code>, then the compiled regular expression
does have the capacity to record how subexpressions match. Also,
<code>regcomp</code> tells you how many subexpressions <var>pattern</var> has, by
storing the number in <code><var>compiled</var>->re_nsub</code>. You can use that
value to decide how long an array to allocate to hold information about
subexpression matches.
</p>
<p><code>regcomp</code> returns <code>0</code> if it succeeds in compiling the regular
expression; otherwise, it returns a nonzero error code (see the table
below). You can use <code>regerror</code> to produce an error message string
describing the reason for a nonzero value; see <a href="#Regexp-Cleanup">POSIX Regexp Matching Cleanup</a>.
</p>
</dd></dl>
<p>Here are the possible nonzero values that <code>regcomp</code> can return:
</p>
<dl compact="compact">
<dt> <code>REG_BADBR</code></dt>
<dd><p>There was an invalid ‘<samp>\{…\}</samp>’ construct in the regular
expression. A valid ‘<samp>\{…\}</samp>’ construct must contain either
a single number, or two numbers in increasing order separated by a
comma.
</p>
</dd>
<dt> <code>REG_BADPAT</code></dt>
<dd><p>There was a syntax error in the regular expression.
</p>
</dd>
<dt> <code>REG_BADRPT</code></dt>
<dd><p>A repetition operator such as ‘<samp>?</samp>’ or ‘<samp>*</samp>’ appeared in a bad
position (with no preceding subexpression to act on).
</p>
</dd>
<dt> <code>REG_ECOLLATE</code></dt>
<dd><p>The regular expression referred to an invalid collating element (one not
defined in the current locale for string collation). See section <a href="libc_7.html#Locale-Categories">Categories of Activities that Locales Affect</a>.
</p>
</dd>
<dt> <code>REG_ECTYPE</code></dt>
<dd><p>The regular expression referred to an invalid character class name.
</p>
</dd>
<dt> <code>REG_EESCAPE</code></dt>
<dd><p>The regular expression ended with ‘<samp>\</samp>’.
</p>
</dd>
<dt> <code>REG_ESUBREG</code></dt>
<dd><p>There was an invalid number in the ‘<samp>\<var>digit</var></samp>’ construct.
</p>
</dd>
<dt> <code>REG_EBRACK</code></dt>
<dd><p>There were unbalanced square brackets in the regular expression.
</p>
</dd>
<dt> <code>REG_EPAREN</code></dt>
<dd><p>An extended regular expression had unbalanced parentheses,
or a basic regular expression had unbalanced ‘<samp>\(</samp>’ and ‘<samp>\)</samp>’.
</p>
</dd>
<dt> <code>REG_EBRACE</code></dt>
<dd><p>The regular expression had unbalanced ‘<samp>\{</samp>’ and ‘<samp>\}</samp>’.
</p>
</dd>
<dt> <code>REG_ERANGE</code></dt>
<dd><p>One of the endpoints in a range expression was invalid.
</p>
</dd>
<dt> <code>REG_ESPACE</code></dt>
<dd><p><code>regcomp</code> ran out of memory.
</p></dd>
</dl>
<hr size="6">
<a name="Flags-for-POSIX-Regexps"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#POSIX-Regexp-Compilation" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Matching-POSIX-Regexps" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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-POSIX-Regular-Expressions"></a>
<h3 class="subsection">10.3.2 Flags for POSIX Regular Expressions</h3>
<p>These are the bit flags that you can use in the <var>cflags</var> operand when
compiling a regular expression with <code>regcomp</code>.
</p>
<dl compact="compact">
<dt> <code>REG_EXTENDED</code></dt>
<dd><p>Treat the pattern as an extended regular expression, rather than as a
basic regular expression.
</p>
</dd>
<dt> <code>REG_ICASE</code></dt>
<dd><p>Ignore case when matching letters.
</p>
</dd>
<dt> <code>REG_NOSUB</code></dt>
<dd><p>Don’t bother storing the contents of the <var>matches-ptr</var> array.
</p>
</dd>
<dt> <code>REG_NEWLINE</code></dt>
<dd><p>Treat a newline in <var>string</var> as dividing <var>string</var> into multiple
lines, so that ‘<samp>$</samp>’ can match before the newline and ‘<samp>^</samp>’ can
match after. Also, don’t permit ‘<samp>.</samp>’ to match a newline, and don’t
permit ‘<samp>[^…]</samp>’ to match a newline.
</p>
<p>Otherwise, newline acts like any other ordinary character.
</p></dd>
</dl>
<hr size="6">
<a name="Matching-POSIX-Regexps"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Flags-for-POSIX-Regexps" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Regexp-Subexpressions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Matching-a-Compiled-POSIX-Regular-Expression"></a>
<h3 class="subsection">10.3.3 Matching a Compiled POSIX Regular Expression</h3>
<p>Once you have compiled a regular expression, as described in <a href="#POSIX-Regexp-Compilation">POSIX Regular Expression Compilation</a>, you can match it against strings using
<code>regexec</code>. A match anywhere inside the string counts as success,
unless the regular expression contains anchor characters (‘<samp>^</samp>’ or
‘<samp>$</samp>’).
</p>
<dl>
<dt><a name="index-regexec"></a><u>Function:</u> int <b>regexec</b><i> (const regex_t *restrict <var>compiled</var>, const char *restrict <var>string</var>, size_t <var>nmatch</var>, regmatch_t <var>matchptr</var>[restrict], int <var>eflags</var>)</i></dt>
<dd><p>This function tries to match the compiled regular expression
<code>*<var>compiled</var></code> against <var>string</var>.
</p>
<p><code>regexec</code> returns <code>0</code> if the regular expression matches;
otherwise, it returns a nonzero value. See the table below for
what nonzero values mean. You can use <code>regerror</code> to produce an
error message string describing the reason for a nonzero value;
see <a href="#Regexp-Cleanup">POSIX Regexp Matching Cleanup</a>.
</p>
<p>The argument <var>eflags</var> is a word of bit flags that enable various
options.
</p>
<p>If you want to get information about what part of <var>string</var> actually
matched the regular expression or its subexpressions, use the arguments
<var>matchptr</var> and <var>nmatch</var>. Otherwise, pass <code>0</code> for
<var>nmatch</var>, and <code>NULL</code> for <var>matchptr</var>. See section <a href="#Regexp-Subexpressions">Match Results with Subexpressions</a>.
</p></dd></dl>
<p>You must match the regular expression with the same set of current
locales that were in effect when you compiled the regular expression.
</p>
<p>The function <code>regexec</code> accepts the following flags in the
<var>eflags</var> argument:
</p>
<dl compact="compact">
<dt> <code>REG_NOTBOL</code></dt>
<dd><p>Do not regard the beginning of the specified string as the beginning of
a line; more generally, don’t make any assumptions about what text might
precede it.
</p>
</dd>
<dt> <code>REG_NOTEOL</code></dt>
<dd><p>Do not regard the end of the specified string as the end of a line; more
generally, don’t make any assumptions about what text might follow it.
</p></dd>
</dl>
<p>Here are the possible nonzero values that <code>regexec</code> can return:
</p>
<dl compact="compact">
<dt> <code>REG_NOMATCH</code></dt>
<dd><p>The pattern didn’t match the string. This isn’t really an error.
</p>
</dd>
<dt> <code>REG_ESPACE</code></dt>
<dd><p><code>regexec</code> ran out of memory.
</p></dd>
</dl>
<hr size="6">
<a name="Regexp-Subexpressions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Matching-POSIX-Regexps" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Subexpression-Complications" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Match-Results-with-Subexpressions"></a>
<h3 class="subsection">10.3.4 Match Results with Subexpressions</h3>
<p>When <code>regexec</code> matches parenthetical subexpressions of
<var>pattern</var>, it records which parts of <var>string</var> they match. It
returns that information by storing the offsets into an array whose
elements are structures of type <code>regmatch_t</code>. The first element of
the array (index <code>0</code>) records the part of the string that matched
the entire regular expression. Each other element of the array records
the beginning and end of the part that matched a single parenthetical
subexpression.
</p>
<dl>
<dt><a name="index-regmatch_005ft"></a><u>Data Type:</u> <b>regmatch_t</b></dt>
<dd><p>This is the data type of the <var>matcharray</var> array that you pass to
<code>regexec</code>. It contains two structure fields, as follows:
</p>
<dl compact="compact">
<dt> <code>rm_so</code></dt>
<dd><p>The offset in <var>string</var> of the beginning of a substring. Add this
value to <var>string</var> to get the address of that part.
</p>
</dd>
<dt> <code>rm_eo</code></dt>
<dd><p>The offset in <var>string</var> of the end of the substring.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-regoff_005ft"></a><u>Data Type:</u> <b>regoff_t</b></dt>
<dd><p><code>regoff_t</code> is an alias for another signed integer type.
The fields of <code>regmatch_t</code> have type <code>regoff_t</code>.
</p></dd></dl>
<p>The <code>regmatch_t</code> elements correspond to subexpressions
positionally; the first element (index <code>1</code>) records where the first
subexpression matched, the second element records the second
subexpression, and so on. The order of the subexpressions is the order
in which they begin.
</p>
<p>When you call <code>regexec</code>, you specify how long the <var>matchptr</var>
array is, with the <var>nmatch</var> argument. This tells <code>regexec</code> how
many elements to store. If the actual regular expression has more than
<var>nmatch</var> subexpressions, then you won’t get offset information about
the rest of them. But this doesn’t alter whether the pattern matches a
particular string or not.
</p>
<p>If you don’t want <code>regexec</code> to return any information about where
the subexpressions matched, you can either supply <code>0</code> for
<var>nmatch</var>, or use the flag <code>REG_NOSUB</code> when you compile the
pattern with <code>regcomp</code>.
</p>
<hr size="6">
<a name="Subexpression-Complications"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Regexp-Subexpressions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Regexp-Cleanup" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Complications-in-Subexpression-Matching"></a>
<h3 class="subsection">10.3.5 Complications in Subexpression Matching</h3>
<p>Sometimes a subexpression matches a substring of no characters. This
happens when ‘<samp>f\(o*\)</samp>’ matches the string ‘<samp>fum</samp>’. (It really
matches just the ‘<samp>f</samp>’.) In this case, both of the offsets identify
the point in the string where the null substring was found. In this
example, the offsets are both <code>1</code>.
</p>
<p>Sometimes the entire regular expression can match without using some of
its subexpressions at all—for example, when ‘<samp>ba\(na\)*</samp>’ matches the
string ‘<samp>ba</samp>’, the parenthetical subexpression is not used. When
this happens, <code>regexec</code> stores <code>-1</code> in both fields of the
element for that subexpression.
</p>
<p>Sometimes matching the entire regular expression can match a particular
subexpression more than once—for example, when ‘<samp>ba\(na\)*</samp>’
matches the string ‘<samp>bananana</samp>’, the parenthetical subexpression
matches three times. When this happens, <code>regexec</code> usually stores
the offsets of the last part of the string that matched the
subexpression. In the case of ‘<samp>bananana</samp>’, these offsets are
<code>6</code> and <code>8</code>.
</p>
<p>But the last match is not always the one that is chosen. It’s more
accurate to say that the last <em>opportunity</em> to match is the one
that takes precedence. What this means is that when one subexpression
appears within another, then the results reported for the inner
subexpression reflect whatever happened on the last match of the outer
subexpression. For an example, consider ‘<samp>\(ba\(na\)*s \)*</samp>’ matching
the string ‘<samp>bananas bas </samp>’. The last time the inner expression
actually matches is near the end of the first word. But it is
<em>considered</em> again in the second word, and fails to match there.
<code>regexec</code> reports nonuse of the “na” subexpression.
</p>
<p>Another place where this rule applies is when the regular expression
</p><table><tr><td> </td><td><pre class="smallexample">\(ba\(na\)*s \|nefer\(ti\)* \)*
</pre></td></tr></table>
<p>matches ‘<samp>bananas nefertiti</samp>’. The “na” subexpression does match
in the first word, but it doesn’t match in the second word because the
other alternative is used there. Once again, the second repetition of
the outer subexpression overrides the first, and within that second
repetition, the “na” subexpression is not used. So <code>regexec</code>
reports nonuse of the “na” subexpression.
</p>
<hr size="6">
<a name="Regexp-Cleanup"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Subexpression-Complications" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Regular-Expressions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="POSIX-Regexp-Matching-Cleanup"></a>
<h3 class="subsection">10.3.6 POSIX Regexp Matching Cleanup</h3>
<p>When you are finished using a compiled regular expression, you can
free the storage it uses by calling <code>regfree</code>.
</p>
<dl>
<dt><a name="index-regfree"></a><u>Function:</u> void <b>regfree</b><i> (regex_t *<var>compiled</var>)</i></dt>
<dd><p>Calling <code>regfree</code> frees all the storage that <code>*<var>compiled</var></code>
points to. This includes various internal fields of the <code>regex_t</code>
structure that aren’t documented in this manual.
</p>
<p><code>regfree</code> does not free the object <code>*<var>compiled</var></code> itself.
</p></dd></dl>
<p>You should always free the space in a <code>regex_t</code> structure with
<code>regfree</code> before using the structure to compile another regular
expression.
</p>
<p>When <code>regcomp</code> or <code>regexec</code> reports an error, you can use
the function <code>regerror</code> to turn it into an error message string.
</p>
<dl>
<dt><a name="index-regerror"></a><u>Function:</u> size_t <b>regerror</b><i> (int <var>errcode</var>, const regex_t *restrict <var>compiled</var>, char *restrict <var>buffer</var>, size_t <var>length</var>)</i></dt>
<dd><p>This function produces an error message string for the error code
<var>errcode</var>, and stores the string in <var>length</var> bytes of memory
starting at <var>buffer</var>. For the <var>compiled</var> argument, supply the
same compiled regular expression structure that <code>regcomp</code> or
<code>regexec</code> was working with when it got the error. Alternatively,
you can supply <code>NULL</code> for <var>compiled</var>; you will still get a
meaningful error message, but it might not be as detailed.
</p>
<p>If the error message can’t fit in <var>length</var> bytes (including a
terminating null character), then <code>regerror</code> truncates it.
The string that <code>regerror</code> stores is always null-terminated
even if it has been truncated.
</p>
<p>The return value of <code>regerror</code> is the minimum length needed to
store the entire error message. If this is less than <var>length</var>, then
the error message was not truncated, and you can use it. Otherwise, you
should call <code>regerror</code> again with a larger buffer.
</p>
<p>Here is a function which uses <code>regerror</code>, but always dynamically
allocates a buffer for the error message:
</p>
<table><tr><td> </td><td><pre class="smallexample">char *get_regerror (int errcode, regex_t *compiled)
{
size_t length = regerror (errcode, compiled, NULL, 0);
char *buffer = xmalloc (length);
(void) regerror (errcode, compiled, buffer, length);
return buffer;
}
</pre></td></tr></table>
</dd></dl>
<hr size="6">
<a name="Word-Expansion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Regexp-Cleanup" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Expansion-Stages" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Shell_002dStyle-Word-Expansion"></a>
<h2 class="section">10.4 Shell-Style Word Expansion</h2>
<a name="index-word-expansion"></a>
<a name="index-expansion-of-shell-words"></a>
<p><em>Word expansion</em> means the process of splitting a string into
<em>words</em> and substituting for variables, commands, and wildcards
just as the shell does.
</p>
<p>For example, when you write ‘<samp>ls -l foo.c</samp>’, this string is split
into three separate words—‘<samp>ls</samp>’, ‘<samp>-l</samp>’ and ‘<samp>foo.c</samp>’.
This is the most basic function of word expansion.
</p>
<p>When you write ‘<samp>ls *.c</samp>’, this can become many words, because
the word ‘<samp>*.c</samp>’ can be replaced with any number of file names.
This is called <em>wildcard expansion</em>, and it is also a part of
word expansion.
</p>
<p>When you use ‘<samp>echo $PATH</samp>’ to print your path, you are taking
advantage of <em>variable substitution</em>, which is also part of word
expansion.
</p>
<p>Ordinary programs can perform word expansion just like the shell by
calling the library function <code>wordexp</code>.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Expansion-Stages">10.4.1 The Stages of Word Expansion</a></td><td> </td><td align="left" valign="top"> What word expansion does to a string.
</td></tr>
<tr><td align="left" valign="top"><a href="#Calling-Wordexp">10.4.2 Calling <code>wordexp</code></a></td><td> </td><td align="left" valign="top"> How to call <code>wordexp</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Flags-for-Wordexp">10.4.3 Flags for Word Expansion</a></td><td> </td><td align="left" valign="top"> Options you can enable in <code>wordexp</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Wordexp-Example">10.4.4 <code>wordexp</code> Example</a></td><td> </td><td align="left" valign="top"> A sample program that does word expansion.
</td></tr>
<tr><td align="left" valign="top"><a href="#Tilde-Expansion">10.4.5 Details of Tilde Expansion</a></td><td> </td><td align="left" valign="top"> Details of how tilde expansion works.
</td></tr>
<tr><td align="left" valign="top"><a href="#Variable-Substitution">10.4.6 Details of Variable Substitution</a></td><td> </td><td align="left" valign="top"> Different types of variable substitution.
</td></tr>
</table>
<hr size="6">
<a name="Expansion-Stages"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Word-Expansion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Calling-Wordexp" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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-Stages-of-Word-Expansion"></a>
<h3 class="subsection">10.4.1 The Stages of Word Expansion</h3>
<p>When word expansion is applied to a sequence of words, it performs the
following transformations in the order shown here:
</p>
<ol>
<li>
<a name="index-tilde-expansion"></a>
<em>Tilde expansion</em>: Replacement of ‘<samp>~foo</samp>’ with the name of
the home directory of ‘<samp>foo</samp>’.
</li><li>
Next, three different transformations are applied in the same step,
from left to right:
<ul>
<li>
<a name="index-variable-substitution"></a>
<a name="index-substitution-of-variables-and-commands"></a>
<em>Variable substitution</em>: Environment variables are substituted for
references such as ‘<samp>$foo</samp>’.
</li><li>
<a name="index-command-substitution"></a>
<em>Command substitution</em>: Constructs such as ‘<samp>`cat foo`</samp>’ and
the equivalent ‘<samp>$(cat foo)</samp>’ are replaced with the output from
the inner command.
</li><li>
<a name="index-arithmetic-expansion"></a>
<em>Arithmetic expansion</em>: Constructs such as ‘<samp>$(($x-1))</samp>’ are
replaced with the result of the arithmetic computation.
</li></ul>
</li><li>
<a name="index-field-splitting"></a>
<em>Field splitting</em>: subdivision of the text into <em>words</em>.
</li><li>
<a name="index-wildcard-expansion"></a>
<em>Wildcard expansion</em>: The replacement of a construct such as ‘<samp>*.c</samp>’
with a list of ‘<samp>.c</samp>’ file names. Wildcard expansion applies to an
entire word at a time, and replaces that word with 0 or more file names
that are themselves words.
</li><li>
<a name="index-quote-removal"></a>
<a name="index-removal-of-quotes"></a>
<em>Quote removal</em>: The deletion of string-quotes, now that they have
done their job by inhibiting the above transformations when appropriate.
</li></ol>
<p>For the details of these transformations, and how to write the constructs
that use them, see <cite>The BASH Manual</cite> (to appear).
</p>
<hr size="6">
<a name="Calling-Wordexp"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Expansion-Stages" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Flags-for-Wordexp" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Calling-wordexp"></a>
<h3 class="subsection">10.4.2 Calling <code>wordexp</code></h3>
<p>All the functions, constants and data types for word expansion are
declared in the header file ‘<tt>wordexp.h</tt>’.
</p>
<p>Word expansion produces a vector of words (strings). To return this
vector, <code>wordexp</code> uses a special data type, <code>wordexp_t</code>, which
is a structure. You pass <code>wordexp</code> the address of the structure,
and it fills in the structure’s fields to tell you about the results.
</p>
<dl>
<dt><a name="index-wordexp_005ft"></a><u>Data Type:</u> <b>wordexp_t</b></dt>
<dd><p>This data type holds a pointer to a word vector. More precisely, it
records both the address of the word vector and its size.
</p>
<dl compact="compact">
<dt> <code>we_wordc</code></dt>
<dd><p>The number of elements in the vector.
</p>
</dd>
<dt> <code>we_wordv</code></dt>
<dd><p>The address of the vector. This field has type <code>char **</code>.
</p>
</dd>
<dt> <code>we_offs</code></dt>
<dd><p>The offset of the first real element of the vector, from its nominal
address in the <code>we_wordv</code> field. Unlike the other fields, this
is always an input to <code>wordexp</code>, rather than an output from it.
</p>
<p>If you use a nonzero offset, then that many elements at the beginning of
the vector are left empty. (The <code>wordexp</code> function fills them with
null pointers.)
</p>
<p>The <code>we_offs</code> field is meaningful only if you use the
<code>WRDE_DOOFFS</code> flag. Otherwise, the offset is always zero
regardless of what is in this field, and the first real element comes at
the beginning of the vector.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-wordexp"></a><u>Function:</u> int <b>wordexp</b><i> (const char *<var>words</var>, wordexp_t *<var>word-vector-ptr</var>, int <var>flags</var>)</i></dt>
<dd><p>Perform word expansion on the string <var>words</var>, putting the result in
a newly allocated vector, and store the size and address of this vector
into <code>*<var>word-vector-ptr</var></code>. The argument <var>flags</var> is a
combination of bit flags; see <a href="#Flags-for-Wordexp">Flags for Word Expansion</a>, for details of
the flags.
</p>
<p>You shouldn’t use any of the characters ‘<samp>|&;<></samp>’ in the string
<var>words</var> unless they are quoted; likewise for newline. If you use
these characters unquoted, you will get the <code>WRDE_BADCHAR</code> error
code. Don’t use parentheses or braces unless they are quoted or part of
a word expansion construct. If you use quotation characters ‘<samp>'"`</samp>’,
they should come in pairs that balance.
</p>
<p>The results of word expansion are a sequence of words. The function
<code>wordexp</code> allocates a string for each resulting word, then
allocates a vector of type <code>char **</code> to store the addresses of
these strings. The last element of the vector is a null pointer.
This vector is called the <em>word vector</em>.
</p>
<p>To return this vector, <code>wordexp</code> stores both its address and its
length (number of elements, not counting the terminating null pointer)
into <code>*<var>word-vector-ptr</var></code>.
</p>
<p>If <code>wordexp</code> succeeds, it returns 0. Otherwise, it returns one
of these error codes:
</p>
<dl compact="compact">
<dt> <code>WRDE_BADCHAR</code></dt>
<dd><p>The input string <var>words</var> contains an unquoted invalid character such
as ‘<samp>|</samp>’.
</p>
</dd>
<dt> <code>WRDE_BADVAL</code></dt>
<dd><p>The input string refers to an undefined shell variable, and you used the flag
<code>WRDE_UNDEF</code> to forbid such references.
</p>
</dd>
<dt> <code>WRDE_CMDSUB</code></dt>
<dd><p>The input string uses command substitution, and you used the flag
<code>WRDE_NOCMD</code> to forbid command substitution.
</p>
</dd>
<dt> <code>WRDE_NOSPACE</code></dt>
<dd><p>It was impossible to allocate memory to hold the result. In this case,
<code>wordexp</code> can store part of the results—as much as it could
allocate room for.
</p>
</dd>
<dt> <code>WRDE_SYNTAX</code></dt>
<dd><p>There was a syntax error in the input string. For example, an unmatched
quoting character is a syntax error.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-wordfree"></a><u>Function:</u> void <b>wordfree</b><i> (wordexp_t *<var>word-vector-ptr</var>)</i></dt>
<dd><p>Free the storage used for the word-strings and vector that
<code>*<var>word-vector-ptr</var></code> points to. This does not free the
structure <code>*<var>word-vector-ptr</var></code> itself—only the other
data it points to.
</p></dd></dl>
<hr size="6">
<a name="Flags-for-Wordexp"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Calling-Wordexp" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Wordexp-Example" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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-Word-Expansion"></a>
<h3 class="subsection">10.4.3 Flags for Word Expansion</h3>
<p>This section describes the flags that you can specify in the
<var>flags</var> argument to <code>wordexp</code>. Choose the flags you want,
and combine them with the C operator <code>|</code>.
</p>
<dl compact="compact">
<dt> <code>WRDE_APPEND</code></dt>
<dd><p>Append the words from this expansion to the vector of words produced by
previous calls to <code>wordexp</code>. This way you can effectively expand
several words as if they were concatenated with spaces between them.
</p>
<p>In order for appending to work, you must not modify the contents of the
word vector structure between calls to <code>wordexp</code>. And, if you set
<code>WRDE_DOOFFS</code> in the first call to <code>wordexp</code>, you must also
set it when you append to the results.
</p>
</dd>
<dt> <code>WRDE_DOOFFS</code></dt>
<dd><p>Leave blank slots at the beginning of the vector of words.
The <code>we_offs</code> field says how many slots to leave.
The blank slots contain null pointers.
</p>
</dd>
<dt> <code>WRDE_NOCMD</code></dt>
<dd><p>Don’t do command substitution; if the input requests command substitution,
report an error.
</p>
</dd>
<dt> <code>WRDE_REUSE</code></dt>
<dd><p>Reuse a word vector made by a previous call to <code>wordexp</code>.
Instead of allocating a new vector of words, this call to <code>wordexp</code>
will use the vector that already exists (making it larger if necessary).
</p>
<p>Note that the vector may move, so it is not safe to save an old pointer
and use it again after calling <code>wordexp</code>. You must fetch
<code>we_pathv</code> anew after each call.
</p>
</dd>
<dt> <code>WRDE_SHOWERR</code></dt>
<dd><p>Do show any error messages printed by commands run by command substitution.
More precisely, allow these commands to inherit the standard error output
stream of the current process. By default, <code>wordexp</code> gives these
commands a standard error stream that discards all output.
</p>
</dd>
<dt> <code>WRDE_UNDEF</code></dt>
<dd><p>If the input refers to a shell variable that is not defined, report an
error.
</p></dd>
</dl>
<hr size="6">
<a name="Wordexp-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Flags-for-Wordexp" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Tilde-Expansion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="wordexp-Example"></a>
<h3 class="subsection">10.4.4 <code>wordexp</code> Example</h3>
<p>Here is an example of using <code>wordexp</code> to expand several strings
and use the results to run a shell command. It also shows the use of
<code>WRDE_APPEND</code> to concatenate the expansions and of <code>wordfree</code>
to free the space allocated by <code>wordexp</code>.
</p>
<table><tr><td> </td><td><pre class="smallexample">int
expand_and_execute (const char *program, const char **options)
{
wordexp_t result;
pid_t pid
int status, i;
/* <span class="roman">Expand the string for the program to run.</span> */
switch (wordexp (program, &result, 0))
{
case 0: /* <span class="roman">Successful</span>. */
break;
case WRDE_NOSPACE:
/* <span class="roman">If the error was <code>WRDE_NOSPACE</code>,</span>
<span class="roman">then perhaps part of the result was allocated.</span> */
wordfree (&result);
default: /* <span class="roman">Some other error.</span> */
return -1;
}
/* <span class="roman">Expand the strings specified for the arguments.</span> */
for (i = 0; options[i] != NULL; i++)
{
if (wordexp (options[i], &result, WRDE_APPEND))
{
wordfree (&result);
return -1;
}
}
pid = fork ();
if (pid == 0)
{
/* <span class="roman">This is the child process. Execute the command.</span> */
execv (result.we_wordv[0], result.we_wordv);
exit (EXIT_FAILURE);
}
else if (pid < 0)
/* <span class="roman">The fork failed. Report failure.</span> */
status = -1;
else
/* <span class="roman">This is the parent process. Wait for the child to complete.</span> */
if (waitpid (pid, &status, 0) != pid)
status = -1;
wordfree (&result);
return status;
}
</pre></td></tr></table>
<hr size="6">
<a name="Tilde-Expansion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Wordexp-Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Variable-Substitution" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Details-of-Tilde-Expansion"></a>
<h3 class="subsection">10.4.5 Details of Tilde Expansion</h3>
<p>It’s a standard part of shell syntax that you can use ‘<samp>~</samp>’ at the
beginning of a file name to stand for your own home directory. You
can use ‘<samp>~<var>user</var></samp>’ to stand for <var>user</var>’s home directory.
</p>
<p><em>Tilde expansion</em> is the process of converting these abbreviations
to the directory names that they stand for.
</p>
<p>Tilde expansion applies to the ‘<samp>~</samp>’ plus all following characters up
to whitespace or a slash. It takes place only at the beginning of a
word, and only if none of the characters to be transformed is quoted in
any way.
</p>
<p>Plain ‘<samp>~</samp>’ uses the value of the environment variable <code>HOME</code>
as the proper home directory name. ‘<samp>~</samp>’ followed by a user name
uses <code>getpwname</code> to look up that user in the user database, and
uses whatever directory is recorded there. Thus, ‘<samp>~</samp>’ followed
by your own name can give different results from plain ‘<samp>~</samp>’, if
the value of <code>HOME</code> is not really your home directory.
</p>
<hr size="6">
<a name="Variable-Substitution"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Tilde-Expansion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Word-Expansion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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="Details-of-Variable-Substitution"></a>
<h3 class="subsection">10.4.6 Details of Variable Substitution</h3>
<p>Part of ordinary shell syntax is the use of ‘<samp>$<var>variable</var></samp>’ to
substitute the value of a shell variable into a command. This is called
<em>variable substitution</em>, and it is one part of doing word expansion.
</p>
<p>There are two basic ways you can write a variable reference for
substitution:
</p>
<dl compact="compact">
<dt> <code>${<var>variable</var>}</code></dt>
<dd><p>If you write braces around the variable name, then it is completely
unambiguous where the variable name ends. You can concatenate
additional letters onto the end of the variable value by writing them
immediately after the close brace. For example, ‘<samp>${foo}s</samp>’
expands into ‘<samp>tractors</samp>’.
</p>
</dd>
<dt> <code>$<var>variable</var></code></dt>
<dd><p>If you do not put braces around the variable name, then the variable
name consists of all the alphanumeric characters and underscores that
follow the ‘<samp>$</samp>’. The next punctuation character ends the variable
name. Thus, ‘<samp>$foo-bar</samp>’ refers to the variable <code>foo</code> and expands
into ‘<samp>tractor-bar</samp>’.
</p></dd>
</dl>
<p>When you use braces, you can also use various constructs to modify the
value that is substituted, or test it in various ways.
</p>
<dl compact="compact">
<dt> <code>${<var>variable</var>:-<var>default</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but if that is empty or
undefined, use <var>default</var> instead.
</p>
</dd>
<dt> <code>${<var>variable</var>:=<var>default</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but if that is empty or
undefined, use <var>default</var> instead and set the variable to
<var>default</var>.
</p>
</dd>
<dt> <code>${<var>variable</var>:?<var>message</var>}</code></dt>
<dd><p>If <var>variable</var> is defined and not empty, substitute its value.
</p>
<p>Otherwise, print <var>message</var> as an error message on the standard error
stream, and consider word expansion a failure.
</p>
</dd>
<dt> <code>${<var>variable</var>:+<var>replacement</var>}</code></dt>
<dd><p>Substitute <var>replacement</var>, but only if <var>variable</var> is defined and
nonempty. Otherwise, substitute nothing for this construct.
</p></dd>
</dl>
<dl compact="compact">
<dt> <code>${#<var>variable</var>}</code></dt>
<dd><p>Substitute a numeral which expresses in base ten the number of
characters in the value of <var>variable</var>. ‘<samp>${#foo}</samp>’ stands for
‘<samp>7</samp>’, because ‘<samp>tractor</samp>’ is seven characters.
</p></dd>
</dl>
<p>These variants of variable substitution let you remove part of the
variable’s value before substituting it. The <var>prefix</var> and
<var>suffix</var> are not mere strings; they are wildcard patterns, just
like the patterns that you use to match multiple file names. But
in this context, they match against parts of the variable value
rather than against file names.
</p>
<dl compact="compact">
<dt> <code>${<var>variable</var>%%<var>suffix</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but first discard from that
variable any portion at the end that matches the pattern <var>suffix</var>.
</p>
<p>If there is more than one alternative for how to match against
<var>suffix</var>, this construct uses the longest possible match.
</p>
<p>Thus, ‘<samp>${foo%%r*}</samp>’ substitutes ‘<samp>t</samp>’, because the largest
match for ‘<samp>r*</samp>’ at the end of ‘<samp>tractor</samp>’ is ‘<samp>ractor</samp>’.
</p>
</dd>
<dt> <code>${<var>variable</var>%<var>suffix</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but first discard from that
variable any portion at the end that matches the pattern <var>suffix</var>.
</p>
<p>If there is more than one alternative for how to match against
<var>suffix</var>, this construct uses the shortest possible alternative.
</p>
<p>Thus, ‘<samp>${foo%r*}</samp>’ substitutes ‘<samp>tracto</samp>’, because the shortest
match for ‘<samp>r*</samp>’ at the end of ‘<samp>tractor</samp>’ is just ‘<samp>r</samp>’.
</p>
</dd>
<dt> <code>${<var>variable</var>##<var>prefix</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but first discard from that
variable any portion at the beginning that matches the pattern <var>prefix</var>.
</p>
<p>If there is more than one alternative for how to match against
<var>prefix</var>, this construct uses the longest possible match.
</p>
<p>Thus, ‘<samp>${foo##*t}</samp>’ substitutes ‘<samp>or</samp>’, because the largest
match for ‘<samp>*t</samp>’ at the beginning of ‘<samp>tractor</samp>’ is ‘<samp>tract</samp>’.
</p>
</dd>
<dt> <code>${<var>variable</var>#<var>prefix</var>}</code></dt>
<dd><p>Substitute the value of <var>variable</var>, but first discard from that
variable any portion at the beginning that matches the pattern <var>prefix</var>.
</p>
<p>If there is more than one alternative for how to match against
<var>prefix</var>, this construct uses the shortest possible alternative.
</p>
<p>Thus, ‘<samp>${foo#*t}</samp>’ substitutes ‘<samp>ractor</samp>’, because the shortest
match for ‘<samp>*t</samp>’ at the beginning of ‘<samp>tractor</samp>’ is just ‘<samp>t</samp>’.
</p>
</dd>
</dl>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Pattern-Matching" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc_11.html#I_002fO-Overview" 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>
|