exec
This commit is contained in:
@@ -6,57 +6,49 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 15:43:24 by lderidde ### ########.fr */
|
||||
/* Updated: 2025/01/28 10:41:39 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
// int is_builtin(char *str)
|
||||
// {
|
||||
// if (ft_strncmp(str, "exit", 4) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "pwd", 3) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "echo", 4) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "env", 3) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "unset", 5) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "cd", 2) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "export", 6) == 0)
|
||||
// return (1);
|
||||
// }
|
||||
int is_builtin(char *str)
|
||||
{
|
||||
if (ft_strncmp(str, "exit", 4) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "pwd", 3) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "echo", 4) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "env", 3) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "unset", 5) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "cd", 2) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "export", 6) == 0)
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
// int exec_builtin(t_ast_n *node)
|
||||
// {
|
||||
// if (ft_strncmp(node->cmd, "exit", 4) == 0)
|
||||
// return (builtin_exit(node->cmd, true));
|
||||
// if (ft_strncmp(node->cmd, "pwd", 3) == 0)
|
||||
// return (builtin_pwd(node->cmd));
|
||||
// if (ft_strncmp(node->cmd, "echo", 4) == 0)
|
||||
// return (builtin_echo(ft_split(node->arg, " "), node->head->env));
|
||||
// if (ft_strncmp(node->cmd, "env", 3) == 0)
|
||||
// return (builtin_env(node->cmd, node->head->env));
|
||||
// if (ft_strncmp(node->cmd, "unset", 5) == 0)
|
||||
// return (builtin_unset(ft_split(node->arg, " "), node->head));
|
||||
// if (ft_strncmp(node->cmd, "cd", 2) == 0)
|
||||
// return (builtin_cd(ft_split(node->arg, " "), node->head));
|
||||
// if (ft_strncmp(node->cmd, "export", 6) == 0)
|
||||
// return (builtin_export(ft_split(node->arg, " "), node->head));
|
||||
// }
|
||||
|
||||
// void free_tab(char **tab)
|
||||
// {
|
||||
// int i;
|
||||
//
|
||||
// i = -1;
|
||||
// while (tab[++i])
|
||||
// free(tab[i]);
|
||||
// free(tab);
|
||||
// }
|
||||
int exec_builtin(t_ast_n *node)
|
||||
{
|
||||
if (ft_strncmp(node->cmd, "exit", 4) == 0)
|
||||
return (builtin_exit(node->args, true));
|
||||
else if (ft_strncmp(node->cmd, "pwd", 3) == 0)
|
||||
return (builtin_pwd(node->args));
|
||||
else if (ft_strncmp(node->cmd, "echo", 4) == 0)
|
||||
return (builtin_echo(node->args, node->head->env));
|
||||
else if (ft_strncmp(node->cmd, "env", 3) == 0)
|
||||
return (builtin_env(node->args, node->head->env));
|
||||
else if (ft_strncmp(node->cmd, "unset", 5) == 0)
|
||||
return (builtin_unset(node->args, node->head));
|
||||
else if (ft_strncmp(node->cmd, "cd", 2) == 0)
|
||||
return (builtin_cd(node->args, node->head));
|
||||
else
|
||||
return (builtin_export(node->args, node->head));
|
||||
}
|
||||
|
||||
char *find_path(char *cmd, char **env)
|
||||
{
|
||||
@@ -65,10 +57,9 @@ char *find_path(char *cmd, char **env)
|
||||
char **paths;
|
||||
int i;
|
||||
|
||||
// if (access(cmd, F_OK) == 0)
|
||||
// return (cmd);
|
||||
if (access(cmd, F_OK) == 0)
|
||||
return (cmd);
|
||||
i = 0;
|
||||
ft_printf("test2\n");
|
||||
while (ft_strnstr(env[i], "PATH=", 5) == NULL)
|
||||
i++;
|
||||
paths = ft_split(env[i] + 5, ":");
|
||||
@@ -77,7 +68,6 @@ char *find_path(char *cmd, char **env)
|
||||
{
|
||||
tmp = ft_strjoin(paths[i], "/");
|
||||
path = ft_strjoin(tmp, cmd);
|
||||
printf("path: %s\n", path);
|
||||
free(tmp);
|
||||
if (access(path, F_OK) == 0)
|
||||
return (free_tab(paths), path);
|
||||
@@ -121,7 +111,7 @@ void handle_redir(t_ast_n *node)
|
||||
else if (node->redir == _RED_L)
|
||||
handle_file(node, 1);
|
||||
else if (node->redir == _RED_R)
|
||||
handle_file(node , 2);
|
||||
handle_file(node, 2);
|
||||
else if (node->redir == _RED_DR)
|
||||
handle_file(node, 3);
|
||||
if (node->redir == _RED_L)
|
||||
@@ -141,10 +131,7 @@ int exec(t_ast_n *node)
|
||||
char *path;
|
||||
|
||||
handle_redir(node);
|
||||
ft_printf("test1\n");
|
||||
path = find_path(node->cmd, node->head->env);
|
||||
ft_printf("test3\n");
|
||||
printf("path: %s\n", path);
|
||||
if (!path)
|
||||
return_error(node->cmd, "command not found", 127);
|
||||
if (access(path, X_OK) != 0)
|
||||
@@ -163,10 +150,10 @@ int exec_scmd(t_ast_n *node)
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
// if (is_builtin(node->cmd))
|
||||
// return (exec_builtin(node));
|
||||
// else
|
||||
// {
|
||||
if (is_builtin(node->cmd))
|
||||
return (exec_builtin(node));
|
||||
else
|
||||
{
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
@@ -174,22 +161,20 @@ int exec_scmd(t_ast_n *node)
|
||||
}
|
||||
else
|
||||
{
|
||||
wait(NULL);
|
||||
return (0);
|
||||
waitpid(pid, &status, 0);
|
||||
if (WIFEXITED(status))
|
||||
return (WEXITSTATUS(status));
|
||||
else
|
||||
return (-1);
|
||||
}
|
||||
return (0);
|
||||
// }
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
int execute_command(t_ast_n *node)
|
||||
{
|
||||
int status;
|
||||
|
||||
printf("test100\n");
|
||||
if (node->state == _CMD)
|
||||
return (exec_scmd(node));
|
||||
else if (node->state == _AND)
|
||||
|
||||
Reference in New Issue
Block a user