├── 1 ├── ; ├── f3 ├── f4 ├── _(1) ├── a ├── p=out ├── uuuuu ├── file ├── mini ├── ot ├── leee ├── main.o ├── pipe.o ├── utils.o ├── ft_atoi.o ├── ft_env.o ├── ft_exit.o ├── minishell ├── ft_execve.o ├── ft_export.o ├── ft_split.o ├── ft_split_1.o ├── ft_unset.o ├── minishell.a ├── minishell.o ├── parsing ├── env.o ├── token.o ├── check_fir.o ├── check_sec.o ├── env.c ├── token.c ├── check_fir.c └── check_sec.c ├── redirections.o ├── bultins ├── ft_cd.o ├── ft_echo.o ├── ft_pwd.o ├── ft_cntrl_c.o ├── ft_pwd.c ├── ft_cntrl_c.c ├── ft_echo.c └── ft_cd.c ├── get_next_line.o ├── get_next_line_utils.o ├── abdo ├── tricky.txt ├── ft_env.c ├── main2.c ├── man ├── f2 ├── output ├── Makefile ├── abc ├── _ ├── ft_unset.c ├── ft_exit.c ├── ft_atoi.c ├── redirections.c ├── get_next_line.c ├── pipe.c ├── get_next_line_utils.c ├── ft_split_1.c ├── ft_split.c ├── utils.c ├── main.c ├── ft_export.c ├── includes └── minishell.h ├── ft_execve.c ├── f1 └── minishell.c /;: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /f3: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /f4: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /_(1): -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /a: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /p=out: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /uuuuu: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /file: -------------------------------------------------------------------------------- 1 | g 2 | -------------------------------------------------------------------------------- /mini: -------------------------------------------------------------------------------- 1 | $? = 1 2 | -------------------------------------------------------------------------------- /ot: -------------------------------------------------------------------------------- 1 | @gcc $(NAMA) -o $(NAME) 2 | -------------------------------------------------------------------------------- /leee: -------------------------------------------------------------------------------- 1 | @gcc $(NAMA) -o $(NAME) 2 | -------------------------------------------------------------------------------- /main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/main.o -------------------------------------------------------------------------------- /pipe.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/pipe.o -------------------------------------------------------------------------------- /utils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/utils.o -------------------------------------------------------------------------------- /ft_atoi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_atoi.o -------------------------------------------------------------------------------- /ft_env.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_env.o -------------------------------------------------------------------------------- /ft_exit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_exit.o -------------------------------------------------------------------------------- /minishell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/minishell -------------------------------------------------------------------------------- /ft_execve.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_execve.o -------------------------------------------------------------------------------- /ft_export.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_export.o -------------------------------------------------------------------------------- /ft_split.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_split.o -------------------------------------------------------------------------------- /ft_split_1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_split_1.o -------------------------------------------------------------------------------- /ft_unset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/ft_unset.o -------------------------------------------------------------------------------- /minishell.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/minishell.a -------------------------------------------------------------------------------- /minishell.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/minishell.o -------------------------------------------------------------------------------- /parsing/env.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/parsing/env.o -------------------------------------------------------------------------------- /redirections.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/redirections.o -------------------------------------------------------------------------------- /bultins/ft_cd.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/bultins/ft_cd.o -------------------------------------------------------------------------------- /bultins/ft_echo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/bultins/ft_echo.o -------------------------------------------------------------------------------- /bultins/ft_pwd.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/bultins/ft_pwd.o -------------------------------------------------------------------------------- /get_next_line.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/get_next_line.o -------------------------------------------------------------------------------- /parsing/token.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/parsing/token.o -------------------------------------------------------------------------------- /parsing/check_fir.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/parsing/check_fir.o -------------------------------------------------------------------------------- /parsing/check_sec.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/parsing/check_sec.o -------------------------------------------------------------------------------- /bultins/ft_cntrl_c.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/bultins/ft_cntrl_c.o -------------------------------------------------------------------------------- /get_next_line_utils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amouhtal/minishell/HEAD/get_next_line_utils.o -------------------------------------------------------------------------------- /abdo: -------------------------------------------------------------------------------- 1 | @ar rcs $(NAMA) $(OBJ) 2 | @gcc $(NAMA) -o $(NAME) 3 | @echo 'SUCCESS!' 4 | @rm -rf $(OBJ) 5 | @rm -rf $(NAME) 6 | @rm -rf $(NAMA) 7 | -------------------------------------------------------------------------------- /tricky.txt: -------------------------------------------------------------------------------- 1 | I will be printed in the file tricky.txt 2 | I will be printed in the file tricky.txt 3 | I will be printed in the file tricky.txt 4 | -------------------------------------------------------------------------------- /ft_env.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int ft_env(t_env *env) 4 | { 5 | int check; 6 | 7 | while (env->prev) 8 | env = env->prev; 9 | while (env) 10 | { 11 | check = indexof(env->value, '='); 12 | if ((int)ft_strlen(env->value) != check) 13 | { 14 | ft_putstr_fd(env->value, 1); 15 | ft_putstr_fd("\n", 1); 16 | } 17 | env = env->next; 18 | } 19 | return 1; 20 | } 21 | -------------------------------------------------------------------------------- /main2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern char **environ; 5 | 6 | int main(void) 7 | { 8 | char *args[] = { "ls", "-l", "-a", NULL }; 9 | char *env_args[] = { "PATH=/bin", "USER=me", NULL }; 10 | 11 | // execve(args[0], args, env_args); 12 | // fprintf(stderr, "Oops!\n"); 13 | 14 | environ = env_args; 15 | execve(args[0], &args[0]); 16 | // fprintf(stderr, "Oops again!\n"); 17 | 18 | return -1; 19 | } -------------------------------------------------------------------------------- /bultins/ft_pwd.c: -------------------------------------------------------------------------------- 1 | #include "../includes/minishell.h" 2 | 3 | int ft_pwd(t_minishell **minishell) 4 | { 5 | int i; 6 | char current_dir[PATH_MAX + 1]; 7 | i = 0; 8 | 9 | while (++i < PATH_MAX) 10 | current_dir[i] = 0; 11 | //current_dir = current_dirft_strdup(getcwd(, 100)); 12 | //ft_putstr_fd("current_dir", 1); 13 | if (getcwd(current_dir, 100)) 14 | ft_putstr_fd(current_dir, 1); 15 | else 16 | ft_putstr_fd(current_dir, 1); 17 | ft_putstr_fd("\n", 1); 18 | sign.exit_status = 0; 19 | return (1); 20 | } -------------------------------------------------------------------------------- /man: -------------------------------------------------------------------------------- 1 | 1 2 | bultins 3 | ft_atoi.c 4 | ft_atoi.o 5 | ft_env.c 6 | ft_env.o 7 | ft_execve.c 8 | ft_execve.o 9 | ft_exit.c 10 | ft_exit.o 11 | ft_export.c 12 | ft_export.o 13 | ft_split_1.c 14 | ft_split_1.o 15 | ft_split.c 16 | ft_split.o 17 | ft_unset.c 18 | ft_unset.o 19 | get_next_line.c 20 | get_next_line.o 21 | get_next_line_utils.c 22 | get_next_line_utils.o 23 | includes 24 | libft 25 | main2.c 26 | main.c 27 | main.o 28 | Makefile 29 | man 30 | mini 31 | minishell 32 | minishell.a 33 | minishell.c 34 | minishell.o 35 | parsing 36 | redirections.c 37 | redirections.o 38 | tricky.txt 39 | utils.c 40 | utils.o 41 | -------------------------------------------------------------------------------- /f2: -------------------------------------------------------------------------------- 1 | 1 2 | bultins 3 | f1 4 | f2 5 | ft_atoi.c 6 | ft_atoi.o 7 | ft_env.c 8 | ft_env.o 9 | ft_execve.c 10 | ft_execve.o 11 | ft_exit.c 12 | ft_exit.o 13 | ft_export.c 14 | ft_export.o 15 | ft_split_1.c 16 | ft_split_1.o 17 | ft_split.c 18 | ft_split.o 19 | ft_unset.c 20 | ft_unset.o 21 | get_next_line.c 22 | get_next_line.o 23 | get_next_line_utils.c 24 | get_next_line_utils.o 25 | includes 26 | libft 27 | main2.c 28 | main.c 29 | main.o 30 | Makefile 31 | man 32 | mini 33 | minishell 34 | minishell.a 35 | minishell.c 36 | minishell.o 37 | output 38 | parsing 39 | pipe.c 40 | pipe.o 41 | redirections.c 42 | redirections.o 43 | tricky.txt 44 | utils.c 45 | utils.o 46 | -------------------------------------------------------------------------------- /output: -------------------------------------------------------------------------------- 1 | NAME = minishell 2 | 3 | NAMA = minishell.a 4 | 5 | SRC = main.c utils.c get_next_line.c get_next_line_utils.c ft_split.c ft_split_1.c parsing/env.c \ 6 | parsing/token.c parsing/check_fir.c parsing/check_sec.c minishell.c bultins/ft_cd.c bultins/ft_cntrl_c.c bultins/ft_pwd.c \ 7 | ft_exit.c ft_unset.c ft_export.c ft_env.c ft_atoi.c bultins/ft_echo.c ft_execve.c redirections.c pipe.c 8 | 9 | 10 | OBJ = $(SRC:.c=.o) 11 | 12 | #CFLAGS = -Wall -Wextra -Werror 13 | 14 | all : $(NAME) 15 | 16 | $(NAME) : $(OBJ) 17 | @ar rcs $(NAMA) $(OBJ) 18 | @gcc $(NAMA) -o $(NAME) 19 | @echo 'SUCCESS!' 20 | clean : 21 | @rm -rf $(OBJ) 22 | fclean : clean 23 | @rm -rf $(NAME) 24 | @rm -rf $(NAMA) 25 | re : fclean all -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME = minishell 2 | 3 | NAMA = minishell.a 4 | 5 | SRC = main.c utils.c get_next_line.c get_next_line_utils.c ft_split.c ft_split_1.c parsing/env.c \ 6 | parsing/token.c parsing/check_fir.c parsing/check_sec.c minishell.c bultins/ft_cd.c bultins/ft_cntrl_c.c bultins/ft_pwd.c \ 7 | ft_exit.c ft_unset.c ft_export.c ft_env.c ft_atoi.c bultins/ft_echo.c ft_execve.c redirections.c pipe.c 8 | 9 | 10 | OBJ = $(SRC:.c=.o) 11 | 12 | #CFLAGS = -Wall -Wextra -Werror 13 | 14 | all : $(NAME) 15 | 16 | $(NAME) : $(OBJ) 17 | @ar rcs $(NAMA) $(OBJ) 18 | @gcc $(NAMA) -o $(NAME) 19 | @echo 'SUCCESS!' 20 | clean : 21 | @rm -rf $(OBJ) 22 | fclean : clean 23 | @rm -rf $(NAME) 24 | @rm -rf $(NAMA) 25 | re : fclean all -------------------------------------------------------------------------------- /abc: -------------------------------------------------------------------------------- 1 | < 2 | _ 3 | 1 4 | abc 5 | abdo 6 | bultins 7 | f1 8 | f2 9 | f3 10 | f4 11 | file 12 | ft_atoi.c 13 | ft_atoi.o 14 | ft_env.c 15 | ft_env.o 16 | ft_execve.c 17 | ft_execve.o 18 | ft_exit.c 19 | ft_exit.o 20 | ft_export.c 21 | ft_export.o 22 | ft_split_1.c 23 | ft_split_1.o 24 | ft_split.c 25 | ft_split.o 26 | ft_unset.c 27 | ft_unset.o 28 | get_next_line.c 29 | get_next_line.o 30 | get_next_line_utils.c 31 | get_next_line_utils.o 32 | includes 33 | leee 34 | main2.c 35 | main.c 36 | main.o 37 | Makefile 38 | man 39 | mini 40 | minishell 41 | minishell.a 42 | minishell.c 43 | minishell.o 44 | ot 45 | output 46 | parsing 47 | pipe.c 48 | pipe.o 49 | p=out 50 | redirections.c 51 | redirections.o 52 | tricky.txt 53 | utils.c 54 | utils.o 55 | uuuuu 56 | -------------------------------------------------------------------------------- /_: -------------------------------------------------------------------------------- 1 | < 2 | _ 3 | 1 4 | abc 5 | abdo 6 | bultins 7 | f1 8 | f2 9 | f3 10 | f4 11 | file 12 | ft_atoi.c 13 | ft_atoi.o 14 | ft_env.c 15 | ft_env.o 16 | ft_execve.c 17 | ft_execve.o 18 | ft_exit.c 19 | ft_exit.o 20 | ft_export.c 21 | ft_export.o 22 | ft_split_1.c 23 | ft_split_1.o 24 | ft_split.c 25 | ft_split.o 26 | ft_unset.c 27 | ft_unset.o 28 | get_next_line.c 29 | get_next_line.o 30 | get_next_line_utils.c 31 | get_next_line_utils.o 32 | includes 33 | leee 34 | main2.c 35 | main.c 36 | main.o 37 | Makefile 38 | man 39 | mini 40 | minishell 41 | minishell.a 42 | minishell.c 43 | minishell.o 44 | ot 45 | output 46 | parsing 47 | pipe.c 48 | pipe.o 49 | p=out 50 | redirections.c 51 | redirections.o 52 | tricky.txt 53 | utils.c 54 | utils.o 55 | uuuuu 56 | 57 | $? = 0 58 | -------------------------------------------------------------------------------- /ft_unset.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int ft_unset_variable(t_env *env, char *arg_var) 4 | { 5 | struct env *tmp_n; 6 | t_env *tmp_env; 7 | char *var; 8 | 9 | while (env->prev) 10 | env = env->prev; 11 | while (env) 12 | { 13 | tmp_n = env->next; 14 | tmp_env = env; 15 | var = ft_substr((char *)env->value, 0, indexof(env->value, '=')); 16 | if (!ft_strcmp(var, arg_var)) 17 | { 18 | env = env->prev; 19 | env->next = tmp_env->next; 20 | env = tmp_env->next; 21 | if (tmp_env->next) 22 | env->prev = tmp_env->prev; 23 | free(tmp_env); 24 | return (1); 25 | } 26 | env = env->next; 27 | } 28 | return (0); 29 | } 30 | 31 | void ft_unset(t_env *env, t_minishell **minishell) 32 | { 33 | int i; 34 | char **args; 35 | 36 | args = get_args(minishell); 37 | i = 0; 38 | while (args[i]) 39 | { 40 | ft_unset_variable(env, args[i]); 41 | i++; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /parsing/env.c: -------------------------------------------------------------------------------- 1 | #include"../includes/minishell.h" 2 | t_env *new_env(void *value) 3 | { 4 | t_env *new; 5 | new = malloc(sizeof(t_env)); 6 | new->next = NULL; 7 | new->prev = NULL; 8 | new->value = ft_strdup(value); 9 | return(new); 10 | } 11 | 12 | t_env *add_env(t_env *list, void *value) 13 | { 14 | t_env *new; 15 | if (!list || !value) 16 | return (NULL); 17 | while(list->next) 18 | list = list->next; 19 | new = malloc(sizeof(t_env)); 20 | new->next = NULL; 21 | new->prev = list; 22 | new->value = ft_strdup(value); 23 | list->next = new; 24 | // puts("here\n"); 25 | return (list); 26 | } 27 | t_env *get_env(char **env,char **argv,int argc) 28 | { 29 | t_env *list; 30 | int i; 31 | if(argc < 0) 32 | argv[0]=NULL; 33 | i = 1; 34 | list = new_env(env[0]); 35 | while (env[i]) 36 | { 37 | list = add_env(list, env[i]); 38 | i++; 39 | } 40 | return (list); 41 | } -------------------------------------------------------------------------------- /bultins/ft_cntrl_c.c: -------------------------------------------------------------------------------- 1 | #include "../includes/minishell.h" 2 | 3 | void ft_cntr_c(int sig) 4 | { 5 | int status; 6 | int wpid; 7 | while ((wpid = wait(&status)) > 0); 8 | sign.exit_status = 0; 9 | if (WTERMSIG(status) == SIGINT) 10 | { 11 | ft_putstr_fd("\b\b \b\b", STDERR); 12 | return ; 13 | } 14 | if (sig == SIGINT) 15 | { 16 | ft_putstr_fd("\b\b ", 2); 17 | ft_putstr_fd("\n\033[1;34m$minishell$~> 👾\033[0m", 2); 18 | } 19 | return ; 20 | signal(sig, ft_cntr_c); 21 | } 22 | 23 | void ft_cntr_d(int signum) 24 | { 25 | int status; 26 | char *nbr; 27 | int wpid; 28 | 29 | nbr = ft_itoa(signum); 30 | while ((wpid = wait(&status)) > 0); 31 | sign.exit_status = 0; 32 | printf("%d\n",sign.pid); 33 | if (sign.pid == 0) 34 | { 35 | ft_putstr_fd("\b\b", 2); 36 | ft_putstr_fd("QUIT : ", STDERR); 37 | ft_putstr_fd(nbr, STDERR); 38 | ft_putstr_fd("\n", STDERR); 39 | return ; 40 | }else 41 | ft_putstr_fd("\b\b \b\b", STDERR); 42 | signal(signum, ft_cntr_d); 43 | } 44 | -------------------------------------------------------------------------------- /bultins/ft_echo.c: -------------------------------------------------------------------------------- 1 | #include "../includes/minishell.h" 2 | 3 | int nb_args(char **args) 4 | { 5 | int size; 6 | 7 | size = 0; 8 | while (args[size]) 9 | size++; 10 | return (size); 11 | } 12 | 13 | // int tab_rows(t_node **token) 14 | // { 15 | // int i; 16 | // t_node *tmp; 17 | 18 | // tmp = *token; 19 | // i = 0; 20 | // while (tmp && tmp->next && tmp->type == ARG) 21 | // { 22 | // i++; 23 | // tmp = tmp->next; 24 | // } 25 | // if (tmp && tmp->type == ARG) 26 | // i++; 27 | // return (i); 28 | // } 29 | 30 | int ft_echo(t_minishell **minishell) 31 | { 32 | int i; 33 | int n_option; 34 | int cpt; 35 | char **args; 36 | 37 | 38 | args = get_args(minishell); 39 | 40 | i = 0; 41 | n_option = 0; 42 | cpt = 0; 43 | while (args[i] && ft_check_n(args[i], "-n") == 0) 44 | { 45 | n_option = 1; 46 | i++; 47 | } 48 | while (args[i]) 49 | { 50 | ft_putstr_fd(args[i], 1); 51 | if (args[i + 1] && args[i][0] != '\0') 52 | write(1, " ", 1); 53 | i++; 54 | } 55 | if (n_option == 0) 56 | write(1, "\n", 1); 57 | sign.exit_status = 0; 58 | return (1); 59 | } 60 | -------------------------------------------------------------------------------- /parsing/token.c: -------------------------------------------------------------------------------- 1 | #include "../includes/minishell.h" 2 | 3 | t_token *new_token() 4 | { 5 | t_token *tmp; 6 | tmp = malloc(sizeof(t_token)); 7 | tmp->type = 0; 8 | tmp->str = NULL; 9 | tmp->next = NULL; 10 | tmp->prev = NULL; 11 | return(tmp); 12 | } 13 | t_token *add_arg_cmd(t_token *first,char *str) 14 | { 15 | t_token *new; 16 | int i; 17 | int j; 18 | 19 | j = 0; 20 | i = 0; 21 | new = new_token(); 22 | while (first->prev) 23 | first = first->prev; 24 | while (first->next) 25 | first = first->next; 26 | if(str[0] == '|') 27 | new->type = PIPE; 28 | else if(str[0] == '<' && str[1] == '\0') 29 | new->type = LESS; 30 | else if(str[0] == '>' && str[1] == '\0') 31 | new->type = GREAT; 32 | else if(str[0] == '>' && str[1] == '>') 33 | new->type = DOUBLE_GREAT; 34 | else if(str[0] == ';') 35 | new->type = SEP; 36 | else 37 | { 38 | if(first->type == PIPE || first->type == SEP ) 39 | new->type=CMD; 40 | else 41 | new->type = ARG; 42 | } 43 | new->str = ft_strdup(str); 44 | new->prev= first; 45 | new->next =NULL; 46 | first->next = new; 47 | return(first); 48 | } -------------------------------------------------------------------------------- /ft_exit.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int ft_strisnum(const char *str) 4 | { 5 | int i; 6 | 7 | i = 0; 8 | if (str == NULL) 9 | return (0); 10 | if (str[0] == '-') 11 | i++; 12 | while (str[i]) 13 | { 14 | if (str[i] < '0' || str[i] > '9') 15 | return (0); 16 | i++; 17 | } 18 | return (1); 19 | } 20 | 21 | int ft_exit(char *cmd, t_minishell **minishell) 22 | { 23 | char **arg; 24 | int exit_nbr; 25 | int i; 26 | 27 | i = 0; 28 | if(cmd) 29 | cmd =NULL; 30 | arg = get_args(minishell); 31 | exit_nbr = 0; 32 | if (arg[1]) 33 | ft_putstr_fd("exit: too many arguments\n", 2); 34 | else 35 | { 36 | if (arg[0]) 37 | { 38 | if (ft_strisnum(arg[0]) == 0) 39 | { 40 | ft_putstr_fd("exit: ", 2); 41 | ft_putstr_fd(arg[0], 2); 42 | ft_putstr_fd(": numeric argument required\n", 2); 43 | exit_nbr = 255; 44 | } 45 | else 46 | exit_nbr = ft_atoi(arg[0]); 47 | } 48 | /*puts(" //// \t ////"); 49 | puts(" (°>°) \t (°>°)"); 50 | puts("__| |__ \t__| |__"); 51 | puts("-- -- \t-- -- "); 52 | puts(" ) | ) \t ) | )"); 53 | puts("( ( ) \t( ( )"); 54 | puts(" ||| \t |||"); 55 | puts(" > > \t > >");*/ 56 | exit(exit_nbr); 57 | } 58 | 59 | //printf("%s\n", arg[0]); 60 | 61 | //if( if_pi) 62 | return 0; 63 | } -------------------------------------------------------------------------------- /ft_atoi.c: -------------------------------------------------------------------------------- 1 | 2 | #include "includes/minishell.h" 3 | 4 | int ft_atoi(const char *str) 5 | { 6 | int i; 7 | int res; 8 | int sign; 9 | 10 | i = 0; 11 | res = 0; 12 | sign = 1; 13 | while ((str[i] == ' ' || str[i] == '\n' || str[i] == '\t' || str[i] == '\r' 14 | || str[i] == '\f' || str[i] == '\v') && str[i]) 15 | i++; 16 | if (str[i] == '-') 17 | sign = -1; 18 | if (str[i] == '-' || str[i] == '+') 19 | i++; 20 | while (str[i] >= '0' && str[i] <= '9' && str[i]) 21 | { 22 | res = (res * 10) + (str[i] - '0'); 23 | i++; 24 | } 25 | return (res * sign); 26 | } 27 | 28 | static int ft_count(long n) 29 | { 30 | int count; 31 | count = 0; 32 | if (n < 0) 33 | { 34 | n = n * (-1); 35 | count++; 36 | } 37 | while (n > 9) 38 | { 39 | n = n / 10; 40 | count++; 41 | } 42 | count++; 43 | return (count); 44 | } 45 | 46 | char *ft_itoa(int n) 47 | { 48 | int count; 49 | char *str; 50 | int sign; 51 | long l; 52 | l = n; 53 | sign = 0; 54 | count = ft_count(l); 55 | if (l < 0) 56 | { 57 | sign = 1; 58 | l = l * (-1); 59 | } 60 | str = (char *)malloc((count + 1) * sizeof(char)); 61 | if (str == NULL) 62 | return (NULL); 63 | str[count--] = '\0'; 64 | while (count >= 0) 65 | { 66 | str[count--] = (l % 10) + '0'; 67 | l = l / 10; 68 | } 69 | if (sign == 1) 70 | str[0] = '-'; 71 | return (str); 72 | } -------------------------------------------------------------------------------- /redirections.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | void ft_great(t_minishell *minishell) 4 | { 5 | t_token *tmp; 6 | 7 | close(minishell->fd_out); 8 | tmp = minishell->token; 9 | while (tmp && tmp->type != GREAT) 10 | tmp = tmp->next; 11 | while (tmp && tmp->type == GREAT) 12 | { 13 | tmp = tmp->next; 14 | minishell->fd_out = open(tmp->str , O_CREAT | O_WRONLY | O_TRUNC, 0664); 15 | while (tmp && tmp->type == ARG) 16 | tmp = tmp->next; 17 | } 18 | dup2(minishell->fd_out, STDOUT); 19 | 20 | } 21 | 22 | void ft_append(t_minishell *minishell) 23 | { 24 | t_token *tmp; 25 | 26 | close(minishell->fd_out); 27 | tmp = minishell->token; 28 | while (tmp && tmp->type != DOUBLE_GREAT) 29 | tmp = tmp->next; 30 | while (tmp && tmp->type == DOUBLE_GREAT) 31 | { 32 | tmp = tmp->next; 33 | minishell->fd_out = open(tmp->str , O_CREAT | O_WRONLY | O_APPEND, 0664); 34 | while (tmp && tmp->type == ARG) 35 | tmp = tmp->next; 36 | } 37 | dup2(minishell->fd_out, STDOUT); 38 | } 39 | 40 | void ft_input(t_minishell *minishell) 41 | { 42 | t_token *tmp; 43 | close(minishell->fd_in); 44 | tmp = minishell->token; 45 | while (tmp && tmp->type != LESS) 46 | tmp = tmp->next; 47 | while (tmp && tmp->type == LESS) 48 | { 49 | tmp = tmp->next; 50 | if (tmp && (tmp->type == GREAT)) 51 | { 52 | //dup2(minishell->stdo, STDOUT); 53 | tmp = tmp->next; 54 | } 55 | minishell->fd_in = open(tmp->str, O_RDONLY, S_IRWXU); 56 | if (minishell->fd_in == -1) 57 | ft_putstr_fd("Error", 2); 58 | while (tmp && tmp->type == ARG) 59 | tmp = tmp->next; 60 | } 61 | dup2(minishell->fd_in, 0); 62 | } 63 | -------------------------------------------------------------------------------- /bultins/ft_cd.c: -------------------------------------------------------------------------------- 1 | # include "../includes/minishell.h" 2 | 3 | int ft_count_args(char **args) 4 | { 5 | int i; 6 | 7 | i = 0; 8 | while (args[i]) 9 | i++; 10 | return (i); 11 | } 12 | 13 | int ft_cd( t_env *env, t_minishell **minishell) 14 | { 15 | char *oldpath; 16 | char *pwd; 17 | char *cpwd; 18 | char cwd[PATH_MAX + 1]; 19 | char *path; 20 | char **arg; 21 | int i = 0; 22 | struct stat stt; 23 | 24 | path = malloc(PATH_MAX + 1); 25 | cpwd = malloc(PATH_MAX + 1); 26 | 27 | while (++i < PATH_MAX) 28 | { 29 | path[i] = 0; 30 | cpwd[i] = 0; 31 | } 32 | arg = get_args(minishell); 33 | getcwd(cwd, sizeof(cwd)); 34 | oldpath = ft_strjoin("OLDPWD=",cwd); 35 | sign.exit_status = 0; 36 | 37 | if (arg[0]) 38 | { 39 | if (!ft_strcmp(arg[0], "-")) 40 | { 41 | printf("<== \n"); 42 | path = get_env_var(env, "OLDPWD", 1); 43 | if (path[0] == '\0') 44 | ft_putstr_fd("OLDPWD not set\n", STDERR); 45 | } 46 | else if (ft_count_args(arg) > 1) 47 | { 48 | ft_putstr_fd("bash: cd: too many arguments\n", STDERR); 49 | sign.exit_status = 1; 50 | } 51 | else 52 | path = arg[0]; 53 | } 54 | else 55 | path = get_env_var(env, "HOME", 1); 56 | 57 | /* if (lstat(path, &stt) == -1) 58 | ft_putstr_fd("access denied",2); 59 | if (!S_ISDIR(stt.st_mode)) 60 | ft_putstr_fd("\\\\\\\\msg", 2);*/ 61 | if (chdir(path) != 0 && ft_count_args(arg) < 2) 62 | { 63 | ft_putstr_fd("changing directory failed\n", STDERR); 64 | sign.exit_status = 1; 65 | } 66 | else 67 | { 68 | 69 | pwd = ft_strjoin("PWD=",getcwd(cpwd, 100)); 70 | if (valid_arg(env, oldpath) /*|| valid_arg(env, pwd)*/) 71 | { 72 | //env = add_env(env, pwd); 73 | //env = add_env(env, oldpath); 74 | } 75 | } 76 | return (1); 77 | } 78 | -------------------------------------------------------------------------------- /get_next_line.c: -------------------------------------------------------------------------------- 1 | 2 | #include "includes/minishell.h" 3 | 4 | size_t ft_strlen(const char *str) 5 | { 6 | size_t i; 7 | 8 | i = 0; 9 | while (str[i]) 10 | i++; 11 | return (i); 12 | } 13 | 14 | char *ft_chr(const char *s, int c) 15 | { 16 | char c2; 17 | char *str; 18 | int i; 19 | 20 | c2 = (char)c; 21 | str = (char *)s; 22 | i = 0; 23 | if (c2 == '\0') 24 | return (str + ft_strlen(str)); 25 | while (str[i]) 26 | { 27 | if (str[i] == c2) 28 | return (str + i); 29 | i++; 30 | } 31 | return (NULL); 32 | } 33 | 34 | char *str_get(char *rest, int i, char **line) 35 | { 36 | char *ptr; 37 | 38 | while (rest[i]) 39 | { 40 | if (rest[i] == '\n') 41 | { 42 | free(*line); 43 | *line = ft_substr(rest, 0, i); 44 | ptr = rest; 45 | rest = ft_strdup(rest + i + 1); 46 | free(ptr); 47 | return (rest); 48 | } 49 | i++; 50 | } 51 | if (rest[0] != '\0') 52 | { 53 | free(*line); 54 | *line = ft_substr(rest, 0, i); 55 | ptr = rest; 56 | free(ptr); 57 | return (NULL); 58 | } 59 | free(rest); 60 | return (NULL); 61 | } 62 | 63 | int get_next_line(int fd, char **line) 64 | { 65 | char *buff; 66 | static char *rest; 67 | int j; 68 | char *ptr; 69 | 70 | if (BUFFER_SIZE <= 0 || fd < 0 || fd > 4864 || read(fd, NULL, 0) < 0 71 | || !line || !(buff = (char *)malloc((sizeof(char)) * BUFFER_SIZE + 1))) 72 | return (-1); 73 | *line = ft_strdup("\0"); 74 | if (rest == NULL) 75 | if (!(rest = ft_calloc(1, 1))) 76 | return (-1); 77 | while ((!ft_chr(rest, '\n')) && (j = read(fd, buff, BUFFER_SIZE)) > 0) 78 | { 79 | buff[j] = '\0'; 80 | ptr = rest; 81 | rest = ft_strjoin(rest, buff); 82 | free(ptr); 83 | } 84 | free(buff); 85 | if (j < 0) 86 | free(rest); 87 | if (j < 0) 88 | return (-1); 89 | return (((rest = str_get(rest, 0, line)) == NULL) ? 0 : 1); 90 | } 91 | -------------------------------------------------------------------------------- /1: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #define nbrofpipes 3 6 | /* 7 | * loop over commands by sharing 8 | * pipes. 9 | */ 10 | 11 | static void pipeline(char ***cmd) 12 | { 13 | 14 | int pipefds[2 * nbrofpipes]; 15 | pid_t pid; 16 | int i; 17 | int status; 18 | 19 | for( i = 0; i < nbrofpipes; i++ ) 20 | { 21 | if( pipe(pipefds + i*2) < 0 ) 22 | { 23 | exit(0); 24 | } 25 | } 26 | 27 | int j = 0; 28 | while (*cmd != NULL) 29 | { 30 | pid = fork(); 31 | if( pid == 0 ){ 32 | /* child gets input from the previous command, 33 | if it's not the first command */ 34 | if(*(cmd + 1) != NULL) 35 | { 36 | if(dup2(pipefds[(j + 1)], 1) < 0) 37 | { 38 | exit(0); 39 | } 40 | } 41 | /* child outputs to next command, if it's not 42 | the last command */ 43 | if(j != 0) 44 | { 45 | if( dup2(pipefds[j - 2], 0) < 0 ) 46 | { 47 | exit(0); 48 | } 49 | } 50 | for(i = 0; i < 2* nbrofpipes; i++){ 51 | close(pipefds[i]); 52 | } 53 | 54 | execvp((*cmd)[0], *cmd); 55 | // perror and exit 56 | } 57 | else if( pid < 0 ) 58 | { 59 | exit(0); 60 | } 61 | cmd++; 62 | j += 2; 63 | } 64 | for(i = 0; i < 2 * nbrofpipes; i++){ 65 | close(pipefds[i]); 66 | } 67 | 68 | for(i = 0; i < nbrofpipes + 1; i++) 69 | wait(&status); 70 | } 71 | 72 | /* 73 | * Compute multi-pipeline based 74 | * on a command list. 75 | */ 76 | int main(int argc, char *argv[]) 77 | { 78 | char *ls[] = {"cat", NULL}; 79 | char *rev[] = {"ls", NULL}; 80 | char *nl[] = {"nl", NULL}; 81 | char *cat[] = {"cat", "-e", NULL}; 82 | char **cmd[] = {ls, rev, nl, cat, NULL}; 83 | 84 | pipeline(cmd); 85 | 86 | return (0); 87 | } -------------------------------------------------------------------------------- /pipe.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | void ft_close_fds(t_minishell *minishell, int *pipefds) 4 | { 5 | int i; 6 | int status; 7 | 8 | i = 0; 9 | while (i < 2 * minishell->nbrofpipe2) 10 | { 11 | close(pipefds[i]); 12 | i++; 13 | } 14 | i = 0; 15 | if (!minishell->nbrofpipe2) 16 | waitpid(sign.pid, &status, 0); 17 | while (i < minishell->nbrofpipe2 ) 18 | { 19 | waitpid(minishell->pid_list[i], &minishell->status, 0); 20 | if (WIFEXITED(minishell->status)) 21 | sign.exit_status = WEXITSTATUS(minishell->status); 22 | i++; 23 | } 24 | } 25 | 26 | void ft_pipe(t_minishell *minishell, t_env *env) 27 | { 28 | int pipefds[minishell->nbrofpipe2 * 2]; 29 | int i = 0; 30 | int status; 31 | int therepipe = 1; 32 | int cpt = 0; 33 | while (i < minishell->nbrofpipe2) 34 | { 35 | if(pipe(pipefds + i * 2) < 0 ) 36 | exit(0); 37 | i++; 38 | } 39 | int j = 0; 40 | while(therepipe) 41 | { 42 | therepipe = if_pipe_next(minishell->token) == 0 ? 0 : 1; 43 | sign.pid = fork(); 44 | if(sign.pid == 0 ) 45 | { 46 | if(if_pipe_next(minishell->token) == PIPE) 47 | if(dup2(pipefds[(j + 1)], 1) < 0) 48 | exit(0); 49 | if(j != 0) 50 | if(dup2(pipefds[j - 2], 0) < 0) 51 | exit(0); 52 | i = 0; 53 | while (i < 2 * minishell->nbrofpipe2) 54 | close(pipefds[i++]); 55 | if (!ft_bultins(minishell->cmd, &minishell, env)) 56 | { 57 | ft_execve(env, &minishell, minishell->cmd); 58 | if(minishell->cmd != NULL) 59 | { 60 | free(minishell->cmd); 61 | minishell->cmd = NULL; 62 | } 63 | } 64 | } 65 | else if(sign.pid < 0) 66 | exit(0); 67 | j += 2; 68 | minishell->pid_list[cpt++] = sign.pid; 69 | while (minishell->token && (minishell->token->type == CMD || minishell->token->type == ARG || minishell->token->type == LESS)) 70 | minishell->token = minishell->token->next; 71 | if (minishell->token->type == PIPE) 72 | minishell->token = minishell->token->next; 73 | get_cmd(minishell); 74 | } 75 | ft_close_fds(minishell, pipefds); 76 | } 77 | -------------------------------------------------------------------------------- /get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | 2 | #include "includes/minishell.h" 3 | 4 | char *ft_strdup(const char *s1) 5 | { 6 | int i; 7 | char *str; 8 | 9 | i = 0; 10 | while (s1[i]) 11 | i++; 12 | if (!(str = (char*)malloc((i + 1) * sizeof(char)))) 13 | return (NULL); 14 | i = 0; 15 | while (s1[i]) 16 | { 17 | str[i] = s1[i]; 18 | i++; 19 | } 20 | str[i] = '\0'; 21 | return (str); 22 | } 23 | 24 | char *line_of_env(const char *s1) 25 | { 26 | int i; 27 | char *str; 28 | int j; 29 | 30 | j = 0; 31 | i = 0; 32 | while (s1[i]) 33 | i++; 34 | if (!(str = (char*)malloc((i + 3) * sizeof(char)))) 35 | return (NULL); 36 | i = 0; 37 | while (s1[i]) 38 | { 39 | str[j] = s1[i]; 40 | if (s1[i] == '=' && i == j) 41 | { 42 | j++; 43 | str[j] = '"'; 44 | } 45 | i++; 46 | j++; 47 | } 48 | str[j++] = '"'; 49 | str[j] = '\0'; 50 | return (str); 51 | } 52 | 53 | char *ft_substr(char const *s, unsigned int start, size_t len) 54 | { 55 | char *ab; 56 | size_t i; 57 | 58 | i = 0; 59 | if (s == 0) 60 | return (NULL); 61 | // if (start > len) 62 | // return (ft_strdup("")); 63 | ab = (char *)malloc((len + 1) * sizeof(char)); 64 | if (ab == NULL) 65 | return (NULL); 66 | while (s[start + i] && i < len) 67 | { 68 | ab[i] = s[start + i]; 69 | i++; 70 | } 71 | ab[i] = '\0'; 72 | return (ab); 73 | } 74 | 75 | char *ft_strjoin(char const *s1, char const *s2) 76 | { 77 | char *mm; 78 | size_t i; 79 | size_t j; 80 | size_t k; 81 | 82 | j = 0; 83 | k = 0; 84 | if (s1 && s2) 85 | { 86 | i = ft_strlen(s1) + ft_strlen(s2); 87 | mm = (char*)malloc((i + 1) * sizeof(char)); 88 | if (mm == NULL) 89 | return (NULL); 90 | i = 0; 91 | while (s1[i]) 92 | mm[j++] = s1[i++]; 93 | while (s2[k]) 94 | mm[j++] = s2[k++]; 95 | mm[j] = '\0'; 96 | return (mm); 97 | } 98 | return (NULL); 99 | } 100 | 101 | void *ft_calloc(size_t count, size_t size) 102 | { 103 | void *hep; 104 | 105 | hep = (void*)malloc(count * size); 106 | if (hep == NULL) 107 | return (NULL); 108 | ft_bzero(hep, count * size); 109 | return (hep); 110 | } 111 | 112 | void ft_bzero(void *s, size_t n) 113 | { 114 | size_t cont; 115 | char *s2; 116 | 117 | cont = 0; 118 | s2 = (char *)s; 119 | while (cont < n) 120 | { 121 | s2[cont] = 0; 122 | cont++; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /ft_split_1.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split_1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mel-hamr +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/10/22 18:51:32 by amouhtal #+# #+# */ 9 | /* Updated: 2021/01/26 12:04:34 by mel-hamr ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "includes/minishell.h" 14 | 15 | int skip_quots(char *str, char c, int i) 16 | { 17 | int j; 18 | 19 | j = i + 1; 20 | while (str[j]) 21 | { 22 | if(str[j] == c ) 23 | return(j); 24 | j++; 25 | } 26 | return(j); 27 | } 28 | 29 | static int countwords(char *ptr, char c, char c1) 30 | { 31 | int i; 32 | int count; 33 | 34 | i = 0; 35 | count = 0; 36 | while (ptr[i]) 37 | { 38 | if ((ptr[i] == c || ptr[i] == c1))// && ptr[i+1] != '\0' ) 39 | count++; 40 | i++; 41 | } 42 | return (count + 1); 43 | } 44 | 45 | static char *affectation(char c, char c1, int *ii, char *ptr) 46 | { 47 | int k; 48 | char *p; 49 | int n; 50 | 51 | k = 0; 52 | n = 0; 53 | while ((ptr[*ii + n] != c && ptr[*ii + n] != c1) && ptr[*ii + n]) 54 | { 55 | if (ptr[*ii + n] == '\'' || ptr[*ii + n] == '"') 56 | n = skip_quots(ptr, ptr[*ii + n], *ii + n); 57 | n++; 58 | } 59 | if (!(p = (char *)malloc(sizeof(char) * (n + 1)))) 60 | return (NULL); 61 | while (n > k) 62 | { 63 | p[k] = ptr[k + *ii]; 64 | k++; 65 | } 66 | p[k] = '\0'; 67 | *ii = n + *ii; 68 | return (p); 69 | } 70 | 71 | static void free_news(char **news) 72 | { 73 | int i; 74 | 75 | i = 0; 76 | while (news[i]) 77 | { 78 | free(news[i]); 79 | i++; 80 | } 81 | } 82 | 83 | char **ft_split_1(char *s, char c, char c1) 84 | { 85 | int index; 86 | char **news; 87 | int i; 88 | int j; 89 | 90 | j = 0; 91 | if (!(news = (char **)malloc(sizeof(char*) * (countwords(s, c, c1) + 1)))) 92 | return (0); 93 | i = 0; 94 | index = -1; 95 | while (s[i]&& j < (countwords(s, c, c1))) 96 | { 97 | 98 | if (s[i] == c || s[i] == c1) 99 | i++; 100 | if ((news[++index] = affectation(c, c1, &i, s)) == NULL) 101 | { 102 | free_news(news); 103 | free(s); 104 | return (0); 105 | } 106 | j++; 107 | } 108 | news[index + 1] = NULL; 109 | return (news); 110 | } 111 | -------------------------------------------------------------------------------- /ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mel-hamr +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/10/24 17:32:18 by mel-hamr #+# #+# */ 9 | /* Updated: 2021/01/26 17:32:01 by mel-hamr ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "includes/minishell.h" 14 | 15 | static int c_word(char *str, char c) 16 | { 17 | int i; 18 | int count; 19 | 20 | i = -1; 21 | count = 0; 22 | while (str[++i]) 23 | { 24 | if (str[i] == '\'' || str[i] == '"') 25 | { 26 | i = skip_quots(str, str[i],i); 27 | //printf("|%c|",ptr[nn+1]); 28 | } 29 | if (str[i] == c && str[i + 1] != c) 30 | count++; 31 | if (str[i] != c && str[i + 1] == '\0') 32 | count++; 33 | } 34 | return (count); 35 | } 36 | 37 | static void ft_free(char **tab, char *str) 38 | { 39 | int i; 40 | 41 | i = 0; 42 | while (tab[i]) 43 | { 44 | free(tab[i]); 45 | i++; 46 | } 47 | free(str); 48 | } 49 | 50 | static char *remplir(char *s, int *k, char c) 51 | { 52 | int i; 53 | int start; 54 | int end; 55 | char *str; 56 | int startt; 57 | i = *k; 58 | while (s[i] == c) 59 | i++; 60 | start = i; 61 | startt = i; 62 | while (s[i] != c && s[i]) 63 | { 64 | if (s[i] == '\'' || s[i] == '"') 65 | { 66 | i = skip_quots(s, s[i],i); 67 | //printf("|%c|",ptr[nn+1]); 68 | } 69 | i++; 70 | 71 | } 72 | end = i; 73 | *k = i; 74 | while(startt < end) 75 | { 76 | //printf("%c",s[startt]); 77 | startt++; 78 | } 79 | //printf("|%d|",end -start); 80 | str = ft_substr(s, start, (end - start )); 81 | //printf("%s\n",str); 82 | if (str == NULL) 83 | return (NULL); 84 | return (str); 85 | } 86 | 87 | char **ft_split(char const *s, char c) 88 | { 89 | char *str; 90 | char **tab; 91 | int i; 92 | int j; 93 | int k; 94 | 95 | if (s == NULL) 96 | return (NULL); 97 | i = -1; 98 | str = ft_strtrim(s, &c); 99 | if (str == NULL) 100 | return (NULL); 101 | j = c_word(str, c); 102 | //printf("|%d|",j); 103 | tab = (char **)malloc((j + 1) * sizeof(char*)); 104 | if (tab == NULL) 105 | return (NULL); 106 | k = 0; 107 | while (++i < j) 108 | if ((tab[i] = remplir(str, &k, c)) == NULL) 109 | { 110 | ft_free(tab, str); 111 | return (0); 112 | } 113 | tab[i] = 0; 114 | return (tab); 115 | } 116 | -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int ft_strcmp(const char *s1, const char *s2) 4 | { 5 | size_t i; 6 | i = 0; 7 | if ((!s1 || !s2)) 8 | return (1); 9 | while ((s1[i] || s2[i])) 10 | { 11 | if (s1[i] != s2[i]) 12 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 13 | i++; 14 | } 15 | return (0); 16 | } 17 | 18 | int ft_check_n(const char *s1, const char *s2) 19 | { 20 | size_t i; 21 | i = 0; 22 | if ((!s1 || !s2)) 23 | return (1); 24 | if (s1[i] == '-') 25 | { 26 | i++; 27 | while (s1[i]) 28 | { 29 | if (s1[i] != 'n') 30 | return 1; 31 | i++; 32 | } 33 | return (0); 34 | } 35 | /* while ((s1[i] || s2[i])) 36 | { 37 | if (s1[i] != s2[i]) 38 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 39 | i++; 40 | }*/ 41 | return (1); 42 | } 43 | 44 | int indexof(char *str, char c) 45 | { 46 | int i = 0; 47 | while(str[i]) 48 | { 49 | if(str[i] == c) 50 | return i; 51 | i++; 52 | } 53 | return(i); 54 | } 55 | 56 | int skip_quots(char *str, char c, int i) 57 | { 58 | int j; 59 | 60 | j = i + 1; 61 | while (str[j]) 62 | { 63 | if(str[j] == c ) 64 | { 65 | return(j); 66 | } 67 | j++; 68 | } 69 | return(j); 70 | } 71 | 72 | void ft_putstr_fd(char *s, int fd) 73 | { 74 | int i; 75 | 76 | i = 0; 77 | if (!s) 78 | return ; 79 | while (s[i]) 80 | { 81 | write(fd, &s[i], 1); 82 | i++; 83 | } 84 | } 85 | 86 | 87 | static int checkset(char c, char *set) 88 | { 89 | int i; 90 | 91 | i = 0; 92 | while (set[i]) 93 | { 94 | if (set[i] == c) 95 | return (1); 96 | i++; 97 | } 98 | return (0); 99 | } 100 | 101 | char *ft_strtrim(char const *s1, char const *set) 102 | { 103 | char *str; 104 | char *cpyset; 105 | int len; 106 | 107 | str = ft_strdup(s1); 108 | if (str == NULL) 109 | return (NULL); 110 | cpyset = (char *)set; 111 | while (checkset(str[0], cpyset) == 1) 112 | str++; 113 | len = ft_strlen(str) - 1; 114 | while (len > 0) 115 | { 116 | if (checkset(str[len], cpyset) == 0) 117 | break ; 118 | str[len] = '\0'; 119 | len--; 120 | } 121 | return (ft_strdup(str)); 122 | } 123 | 124 | int ft_isalpha(int c) 125 | { 126 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 127 | { 128 | return (1); 129 | } 130 | return (0); 131 | } 132 | int tab_rows(t_token *token) 133 | { 134 | int i; 135 | 136 | 137 | i = 0; 138 | while (token && token->type == ARG ) 139 | { 140 | if(token->type == ARG ) 141 | i++; 142 | token = token->next; 143 | } 144 | // if ((*token) && (*token)->type == ARG) 145 | // i++; 146 | return (i); 147 | } 148 | 149 | char **get_args(t_minishell **minishell) 150 | { 151 | 152 | char **args; 153 | int i; 154 | int size; 155 | size = tab_rows((*minishell)->token); 156 | i = 0; 157 | args = (char **)malloc(sizeof(char *) * (size + 1)); 158 | while (i < size) 159 | { 160 | args[i] = ft_strdup((*minishell)->token->str); 161 | (*minishell)->token = (*minishell)->token->next; 162 | i++; 163 | } 164 | args[i] = NULL; 165 | return (args); 166 | } 167 | -------------------------------------------------------------------------------- /parsing/check_fir.c: -------------------------------------------------------------------------------- 1 | #include"../includes/minishell.h" 2 | 3 | int count_lent(char *line) 4 | { 5 | int i; 6 | int count; 7 | 8 | count =0; 9 | i = 0; 10 | while (line[i]) 11 | { 12 | 13 | if (line[i] == '\'' || line[i] == '"') 14 | i = skip_quots(line, line[i ], i); 15 | if(line[i] == '<' ||line[i] == '>' || line[i] == '|' || line[i] == ';') 16 | { 17 | if((line[i+1] == '<' && line[i] == '<' )|| (line[i+1] == '>' && line[i] == '>' ) ) 18 | i++; 19 | if(line[i-1] != ' ' && i > 0) 20 | count++; 21 | if(line[i+1] != ' ' && line[i+1] != '\0') 22 | count++; 23 | } 24 | i++; 25 | } 26 | return(count); 27 | } 28 | 29 | void fill_btw_qutes(char *line,char *fixed_line,int *i,int *j,char c) 30 | { 31 | fixed_line[*j] = line[*i]; 32 | *i = *i+1; 33 | *j = *j+1; 34 | while (line[*i] != c && line[*i]) 35 | { 36 | fixed_line[*j] = line[*i]; 37 | *i = *i+1; 38 | *j = *j+1; 39 | } 40 | } 41 | char *fix_line(char *line) 42 | { 43 | char *fixed_line; 44 | int i; 45 | int j; 46 | 47 | j=0; 48 | fixed_line= (char *)malloc(sizeof(char) * (ft_strlen(line) + count_lent(line) +1 )); 49 | i=0; 50 | while (line[i]) 51 | { 52 | if(line[i] == '"' || line[i] =='\'') 53 | fill_btw_qutes(line,fixed_line,&i,&j,line[i]); 54 | if(line[i] == '<' ||line[i] == '>' || line[i] == '|' || line[i] == ';') 55 | { 56 | if(line[i-1] != ' ' && line[i-1] != line[i]) 57 | fixed_line[j++] = ' '; 58 | fixed_line[j++] = line[i]; 59 | if(line[i+1] != ' ' && line[i+1] != line[i]) 60 | fixed_line[j++] = ' '; 61 | i++; 62 | } 63 | else 64 | fixed_line[j++] = line[i++]; 65 | } 66 | fixed_line[j]='\0'; 67 | return(fixed_line); 68 | } 69 | void add_last_token(t_token *token) 70 | { 71 | t_token *n_token; 72 | 73 | while (token->next) 74 | { 75 | token= token->next; 76 | } 77 | 78 | n_token =new_token(); 79 | n_token->type = SEP; 80 | n_token->str = ft_strdup(";"); 81 | n_token->prev=token; 82 | token->next = n_token; 83 | 84 | } 85 | void check_fir(t_minishell *mini,char *line) 86 | { 87 | int i; 88 | int j; 89 | char **tab; 90 | 91 | j=0; 92 | i= 0; 93 | while (line[i]) 94 | { 95 | if((line[i] == ';' && line[i + 1] == ';') || (line[i] == '|' && line[i + 1] == '|' ) 96 | ||(line[i] == ';' && line[i + 1] == '|') || (line[i] == '|' && line[i + 1] == ';') ) 97 | { 98 | ft_putstr_fd("error double char : ", 1); 99 | break; 100 | } 101 | i++; 102 | } 103 | i=1; 104 | line = fix_line(line); 105 | tab = ft_split(line,' '); 106 | i = 0; 107 | // while (tab[i]) 108 | // { 109 | // printf("|%s|\n",tab[i]); 110 | // i++; 111 | // } 112 | i= 1; 113 | if(tab[0][0] == '|') 114 | mini->token->type = PIPE; 115 | else if(tab[0][0] == '<' && tab[0][1] == '\0') 116 | mini->token->type = LESS; 117 | else if(tab[0][0] == '>' && tab[0][1] == '\0') 118 | mini->token->type = GREAT; 119 | else if(tab[0][0] == '>' && tab[0][1] == '>') 120 | mini->token->type = DOUBLE_GREAT; 121 | else if(tab[0][0] == ';') 122 | mini->token->type = SEP; 123 | else 124 | { 125 | mini->token->type=CMD; 126 | } 127 | //token->type = CMD; 128 | mini->token->str = ft_strdup(tab[0]); 129 | while (tab[i]) 130 | { 131 | mini->token=add_arg_cmd(mini->token,tab[i]); 132 | i++; 133 | } 134 | add_last_token(mini->token); 135 | //return (0); 136 | } -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | // char **exp(t_env *ls) 4 | // { 5 | // char **tab; 6 | // int i; 7 | // tab = (char **)malloc(ft_lstsize(ls) * sizeof(char *) +1); 8 | // i = -1; 9 | // while (ls) 10 | // { 11 | // tab[++i] = ft_strdup(ls->value); 12 | // ls = ls->next; 13 | // } 14 | // tab[++i] = NULL; 15 | // return (tab); 16 | // } 17 | // int get_env_variable(t_env *env, char *arg_var) 18 | // { 19 | // struct env *tmp_n; 20 | // t_env *tmp_env; 21 | // char *var; 22 | // while (env->prev) 23 | // env = env->prev; 24 | // while (env) 25 | // { 26 | // tmp_n = env->next; 27 | // tmp_env = env; 28 | // var = ft_substr((char *)env->value, 0, indexof(env->value, '=')); 29 | // if (!ft_strcmp(var, arg_var)) 30 | // { 31 | // env = env->prev; 32 | // env->next = tmp_env->next; 33 | // env = tmp_env->next; 34 | // if (tmp_env->next) 35 | // env->prev = tmp_env->prev; 36 | // free(tmp_env); 37 | // return (1); 38 | // } 39 | // env = env->next; 40 | // } 41 | // return (0); 42 | // } 43 | void get_first(t_env **env) 44 | { 45 | while ((*env)->prev) 46 | { 47 | (*env) = (*env)->prev; 48 | } 49 | } 50 | void printf_arg_cmd(t_token *first) 51 | { 52 | int i; 53 | 54 | i = 0; 55 | 56 | while (first->prev) 57 | first = first->prev; 58 | while (first->next) 59 | { 60 | printf("type=%d\n", first->type); 61 | printf("str=%s\n", first->str); 62 | printf("=============================\n"); 63 | first = first->next; 64 | } 65 | printf("type=%d\n", first->type); 66 | printf("str=%s\n", first->str); 67 | printf("=============================\n"); 68 | i = 0; 69 | } 70 | void test(t_token *token) 71 | { 72 | while (token->prev) 73 | token = token->prev; 74 | } 75 | int main(int argc, char **argv, char **env) 76 | { 77 | t_minishell minishell; 78 | char *line; 79 | int s; 80 | int status; 81 | // t_env *temp; 82 | s = 1; 83 | minishell.env = get_env(env, argv, argc); 84 | increment_shlvl(minishell.env); 85 | status = 1; 86 | sign.pid = -1; 87 | signal(SIGINT, ft_cntr_c); 88 | signal(SIGQUIT, ft_cntr_d); 89 | //get_first(&list); 90 | // temp = mini.env; 91 | // while (temp->prev) 92 | // temp = temp->prev; 93 | 94 | // while (temp) 95 | // { 96 | // printf("%s\n",temp->value); 97 | // temp= temp->next; 98 | // } 99 | 100 | while (status) 101 | { 102 | line = NULL; 103 | minishell.token = new_token(); 104 | ft_putstr_fd("\033[1;34m$minishell$~> 👾\033[0m", 1); 105 | int r = get_next_line(0, &line); 106 | if (r == 0) 107 | { 108 | ft_putstr_fd("exit\n", 1); 109 | exit(0); 110 | } 111 | if (line[0] != '\0') 112 | { 113 | minishell.stdi = dup(0); 114 | minishell.stdo = dup(1); 115 | // puts("here"); 116 | check_fir(&minishell, line); 117 | check_sec(&minishell); 118 | // printf_arg_cmd(minishell.token); 119 | // printf("%s\n",mini.token->str); 120 | // test(mini.token); 121 | minishell.pid_list = malloc(sizeof(pid_t) * (minishell.nbrofpipe = count_pipe(minishell.token))); 122 | minishell.current_pipe = 0; 123 | ft_executor(&minishell, minishell.env); 124 | // printf("$? = %d\n", sign.exit_status); 125 | dup2(minishell.stdo, STDOUT); 126 | dup2(minishell.stdi, STDIN); 127 | free(minishell.token); 128 | } 129 | } 130 | return 0; 131 | } -------------------------------------------------------------------------------- /ft_export.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | 4 | int ft_search_variable(t_env *env, char *arg) 5 | { 6 | char *tmp; 7 | char *var; 8 | char *arg_var; 9 | 10 | while (env->prev) 11 | env = env->prev; 12 | 13 | while (env) 14 | { 15 | arg_var = ft_substr(arg, 0, indexof(arg, '=')); 16 | var = ft_substr((char *)env->value, 0, indexof(env->value, '=')); 17 | if(!ft_strcmp(var, arg_var)) 18 | { 19 | 20 | tmp = env->value; 21 | env->value = (void *)ft_strdup(arg); 22 | free(tmp); 23 | return (1); 24 | } 25 | env = env->next; 26 | } 27 | return (0); 28 | } 29 | int valid_arg(t_env *env, char *arg) 30 | { 31 | int i; 32 | 33 | i= 0; 34 | //if (ft_isalpha(arg[0]) || arg[0] == '_') 35 | // while(arg[i] && arg[i] != '=') 36 | // i++; 37 | //ft_putstr_fd(&arg[i], 1); 38 | if (!ft_isalpha(arg[0]) && arg[0] != '_') 39 | { 40 | ft_putstr_fd("export: `", 2); 41 | ft_putstr_fd(arg, 2); 42 | ft_putstr_fd("': not a valid identifier\n", 2); 43 | return (0); 44 | } 45 | if (ft_search_variable(env, arg)) 46 | return (0); 47 | return (1); 48 | } 49 | 50 | char *get_env_var(t_env *env, char *variable, int value) 51 | { 52 | char *env_var; 53 | char *ret; 54 | 55 | ret = NULL; 56 | while (env->prev) 57 | env = env->prev; 58 | while (env) 59 | { 60 | env_var = ft_substr((char *)env->value, 0, indexof(env->value, '=')); 61 | if(!ft_strcmp(env_var, variable)) 62 | break; 63 | env = env->next; 64 | } 65 | if (value == 1) 66 | ret = ft_substr((char *)env->value, indexof(env->value, '=') + 1, ft_strlen(env->value)); 67 | ret = ft_substr((char *)env->value, indexof(env->value, '=') + 1, ft_strlen(env->value)); 68 | return (ret); 69 | } 70 | 71 | 72 | 73 | char **sorted_table(t_env *env) 74 | { 75 | int i; 76 | int j; 77 | char *tmp; 78 | char **ret; 79 | i = 0; 80 | while (env->prev) 81 | env = env->prev; 82 | while (env->next) 83 | { 84 | env = env->next; 85 | i++; 86 | } 87 | if (!(ret = (char **)malloc(sizeof(char *) * (i + 1)))) 88 | return NULL; 89 | j = 0; 90 | while (env->prev) 91 | env = env->prev; 92 | while (env->next) 93 | { 94 | ret[j] = line_of_env(env->value); 95 | env = env->next; 96 | j++; 97 | } 98 | ret[j++] = line_of_env(env->value); 99 | ret[j] = NULL; 100 | j = 0; 101 | while (j < i) 102 | { 103 | if (ft_strcmp(ret[j], ret[j + 1]) > 0 && ret[j + 1] != NULL) 104 | { 105 | tmp = ret[j]; 106 | ret[j] = ret[j + 1]; 107 | ret[j + 1] = tmp; 108 | j = -1; 109 | } 110 | j++; 111 | } 112 | // i = 0; 113 | // while (ret[i]) 114 | // { 115 | // ft_putstr_fd("declare -x ", 1); 116 | // i++; 117 | // } 118 | return ret; 119 | } 120 | 121 | void ft_exp(t_env **env, t_minishell **minishell) 122 | { 123 | int i; 124 | char **exptab; 125 | char **args; 126 | 127 | args = get_args(minishell); 128 | exptab = sorted_table((*env)); 129 | i = 0; 130 | if (!args[0]) 131 | while (exptab[i]) 132 | { 133 | ft_putstr_fd("declare -x ", 1); 134 | ft_putstr_fd(exptab[i], 1); 135 | ft_putstr_fd("\n", 1); 136 | i++; 137 | } 138 | else 139 | while (args[i]) 140 | { 141 | 142 | if (valid_arg((*env), args[i])) 143 | { 144 | (*env) = add_env((*env), args[i]); 145 | } 146 | i++; 147 | } 148 | // int i; 149 | 150 | // i = 1; 151 | // if (!args[1]) 152 | // { 153 | // while (env->prev) 154 | // { 155 | // env = env->prev; 156 | // //cpt++; 157 | // } 158 | // while (env) 159 | // { 160 | // ft_putstr_fd("declare -x ", 1); 161 | // ft_putstr_fd(env->value, 1); 162 | // ft_putstr_fd("\n", 1); 163 | // env = env->next; 164 | // } 165 | // } 166 | } 167 | -------------------------------------------------------------------------------- /includes/minishell.h: -------------------------------------------------------------------------------- 1 | # ifndef MINISHELL_H 2 | # define MINISHELL_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | # define STDIN 0 14 | # define STDOUT 1 15 | # define STDERR 2 16 | #define BUFFER_SIZE 50 17 | #define PATH_MAX 4096 18 | #define LESS 4 19 | #define GREAT 7 20 | #define PIPE 1 21 | #define DOUBLE_GREAT 77 22 | #define CMD 2 23 | #define SEP 5 24 | #define ARG 3 25 | #define END 8 26 | typedef struct token 27 | { 28 | int type; 29 | char *str; 30 | struct token *next; 31 | struct token *prev; 32 | } t_token; 33 | 34 | typedef struct env 35 | { 36 | char *value; 37 | struct env *next; 38 | struct env *prev; 39 | } t_env; 40 | 41 | typedef struct minishell 42 | { 43 | t_env *env; 44 | t_token *token; 45 | pid_t *pid_list; 46 | int nbrofpipe; 47 | int nbrofpipe2; 48 | int current_pipe; 49 | int ret; 50 | int status; 51 | int stdi; 52 | int stdo; 53 | int fd[2]; 54 | int fdd; 55 | char *cmd; 56 | int pipe; 57 | int fd_in; 58 | int fd_out; 59 | int prev_pipe; 60 | } t_minishell; 61 | 62 | typedef struct s_sig 63 | { 64 | pid_t pid; 65 | int exit_status; 66 | } t_sig; 67 | t_env *add_env(t_env *list, void *value); 68 | void check_fir(t_minishell *mini,char *line); 69 | void check_sec(t_minishell *mini); 70 | t_token *add_arg_cmd(t_token *first,char *str); 71 | t_token *new_token(); 72 | size_t ft_strlen(const char *s); 73 | t_env *get_env(char **env,char **argv,int argc); 74 | char *fix_line(char *line); 75 | int indexof(char *str, char c); 76 | int ft_strcmp(const char *s1, const char *s2); 77 | void ft_putstr_fd(char *s, int fd); 78 | char *ft_chr(const char *s, int c); 79 | char *str_get(char *rest, int i, char **line); 80 | int get_next_line(int fd, char **line); 81 | void ft_bzero(void *s, size_t n); 82 | void *ft_calloc(size_t count, size_t size); 83 | char *ft_strjoin(char const *s1, char const *s2); 84 | char *ft_substr(char const *s, unsigned int start, size_t len); 85 | char **ft_split(char const *s, char c); 86 | char **ft_split_1(char *s, char c, char c1); 87 | char *ft_strdup(const char *s1); 88 | char *ft_strtrim(char const *s1, char const *set); 89 | int skip_quots(char *str, char c, int i); 90 | int ft_isalpha(int c); 91 | void increment_shlvl(t_env *env); 92 | char *ft_itoa(int n); 93 | void ft_pipe(t_minishell *minishell, t_env *env); 94 | int ft_atoi(const char *str); 95 | void clear(t_token *first); 96 | int count_pipe(t_token *token); 97 | char **ft_cmd(); 98 | int ft_cd( t_env *env, t_minishell **minishell); 99 | void ft_cntr_c(int signum); 100 | int ft_executor(t_minishell *minishell, t_env *env); 101 | void ft_cntr_c(int signum); 102 | void ft_cntr_d(int signum); 103 | int ft_echo(t_minishell **minishell); 104 | int ft_pwd(t_minishell **minishell); 105 | int ft_exit(char *cmd, t_minishell **minishell); 106 | void ft_unset(t_env *env, t_minishell **minishell); 107 | void ft_exp(t_env **env, t_minishell **minishell); 108 | int ft_execve(t_env *env, t_minishell **minishell,char *cmd); 109 | int ft_env(t_env *env); 110 | char **get_args(t_minishell **minishell); 111 | char *get_env_var(t_env *env, char *variable, int value); 112 | int valid_arg(t_env *env, char *arg); 113 | int ft_execve_no_pipe(t_env *env, t_minishell **minishell,char *cmd); 114 | char *line_of_env(const char *s1); 115 | void ft_great(t_minishell *minishell); 116 | void ft_input(t_minishell *minishell); 117 | void ft_append(t_minishell *minishell); 118 | void ft_close_fds(t_minishell *minishell, int *pipefds); 119 | int ft_check_n(const char *s1, const char *s2); 120 | int if_pipe_next(t_token *token); 121 | int ft_bultins(char *cmd, t_minishell **minishell, t_env *env); 122 | void get_cmd(t_minishell *minishell); 123 | extern t_sig sign; 124 | # endif -------------------------------------------------------------------------------- /ft_execve.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int count_tab(char **tab) 4 | { 5 | int i; 6 | i = 0; 7 | 8 | while(tab[i]) 9 | i++; 10 | return i; 11 | } 12 | 13 | char **ft_join_cmd(char **tab, char *arg) 14 | { 15 | int i; 16 | char **ret; 17 | 18 | i = -1; 19 | if (!(ret = malloc(sizeof(ret) * (count_tab(tab) + 1)))) 20 | return 0; 21 | 22 | while (tab[++i]) 23 | ret[i] =ret[i] = ft_strjoin(tab[i], arg); 24 | ret[i] = NULL; 25 | i = -1; 26 | while (tab[++i]) 27 | free(tab[i]); 28 | free(tab); 29 | return (ret); 30 | } 31 | 32 | char *if_arg_in_env(char **tab_env) 33 | { 34 | int i; 35 | char *path; 36 | struct stat stats; 37 | 38 | i = 0; 39 | while (tab_env[i]) 40 | { 41 | path = tab_env[i]; 42 | if ((stat(path, &stats)) == 0) 43 | if (stats.st_mode & X_OK) 44 | return (path); 45 | i++; 46 | } 47 | return (NULL); 48 | } 49 | 50 | char **exp_(t_env *ls) 51 | { 52 | char **tab; 53 | t_env *tmp; 54 | int cpt; 55 | int i; 56 | 57 | while(ls->prev) 58 | ls = ls->prev; 59 | tmp = ls; 60 | cpt = 0; 61 | while (tmp) 62 | { 63 | tmp = tmp->next; 64 | cpt++; 65 | } 66 | tab = (char **)malloc((cpt + 1) * sizeof(char *)); 67 | i = -1; 68 | while (ls) 69 | { 70 | tab[++i] = ft_strdup(ls->value); 71 | ls = ls->next; 72 | } 73 | tab[i + 1] = NULL; 74 | return (tab); 75 | } 76 | 77 | char **include_cmd(char **tab, char *cmd) 78 | { 79 | int i; 80 | char **ret; 81 | 82 | i = 0; 83 | while (tab[i]) 84 | i++; 85 | ret = (char **)malloc(sizeof(char *) * (i + 2)); 86 | i = 0; 87 | ret[0] = ft_strdup(cmd); 88 | while(tab[i]) 89 | { 90 | ret[i + 1] = ft_strdup(tab[i]); 91 | i++; 92 | } 93 | ret[i + 1] = NULL; 94 | return (ret); 95 | } 96 | 97 | int ft_execve(t_env *env, t_minishell **minishell,char *cmd) 98 | { 99 | int i = 0; 100 | char **args; 101 | int j = -1; 102 | char *path; 103 | char **tab; 104 | char **envp; 105 | pid_t pid; 106 | 107 | args = get_args(minishell); 108 | while(args[i]) 109 | i++; 110 | args = include_cmd(args, cmd); 111 | i = 0; 112 | path = get_env_var(env, "PATH", 1); 113 | tab = ft_split(path, ':'); 114 | tab = ft_join_cmd(tab, ft_strjoin("/", cmd)); 115 | envp = exp_(env); 116 | 117 | if ((path = if_arg_in_env(tab))) 118 | { 119 | if (execve(path, args, envp) == -1) 120 | ft_putstr_fd("Could not execve", 3); 121 | } 122 | else if ((path = if_arg_in_env(&cmd))) 123 | { 124 | if (execve(path, args, envp) == -1) 125 | ft_putstr_fd("Could not execve", 3); 126 | } 127 | else 128 | { 129 | if (!ft_strcmp("minishell", cmd)) 130 | increment_shlvl(env); 131 | else 132 | { 133 | ft_putstr_fd(cmd, 2); 134 | ft_putstr_fd(": command not found\n",2); 135 | exit(127); 136 | } 137 | } 138 | return (1); 139 | } 140 | 141 | int ft_execve_no_pipe(t_env *env, t_minishell **minishell,char *cmd) 142 | { 143 | int i = 0; 144 | char **args; 145 | int j = -1; 146 | char *path; 147 | char **tab; 148 | char **envp; 149 | // int st; 150 | pid_t pid; 151 | // pid_t pid2; 152 | 153 | args = get_args(minishell); 154 | while(args[i]) 155 | i++; 156 | args = include_cmd(args, cmd); 157 | i = 0; 158 | path = get_env_var(env, "PATH", 1); 159 | tab = ft_split(path, ':'); 160 | tab = ft_join_cmd(tab, ft_strjoin("/", cmd)); 161 | envp = exp_(env); 162 | pid = fork(); 163 | if (pid == 0) 164 | { 165 | if ((path = if_arg_in_env(tab))) 166 | { 167 | if (execve(path, args, envp) == -1) 168 | ft_putstr_fd("Could not execve", 3); 169 | } 170 | else if ((path = if_arg_in_env(&cmd))) 171 | { 172 | if (execve(path, args, envp) == -1) 173 | ft_putstr_fd("Could not execve", 3); 174 | } 175 | else 176 | { 177 | if (!ft_strcmp("minishell", cmd)) 178 | increment_shlvl(env); 179 | else 180 | { 181 | ft_putstr_fd(cmd, 2); 182 | ft_putstr_fd(": command not found\n",2); 183 | exit(127); 184 | } 185 | } 186 | } 187 | else 188 | { 189 | waitpid(pid, &(*minishell)->status, 0); 190 | if (WIFEXITED((*minishell)->status)) 191 | { 192 | sign.exit_status = WEXITSTATUS((*minishell)->status); 193 | printf("\n$? = %d\n", sign.exit_status); 194 | } 195 | } 196 | return (1); 197 | } -------------------------------------------------------------------------------- /parsing/check_sec.c: -------------------------------------------------------------------------------- 1 | #include"../includes/minishell.h" 2 | int count_dollar(char *str) 3 | { 4 | int i; 5 | int count; 6 | 7 | count = 0; 8 | i=0; 9 | while (str[i]) 10 | { 11 | if(str[i] == '$') 12 | count++; 13 | i++; 14 | } 15 | return(count); 16 | } 17 | char *get_env_variable(t_env *env, char *arg_var) 18 | { 19 | char *ret; 20 | char *var; 21 | int index; 22 | 23 | while (env->prev) 24 | env = env->prev; 25 | while (env) 26 | { 27 | index = indexof(env->value, '='); 28 | var = ft_substr((char *)env->value, 0,index); 29 | if (!ft_strcmp(var, arg_var)) 30 | { 31 | ret = ft_substr(env->value,(index+1),ft_strlen(env->value)); 32 | 33 | return (ret); 34 | } 35 | env = env->next; 36 | } 37 | return (NULL); 38 | } 39 | char *correct_arg_2(char *str,char *ret,char *dollar) 40 | { 41 | int lent_arg; 42 | char *strr; 43 | int i; 44 | int j; 45 | int k; 46 | int test; 47 | 48 | test =0; 49 | i = 0; 50 | j = 0; 51 | k = 0; 52 | lent_arg = ft_strlen(str) - ft_strlen(dollar) + ft_strlen(ret) ; 53 | strr = (char *)malloc(sizeof(char *) *(lent_arg +1)); 54 | // while (str[i] != '$' && str[i] != '\0') 55 | // { 56 | 57 | // } 58 | 59 | while (str[i]) 60 | { 61 | if(str[i] == '$'&& test == 0) 62 | { 63 | test = 1; 64 | i++; 65 | while (( str[i] == '_' || ft_isalpha(str[i])) && str[i] != '\0') 66 | i++; 67 | while (ret[k]) 68 | { 69 | strr[j] = ret[k]; 70 | k++; 71 | j++; 72 | } 73 | 74 | } 75 | strr[j] = str[i]; 76 | i++; 77 | j++; 78 | } 79 | strr[j] = '\0'; 80 | return(strr); 81 | } 82 | char *get_dollar(char *str, t_env *env) 83 | { 84 | int i; 85 | int j; 86 | char *dollar; 87 | char *ret; 88 | int check; 89 | int k; 90 | 91 | k= 0; 92 | check =0; 93 | if(env) 94 | dollar = NULL; 95 | j=0; 96 | i = 0; 97 | while (str[i]) 98 | { 99 | if(str[i] == '$') 100 | { 101 | j = i+1; 102 | while ((str[j] == '_' || ft_isalpha(str[j])) && str[j] != '\0') 103 | j++; 104 | // if(j == 1 && str[j] != '\0') 105 | // j++; 106 | dollar =ft_substr(str,i,j); 107 | //printf("%d||%d||%s\n",i,j,dollar); 108 | ret=get_env_variable(env,&dollar[1]); 109 | if(ret) 110 | { 111 | if(check == 0) 112 | check =1; 113 | ret = correct_arg_2(str,ret,dollar); 114 | str = ft_strdup(ret); 115 | // printf("|%s|\n",ret); 116 | 117 | } 118 | } 119 | i++; 120 | } 121 | if (check ==1) 122 | return(ret); 123 | else 124 | return(ft_substr(str,0,indexof(str,'$'))); 125 | } 126 | 127 | int check_quotes_arg(char *str) 128 | { 129 | int i; 130 | char c; 131 | 132 | 133 | i=0; 134 | while (str[i]) 135 | { 136 | if(str[i] == '"' || str[i] == '\'') 137 | { 138 | c = str[i]; 139 | i++; 140 | while (str[i]) 141 | { 142 | if(str[i] == c || str[i] == c) 143 | return(str[i]); 144 | i++; 145 | } 146 | } 147 | i++; 148 | } 149 | return(str[i]); 150 | } 151 | 152 | char *correct_arg(char *str,char c) 153 | { 154 | char *cor_str; 155 | int i; 156 | int j; 157 | 158 | j =0; 159 | i = 0; 160 | cor_str = (char *)malloc(sizeof(char) * (ft_strlen(str)-1)); 161 | while (str[i]) 162 | { 163 | if(str[i]== c ) 164 | i++; 165 | cor_str[j] = str[i]; 166 | i++; 167 | j++; 168 | } 169 | cor_str[j]='\0'; 170 | return(cor_str); 171 | } 172 | 173 | t_token *final_arg(t_token *token,t_env *env) 174 | { 175 | t_token *tmp; 176 | char c; 177 | t_env *env1; 178 | 179 | env1=env; 180 | c = 'a'; 181 | tmp = token; 182 | while (tmp->prev) 183 | tmp = tmp->prev; 184 | while (tmp) 185 | { 186 | if(tmp->type == ARG) 187 | { 188 | c = check_quotes_arg(tmp->str); 189 | if(c == '"' | c == '\'') 190 | { 191 | tmp->str = correct_arg(tmp->str,c); 192 | } 193 | if(c != '\'') 194 | tmp->str = get_dollar(tmp->str,env); 195 | } 196 | tmp =tmp->next; 197 | } 198 | return(token); 199 | } 200 | t_token *next(t_token *token) 201 | { 202 | if(token->next != NULL) 203 | token = token->next; 204 | return(token); 205 | } 206 | void fix_token(t_token *token,int is_cmd) 207 | { 208 | if(is_cmd == 0) 209 | { 210 | token = next(token); 211 | if(token->next != NULL) 212 | if(token->type == ARG) 213 | token->type = CMD; 214 | 215 | } 216 | 217 | } 218 | void check_sec(t_minishell *mini) 219 | { 220 | t_token *tmp; 221 | int is_cmd; 222 | int is_great; 223 | 224 | is_great = 0; 225 | is_cmd = 0; 226 | tmp = mini->token; 227 | while (tmp->prev) 228 | tmp = tmp->prev; 229 | 230 | while (tmp) 231 | { 232 | if(tmp->type == CMD) 233 | is_cmd = 1; 234 | else if(tmp->type == PIPE || tmp->type == SEP) 235 | { 236 | is_cmd = 0; 237 | } 238 | else if(tmp->type == GREAT) 239 | { 240 | tmp = next(tmp); 241 | fix_token(tmp,is_cmd); 242 | } 243 | tmp = tmp->next; 244 | } 245 | final_arg(mini->token,mini->env); 246 | 247 | //return(token); 248 | } -------------------------------------------------------------------------------- /f1: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | int count_tab(char **tab) 4 | { 5 | int i; 6 | i = 0; 7 | 8 | while(tab[i]) 9 | i++; 10 | return i; 11 | } 12 | 13 | char **ft_join_cmd(char **tab, char *arg) 14 | { 15 | int i; 16 | char **ret; 17 | 18 | i = -1; 19 | if (!(ret = malloc(sizeof(ret) * (count_tab(tab) + 1)))) 20 | return 0; 21 | 22 | while (tab[++i]) 23 | ret[i] =ret[i] = ft_strjoin(tab[i], arg); 24 | ret[i] = NULL; 25 | i = -1; 26 | while (tab[++i]) 27 | free(tab[i]); 28 | free(tab); 29 | return (ret); 30 | } 31 | 32 | char *if_arg_in_env(char **tab_env) 33 | { 34 | int i; 35 | char *path; 36 | struct stat stats; 37 | 38 | i = 0; 39 | while (tab_env[i]) 40 | { 41 | path = tab_env[i]; 42 | if ((stat(path, &stats)) == 0) 43 | if (stats.st_mode & X_OK) 44 | return (path); 45 | i++; 46 | } 47 | return (NULL); 48 | } 49 | 50 | char **exp_(t_env *ls) 51 | { 52 | char **tab; 53 | t_env *tmp; 54 | int cpt; 55 | int i; 56 | 57 | while(ls->prev) 58 | ls = ls->prev; 59 | tmp = ls; 60 | cpt = 0; 61 | while (tmp) 62 | { 63 | tmp = tmp->next; 64 | cpt++; 65 | } 66 | tab = (char **)malloc((cpt + 1) * sizeof(char *)); 67 | i = -1; 68 | while (ls) 69 | { 70 | tab[++i] = ft_strdup(ls->value); 71 | ls = ls->next; 72 | } 73 | tab[i + 1] = NULL; 74 | return (tab); 75 | } 76 | 77 | char **include_cmd(char **tab, char *cmd) 78 | { 79 | int i; 80 | char **ret; 81 | 82 | i = 0; 83 | while (tab[i]) 84 | i++; 85 | ret = (char **)malloc(sizeof(char *) * (i + 2)); 86 | i = 0; 87 | ret[0] = ft_strdup(cmd); 88 | while(tab[i]) 89 | { 90 | ret[i + 1] = ft_strdup(tab[i]); 91 | i++; 92 | } 93 | ret[i + 1] = NULL; 94 | return (ret); 95 | } 96 | 97 | int ft_execve(t_env *env, t_minishell **minishell,char *cmd) 98 | { 99 | int i = 0; 100 | char **args; 101 | int j = -1; 102 | char *path; 103 | char **tab; 104 | char **envp; 105 | // int st; 106 | pid_t pid; 107 | // pid_t pid2; 108 | 109 | args = get_args(minishell); 110 | while(args[i]) 111 | i++; 112 | args = include_cmd(args, cmd); 113 | i = 0; 114 | path = get_env_var(env, "PATH", 1); 115 | tab = ft_split(path, ':'); 116 | tab = ft_join_cmd(tab, ft_strjoin("/", cmd)); 117 | envp = exp_(env); 118 | // pid = fork(); 119 | // if (pid == 0) 120 | // { 121 | if ((path = if_arg_in_env(tab))) 122 | { 123 | if (execve(path, args, envp) == -1) 124 | ft_putstr_fd("Could not execve", 3); 125 | // execve(path, args, envp); 126 | // puts("\n\n\n\n\nhere"); 127 | } 128 | else if ((path = if_arg_in_env(&cmd))) 129 | { 130 | if (execve(path, args, envp) == -1) 131 | ft_putstr_fd("Could not execve", 3); 132 | // puts("\n\n\n\n\nhere"); 133 | // execve(path, args, envp); 134 | }else 135 | { 136 | if (!ft_strcmp("minishell", cmd)) 137 | increment_shlvl(env); 138 | else 139 | { 140 | ft_putstr_fd(cmd, 2); 141 | ft_putstr_fd(": command not found\n",2); 142 | exit(127); 143 | } 144 | } 145 | // exit(0); 146 | // } 147 | // else 148 | // { 149 | // // if (!(*minishell)->nbrofpipe) 150 | // waitpid(pid, &(*minishell)->status, 0); 151 | // // else 152 | // // { 153 | // // while (++j < (*minishell)->nbrofpipe + 1) 154 | // // waitpid((*minishell)->pid[j], &(*minishell)->status, 0); 155 | // // } 156 | // // wait(NULL); 157 | // if (WIFEXITED((*minishell)->status)) 158 | // { 159 | // (*minishell)->exit_status = WEXITSTATUS((*minishell)->status); 160 | // return ((*minishell)->exit_status); 161 | // // printf("Exit status of the child was %d\n", (*minishell)->exit_status); 162 | // } 163 | // return (1); 164 | // } 165 | return (1); 166 | } 167 | 168 | int ft_execve_no_pipe(t_env *env, t_minishell **minishell,char *cmd) 169 | { 170 | int i = 0; 171 | char **args; 172 | int j = -1; 173 | char *path; 174 | char **tab; 175 | char **envp; 176 | // int st; 177 | pid_t pid; 178 | // pid_t pid2; 179 | 180 | args = get_args(minishell); 181 | while(args[i]) 182 | i++; 183 | args = include_cmd(args, cmd); 184 | i = 0; 185 | path = get_env_var(env, "PATH", 1); 186 | tab = ft_split(path, ':'); 187 | tab = ft_join_cmd(tab, ft_strjoin("/", cmd)); 188 | envp = exp_(env); 189 | pid = fork(); 190 | if (pid == 0) 191 | { 192 | if ((path = if_arg_in_env(tab))) 193 | { 194 | if (execve(path, args, envp) == -1) 195 | ft_putstr_fd("Could not execve", 3); 196 | // execve(path, args, envp); 197 | // puts("\n\n\n\n\nhere"); 198 | } 199 | else if ((path = if_arg_in_env(&cmd))) 200 | { 201 | if (execve(path, args, envp) == -1) 202 | ft_putstr_fd("Could not execve", 3); 203 | // puts("\n\n\n\n\nhere"); 204 | // execve(path, args, envp); 205 | }else 206 | { 207 | if (!ft_strcmp("minishell", cmd)) 208 | increment_shlvl(env); 209 | else 210 | { 211 | ft_putstr_fd(cmd, 2); 212 | ft_putstr_fd(": command not found\n",2); 213 | exit(127); 214 | } 215 | } 216 | // exit(0); 217 | } 218 | else 219 | { 220 | // if (!(*minishell)->nbrofpipe) 221 | waitpid(pid, &(*minishell)->status, 0); 222 | // else 223 | // { 224 | // while (++j < (*minishell)->nbrofpipe + 1) 225 | // waitpid((*minishell)->pid[j], &(*minishell)->status, 0); 226 | // } 227 | // wait(NULL); 228 | if (WIFEXITED((*minishell)->status)) 229 | { 230 | (*minishell)->exit_status = WEXITSTATUS((*minishell)->status); 231 | return ((*minishell)->exit_status); 232 | // printf("Exit status of the child was %d\n", (*minishell)->exit_status); 233 | } 234 | return (1); 235 | } 236 | return (1); 237 | } -------------------------------------------------------------------------------- /minishell.c: -------------------------------------------------------------------------------- 1 | #include "includes/minishell.h" 2 | 3 | t_sig sign; 4 | 5 | void clear(t_token *first) 6 | { 7 | if(first->str) 8 | free(first->str); 9 | } 10 | 11 | int count_pipe(t_token *token) 12 | { 13 | int ret; 14 | t_token *tmp; 15 | 16 | tmp = token; 17 | ret = 0; 18 | while( token->prev) 19 | token = token->prev; 20 | while(token) 21 | { 22 | //printf("%d\n",token->type); 23 | if (token->type == PIPE) 24 | ret++; 25 | token = token->next; 26 | } 27 | token = tmp; 28 | return ret; 29 | } 30 | 31 | int count_pipe2(t_token *token) 32 | { 33 | int ret; 34 | t_token *tmp; 35 | 36 | tmp = token; 37 | ret = 0; 38 | while( token->prev) 39 | token = token->prev; 40 | while(token && token->type < 4) 41 | { 42 | //printf("%d\n",token->type); 43 | if (token->type == PIPE) 44 | ret++; 45 | token = token->next; 46 | } 47 | token = tmp; 48 | return ret; 49 | } 50 | 51 | char **ft_cmd() 52 | { 53 | char **tab; 54 | tab = (char **)malloc(sizeof(char *) * 3); 55 | tab[0] = ft_strdup("exit"); 56 | tab[1] = ft_strdup(".."); 57 | tab[2] = NULL; 58 | return tab; 59 | } 60 | 61 | /* 62 | int if_pipe_prev(t_token *token) 63 | { 64 | // puts(token->str); 65 | while (token->prev && (token->type == ARG || token->type == CMD)) 66 | token = token->prev; 67 | if (token && token->type == PIPE) 68 | return PIPE; 69 | if (token->prev && token->type == CMD) 70 | token = token->prev; 71 | while (token && (token->type != CMD && token->type != SEP)) 72 | { 73 | token = token->prev; 74 | } 75 | 76 | return (0); 77 | }*/ 78 | 79 | int if_pipe_next(t_token *token) 80 | { 81 | while(token && (token->type != CMD || token->type != SEP)) 82 | { 83 | if (token && token->type == PIPE) 84 | return PIPE; 85 | token = token->next; 86 | } 87 | return (0); 88 | } 89 | 90 | int if_buitins(char *cmd) 91 | { 92 | if (!ft_strcmp(cmd, "echo")) 93 | return (1); 94 | else if (!ft_strcmp(cmd, "cd")) 95 | return (1); 96 | else if (!ft_strcmp(cmd, "pwd")) 97 | return (1); 98 | else if (!ft_strcmp(cmd, "exit")) 99 | return (1); 100 | else if (!ft_strcmp(cmd, "env")) 101 | return (1); 102 | else if (!ft_strcmp(cmd, "export")) 103 | return (1); 104 | else if (!ft_strcmp(cmd, "unset")) 105 | return (1); 106 | return (0); 107 | } 108 | 109 | int ft_bultins(char *cmd, t_minishell **minishell, t_env *env) 110 | { 111 | int status; 112 | int ret; 113 | ret = 1; 114 | // if(if_pipe_prev((*minishell)->token->prev)) 115 | // { 116 | // ft_putstr_fd("Yeap", 2); 117 | // } 118 | if (!ft_strcmp(cmd, "echo")) 119 | ft_echo(minishell); 120 | else if (!ft_strcmp(cmd, "cd")) 121 | ft_cd(env, minishell); 122 | else if (!ft_strcmp(cmd, "pwd")) 123 | ft_pwd(minishell); 124 | else if (!ft_strcmp(cmd, "exit")) 125 | status = ft_exit(cmd, minishell); 126 | else if (!ft_strcmp(cmd, "env")) 127 | ft_env(env); 128 | else if (!ft_strcmp(cmd, "export")) 129 | ft_exp(&env, minishell); 130 | else if (!ft_strcmp(cmd, "unset")) 131 | ft_unset(env, minishell); 132 | 133 | if (if_buitins(cmd)) 134 | return (1); 135 | else 136 | return (0); 137 | } 138 | 139 | int if_great_next(t_token *token) 140 | { 141 | while(token && (token->type != CMD || token->type != SEP)) 142 | { 143 | if (token && token->type == GREAT) 144 | { 145 | return GREAT; 146 | } 147 | token = token->next; 148 | } 149 | return (0); 150 | } 151 | 152 | int if_append(t_token *token) 153 | { 154 | // while(token && (token->type == ARG || token->type == DOUBLE_GREAT)) 155 | while(token && (token->type != CMD || token->type == SEP)) 156 | { 157 | if (token && token->type == DOUBLE_GREAT) 158 | return DOUBLE_GREAT; 159 | token = token->next; 160 | } 161 | return (0); 162 | } 163 | 164 | int if_input(t_token *token) 165 | { 166 | while(token && (token->type == ARG || token->type == LESS)) 167 | { 168 | if (token && token->type == LESS) 169 | return LESS; 170 | token = token->next; 171 | } 172 | return (0); 173 | } 174 | 175 | void ft_piping(t_minishell *minishell, t_env *env) 176 | { 177 | dup2(minishell->fdd, STDIN); 178 | if (if_pipe_next(minishell->token) == PIPE) 179 | { 180 | dup2(minishell->fd[1], STDOUT); 181 | close(minishell->fd[1]); 182 | } 183 | close(minishell->fd[0]); 184 | // if (!ft_bultins(minishell->cmd, &minishell, env)) 185 | // { 186 | // ft_execve(env, &minishell, minishell->cmd); 187 | // if(minishell->cmd != NULL) 188 | // { 189 | // free(minishell->cmd); 190 | // minishell->cmd = NULL; 191 | // } 192 | // } 193 | } 194 | void get_cmd(t_minishell *minishell) 195 | { 196 | if (minishell->token && minishell->token->type == CMD) 197 | { 198 | minishell->cmd = NULL; 199 | minishell->cmd = ft_strdup(minishell->token->str); 200 | if (minishell->token->next) 201 | minishell->token = minishell->token->next; 202 | 203 | } 204 | else 205 | { 206 | if (minishell->cmd) 207 | { 208 | free(minishell->cmd); 209 | minishell->cmd = NULL; 210 | } 211 | } 212 | } 213 | 214 | 215 | void get_cmd2(t_minishell *minishell) 216 | { 217 | if (minishell->token && minishell->token->type == CMD) 218 | { 219 | minishell->cmd = NULL; 220 | minishell->cmd = ft_strdup(minishell->token->str); 221 | if (minishell->token->next) 222 | minishell->token = minishell->token->next; 223 | }else 224 | { 225 | if (minishell->cmd) 226 | { 227 | free(minishell->cmd); 228 | minishell->cmd = NULL; 229 | } 230 | } 231 | 232 | } 233 | 234 | /*int if_pipe_next(t_token *token) 235 | { 236 | while(token && (token->type != CMD || token->type != SEP)) 237 | { 238 | if (token && token->type == PIPE) 239 | return PIPE; 240 | token = token->next; 241 | } 242 | return (0); 243 | } 244 | */ 245 | int redir_pipe(t_minishell *minishell, t_env *env, int *cpt) 246 | { 247 | int df = 0; 248 | t_token *tmp; 249 | 250 | minishell->nbrofpipe2 = count_pipe2(minishell->token); 251 | if (if_great_next(minishell->token)) 252 | ft_great(minishell); 253 | if (if_append(minishell->token)) 254 | ft_append(minishell); 255 | if (if_input(minishell->token)) 256 | ft_input(minishell); 257 | if (minishell->token && (if_pipe_next(minishell->token) == PIPE)) 258 | { 259 | ft_pipe(minishell, env); 260 | return (1); 261 | } 262 | return (0); 263 | } 264 | 265 | void next_run(t_minishell **minishell) 266 | { 267 | while ((*minishell)->token && ((*minishell)->token->type == PIPE || (*minishell)->token->type == GREAT 268 | || (*minishell)->token->type == LESS || (*minishell)->token->type == DOUBLE_GREAT|| (*minishell)->token->type == ARG 269 | || (*minishell)->token->type == SEP)) 270 | { 271 | if ((*minishell)->token->type == SEP) 272 | { 273 | dup2((*minishell)->stdo, STDOUT); 274 | dup2((*minishell)->stdi, STDIN); 275 | } 276 | (*minishell)->token = (*minishell)->token->next; 277 | } 278 | } 279 | 280 | int ft_executor(t_minishell *minishell, t_env *env) 281 | { 282 | 283 | int cpt = 0; 284 | minishell->fdd = 0; 285 | while (minishell->token->prev) 286 | minishell->token = minishell->token->prev; 287 | while (minishell->token) 288 | { 289 | 290 | get_cmd(minishell); 291 | minishell->pipe = 0; 292 | minishell->pipe = redir_pipe(minishell, env, &cpt); 293 | cpt++; 294 | if ((minishell->cmd && if_pipe_next(minishell->token) != PIPE) && minishell->pipe == 0) 295 | { 296 | if (!ft_bultins(minishell->cmd, &minishell, env)) 297 | ft_execve_no_pipe(env, &minishell, minishell->cmd); 298 | if(minishell->cmd != NULL) 299 | { 300 | free(minishell->cmd); 301 | minishell->cmd = NULL; 302 | } 303 | } 304 | next_run(&minishell); 305 | } 306 | // while (0 < minishell->nbrofpipe + 1) 307 | // { 308 | // waitpid(minishell->pid_list[minishell->nbrofpipe--], &minishell->status, 0); // here 309 | // if (WIFEXITED(minishell->status)) 310 | // minishell->exit_status = WEXITSTATUS(minishell->status); 311 | // } 312 | return (1); 313 | } 314 | 315 | void increment_shlvl(t_env *env) 316 | { 317 | char *var; 318 | char *shlvl; 319 | 320 | while (env->prev) 321 | env = env->prev; 322 | while (env) 323 | { 324 | var = ft_substr((char *)env->value, 0, indexof(env->value, '=') + 1); 325 | if (!ft_strcmp(var, "SHLVL=")) 326 | { 327 | shlvl = ft_substr(env->value, indexof(env->value, '=') + 1, ft_strlen(env->value)); 328 | shlvl = ft_itoa(ft_atoi(shlvl) + 1); 329 | env->value = ft_strjoin(var, shlvl); 330 | break ; 331 | } 332 | env = env->next; 333 | } 334 | } 335 | 336 | 337 | --------------------------------------------------------------------------------