Exemple #1
0
static int	exec_redout_get_fd(t_tree *root)
{
	char	*path;
	int		open_fd;
	int		opt;

	if ((path = cd_get_path2(root->cmd[1])) == NULL)
		return (sh_reset_std_fd() - 1);
	if (access(path, F_OK) == 0 && access(path, W_OK) < 0)
	{
		sh_reset_std_fd();
		ft_dprintf(STDERR_FILENO, "shell: %s: permission denied\n",
				root->cmd[1]);
		free(path);
		return (-1);
	}
	opt = O_WRONLY | O_CREAT | (root->types == D_GREAT ? O_APPEND : O_TRUNC);
	if ((open_fd = open(path, opt, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
	{
		sh_reset_std_fd();
		ft_dprintf(STDERR_FILENO, "shell: %s: cannot write on this\n",
				root->cmd[1]);
	}
	free(path);
	return (open_fd);
}
int			exec_or(t_tree *root)
{
	int		ret;

	if ((ret = sh_interpret(root->left)) == 0)
	{
		exec_free_root(root->right);
		return (ret);
	}
	else
	{
		sh_reset_std_fd();
		return (sh_interpret(root->right));
	}
}
int			exec_end(t_tree *root)
{
	char	*ret;
	int		i;

	i = sh_interpret(root->left);
	ret = ft_itoa(i);
	if ((sh_change_var_env("?", ret)) == -1)
		sh_add_var_env("?", ret);
	free(ret);
	sh_reset_std_fd();
	if (root->right != NULL)
		return (sh_interpret(root->right));
	else
		return (i);
}
Exemple #4
0
static int	exec_redout_g_and(t_tree *root)
{
	int		open_fd;

	if (root->types == G_AND)
	{
		if (root->cmd[1][0] > 48 && root->cmd[1][0] < 58)
		{
			if ((open_fd = ft_atoi(root->cmd[1])) > STDERR_FILENO)
			{
				sh_reset_std_fd();
				ft_dprintf(STDERR_FILENO, "shell: %d: bad file descriptor\n",
						open_fd);
				return (-1);
			}
			else
				return (open_fd);
		}
	}
	return (exec_redout_get_fd(root));
}