Skip to content

Commit

Permalink
Made dirname() binary-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moriyoshi Koizumi committed Apr 12, 2003
1 parent 9e46bdd commit fa0ac8a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ ext/standard
* NOT binary safe:
strtok()
basename()
dirname()

ext/wddx
--------
Expand Down
25 changes: 14 additions & 11 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,30 +1142,30 @@ PHP_FUNCTION(basename)

/* {{{ php_dirname
Returns directory name component of path */
PHPAPI void php_dirname(char *path, int len)
PHPAPI size_t php_dirname(char *path, size_t len)
{
register char *end = path + len - 1;

#ifdef PHP_WIN32
/* Note that on Win32 CWD is per drive (heritage from CP/M).
* This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
*/
if ((2 <= len) && isalpha(path[0]) && (':' == path[1])) {
if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
/* Skip over the drive spec (if any) so as not to change */
path += 2;
if (2 == len) {
/* Return "c:" on Win32 for dirname("c:").
* It would be more consistent to return "c:."
* but that would require making the string *longer*.
*/
return;
return len;
}
}
#endif

if (len <= 0) {
if (len == 0) {
/* Illegal use of this function */
return;
return 0;
}

/* Strip trailing slashes */
Expand All @@ -1176,7 +1176,7 @@ PHPAPI void php_dirname(char *path, int len)
/* The path only contained slashes */
path[0] = DEFAULT_SLASH;
path[1] = '\0';
return;
return 1;
}

/* Strip filename */
Expand All @@ -1187,7 +1187,7 @@ PHPAPI void php_dirname(char *path, int len)
/* No slash found, therefore return '.' */
path[0] = '.';
path[1] = '\0';
return;
return 1;
}

/* Strip slashes which came before the file name */
Expand All @@ -1197,9 +1197,11 @@ PHPAPI void php_dirname(char *path, int len)
if (end < path) {
path[0] = DEFAULT_SLASH;
path[1] = '\0';
return;
return 1;
}
*(end+1) = '\0';

return (size_t)(end + 1 - path);
}
/* }}} */

Expand All @@ -1209,16 +1211,17 @@ PHP_FUNCTION(dirname)
{
zval **str;
char *ret;

size_t ret_len;

if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(str);

ret = estrndup(Z_STRVAL_PP(str), Z_STRLEN_PP(str));
php_dirname(ret, Z_STRLEN_PP(str));
ret_len = php_dirname(ret, Z_STRLEN_PP(str));

RETURN_STRING(ret, 0);
RETURN_STRINGL(ret, ret_len, 0);
}
/* }}} */

Expand Down
Binary file modified tests/run-test/test007.phpt
Binary file not shown.

0 comments on commit fa0ac8a

Please sign in to comment.