mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 19:36:50 +01:00
「🔨」 fix(get_path): fixed algorythm for better performances
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
|
/* 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"
|
# 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);
|
int exec_split_cmd(t_list *list_cmd, t_env *env);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,37 +6,12 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
|
/* 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"
|
#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)
|
char *get_cmd_local_path(char *cmd, t_env *env)
|
||||||
{
|
{
|
||||||
(void) cmd;
|
(void) cmd;
|
||||||
@ -50,7 +25,7 @@ int switch_cmd_path(t_cmd *cmd, t_env *env)
|
|||||||
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
|
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
|
||||||
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
|
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
|
||||||
else if (cmd->cmd[0] != '/')
|
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))
|
if (!(cmd->cmd))
|
||||||
return (-1);
|
return (-1);
|
||||||
return (0);
|
return (0);
|
||||||
|
@ -6,123 +6,49 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
|
/* 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"
|
#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;
|
|
||||||
|
|
||||||
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;
|
char *abs_path;
|
||||||
struct stat entry;
|
|
||||||
int status;
|
|
||||||
|
|
||||||
abs_path = ft_calloc(sizeof(char), strlen(path)
|
abs_path = ft_strjoin(path, "/");
|
||||||
+ strlen(dir_entry->d_name) + 3);
|
|
||||||
if (!abs_path)
|
if (!abs_path)
|
||||||
return (-1);
|
return (NULL);
|
||||||
ft_strlcpy(abs_path, path, ft_strlen(path) + 1);
|
abs_path = ft_strjoin_free_s1(abs_path, cmd);
|
||||||
ft_strlcat(abs_path, "/", ft_strlen(abs_path) + 2);
|
if (!abs_path)
|
||||||
ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(abs_path)
|
return (NULL);
|
||||||
+ ft_strlen(dir_entry->d_name) + 1);
|
if (access(abs_path, X_OK) == 0)
|
||||||
stat(abs_path, &entry);
|
return (abs_path);
|
||||||
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);
|
free(abs_path);
|
||||||
return (-1);
|
return (NULL);
|
||||||
}
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_path_list(char *path, t_list *list_entry)
|
char *get_path(char *path, char *cmd)
|
||||||
{
|
|
||||||
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 **path_dir;
|
char **path_dir;
|
||||||
char **path_list;
|
|
||||||
t_list *list_entry;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!path)
|
if (!path)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
list_entry = ft_lstnew(NULL);
|
|
||||||
path_dir = ft_split(path, ':');
|
path_dir = ft_split(path, ':');
|
||||||
if (!path_dir)
|
if (!path_dir)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (path_dir[i])
|
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++;
|
i++;
|
||||||
}
|
}
|
||||||
path_list = path_trans_list_to_char(list_entry);
|
ft_free("ac", &path_dir, &cmd);
|
||||||
ft_free("al", &path_dir, &list_entry);
|
return (path);
|
||||||
return (path_list);
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user