From: Lukas Jiriste Date: Thu, 1 Aug 2024 10:02:07 +0000 (+0200) Subject: Add the structure of ex_fields, implement find_exe X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=304bd0f6f752c7db94daa2c8a291066ce5d468fc;p=42%2Fminishell.git Add the structure of ex_fields, implement find_exe The find_exe goes through the PATH variable of environment and looks whether any of the directories contains the executable entered. --- diff --git a/src/execution.c b/src/execution.c index e62fee4..1fa0302 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */ -/* Updated: 2024/08/01 09:51:23 by ljiriste ### ########.fr */ +/* Updated: 2024/08/01 11:48:48 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,10 @@ #include #include +#include +#include +#include + int cmp_var_name(const char *a, const char *b) { int namelen; @@ -583,7 +587,7 @@ int assignments_to_env(const t_vec *assignments, t_execution_env *env) return (ft_vec_insert_range(&env->vars->other, assignments->vec, assignments->size, 0) == success); } -void free_fields(char **fields) +void free_split(char **fields) { size_t i; @@ -597,13 +601,104 @@ void free_fields(char **fields) return ; } -int ex_fields(char **fields,__attribute__((unused)) const t_vec *assignments,__attribute__((unused)) const t_vec *redirections,__attribute__((unused)) t_execution_env *env) +int is_contained_in_dir(const char *exe_name, DIR *dir) { - size_t i; + struct dirent *dir_entry; + dir_entry = readdir(dir); + while (dir_entry) + { + if (!ft_strcmp(dir_entry->d_name, exe_name)) + return (1); + dir_entry = readdir(dir); + } + return (0); +} + +char *find_exe(const char *exe_name, const t_execution_env *env) +{ + const char *path; + char **paths; + size_t i; + DIR *dir; + char *res; + + path = get_env_var_value(env, "PATH"); + paths = ft_split(path, ":"); + if (!paths) + return (NULL); + res = NULL; i = 0; - while (fields[i]) - ft_printf("%s\n", fields[i++]); + while (paths[i]) + { + dir = opendir(paths[i]); + if (!dir) + { + ++i; + continue ; + } + if (is_contained_in_dir(exe_name, dir)) + { + if (ft_strcat_alloc(&res, paths[i])) + if (ft_strcat_alloc(&res, "/")) + ft_strcat_alloc(&res, exe_name); + break ; + } + closedir(dir); + ++i; + } + free_split(paths); + closedir(dir); + return (res); +} + +int dup_pipes(__attribute__((unused)) const t_execution_env *env) +{ + return (0); +} + +int dup_redirections(__attribute__((unused)) const t_vec *redirections) +{ + return (0); +} + +int add_exported(__attribute__((unused)) t_vec *assignments, __attribute__((unused)) const t_execution_env *env) +{ + return (0); +} + +int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_execution_env *env) +{ + pid_t pid; + int status; + char *path; + + path = find_exe(fields[0], env); + if (!path) + return (1); + pid = -1; + //pid = fork(); + if (pid == 0) + { + dup_pipes(env); + dup_redirections(redirections); + add_exported(assignments, env); + ft_vec_append(assignments, nullptr); + execve(path, fields, assignments->vec); + exit(-1); + } + else if (pid > 0) + { + free(path); + waitpid(pid, &status, 0); + if (WIFEXITED(status)) + env->ret_val = WEXITSTATUS(status); + } + else + { + free(path); + return (1); + } return (0); } @@ -630,13 +725,13 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env) assignments_to_env(&assignments, env); ft_vec_free(&redirections, NULL); ft_vec_free(&assignments, NULL); - free_fields(fields); + free_split(fields); return (0); } res = ex_fields(fields, &assignments, &redirections, env); ft_vec_free(&redirections, NULL); ft_vec_free(&assignments, free); - free_fields(fields); + free_split(fields); return (res); }