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

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/04/01 01:49:49 by rparodi ### ########.fr # # Updated: 2024/04/13 20:18:33 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -33,6 +33,7 @@ LIB = ./libft/ft_bzero.c \
./libft/ft_strcmp.c \ ./libft/ft_strcmp.c \
./libft/ft_strdup.c \ ./libft/ft_strdup.c \
./libft/ft_strlcpy.c \ ./libft/ft_strlcpy.c \
./libft/ft_strjoin.c \
./libft/ft_strlen.c ./libft/ft_strlen.c
SRC = ./sources/ft_cmd.c \ SRC = ./sources/ft_cmd.c \

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */ /* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */
/* Updated: 2024/04/13 17:05:26 by rparodi ### ########.fr */ /* Updated: 2024/04/13 20:17:16 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,11 +37,12 @@ typedef struct s_utils
} t_utils; } t_utils;
t_i32 main(t_i32 argc, t_str argv[], t_str arge[]); t_i32 main(t_i32 argc, t_str argv[], t_str arge[]);
void ft_other_cmd(t_utils *shcat, t_usize i); void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i);
t_i32 ft_strcmp(const char *s1, const char *s2); t_i32 ft_strcmp(const char *s1, const char *s2);
t_i32 ft_check_type_operators(t_str operators); t_i32 ft_check_type_operators(t_str operators);
t_str *ft_split(t_const_str s, t_i8 c); t_str *ft_split(t_const_str s, t_i8 c);
t_str ft_strdup(t_const_str s); t_str ft_strdup(t_const_str s);
char *ft_strjoin(char const *s1, char const *s2);
void *ft_calloc(t_usize nmemb, t_usize size); void *ft_calloc(t_usize nmemb, t_usize size);
size_t ft_strlen(t_const_str s); size_t ft_strlen(t_const_str s);
t_usize ft_strlcpy(t_str dst, t_const_str src, t_usize size); t_usize ft_strlcpy(t_str dst, t_const_str src, t_usize size);
@ -51,5 +52,6 @@ void ft_free_strs(t_str *strs);
void ft_pwd(void); void ft_pwd(void);
void ft_echo(t_str txt, t_str flag); void ft_echo(t_str txt, t_str flag);
void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status); void ft_exit(t_utils *maiboyerlpb, t_u8 exit_status);
void ft_free(void *ptr);
#endif #endif

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:55:15 by rparodi #+# #+# */
/* Updated: 2024/04/13 20:16:50 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t save_i;
char *str;
i = 0;
str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2)) + 1);
if (!str)
return (NULL);
while (s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
save_i = i;
i = 0;
while (s2[i] != '\0')
{
str[save_i + i] = s2[i];
i++;
}
str[i + save_i] = '\0';
return (str);
}

View file

@ -5,35 +5,77 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 01:00:30 by rparodi #+# #+# */ /* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
/* Updated: 2024/04/01 15:48:48 by rparodi ### ########.fr */ /* Updated: 2024/04/13 20:37:13 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/minishell.h" #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); 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; pid_t pid;
t_i32 options; 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; options = 0;
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
ft_exit(shcat, 1); ft_exit(shcat, 1);
if (pid == 0) if (pid == 0)
ft_exec_cmd(shcat, shcat->strs_input[i], NULL); ft_exec_cmd(shcat, cmd, args);
waitpid(pid, NULL, options); waitpid(pid, NULL, options);
} }

View file

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

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* 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) 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"); printf("Have to redirect in the file\n");
else if (ft_strcmp(operators, ">>") == 0) else if (ft_strcmp(operators, ">>") == 0)
printf("Have to redirect at the end of the file after\n"); 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); return (1);
} }
void ft_check(t_utils *shcat, char **input) void ft_check(t_utils *shcat, char **input)
{ {
t_usize i; t_usize i;
t_usize prev_i;
i = 0; i = 0;
prev_i = 0;
while (input[i] != NULL) while (input[i] != NULL)
{ {
if (ft_check_type_operators(input[i]) == 1) while (ft_check_type_operators(input[i]) == 1)
printf("Operateur\n"); 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 else
{ ft_other_cmd(shcat, i, prev_i);
if (ft_strcmp(input[i], "exit") == 0) prev_i = i;
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);
}
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); shcat = (t_utils *)ft_calloc(sizeof(t_utils), 1);
if (argc == 2) if (argc == 2)
shcat->name_shell = ft_strdup(strcat(argv[1], " > ")); shcat->name_shell = ft_strdup(strcat(argv[1], " \001🐱\002 > "));
else else
shcat->name_shell = ft_strdup("shcat > "); shcat->name_shell = ft_strdup("shcat \001🐱\002 > ");
shcat->envp = arge; shcat->envp = arge;
ft_init_arge(arge, shcat); ft_init_arge(arge, shcat);
ft_take_args(shcat); ft_take_args(shcat);