libtfise everything

This commit is contained in:
mcolonna 2024-05-01 16:28:34 +02:00
parent f8c50143d6
commit 74564daa52
3 changed files with 25 additions and 37 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/30 15:00:48 by mcolonna ### ########.fr */ /* Updated: 2024/05/01 17:52:52 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -79,7 +79,7 @@ void fatal_error_msg(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.
char **get_path(char *const envp[]); const char **get_path(const t_memclass mc, char *const envp[]);
// Search the program in $PATH. // Search the program in $PATH.
// Returns the path of the program to use, or NULL if there is none. // Returns the path of the program to use, or NULL if there is none.

View file

@ -6,54 +6,43 @@
/* 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/30 13:58:04 by mcolonna ### ########.fr */ /* Updated: 2024/05/01 17:51:49 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "include.h" #include "include.h"
// Free every element of tab, then tab itself. // Execute a program with specific stdin and stdout:
static void free_tab(char **tab) // - inout[0] is stdin
{ // - inout[0] is stdout
int i; // If call.program doesn't start with "./" or "/", find the program in $PATH.
// (TODO)
i = 0; // If the program wasn't to found in $PATH, or the fork didn't work,
while (tab[i] != NULL) // write the error (TODO) and return the error status.
{
free(tab[i]);
i++;
}
free(tab);
}
static void fork_exec(const char *path, t_call call, const int inout[2],
char *const envp[])
{
dup2(inout[0], 0);
dup2(inout[1], 1);
execve(path, call.argv, envp);
}
static int execute_call(t_memclass mc, t_call call, const int inout[2], static int execute_call(t_memclass mc, t_call call, const int inout[2],
char *const envp[]) char *const envp[])
{ {
char **const path = get_path(envp); const char **path = get_path(mc, envp);
pid_t pid; pid_t pid;
const char *program_path; const char *program_path;
program_path = search_path(mc, (const char **)path, call.program); program_path = search_path(mc, path, call.program);
if (!program_path) if (!program_path)
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(program_path, call, inout, envp); {
free_tab(path); dup2(inout[0], 0);
dup2(inout[1], 1);
execve(program_path, call.argv, envp);
}
return (0); return (0);
} }
// TODO works with only one call (no pipe). // TODO works with only one call (no pipe).
// Execute a command, return the error status.
int execute_command(t_memclass mc, t_command command, char *const envp[]) int execute_command(t_memclass mc, t_command command, char *const envp[])
{ {
const int inout[] = {command.input_fd, command.output_fd}; const int inout[] = {command.input_fd, command.output_fd};

View file

@ -6,24 +6,23 @@
/* 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/29 15:54:40 by mcolonna ### ########.fr */ /* Updated: 2024/05/01 17:53:38 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "include.h" #include "include.h"
char **get_path(char *const envp[]) const char **get_path(const t_memclass mc, char *const envp[])
{ {
int i; int i;
char **re; const char **re;
char *path; char *path;
i = 0; i = 0;
while (ft_strncmp(envp[i], "PATH=", 5) != 0) while (ft_strncmp(envp[i], "PATH=", 5) != 0)
i++; i++;
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5); path = envp[i] + 5;
re = ft_split(path, ':'); re = (const char **)str_split(fatal_error, mc, path, ":");
free(path);
return (re); return (re);
} }