basename(3)

โœจ basename(3) โ€” Library Functions Manual โœจ

okay bestie, gather round, we're parsing pathname components today and it's honestly giving main character energy ๐Ÿ’… this one lives in section 3 of your man pages, and it's a whole saga. buckle up ๐ŸŽ€


๐ŸŒธ NAME ๐ŸŒธ

basename, dirname โ€” parse pathname components


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” yeah she's built in, no extra packages, we love a low-maintenance queen ๐Ÿซถ


๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

#include <libgen.h>

char *dirname(char *path);
char *basename(char *path);

like, that's it. two functions. that's the whole vibe check.


๐Ÿ’– DESCRIPTION ๐Ÿ’–

โš ๏ธ WARNING, bestie, pay attention: there are two different functions called basename(). yes, TWO. see below. this is not a drill.

The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. in the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. trailing '/' characters are not counted as part of the pathname โ€” they're just out here being decorative, iconic but irrelevant ๐Ÿ’…

If path does not contain a slash, dirname() returns the string "." while basename() returns a copy of path. If path is the string "/", then both dirname() and basename() return the string "/". And if path is a null pointer or points to an empty string, then both dirname() and basename() return the string ".". no cap, that's just how she is โœจ

Concatenating the string returned by dirname(), a "/", and the string returned by basename() yields a complete pathname. it's giving puzzle pieces that snap together ๐Ÿงฉ

Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions. yes literally, she will eat your string. protect your data ๐Ÿซถ

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 path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required. read that again. it matters. ๐Ÿ”ฅ

The following list of examples (taken from SUSv2) shows the strings returned by dirname() and basename() for different paths:

path       dirname   basename
/usr/lib   /usr      lib
/usr/      /         usr
usr        .         usr
/          /         /
.          .         .
..         .         ..

look at the /usr/ row. dirname is / and basename is usr. the trailing slash got absolutely ghosted ๐Ÿ’€


๐Ÿฆ‹ RETURN VALUE ๐Ÿฆ‹

Both dirname() and basename() return pointers to null-terminated strings.

๐Ÿšจ Do NOT pass these pointers to free(3). i'm serious. do not. that memory was never yours to free, bestie, that's a whole situation nobody wants ๐Ÿ˜ญ

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

For an explanation of the terms used in this section, see attributes(7).

  • Interface: basename(), dirname()
  • Attribute: Thread safety
  • Value: MT-Safe

MT-Safe!! she's thread-safe!! we stan a function that doesn't start drama across threads ๐ŸŒˆโœจ


๐Ÿ’œ VERSIONS ๐Ÿ’œ

There are two different versions of basename() โ€” the POSIX version described above, and the GNU version, which one gets after:

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <string.h>

The GNU version never modifies its argument, and returns the empty string when path has a trailing slash, and in particular also when it is "/". There is no GNU version of dirname(). dirname said "one of me is enough" and honestly? respect ๐ŸŽ€

With glibc, one gets the POSIX version of basename() when <libgen.h> is included, and the GNU version otherwise. so like... the header you pick decides which girl you get. no pressure tho ๐Ÿ™ˆ


โญ STANDARDS โญ

POSIX.1-2008.


๐Ÿ“ HISTORY ๐Ÿ“

POSIX.1-2001.

she's been around. vintage. classic. legacy icon ๐Ÿ’…


๐Ÿ’€ BUGS ๐Ÿ’€

In the glibc implementation, the POSIX versions of these functions modify the path argument, and segfault when called with a static string such as "/usr/".

๐Ÿšจ girl. SEGFAULT. with a static string. like a string literal. as in dirname("/usr/") will just straight up delete your whole program from existence. do not do this. i am begging ๐Ÿ™

Before glibc 2.2.1, the glibc version of dirname() did not correctly handle pathnames with trailing '/' characters, and generated a segfault if given a NULL argument. old glibc was going through it fr fr ๐Ÿ˜ญ (fixed now, but knowledge is power, bestie)


๐ŸŒธ EXAMPLES ๐ŸŒธ

The following code snippet demonstrates the use of basename() and dirname():

char *dirc, *basec, *bname, *dname;
char *path = "/etc/passwd";

dirc = strdup(path);
basec = strdup(path);
dname = dirname(dirc);
bname = basename(basec);
printf("dirname=%s, basename=%s\n", dname, bname);

see how she strdup()s the path twice before handing it over? that's the move. that's the whole move. protect the original, feed the copy to the function that eats strings ๐Ÿซถโœจ that's it, that's the entire tutorial, you're welcome.


๐ŸŽ€ SEE ALSO ๐ŸŽ€


and that's the page, bestie!! this was basename(3) from Linux man-pages 6.20, dated 2026-02-08 โ€” go forth, parse your paths, strdup() before you wreck yourself, and remember: there are two basename()s and only one of them is safe to free-adjacent. slay responsibly ๐Ÿ’–๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms