[FIX] several things*

fix:
- free_tab(): useless line
- fork_exec(): comp error
change:
- search_path() works differently (doesn't modify path)
- execute_command() is changed accordingly
- get_path() now returns a non-const
This commit is contained in:
mcolonna 2024-04-25 17:25:37 +02:00
parent 7654aeb2d8
commit e3c98f2d22
3 changed files with 27 additions and 24 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/04/24 13:19:28 by mcolonna ### ########.fr */ /* Updated: 2024/04/25 17:30:22 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,6 +70,6 @@ void minishell_error(const char *msg);
// Get the PATH values. // Get the PATH values.
// Return a list of strings ended by NULL. // Return a list of strings ended by NULL.
const char **get_path(const char **envp); char **get_path(const char **envp);
#endif #endif

View file

@ -6,12 +6,13 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/04/24 14:22:13 by jschaft ### ########.fr */ /* Updated: 2024/04/25 17:23:34 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "include.h" #include "include.h"
// Free every element of tab, then tab itself.
static void free_tab(char **tab) static void free_tab(char **tab)
{ {
int i; int i;
@ -22,41 +23,35 @@ static void free_tab(char **tab)
free(tab[i]); free(tab[i]);
i++; i++;
} }
free(tab[i]);
free(tab); free(tab);
} }
// TODO works with only one call (no pipe).
// TODO is t_call.argv ended by NULL?
static void fork_exec(const char *path, t_command command, const char **envp) static void fork_exec(const char *path, t_command command, const char **envp)
{ {
if (command.input_fd != 0) if (command.input_fd != 0)
dup2(command.input_fd, 0); dup2(command.input_fd, 0);
if (command.output_fd != 1) if (command.output_fd != 1)
dup2(command.output_fd, 1); dup2(command.output_fd, 1);
execve(path, command.calls->argv, envp); execve(path, command.calls[0]->argv, envp);
} }
// TODO works with only one call (no pipe).
int execute_command(t_command command, const char **envp) int execute_command(t_command command, const char **envp)
{ {
char **path; const char **path = get_path(envp);
pid_t pid; pid_t pid;
int i; int program_path;
i = 0; program_path = search_path(path);
path = get_path(envp); if (!program_path)
while (path[i] != NULL)
{
path[i] = ft_strjoin(path[i], "/");
path[i] = ft_strjoin(path[i], command.calls->program);
i++;
}
i = search_path(path);
if (i == -1)
return (127); return (127);
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
return (1); return (1);
if (pid == 0) if (pid == 0)
fork_exec((const char *)path[i], command, envp); fork_exec(program_path, command, envp);
free_tab(path); free_tab(path);
return (0); return (0);
} }

View file

@ -6,12 +6,13 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/04/24 14:08:11 by jschaft ### ########.fr */ /* Updated: 2024/04/25 17:30:05 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "include.h" #include "include.h"
// Get the path values from envp.
char **get_path(const char **envp) char **get_path(const char **envp)
{ {
int i; int i;
@ -27,16 +28,23 @@ char **get_path(const char **envp)
return (char **re); return (char **re);
} }
int search_path(char **path) // Search the program in PATH.
// Returns the path of the program to use, or NULL if there is none.
// TODO does it return the good path if there is several possibilities?
const char *search_path(
const t_memclass mc, const char **path, const char *prog)
{ {
int i; const t_memclass mc_in = mem_newclass(fatal_error);
const char *prog2 = str_join(fatal_error, mc_in, "/", prog);
int i;
i = 0; i = 0;
while (path[i] != NULL) while (path[i] != NULL)
{ {
if (access(path[i], X_OK) >= 0) if (access(str_join(fatal_error, mc_in, path[i], prog2), X_OK) >= 0)
return (i); return (i);
i++; i++;
} }
return (-1); mem_freeall(mc_in);
return (NULL);
} }