🔨」 fix(get_path): fixed algorythm for better performances

This commit is contained in:
yosyo
2024-06-13 14:21:45 +02:00
parent f70c506fed
commit 8ec6d94a52
3 changed files with 25 additions and 124 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/06/04 23:07:27 by mmoussou ### ########.fr */
/* Updated: 2024/06/13 14:12:31 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
# include "env.h"
char **get_path(char *path);
char *get_path(char *path, char *cmd);
int exec_split_cmd(t_list *list_cmd, t_env *env);
/**

View File

@ -6,37 +6,12 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/06/10 18:08:11 by mmoussou ### ########.fr */
/* Updated: 2024/06/13 14:11:18 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *get_cmd_global_path(char *cmd, t_env *env)
{
int i;
char *path;
char **path_list;
path = NULL;
path_list = get_path(env_get_value("PATH", env));
if (!path_list)
return (NULL);
i = 0;
while (path_list[i])
{
if (!ft_strcmp(ft_strrchr(path_list[i], '/') + 1, cmd))
{
path = ft_strdup(path_list[i]);
free(cmd);
return (path);
}
i++;
}
free(cmd);
return (NULL);
}
char *get_cmd_local_path(char *cmd, t_env *env)
{
(void) cmd;
@ -50,7 +25,7 @@ int switch_cmd_path(t_cmd *cmd, t_env *env)
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
else if (cmd->cmd[0] != '/')
cmd->cmd = get_cmd_global_path(cmd->cmd, env);
cmd->cmd = get_path(env_get_value("PATH", env), cmd->cmd);
if (!(cmd->cmd))
return (-1);
return (0);

View File

@ -6,123 +6,49 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
/* Updated: 2024/06/10 17:49:21 by mmoussou ### ########.fr */
/* Updated: 2024/06/13 14:19:20 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int add_element_to_list(t_list *list_entry, char *abs_path)
char *check_path_access(char *path, char *cmd)
{
t_list *new_list_entry;
char *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);
}
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) + 3);
abs_path = ft_strjoin(path, "/");
if (!abs_path)
return (-1);
ft_strlcpy(abs_path, path, ft_strlen(path) + 1);
ft_strlcat(abs_path, "/", ft_strlen(abs_path) + 2);
ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(abs_path)
+ ft_strlen(dir_entry->d_name) + 1);
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);
}
}
return (0);
return (NULL);
abs_path = ft_strjoin_free_s1(abs_path, cmd);
if (!abs_path)
return (NULL);
if (access(abs_path, X_OK) == 0)
return (abs_path);
free(abs_path);
return (NULL);
}
int get_path_list(char *path, t_list *list_entry)
{
DIR *path_dir;
struct dirent *dir_entry;
int status;
path_dir = opendir(path);
if (!path_dir)
return (-1);
dir_entry = readdir(path_dir);
while (dir_entry)
{
if (!ft_strncmp(dir_entry->d_name, ".", 2)
|| !ft_strncmp(dir_entry->d_name, "..", 3))
{
dir_entry = readdir(path_dir);
continue ;
}
status = add_path_to_list(path, dir_entry, list_entry);
if (status)
closedir(path_dir);
if (status)
return (-1);
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 *get_path(char *path, char *cmd)
{
char **path_dir;
char **path_list;
t_list *list_entry;
int i;
if (!path)
return (NULL);
list_entry = ft_lstnew(NULL);
path_dir = ft_split(path, ':');
if (!path_dir)
return (NULL);
i = 0;
while (path_dir[i])
{
get_path_list(path_dir[i], list_entry);
path = check_path_access(path_dir[i], cmd);
if (path)
{
ft_free("ac", &path_dir, &cmd);
return (path);
}
i++;
}
path_list = path_trans_list_to_char(list_entry);
ft_free("al", &path_dir, &list_entry);
return (path_list);
ft_free("ac", &path_dir, &cmd);
return (path);
}