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
pathconsists entirely of the'/'character,basename()returns a pointer to the string"/". - if the string pointed to by
pathis exactly"//", it is implementation-defined whether'/'or"//"is returned. (iykyk. two slashes is a whole personality crisis and nobody agrees on the answer. π) - if
pathis a null pointer or points to an empty string,basename()returns a pointer to the string".".
β οΈ warning bestie, listen up: thebasename()function may modify the string pointed to bypath, 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 tobasename(). 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 operandusrβbasenamewritesusr,dirnamewrites."usr/"βbasename()returns"usr",dirname()returns"."; utility operandusr/βbasenamewritesusr,dirnamewrites.""βbasename()returns".",dirname()returns"."; utility operand""βbasenamewrites.or empty string,dirnamewrites."/"βbasename()returns"/",dirname()returns"/"; utility operand/βbasenamewrites/,dirnamewrites/"//"βbasename()returns"/"or"//",dirname()returns"/"or"//"; utility operand//βbasenamewrites/or//,dirnamewrites/or//"///"βbasename()returns"/",dirname()returns"/"; utility operand///βbasenamewrites/,dirnamewrites/"/usr/"βbasename()returns"usr",dirname()returns"/"; utility operand/usr/βbasenamewritesusr,dirnamewrites/"/usr/lib"βbasename()returns"lib",dirname()returns"/usr"; utility operand/usr/libβbasenamewriteslib,dirnamewrites/usr"//usr//lib//"βbasename()returns"lib",dirname()returns"//usr"; utility operand//usr//lib//βbasenamewriteslib,dirnamewrites//usr"/home//dwc//test"βbasename()returns"test",dirname()returns"/home//dwc"; utility operand/home//dwc//testβbasenamewritestest,dirnamewrites/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
π COPYRIGHT π
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. π«Άπ
β¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms