version 1.9, 2009/11/24 13:34:20 |
version 1.9.22.1, 2014/08/10 06:51:50 |
Line 35 __RCSID("$NetBSD$"); |
|
Line 35 __RCSID("$NetBSD$"); |
|
#endif /* !LIBC_SCCS && !lint */ |
#endif /* !LIBC_SCCS && !lint */ |
|
|
#include "namespace.h" |
#include "namespace.h" |
|
#include <sys/param.h> |
#include <libgen.h> |
#include <libgen.h> |
|
#include <string.h> |
#include <limits.h> |
#include <limits.h> |
#include <string.h> |
#include <string.h> |
|
|
Line 43 __RCSID("$NetBSD$"); |
|
Line 45 __RCSID("$NetBSD$"); |
|
__weak_alias(basename,_basename) |
__weak_alias(basename,_basename) |
#endif |
#endif |
|
|
#if !HAVE_BASENAME |
static size_t |
char * |
xbasename_r(const char *path, char *buf, size_t buflen) |
basename(char *path) |
|
{ |
{ |
static char result[PATH_MAX]; |
const char *startp, *endp; |
const char *p, *lastp; |
|
size_t len; |
size_t len; |
|
|
/* |
/* |
* If `path' is a null pointer or points to an empty string, |
* If `path' is a null pointer or points to an empty string, |
* return a pointer to the string ".". |
* return a pointer to the string ".". |
*/ |
*/ |
if ((path == NULL) || (*path == '\0')) { |
if (path == NULL || *path == '\0') { |
result[0] = '.'; |
startp = "."; |
result[1] = '\0'; |
len = 1; |
|
goto out; |
return (result); |
|
} |
} |
|
|
/* Strip trailing slashes, if any. */ |
/* Strip trailing slashes, if any. */ |
lastp = path + strlen(path) - 1; |
endp = path + strlen(path) - 1; |
while (lastp != path && *lastp == '/') |
while (endp != path && *endp == '/') |
lastp--; |
endp--; |
|
|
|
/* Only slashes -> "/" */ |
|
if (endp == path && *endp == '/') { |
|
startp = "/"; |
|
len = 1; |
|
goto out; |
|
} |
|
|
/* Now find the beginning of this (final) component. */ |
/* Now find the beginning of this (final) component. */ |
p = lastp; |
for (startp = endp; startp > path && *(startp - 1) != '/'; startp--) |
while (p != path && *(p - 1) != '/') |
continue; |
p--; |
|
|
|
/* ...and copy the result into the result buffer. */ |
/* ...and copy the result into the result buffer. */ |
len = (lastp - p) + 1 /* last char */; |
len = (endp - startp) + 1 /* last char */; |
if (len > (PATH_MAX - 1)) |
out: |
len = PATH_MAX - 1; |
if (buf != NULL && buflen != 0) { |
|
buflen = MIN(len, buflen - 1); |
|
memcpy(buf, startp, buflen); |
|
buf[buflen] = '\0'; |
|
} |
|
return len; |
|
} |
|
|
|
#if !HAVE_BASENAME |
|
|
memcpy(result, p, len); |
char * |
result[len] = '\0'; |
basename(char *path) { |
|
static char result[PATH_MAX]; |
|
|
return (result); |
(void)xbasename_r(path, result, sizeof(result)); |
|
return result; |
} |
} |
|
|
#endif |
#endif |