Need a PHPAPI version of basename for some stuff I am working on.

Also fixed a bug along the way in the basename function.  If it
was fed something like "filename.ext/////" it would return the string
with all the slashes whereas if you fed it "/path/filename.ext////" it
would get it right.
@ Fixed basename() bug where "file.ext///" would not return the same
@ as "/path/file.ext///" (Rasmus)
This commit is contained in:
Rasmus Lerdorf 2000-05-23 20:16:14 +00:00
parent ee82f87cbf
commit 7182e722ca
2 changed files with 34 additions and 18 deletions

View File

@ -100,6 +100,7 @@ PHPAPI char *php_addslashes(char *str, int length, int *new_length, int freeit);
PHPAPI char *php_addcslashes(char *str, int length, int *new_length, int freeit, char *what, int wlength);
PHPAPI void php_stripslashes(char *str, int *len);
PHPAPI void php_stripcslashes(char *str, int *len);
PHPAPI void php_basename(char *str, int len);
PHPAPI void php_dirname(char *str, int len);
PHPAPI char *php_stristr(unsigned char *s, unsigned char *t, size_t s_len, size_t t_len);
PHPAPI char *php_str_to_str(char *haystack, int length, char *needle,

View File

@ -12,7 +12,7 @@
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Authors: Rasmus Lerdorf <rasmus@php.net> |
| Stig Sæther Bakken <ssb@fast.no> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
@ -473,35 +473,50 @@ PHP_FUNCTION(strtolower)
}
/* }}} */
/* {{{ proto string basename(string path)
Return the filename component of the path */
PHP_FUNCTION(basename)
PHPAPI char *php_basename(char *s, size_t len)
{
zval **str;
char *ret, *c;
if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &str)) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
ret = estrdup((*str)->value.str.val);
c = ret + (*str)->value.str.len -1;
char *ret=NULL, *c, *p=NULL, buf='\0';
c = s + len - 1;
/* strip trailing slashes */
while (*c == '/'
#ifdef PHP_WIN32
|| *c == '\\'
#endif
)
c--;
*(c + 1) = '\0';
if ((c = strrchr(ret, '/'))
if(c < s+len-1) {
buf = *(c + 1); /* Save overwritten char */
*(c + 1) = '\0'; /* overwrite char */
p = c + 1; /* Save pointer to overwritten char */
}
if ((c = strrchr(s, '/'))
#ifdef PHP_WIN32
|| (c = strrchr(ret, '\\'))
|| (c = strrchr(s, '\\'))
#endif
) {
RETVAL_STRING(c + 1,1);
ret = estrdup(c + 1);
} else {
RETVAL_STRING((*str)->value.str.val,1);
ret = estrdup(s);
}
if(buf) *p = buf;
return (ret);
}
/* {{{ proto string basename(string path)
Return the filename component of the path */
PHP_FUNCTION(basename)
{
zval **str;
char *ret;
if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &str)) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);
ret = php_basename((*str)->value.str.val,(*str)->value.str.len);
RETVAL_STRING(ret,1)
efree(ret);
}
/* }}} */