php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch 0001-Fix-70112-RFE-Allow-dirname-to-go-up-various-time.patch for Unknown/Other Function Bug #70112

Patch version 2015-07-22 11:46 UTC

Return to Bug #70112 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: remi@php.net

From a51a8cb8391a1477c06d1553d79bc189dac46826 Mon Sep 17 00:00:00 2001
From: Remi Collet <fedora@famillecollet.com>
Date: Wed, 22 Jul 2015 13:45:28 +0200
Subject: [PATCH] Fix #70112 [RFE] Allow "dirname" to go up various time

---
 ext/standard/basic_functions.c                |  3 ++-
 ext/standard/string.c                         | 14 ++++++++++----
 ext/standard/tests/strings/dirname_error.phpt | 12 +++++++++---
 ext/standard/tests/strings/dirname_multi.phpt | 18 ++++++++++++++++++
 4 files changed, 39 insertions(+), 8 deletions(-)
 create mode 100644 ext/standard/tests/strings/dirname_multi.phpt

diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 488b71b..2c65aff 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -2213,8 +2213,9 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_basename, 0, 0, 1)
 	ZEND_ARG_INFO(0, suffix)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO(arginfo_dirname, 0)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_dirname, 0, 0, 1)
 	ZEND_ARG_INFO(0, path)
+	ZEND_ARG_INFO(0, nb)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_pathinfo, 0, 0, 1)
diff --git a/ext/standard/string.c b/ext/standard/string.c
index bb482ba..82a1e8e 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -1648,20 +1648,26 @@ PHPAPI size_t php_dirname(char *path, size_t len)
 }
 /* }}} */
 
-/* {{{ proto string dirname(string path)
+/* {{{ proto string dirname(string path[, int nb])
    Returns the directory name component of the path */
 PHP_FUNCTION(dirname)
 {
 	char *str;
 	zend_string *ret;
 	size_t str_len;
+	zend_long nb = 1;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &str_len) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str, &str_len, &nb) == FAILURE) {
+		return;
+	}
+	if (nb < 1) {
+		php_error_docref(NULL, E_WARNING, "Invalid argument, nb must be >= 1");
 		return;
 	}
-
 	ret = zend_string_init(str, str_len, 0);
-	ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len);
+	do {
+		ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), ZSTR_LEN(ret));
+	} while (ZSTR_LEN(ret) && --nb>0);
 
 	RETURN_NEW_STR(ret);
 }
diff --git a/ext/standard/tests/strings/dirname_error.phpt b/ext/standard/tests/strings/dirname_error.phpt
index bf63102..e5802dc 100644
--- a/ext/standard/tests/strings/dirname_error.phpt
+++ b/ext/standard/tests/strings/dirname_error.phpt
@@ -9,17 +9,23 @@ echo "*** Testing error conditions ***\n";
 // zero arguments 
 var_dump( dirname() );
 
+// Bad arg
+var_dump( dirname("/var/tmp/bar.gz", 0) );
+
 // more than expected no. of arguments
-var_dump( dirname("/var/tmp/bar.gz", ".gz") );
+var_dump( dirname("/var/tmp/bar.gz", 1, ".gz") );
 
 echo "Done\n";
 ?>
 --EXPECTF--
 *** Testing error conditions ***
 
-Warning: dirname() expects exactly 1 parameter, 0 given in %s on line %d
+Warning: dirname() expects at least 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: dirname(): Invalid argument, nb must be >= 1 in %s on line %d
 NULL
 
-Warning: dirname() expects exactly 1 parameter, 2 given in %s on line %d
+Warning: dirname() expects at most 2 parameters, 3 given in %s on line %d
 NULL
 Done
diff --git a/ext/standard/tests/strings/dirname_multi.phpt b/ext/standard/tests/strings/dirname_multi.phpt
new file mode 100644
index 0000000..113dd3c
--- /dev/null
+++ b/ext/standard/tests/strings/dirname_multi.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Test dirname() function : usage variations
+--FILE--
+<?php
+/* Prototype: string dirname ( string $path [, int nb]);
+   Description: Returns directory name component of path.
+*/
+for ($i=1 ; $i<5 ; $i++) {
+	var_dump(dirname("/foo/bar/baz", $i));
+}
+?>
+Done
+--EXPECT--
+string(8) "/foo/bar"
+string(4) "/foo"
+string(1) "/"
+string(1) "/"
+Done
-- 
2.1.0

 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Oct 04 07:04:13 2022 UTC