Corrected the exec part

This commit is contained in:
Raphaël 2024-04-15 11:19:01 +02:00
parent b387c00e43
commit e1659508c6
6 changed files with 128 additions and 35 deletions

View file

@ -5,35 +5,77 @@
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 01:00:30 by rparodi #+# #+# */
/* Updated: 2024/04/01 15:48:48 by rparodi ### ########.fr */
/* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
/* Updated: 2024/04/13 20:37:13 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
void ft_exec_cmd(t_utils *utils, char *cmd, char *cmd_args)
char *ft_check_cmds(t_utils *utils, char *command)
{
const char **args = (const char **)ft_split(cmd_args, ' ');
size_t i;
char *str;
if (execve(cmd, (char *const *)args, (char *const *)utils->envp) == -1)
i = 0;
str = NULL;
if (access(command, X_OK) != -1)
return (command);
else if (ft_strlen(command) != 1)
{
ft_free_strs((char **)args);
while (utils->path[i] != NULL)
{
if (str)
ft_free(str);
str = ft_strjoin(utils->path[i], command);
if (str == NULL)
ft_exit(utils, 1);
else if (access(str, F_OK) == -1 || access(str, X_OK) == -1)
i++;
else
return (str);
}
}
return (ft_exit(utils, 1), NULL);
}
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
{
if (execve(cmd, cmd_args, utils->envp) == -1)
{
printf("ERROR >\n execve\n %s\n", cmd);
ft_free_strs((char **)cmd_args);
ft_exit(utils, 1);
}
}
void ft_other_cmd(t_utils *shcat, t_usize i)
void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
{
pid_t pid;
t_i32 options;
t_str *args;
t_usize k;
t_usize tmp;
t_str cmd;
printf("ft_other_cmd = %s\n", shcat->strs_input[i]);
k = prev_i;
tmp = prev_i;
args = (t_str *)malloc(sizeof(t_str) * (i + 2));
while (prev_i < i)
{
printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]);
args[k] = malloc(ft_strlen(shcat->strs_input[prev_i]));
ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i]));
prev_i++;
k++;
}
args[k] = NULL;
cmd = ft_check_cmds(shcat, ft_strjoin("/", args[tmp]));
options = 0;
pid = fork();
if (pid == -1)
ft_exit(shcat, 1);
if (pid == 0)
ft_exec_cmd(shcat, shcat->strs_input[i], NULL);
ft_exec_cmd(shcat, cmd, args);
waitpid(pid, NULL, options);
}

View file

@ -6,12 +6,19 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/29 11:35:51 by rparodi #+# #+# */
/* Updated: 2024/04/01 18:21:22 by rparodi ### ########.fr */
/* Updated: 2024/04/13 20:15:37 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
void ft_free(void *ptr)
{
if (!ptr)
free(ptr);
ptr = NULL;
}
void ft_free_strs(t_str *strs)
{
t_usize i;
@ -19,18 +26,18 @@ void ft_free_strs(t_str *strs)
i = 0;
while (strs[i])
{
free(strs[i]);
ft_free(strs[i]);
i++;
}
free(strs);
ft_free(strs);
}
void ft_free_utils(t_utils *s)
{
if (s->name_shell)
free(s->name_shell);
ft_free(s->name_shell);
if (s->str_input)
free(s->str_input);
ft_free(s->str_input);
if (s->strs_input)
ft_free_strs(s->strs_input);
if (s->path)

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/04/13 17:05:11 by rparodi ### ########.fr */
/* Updated: 2024/04/13 17:47:50 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,9 @@
t_i32 ft_check_type_operators(t_str operators)
{
if (ft_strcmp(operators, ">") == 0)
if (operators == NULL)
printf("End of input");
else if (ft_strcmp(operators, ">") == 0)
printf("Have to redirect in the file\n");
else if (ft_strcmp(operators, ">>") == 0)
printf("Have to redirect at the end of the file after\n");
@ -43,26 +45,26 @@ t_i32 ft_check_type_operators(t_str operators)
return (1);
}
void ft_check(t_utils *shcat, char **input)
void ft_check(t_utils *shcat, char **input)
{
t_usize i;
t_usize prev_i;
i = 0;
prev_i = 0;
while (input[i] != NULL)
{
if (ft_check_type_operators(input[i]) == 1)
printf("Operateur\n");
while (ft_check_type_operators(input[i]) == 1)
i++;
if (ft_strcmp(input[i], "exit") == 0)
ft_exit(shcat, 0);
else if (ft_strcmp(input[i], "pwd") == 0)
ft_pwd();
else if (ft_strcmp(input[i], "echo") == 0)
ft_echo("ECHO MAIS PAS ARG BORDEL !\n", "flag");
else
{
if (ft_strcmp(input[i], "exit") == 0)
ft_exit(shcat, 0);
else if (ft_strcmp(input[i], "pwd") == 0)
ft_pwd();
else if (ft_strcmp(input[i], "echo") == 0)
ft_echo("ECHO MAIS PAS ARG BORDEL !\n", "flag");
else
ft_other_cmd(shcat, i);
}
ft_other_cmd(shcat, i, prev_i);
prev_i = i;
i++;
}
}
@ -119,9 +121,9 @@ t_i32 main(t_i32 argc, t_str argv[], t_str arge[])
shcat = (t_utils *)ft_calloc(sizeof(t_utils), 1);
if (argc == 2)
shcat->name_shell = ft_strdup(strcat(argv[1], " > "));
shcat->name_shell = ft_strdup(strcat(argv[1], " \001🐱\002 > "));
else
shcat->name_shell = ft_strdup("shcat > ");
shcat->name_shell = ft_strdup("shcat \001🐱\002 > ");
shcat->envp = arge;
ft_init_arge(arge, shcat);
ft_take_args(shcat);