这将会怎样呢?假设在命令行像这样输入。它会删除所有的文件和目录,从root的目录开始。我们需要sanitize这个输入来render the semicolon(;)metacharacter harmless.在Perl中,利用表4中的函数可以很容易的实现。(C中的这些等价函数在表5中;它们来自cgihtml的C库。)
程序5. C语言中的escape_input(). char *escape_input(char *str) /* takes string and escapes all metacharacters.should be used before including string in system() or similar call. */ { int i,j = 0; char *new = malloc(sizeof(char) * (strlen(str) * 2 + 1)); for (i = 0; i < strlen(str); i++) { printf("i = %d; j = %d\n",i,j); switch (str[i]) { case ’|’: case ’&’: case ’;’: case ’(’: case ’)’: case ’<’: case ’>’: case ’\’’: case ’"’: case ’*’: case ’?’: case ’\\’: case ’[’: case ’]’: case ’$’: case ’!’: case ’#’: case ’;’: case ’`’: case ’{’: case ’}’: new[j] = ’\\’; j++; break; default: break; } new[j] = str[i]; j++; } new[j] = ’\n’; return new; }