Add the structure of ex_fields, implement find_exe
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 1 Aug 2024 10:02:07 +0000 (12:02 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 1 Aug 2024 10:02:07 +0000 (12:02 +0200)
The find_exe goes through the PATH variable of environment and looks
whether any of the directories contains the executable entered.

src/execution.c

index e62fee4757911e6e8470e26dc23e62b852beb84f..1fa030233495c16b7c900f8a7c9d4c072b19d44d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <fcntl.h>
 #include <sys/stat.h>
 
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/wait.h>
+
 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);
 }