diff --git a/include/libgen.h b/include/libgen.h index cd27fd1..71f06eb 100644 --- a/include/libgen.h +++ b/include/libgen.h @@ -2,6 +2,7 @@ #define __LIBGEN_H char *basename (char *path); +char *posix_basename(char *path); char *dirname (char *path); #endif /* __LIBGEN_H */ diff --git a/lib/libgen.c b/lib/libgen.c index 1e43cf3..08ef352 100644 --- a/lib/libgen.c +++ b/lib/libgen.c @@ -37,6 +37,29 @@ } EXPORT_SYMBOL(basename); +/* + * There are two different versions of basename(): The GNU version implemented + * above and the POSIX version. 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 "/". + */ +char *posix_basename(char *path) +{ + char *fname; + + fname = path + strlen(path) - 1; + + while (*fname == '/') { + if (fname == path) + return path; + *fname = '\0'; + fname--; + } + + return basename(path); +} +EXPORT_SYMBOL(posix_basename); + char *dirname (char *path) { char *fname;