示例#1
0
int		simple_left(t_cmd *cmd, t_list *list, char **env, int builtin)
{
  t_red		var;
  int		fd;
  int		reset;

  (void)builtin;
  init_simpleleft(cmd, &var);
  if (!(cmd->go_on))
    return (FAILURE);
  if (cmd->prev && cmd->prev->token == PIPE)
    return (FAILURE + 0 * fprintf(stderr, "Ambiguous output redirect.\n"));
  var.is_builtin = builtin;
  if ((fd = open(var.name, O_RDONLY)) == -1)
    return (fprintf(stderr, "%s: %s\n",
		    var.name, strerror(errno)) * 0 + FAILURE);
  if (!var.cmd[0])
    return (SUCCESS);
  reset = dup(0);
  dup2(fd, 0);
  if (exec_left(list, env, cmd) == FAILURE)
    check_go_on(cmd);
  dup2(reset, 0);
  close(fd);
  xfree(var.name);
  free_tab(var.cmd);
  return (SUCCESS);
}
示例#2
0
文件: pipe.c 项目: rotarui/42sh
t_bool	exec_pipe(t_lexem *node, t_shell *shell)
{
  pid_t	pid;
  int	pipefd[2];

  if (!(node->left) || !(node->right))
    return ((node->is_pipe) ? (TRUE) : (CONTINUE));
  if (pipe(pipefd) != EXIT_SUCCESS)
    return (FALSE);
  node->right->fd[STDIN_FILENO] = pipefd[0];
  node->left->fd[STDOUT_FILENO] = pipefd[1];
  node->left->fd[STDERR_FILENO] = (node->type == PIPE_ERR)
    ? (pipefd[1]) : (STDERR_FILENO);
  if ((pid = fork()) == -1)
    return (FALSE);
  return ((pid == 0) ? (exec_left(node, shell, pipefd))
	  : (exec_right(node, shell, pipefd)));
}