BASENAME(3P)

βœ¨πŸ’– BASENAME(3P) πŸ’–βœ¨

welcome to the POSIX Programmer's Manual, bestie β€” this is the page for basename, straight out of the POSIX programmer's manual gang. come get your last path component, it's cute, it's free, it's giving "I know where I live" energy. πŸ¦‹


🌸 PROLOG 🌸

okay so real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like… consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. no cap, check before you ship it. πŸ’…


πŸ‘‘ NAME πŸ‘‘

basename β€” return the last component of a pathname. that's it. that's the whole job. and she ate. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <libgen.h>

char *basename(char *path);

πŸ’œ DESCRIPTION πŸ’œ

so the basename() function shall take the pathname pointed to by path and return a pointer to the final component of the pathname, deleting any trailing '/' characters. like it's doing a little haircut on your slashes. βœ‚οΈ

If the string pointed to by path consists entirely of the '/' character, basename() shall return a pointer to the string "/". And if the string pointed to by path is exactly "//", it is implementation-defined whether '/' or "//" is returned β€” so like, don't build your whole personality on that one, it depends on the machine. πŸ™ˆ

If path is a null pointer or points to an empty string, basename() shall return a pointer to the string ".".

⚠️ bestie, listen: the basename() function may modify the string pointed to by path, and may return a pointer to internal storage. The returned pointer might be invalidated or the storage might be overwritten by a subsequent call to basename(). The returned pointer might also be invalidated if the calling thread is terminated. so please, for the love of everything, do not keep that pointer around like a souvenir. 😭

The basename() function need not be thread-safe. ngl that's just how she is.


πŸ’… RETURN VALUE πŸ’…

The basename() function shall return a pointer to the final component of path. slay, that's the whole return value, nothing extra. 🫢


πŸ“ ERRORS πŸ“

No errors are defined. iconic, no drama, no errno nonsense. ✨

The following sections are informative. (iykyk β€” that means they're the vibe-and-context sections, not the normy requirements ones. πŸ“š)


🌈 EXAMPLES 🌈

🌸 Using basename() 🌸

The following program fragment returns a pointer to the value lib, which is the base name of /usr/lib.

#include <libgen.h>
...
char name[] = "/usr/lib";
char *base;

base = basename(name);
...

see how she just… finds the lib at the end? main character energy fr fr. πŸ’–

πŸŽ€ Sample Input and Output Strings for the basename() and dirname() Functions and the basename and dirname Utilities πŸŽ€

okay so this is the big glow-up chart. the columns in the original are, in order: basename() and dirname() Functions path Argument, String Returned by basename(), String Returned by dirname(), basename and dirname Utilities string Operand, Output Written by basename Utility, and Output Written by dirname Utility. here's every single row, no skipping: πŸ’…

  • path "usr" β€” basename() returns "usr", dirname() returns "."; utility operand usr β†’ basename utility writes usr, dirname utility writes .
  • path "usr/" β€” basename() returns "usr", dirname() returns "."; utility operand usr/ β†’ basename utility writes usr, dirname utility writes .
  • path "" β€” basename() returns ".", dirname() returns "."; utility operand "" β†’ basename utility writes . or empty string, dirname utility writes .
  • path "/" β€” basename() returns "/", dirname() returns "/"; utility operand / β†’ basename utility writes /, dirname utility writes /
  • path "//" β€” basename() returns "/" or "//", dirname() returns "/" or "//"; utility operand // β†’ basename utility writes / or //, dirname utility writes / or //
  • path "///" β€” basename() returns "/", dirname() returns "/"; utility operand /// β†’ basename utility writes /, dirname utility writes /
  • path "/usr/" β€” basename() returns "usr", dirname() returns "/"; utility operand /usr/ β†’ basename utility writes usr, dirname utility writes /
  • path "/usr/lib" β€” basename() returns "lib", dirname() returns "/usr"; utility operand /usr/lib β†’ basename utility writes lib, dirname utility writes /usr
  • path "//usr//lib//" β€” basename() returns "lib", dirname() returns "//usr"; utility operand //usr//lib// β†’ basename utility writes lib, dirname utility writes //usr
  • path "/home//dwc//test" β€” basename() returns "test", dirname() returns "/home//dwc"; utility operand /home//dwc//test β†’ basename utility writes test, dirname utility writes /home//dwc

every row accounted for, every slash where it belongs. obsessed. 🌈


🧚 APPLICATION USAGE 🧚

None. short queen. nothing to add. 🫢


⭐ RATIONALE ⭐

None. again, no notes, she's confident. πŸ’…


πŸ’« FUTURE DIRECTIONS πŸ’«

None. she's not going anywhere, she's booked and busy as-is. ✨


πŸ”₯ SEE ALSO πŸ”₯

  • dirname()
  • The Base Definitions volume of POSIX.1‐2017, <libgen.h>
  • The Shell and Utilities volume of POSIX.1‐2017, basename

go read your sister pages, bestie, they're literally right there. πŸŽ€


okay legal girlies, here's the fine print, and yes it all has to stay: Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


πŸ’– and that's the whole page! courtesy of IEEE/The Open Group, dated 2017, filed under BASENAME(3P). mwah. stay slaying, and remember: delete your trailing slashes, not your standards. πŸ’…βœ¨πŸ¦‹
πŸ“¦ where this page comes from