【发布时间】:2016-07-13 17:03:00
【问题描述】:
我正在实现一个 shell 并致力于重定向:将输出写入文件或从文件读取输入。我面临的问题是:
如果我将此命令输入到我的程序“sort ”中,我得到的文件的名称是“`b” " 虽然它不应该写入文件!并且程序挂起并且没有排序输出!
-
如果我输入:“ls > out.txt”,输出会同时写入终端和 out.txt 文件
李> 1234563然后它就停止了!
这是我的代码:
int execute(char **args)
{
int i, in, out;
char * inputF;
char * outputF;
if (args[0] == NULL) {
return 1;
}
int pNum = 0;
//char * cwd;
//cwd = getcurDirectory();
i = 0;
printf("***Testing: Before while loop\n");
while (args[i] != NULL) {
printf("***Testing: entered while loop: %d\n", i);
if (strcmp(args[i], "<") == 0) {
printf("***Testing: found %s in args[%d] \n", args[i], i);
in = 1;
args[i] = NULL;
inputF = args[i+1];
args[i+1] = NULL;
++i;
continue;
}
if (strcmp(args[i], ">") == 0) {
printf("***Testing: found %s in args[%d] \n", args[i], i);
out = 1;
args[i] = NULL;
outputF = args[i+1];
args[i+1] = NULL;
++i;
continue;
}
if (strcmp(args[i], "|") == 0) {
printf("***Testing: found %s in args[%d] \n", args[i], i);
++pNum;
}
++i;
}
if(in == 1 || out == 1){
int pid = fork();
if (pid == -1) {
perror("fork");
} else if (pid == 0) {
if (in) {
int fd0 = open(inputF, O_RDONLY, 0);
dup2(fd0, STDIN_FILENO);
close(fd0);
in = 0;
}
if (out) {
int fd1 = creat(outputF, 0644);
dup2(fd1, STDOUT_FILENO);
close(fd1);
out = 0;
}
int r;
for(r = 0; r < sizeof(args); ++r)
printf("***Testing: args[%d] %s \n", r, args[r]);
//setenv("parent",cwd,1);
if(execvp(args[0], args) < 0 ){
perror(*args);
exit(EXIT_FAILURE);
}
} else {
waitpid(pid, 0, 0);
//free(args);
}
}
if (pNum > 0){
//printf("***Testing: Phew! got a pipe! \n");
return handel_piping(args, pNum);
}
for (i = 0; i < builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}
return launch_cmd(args);
}
【问题讨论】:
-
for(r = 0; r < sizeof(args); ++r)是错误的。args是指针,而不是数组,所以sizeof(args)是指针中的字节数(通常为 4 或 8,具体取决于架构)。 -
警告:
in和out似乎没有初始化。 -
@purplepsycho 感谢您的提示,感谢您,我在我的解决方案中考虑了它们,但我仍然遇到我在帖子中提到的相同问题。还有其他提示吗?