]> granicus.if.org Git - php/commitdiff
Need a PHPAPI version of basename for some stuff I am working on.
authorRasmus Lerdorf <rasmus@php.net>
Tue, 23 May 2000 20:16:14 +0000 (20:16 +0000)
committerRasmus Lerdorf <rasmus@php.net>
Tue, 23 May 2000 20:16:14 +0000 (20:16 +0000)
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)

ext/standard/php_string.h
ext/standard/string.c

index 3c894f150482b5a478dbe1a259a6b14045d2b73c..14a3195fd21be859d979a43b70dd78989907614c 100644 (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,
index a43eff7cb6cefe2b365a18d1c14cefe710d10707..faabe4c702f5cf61a209636cd7ebb58860b3e853 100644 (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);
 }
 /* }}} */