Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/dirname.html
2024-02-19 00:21:47 -05:00

245 lines
6.9 KiB
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 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-2001<br>
Copyright &copy; 2001 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 &lt;<a href="../basedefs/libgen.h.html">libgen.h</a>&gt;<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 a
string that is a pathname of the parent directory of that file. Trailing <tt>'/'</tt> characters in the path are not counted as
part 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 be
thread-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> is
a 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 that
may 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 the
file.</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 of
the <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 the
file.</p>
<pre>
<tt>#include &lt;unistd.h&gt;
#include &lt;limits.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;string.h&gt;
#include &lt;libgen.h&gt;
...
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 complete
pathname. The expression <i>dirname</i>(<i>path</i>) obtains the pathname of the directory where <i>basename</i>(<i>path</i>) is
found.</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&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/libgen.h.html"><i>&lt;libgen.h&gt;</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 &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>