🔨」 fix(exec_fn): fixed exec functions, not normed

This commit is contained in:
y-syo
2024-06-10 18:17:12 +02:00
parent 7b84f8fbb1
commit f70c506fed
2 changed files with 14 additions and 16 deletions

View File

@ -6,7 +6,7 @@
/* 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/05 01:08:00 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 18:08:11 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,12 +28,13 @@ char *get_cmd_global_path(char *cmd, t_env *env)
if (!ft_strcmp(ft_strrchr(path_list[i], '/') + 1, cmd)) if (!ft_strcmp(ft_strrchr(path_list[i], '/') + 1, cmd))
{ {
path = ft_strdup(path_list[i]); path = ft_strdup(path_list[i]);
free(cmd);
return (path); return (path);
} }
i++; i++;
} }
free(cmd); free(cmd);
return (path); return (NULL);
} }
char *get_cmd_local_path(char *cmd, t_env *env) char *get_cmd_local_path(char *cmd, t_env *env)
@ -46,10 +47,10 @@ char *get_cmd_local_path(char *cmd, t_env *env)
int switch_cmd_path(t_cmd *cmd, t_env *env) int switch_cmd_path(t_cmd *cmd, t_env *env)
{ {
if (cmd->cmd[0] != '/') if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
cmd->cmd = get_cmd_global_path(cmd->cmd, env);
else 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] != '/')
cmd->cmd = get_cmd_global_path(cmd->cmd, env);
if (!(cmd->cmd)) if (!(cmd->cmd))
return (-1); return (-1);
return (0); return (0);
@ -70,7 +71,7 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t)
if (status == -1) if (status == -1)
return (-1); return (-1);
status = switch_cmd_path(cmd, env_t); status = switch_cmd_path(cmd, env_t);
if (!status) if (status == -1)
return (-1); return (-1);
fork_pid = fork(); fork_pid = fork();
if (fork_pid == -1) if (fork_pid == -1)
@ -96,7 +97,7 @@ int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t)
if (status == -1) if (status == -1)
return (-1); return (-1);
status = switch_cmd_path(cmd, env_t); status = switch_cmd_path(cmd, env_t);
if (!status) if (status == -1)
return (-1); return (-1);
fork_pid = fork(); fork_pid = fork();
if (fork_pid == -1) if (fork_pid == -1)

View File

@ -6,7 +6,7 @@
/* 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/05 01:05:24 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 17:49:21 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,10 +22,7 @@ int add_element_to_list(t_list *list_entry, char *abs_path)
{ {
new_list_entry = ft_lstnew(abs_path); new_list_entry = ft_lstnew(abs_path);
if (!new_list_entry) if (!new_list_entry)
{
free(abs_path);
return (-1); return (-1);
}
ft_lstadd_back(&list_entry, new_list_entry); ft_lstadd_back(&list_entry, new_list_entry);
} }
return (0); return (0);
@ -38,12 +35,13 @@ int add_path_to_list(char *path, struct dirent *dir_entry, t_list *list_entry)
int status; int status;
abs_path = ft_calloc(sizeof(char), strlen(path) abs_path = ft_calloc(sizeof(char), strlen(path)
+ strlen(dir_entry->d_name) + 2); + strlen(dir_entry->d_name) + 3);
if (!abs_path) if (!abs_path)
return (-1); return (-1);
ft_strlcat(abs_path, path, ft_strlen(path)); ft_strlcpy(abs_path, path, ft_strlen(path) + 1);
ft_strlcat(abs_path, "/", 1); ft_strlcat(abs_path, "/", ft_strlen(abs_path) + 2);
ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(dir_entry->d_name)); ft_strlcat(abs_path, dir_entry->d_name, ft_strlen(abs_path)
+ ft_strlen(dir_entry->d_name) + 1);
stat(abs_path, &entry); stat(abs_path, &entry);
if (S_ISREG(entry.st_mode) if (S_ISREG(entry.st_mode)
&& (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) && (entry.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
@ -55,7 +53,6 @@ int add_path_to_list(char *path, struct dirent *dir_entry, t_list *list_entry)
return (-1); return (-1);
} }
} }
free(abs_path);
return (0); return (0);
} }