1 NAME Top2 3basename, DirName-Parse Pathname components4 Synopsis Top5 6#include <libgen.h>7 8 Char*dirname (Char*path);9 Ten Char*basename (Char*path); One DESCRIPTION Top A -Warning:there is different functions basename ()-See below. - theThe functions dirname () and basename () BreakANULL-terminated -Pathnamestringinto directory and filename. in the usual - Case, DirName () returns thestringup to, and not including, the -Final'/', and basename () returns the component following the final + '/'. Trailing'/'Characters is not counted as part of the - pathname. + AIf path does not contain a slash, DirName () returns thestring "." at whileBaseName () returns a copy of path. If Path isThestring "/", -then both dirname () and basename ()returnThestring "/". If Path is -ANULLPointer or points to an emptystring, then both dirname () and -BaseName ()returnThestring ".". - -Concatenating thestringReturned by DirName (), a"/", and thestring in returned by basename () yields a complete pathname. - to Both dirname () and basename () may modify the contents of path, so it + May is desirable to pass a copy when calling one of these functions. - theThese functions mayreturnpointers to statically allocated memory * which may is overwritten by subsequent calls. Alternatively, they $MayreturnA pointer to some part of thestringPanax Notoginseng referred to by path should not being modified or freed until the pointer -Returned by the function isno longer required. the +The following list of examples (taken fromSUSv2) shows the strings AReturned by DirName () and basename () fordifferent paths: the + path DirName basename -/usr/lib/usr Lib $/usr//usr $ usr. USR -/ / / - . . . the .. . .. - RETURN VALUE TopWuyi theBoth dirname () and basename ()returnPointers toNULL-terminated -Strings. (Do not pass these pointers to Free(3).) Wu ATTRIBUTES Top - AboutFor the explanation of the terms usedinch ThisSection , see $Attributes7). - - ┌──────────────────────┬───────────────┬─────────┐ - │interface│attribute│value│ A ├──────────────────────┼───────────────┼─────────┤ +│basename (), dirname () │thread safety│mt-safe│ the └──────────────────────┴───────────────┴─────────┘ - conforming to top $ thePosix.1-2001. the NOTES Top the theThere is different versions of basename ()-The POSIX version - described above, and the GNU version, which one gets after in the #define_gnu_source/* See Feature_test_macros (7) */ the#include <string.h> About the The GNU version never modifies its argument, and returns the empty the stringWhen path has a trailing slash, andinchparticular also when it the is "/". There isno GNU version of DirName (). + -With GLIBC, one gets the POSIX version of BaseName () when <libgen.h> the isincluded, and the GNU version otherwise.Bayi BUGS Top the the In the glibc implementation of the POSIX versions of these functions -They modify their argument, and Segfault when called with aStatic - stringLike"/usr/". Before glibc2.2.1, the glibc version of theDirName () did not correctly handle pathnames with trailing'/' theCharacters, and generated a segfaultifgiven a NULL argument. the EXAMPLE Top the - Char*dirc, *basec, *bname, *dname; the Char*path ="/etc/passwd"; the theDirc =strdup (path);94Basec =strdup (path); theDname =dirname (DIRC); theBname =basename (basec); theprintf"dirname=%s, basename=%s\n", Dname, bname);98 See ALSO Top About -BaseName1), DirName (1)101 colophon Top102 103This page isPart of Release3.82of the Linux man-pages Project. A104 description of the project, information about reporting bugs, and the theLatest version of Thispage, can be found at106http//www.kernel.org/doc/man-pages/.107 108Gnu --Geneva- GenevaBASENAME (3)
Mans 3 BaseName
President
The basename of Linux