diff --git a/src/exec/exec_split_cmd.c b/src/exec/exec_split_cmd.c new file mode 100644 index 0000000..792d32e --- /dev/null +++ b/src/exec/exec_split_cmd.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_split_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou infile, STDIN_FILENO); + if (status == -1) + return (-1); + if (cmd->outfile != STDOUT_FILENO) + status = dup2(STDOUT_FILENO, cmd->outfile); + else + 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)) + return (-1); + + int fork_pid = fork(); + if (fork_pid == -1) + return (-1); + if (!fork_pid) + { + status = execve(cmd->cmd, cmd->argv, env); + exit(-1); + } + else + waitpid(fork_pid, NULL, 0); + if (status == -1) + return (-1); + return (0); +} + +int exec_split_cmd(t_list *list_cmd, t_env *env) +{ + char **env_array; + int status; + t_cmd *cmd; + + env_array = env_get(env); + if (!env_array) + return (-1); + while (list_cmd->next) + { + status = exec_single_cmd(list_cmd->content, env_array, env); + if (status == -1) + { + ft_free("a", &env_array); + return (-1); + } + list_cmd = list_cmd->next; + } + cmd = list_cmd->content; + status = dup2(cmd->infile, STDIN_FILENO); + if (status == -1) + return (-1); + status = dup2(STDOUT_FILENO, cmd->outfile); + if (status == -1) + return (-1); + if (cmd->cmd[0] != '/') + cmd->cmd = get_cmd_path(cmd->cmd, env); + if (!(cmd->cmd)) + return (-1); + int fork_pid = fork(); + if (fork_pid == -1) + return (-1); + if (!fork_pid) + { + status = execve(cmd->cmd, cmd->argv, env_array); + exit(-1); + } + 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 ea4ed13..a46e80a 100644 --- a/src/exec/get_path.c +++ b/src/exec/get_path.c @@ -6,18 +6,19 @@ /* By: mmoussou d_name, ".", ft_strlen(dir_entry->d_name)) - || !ft_strncmp(dir_entry->d_name, "..", ft_strlen(dir_entry->d_name))) + || !ft_strncmp(dir_entry->d_name, "..", + ft_strlen(dir_entry->d_name))) { dir_entry = readdir(path_dir); continue ; } - abs_path = ft_calloc(sizeof(char), strlen(path) + strlen(dir_entry->d_name) + 1); + abs_path = ft_calloc(sizeof(char), strlen(path) + + strlen(dir_entry->d_name) + 2); if (!abs_path) 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))) - printf("%s\n", dir_entry->d_name); - free(abs_path); + { + 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); } -int get_path(char *path) +char **get_path(char *path) { char **path_dir; + char **path_list; + t_list *list_entry; int i; + list_entry = ft_lstnew(NULL); path_dir = ft_split(path, ':'); if (!path_dir) - return (-1); + return (NULL); i = 0; while (path_dir[i]) { - get_path_list(path_dir[i]); + get_path_list(path_dir[i], list_entry); i++; } - ft_free("a", &path_dir); - return (0); + 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++; + } + ft_free("al", &path_dir, &list_entry); + return (path_list); }