📄 glob.html
字号:
</li><li><p>The <i>eerrno</i> argument is the value of <i>errno</i> from the failure, as set by <a href="../functions/opendir.html"><i>opendir</i>()</a>, <a href="../functions/readdir.html"><i>readdir</i>()</a>, or <a href="../functions/stat.html"><i>stat</i>()</a>. (Other values may be used to report other errors not explicitly documented for thosefunctions.)</p></li></ol><p>If (<i>*errfunc</i>()) is called and returns non-zero, or if the GLOB_ERR flag is set in <i>flags</i>, <i>glob</i>() shall stopthe scan and return GLOB_ABORTED after setting <i>gl_pathc</i> and <i>gl_pathv</i> in <i>pglob</i> to reflect the paths alreadyscanned. If GLOB_ERR is not set and either <i>errfunc</i> is a null pointer or (<i>*errfunc</i>()) returns 0, the error shall beignored.</p><p>The <i>glob</i>() function shall not fail because of large files.</p></blockquote><h4><a name="tag_03_260_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>glob</i>() shall return 0. The argument <i>pglob</i>-><i>gl_pathc</i> shall return the numberof matched pathnames and the argument <i>pglob</i>-><i>gl_pathv</i> shall contain a pointer to a null-terminated list of matchedand sorted pathnames. However, if <i>pglob</i>-><i>gl_pathc</i> is 0, the content of <i>pglob</i>-><i>gl_pathv</i> isundefined.</p><p>The <i>globfree</i>() function shall not return a value.</p><p>If <i>glob</i>() terminates due to an error, it shall return one of the non-zero constants defined in <a href="../basedefs/glob.h.html"><i><glob.h></i></a>. The arguments <i>pglob</i>-><i>gl_pathc</i> and<i>pglob</i>-><i>gl_pathv</i> are still set as defined above.</p></blockquote><h4><a name="tag_03_260_05"></a>ERRORS</h4><blockquote><p>The <i>glob</i>() function shall fail and return the corresponding value if:</p><dl compact><dt>GLOB_ABORTED</dt><dd>The scan was stopped because GLOB_ERR was set or (<i>*errfunc</i>()) returned non-zero.</dd><dt>GLOB_NOMATCH</dt><dd>The pattern does not match any existing pathname, and GLOB_NOCHECK was not set in flags.</dd><dt>GLOB_NOSPACE</dt><dd>An attempt to allocate memory failed.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_260_06"></a>EXAMPLES</h4><blockquote><p>One use of the GLOB_DOOFFS flag is by applications that build an argument list for use with <a href="../functions/execv.html"><i>execv</i>()</a>, <a href="../functions/execve.html"><i>execve</i>()</a>, or <a href="../functions/execvp.html"><i>execvp</i>()</a>. Suppose, for example, that an application wants to do the equivalent of:</p><pre><tt>ls -l *.c</tt></pre><p>but for some reason:</p><pre><tt>system("ls -l *.c")</tt></pre><p>is not acceptable. The application could obtain approximately the same result using the sequence:</p><pre><tt>globbuf.gl_offs = 2;glob("*.c", GLOB_DOOFFS, NULL, &globbuf);globbuf.gl_pathv[0] = "ls";globbuf.gl_pathv[1] = "-l";execvp("ls", &globbuf.gl_pathv[0]);</tt></pre><p>Using the same example:</p><pre><tt>ls -l *.c *.h</tt></pre><p>could be approximately simulated using GLOB_APPEND as follows:</p><pre><tt>globbuf.gl_offs = 2;glob("*.c", GLOB_DOOFFS, NULL, &globbuf);glob("*.h", GLOB_DOOFFS|GLOB_APPEND, NULL, &globbuf);...</tt></pre></blockquote><h4><a name="tag_03_260_07"></a>APPLICATION USAGE</h4><blockquote><p>This function is not provided for the purpose of enabling utilities to perform pathname expansion on their arguments, as thisoperation is performed by the shell, and utilities are explicitly not expected to redo this. Instead, it is provided forapplications that need to do pathname expansion on strings obtained from other sources, such as a pattern typed by a user or readfrom a file.</p><p>If a utility needs to see if a pathname matches a given pattern, it can use <a href="../functions/fnmatch.html"><i>fnmatch</i>()</a>.</p><p>Note that <i>gl_pathc</i> and <i>gl_pathv</i> have meaning even if <i>glob</i>() fails. This allows <i>glob</i>() to reportpartial results in the event of an error. However, if <i>gl_pathc</i> is 0, <i>gl_pathv</i> is unspecified even if <i>glob</i>()did not return an error.</p><p>The GLOB_NOCHECK option could be used when an application wants to expand a pathname if wildcards are specified, but wants totreat the pattern as just a string otherwise. The <a href="../utilities/sh.html"><i>sh</i></a> utility might use this foroption-arguments, for example.</p><p>The new pathnames generated by a subsequent call with GLOB_APPEND are not sorted together with the previous pathnames. Thismirrors the way that the shell handles pathname expansion when multiple expansions are done on a command line.</p><p>Applications that need tilde and parameter expansion should use <a href="../functions/wordexp.html"><i>wordexp</i>()</a>.</p></blockquote><h4><a name="tag_03_260_08"></a>RATIONALE</h4><blockquote><p>It was claimed that the GLOB_DOOFFS flag is unnecessary because it could be simulated using:</p><pre><tt>new = (char **)malloc((n + pglob->gl_pathc + 1) * sizeof(char *));(void) memcpy(new+n, pglob->gl_pathv, pglob->gl_pathc * sizeof(char *));(void) memset(new, 0, n * sizeof(char *));free(pglob->gl_pathv);pglob->gl_pathv = new;</tt></pre><p>However, this assumes that the memory pointed to by <i>gl_pathv</i> is a block that was separately created using <a href="../functions/malloc.html"><i>malloc</i>()</a>. This is not necessarily the case. An application should make no assumptions abouthow the memory referenced by fields in <i>pglob</i> was allocated. It might have been obtained from <a href="../functions/malloc.html"><i>malloc</i>()</a> in a large chunk and then carved up within <i>glob</i>(), or it might have beencreated using a different memory allocator. It is not the intent of the standard developers to specify or imply how the memory usedby <i>glob</i>() is managed.</p><p>The GLOB_APPEND flag would be used when an application wants to expand several different patterns into a single list.</p></blockquote><h4><a name="tag_03_260_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_260_10"></a>SEE ALSO</h4><blockquote><p><a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> , <a href="fnmatch.html"><i>fnmatch</i>()</a> , <ahref="opendir.html"><i>opendir</i>()</a> , <a href="readdir.html"><i>readdir</i>()</a> , <a href="stat.html"><i>stat</i>()</a> , <ahref="wordexp.html"><i>wordexp</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/glob.h.html"><i><glob.h></i></a>, the Shell and Utilities volume of IEEE Std 1003.1-2001</p></blockquote><h4><a name="tag_03_260_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4. Derived from the ISO POSIX-2 standard.</p></blockquote><h4><a name="tag_03_260_12"></a>Issue 5</h4><blockquote><p>Moved from POSIX2 C-language Binding to BASE.</p></blockquote><h4><a name="tag_03_260_13"></a>Issue 6</h4><blockquote><p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p><p>The <b>restrict</b> keyword is added to the <i>glob</i>() prototype for alignment with the ISO/IEC 9899:1999 standard.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -