You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.0 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* caller.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/03 20:29:01 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "minishell.h"
3 years ago
int command_call(char *argv[])
3 years ago
{
if (ft_strncmp(argv[0], "echo", 4) == 0)
3 years ago
return (ft_echo(argv));
3 years ago
else if (ft_strncmp(argv[0], "pwd", 3) == 0)
3 years ago
return (ft_pwd(argv));
3 years ago
else if (ft_strncmp(argv[0], "cd", 2) == 0)
3 years ago
return (ft_cd(argv));
3 years ago
else if (ft_strncmp(argv[0], "export", 6) == 0)
3 years ago
return (ft_export(argv));
3 years ago
else if (ft_strncmp(argv[0], "unset", 5) == 0)
3 years ago
return (ft_unset(argv));
3 years ago
else if (ft_strncmp(argv[0], "env", 3) == 0)
3 years ago
return (ft_env(argv));
3 years ago
else if (ft_strncmp(argv[0], "exit", 4) == 0)
return (close_minishell(0));
else
{
errno = EINVAL;
return (-1);
}
}
3 years ago
3 years ago
void exe(char *argv[], char *const envp[])
{
char **path_dirs;
char *path;
size_t path_length;
execve(argv[0], argv, envp);
path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs)
{
path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0]));
free(path);
execve(path, argv, envp);
path_dirs++;
}
}