summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-03-25 11:11:30 +0100
committerNikita Popov <nikita.ppv@gmail.com>2020-03-25 11:11:30 +0100
commita524785d5d26be1608404e439652ba11839755eb (patch)
tree644c7f75a9e2a924db46c6c3873e19f78ba761d6 /ext
parentd6a19bae51bd48a3d33aa568517ff14e81d2359d (diff)
downloadphp-git-a524785d5d26be1608404e439652ba11839755eb.tar.gz
Clean up php_exec() implementation a bit
Diffstat (limited to 'ext')
-rw-r--r--ext/standard/exec.c61
1 files changed, 27 insertions, 34 deletions
diff --git a/ext/standard/exec.c b/ext/standard/exec.c
index 3b0e0a034f..61a63eaf75 100644
--- a/ext/standard/exec.c
+++ b/ext/standard/exec.c
@@ -81,6 +81,28 @@ PHP_MINIT_FUNCTION(exec)
}
/* }}} */
+static size_t strip_trailing_whitespace(char *buf, size_t bufl) {
+ size_t l = bufl;
+ while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
+ if (l != (bufl - 1)) {
+ bufl = l + 1;
+ buf[bufl] = '\0';
+ }
+ return bufl;
+}
+
+static void handle_line(int type, zval *array, char *buf, size_t bufl) {
+ if (type == 1) {
+ PHPWRITE(buf, bufl);
+ if (php_output_get_level() < 1) {
+ sapi_flush();
+ }
+ } else if (type == 2) {
+ bufl = strip_trailing_whitespace(buf, bufl);
+ add_next_index_stringl(array, buf, bufl);
+ }
+}
+
/* {{{ php_exec
* If type==0, only last line of output is returned (exec)
* If type==1, all lines will be printed and last lined returned (system)
@@ -92,7 +114,6 @@ PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value)
{
FILE *fp;
char *buf;
- size_t l = 0;
int pclose_return;
char *b, *d=NULL;
php_stream *stream;
@@ -139,45 +160,17 @@ PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value)
bufl += b - buf;
}
- if (type == 1) {
- PHPWRITE(buf, bufl);
- if (php_output_get_level() < 1) {
- sapi_flush();
- }
- } else if (type == 2) {
- /* strip trailing whitespaces */
- l = bufl;
- while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
- if (l != (bufl - 1)) {
- bufl = l + 1;
- buf[bufl] = '\0';
- }
- add_next_index_stringl(array, buf, bufl);
- }
+ handle_line(type, array, buf, bufl);
b = buf;
}
if (bufl) {
- /* output remaining data in buffer */
- if (type == 1 && buf != b) {
- PHPWRITE(buf, bufl);
- if (php_output_get_level() < 1) {
- sapi_flush();
- }
- }
- /* strip trailing whitespaces if we have not done so already */
- if ((type == 2 && buf != b) || type != 2) {
- l = bufl;
- while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
- if (l != (bufl - 1)) {
- bufl = l + 1;
- buf[bufl] = '\0';
- }
- if (type == 2) {
- add_next_index_stringl(array, buf, bufl);
- }
+ if (buf != b) {
+ /* Process remaining output */
+ handle_line(type, array, buf, bufl);
}
/* Return last line from the shell command */
+ bufl = strip_trailing_whitespace(buf, bufl);
RETVAL_STRINGL(buf, bufl);
} else { /* should return NULL, but for BC we return "" */
RETVAL_EMPTY_STRING();