290 lines
6.3 KiB
HTML
290 lines
6.3 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of BASENAME</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>BASENAME</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
basename, dirname - parse pathname components
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/libgen.h">libgen.h</A>></B>
|
|
|
|
<B>char *dirname(char *</B><I>path</I><B>);</B>
|
|
|
|
<B>char *basename(char *</B><I>path</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
Warning: there are two different functions
|
|
<B>basename</B>()
|
|
|
|
- see below.
|
|
<P>
|
|
|
|
The functions
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
break a null-terminated pathname string into directory
|
|
and filename components.
|
|
In the usual case,
|
|
<B>dirname</B>()
|
|
|
|
returns the string up to, but not including, the final '/', and
|
|
<B>basename</B>()
|
|
|
|
returns the component following the final '/'.
|
|
Trailing '/' characters are not counted as part of the pathname.
|
|
<P>
|
|
|
|
If
|
|
<I>path</I>
|
|
|
|
does not contain a slash,
|
|
<B>dirname</B>()
|
|
|
|
returns the string "." while
|
|
<B>basename</B>()
|
|
|
|
returns a copy of
|
|
<I>path</I>.
|
|
|
|
If
|
|
<I>path</I>
|
|
|
|
is the string "/", then both
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
return the string "/".
|
|
If
|
|
<I>path</I>
|
|
|
|
is a null pointer or points to an empty string, then both
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
return the string ".".
|
|
<P>
|
|
|
|
Concatenating the string returned by
|
|
<B>dirname</B>(),
|
|
|
|
a "/", and the string returned by
|
|
<B>basename</B>()
|
|
|
|
yields a complete pathname.
|
|
<P>
|
|
|
|
Both
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
may modify the contents of
|
|
<I>path</I>,
|
|
|
|
so it may be desirable to pass a copy when calling one of
|
|
these functions.
|
|
<P>
|
|
|
|
These functions may return pointers to statically allocated memory
|
|
which may be overwritten by subsequent calls.
|
|
Alternatively, they may return a pointer to some part of
|
|
<I>path</I>,
|
|
|
|
so that the string referred to by
|
|
<I>path</I>
|
|
|
|
should not be modified or freed until the pointer returned by
|
|
the function is no longer required.
|
|
<P>
|
|
|
|
The following list of examples (taken from SUSv2)
|
|
shows the strings returned by
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
for different paths:
|
|
<DL COMPACT><DT id="1"><DD>
|
|
<TABLE>
|
|
<TR VALIGN=top><TD><B>path </B></TD><TD><B>dirname</B></TD><TD><B>basename</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>/usr/lib</TD><TD>/usr</TD><TD>lib<BR></TD><TD><BR></TD></TR>
|
|
<TR VALIGN=top><TD>/usr/ </TD><TD>/</TD><TD>usr<BR></TD><TD><BR></TD></TR>
|
|
<TR VALIGN=top><TD>usr </TD><TD>.</TD><TD>usr<BR></TD><TD><BR></TD></TR>
|
|
<TR VALIGN=top><TD>/ </TD><TD>/</TD><TD>/<BR></TD><TD><BR></TD></TR>
|
|
<TR VALIGN=top><TD>. </TD><TD>.</TD><TD>.<BR></TD><TD><BR></TD></TR>
|
|
<TR VALIGN=top><TD>.. </TD><TD>.</TD><TD>..<BR></TD><TD><BR></TD></TR>
|
|
</TABLE>
|
|
|
|
</DL>
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
Both
|
|
<B>dirname</B>()
|
|
|
|
and
|
|
<B>basename</B>()
|
|
|
|
return pointers to null-terminated strings.
|
|
(Do not pass these pointers to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).)
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>basename</B>(),
|
|
|
|
<B>dirname</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
There are two different versions of
|
|
<B>basename</B>()
|
|
|
|
- the POSIX version described above, and the GNU version, which one gets
|
|
after
|
|
<P>
|
|
|
|
|
|
|
|
<B> #define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
|
|
|
|
<B> #include <<A HREF="file:///usr/include/string.h">string.h</A>></B>
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The GNU version never modifies its argument, and returns the
|
|
empty string when
|
|
<I>path</I>
|
|
|
|
has a trailing slash, and in particular also when it is "/".
|
|
There is no GNU version of
|
|
<B>dirname</B>().
|
|
|
|
<P>
|
|
|
|
With glibc, one gets the POSIX version of
|
|
<B>basename</B>()
|
|
|
|
when
|
|
<I><<A HREF="file:///usr/include/libgen.h">libgen.h</A>></I>
|
|
|
|
is included, and the GNU version otherwise.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
In the glibc implementation,
|
|
the POSIX versions of these functions modify the
|
|
<I>path</I>
|
|
|
|
argument, and segfault when called with a static string
|
|
such as "/usr/".
|
|
<P>
|
|
|
|
Before glibc 2.2.1, the glibc version of
|
|
<B>dirname</B>()
|
|
|
|
did not correctly handle pathnames with trailing '/' characters,
|
|
and generated a segfault if given a NULL argument.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following code snippet demonstrates the use of
|
|
<B>basename</B>()
|
|
|
|
and
|
|
<B>dirname</B>():
|
|
|
|
|
|
|
|
char *dirc, *basec, *bname, *dname;
|
|
char *path = "/etc/passwd";
|
|
<P>
|
|
dirc = strdup(path);
|
|
basec = strdup(path);
|
|
dname = dirname(dirc);
|
|
bname = basename(basec);
|
|
printf("dirname=%s, basename=%s\n", dname, bname);
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+basename">basename</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+dirname">dirname</A></B>(1)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="6"><A HREF="#lbAF">ATTRIBUTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="10"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DT id="11"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="12"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:36 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|