cvs: php4 /ext/standard/ php_string.h string.c

[email protected] ("Rasmus Lerdorf")
Newsgroups php.version4
Message-ID <cvsrasmus959112974@cvsserver>
rasmus		Tue May 23 22:16:14 2000 EDT

  Modified files:
    /php4/ext/standard	php_string.h string.c 
  Log:
  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)
  
  
Index: php4/ext/standard/php_string.h
diff -u php4/ext/standard/php_string.h:1.17 php4/ext/standard/php_string.h:1.18
--- php4/ext/standard/php_string.h:1.17	Tue May 23 16:37:39 2000
+++ php4/ext/standard/php_string.h	Tue May 23 22:16:14 2000
@@ -29,7 +29,7 @@
  */
 
 
-/* $Id: php_string.h,v 1.17 2000/05/23 14:37:39 hholzgra Exp $ */
+/* $Id: php_string.h,v 1.18 2000/05/23 20:16:14 rasmus Exp $ */
 
 /* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
 
@@ -100,6 +100,7 @@
 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: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.114 php4/ext/standard/string.c:1.115
--- php4/ext/standard/string.c:1.114	Tue May 23 14:38:12 2000
+++ php4/ext/standard/string.c	Tue May 23 22:16:14 2000
@@ -12,13 +12,13 @@
    | obtain it through the world-wide-web, please send a note to          |
    | [email protected] so we can mail you a copy immediately.               |
    +----------------------------------------------------------------------+
-   | Authors: Rasmus Lerdorf <[email protected]>                       |
+   | Authors: Rasmus Lerdorf <[email protected]>                             |
    |          Stig Sæther Bakken <[email protected]>                            |
    |          Zeev Suraski <[email protected]>                                |
    +----------------------------------------------------------------------+
  */
 
-/* $Id: string.c,v 1.114 2000/05/23 12:38:12 andrei Exp $ */
+/* $Id: string.c,v 1.115 2000/05/23 20:16:14 rasmus Exp $ */
 
 /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
 
@@ -473,35 +473,50 @@
 }
 /* }}} */
 
-/* {{{ 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);
 }
 /* }}} */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.