Hi, I don't have access to a Linux machine at the moment so I tested it out only on Win32/XP.
Could somebody please quickly check and commit it...? Addition: ftruncate() can take a string parameter as well... I have added the safe_mode check (actually borrowed from unlink). That {{{ proto thing needs to docs needs to be updated as well. Maybe 'mixed' instead of 'resource'? Thanks! Faisal
--- tests\file.c Sun May 12 21:08:32 2002 +++ file.c Fri May 17 19:17:58 2002 @@ -1752,27 +1752,61 @@ Truncate file to 'size' length */ PHP_NAMED_FUNCTION(php_if_ftruncate) { - zval **fp , **size; + zval **file , **size; short int ret; - int type; + int type , fd; void *what; + char* filename; - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &fp, &size) == FAILURE) { + if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &file, &size) == +FAILURE) { WRONG_PARAM_COUNT; } - /* XXX: add stream support --Wez. */ - what = zend_fetch_resource(fp TSRMLS_CC,-1, "File-Handle", &type, 3, le_fopen, le_popen, le_socket); - ZEND_VERIFY_RESOURCE(what); + convert_to_long_ex(size); + + if ( Z_TYPE_PP(file) == IS_STRING ) + { + convert_to_string_ex(file); + filename = Z_STRVAL_PP(file); + + /* Safe-mode check, borrowed from ulink */ + if (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(file), NULL, +CHECKUID_CHECK_FILE_AND_DIR)) { + RETURN_FALSE; + } - if (type == le_socket) { - php_error(E_WARNING, "can't truncate sockets!"); - RETURN_FALSE; + if ( Z_STRLEN_PP(file) == 0 ) + { + php_error(E_WARNING, "ftruncate() requires non-empty string or +a valid file-handle"); + RETURN_FALSE; + } + + /* Borrowed from the copy routine */ +#ifdef PHP_WIN32 + if ((fd=VCWD_OPEN_MODE(filename, _O_WRONLY|_O_CREAT|_O_BINARY, +_S_IWRITE))==-1) { +#else + if ((fd=VCWD_OPEN(filename, O_WRONLY))==-1) { +#endif + php_error(E_WARNING, "Unable to open '%s': %s", filename, +strerror(errno)); + close(fd); + RETURN_FALSE; + } + + ftruncate ( fd , Z_LVAL_PP(size) ); + close ( fd ); } + else + { + /* XXX: add stream support --Wez. */ + what = zend_fetch_resource(file TSRMLS_CC,-1, "File-Handle", &type, 3, +le_fopen, le_popen, le_socket); + ZEND_VERIFY_RESOURCE(what); - convert_to_long_ex(size); + if (type == le_socket) { + php_error(E_WARNING, "can't truncate sockets!"); + RETURN_FALSE; + } - ret = ftruncate(fileno((FILE *) what), Z_LVAL_PP(size)); + ret = ftruncate(fileno((FILE *) what), Z_LVAL_PP(size)); + } RETURN_LONG(ret + 1); } /* }}} */
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php