【发布时间】:2012-05-19 17:17:44
【问题描述】:
我已经将用户的给定命令分成子字符串,这是代码:
int i;
char *line = malloc(BUFFER);
char *origLine = line;
fgets(line, 128, stdin); // get a line from stdin
// get complete diagnostics on the given string
lineData info = runDiagnostics(line);
char command[20];
sscanf(line, "%20s ", command);
line = strchr(line, ' ');
printf("The Command is: %s\n", command);
int currentCount = 0; // number of elements in the line
int *argumentsCount = ¤tCount; // pointer to that
// get the elements separated
char** arguments = separateLineGetElements(line,argumentsCount);
// here we call a method that would execute the commands
if (execvp(*arguments,*argumentsCount) < 0) // execute the command
{
printf("ERROR: exec failed\n");
exit(1);
}
当我在execvp(*arguments,*argumentsCount) 中执行命令时,它失败了。
怎么了?
谢谢。
编辑:
用户的输入是:ls > a.out,因此我有 3 个字符串,它们是:
ls,>,a.out,它失败了。
【问题讨论】: