iliaa Sun Feb 9 15:35:54 2003 EDT Modified files: /php4/ext/standard file.c Log: Added feature request #17983 (optional parameter to mkdir() that makes directory creation recursive). Fixed prototype for file() function. Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.297 php4/ext/standard/file.c:1.298 --- php4/ext/standard/file.c:1.297 Sun Feb 9 15:05:13 2003 +++ php4/ext/standard/file.c Sun Feb 9 15:35:54 2003 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.297 2003/02/09 20:05:13 iliaa Exp $ */ +/* $Id: file.c,v 1.298 2003/02/09 20:35:54 iliaa Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -444,7 +444,7 @@ } /* }}} */ -/* {{{ proto array file(string filename [, bool use_include_path]) +/* {{{ proto array file(string filename [, bool use_include_path [, bool +include_new_line [, bool skip_blank_lines]]]) Read entire file into an array */ #define PHP_FILE_BUF_SIZE 80 @@ -1733,32 +1733,86 @@ } /* }}} */ -/* {{{ proto bool mkdir(string pathname[, int mode]) + +/* {{{ proto int mkdir(char *dir int mode) +*/ + +PHPAPI int php_mkdir(char *dir, long mode TSRMLS_DC) +{ + int ret; + + if (PG(safe_mode) && (!php_checkuid(dir, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + return -1; + } + + if (php_check_open_basedir(dir TSRMLS_CC)) { + return -1; + } + + if ((ret = VCWD_MKDIR(dir, (mode_t)mode)) < 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, strerror(errno)); + } + + return ret; +} +/* }}} */ + +/* {{{ proto bool mkdir(string pathname [, int mode [, bool recursive]) Create a directory */ PHP_FUNCTION(mkdir) { int dir_len, ret; long mode = 0777; char *dir; + zend_bool recursive = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &dir, &dir_len, &mode) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &dir, &dir_len, +&mode, &recursive) == FAILURE) { return; } - if (PG(safe_mode) && (!php_checkuid(dir, NULL, CHECKUID_CHECK_FILE_AND_DIR))) { - RETURN_FALSE; - } - - if (php_check_open_basedir(dir TSRMLS_CC)) { - RETURN_FALSE; + if (!recursive) { + ret = php_mkdir(dir, mode); + } else { + /* we look for directory separator from the end of string, thus +hopefuly reducing our work load */ + char *p, *e, *buf; + struct stat sb; + + buf = estrndup(dir, dir_len); + e = buf + dir_len; + + /* find a top level directory we need to create */ + while ((p = strrchr(buf, DEFAULT_SLASH))) { + *p = '\0'; + if (VCWD_STAT(buf, &sb) == 0) { + *p = DEFAULT_SLASH; + break; + } + } + if (p == buf) { + ret = php_mkdir(dir, mode); + } else if (!(ret = php_mkdir(buf, mode))) { + if (!p) { + p = buf; + } + /* create any needed directories if the creation of the 1st +directory worked */ + while (++p != e) { + if (*p == '\0' && *(p + 1) != '\0') { + *p = DEFAULT_SLASH; + if ((ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) +{ + php_error_docref(NULL TSRMLS_CC, +E_WARNING, strerror(errno)); + break; + } + } + } + } + efree(buf); } - ret = VCWD_MKDIR(dir, (mode_t)mode); if (ret < 0) { - php_error_docref1(NULL TSRMLS_CC, dir, E_WARNING, "%s", strerror(errno)); RETURN_FALSE; + } else { + RETURN_TRUE; } - RETURN_TRUE; } /* }}} */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php