@@ -6,33 +6,49 @@
*/
#include <libgen.h>
-
-char *__xpg_basename(register char *path)
+#include <string.h>
+char *__xpg_basename (char *path)
{
- static const char null_or_empty[] = ".";
- char *first;
- register char *last;
-
- first = (char *) null_or_empty;
+ char *p;
- if (path && *path) {
- first = path;
- last = path - 1;
+ if (path == NULL || path[0] == '\0')
+ /* We return a pointer to a static string containing ".". */
+ p = (char *) ".";
+ else {
+ p = strrchr (path, '/');
- do {
- if ((*path != '/') && (path > ++last)) {
- last = first = path;
- }
- } while (*++path);
+ if (p == NULL)
+ /* There is no slash in the filename. Return the whole string. */
+ p = path;
+ else {
+ if (p[1] == '\0') {
+ /* We must remove trailing '/'. */
+ while (p > path && p[-1] == '/')
+ --p;
- if (*first == '/') {
- last = first;
+ /* Now we can be in two situations:
+ a) the string only contains '/' characters, so we return
+ '/'
+ b) p points past the last component, but we have to remove
+ the trailing slash. */
+ if (p > path) {
+ *p-- = '\0';
+ while (p > path && p[-1] != '/')
+ --p;
+ } else
+ /* The last slash we already found is the right position
+ to return. */
+ while (p[1] != '\0')
+ ++p;
+ } else
+ /* Go to the first character of the name. */
+ ++p;
}
- last[1] = 0;
}
- return first;
+ return p;
}
+
#ifndef __USE_GNU
# undef basename
weak_alias(__xpg_basename,basename)