BASENAME(3POSIX)

🫢 BASENAME(3POSIX) β€” the POSIX girlies' guide 🫢

okay so bestie, we're reading basename(3POSIX) from the POSIX Programmer's Manual today, and honestly? she's a short one but she EATS. get comfy, we're doing path surgery. πŸ’…


🌸 PROLOG 🌸

so real talk before we start: this manual page is part of the POSIX Programmer's Manual ✨. But β€” and I cannot stress this enough β€” the Linux implementation of this interface may differ (bestie, consult the corresponding Linux manual page for details on Linux behavior), or the interface may literally not be implemented on Linux at all. 😭 no cap.


✨ NAME ✨

basename β€” return the last component of a pathname. that's it. that's her whole job. main character behavior fr fr. πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

#include <libgen.h>

char *basename(char *path);

yes she's a char * function, she takes one char *path, and she lives in <libgen.h>. remember that header or she will not find you. πŸ¦‹


πŸ’œ DESCRIPTION πŸ’œ

okay so here's the tea. basename() takes the pathname that path points to and hands you back a pointer to the final component of that pathname β€” and along the way she deletes any trailing '/' characters. bye slash, you're not the moment. ✨

the edge cases though, because she is a woman of rules:

  • if the string pointed to by path consists entirely of the '/' character, basename() returns a pointer to the string "/".
  • if the string pointed to by path is exactly "//", it is implementation-defined whether '/' or "//" is returned. (iykyk. two slashes is a whole personality crisis and nobody agrees on the answer. πŸ™ˆ)
  • if path is a null pointer or points to an empty string, basename() returns a pointer to the string ".".
⚠️ warning bestie, listen up: the basename() function may modify the string pointed to by path, and she may return a pointer to internal storage. that returned pointer might be invalidated, or the storage might be overwritten, by a subsequent call to basename(). and the returned pointer might also be invalidated if the calling thread is terminated. so like… don't hold onto that pointer like it's your emotional support string. she's temporary. πŸ’€

and one more: the basename() function need not be thread-safe. not her problem, not her era. 🌈


⭐ RETURN VALUE ⭐

basename() returns a pointer to the final component of path. that's the whole thing. queen of knowing what she wants. πŸ’…


πŸ¦‹ ERRORS πŸ¦‹

No errors are defined. none. zero. she's not here to give you problems. ✨

The following sections are informative.


πŸ“ EXAMPLES πŸ“

πŸ’– Using basename() πŸ’–

this little 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);
...

she ate that. πŸ”₯

πŸ’– Sample Input and Output Strings for the basename() and dirname() Functions and the basename and dirname Utilities πŸ’–

okay this is the chart bestie, the full lineup, every single row. columns are: the path argument for the basename() and dirname() functions, the string returned by basename(), the string returned by dirname(), the string operand for the basename and dirname utilities, the output written by the basename utility, and the output written by the dirname utility. 🌈

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

double slashes everywhere and she's still unbothered. that's the vibe check passing. πŸ’…


🌸 APPLICATION USAGE 🌸

None. nothing to add. she said what she said. ✨


🌸 RATIONALE 🌸

None. the reasoning is simply that she's right. πŸ’œ


🌸 FUTURE DIRECTIONS 🌸

None. the future? she's already in it bestie. πŸ¦‹


πŸ‘‘ SEE ALSO πŸ‘‘

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

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 bestie πŸŽ€ β€” brought to you by IEEE/The Open Group, 2017, filed under BASENAME(3POSIX). go delete a trailing slash for me, you're gonna slay. πŸ«ΆπŸ’…βœ¨