jani Mon Nov 5 14:06:53 2007 UTC Modified files: (Branch: PHP_5_3) /php-src/ext/standard exec.c exec.h Log: MFH: sync http://cvs.php.net/viewvc.cgi/php-src/ext/standard/exec.c?r1=1.113.2.3.2.1.2.1&r2=1.113.2.3.2.1.2.2&diff_format=u Index: php-src/ext/standard/exec.c diff -u php-src/ext/standard/exec.c:1.113.2.3.2.1.2.1 php-src/ext/standard/exec.c:1.113.2.3.2.1.2.2 --- php-src/ext/standard/exec.c:1.113.2.3.2.1.2.1 Sun Sep 30 05:49:44 2007 +++ php-src/ext/standard/exec.c Mon Nov 5 14:06:53 2007 @@ -16,7 +16,7 @@ | Ilia Alshanetsky <[EMAIL PROTECTED]> | +----------------------------------------------------------------------+ */ -/* $Id: exec.c,v 1.113.2.3.2.1.2.1 2007/09/30 05:49:44 jani Exp $ */ +/* $Id: exec.c,v 1.113.2.3.2.1.2.2 2007/11/05 14:06:53 jani Exp $ */ #include <stdio.h> #include "php.h" @@ -57,7 +57,7 @@ * If type==3, output will be printed binary, no lines will be saved or returned (passthru) * */ -int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC) +PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC) { FILE *fp; char *buf, *tmp=NULL; @@ -111,7 +111,7 @@ if (type != 3) { b = buf; - + while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) { /* no new line found, let's read some more */ if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) { @@ -132,7 +132,7 @@ PHPWRITE(buf, bufl); sapi_flush(TSRMLS_C); } else if (type == 2) { - /* strip trailing whitespaces */ + /* strip trailing whitespaces */ l = bufl; while (l-- && isspace(((unsigned char *)buf)[l])); if (l != (int)(bufl - 1)) { @@ -144,7 +144,7 @@ b = buf; } if (bufl) { - /* strip trailing whitespaces if we have not done so already */ + /* strip trailing whitespaces if we have not done so already */ if (type != 2) { l = bufl; while (l-- && isspace(((unsigned char *)buf)[l])); @@ -157,7 +157,7 @@ /* Return last line from the shell command */ if (PG(magic_quotes_runtime)) { int len; - + tmp = php_addslashes(buf, bufl, &len, 0 TSRMLS_CC); RETVAL_STRINGL(tmp, len, 0); } else { @@ -172,7 +172,7 @@ } } - pclose_return = php_stream_close(stream); + pclose_return = php_stream_close(stream); efree(buf); done: @@ -191,7 +191,7 @@ } /* }}} */ -static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) +static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */ { char *cmd; int cmd_len; @@ -226,6 +226,7 @@ ZVAL_LONG(ret_code, ret); } } +/* }}} */ /* {{{ proto string exec(string command [, array &output [, int &return_value]]) Execute an external program */ @@ -233,7 +234,6 @@ { php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); } - /* }}} */ /* {{{ proto int system(string command [, int &return_value]) @@ -261,14 +261,15 @@ *NOT* safe for binary strings */ -char *php_escape_shell_cmd(char *str) { - register int x, y, l; +PHPAPI char *php_escape_shell_cmd(char *str) +{ + register int x, y, l = strlen(str); char *cmd; char *p = NULL; + size_t estimate = (2 * l) + 1; - l = strlen(str); cmd = safe_emalloc(2, l, 1); - + for (x = 0, y = 0; x < l; x++) { switch (str[x]) { case '"': @@ -319,21 +320,27 @@ } } cmd[y] = '\0'; + + if ((estimate - y) > 4096) { + /* realloc if the estimate was way overill + * Arbitrary cutoff point of 4096 */ + cmd = erealloc(cmd, y + 1); + } + return cmd; } /* }}} */ /* {{{ php_escape_shell_arg */ -char *php_escape_shell_arg(char *str) { - int x, y, l; +PHPAPI char *php_escape_shell_arg(char *str) +{ + int x, y = 0, l = strlen(str); char *cmd; + size_t estimate = (4 * l) + 3; - y = 0; - l = strlen(str); - cmd = safe_emalloc(4, l, 3); /* worst case */ - + #ifdef PHP_WIN32 cmd[y++] = '"'; #else @@ -364,6 +371,12 @@ cmd[y++] = '\''; #endif cmd[y] = '\0'; + + if ((estimate - y) > 4096) { + /* realloc if the estimate was way overill + * Arbitrary cutoff point of 4096 */ + cmd = erealloc(cmd, y + 1); + } return cmd; } /* }}} */ @@ -372,18 +385,19 @@ Escape shell metacharacters */ PHP_FUNCTION(escapeshellcmd) { - zval **arg1; + char *command; + int command_len; char *cmd = NULL; - if (zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) { + return; } - - convert_to_string_ex(arg1); - if (Z_STRLEN_PP(arg1)) { - cmd = php_escape_shell_cmd(Z_STRVAL_PP(arg1)); - RETVAL_STRING(cmd, 1); - efree(cmd); + + if (command_len) { + cmd = php_escape_shell_cmd(command); + RETVAL_STRING(cmd, 0); + } else { + RETVAL_EMPTY_STRING(); } } /* }}} */ @@ -392,18 +406,17 @@ Quote and escape an argument for use in a shell command */ PHP_FUNCTION(escapeshellarg) { - zval **arg1; + char *argument; + int argument_len; char *cmd = NULL; - if (zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &argument, &argument_len) == FAILURE) { + return; } - - convert_to_string_ex(arg1); - if (Z_STRLEN_PP(arg1)) { - cmd = php_escape_shell_arg(Z_STRVAL_PP(arg1)); - RETVAL_STRING(cmd, 1); - efree(cmd); + + if (argument) { + cmd = php_escape_shell_arg(argument); + RETVAL_STRING(cmd, 0); } } /* }}} */ @@ -414,37 +427,35 @@ { FILE *in; size_t total_readbytes; - zval **cmd; + char *command; + int command_len; char *ret; php_stream *stream; - if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) { + return; } - + if (PG(safe_mode)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode"); RETURN_FALSE; } - convert_to_string_ex(cmd); #ifdef PHP_WIN32 - if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "rt"))==NULL) { + if ((in=VCWD_POPEN(command, "rt"))==NULL) { #else - if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "r"))==NULL) { + if ((in=VCWD_POPEN(command, "r"))==NULL) { #endif - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", Z_STRVAL_PP(cmd)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", command); RETURN_FALSE; } stream = php_stream_fopen_from_pipe(in, "rb"); total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0); - php_stream_close(stream); - + php_stream_close(stream); + if (total_readbytes > 0) { - RETURN_STRINGL(ret, total_readbytes, 0); - } else { - RETURN_NULL(); + RETVAL_STRINGL(ret, total_readbytes, 0); } } /* }}} */ @@ -466,7 +477,7 @@ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only a super user may attempt to increase the priority of a process"); RETURN_FALSE; } - + RETURN_TRUE; } /* }}} */ http://cvs.php.net/viewvc.cgi/php-src/ext/standard/exec.h?r1=1.24.2.3.2.1&r2=1.24.2.3.2.1.2.1&diff_format=u Index: php-src/ext/standard/exec.h diff -u php-src/ext/standard/exec.h:1.24.2.3.2.1 php-src/ext/standard/exec.h:1.24.2.3.2.1.2.1 --- php-src/ext/standard/exec.h:1.24.2.3.2.1 Mon Jan 1 09:36:08 2007 +++ php-src/ext/standard/exec.h Mon Nov 5 14:06:53 2007 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: exec.h,v 1.24.2.3.2.1 2007/01/01 09:36:08 sebastian Exp $ */ +/* $Id: exec.h,v 1.24.2.3.2.1.2.1 2007/11/05 14:06:53 jani Exp $ */ #ifndef EXEC_H #define EXEC_H @@ -36,6 +36,6 @@ PHPAPI char *php_escape_shell_cmd(char *); PHPAPI char *php_escape_shell_arg(char *); -int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC); +PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC); #endif /* EXEC_H */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php