cvs: php4 /ext/standard basic_functions.c exec.c exec.h

From: Date: Tue, 05 Sep 2000 16:55:33 +0000
Subject: cvs: php4 /ext/standard basic_functions.c exec.c exec.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-1580@lists.php.net to get a copy of this message
rasmus Tue Sep 5 09:55:33 2000 EDT Modified files: /php4/ext/standard basic_functions.c exec.c exec.h Log: Add EscapeShellArg() function which turns a b into 'a b' and a'b into 'a'\''b' @Add EscapeShellArg() function (Rasmus) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.247 php4/ext/standard/basic_functions.c:1.248 --- php4/ext/standard/basic_functions.c:1.247 Tue Sep 5 07:23:16 2000 +++ php4/ext/standard/basic_functions.c Tue Sep 5 09:55:32 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.247 2000/09/05 14:23:16 stas Exp $ */ +/* $Id: basic_functions.c,v 1.248 2000/09/05 16:55:32 rasmus Exp $ */ #include "php.h" #include "php_main.h" @@ -212,6 +212,7 @@ PHP_FE(exec, second_and_third_args_force_ref) PHP_FE(system, second_arg_force_ref) PHP_FE(escapeshellcmd, NULL) + PHP_FE(escapeshellarg, NULL) PHP_FE(passthru, second_arg_force_ref) PHP_FE(shell_exec, NULL) Index: php4/ext/standard/exec.c diff -u php4/ext/standard/exec.c:1.39 php4/ext/standard/exec.c:1.40 --- php4/ext/standard/exec.c:1.39 Sun Aug 6 10:56:56 2000 +++ php4/ext/standard/exec.c Tue Sep 5 09:55:32 2000 @@ -15,7 +15,7 @@ | Author: Rasmus Lerdorf | +----------------------------------------------------------------------+ */ -/* $Id: exec.c,v 1.39 2000/08/06 17:56:56 eschmid Exp $ */ +/* $Id: exec.c,v 1.40 2000/09/05 16:55:32 rasmus Exp $ */ #include <stdio.h> #include "php.h" @@ -314,7 +314,7 @@ cmd = emalloc(2 * l + 1); strcpy(cmd, str); for (x = 0; cmd[x]; x++) { - if (php_get_index("&;`'\"|*?~<>^()[]{}$\\\x0A\xFF", cmd[x]) != -1) { + if (php_get_index("#&;`'\"|*?~<>^()[]{}$\\\x0A\xFF", cmd[x]) != -1) { for (y = l + 1; y > x; y--) cmd[y] = cmd[y - 1]; l++; /* length has been increased */ @@ -325,6 +325,32 @@ return cmd; } +char * php_escape_shell_arg(char *str) { + register int x, y, l; + char *cmd; + + l = strlen(str); + cmd = emalloc(4 * l + 3); + cmd[0] = '\''; + strcpy(cmd+1, str); + l++; + + for (x = 1; cmd[x]; x++) { + if (cmd[x] == '\'') { + for (y = l + 3; y > x+1; y--) { + cmd[y] = cmd[y - 3]; + } + cmd[++x] = '\\'; + cmd[++x] = '\''; + cmd[++x] = '\''; + l+=3; /* length was increased by 3 */ + } + } + cmd[l++] = '\''; + cmd[l] = '\0'; + return cmd; +} + /* {{{ proto string escapeshellcmd(string command) Escape shell metacharacters */ PHP_FUNCTION(escapeshellcmd) @@ -339,6 +365,26 @@ convert_to_string_ex(arg1); if ((*arg1)->value.str.len) { cmd = php_escape_shell_cmd((*arg1)->value.str.val); + RETVAL_STRING(cmd, 1); + efree(cmd); + } +} +/* }}} */ + +/* {{{ proto string escapeshellarg(string arg) + Quote and escape an argument for use in a shell command */ +PHP_FUNCTION(escapeshellarg) +{ + pval **arg1; + char *cmd = NULL; + + if (zend_get_parameters_ex(1, &arg1) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_string_ex(arg1); + if ((*arg1)->value.str.len) { + cmd = php_escape_shell_arg((*arg1)->value.str.val); RETVAL_STRING(cmd, 1); efree(cmd); } Index: php4/ext/standard/exec.h diff -u php4/ext/standard/exec.h:1.5 php4/ext/standard/exec.h:1.6 --- php4/ext/standard/exec.h:1.5 Sun Jul 23 18:39:49 2000 +++ php4/ext/standard/exec.h Tue Sep 5 09:55:32 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: exec.h,v 1.5 2000/07/24 01:39:49 david Exp $ */ +/* $Id: exec.h,v 1.6 2000/09/05 16:55:32 rasmus Exp $ */ #ifndef EXEC_H #define EXEC_H @@ -24,6 +24,7 @@ PHP_FUNCTION(system); PHP_FUNCTION(exec); PHP_FUNCTION(escapeshellcmd); +PHP_FUNCTION(escapeshellarg); PHP_FUNCTION(passthru); PHP_FUNCTION(shell_exec);

« previous php.cvs (#1580) next »