diff --git a/include/execution.h b/include/execution.h index b5c5473..f0eb12d 100644 --- a/include/execution.h +++ b/include/execution.h @@ -6,7 +6,7 @@ /* By: mmoussou cmd[0] != '/') + cmd->cmd = get_cmd_global_path(cmd->cmd, env); + else if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/') + cmd->cmd = get_cmd_local_path(cmd->cmd, env); + if (!(cmd->cmd)) + return (-1); + return (0); +} + int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t) { int fork_pid; @@ -50,22 +69,18 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t) status = dup2(STDOUT_FILENO, STDIN_FILENO); if (status == -1) return (-1); - if (cmd->cmd[0] != '/') - cmd->cmd = get_cmd_path(cmd->cmd, env_t); - if (!(cmd->cmd)) + status = switch_cmd_path(cmd, env_t); + if (!status) return (-1); fork_pid = fork(); if (fork_pid == -1) return (-1); if (!fork_pid) - { status = execve(cmd->cmd, cmd->argv, env); + if (!fork_pid) exit(-1); - } else waitpid(fork_pid, NULL, 0); - if (status == -1) - return (-1); return (0); } @@ -80,9 +95,8 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t) status = dup2(STDOUT_FILENO, cmd->outfile); if (status == -1) return (-1); - if (cmd->cmd[0] != '/') - cmd->cmd = get_cmd_path(cmd->cmd, env_t); - if (!(cmd->cmd)) + status = switch_cmd_path(cmd, env_t); + if (!status) return (-1); fork_pid = fork(); if (fork_pid == -1) @@ -94,8 +108,6 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t) } else waitpid(fork_pid, NULL, 0); - if (status == -1) - return (-1); return (0); } diff --git a/src/exec/get_path.c b/src/exec/get_path.c index ae263a1..551c403 100644 --- a/src/exec/get_path.c +++ b/src/exec/get_path.c @@ -6,19 +6,64 @@ /* By: mmoussou content == NULL) + list_entry->content = abs_path; + else + { + new_list_entry = ft_lstnew(abs_path); + if (!new_list_entry) + { + free(abs_path); + return (-1); + } + ft_lstadd_back(&list_entry, new_list_entry); + } + return (0); +} + +int add_path_to_list(char *path, struct dirent *dir_entry, t_list *list_entry) +{ + char *abs_path; + struct stat entry; + int status; + + abs_path = ft_calloc(sizeof(char), strlen(path) + + strlen(dir_entry->d_name) + 2); + if (!abs_path) + return (-1); + ft_strlcat(abs_path, path, ft_strlen(path)); + ft_strlcat(abs_path, "/", 1); + ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(dir_entry->d_name)); + stat(abs_path, &entry); + if (S_ISREG(entry.st_mode) + && (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) + { + status = add_element_to_list(list_entry, abs_path); + if (status) + { + free(abs_path); + return (-1); + } + } + free(abs_path); + return (0); +} + int get_path_list(char *path, t_list *list_entry) { DIR *path_dir; struct dirent *dir_entry; - char *abs_path; - struct stat entry; - t_list *new_list_entry; + int status; path_dir = opendir(path); if (!path_dir) @@ -26,40 +71,41 @@ int get_path_list(char *path, t_list *list_entry) dir_entry = readdir(path_dir); while (dir_entry) { - if (!ft_strncmp(dir_entry->d_name, ".", ft_strlen(dir_entry->d_name)) - || !ft_strncmp(dir_entry->d_name, "..", - ft_strlen(dir_entry->d_name))) + if (!ft_strncmp(dir_entry->d_name, ".", 2) + || !ft_strncmp(dir_entry->d_name, "..", 3)) { dir_entry = readdir(path_dir); continue ; } - abs_path = ft_calloc(sizeof(char), strlen(path) - + strlen(dir_entry->d_name) + 2); - if (!abs_path) + status = add_path_to_list(path, dir_entry, list_entry); + if (status) + closedir(path_dir); + if (status) return (-1); - strcat(abs_path, path); - strcat(abs_path, "/"); - strcat(abs_path, dir_entry->d_name); - stat(abs_path, &entry); - if (S_ISREG(entry.st_mode) - && (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) - { - if (list_entry->content == NULL) - list_entry->content = abs_path; - else - { - new_list_entry = ft_lstnew(abs_path); - if (!new_list_entry) - return (-1); - ft_lstadd_back(&list_entry, new_list_entry); - } - } dir_entry = readdir(path_dir); } closedir(path_dir); return (0); } +char **path_trans_list_to_char(t_list *list_entry) +{ + char **path_list; + int i; + + i = 0; + path_list = ft_calloc(sizeof(char *), ft_lstsize(list_entry) + 1); + if (!path_list) + return (path_list); + while (list_entry) + { + path_list[i] = list_entry->content; + list_entry = list_entry->next; + i++; + } + return (path_list); +} + char **get_path(char *path) { char **path_dir; @@ -79,14 +125,7 @@ char **get_path(char *path) get_path_list(path_dir[i], list_entry); i++; } - i = 0; - path_list = ft_calloc(sizeof(char *), ft_lstsize(list_entry) + 1); - while (list_entry) - { - path_list[i] = list_entry->content; - list_entry = list_entry->next; - i++; - } + path_list = path_trans_list_to_char(list_entry); ft_free("al", &path_dir, &list_entry); return (path_list); } diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index 33bcb36..5ce7a14 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: mmoussou : close ce fd + 1: réouvre le fichier pour la lecture -1: pour signifier qu'il faut decrémenter la static */ int fd_manager(int mode) @@ -25,13 +25,10 @@ int fd_manager(int mode) char *path; int fd; - if (mode > 0) - close(mode); - if (mode) - { + if (mode < 0) index--; + if (mode < 0) return (0); - } path = ft_calloc(sizeof(char), 24 + 3); if (!path) return (-1); @@ -41,9 +38,12 @@ int fd_manager(int mode) if (!index_itoa) return (-1); index++; - strcat(path, "/tmp/.minishell-heredoc-"); - strcat(path, index_itoa); - fd = open(path, O_RDONLY | O_WRONLY | O_TRUNC | O_CREAT, 0644); + ft_strlcat(path, "/tmp/.minishell-heredoc-", 24); + ft_strlcat(path, index_itoa, ft_strlen(index_itoa)); + if (mode > 0) + fd = open(path, O_RDONLY); + else + fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); return (fd); } @@ -52,7 +52,6 @@ static int get_input(char *delimiter, int fd) char *line; int status; - printf("%s | ", delimiter); line = readline("heredoc> "); while (ft_strcmp(line, delimiter)) { @@ -73,9 +72,7 @@ static int get_input(char *delimiter, int fd) status = write(fd, "\0", 1); if (status == -1) fd_manager(fd); - if (status == -1) - return (-1); - return (0); + return (-(status == -1)); } int ft_heredoc(char *delimiter) @@ -92,7 +89,8 @@ int ft_heredoc(char *delimiter) fork_pid = fork(); if (fork_pid == -1) { - fd_manager(fd); + fd_manager(-1); + close(fd); return (-1); } if (!fork_pid) @@ -102,5 +100,5 @@ int ft_heredoc(char *delimiter) } else waitpid(fork_pid, NULL, 0); - return (fd); + return (fd_manager(1)); }