/* {{{ php_escape_shell_cmd
Escape all chars that could possibly be used to
break out of a shell command
This function emalloc’s a string and returns the pointer.
Remember to efree it when done with it.
*NOT* safe for binary strings
*/
char *php_escape_shell_cmd(char *str) {
register int x, y, l;
char *cmd;
char *p = NULL;
l = strlen(str);
cmd = safe_emalloc(2, l, 1);
for (x = 0, y = 0; x < l; x ) {
switch (str[x]) {
case ’"’:
case ’\’’:
#ifndef PHP_WIN32
if (!p && (p = memchr(str x 1, str[x], l - x - 1))) {
/* noop */
} else if (p && *p == str[x]) {
p = NULL;
} else {
cmd[y ] = ’\\’;
}
cmd[y ] = str[x];
break;
#endif
case ’#’: /* This is character-set independent */
case ’&’:
case ’;’:
case ’`’:
case ’|’:
case ’*’:
case ’?’:
case ’~’:
case ’<’:
case ’>’:
case ’^’:
case ’(’:
case ’)’:
case ’[’:
case ’]’:
case ’{’:
case ’}’:
case ’$’:
case ’\\’:
case ’\x0A’: /* excluding these two */
case ’\xFF’:
#ifdef PHP_WIN32
/* since Windows does not allow us to escape these chars, just remove them */
case ’%’:
cmd[y ] = ’ ’;
break;
#endif
cmd[y ] = ’\\’;
/* fall-through */
default:
cmd[y ] = str[x];