{"id":6889,"date":"2022-12-20T19:34:15","date_gmt":"2022-12-20T22:34:15","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/basename-man3\/"},"modified":"2022-12-20T19:34:15","modified_gmt":"2022-12-20T22:34:15","slug":"basename-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/basename-man3\/","title":{"rendered":"BASENAME (man3)"},"content":{"rendered":"<h1 align=\"center\">BASENAME<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#NOTES\">NOTES<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">basename, dirname \u2212 parse pathname components<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <libgen.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>char *dirname(char *<\/b><i>path<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>char *basename(char *<\/b><i>path<\/i><b>);<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Warning: there are two different functions <b>basename<\/b>() &#8211; see below.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8216;\/&#8217;, and <b>basename<\/b>() returns the component following the final &#8216;\/&#8217;. Trailing &#8216;\/&#8217; characters are not counted as part of the pathname.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>path<\/i> does not contain a slash, <b>dirname<\/b>() returns the string &#8220;.&#8221; while <b>basename<\/b>() returns a copy of <i>path<\/i>. If <i>path<\/i> is the string &#8220;\/&#8221;, then both <b>dirname<\/b>() and <b>basename<\/b>() return the string &#8220;\/&#8221;. 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 &#8220;.&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Concatenating the string returned by <b>dirname<\/b>(), a &#8220;\/&#8221;, and the string returned by <b>basename<\/b>() yields a complete pathname.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<p style=\"margin-left:11%; margin-top: 1em\">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>\n<p style=\"margin-left:11%; margin-top: 1em\">The following list of examples (taken from SUSv2) shows the strings returned by <b>dirname<\/b>() and <b>basename<\/b>() for different paths:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"15%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1579731.png\" alt=\"Image grohtml-1579731.png\"><\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Both <b>dirname<\/b>() and <b>basename<\/b>() return pointers to null-terminated strings. (Do not pass these pointers to <b>free<\/b>(3).)<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1579732.png\" alt=\"Image grohtml-1579732.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">POSIX.1-2001, POSIX.1-2008.<\/p>\n<h2>NOTES <a name=\"NOTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">There are two different versions of <b>basename<\/b>() &#8211; the POSIX version described above, and the GNU version, which one gets after<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\"><b>#define _GNU_SOURCE<\/b> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var See feature_test_macros(7) bodies\/ usr\/ <b><br \/> #include <string.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;\/&#8221;. There is no GNU version of <b>dirname<\/b>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">With glibc, one gets the POSIX version of <b>basename<\/b>() when <i><libgen.h><\/i> is included, and the GNU version otherwise.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;\/usr\/&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Before glibc 2.2.1, the glibc version of <b>dirname<\/b>() did not correctly handle pathnames with trailing &#8216;\/&#8217; characters, and generated a segfault if given a NULL argument.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The following code snippet demonstrates the use of <b>basename<\/b>() and <b>dirname<\/b>():<\/p>\n<p style=\"margin-left:17%;\">char *dirc, *basec, *bname, *dname; <br \/> char *path = &#8220;\/etc\/passwd&#8221;;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">dirc = strdup(path); <br \/> basec = strdup(path); <br \/> dname = dirname(dirc); <br \/> bname = basename(basec); <br \/> printf(&#8220;dirname=%s, basename=%sn&#8221;, dname, bname);<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>basename<\/b>(1), <b>dirname<\/b>(1)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 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 https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  basename, dirname \u2212 parse pathname components <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,1749,3007],"class_list":["post-6889","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-basename","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6889"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6889\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}