mirror of
https://github.com/php/php-src.git
synced 2025-01-15 16:24:37 +08:00
Cleanup and also include the error path into test
This commit is contained in:
parent
605fbabfba
commit
11613a1b58
@ -1653,8 +1653,8 @@ PHPAPI size_t php_dirname(char *path, size_t len)
|
||||
PHP_FUNCTION(dirname)
|
||||
{
|
||||
char *str;
|
||||
zend_string *ret;
|
||||
size_t str_len;
|
||||
zend_string *ret;
|
||||
zend_long levels = 1;
|
||||
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str, &str_len, &levels) == FAILURE) {
|
||||
@ -1666,18 +1666,16 @@ PHP_FUNCTION(dirname)
|
||||
if (levels == 1) {
|
||||
/* Defaut case */
|
||||
ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len);
|
||||
|
||||
} else if (levels < 1) {
|
||||
php_error_docref(NULL, E_WARNING, "Invalid argument, levels must be >= 1");
|
||||
return;
|
||||
|
||||
|
||||
} else {
|
||||
/* Some levels up */
|
||||
do {
|
||||
ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len=ZSTR_LEN(ret));
|
||||
} while (ZSTR_LEN(ret)<str_len && --levels>0);
|
||||
ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len = ZSTR_LEN(ret));
|
||||
} while (ZSTR_LEN(ret) < str_len && --levels);
|
||||
}
|
||||
|
||||
RETURN_NEW_STR(ret);
|
||||
}
|
||||
/* }}} */
|
||||
|
@ -5,13 +5,15 @@ Test dirname() function : usage variations
|
||||
/* Prototype: string dirname ( string $path [, int nb]);
|
||||
Description: Returns directory name component of path.
|
||||
*/
|
||||
for ($i=1 ; $i<5 ; $i++) {
|
||||
for ($i=0 ; $i<5 ; $i++) {
|
||||
var_dump(dirname("/foo/bar/baz", $i));
|
||||
}
|
||||
var_dump(dirname("/foo/bar/baz", PHP_INT_MAX));
|
||||
?>
|
||||
Done
|
||||
--EXPECT--
|
||||
--EXPECTF--
|
||||
Warning: dirname(): Invalid argument, levels must be >= 1 in %sdirname_multi.php on line %d
|
||||
NULL
|
||||
string(8) "/foo/bar"
|
||||
string(4) "/foo"
|
||||
string(1) "/"
|
||||
|
Loading…
Reference in New Issue
Block a user