Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
basename(3)
Edit
PageHistory
Diff
Info
LikePages
DIRNAME !!!DIRNAME !!NAME dirname, basename - Parse pathname components !!SYNOPSIS __#include__ <string.h> __char *dirname(char__''*path'' __);__ __char *basename(char__ ''*path''__);__ !!DESCRIPTION __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. If ''path'' does not contain a slash, __dirname__ returns the string __basename__ returns a copy of ''path''. If ''path'' is the string ''dirname__ and __basename__ return the string __path'' is a NULL pointer or points to an empty string, then both __dirname__ and __basename__ return the string Concatenating the string returned by __dirname__, a __basename__ yields a complete pathname. Both __dirname__ and __basename__ may modify the contents of ''path'', so if you need to preserve the pathname string, copies should be passed to these functions. Furthermore, __dirname__ and __basename__ may return pointers to statically allocated memory which may overwritten by subsequent calls. 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" |"/"|"/"|"/" |"."|"."|"." |".."|"."|".." !!EXAMPLE 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); free(dirc); free(basec); !!RETURN VALUE Both __dirname__ and __basename__ return pointers to null-terminated strings. !!BUGS In versions of glibc up to and including 2.2.1, __dirname__ does not correctly handle pathnames with trailing '/' characters, and generates a segmentation violation if given a NULL argument. !!CONFORMING TO [SUSv2] !!SEE ALSO dirname(1), basename(1), ---- !PerryLorier: I after reading this man page you're probably as paranoid about this function as I am. Here is an efficient and reliable implementation: char *mybasename(char *x) { if (!x) return x; return strrchr(x) ? strrchr(x)+1 : x; } if this is listed as being static then the compiler should do a nice job of optimising this.
One page links to
basename(3)
:
Man3b
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.