📄 dirname.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>dirname</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="dirname"></a> <a name="tag_03_107"></a><!-- dirname --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_107_01"></a>NAME</h4><blockquote>dirname - report the parent directory name of a file pathname</blockquote><h4><a name="tag_03_107_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/libgen.h.html">libgen.h</a>><br><br> char *dirname(char *</tt><i>path</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_107_03"></a>DESCRIPTION</h4><blockquote><p>The <i>dirname</i>() function shall take a pointer to a character string that contains a pathname, and return a pointer to astring that is a pathname of the parent directory of that file. Trailing <tt>'/'</tt> characters in the path are not counted aspart of the path.</p><p>If <i>path</i> does not contain a <tt>'/'</tt> , then <i>dirname</i>() shall return a pointer to the string <tt>"."</tt> . If<i>path</i> is a null pointer or points to an empty string, <i>dirname</i>() shall return a pointer to the string <tt>"."</tt>.</p><p>The <i>dirname</i>() function need not be reentrant. A function that is not required to be reentrant is not required to bethread-safe.</p></blockquote><h4><a name="tag_03_107_04"></a>RETURN VALUE</h4><blockquote><p>The <i>dirname</i>() function shall return a pointer to a string that is the parent directory of <i>path</i>. If <i>path</i> isa null pointer or points to an empty string, a pointer to a string <tt>"."</tt> is returned.</p><p>The <i>dirname</i>() function may modify the string pointed to by <i>path</i>, and may return a pointer to static storage thatmay then be overwritten by subsequent calls to <i>dirname</i>().</p></blockquote><h4><a name="tag_03_107_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_107_06"></a>EXAMPLES</h4><blockquote><p>The following code fragment reads a pathname, changes the current working directory to the parent directory, and opens thefile.</p><pre><tt>char path[PATH_MAX], *pathcopy;int fd;fgets(path, PATH_MAX, stdin);pathcopy = strdup(path);chdir(dirname(pathcopy));fd = open(basename(path), O_RDONLY);</tt></pre><h5><a name="tag_03_107_06_01"></a>Sample Input and Output Strings for dirname()</h5><p>In the following table, the input string is the value pointed to by <i>path</i>, and the output string is the return value ofthe <i>dirname</i>() function.</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b>Input String</b></p></th><th align="center"><p class="tent"><b>Output String</b></p></th></tr><tr valign="top"><td align="left"><p class="tent">"/usr/lib"</p></td><td align="left"><p class="tent">"/usr"</p></td></tr><tr valign="top"><td align="left"><p class="tent">"/usr/"</p></td><td align="left"><p class="tent">"/"</p></td></tr><tr valign="top"><td align="left"><p class="tent">"usr"</p></td><td align="left"><p class="tent">"."</p></td></tr><tr valign="top"><td align="left"><p class="tent">"/"</p></td><td align="left"><p class="tent">"/"</p></td></tr><tr valign="top"><td align="left"><p class="tent">"."</p></td><td align="left"><p class="tent">"."</p></td></tr><tr valign="top"><td align="left"><p class="tent">".."</p></td><td align="left"><p class="tent">"."</p></td></tr></table></center><h5><a name="tag_03_107_06_02"></a>Changing the Current Directory to the Parent Directory</h5><p>The following program fragment reads a pathname, changes the current working directory to the parent directory, and opens thefile.</p><pre><tt>#include <unistd.h>#include <limits.h>#include <stdio.h>#include <fcntl.h>#include <string.h>#include <libgen.h>...char path[PATH_MAX], *pathcopy;int fd;...fgets(path, PATH_MAX, stdin);pathcopy = strdup(path);chdir(dirname(pathcopy));fd = open(basename(path), O_RDONLY);</tt></pre></blockquote><h4><a name="tag_03_107_07"></a>APPLICATION USAGE</h4><blockquote><p>The <i>dirname</i>() and <a href="../functions/basename.html"><i>basename</i>()</a> functions together yield a completepathname. The expression <i>dirname</i>(<i>path</i>) obtains the pathname of the directory where <i>basename</i>(<i>path</i>) isfound.</p><p>Since the meaning of the leading <tt>"//"</tt> is implementation-defined, <i>dirname</i>(" <b>//foo</b>) may return either<tt>"//"</tt> or <tt>'/'</tt> (but nothing else).</p></blockquote><h4><a name="tag_03_107_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_107_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_107_10"></a>SEE ALSO</h4><blockquote><p><a href="basename.html"><i>basename</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/libgen.h.html"><i><libgen.h></i></a></p></blockquote><h4><a name="tag_03_107_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4, Version 2.</p></blockquote><h4><a name="tag_03_107_12"></a>Issue 5</h4><blockquote><p>Moved from X/OPEN UNIX extension to BASE.</p><p>Normative text previously in the APPLICATION USAGE section is moved to the DESCRIPTION.</p><p>A note indicating that this function need not be reentrant is added to the DESCRIPTION.</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 + -