Merge exec_command() and parse_command() branchs

This commit is contained in:
mcolonna 2024-04-25 17:47:21 +02:00
commit 6066d0529b
3 changed files with 84 additions and 5 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/04/25 16:53:06 by mcolonna ### ########.fr */
/* Updated: 2024/04/25 17:47:02 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -73,6 +73,6 @@ void fatal_error(const char *msg);
// Get the PATH values.
// Return a list of strings ended by NULL.
const char **get_path(const char **envp);
char **get_path(const char **envp);
#endif

57
src/exec_command.c Normal file
View file

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

View file

@ -6,13 +6,14 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/04/24 13:26:41 by mcolonna ### ########.fr */
/* Updated: 2024/04/25 17:30:05 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
const char **get_path(const char **envp)
// Get the path values from envp.
char **get_path(const char **envp)
{
int i;
char **re;
@ -24,5 +25,26 @@ const char **get_path(const char **envp)
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
re = ft_split(path, ':');
free(path);
return ((const char **)re);
return (char **re);
}
// 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)
{
const t_memclass mc_in = mem_newclass(fatal_error);
const char *prog2 = str_join(fatal_error, mc_in, "/", prog);
int i;
i = 0;
while (path[i] != NULL)
{
if (access(str_join(fatal_error, mc_in, path[i], prog2), X_OK) >= 0)
return (i);
i++;
}
mem_freeall(mc_in);
return (NULL);
}