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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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.
// 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.
// 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
// 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);
}
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);
}
// Execute a program with specific stdin and stdout:
// - inout[0] is stdin
// - inout[0] is stdout
// If call.program doesn't start with "./" or "/", find the program in $PATH.
// (TODO)
// If the program wasn't to found in $PATH, or the fork didn't work,
// write the error (TODO) and return the error status.
static int execute_call(t_memclass mc, t_call call, const int inout[2],
char *const envp[])
{
char **const path = get_path(envp);
pid_t pid;
const char *program_path;
const char **path = get_path(mc, envp);
pid_t pid;
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)
return (127);
pid = fork();
if (pid < 0)
return (1);
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);
}
// 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[])
{
const int inout[] = {command.input_fd, command.output_fd};

View file

@ -6,24 +6,23 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
char **get_path(char *const envp[])
const char **get_path(const t_memclass mc, char *const envp[])
{
int i;
char **re;
const char **re;
char *path;
i = 0;
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
i++;
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
re = ft_split(path, ':');
free(path);
path = envp[i] + 5;
re = (const char **)str_split(fatal_error, mc, path, ":");
return (re);
}