iliaa           Tue Feb 25 18:52:39 2003 EDT

  Modified files:              
    /php4/ext/standard  exec.c 
  Log:
  Made shell_exec() use streams, this simplifies the code and in some cases 
  makes it a little faster too.
  
  
Index: php4/ext/standard/exec.c
diff -u php4/ext/standard/exec.c:1.96 php4/ext/standard/exec.c:1.97
--- php4/ext/standard/exec.c:1.96       Tue Feb 25 11:21:00 2003
+++ php4/ext/standard/exec.c    Tue Feb 25 18:52:34 2003
@@ -15,7 +15,7 @@
    | Author: Rasmus Lerdorf                                               |
    +----------------------------------------------------------------------+
  */
-/* $Id: exec.c,v 1.96 2003/02/25 16:21:00 iliaa Exp $ */
+/* $Id: exec.c,v 1.97 2003/02/25 23:52:34 iliaa Exp $ */
 
 #include <stdio.h>
 #include "php.h"
@@ -452,9 +452,10 @@
 PHP_FUNCTION(shell_exec)
 {
        FILE *in;
-       int readbytes, total_readbytes=0, allocated_space;
+       size_t total_readbytes;
        pval **cmd;
        char *ret;
+       php_stream *stream;
 
        if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) {
                WRONG_PARAM_COUNT;
@@ -474,21 +475,16 @@
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", 
Z_STRVAL_PP(cmd));
                RETURN_FALSE;
        }
-       allocated_space = EXEC_INPUT_BUF;
-       ret = (char *) emalloc(allocated_space);
-       while (1) {
-               readbytes = fread(ret+total_readbytes, 1, EXEC_INPUT_BUF, in);
-               if (readbytes<=0) {
-                       break;
-               }
-               total_readbytes += readbytes;
-               allocated_space = total_readbytes+EXEC_INPUT_BUF;
-               ret = (char *) erealloc(ret, allocated_space);
-       }
-       pclose(in);
+
+       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); 
        
-       RETVAL_STRINGL(ret, total_readbytes, 0);
-       Z_STRVAL_P(return_value)[total_readbytes] = '\0';       
+       if (total_readbytes > 0) {
+               RETURN_STRINGL(ret, total_readbytes, 0);
+       } else {
+               RETURN_NULL();  
+       }
 }
 /* }}} */
 



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to