Esempio n. 1
0
int			ft_check_unlocated_cmd(t_sh *sh, char **args)
{
	int		i;
	char	*path;
	char	**pathlst;

	i = -1;
	path = ft_search_inallenv(sh, "PATH");
	pathlst = ft_strsplit(path, ':');
	while (path && pathlst[++i])
	{
		ft_strdel(&path);
		path = ft_strjoin_key(pathlst[i], args[0], "/");
		if (access(path, F_OK) != -1)
			break ;
	}
	if (!path || !pathlst[i])
		LAST_RET_VAL = ft_error2("command not found", args[0]);
	else if (access(path, X_OK) == -1)
		LAST_RET_VAL = ft_error2("permission denied", args[0]);
	else
		ft_fork_and_exec(sh, path, args);
	ft_strdel(&path);
	ft_strtabdel(&pathlst);
	return (LAST_RET_VAL);
}
Esempio n. 2
0
int			ft_check_located_cmd(t_sh *sh, char **args)
{
	if (!ft_strnequ(args[0], "./", 2) && !ft_strnequ(args[0], "../", 3))
	{
		if (!ft_strnequ(args[0], "/", 1))
			return (ft_check_unlocated_cmd(sh, args));
	}
	if (access(args[0], F_OK) == -1)
		return (ft_error2("command not found", args[0]));
	if (access(args[0], X_OK) == -1)
		return (ft_error2("permission denied", args[0]));
	ft_fork_and_exec(sh, args[0], args);
	return (0);
}
Esempio n. 3
0
void	check_file(char *av, t_args *args)
{
	struct stat	*stats;

	stats = malloc(sizeof(*stats));
	if (!lstat(av, stats))
	{
		if (S_ISDIR(stats->st_mode))
			add_file(&(args->dirs), new_file(ft_strdup(av)));
		else if (S_ISREG(stats->st_mode))
			add_file(&(args->files), new_file(ft_strdup(av)));
		else if (S_ISLNK(stats->st_mode))
		{
			if (args->opt->l)
				add_file(&(args->files), new_file(ft_strdup(av)));
			else
				add_file(&(args->dirs), new_file(ft_strdup(av)));
		}
	}
	else
	{
		ft_error2(av);
		args->error = 1;
	}
	free(stats);
}
Esempio n. 4
0
void		ft_exec_cmd(t_sh *sh, pid_t pid, char *path, char **args)
{
	if (pid == 0)
	{
		if (execve(path, args, ENV) == -1)
		{
			ft_error2("exec format error", args[0]);
			exit(EXIT_FAILURE);
		}
	}
	else if (pid < 0)
	{
		ft_error2("failure during execution", args[0]);
		LAST_RET_VAL = EXIT_FAILURE;
	}
	else
	{
		wait(&pid);
		LAST_RET_VAL = WEXITSTATUS(pid);
	}
}
Esempio n. 5
0
void	check_options(char *av, t_args *args)
{
	int i;

	i = 1;
	while (av[i])
	{
		if (av[i] != 'R' && av[i] != 'l' &&
		av[i] != 'a' && av[i] != 'r' &&
		av[i] != 't' && av[i] != 'p' && av[i] != 'f' && av[i] != 'g' &&
		av[i] != 'h')
			ft_error(av[i]);
		i++;
	}
	if (i == 1)
	{
		ft_error2(av);
		args->error = 1;
	}
	modif_options(av, args);
}