Blame man3/basename.3

Packit 7cfc04
.\" Copyright (c) 2000 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Created, 14 Dec 2000 by Michael Kerrisk
Packit 7cfc04
.\"
Packit 7cfc04
.TH BASENAME 3  2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
basename, dirname \- parse pathname components
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <libgen.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *dirname(char *" path );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *basename(char *" path );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
Warning: there are two different functions
Packit 7cfc04
.BR basename ()
Packit 7cfc04
- see below.
Packit 7cfc04
.PP
Packit 7cfc04
The functions
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
break a null-terminated pathname string into directory
Packit 7cfc04
and filename components.
Packit 7cfc04
In the usual case,
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
returns the string up to, but not including, the final \(aq/\(aq, and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
returns the component following the final \(aq/\(aq.
Packit 7cfc04
Trailing \(aq/\(aq characters are not counted as part of the pathname.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I path
Packit 7cfc04
does not contain a slash,
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
returns the string "." while
Packit 7cfc04
.BR basename ()
Packit 7cfc04
returns a copy of
Packit 7cfc04
.IR path .
Packit 7cfc04
If
Packit 7cfc04
.I path
Packit 7cfc04
is the string "/", then both
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
return the string "/".
Packit 7cfc04
If
Packit 7cfc04
.I path
Packit 7cfc04
is a null pointer or points to an empty string, then both
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
return the string ".".
Packit 7cfc04
.PP
Packit 7cfc04
Concatenating the string returned by
Packit 7cfc04
.BR dirname (),
Packit 7cfc04
a "/", and the string returned by
Packit 7cfc04
.BR basename ()
Packit 7cfc04
yields a complete pathname.
Packit 7cfc04
.PP
Packit 7cfc04
Both
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
may modify the contents of
Packit 7cfc04
.IR path ,
Packit 7cfc04
so it may be desirable to pass a copy when calling one of
Packit 7cfc04
these functions.
Packit 7cfc04
.PP
Packit 7cfc04
These functions may return pointers to statically allocated memory
Packit 7cfc04
which may be overwritten by subsequent calls.
Packit 7cfc04
Alternatively, they may return a pointer to some part of
Packit 7cfc04
.IR path ,
Packit 7cfc04
so that the string referred to by
Packit 7cfc04
.I path
Packit 7cfc04
should not be modified or freed until the pointer returned by
Packit 7cfc04
the function is no longer required.
Packit 7cfc04
.PP
Packit 7cfc04
The following list of examples (taken from SUSv2)
Packit 7cfc04
shows the strings returned by
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
for different paths:
Packit 7cfc04
.RS
Packit 7cfc04
.TS
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l l.
Packit 7cfc04
path    	dirname	basename
Packit 7cfc04
/usr/lib	/usr	lib
Packit 7cfc04
/usr/   	/	usr
Packit 7cfc04
usr     	.	usr
Packit 7cfc04
/       	/	/
Packit 7cfc04
\&.       	.	.
Packit 7cfc04
\&..      	.	..
Packit 7cfc04
.TE
Packit 7cfc04
.RE
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
Both
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
and
Packit 7cfc04
.BR basename ()
Packit 7cfc04
return pointers to null-terminated strings.
Packit 7cfc04
(Do not pass these pointers to
Packit 7cfc04
.BR free (3).)
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw21 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR basename (),
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
There are two different versions of
Packit 7cfc04
.BR basename ()
Packit 7cfc04
- the POSIX version described above, and the GNU version, which one gets
Packit 7cfc04
after
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.BR "    #define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
Packit 7cfc04
.B "    #include <string.h>"
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The GNU version never modifies its argument, and returns the
Packit 7cfc04
empty string when
Packit 7cfc04
.I path
Packit 7cfc04
has a trailing slash, and in particular also when it is "/".
Packit 7cfc04
There is no GNU version of
Packit 7cfc04
.BR dirname ().
Packit 7cfc04
.PP
Packit 7cfc04
With glibc, one gets the POSIX version of
Packit 7cfc04
.BR basename ()
Packit 7cfc04
when
Packit 7cfc04
.I <libgen.h>
Packit 7cfc04
is included, and the GNU version otherwise.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
In the glibc implementation,
Packit 7cfc04
the POSIX versions of these functions modify the
Packit 7cfc04
.I path
Packit 7cfc04
argument, and segfault when called with a static string
Packit 7cfc04
such as "/usr/".
Packit 7cfc04
.PP
Packit 7cfc04
Before glibc 2.2.1, the glibc version of
Packit 7cfc04
.BR dirname ()
Packit 7cfc04
did not correctly handle pathnames with trailing \(aq/\(aq characters,
Packit 7cfc04
and generated a segfault if given a NULL argument.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The following code snippet demonstrates the use of
Packit 7cfc04
.BR basename ()
Packit 7cfc04
and
Packit 7cfc04
.BR dirname ():
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char *dirc, *basec, *bname, *dname;
Packit 7cfc04
char *path = "/etc/passwd";
Packit 7cfc04
Packit 7cfc04
dirc = strdup(path);
Packit 7cfc04
basec = strdup(path);
Packit 7cfc04
dname = dirname(dirc);
Packit 7cfc04
bname = basename(basec);
Packit 7cfc04
printf("dirname=%s, basename=%s\\n", dname, bname);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR basename (1),
Packit 7cfc04
.BR dirname (1)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.