Skip to content

Commit aee0c18

Browse files
author
Ilia Alshanetsky
committed
Made shell_exec() use streams, this simplifies the code and in some cases
makes it a little faster too.
1 parent bce8bfd commit aee0c18

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

ext/standard/exec.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,10 @@ PHP_FUNCTION(escapeshellarg)
452452
PHP_FUNCTION(shell_exec)
453453
{
454454
FILE *in;
455-
int readbytes, total_readbytes=0, allocated_space;
455+
size_t total_readbytes;
456456
pval **cmd;
457457
char *ret;
458+
php_stream *stream;
458459

459460
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) {
460461
WRONG_PARAM_COUNT;
@@ -474,21 +475,16 @@ PHP_FUNCTION(shell_exec)
474475
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", Z_STRVAL_PP(cmd));
475476
RETURN_FALSE;
476477
}
477-
allocated_space = EXEC_INPUT_BUF;
478-
ret = (char *) emalloc(allocated_space);
479-
while (1) {
480-
readbytes = fread(ret+total_readbytes, 1, EXEC_INPUT_BUF, in);
481-
if (readbytes<=0) {
482-
break;
483-
}
484-
total_readbytes += readbytes;
485-
allocated_space = total_readbytes+EXEC_INPUT_BUF;
486-
ret = (char *) erealloc(ret, allocated_space);
487-
}
488-
pclose(in);
478+
479+
stream = php_stream_fopen_from_pipe(in, "rb");
480+
total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0);
481+
php_stream_close(stream);
489482

490-
RETVAL_STRINGL(ret, total_readbytes, 0);
491-
Z_STRVAL_P(return_value)[total_readbytes] = '\0';
483+
if (total_readbytes > 0) {
484+
RETURN_STRINGL(ret, total_readbytes, 0);
485+
} else {
486+
RETURN_NULL();
487+
}
492488
}
493489
/* }}} */
494490

0 commit comments

Comments
 (0)