/* 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;
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;
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);
}
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);
}