C:(标准输入):错误的文件描述符
C : (standard input): Bad file descriptor
我尝试为学校项目重新创建一个模拟此类命令的程序
< infile grep g | wc -c > outfile
有人知道我为什么会这样
grep: (standard input): Bad file descriptor
当运行下面的代码
void first_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
if ((cmd->fd = open(cmd->file, O_WRONLY)) == -1)
perror_exit("open");
if (close(pipe_fd[0]) == -1)
perror_exit("close 1 1st cmd");
if (dup2(cmd->fd, STDIN_FILENO) == -1)
perror_exit("dup2 1st cmd");
if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
perror_exit("dup2 1st cmd");
if (close(pipe_fd[1]) == -1)
perror_exit("close 2 1st cmd");
if (execve(cmd->path_exec, cmd->tab_cmd, envp) == -1)
perror_exit("execve 1st cmd");
}
void last_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
cmd = cmd->next;
if ((cmd->fd = open(cmd->file, O_WRONLY)) == -1)
perror_exit("open 2nd cmd");
if (close(pipe_fd[1]) == -1)
perror_exit("close 1 2nd cmd");
if (dup2(cmd->fd, STDOUT_FILENO) == -1)
perror_exit("dup2 2nd cmd");
if (dup2(pipe_fd[0], STDIN_FILENO) == -1)
perror_exit("dup2 2nd cmd");
if (close(pipe_fd[0]) == -1)
perror_exit("close 2 2nd cmd");
if (execve(cmd->path_exec, cmd->tab_cmd, envp) == -1)
perror_exit("execve 2nd cmd");
exit(EXIT_SUCCESS);
}
void fork_it(t_list *list, char **envp)
{
pid_t pid;
pid_t pid_2;
t_command *cmd;
int status;
int pipe_fd[2];
cmd = list->first;
if (pipe(pipe_fd) == -1)
perror_exit("pipe");
pid = fork();
if (pid < 0)
perror_exit("fork");
else if (pid == 0)
first_cmd_exec(cmd, envp, pipe_fd);
pid_2 = fork();
if (pid_2 < 0)
perror_exit("fork");
else if (pid_2 == 0)
last_cmd_exec(cmd, envp, pipe_fd);
else
{
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(pid, &status, 0);
waitpid(pid_2, &status, 0);
}
}
如果我理解正确,我必须使用 dup2
将标准输入替换为我的 fd(我的 infile),以便我的 execve
命令可以使用它,我的第二个命令也是如此管道后的命令,以便它可以写入我的输出文件,但我一定是在我的理解或我的代码中遗漏了一些东西。
提前致谢
https://man7.org/linux/man-pages/man2/read.2.html
EBADF fd is not a valid file descriptor or is not open for
reading.
grep 的标准输入是否开放阅读?
void first_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
if ((cmd->fd = open(cmd->file, **O_WRONLY**)) == -1)
不,您似乎出于某种原因以只写方式打开它。
我尝试为学校项目重新创建一个模拟此类命令的程序
< infile grep g | wc -c > outfile
有人知道我为什么会这样
grep: (standard input): Bad file descriptor
当运行下面的代码
void first_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
if ((cmd->fd = open(cmd->file, O_WRONLY)) == -1)
perror_exit("open");
if (close(pipe_fd[0]) == -1)
perror_exit("close 1 1st cmd");
if (dup2(cmd->fd, STDIN_FILENO) == -1)
perror_exit("dup2 1st cmd");
if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
perror_exit("dup2 1st cmd");
if (close(pipe_fd[1]) == -1)
perror_exit("close 2 1st cmd");
if (execve(cmd->path_exec, cmd->tab_cmd, envp) == -1)
perror_exit("execve 1st cmd");
}
void last_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
cmd = cmd->next;
if ((cmd->fd = open(cmd->file, O_WRONLY)) == -1)
perror_exit("open 2nd cmd");
if (close(pipe_fd[1]) == -1)
perror_exit("close 1 2nd cmd");
if (dup2(cmd->fd, STDOUT_FILENO) == -1)
perror_exit("dup2 2nd cmd");
if (dup2(pipe_fd[0], STDIN_FILENO) == -1)
perror_exit("dup2 2nd cmd");
if (close(pipe_fd[0]) == -1)
perror_exit("close 2 2nd cmd");
if (execve(cmd->path_exec, cmd->tab_cmd, envp) == -1)
perror_exit("execve 2nd cmd");
exit(EXIT_SUCCESS);
}
void fork_it(t_list *list, char **envp)
{
pid_t pid;
pid_t pid_2;
t_command *cmd;
int status;
int pipe_fd[2];
cmd = list->first;
if (pipe(pipe_fd) == -1)
perror_exit("pipe");
pid = fork();
if (pid < 0)
perror_exit("fork");
else if (pid == 0)
first_cmd_exec(cmd, envp, pipe_fd);
pid_2 = fork();
if (pid_2 < 0)
perror_exit("fork");
else if (pid_2 == 0)
last_cmd_exec(cmd, envp, pipe_fd);
else
{
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(pid, &status, 0);
waitpid(pid_2, &status, 0);
}
}
如果我理解正确,我必须使用 dup2
将标准输入替换为我的 fd(我的 infile),以便我的 execve
命令可以使用它,我的第二个命令也是如此管道后的命令,以便它可以写入我的输出文件,但我一定是在我的理解或我的代码中遗漏了一些东西。
提前致谢
https://man7.org/linux/man-pages/man2/read.2.html
EBADF fd is not a valid file descriptor or is not open for reading.
grep 的标准输入是否开放阅读?
void first_cmd_exec(t_command *cmd, char **envp, int *pipe_fd)
{
if ((cmd->fd = open(cmd->file, **O_WRONLY**)) == -1)
不,您似乎出于某种原因以只写方式打开它。