execvp() 未处理写入输出 >filename.log
execvp() not processing writing to output >filename.log
我的代码中的 execvp 有一些问题,我想写一个简单的终端将命令的结果保存在日志文件中,问题是当我使用 "> a.log"
时应该将结果带到输出,它没有响应并出错!
int lsh_launch(char **args)
{
pid_t pid;
int status;
int i = 0;
while (args[i] != NULL)
{
printf("%s\n", args[i]);
i++;
}
args[i] = ">";
args[i + 1] = "a.log";
pid = fork();
if (pid == 0)
{
printf("child proc\n");
// Child process
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
// Error forking
perror("lsh");
}
else
{
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
当我将 args 更改为其他值(例如 -v
以查看版本)时,它起作用了,而且问题似乎出在导出到输出上!当我对程序执行 ls
和 > a.log
时的结果是:
- ls: cannot access >: No such file or directory
- ls: cannot access a.log: No such file or directory
假设 args
是 argv
传递给 main()
函数1:
您的程序调用了未定义的行为
while (args[i] != NULL)
{
printf("%s\n", args[i]);
i++;
}
在此循环结束时,i
的值超出了 args
数组的范围。于是
args[i] = ">";
args[i + 1] = "a.log";
尝试在禁止的位置写入,调用未定义的行为。
1不清楚,因为显然缺少一些代码
重定向不是通过这种方式获得的。 >
(或类似的)是在 shell 中进行重定向的语法。 shell 解释命令行并在 exec
命令之前进行重定向,这样:
pid = fork();
switch(pid) {
case 0:
d = open("myfile",O_WRONLY);
dup2(d,STDOUT_FILENO); // redirect *stdout* to open file d by duplicating it
close(d); // now unused d (d is a duplicate of *stdout*
exec**(...); // now mutate to a new code which inherits open file descriptors
exit(1);
break;
case -1: // error case of fork
break;
default:
wait(NULL); // or whatever you want, don't wait for *background style*
break;
}
我的代码中的 execvp 有一些问题,我想写一个简单的终端将命令的结果保存在日志文件中,问题是当我使用 "> a.log"
时应该将结果带到输出,它没有响应并出错!
int lsh_launch(char **args)
{
pid_t pid;
int status;
int i = 0;
while (args[i] != NULL)
{
printf("%s\n", args[i]);
i++;
}
args[i] = ">";
args[i + 1] = "a.log";
pid = fork();
if (pid == 0)
{
printf("child proc\n");
// Child process
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
// Error forking
perror("lsh");
}
else
{
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
当我将 args 更改为其他值(例如 -v
以查看版本)时,它起作用了,而且问题似乎出在导出到输出上!当我对程序执行 ls
和 > a.log
时的结果是:
- ls: cannot access >: No such file or directory
- ls: cannot access a.log: No such file or directory
假设 args
是 argv
传递给 main()
函数1:
您的程序调用了未定义的行为
while (args[i] != NULL)
{
printf("%s\n", args[i]);
i++;
}
在此循环结束时,i
的值超出了 args
数组的范围。于是
args[i] = ">";
args[i + 1] = "a.log";
尝试在禁止的位置写入,调用未定义的行为。
1不清楚,因为显然缺少一些代码
重定向不是通过这种方式获得的。 >
(或类似的)是在 shell 中进行重定向的语法。 shell 解释命令行并在 exec
命令之前进行重定向,这样:
pid = fork();
switch(pid) {
case 0:
d = open("myfile",O_WRONLY);
dup2(d,STDOUT_FILENO); // redirect *stdout* to open file d by duplicating it
close(d); // now unused d (d is a duplicate of *stdout*
exec**(...); // now mutate to a new code which inherits open file descriptors
exit(1);
break;
case -1: // error case of fork
break;
default:
wait(NULL); // or whatever you want, don't wait for *background style*
break;
}