From cf10a76c4a256da5e354688bc0dce148933ec21c Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:56:47 +0200 Subject: [PATCH 01/86] Create README.md --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c5c34aa --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +Documentation pour les fonctions : + +Gestion des lignes recu en prompt : +readline () lis le prompt et stock dans un char * +rl_on_new_line () +rl_replace_line () +add_history () sauvegarde des lignes +rl_clear_history () vide l'historique +rl_redisplay () affiche une ancienne ligne + https://tiswww.case.edu/php/chet/readline/readline.html + +Gestion de droits equivalant sudo : +access () verifircation des droits du process appelant + https://linux.die.net/man/2/access + + + From 75225d534aa0ddcbd1ecc4a78e2e2cf9a2750f47 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:16:02 +0200 Subject: [PATCH 02/86] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c5c34aa..5759ff1 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,11 @@ Gestion de droits equivalant sudo : access () verifircation des droits du process appelant https://linux.die.net/man/2/access - +Gestion de process : +wait() +waitpid() + https://www.man-linux-magique.net/man2/waitpid.html +wait3() attend n’importe quel fils +wait4() attend un ou des fils spécifique(s) + https://www.man-linux-magique.net/man2/wait3.html From f06d8a8edeb64762b34626d6170d808cf077568c Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:04:16 +0200 Subject: [PATCH 03/86] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5759ff1..7f3372e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ Documentation pour les fonctions : + Gestion des lignes recu en prompt : readline () lis le prompt et stock dans un char * rl_on_new_line () @@ -9,10 +10,12 @@ rl_clear_history () vide l'historique rl_redisplay () affiche une ancienne ligne https://tiswww.case.edu/php/chet/readline/readline.html + Gestion de droits equivalant sudo : access () verifircation des droits du process appelant https://linux.die.net/man/2/access + Gestion de process : wait() waitpid() @@ -21,3 +24,4 @@ wait3() attend n’importe quel fils wait4() attend un ou des fils spécifique(s) https://www.man-linux-magique.net/man2/wait3.html +LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR From d6efc5dc35d8e946a1fc9a72ef2564aab16683e7 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:04:44 +0200 Subject: [PATCH 04/86] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7f3372e..386ed40 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ access () verifircation des droits du process appelant Gestion de process : + wait() waitpid() https://www.man-linux-magique.net/man2/waitpid.html From 23d1c18887610004d892568c48d17599643db1d8 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:05:05 +0200 Subject: [PATCH 05/86] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 386ed40..3ba147b 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,15 @@ access () verifircation des droits du process appelant Gestion de process : wait() + waitpid() + https://www.man-linux-magique.net/man2/waitpid.html + wait3() attend n’importe quel fils + wait4() attend un ou des fils spécifique(s) + https://www.man-linux-magique.net/man2/wait3.html LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR From 2a1ceafa3e2d3d446710dd757f40383bfbb83281 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:05:26 +0200 Subject: [PATCH 06/86] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ba147b..6dc208e 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ wait() waitpid() - https://www.man-linux-magique.net/man2/waitpid.html + https://www.man-linux-magique.net/man2/waitpid.html wait3() attend n’importe quel fils wait4() attend un ou des fils spécifique(s) - https://www.man-linux-magique.net/man2/wait3.html + https://www.man-linux-magique.net/man2/wait3.html LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR From 574031f783ef072eaed05b06fcf19e9be73a6310 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:06:13 +0200 Subject: [PATCH 07/86] git learning --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6dc208e..950ef5b 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,27 @@ Documentation pour les fonctions : Gestion des lignes recu en prompt : + readline () lis le prompt et stock dans un char * + rl_on_new_line () + rl_replace_line () + add_history () sauvegarde des lignes + rl_clear_history () vide l'historique + rl_redisplay () affiche une ancienne ligne - https://tiswww.case.edu/php/chet/readline/readline.html + + https://tiswww.case.edu/php/chet/readline/readline.html Gestion de droits equivalant sudo : + access () verifircation des droits du process appelant - https://linux.die.net/man/2/access + + https://linux.die.net/man/2/access Gestion de process : From 570f7e4d2ce703a6265ea7100ed44b1a7a32dfeb Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:06:37 +0200 Subject: [PATCH 08/86] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 950ef5b..e6cdfd5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Documentation pour les fonctions : + Documentation pour les fonctions : Gestion des lignes recu en prompt : From c471f017f8f00ad991d065b5c2da174e1917ab9b Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:29:28 +0200 Subject: [PATCH 09/86] add fork --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e6cdfd5..56b97f7 100644 --- a/README.md +++ b/README.md @@ -38,5 +38,9 @@ wait3() attend n’importe quel fils wait4() attend un ou des fils spécifique(s) https://www.man-linux-magique.net/man2/wait3.html +fork() creation d'une copie du process + + http://manpagesfr.free.fr/man/man2/fork.2.html LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR + From 68547c3b0e66238964c4a2962a08ddbd832b3740 Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 19:38:15 +0200 Subject: [PATCH 10/86] description de toutes les fonctions --- README.md | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 56b97f7..d8f2ba9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR + + Documentation pour les fonctions : @@ -18,18 +21,21 @@ rl_redisplay () affiche une ancienne ligne https://tiswww.case.edu/php/chet/readline/readline.html -Gestion de droits equivalant sudo : +Gestion de process : + +execve () execute un programme, une commande de shell par exemple -access () verifircation des droits du process appelant - - https://linux.die.net/man/2/access + http://manpagesfr.free.fr/man/man2/execve.2.html +dup () Dupliquer un descripteur de fichier -Gestion de process : +dup () transforme newfd en une copie de oldfd -wait() + https://www.man-linux-magique.net/man2/dup2.html -waitpid() +wait() attend jusqu'a que l'un de ses fils est termine + +waitpid() attend jusqu'a que le process specifie est termine https://www.man-linux-magique.net/man2/waitpid.html @@ -42,5 +48,108 @@ fork() creation d'une copie du process http://manpagesfr.free.fr/man/man2/fork.2.html -LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR +ioctl() modifie des process, permet de controler un terminal + + http://manpagesfr.free.fr/man/man2/ioctl.2.html + + +Gestion des signaux : + +signal() reception de signaux + +sigaction() reception de signaux stocke dans une structure + +sigemptyset() initialise les signaux + +sigaddset() ajoute et supprime les signum + + https://linux.die.net/man/3/sigemptyset + + +Gestion des document : + +access () verifircation des droits du process appelant + + https://linux.die.net/man/2/access + +getcwd () retourne un char * du directory du process appelant + + https://man7.org/linux/man-pages/man3/getcwd.3.html + +chdir () change de directory a celui specifie + + https://man7.org/linux/man-pages/man2/chdir.2.html + +opendir() Ouvrir un répertoire + + https://man7.org/linux/man-pages/man3/opendir.3.html + +readdir() Read a directory + + http://manpagesfr.free.fr/man/man3/readdir.3.html + +closedir () close dir + + https://man7.org/linux/man-pages/man3/closedir.3.html + +stat () récupèrent des renseignements sur le fichier pointé par pathname + +lstat () comme stat mais avec un lien symbolique + +fstat () comme stat() mais avec un fd + + https://fr.manpages.org/stat/2 + +unlink () Détruire un nom et éventuellement le fichier associé + + http://manpagesfr.free.fr/man/man2/unlink.2.html + + +Gestion Des Erreurs : + +strerror() decrit le code d'erreur passe en argument + + http://manpagesfr.free.fr/man/man3/strerror.3.html + +perror() Afficher un message d'erreur système + + http://manpagesfr.free.fr/man/man3/perror.3.html + +isatty() verifie si le fd est ouvert + + http://manpagesfr.free.fr/man/man3/isatty.3.html + +ttyname() Renvoie le path d'un fd + + http://manpagesfr.free.fr/man/man3/ttyname.3.html + +ttyslot() renvoie la position de l'entrée du terminal en cours dans un fichier + + http://manpagesfr.free.fr/man/man3/ttyslot.3.html + +getenv() donne la valeur d'une variable d'environnement + + http://manpagesfr.free.fr/man/man3/getenv.3.html + +tcgetattr() récupère les paramètres associés à l'objet référencé par fd + +tcsetattr() fixe les paramètres du terminal + + http://manpagesfr.free.fr/man/man3/termios.3.html + + +Sous-Routine : + +tgetent() + +tgetflag() + +tgetnum() + +tgetstr() + +tgoto() + +tputs() + https://linux.die.net/man/3/tgetent From 9acbd04a8de7abef1c819da9a686fdc9852d8f6f Mon Sep 17 00:00:00 2001 From: Gregoirebrn <150047639+Gregoirebrn@users.noreply.github.com> Date: Tue, 16 Apr 2024 19:44:36 +0200 Subject: [PATCH 11/86] added pipe function --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index d8f2ba9..63bd7c1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ rl_redisplay () affiche une ancienne ligne Gestion de process : +pipe () Créer un tube dans un sens de communication + + http://manpagesfr.free.fr/man/man2/pipe.2.html + execve () execute un programme, une commande de shell par exemple http://manpagesfr.free.fr/man/man2/execve.2.html From ce33a54c33ccd3f47024037d3d9a6381be59f042 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 1 Jul 2024 01:24:15 +0200 Subject: [PATCH 12/86] retry --- last_push/last | 1 + 1 file changed, 1 insertion(+) create mode 160000 last_push/last diff --git a/last_push/last b/last_push/last new file mode 160000 index 0000000..85ab972 --- /dev/null +++ b/last_push/last @@ -0,0 +1 @@ +Subproject commit 85ab9724effa3bf63ef751c5a2e8304793b16f27 From 16e7018549270448359a01592f0f7dfcadd41e7a Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 1 Jul 2024 01:25:40 +0200 Subject: [PATCH 13/86] del --- Makefile | 68 ----------- README.md | 159 ------------------------- data/header.txt | 6 - data/header3.txt | 6 - includes/minishell.h | 31 ----- last_push/last | 1 - srcs/main.c | 60 ---------- super_libft/Makefile | 82 ------------- super_libft/libft.h | 104 ---------------- super_libft/srcs/ft_atoi.c | 39 ------ super_libft/srcs/ft_bzero.c | 27 ----- super_libft/srcs/ft_calloc.c | 37 ------ super_libft/srcs/ft_isalnum.c | 21 ---- super_libft/srcs/ft_isalpha.c | 19 --- super_libft/srcs/ft_isascii.c | 18 --- super_libft/srcs/ft_isdigit.c | 18 --- super_libft/srcs/ft_isprint.c | 18 --- super_libft/srcs/ft_itoa.c | 66 ---------- super_libft/srcs/ft_lstadd_back.c | 29 ----- super_libft/srcs/ft_lstadd_front.c | 22 ---- super_libft/srcs/ft_lstclear.c | 28 ----- super_libft/srcs/ft_lstdelone.c | 21 ---- super_libft/srcs/ft_lstiter.c | 27 ----- super_libft/srcs/ft_lstlast.c | 22 ---- super_libft/srcs/ft_lstmap.c | 38 ------ super_libft/srcs/ft_lstnew.c | 25 ---- super_libft/srcs/ft_lstsize.c | 28 ----- super_libft/srcs/ft_memchr.c | 27 ----- super_libft/srcs/ft_memcmp.c | 27 ----- super_libft/srcs/ft_memcpy.c | 28 ----- super_libft/srcs/ft_memmove.c | 25 ---- super_libft/srcs/ft_memset.c | 28 ----- super_libft/srcs/ft_printf.c | 83 ------------- super_libft/srcs/ft_printf_utils.c | 81 ------------- super_libft/srcs/ft_putchar_fd.c | 18 --- super_libft/srcs/ft_putendl_fd.c | 19 --- super_libft/srcs/ft_putnbr_fd.c | 34 ------ super_libft/srcs/ft_putstr_fd.c | 25 ---- super_libft/srcs/ft_split.c | 106 ----------------- super_libft/srcs/ft_strchr.c | 25 ---- super_libft/srcs/ft_strdup.c | 33 ----- super_libft/srcs/ft_striteri.c | 25 ---- super_libft/srcs/ft_strjoin.c | 40 ------- super_libft/srcs/ft_strlcat.c | 38 ------ super_libft/srcs/ft_strlcpy.c | 33 ----- super_libft/srcs/ft_strlen.c | 23 ---- super_libft/srcs/ft_strmapi.c | 33 ----- super_libft/srcs/ft_strncmp.c | 27 ----- super_libft/srcs/ft_strnstr.c | 35 ------ super_libft/srcs/ft_strrchr.c | 30 ----- super_libft/srcs/ft_strtrim.c | 73 ------------ super_libft/srcs/ft_substr.c | 38 ------ super_libft/srcs/ft_tolower.c | 18 --- super_libft/srcs/ft_toupper.c | 18 --- super_libft/srcs/get_next_line.c | 63 ---------- super_libft/srcs/get_next_line_utils.c | 101 ---------------- 56 files changed, 2174 deletions(-) delete mode 100644 Makefile delete mode 100644 README.md delete mode 100644 data/header.txt delete mode 100644 data/header3.txt delete mode 100644 includes/minishell.h delete mode 160000 last_push/last delete mode 100644 srcs/main.c delete mode 100644 super_libft/Makefile delete mode 100644 super_libft/libft.h delete mode 100644 super_libft/srcs/ft_atoi.c delete mode 100644 super_libft/srcs/ft_bzero.c delete mode 100644 super_libft/srcs/ft_calloc.c delete mode 100644 super_libft/srcs/ft_isalnum.c delete mode 100644 super_libft/srcs/ft_isalpha.c delete mode 100644 super_libft/srcs/ft_isascii.c delete mode 100644 super_libft/srcs/ft_isdigit.c delete mode 100644 super_libft/srcs/ft_isprint.c delete mode 100644 super_libft/srcs/ft_itoa.c delete mode 100644 super_libft/srcs/ft_lstadd_back.c delete mode 100644 super_libft/srcs/ft_lstadd_front.c delete mode 100644 super_libft/srcs/ft_lstclear.c delete mode 100644 super_libft/srcs/ft_lstdelone.c delete mode 100644 super_libft/srcs/ft_lstiter.c delete mode 100644 super_libft/srcs/ft_lstlast.c delete mode 100644 super_libft/srcs/ft_lstmap.c delete mode 100644 super_libft/srcs/ft_lstnew.c delete mode 100644 super_libft/srcs/ft_lstsize.c delete mode 100644 super_libft/srcs/ft_memchr.c delete mode 100644 super_libft/srcs/ft_memcmp.c delete mode 100644 super_libft/srcs/ft_memcpy.c delete mode 100644 super_libft/srcs/ft_memmove.c delete mode 100644 super_libft/srcs/ft_memset.c delete mode 100644 super_libft/srcs/ft_printf.c delete mode 100644 super_libft/srcs/ft_printf_utils.c delete mode 100644 super_libft/srcs/ft_putchar_fd.c delete mode 100644 super_libft/srcs/ft_putendl_fd.c delete mode 100644 super_libft/srcs/ft_putnbr_fd.c delete mode 100644 super_libft/srcs/ft_putstr_fd.c delete mode 100644 super_libft/srcs/ft_split.c delete mode 100644 super_libft/srcs/ft_strchr.c delete mode 100644 super_libft/srcs/ft_strdup.c delete mode 100644 super_libft/srcs/ft_striteri.c delete mode 100644 super_libft/srcs/ft_strjoin.c delete mode 100644 super_libft/srcs/ft_strlcat.c delete mode 100644 super_libft/srcs/ft_strlcpy.c delete mode 100644 super_libft/srcs/ft_strlen.c delete mode 100644 super_libft/srcs/ft_strmapi.c delete mode 100644 super_libft/srcs/ft_strncmp.c delete mode 100644 super_libft/srcs/ft_strnstr.c delete mode 100644 super_libft/srcs/ft_strrchr.c delete mode 100644 super_libft/srcs/ft_strtrim.c delete mode 100644 super_libft/srcs/ft_substr.c delete mode 100644 super_libft/srcs/ft_tolower.c delete mode 100644 super_libft/srcs/ft_toupper.c delete mode 100644 super_libft/srcs/get_next_line.c delete mode 100644 super_libft/srcs/get_next_line_utils.c diff --git a/Makefile b/Makefile deleted file mode 100644 index 611e1de..0000000 --- a/Makefile +++ /dev/null @@ -1,68 +0,0 @@ -######################################################################################################################## -# VARIABLE # -######################################################################################################################## - -SRCS := main.c - -SRCS_D := srcs/ - -OBJS_D := objs/ - -OBJS := $(SRCS:%.c=$(OBJS_D)%.o) - -HEAD := includes/minishell.h - -HEAD_D := . - -CFLAGS := -Wall -Wextra -Werror -g3 - -NAME := minishell - -######################################################################################################################## -# LIB # -######################################################################################################################## - -LIB := libft.a - -LIB_D := super_libft/ - -LIB_I := $(LIB_D) - -LIB_H := $(LIB_I)libft.h - -LIB_A := $(addprefix $(LIB_D), $(LIB)) - -######################################################################################################################## -# RULES # -######################################################################################################################## - -all : lib - $(MAKE) $(NAME) - -lib : - $(MAKE) -C $(LIB_D) - -$(NAME) : $(OBJS_D) $(OBJS) $(LIB_A) $(HEAD) - $(CC) $(CFLAGS) -o $(NAME) $(OBJS) $(LIB_A) - -$(OBJS) : $(OBJS_D)%.o: $(SRCS_D)%.c $(HEAD) $(LIB_H) - $(CC) $(CFLAGS) -I/usr/include -Isuper_libft -c $< -o $@ - -$(OBJS_D) : - @mkdir -p $(OBJS_D) - -######################################################################################################################## -# COMMANDS # -######################################################################################################################## - -clean : - $(RM) -r $(OBJS) $(OBJS_D) - $(MAKE) clean -C super_libft - -fclean : clean - $(RM) $(NAME) $(NAME_B) - $(MAKE) fclean -C super_libft - -re : fclean all - -.PHONY: all clean fclean re lib \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 63bd7c1..0000000 --- a/README.md +++ /dev/null @@ -1,159 +0,0 @@ -LEARN GIT : https://learngitbranching.js.org/?locale=fr_FR - - - Documentation pour les fonctions : - - -Gestion des lignes recu en prompt : - -readline () lis le prompt et stock dans un char * - -rl_on_new_line () - -rl_replace_line () - -add_history () sauvegarde des lignes - -rl_clear_history () vide l'historique - -rl_redisplay () affiche une ancienne ligne - - https://tiswww.case.edu/php/chet/readline/readline.html - - -Gestion de process : - -pipe () Créer un tube dans un sens de communication - - http://manpagesfr.free.fr/man/man2/pipe.2.html - -execve () execute un programme, une commande de shell par exemple - - http://manpagesfr.free.fr/man/man2/execve.2.html - -dup () Dupliquer un descripteur de fichier - -dup () transforme newfd en une copie de oldfd - - https://www.man-linux-magique.net/man2/dup2.html - -wait() attend jusqu'a que l'un de ses fils est termine - -waitpid() attend jusqu'a que le process specifie est termine - - https://www.man-linux-magique.net/man2/waitpid.html - -wait3() attend n’importe quel fils - -wait4() attend un ou des fils spécifique(s) - - https://www.man-linux-magique.net/man2/wait3.html -fork() creation d'une copie du process - - http://manpagesfr.free.fr/man/man2/fork.2.html - -ioctl() modifie des process, permet de controler un terminal - - http://manpagesfr.free.fr/man/man2/ioctl.2.html - - -Gestion des signaux : - -signal() reception de signaux - -sigaction() reception de signaux stocke dans une structure - -sigemptyset() initialise les signaux - -sigaddset() ajoute et supprime les signum - - https://linux.die.net/man/3/sigemptyset - - -Gestion des document : - -access () verifircation des droits du process appelant - - https://linux.die.net/man/2/access - -getcwd () retourne un char * du directory du process appelant - - https://man7.org/linux/man-pages/man3/getcwd.3.html - -chdir () change de directory a celui specifie - - https://man7.org/linux/man-pages/man2/chdir.2.html - -opendir() Ouvrir un répertoire - - https://man7.org/linux/man-pages/man3/opendir.3.html - -readdir() Read a directory - - http://manpagesfr.free.fr/man/man3/readdir.3.html - -closedir () close dir - - https://man7.org/linux/man-pages/man3/closedir.3.html - -stat () récupèrent des renseignements sur le fichier pointé par pathname - -lstat () comme stat mais avec un lien symbolique - -fstat () comme stat() mais avec un fd - - https://fr.manpages.org/stat/2 - -unlink () Détruire un nom et éventuellement le fichier associé - - http://manpagesfr.free.fr/man/man2/unlink.2.html - - -Gestion Des Erreurs : - -strerror() decrit le code d'erreur passe en argument - - http://manpagesfr.free.fr/man/man3/strerror.3.html - -perror() Afficher un message d'erreur système - - http://manpagesfr.free.fr/man/man3/perror.3.html - -isatty() verifie si le fd est ouvert - - http://manpagesfr.free.fr/man/man3/isatty.3.html - -ttyname() Renvoie le path d'un fd - - http://manpagesfr.free.fr/man/man3/ttyname.3.html - -ttyslot() renvoie la position de l'entrée du terminal en cours dans un fichier - - http://manpagesfr.free.fr/man/man3/ttyslot.3.html - -getenv() donne la valeur d'une variable d'environnement - - http://manpagesfr.free.fr/man/man3/getenv.3.html - -tcgetattr() récupère les paramètres associés à l'objet référencé par fd - -tcsetattr() fixe les paramètres du terminal - - http://manpagesfr.free.fr/man/man3/termios.3.html - - -Sous-Routine : - -tgetent() - -tgetflag() - -tgetnum() - -tgetstr() - -tgoto() - -tputs() - - https://linux.die.net/man/3/tgetent diff --git a/data/header.txt b/data/header.txt deleted file mode 100644 index 36ef5c4..0000000 --- a/data/header.txt +++ /dev/null @@ -1,6 +0,0 @@ - _ __ __ __ _ _ __ ____ -| | / /__ / /________ ____ ___ ___ / /_____ ____ ___ (_)___ (_)____/ /_ ___ / / / -| | /| / / _ \/ / ___/ __ \/ __ `__ \/ _ \ / __/ __ \ / __ `__ \/ / __ \/ / ___/ __ \/ _ \/ / / -| |/ |/ / __/ / /__/ /_/ / / / / / / __/ / /_/ /_/ / / / / / / / / / / / (__ ) / / / __/ / / -|__/|__/\___/_/\___/\____/_/ /_/ /_/\___/ \__/\____/ /_/ /_/ /_/_/_/ /_/_/____/_/ /_/\___/_/_/ - by grebrune and beroy \ No newline at end of file diff --git a/data/header3.txt b/data/header3.txt deleted file mode 100644 index bb617da..0000000 --- a/data/header3.txt +++ /dev/null @@ -1,6 +0,0 @@ -.__ ___. .__ __ .___ __ -| | _____ ___________ ____ ______ ______ ____ \_ |__ |__|/ |_ ____ __| _/____ | | _______ _____ ____ -| | \__ \ / ___\_ __ \/ _ \/ ___// ___// __ \ | __ \| \ __\/ __ \ / __ |/ __ \ | |/ /\__ \ \__ \ / \ -| |__/ __ \_ / /_/ > | \( <_> )___ \ \___ \\ ___/ | \_\ \ || | \ ___/ / /_/ \ ___/ | < / __ \_/ __ \| | \ -|____(____ / \___ /|__| \____/____ >____ >\___ > |___ /__||__| \___ > \____ |\___ > |__|_ \(____ (____ /___| / - \/ /_____/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h deleted file mode 100644 index ccb9e1f..0000000 --- a/includes/minishell.h +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* minishell.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/11 16:24:43 by beroy #+# #+# */ -/* Updated: 2024/04/11 16:47:32 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef MINISHELL_H -# define MINISHELL_H - -# include "../super_libft/libft.h" -# include -# include -# include - -// COLOR CODES - -# define BLACK "\033[0;30m" -# define RED "\033[0;31m" -# define GREEN "\033[0;32m" -# define YELLOW "\033[0;33m" -# define BLUE "\033[0;34m" -# define PURPLE "\033[0;35m" -# define CYAN "\033[0;36m" - -#endif diff --git a/last_push/last b/last_push/last deleted file mode 160000 index 85ab972..0000000 --- a/last_push/last +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 85ab9724effa3bf63ef751c5a2e8304793b16f27 diff --git a/srcs/main.c b/srcs/main.c deleted file mode 100644 index e4273c0..0000000 --- a/srcs/main.c +++ /dev/null @@ -1,60 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/04/11 18:40:37 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/minishell.h" -char *ft_color(int i) -{ - if (i % 6 == 0) - return (YELLOW); - if (i % 6 == 1) - return (GREEN); - if (i % 6 == 2) - return (CYAN); - if (i % 6 == 3) - return (BLUE); - if (i % 6 == 4) - return (PURPLE); - else - return (RED); -} - -void ft_header(void) -{ - char *str; - char *color; - int fd; - int i; - - fd = open("data/header.txt", O_RDONLY); - str = get_next_line(fd); - i = 0; - while (str) - { - color = ft_color(i); - printf("%s%s", color, str); - str = get_next_line(fd); - i++; - } - printf("\033[0m\n"); -} - -int main(void) -{ - char input[1025]; - size_t rd; - - ft_header(); - write(1, "> ", 2); - rd = read(1, input, 1024); - input[rd] = 0; - printf("%s", input); -} diff --git a/super_libft/Makefile b/super_libft/Makefile deleted file mode 100644 index 2c4fe01..0000000 --- a/super_libft/Makefile +++ /dev/null @@ -1,82 +0,0 @@ -SRCS = ft_atoi.c \ - ft_bzero.c \ - ft_calloc.c \ - ft_isalnum.c \ - ft_isalpha.c \ - ft_isascii.c \ - ft_isdigit.c \ - ft_isprint.c \ - ft_itoa.c \ - ft_memchr.c \ - ft_memcmp.c \ - ft_memcpy.c \ - ft_memmove.c \ - ft_memset.c \ - ft_putchar_fd.c \ - ft_putendl_fd.c \ - ft_putnbr_fd.c \ - ft_putstr_fd.c \ - ft_split.c \ - ft_strchr.c \ - ft_strdup.c \ - ft_striteri.c \ - ft_strjoin.c \ - ft_strlcat.c \ - ft_strlcpy.c \ - ft_strlen.c \ - ft_strmapi.c \ - ft_strncmp.c \ - ft_strnstr.c \ - ft_strrchr.c \ - ft_strtrim.c \ - ft_substr.c \ - ft_tolower.c \ - ft_toupper.c \ - ft_lstadd_back.c \ - ft_lstadd_front.c \ - ft_lstclear.c \ - ft_lstdelone.c \ - ft_lstiter.c \ - ft_lstlast.c \ - ft_lstmap.c \ - ft_lstnew.c \ - ft_lstsize.c \ - get_next_line.c \ - get_next_line_utils.c \ - ft_printf.c \ - ft_printf_utils.c - -SRCS_D := srcs/ - -OBJS_D := objs/ - -OBJS := $(SRCS:%.c=$(OBJS_D)%.o) - -HEAD_D := . - -CFLAGS := -Wall -Wextra -Werror - -AR := ar -rc - -NAME := libft.a - -all : $(NAME) - -$(NAME) : $(OBJS_D) $(OBJS) Makefile - $(AR) $(NAME) $(OBJS) - -$(OBJS) : $(OBJS_D)%.o: $(SRCS_D)%.c $(HEAD) - $(CC) $(CFLAGS) -c $< -o $@ - -$(OBJS_D) : - @mkdir -p $(OBJS_D) - -clean : - $(RM) -r $(OBJS) $(OBJS_D) $(OBJS_B) - -fclean : clean - $(RM) $(NAME) - -re : fclean all - -.PHONY: all bonus clean fclean re \ No newline at end of file diff --git a/super_libft/libft.h b/super_libft/libft.h deleted file mode 100644 index 3810575..0000000 --- a/super_libft/libft.h +++ /dev/null @@ -1,104 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* libft.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 13:57:13 by beroy #+# #+# */ -/* Updated: 2024/01/17 16:10:23 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef LIBFT_H -# define LIBFT_H - -# include -# include -# include -# include -# include -# include - -# ifndef BUFFER_SIZE -# define BUFFER_SIZE 3 -# endif -# ifndef OPEN_MAX -# define OPEN_MAX 1024 -# endif - -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; - -void ft_bzero(void *s, size_t n); -void ft_striteri(char *s, void (*f)(unsigned int, char*)); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *str, int fd); -void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); - -void *ft_memchr(const void *s, int c, size_t n); -void *ft_memcpy(void *dest, const void *src, size_t n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memset(void *s, int c, size_t n); -void *ft_calloc(size_t nmemb, size_t size); - -int ft_memcmp(const void *s1, const void *s2, size_t n); -int ft_strncmp(char *s1, const char *s2, size_t n); -int ft_isalnum(int c); -int ft_isalpha(int c); -int ft_isascii(int c); -int ft_isdigit(int c); -int ft_isprint(int c); -int ft_tolower(int c); -int ft_toupper(int c); -int ft_atoi(const char *nptr); - -size_t ft_strlen(const char *str); -size_t ft_strlcpy(char *dst, const char *src, size_t size); -size_t ft_strlcat(char *dest, char *src, unsigned int size); - -char *ft_strchr(const char *s, int c); -char *ft_strrchr(const char *s, int c); -char *ft_strdup(const char *s); -char *ft_substr(char const *s, unsigned int start, size_t len); -char *ft_strjoin(char const *s1, char const *s2); -char *ft_strtrim(char const *s1, char const *set); -char *ft_itoa(int n); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -char *ft_strnstr(const char *big, const char *little, size_t len); - -char **ft_split(char const *s, char c); - -t_list *ft_lstnew(void *content); -t_list *ft_lstlast(t_list *lst); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); - -int ft_lstsize(t_list *lst); - -void ft_lstadd_front(t_list **lst, t_list *new); -void ft_lstadd_back(t_list **lst, t_list *new); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); - -// GNL - -char *get_next_line(int fd); -char *ft_strdup_nl(char *str); -int ft_linecheck(char *str); -char *ft_strjoin_nl(char *s1, char *s2); -void ft_buffclean(char *str); - -// Prinft - -int ft_printf(const char *str, ...); -ssize_t ft_putchar(int c); -ssize_t ft_putstr(char *str); -void ft_putnbr_base(ssize_t nbr, char *str, ssize_t *length); -void ft_putnbr_ul(size_t nbr, char *str, ssize_t *length); - -#endif \ No newline at end of file diff --git a/super_libft/srcs/ft_atoi.c b/super_libft/srcs/ft_atoi.c deleted file mode 100644 index 14c435f..0000000 --- a/super_libft/srcs/ft_atoi.c +++ /dev/null @@ -1,39 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_atoi.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 14:21:21 by beroy #+# #+# */ -/* Updated: 2023/11/07 18:41:02 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -int ft_atoi(const char *nptr) -{ - int i; - int res; - int sign; - - i = 0; - res = 0; - sign = 1; - while ((nptr[i] > 8 && nptr[i] < 14) || nptr[i] == ' ') - i++; - if (nptr[i] == '+') - i++; - else if (nptr[i] == '-') - { - sign *= -1; - i++; - } - while (ft_isdigit(nptr[i]) == 1) - { - res = res * 10 + (nptr[i] - 48); - i++; - } - return (res * sign); -} diff --git a/super_libft/srcs/ft_bzero.c b/super_libft/srcs/ft_bzero.c deleted file mode 100644 index d4a3129..0000000 --- a/super_libft/srcs/ft_bzero.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_bzero.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:52:19 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:56:54 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_bzero(void *s, size_t n) -{ - size_t i; - char *temp; - - temp = (char *)s; - i = 0; - while (i < n) - { - temp[i] = '\0'; - i++; - } -} diff --git a/super_libft/srcs/ft_calloc.c b/super_libft/srcs/ft_calloc.c deleted file mode 100644 index 27b9ad3..0000000 --- a/super_libft/srcs/ft_calloc.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_calloc.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 10:35:43 by beroy #+# #+# */ -/* Updated: 2023/11/14 11:42:55 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void *ft_calloc(size_t nmemb, size_t size) -{ - void *ptr; - size_t i; - - if (nmemb == 0 || size == 0) - { - ptr = malloc(nmemb * size); - return (ptr); - } - if (nmemb > SIZE_MAX / size || size > SIZE_MAX / nmemb) - return (NULL); - ptr = malloc(nmemb * size); - if (ptr == NULL) - return (ptr); - i = 0; - while (i < nmemb * size) - { - ((unsigned char *)ptr)[i] = 0; - i++; - } - return (ptr); -} diff --git a/super_libft/srcs/ft_isalnum.c b/super_libft/srcs/ft_isalnum.c deleted file mode 100644 index eb36ec8..0000000 --- a/super_libft/srcs/ft_isalnum.c +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isalnum.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:04:27 by beroy #+# #+# */ -/* Updated: 2023/11/06 15:34:45 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isalnum(int c) -{ - if (c < 48 - || (c > 57 && c < 65) - || (c > 90 && c < 97) - || c > 122) - return (0); - return (1); -} diff --git a/super_libft/srcs/ft_isalpha.c b/super_libft/srcs/ft_isalpha.c deleted file mode 100644 index 76f047d..0000000 --- a/super_libft/srcs/ft_isalpha.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isalpha.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 14:32:56 by beroy #+# #+# */ -/* Updated: 2023/11/06 15:30:56 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isalpha(int c) -{ - if ((c >= 65 && c <= 90) - || (c >= 97 && c <= 122)) - return (1); - return (0); -} diff --git a/super_libft/srcs/ft_isascii.c b/super_libft/srcs/ft_isascii.c deleted file mode 100644 index aa24c27..0000000 --- a/super_libft/srcs/ft_isascii.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isascii.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:08:35 by beroy #+# #+# */ -/* Updated: 2023/11/06 15:35:46 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isascii(int c) -{ - if (c < 0 || c > 127) - return (0); - return (1); -} diff --git a/super_libft/srcs/ft_isdigit.c b/super_libft/srcs/ft_isdigit.c deleted file mode 100644 index 35121c4..0000000 --- a/super_libft/srcs/ft_isdigit.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isdigit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 14:50:41 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:56:47 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isdigit(int c) -{ - if (c < 48 || c > 57) - return (0); - return (1); -} diff --git a/super_libft/srcs/ft_isprint.c b/super_libft/srcs/ft_isprint.c deleted file mode 100644 index 035965e..0000000 --- a/super_libft/srcs/ft_isprint.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_isprint.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:18:22 by beroy #+# #+# */ -/* Updated: 2023/11/09 10:48:13 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_isprint(int c) -{ - if (c < 32 || c >= 127) - return (0); - return (1); -} diff --git a/super_libft/srcs/ft_itoa.c b/super_libft/srcs/ft_itoa.c deleted file mode 100644 index 7538f3e..0000000 --- a/super_libft/srcs/ft_itoa.c +++ /dev/null @@ -1,66 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_itoa.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 15:11:45 by beroy #+# #+# */ -/* Updated: 2023/11/08 15:48:57 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -static void ft_strfill(long int n, char *str, long int *i) -{ - if (n < 0) - { - str[*i] = '-'; - *i += 1; - ft_strfill(n * -1, str, i); - } - else if (n > 9) - { - ft_strfill(n / 10, str, i); - ft_strfill(n % 10, str, i); - } - else - { - str[*i] = n + 48; - *i += 1; - } -} - -static void ft_strsize(long int n, long int *size) -{ - if (n < 0) - { - *size += 1; - ft_strsize(n * -1, size); - } - else if (n > 9) - { - ft_strsize(n / 10, size); - ft_strsize(n % 10, size); - } - else - *size += 1; -} - -char *ft_itoa(int n) -{ - long int size; - long int i; - char *str; - - size = 0; - i = 0; - ft_strsize(n, &size); - str = malloc(sizeof(char) * (size + 1)); - if (str == NULL) - return (str); - ft_strfill(n, str, &i); - str[i] = 0; - return (str); -} diff --git a/super_libft/srcs/ft_lstadd_back.c b/super_libft/srcs/ft_lstadd_back.c deleted file mode 100644 index 1cff03d..0000000 --- a/super_libft/srcs/ft_lstadd_back.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstadd_back.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/13 13:00:56 by beroy #+# #+# */ -/* Updated: 2023/11/14 11:20:43 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_lstadd_back(t_list **lst, t_list *new) -{ - t_list *last; - - if (lst) - { - if (*lst == NULL) - *lst = new; - else - { - last = ft_lstlast(*lst); - last->next = new; - } - } -} diff --git a/super_libft/srcs/ft_lstadd_front.c b/super_libft/srcs/ft_lstadd_front.c deleted file mode 100644 index 0942a37..0000000 --- a/super_libft/srcs/ft_lstadd_front.c +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstadd_front.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/13 10:50:44 by sweek #+# #+# */ -/* Updated: 2023/11/14 11:01:48 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_lstadd_front(t_list **lst, t_list *new) -{ - if (lst && new) - { - new->next = *lst; - *lst = new; - } -} diff --git a/super_libft/srcs/ft_lstclear.c b/super_libft/srcs/ft_lstclear.c deleted file mode 100644 index 7ac527a..0000000 --- a/super_libft/srcs/ft_lstclear.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstclear.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/14 12:07:11 by beroy #+# #+# */ -/* Updated: 2023/11/14 13:40:20 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_lstclear(t_list **lst, void (*del)(void *)) -{ - t_list *tmp; - - if (!lst || !del) - return ; - tmp = *lst; - while (*lst) - { - tmp = tmp->next; - ft_lstdelone(*lst, del); - *lst = tmp; - } -} diff --git a/super_libft/srcs/ft_lstdelone.c b/super_libft/srcs/ft_lstdelone.c deleted file mode 100644 index c071719..0000000 --- a/super_libft/srcs/ft_lstdelone.c +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstdelone.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/13 13:43:17 by beroy #+# #+# */ -/* Updated: 2023/11/14 11:57:23 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_lstdelone(t_list *lst, void (*del)(void *)) -{ - if (!lst || !del) - return ; - del(lst->content); - free(lst); -} diff --git a/super_libft/srcs/ft_lstiter.c b/super_libft/srcs/ft_lstiter.c deleted file mode 100644 index f4d807a..0000000 --- a/super_libft/srcs/ft_lstiter.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstiter.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/14 14:03:02 by beroy #+# #+# */ -/* Updated: 2023/11/14 14:12:47 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_lstiter(t_list *lst, void (*f)(void *)) -{ - t_list *tmp; - - if (!lst || !f) - return ; - tmp = lst; - while (tmp) - { - f(tmp->content); - tmp = tmp->next; - } -} diff --git a/super_libft/srcs/ft_lstlast.c b/super_libft/srcs/ft_lstlast.c deleted file mode 100644 index 29225f6..0000000 --- a/super_libft/srcs/ft_lstlast.c +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstlast.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/13 11:40:56 by beroy #+# #+# */ -/* Updated: 2023/11/13 16:23:13 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -t_list *ft_lstlast(t_list *lst) -{ - if (!lst) - return (NULL); - while (lst->next != NULL) - lst = lst->next; - return (lst); -} diff --git a/super_libft/srcs/ft_lstmap.c b/super_libft/srcs/ft_lstmap.c deleted file mode 100644 index f4f7329..0000000 --- a/super_libft/srcs/ft_lstmap.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstmap.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/14 14:14:01 by beroy #+# #+# */ -/* Updated: 2023/11/14 15:06:13 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) -{ - t_list *new; - t_list *head; - void *tmp; - - head = NULL; - if (!lst || !f || !del) - return (NULL); - while (lst) - { - tmp = f(lst->content); - new = ft_lstnew(tmp); - if (new == NULL) - { - del(tmp); - ft_lstclear(&head, del); - return (head); - } - ft_lstadd_back(&head, new); - lst = lst->next; - } - return (head); -} diff --git a/super_libft/srcs/ft_lstnew.c b/super_libft/srcs/ft_lstnew.c deleted file mode 100644 index 89cbd7b..0000000 --- a/super_libft/srcs/ft_lstnew.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstnew.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/10 14:28:15 by sweek #+# #+# */ -/* Updated: 2023/11/13 10:55:18 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -t_list *ft_lstnew(void *content) -{ - t_list *s_new; - - s_new = malloc(sizeof(t_list)); - if (s_new == NULL) - return (s_new); - s_new->content = content; - s_new->next = NULL; - return (s_new); -} diff --git a/super_libft/srcs/ft_lstsize.c b/super_libft/srcs/ft_lstsize.c deleted file mode 100644 index f0ea37d..0000000 --- a/super_libft/srcs/ft_lstsize.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstsize.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/13 11:16:51 by beroy #+# #+# */ -/* Updated: 2023/11/13 11:23:54 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -int ft_lstsize(t_list *lst) -{ - int i; - - if (!lst) - return (0); - i = 1; - while (lst->next != NULL) - { - lst = lst->next; - i++; - } - return (i); -} diff --git a/super_libft/srcs/ft_memchr.c b/super_libft/srcs/ft_memchr.c deleted file mode 100644 index 36efe9a..0000000 --- a/super_libft/srcs/ft_memchr.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 16:00:25 by beroy #+# #+# */ -/* Updated: 2023/11/09 13:43:15 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void *ft_memchr(const void *s, int c, size_t n) -{ - size_t i; - - i = 0; - while (i < n) - { - if (((unsigned char *)s)[i] == (unsigned char)c) - return ((unsigned char *)s + i); - i++; - } - return (NULL); -} diff --git a/super_libft/srcs/ft_memcmp.c b/super_libft/srcs/ft_memcmp.c deleted file mode 100644 index 7234ae4..0000000 --- a/super_libft/srcs/ft_memcmp.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memcmp.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 17:04:40 by beroy #+# #+# */ -/* Updated: 2023/11/07 18:01:41 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -int ft_memcmp(const void *s1, const void *s2, size_t n) -{ - size_t i; - - i = 0; - while (i < n) - { - if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i]) - return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); - i++; - } - return (0); -} diff --git a/super_libft/srcs/ft_memcpy.c b/super_libft/srcs/ft_memcpy.c deleted file mode 100644 index 59c3c07..0000000 --- a/super_libft/srcs/ft_memcpy.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memcpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:55:10 by beroy #+# #+# */ -/* Updated: 2023/11/07 14:20:12 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void *ft_memcpy(void *dest, const void *src, size_t n) -{ - size_t i; - - if (!dest && !src) - return (dest); - i = 0; - while (i < n) - { - ((char *) dest)[i] = ((char *)src)[i]; - i++; - } - return (dest); -} diff --git a/super_libft/srcs/ft_memmove.c b/super_libft/srcs/ft_memmove.c deleted file mode 100644 index df9fb00..0000000 --- a/super_libft/srcs/ft_memmove.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memmove.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:57:04 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:57:05 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void *ft_memmove(void *dest, const void *src, size_t n) -{ - if (!dest && !src) - return (dest); - if (src < dest) - while (n-- > 0) - ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; - else - ft_memcpy(dest, src, n); - return (dest); -} diff --git a/super_libft/srcs/ft_memset.c b/super_libft/srcs/ft_memset.c deleted file mode 100644 index 21fd021..0000000 --- a/super_libft/srcs/ft_memset.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_memset.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:44:54 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:57:02 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void *ft_memset(void *s, int c, size_t n) -{ - size_t i; - char *temp; - - temp = (char *)s; - i = 0; - while (i < n) - { - temp[i] = c; - i++; - } - return (temp); -} diff --git a/super_libft/srcs/ft_printf.c b/super_libft/srcs/ft_printf.c deleted file mode 100644 index dbf05e9..0000000 --- a/super_libft/srcs/ft_printf.c +++ /dev/null @@ -1,83 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_printf.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/20 19:03:15 by beroy #+# #+# */ -/* Updated: 2024/01/17 16:17:27 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_printmemory(size_t arg, ssize_t *length) -{ - if ((char *)arg == NULL) - { - *length = ft_putstr("(nil)"); - return ; - } - *length = ft_putstr("0x"); - if (*length == -1) - return ; - ft_putnbr_ul(arg, "0123456789abcdef", length); -} - -void ft_checkarg(const char c, va_list arg, ssize_t *length) -{ - ssize_t tmp; - - tmp = 0; - if (c == 'c') - tmp = ft_putchar((unsigned char)va_arg(arg, int)); - else if (c == 's') - tmp = ft_putstr(va_arg(arg, char *)); - else if (c == 'p') - ft_printmemory((size_t)va_arg(arg, size_t), &tmp); - else if (c == 'd' || c == 'i') - ft_putnbr_base(va_arg(arg, int), "0123456789", &tmp); - else if (c == 'u') - ft_putnbr_base(va_arg(arg, unsigned int), "0123456789", &tmp); - else if (c == 'x') - ft_putnbr_base(va_arg(arg, unsigned int), "0123456789abcdef", &tmp); - else if (c == 'X') - ft_putnbr_base(va_arg(arg, unsigned int), "0123456789ABCDEF", &tmp); - else if (c == '%') - tmp += ft_putchar('%'); - else if (c == 0) - tmp = -1; - if (tmp == -1) - *length = tmp; - else - *length += tmp; -} - -int ft_printf(const char *str, ...) -{ - size_t i; - va_list args; - ssize_t length; - - if (!str) - return (-1); - i = 0; - length = 0; - va_start(args, str); - while (str[i]) - { - if (str[i] != '%') - { - if (ft_putchar(str[i]) == -1) - return (va_end(args), -1); - length++; - } - else - ft_checkarg(str[++i], args, &length); - if (length == -1) - break ; - i++; - } - return (va_end(args), length); -} diff --git a/super_libft/srcs/ft_printf_utils.c b/super_libft/srcs/ft_printf_utils.c deleted file mode 100644 index 75b2da9..0000000 --- a/super_libft/srcs/ft_printf_utils.c +++ /dev/null @@ -1,81 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_printf_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/20 19:18:35 by beroy #+# #+# */ -/* Updated: 2024/01/17 16:20:00 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -ssize_t ft_putchar(int c) -{ - return (write(1, &c, 1)); -} - -ssize_t ft_putstr(char *str) -{ - ssize_t i; - ssize_t j; - - i = 0; - if (!str) - return (ft_putstr("(null)")); - while (str[i]) - { - j = ft_putchar(str[i]); - if (j == -1) - return (-1); - i += j; - } - return (i); -} - -void ft_putnbr_base(ssize_t nbr, char *str, ssize_t *length) -{ - ssize_t i; - - if (nbr < 0) - { - i = ft_putchar('-'); - if (i == -1) - return (*length = -1, (void)0); - *length += i; - ft_putnbr_base(nbr * -1, str, length); - if (*length == -1) - return (*length = -1, (void)0); - } - else - { - if (nbr >= (ssize_t)ft_strlen(str)) - { - ft_putnbr_base(nbr / ft_strlen(str), str, length); - if (*length == -1) - return (*length = -1, (void)0); - } - i = ft_putchar(str[nbr % ft_strlen(str)]); - if (i == -1) - return (*length = -1, (void)0); - *length += i; - } -} - -void ft_putnbr_ul(size_t nbr, char *str, ssize_t *length) -{ - ssize_t i; - - if (nbr >= ft_strlen(str)) - { - ft_putnbr_ul(nbr / ft_strlen(str), str, length); - if (*length == -1) - return (*length = -1, (void)0); - } - i = ft_putchar(str[nbr % ft_strlen(str)]); - if (i == -1) - return (*length = -1, (void)0); - *length += i; -} diff --git a/super_libft/srcs/ft_putchar_fd.c b/super_libft/srcs/ft_putchar_fd.c deleted file mode 100644 index 18c7c4d..0000000 --- a/super_libft/srcs/ft_putchar_fd.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putchar_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 16:50:12 by beroy #+# #+# */ -/* Updated: 2023/11/08 16:50:12 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putchar_fd(char c, int fd) -{ - write(fd, &c, 1); -} diff --git a/super_libft/srcs/ft_putendl_fd.c b/super_libft/srcs/ft_putendl_fd.c deleted file mode 100644 index da271e1..0000000 --- a/super_libft/srcs/ft_putendl_fd.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putendl_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 16:54:34 by beroy #+# #+# */ -/* Updated: 2023/11/08 16:59:54 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putendl_fd(char *s, int fd) -{ - ft_putstr_fd(s, fd); - ft_putchar_fd('\n', fd); -} diff --git a/super_libft/srcs/ft_putnbr_fd.c b/super_libft/srcs/ft_putnbr_fd.c deleted file mode 100644 index d73c5be..0000000 --- a/super_libft/srcs/ft_putnbr_fd.c +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putnbr_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 17:01:09 by beroy #+# #+# */ -/* Updated: 2023/11/08 17:14:53 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putnbr_fd(int n, int fd) -{ - if (n == -2147483648) - write(fd, "-2147483648", 11); - else - { - if (n < 0) - { - n *= -1; - ft_putchar_fd('-', fd); - } - if (n > 9) - { - ft_putnbr_fd(n / 10, fd); - ft_putnbr_fd(n % 10, fd); - } - else - ft_putchar_fd(n + 48, fd); - } -} diff --git a/super_libft/srcs/ft_putstr_fd.c b/super_libft/srcs/ft_putstr_fd.c deleted file mode 100644 index 1ce9095..0000000 --- a/super_libft/srcs/ft_putstr_fd.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_putstr_fd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 16:52:10 by beroy #+# #+# */ -/* Updated: 2023/11/08 16:52:10 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_putstr_fd(char *str, int fd) -{ - size_t i; - - i = 0; - while (str[i]) - { - ft_putchar_fd(str[i], fd); - i++; - } -} diff --git a/super_libft/srcs/ft_split.c b/super_libft/srcs/ft_split.c deleted file mode 100644 index 95127d5..0000000 --- a/super_libft/srcs/ft_split.c +++ /dev/null @@ -1,106 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 12:27:55 by beroy #+# #+# */ -/* Updated: 2023/11/14 13:50:14 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -static size_t ft_countwords(char const *s, char c) -{ - size_t i; - size_t words; - - words = 0; - i = 0; - if (s[i] != c && s[i] != 0) - words++; - i++; - while (s[0] && s[i]) - { - if (s[i] != c && s[i - 1] == c) - words++; - i++; - } - return (words); -} - -static size_t ft_wdlen(char const *s, char c, size_t j) -{ - size_t i; - - i = j; - while (s[i] != c && s[i] != 0) - i++; - return (i); -} - -static char *ft_superdup(char const *s, char c, size_t *j) -{ - char *str; - size_t len; - size_t i; - - len = ft_wdlen(s, c, *j); - str = malloc(sizeof(char) * (len - *j + 1)); - if (str == NULL) - return (str); - i = 0; - while (*j < len) - { - str[i] = s[*j]; - *j += 1; - i++; - } - str[i] = 0; - return (str); -} - -static void *ft_splitdestroy(char **split) -{ - size_t i; - - i = 0; - while (split[i] != NULL) - { - free(split[i]); - i++; - } - free(split); - return (NULL); -} - -char **ft_split(char const *s, char c) -{ - size_t i; - size_t j; - size_t words; - char **split; - - /*if (!s) - return (NULL);*/ - i = 0; - j = 0; - words = ft_countwords(s, c); - split = ft_calloc((words + 1), sizeof(char *)); - if (!s[0] || split == NULL) - return (split); - while (i < words) - { - if (s[j] != c) - { - split[i++] = ft_superdup(s, c, &j); - if (split[i - 1] == NULL) - return (ft_splitdestroy(split)); - } - else - j++; - } - return (split); -} diff --git a/super_libft/srcs/ft_strchr.c b/super_libft/srcs/ft_strchr.c deleted file mode 100644 index f61a209..0000000 --- a/super_libft/srcs/ft_strchr.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 14:43:35 by beroy #+# #+# */ -/* Updated: 2023/11/09 11:29:11 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strchr(const char *s, int c) -{ - int i; - - i = 0; - while (s[i] != (unsigned char)c && s[i]) - i++; - if (s[i] == 0 && (unsigned char)c != 0) - return (NULL); - return (((char *)s) + i); -} diff --git a/super_libft/srcs/ft_strdup.c b/super_libft/srcs/ft_strdup.c deleted file mode 100644 index 60dcfb3..0000000 --- a/super_libft/srcs/ft_strdup.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strdup.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 10:40:49 by beroy #+# #+# */ -/* Updated: 2023/11/08 11:50:04 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strdup(const char *s) -{ - size_t size; - size_t i; - char *ptr; - - size = ft_strlen((char *)s); - ptr = malloc((size + 1) * sizeof(char)); - if (ptr == NULL) - return (ptr); - i = 0; - while (i < size) - { - ptr[i] = s[i]; - i++; - } - ptr[i] = 0; - return (ptr); -} diff --git a/super_libft/srcs/ft_striteri.c b/super_libft/srcs/ft_striteri.c deleted file mode 100644 index e517c7d..0000000 --- a/super_libft/srcs/ft_striteri.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_striteri.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 16:45:22 by beroy #+# #+# */ -/* Updated: 2023/11/08 16:45:40 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_striteri(char *s, void (*f)(unsigned int, char*)) -{ - unsigned int i; - - i = 0; - while (s[i]) - { - f(i, &s[i]); - i++; - } -} diff --git a/super_libft/srcs/ft_strjoin.c b/super_libft/srcs/ft_strjoin.c deleted file mode 100644 index d1f482e..0000000 --- a/super_libft/srcs/ft_strjoin.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strjoin.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sweek +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 11:40:41 by beroy #+# #+# */ -/* Updated: 2023/11/12 14:37:57 by sweek ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strjoin(char const *s1, char const *s2) -{ - size_t len; - size_t i; - size_t j; - char *str; - - len = ft_strlen((char *)s1) + ft_strlen((char *)s2); - i = 0; - str = malloc (sizeof(char) * (len + 1)); - if (str == NULL) - return (str); - while (s1[i]) - { - str[i] = s1[i]; - i++; - } - j = 0; - while (s2[j]) - { - str[i + j] = s2[j]; - j++; - } - str [i + j] = 0; - return (str); -} diff --git a/super_libft/srcs/ft_strlcat.c b/super_libft/srcs/ft_strlcat.c deleted file mode 100644 index 95564d8..0000000 --- a/super_libft/srcs/ft_strlcat.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlcat.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 14:30:06 by beroy #+# #+# */ -/* Updated: 2023/11/07 14:30:06 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -size_t ft_strlcat(char *dest, char *src, unsigned int size) -{ - unsigned int isrc; - unsigned int idest; - unsigned int destlen; - unsigned int srclen; - - isrc = 0; - destlen = ft_strlen(dest); - srclen = ft_strlen(src); - idest = destlen; - if (size == 0) - return (srclen); - while (src[isrc] && idest < size - 1) - { - dest[idest] = src[isrc]; - idest++; - isrc++; - } - dest[idest] = 0; - if (size < idest) - return (srclen + size); - return (destlen + srclen); -} diff --git a/super_libft/srcs/ft_strlcpy.c b/super_libft/srcs/ft_strlcpy.c deleted file mode 100644 index 56e044e..0000000 --- a/super_libft/srcs/ft_strlcpy.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlcpy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 15:32:21 by beroy #+# #+# */ -/* Updated: 2023/11/07 15:42:27 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -size_t ft_strlcpy(char *dst, const char *src, size_t size) -{ - size_t i; - - i = 0; - if (size != 0) - { - while (src[i] != '\0' && i < size - 1) - { - dst[i] = src[i]; - i++; - } - dst[i] = '\0'; - } - i = 0; - while (src[i] != '\0') - i++; - return (i); -} diff --git a/super_libft/srcs/ft_strlen.c b/super_libft/srcs/ft_strlen.c deleted file mode 100644 index 823f6c8..0000000 --- a/super_libft/srcs/ft_strlen.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strlen.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/06 15:37:08 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:56:56 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -size_t ft_strlen(const char *str) -{ - size_t i; - - i = 0; - while (str[i]) - i++; - return (i); -} diff --git a/super_libft/srcs/ft_strmapi.c b/super_libft/srcs/ft_strmapi.c deleted file mode 100644 index dbe6cd0..0000000 --- a/super_libft/srcs/ft_strmapi.c +++ /dev/null @@ -1,33 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strmapi.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 16:14:27 by beroy #+# #+# */ -/* Updated: 2023/11/08 16:32:36 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) -{ - size_t len; - size_t i; - char *str; - - len = ft_strlen((char *)s); - i = 0; - str = malloc(sizeof(char) * (len + 1)); - if (str == NULL) - return (str); - while (s[i]) - { - str[i] = f(i, s[i]); - i++; - } - str[i] = 0; - return (str); -} diff --git a/super_libft/srcs/ft_strncmp.c b/super_libft/srcs/ft_strncmp.c deleted file mode 100644 index 1012c81..0000000 --- a/super_libft/srcs/ft_strncmp.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strncmp.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 15:26:26 by beroy #+# #+# */ -/* Updated: 2023/11/07 15:31:01 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -int ft_strncmp(char *s1, const char *s2, size_t n) -{ - size_t i; - - i = 0; - while ((s1[i] != 0 || s2[i] != 0) && i < n) - { - if (s1[i] != s2[i]) - return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); - i++; - } - return (0); -} diff --git a/super_libft/srcs/ft_strnstr.c b/super_libft/srcs/ft_strnstr.c deleted file mode 100644 index 000a0ca..0000000 --- a/super_libft/srcs/ft_strnstr.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strnstr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 17:56:43 by beroy #+# #+# */ -/* Updated: 2023/11/09 14:57:18 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strnstr(const char *big, const char *little, size_t len) -{ - size_t i; - size_t lilen; - - if (!little[0]) - return ((char *)big); - if (len == 0) - return (NULL); - i = 0; - lilen = ft_strlen((char *)little); - while (big[i] && i < len) - { - if (i + lilen > len) - return (NULL); - if (ft_strncmp((char *)big + i, little, lilen) == 0) - return ((char *)big + i); - i++; - } - return (NULL); -} diff --git a/super_libft/srcs/ft_strrchr.c b/super_libft/srcs/ft_strrchr.c deleted file mode 100644 index 77ce986..0000000 --- a/super_libft/srcs/ft_strrchr.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strrchr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 15:21:42 by beroy #+# #+# */ -/* Updated: 2023/11/09 12:29:56 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strrchr(const char *s, int c) -{ - size_t i; - - if ((unsigned char)c == 0) - return ((char *)s + ft_strlen((char *)s)); - i = ft_strlen((char *)s); - if (i == 0) - return (NULL); - i -= 1; - while (s[i] != (unsigned char)c && i) - i--; - if (i == 0 && s[0] != (unsigned char)c) - return (NULL); - return ((char *)s + i); -} diff --git a/super_libft/srcs/ft_strtrim.c b/super_libft/srcs/ft_strtrim.c deleted file mode 100644 index 8d55417..0000000 --- a/super_libft/srcs/ft_strtrim.c +++ /dev/null @@ -1,73 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strtrim.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 11:50:37 by beroy #+# #+# */ -/* Updated: 2023/11/09 16:42:46 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -static size_t ft_ischarset(char c, char const *set) -{ - size_t i; - size_t chk; - - i = 0; - chk = 0; - while (set[i]) - { - if (set[i] == c) - chk++; - i++; - } - if (chk == 0) - return (0); - return (1); -} - -static char *ft_strldup(char *s1, size_t len) -{ - size_t i; - char *str; - - i = 0; - str = malloc (sizeof(char) * (len + 1)); - if (str == NULL) - return (str); - while (i < len) - { - str[i] = s1[i]; - i++; - } - str[i] = 0; - return (str); -} - -char *ft_strtrim(char const *s1, char const *set) -{ - size_t istart; - size_t iend; - char *str; - - istart = 0; - iend = ft_strlen((char *)s1); - while (s1[istart]) - { - if (ft_ischarset(s1[istart], set) == 0) - break ; - istart++; - } - while (iend > istart) - { - if (ft_ischarset(s1[iend - 1], set) == 0) - break ; - iend--; - } - str = ft_strldup((char *)s1 + istart, iend - istart); - return (str); -} diff --git a/super_libft/srcs/ft_substr.c b/super_libft/srcs/ft_substr.c deleted file mode 100644 index e35f235..0000000 --- a/super_libft/srcs/ft_substr.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_substr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/08 11:21:38 by beroy #+# #+# */ -/* Updated: 2023/11/14 13:59:57 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_substr(char const *s, unsigned int start, size_t len) -{ - size_t i; - size_t j; - char *str; - - i = 0; - while (((unsigned char *)s)[i] && i != start) - i++; - if (len > ft_strlen((char *)s) - i) - len = ft_strlen((char *)s) - i; - str = malloc((len + 1) * sizeof(char)); - if (str == NULL) - return (str); - j = 0; - while (j < len) - { - str[j] = s[i]; - i++; - j++; - } - str[j] = 0; - return (str); -} diff --git a/super_libft/srcs/ft_tolower.c b/super_libft/srcs/ft_tolower.c deleted file mode 100644 index 1526c43..0000000 --- a/super_libft/srcs/ft_tolower.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_tolower.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 13:56:50 by beroy #+# #+# */ -/* Updated: 2023/11/07 15:43:01 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_tolower(int c) -{ - if (c >= 65 && c <= 90) - return (c + 32); - return (c); -} diff --git a/super_libft/srcs/ft_toupper.c b/super_libft/srcs/ft_toupper.c deleted file mode 100644 index 5e032cc..0000000 --- a/super_libft/srcs/ft_toupper.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_toupper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/07 13:56:50 by beroy #+# #+# */ -/* Updated: 2023/11/07 13:56:50 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -int ft_toupper(int c) -{ - if (c >= 97 && c <= 122) - return (c - 32); - return (c); -} diff --git a/super_libft/srcs/get_next_line.c b/super_libft/srcs/get_next_line.c deleted file mode 100644 index e4e8a95..0000000 --- a/super_libft/srcs/get_next_line.c +++ /dev/null @@ -1,63 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/20 11:34:48 by beroy #+# #+# */ -/* Updated: 2024/01/17 16:18:34 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -void ft_bzero_nl(char *str) -{ - size_t i; - - i = 0; - while (str[i] != 0) - { - str[i] = 0; - i++; - } -} - -char *ft_line(int fd, int rd, char *line, char bf[OPEN_MAX][BUFFER_SIZE + 1]) -{ - while (rd && ft_linecheck(line) == 0) - { - rd = read(fd, bf[fd], BUFFER_SIZE); - if (rd < 0) - return (ft_bzero_nl(bf[fd]), free(line), NULL); - bf[fd][rd] = 0; - line = ft_strjoin_nl(line, bf[fd]); - if (line == NULL) - return (NULL); - } - return (line); -} - -char *get_next_line(int fd) -{ - static char buff[OPEN_MAX][BUFFER_SIZE + 1]; - char *line; - int readed; - - readed = 1; - if (fd < 0) - return (NULL); - if (read(fd, NULL, 0) < 0) - return (ft_bzero_nl(buff[fd]), NULL); - line = ft_strdup_nl(buff[fd]); - if (line == NULL) - return (NULL); - line = ft_line(fd, readed, line, buff); - if (line == NULL) - return (NULL); - ft_buffclean(buff[fd]); - if (line[0] == 0) - return (free(line), NULL); - return (line); -} diff --git a/super_libft/srcs/get_next_line_utils.c b/super_libft/srcs/get_next_line_utils.c deleted file mode 100644 index 415fc43..0000000 --- a/super_libft/srcs/get_next_line_utils.c +++ /dev/null @@ -1,101 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/20 12:01:53 by beroy #+# #+# */ -/* Updated: 2024/01/17 16:17:27 by beroy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../libft.h" - -char *ft_strdup_nl(char *str) -{ - size_t i; - size_t len; - char *dup; - - len = 0; - while (str[len] != '\n' && str[len] != 0) - len++; - if (str[len] == '\n') - len++; - dup = malloc(sizeof(char) * (len + 1)); - if (dup == NULL) - return (dup); - i = 0; - while (i < len) - { - dup[i] = str[i]; - i++; - } - dup[i] = 0; - return (dup); -} - -int ft_linecheck(char *str) -{ - size_t i; - - i = 0; - if (ft_strlen(str + i) == 0) - return (0); - while (str[i]) - { - if (str[i] == '\n') - return (1); - i++; - } - return (0); -} - -char *ft_strjoin_nl(char *s1, char *s2) -{ - size_t i; - size_t j; - size_t len; - char *join; - - len = 0; - while (s2[len] != '\n' && s2[len] != 0) - len++; - if (s2[len] == '\n') - len++; - join = malloc(sizeof(char) * (ft_strlen(s1) + len + 1)); - if (join == NULL) - return (free(s1), NULL); - i = 0; - j = 0; - while (s1 && s1[i]) - { - join[i] = s1[i]; - i++; - } - while (s2 && j++ < len) - join[i + j - 1] = s2[j - 1]; - join[i + j - 1] = 0; - free (s1); - return (join); -} - -void ft_buffclean(char *str) -{ - size_t i; - size_t j; - - i = 0; - while (str[i] != '\n' && str[i] != 0) - i++; - if (str[i] == '\n') - i++; - j = 0; - while (str[i + j]) - { - str[j] = str[i + j]; - j++; - } - str[j] = 0; -} From 9d59573b0a763e6e9945218bb95230732549382b Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 1 Jul 2024 01:27:11 +0200 Subject: [PATCH 14/86] last version --- Makefile | 150 +++++++++++++++++++++++++ data/header.txt | 6 + data/header2.txt | 6 + data/header3.txt | 6 + data/tmp | 0 exec/built_cd.c | 129 +++++++++++++++++++++ exec/built_cd_back.c | 117 +++++++++++++++++++ exec/built_echo.c | 89 +++++++++++++++ exec/built_env.c | 55 +++++++++ exec/built_exit.c | 73 ++++++++++++ exec/built_exit_free.c | 56 +++++++++ exec/built_export.c | 118 +++++++++++++++++++ exec/built_export_add.c | 93 +++++++++++++++ exec/built_export_checker.c | 67 +++++++++++ exec/built_export_no_arg.c | 108 ++++++++++++++++++ exec/built_export_utils.c | 81 +++++++++++++ exec/built_pwd.c | 65 +++++++++++ exec/built_unset.c | 55 +++++++++ exec/exec_main.c | 127 +++++++++++++++++++++ exec/exec_redir.c | 75 +++++++++++++ exec/exec_redir_utils.c | 96 ++++++++++++++++ exec/exec_utils.c | 97 ++++++++++++++++ exec/exec_wait.c | 90 +++++++++++++++ exec/heredoc.c | 112 ++++++++++++++++++ exec/signals_main.c | 81 +++++++++++++ includes/builtins.h | 123 ++++++++++++++++++++ includes/minishell.h | 81 +++++++++++++ includes/parser.h | 146 ++++++++++++++++++++++++ srcs/backslash.c | 54 +++++++++ srcs/expand_var.c | 90 +++++++++++++++ srcs/expand_var_utils.c | 109 ++++++++++++++++++ srcs/expand_var_utils2.c | 26 +++++ srcs/format_redir.c | 98 ++++++++++++++++ srcs/free_func.c | 56 +++++++++ srcs/header.c | 52 +++++++++ srcs/init.c | 94 ++++++++++++++++ srcs/init_utils.c | 85 ++++++++++++++ srcs/init_utils2.c | 33 ++++++ srcs/main.c | 72 ++++++++++++ srcs/parser.c | 128 +++++++++++++++++++++ srcs/parser_utils.c | 84 ++++++++++++++ srcs/pre_check.c | 77 +++++++++++++ srcs/redir.c | 31 +++++ srcs/redir_utils.c | 111 ++++++++++++++++++ srcs/split.c | 108 ++++++++++++++++++ srcs/trim.c | 114 +++++++++++++++++++ srcs/trim_utils.c | 31 +++++ srcs/utils.c | 57 ++++++++++ super_libft/Makefile | 84 ++++++++++++++ super_libft/libft.h | 108 ++++++++++++++++++ super_libft/srcs/ft_atoi.c | 39 +++++++ super_libft/srcs/ft_bzero.c | 27 +++++ super_libft/srcs/ft_calloc.c | 37 ++++++ super_libft/srcs/ft_free.c | 21 ++++ super_libft/srcs/ft_isalnum.c | 21 ++++ super_libft/srcs/ft_isalpha.c | 19 ++++ super_libft/srcs/ft_isascii.c | 18 +++ super_libft/srcs/ft_isdigit.c | 18 +++ super_libft/srcs/ft_isprint.c | 18 +++ super_libft/srcs/ft_itoa.c | 66 +++++++++++ super_libft/srcs/ft_lstadd_back.c | 29 +++++ super_libft/srcs/ft_lstadd_front.c | 22 ++++ super_libft/srcs/ft_lstclear.c | 28 +++++ super_libft/srcs/ft_lstdelone.c | 21 ++++ super_libft/srcs/ft_lstiter.c | 27 +++++ super_libft/srcs/ft_lstlast.c | 22 ++++ super_libft/srcs/ft_lstmap.c | 38 +++++++ super_libft/srcs/ft_lstnew.c | 25 +++++ super_libft/srcs/ft_lstsize.c | 28 +++++ super_libft/srcs/ft_memchr.c | 27 +++++ super_libft/srcs/ft_memcmp.c | 27 +++++ super_libft/srcs/ft_memcpy.c | 28 +++++ super_libft/srcs/ft_memmove.c | 25 +++++ super_libft/srcs/ft_memset.c | 28 +++++ super_libft/srcs/ft_printf.c | 83 ++++++++++++++ super_libft/srcs/ft_printf_utils.c | 81 +++++++++++++ super_libft/srcs/ft_putchar_fd.c | 18 +++ super_libft/srcs/ft_putendl_fd.c | 19 ++++ super_libft/srcs/ft_putnbr_fd.c | 34 ++++++ super_libft/srcs/ft_putstr_fd.c | 25 +++++ super_libft/srcs/ft_split.c | 105 +++++++++++++++++ super_libft/srcs/ft_strchr.c | 25 +++++ super_libft/srcs/ft_strcmp.c | 27 +++++ super_libft/srcs/ft_strdup.c | 33 ++++++ super_libft/srcs/ft_striteri.c | 25 +++++ super_libft/srcs/ft_strjoin.c | 40 +++++++ super_libft/srcs/ft_strlcat.c | 38 +++++++ super_libft/srcs/ft_strlcpy.c | 33 ++++++ super_libft/srcs/ft_strlen.c | 23 ++++ super_libft/srcs/ft_strmapi.c | 33 ++++++ super_libft/srcs/ft_strncmp.c | 27 +++++ super_libft/srcs/ft_strnstr.c | 35 ++++++ super_libft/srcs/ft_strrchr.c | 30 +++++ super_libft/srcs/ft_strtrim.c | 73 ++++++++++++ super_libft/srcs/ft_substr.c | 38 +++++++ super_libft/srcs/ft_tolower.c | 18 +++ super_libft/srcs/ft_toupper.c | 18 +++ super_libft/srcs/get_next_line.c | 63 +++++++++++ super_libft/srcs/get_next_line_utils.c | 101 +++++++++++++++++ 99 files changed, 5708 insertions(+) create mode 100644 Makefile create mode 100644 data/header.txt create mode 100644 data/header2.txt create mode 100644 data/header3.txt create mode 100644 data/tmp create mode 100644 exec/built_cd.c create mode 100644 exec/built_cd_back.c create mode 100644 exec/built_echo.c create mode 100644 exec/built_env.c create mode 100644 exec/built_exit.c create mode 100644 exec/built_exit_free.c create mode 100644 exec/built_export.c create mode 100644 exec/built_export_add.c create mode 100644 exec/built_export_checker.c create mode 100644 exec/built_export_no_arg.c create mode 100644 exec/built_export_utils.c create mode 100644 exec/built_pwd.c create mode 100644 exec/built_unset.c create mode 100644 exec/exec_main.c create mode 100644 exec/exec_redir.c create mode 100644 exec/exec_redir_utils.c create mode 100644 exec/exec_utils.c create mode 100644 exec/exec_wait.c create mode 100644 exec/heredoc.c create mode 100644 exec/signals_main.c create mode 100644 includes/builtins.h create mode 100644 includes/minishell.h create mode 100644 includes/parser.h create mode 100644 srcs/backslash.c create mode 100644 srcs/expand_var.c create mode 100644 srcs/expand_var_utils.c create mode 100644 srcs/expand_var_utils2.c create mode 100644 srcs/format_redir.c create mode 100644 srcs/free_func.c create mode 100644 srcs/header.c create mode 100644 srcs/init.c create mode 100644 srcs/init_utils.c create mode 100644 srcs/init_utils2.c create mode 100644 srcs/main.c create mode 100644 srcs/parser.c create mode 100644 srcs/parser_utils.c create mode 100644 srcs/pre_check.c create mode 100644 srcs/redir.c create mode 100644 srcs/redir_utils.c create mode 100644 srcs/split.c create mode 100644 srcs/trim.c create mode 100644 srcs/trim_utils.c create mode 100644 srcs/utils.c create mode 100644 super_libft/Makefile create mode 100644 super_libft/libft.h create mode 100644 super_libft/srcs/ft_atoi.c create mode 100644 super_libft/srcs/ft_bzero.c create mode 100644 super_libft/srcs/ft_calloc.c create mode 100644 super_libft/srcs/ft_free.c create mode 100644 super_libft/srcs/ft_isalnum.c create mode 100644 super_libft/srcs/ft_isalpha.c create mode 100644 super_libft/srcs/ft_isascii.c create mode 100644 super_libft/srcs/ft_isdigit.c create mode 100644 super_libft/srcs/ft_isprint.c create mode 100644 super_libft/srcs/ft_itoa.c create mode 100644 super_libft/srcs/ft_lstadd_back.c create mode 100644 super_libft/srcs/ft_lstadd_front.c create mode 100644 super_libft/srcs/ft_lstclear.c create mode 100644 super_libft/srcs/ft_lstdelone.c create mode 100644 super_libft/srcs/ft_lstiter.c create mode 100644 super_libft/srcs/ft_lstlast.c create mode 100644 super_libft/srcs/ft_lstmap.c create mode 100644 super_libft/srcs/ft_lstnew.c create mode 100644 super_libft/srcs/ft_lstsize.c create mode 100644 super_libft/srcs/ft_memchr.c create mode 100644 super_libft/srcs/ft_memcmp.c create mode 100644 super_libft/srcs/ft_memcpy.c create mode 100644 super_libft/srcs/ft_memmove.c create mode 100644 super_libft/srcs/ft_memset.c create mode 100644 super_libft/srcs/ft_printf.c create mode 100644 super_libft/srcs/ft_printf_utils.c create mode 100644 super_libft/srcs/ft_putchar_fd.c create mode 100644 super_libft/srcs/ft_putendl_fd.c create mode 100644 super_libft/srcs/ft_putnbr_fd.c create mode 100644 super_libft/srcs/ft_putstr_fd.c create mode 100644 super_libft/srcs/ft_split.c create mode 100644 super_libft/srcs/ft_strchr.c create mode 100644 super_libft/srcs/ft_strcmp.c create mode 100644 super_libft/srcs/ft_strdup.c create mode 100644 super_libft/srcs/ft_striteri.c create mode 100644 super_libft/srcs/ft_strjoin.c create mode 100644 super_libft/srcs/ft_strlcat.c create mode 100644 super_libft/srcs/ft_strlcpy.c create mode 100644 super_libft/srcs/ft_strlen.c create mode 100644 super_libft/srcs/ft_strmapi.c create mode 100644 super_libft/srcs/ft_strncmp.c create mode 100644 super_libft/srcs/ft_strnstr.c create mode 100644 super_libft/srcs/ft_strrchr.c create mode 100644 super_libft/srcs/ft_strtrim.c create mode 100644 super_libft/srcs/ft_substr.c create mode 100644 super_libft/srcs/ft_tolower.c create mode 100644 super_libft/srcs/ft_toupper.c create mode 100644 super_libft/srcs/get_next_line.c create mode 100644 super_libft/srcs/get_next_line_utils.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9d1512c --- /dev/null +++ b/Makefile @@ -0,0 +1,150 @@ +######################################################################################################################## +# VARIABLE # +######################################################################################################################## + +SRCS := backslash.c \ + expand_var_utils.c \ + expand_var_utils2.c \ + init.c \ + main.c \ + parser_utils.c \ + redir.c \ + split.c \ + utils.c \ + expand_var.c \ + format_redir.c \ + init_utils.c \ + init_utils2.c \ + parser.c \ + pre_check.c \ + redir_utils.c \ + trim.c \ + trim_utils.c \ + free_func.c \ + header.c + +EXEC := built_cd.c \ + built_cd_back.c \ + built_exit.c \ + built_exit_free.c \ + built_export.c \ + built_export_add.c \ + built_export_checker.c \ + built_export_utils.c \ + exec_main.c \ + exec_utils.c \ + built_echo.c \ + built_pwd.c \ + exec_redir.c \ + exec_wait.c \ + built_env.c \ + built_export_no_arg.c \ + built_unset.c \ + exec_redir_utils.c \ + heredoc.c \ + signals_main.c + + +VALGRIND := valgrind --suppressions=valgrind_ignore_leaks.txt --leak-check=full --show-leak-kinds=all\ + --track-fds=yes --show-mismatched-frees=yes --read-var-info=yes -s --trace-children=yes + +IGN_LEAK := valgrind_ignore_leaks.txt + +SRCS_D := srcs/ + +OBJS_D := objs/ + +EXEC_D := exec/ + +OBJS := $(SRCS:%.c=$(OBJS_D)%.o)\ + +OBJS_E := $(EXEC:%.c=$(OBJS_D)%.o)\ + +HEAD := includes/minishell.h \ + includes/parser.h \ + includes/builtins.h + +HEAD_D := . + +ifeq ($(shell uname), Darwin) +READLINE_DIR = $(shell brew --prefix readline) +endif +READLINE_LIB = -lreadline -lhistory -L $(READLINE_DIR)/lib + +CFLAGS := -Wall -Wextra -Werror -g3 -I$(READLINE_DIR)/include + +NAME := minishell + +######################################################################################################################## +# LIB # +######################################################################################################################## + +LIB := libft.a + +LIB_D := super_libft/ + +LIB_I := $(LIB_D) + +LIB_H := $(LIB_I)libft.h + +LIB_A := $(addprefix $(LIB_D), $(LIB)) + +######################################################################################################################## +# RULES # +######################################################################################################################## +all : lib + $(MAKE) $(NAME) + +lib : + $(MAKE) -C $(LIB_D) + +leaks : all $(IGN_LEAK) + $(VALGRIND) ./$(NAME) + +$(NAME) : $(OBJS_D) $(OBJS) $(OBJS_E) $(LIB_A) $(HEAD) + $(CC) $(CFLAGS) -o $(NAME) $(OBJS) $(OBJS_E) $(LIB_A) -lm ${READLINE_LIB} + +$(OBJS) : $(OBJS_D)%.o: $(SRCS_D)%.c $(HEAD) $(LIB_H) + $(CC) $(CFLAGS) -Isuper_libft -c $< -o $@ + +$(OBJS_E) : $(OBJS_D)%.o: $(EXEC_D)%.c $(HEAD) $(LIB_H) + $(CC) $(CFLAGS) -Isuper_libft -c $< -o $@ + +$(OBJS_D) : + @mkdir -p $(OBJS_D) + +$(IGN_LEAK) : + @echo "{" > $(IGN_LEAK) + @echo " leak readline" >> $(IGN_LEAK) + @echo " Memcheck:Leak" >> $(IGN_LEAK) + @echo " ..." >> $(IGN_LEAK) + @echo " fun:readline" >> $(IGN_LEAK) + @echo "}" >> $(IGN_LEAK) + @echo "{" >> $(IGN_LEAK) + @echo " leak add_history" >> $(IGN_LEAK) + @echo " Memcheck:Leak" >> $(IGN_LEAK) + @echo " ..." >> $(IGN_LEAK) + @echo " fun:add_history" >> $(IGN_LEAK) + @echo "}" >> $(IGN_LEAK) + @echo "{" >> $(IGN_LEAK) + @echo " leak readline_internal_char" >> $(IGN_LEAK) + @echo " Memcheck:Leak" >> $(IGN_LEAK) + @echo " ..." >> $(IGN_LEAK) + @echo " fun:readline_internal_char" >> $(IGN_LEAK) + @echo "}" >> $(IGN_LEAK) +######################################################################################################################## +# COMMANDS # +######################################################################################################################## + +clean : + $(RM) -r $(OBJS) $(OBJS_D) + $(MAKE) clean -C super_libft + +fclean : clean + $(RM) $(NAME) $(NAME_B) + $(MAKE) fclean -C super_libft + $(RM) $(IGN_LEAK) + +re : fclean all + +.PHONY: all clean fclean re lib diff --git a/data/header.txt b/data/header.txt new file mode 100644 index 0000000..36ef5c4 --- /dev/null +++ b/data/header.txt @@ -0,0 +1,6 @@ + _ __ __ __ _ _ __ ____ +| | / /__ / /________ ____ ___ ___ / /_____ ____ ___ (_)___ (_)____/ /_ ___ / / / +| | /| / / _ \/ / ___/ __ \/ __ `__ \/ _ \ / __/ __ \ / __ `__ \/ / __ \/ / ___/ __ \/ _ \/ / / +| |/ |/ / __/ / /__/ /_/ / / / / / / __/ / /_/ /_/ / / / / / / / / / / / (__ ) / / / __/ / / +|__/|__/\___/_/\___/\____/_/ /_/ /_/\___/ \__/\____/ /_/ /_/ /_/_/_/ /_/_/____/_/ /_/\___/_/_/ + by grebrune and beroy \ No newline at end of file diff --git a/data/header2.txt b/data/header2.txt new file mode 100644 index 0000000..e9350a6 --- /dev/null +++ b/data/header2.txt @@ -0,0 +1,6 @@ + __ ____ _ _________ + / |/ (_)___ (_)___ ___ ____ ____ / __/ __( ) + / /|_/ / / __ \/ / __ `__ \/ __ \/ __ \/ /_/ /_ |/ + / / / / / / / / / / / / / / /_/ / / / / __/ __/ +/_/ /_/_/_/ /_/_/_/ /_/ /_/\____/_/ /_/_/ /_/ +by Grebrune & Beroy \ No newline at end of file diff --git a/data/header3.txt b/data/header3.txt new file mode 100644 index 0000000..bb617da --- /dev/null +++ b/data/header3.txt @@ -0,0 +1,6 @@ +.__ ___. .__ __ .___ __ +| | _____ ___________ ____ ______ ______ ____ \_ |__ |__|/ |_ ____ __| _/____ | | _______ _____ ____ +| | \__ \ / ___\_ __ \/ _ \/ ___// ___// __ \ | __ \| \ __\/ __ \ / __ |/ __ \ | |/ /\__ \ \__ \ / \ +| |__/ __ \_ / /_/ > | \( <_> )___ \ \___ \\ ___/ | \_\ \ || | \ ___/ / /_/ \ ___/ | < / __ \_/ __ \| | \ +|____(____ / \___ /|__| \____/____ >____ >\___ > |___ /__||__| \___ > \____ |\___ > |__|_ \(____ (____ /___| / + \/ /_____/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \ No newline at end of file diff --git a/data/tmp b/data/tmp new file mode 100644 index 0000000..e69de29 diff --git a/exec/built_cd.c b/exec/built_cd.c new file mode 100644 index 0000000..17ad7e3 --- /dev/null +++ b/exec/built_cd.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 15:49:11 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:46:19 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int ft_cd(t_head *head) +{ + int err; + char *str; + char *old_pwd; + DIR *ptr_dir; + + old_pwd = NULL; + str = NULL; + if (head->cmd->next || !head->cmd->arg[1]) + return (1); + if (ft_cd_bis(head, &str) == 2) + return (2); + get_path(&old_pwd); + err = chdir(str); + if (err != 0) + { + g_error = 1; + ptr_dir = opendir(head->cmd->arg[1]); + return (free(old_pwd), free(str), \ + write(2, "bash: cd: ", 10), perror(head->cmd->arg[1]), 1); + closedir(ptr_dir); + } + cd_rep_or_new(head, str, old_pwd); + return (0); +} + +int ft_cd_bis(t_head *head, char **str) +{ + if (head->cmd->arg[1] && head->cmd->arg[2]) + return (write(2, "bash: cd: too many arguments\n", 29), 2); + if (head->cmd->arg[1]) + { + if (0 == ft_strcmp(head->cmd->arg[1], "../") + || 0 == ft_strcmp(head->cmd->arg[1], "..")) + return (cd_back(head), 2); + if (0 == ft_strcmp(head->cmd->arg[1], "~/")) + return (cd_tild(head), 2); + get_path(str); + *str = ft_strcat(*str, head->cmd->arg[1]); + if (!*str) + return (ft_free_all(head), exit(1), 1); + } + else + { + cd_no_arg(head, str); + if (!*str) + return (write(2, "bash: cd: HOME not set\n", 23), 2); + } + if (*str == NULL) + return (write(2, "Crash of Malloc\n", 16), 2); + return (0); +} + +int replace_value(t_head *head, char *value, char *replace) +{ + t_env *copy; + + copy = head->env; + while (copy->next != NULL) + { + if (ft_strcmp(copy->name, replace) == 0) + { + free(copy->value); + copy->value = value; + return (0); + } + copy = copy->next; + } + return (1); +} + +void cd_no_arg(t_head *head, char **str) +{ + t_env *env; + + env = head->env; + while (env) + { + if (ft_strcmp(env->name, "HOME") == 0) + { + *str = env->value; + break ; + } + env = env->next; + } +} + +char *ft_strcat(char *path, char *dir) +{ + char *dest; + size_t i; + size_t x; + + dest = malloc(sizeof (char) * (ft_strlen(dir) + ft_strlen(path) + 2)); + if (dest == NULL) + return (dest); + i = 0; + while (path && path[i]) + { + dest[i] = path[i]; + i++; + } + x = 0; + dest[i++] = '/'; + while (dir && dir[x]) + { + dest[i] = dir[x]; + i++; + x++; + } + dest[i] = '\0'; + free(path); + return (dest); +} diff --git a/exec/built_cd_back.c b/exec/built_cd_back.c new file mode 100644 index 0000000..f12c03b --- /dev/null +++ b/exec/built_cd_back.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_cd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 14:35:38 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:38:48 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +char *cd_back_trim(char *str) +{ + char *ret; + int i; + int x; + + i = 0; + while (str && str[i]) + i++; + while (str && str[i] != '/') + i--; + ret = malloc(sizeof(char) * (i + 1)); + x = -1; + while (++x < i) + ret[x] = str[x]; + ret[x] = 0; + return (ret); +} + +void cd_back_error(t_head *head, char *old_pwd, char *new_pwd) +{ + DIR *ptr_dir; + + g_error = 1; + ptr_dir = opendir(head->cmd->arg[1]); + perror("bash: cd"); + closedir(ptr_dir); + free(old_pwd); + free(new_pwd); +} + +void cd_back(t_head *head) +{ + int err; + char *old_pwd; + char *pwd; + char *n_pwd; + + pwd = value_of_name(head->env, "PWD"); + old_pwd = ft_strdup(pwd); + n_pwd = cd_back_trim(pwd); + err = chdir(n_pwd); + if (err != 0) + { + cd_back_error(head, old_pwd, n_pwd); + return ; + } + cd_rep_or_new(head, n_pwd, old_pwd); +} + +char *cd_tild_trim(t_head *head) +{ + char *path; + char *pwd; + int i; + int x; + + pwd = value_of_name(head->env, "PWD"); + if (!pwd) + return (NULL); + i = 0; + if (ft_strncmp("/home/", pwd, 5) == 0) + i = 6; + while (pwd[i] && pwd[i] != '/') + i++; + if (i < 6 || pwd[i] != '/') + return (NULL); + i++; + path = malloc(sizeof(char) * i + 1); + x = -1; + while (i > ++x) + path[x] = pwd[x]; + return (path[x] = '\0', path); +} + +int cd_tild(t_head *head) +{ + int err; + char *old_pwd; + char *pwd; + char *n_pwd; + + pwd = value_of_name(head->env, "PWD"); + if (!pwd) + return (write(2, "bash: cd: ~/: No such file or directory\n", 40), 2); + old_pwd = ft_strdup(pwd); + if (!old_pwd) + return (2); + n_pwd = cd_tild_trim(head); + if (!n_pwd) + return (free(old_pwd), 2); + err = chdir(n_pwd); + if (err != 0) + { + cd_back_error(head, old_pwd, n_pwd); + return (2); + } + if (replace_value(head, n_pwd, "PWD")) + new_pwd(head, n_pwd, "PWD"); + if (replace_value(head, old_pwd, "OLDPWD")) + new_pwd(head, old_pwd, "OLDPWD"); + return (0); +} diff --git a/exec/built_echo.c b/exec/built_echo.c new file mode 100644 index 0000000..d9f8417 --- /dev/null +++ b/exec/built_echo.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_echo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 15:47:13 by grebrune #+# #+# */ +/* Updated: 2024/06/28 11:21:13 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int echo_newline(char *str) +{ + size_t i; + + i = 0; + while (str && str[i]) + { + if (i == 0 && str[i] == '-') + i++; + else if (i != 0 && str[i] == 'n') + i++; + else + return (0); + } + if (i != 0) + return (1); + return (0); +} + +void echo_pipe(t_head *head, t_cmd *copy) +{ + char *str; + + if (copy->prev) + { + str = get_next_line(0); + while (str) + { + free(str); + str = get_next_line(0); + } + } + write(1, "\n", 1); + free_fnp(head, head->fnp); + ft_free_all(head); + exit (0); +} + +void ft_echo(t_head *head, t_cmd *copy) +{ + int i; + int n; + + i = 1; + n = 0; + if (!copy->arg[1]) + echo_pipe(head, copy); + while (echo_newline(copy->arg[i])) + { + i++; + n++; + } + if (ft_strcmp("$?", copy->arg[i]) == 0) + { + printf("%d", g_error); + if (n == 0) + printf("\n"); + exit(0); + } + print_tab(head, copy->arg, i, n); +} + +void print_tab(t_head *head, char **arg, int i, int n) +{ + while (arg[i]) + { + ft_putstr(arg[i]); + i++; + if (arg[i]) + write(1, " ", 1); + } + if (n == 0) + printf("\n"); + exit_free(head, 0); +} diff --git a/exec/built_env.c b/exec/built_env.c new file mode 100644 index 0000000..1f4e42e --- /dev/null +++ b/exec/built_env.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 16:06:10 by grebrune #+# #+# */ +/* Updated: 2024/06/24 16:09:22 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void ft_env(t_head *head) +{ + t_env *copy; + int i; + + if (!head->env) + return ; + copy = head->env; + i = checker_env(head->cmd->arg); + if (i != 0) + { + write(2, "env: '", 6); + write(2, head->cmd->arg[i], ft_strlen(head->cmd->arg[i])); + write(2, "’: No such file or directory\n", 31); + free_fnp(head, head->fnp); + ft_free_all(head); + exit (0); + } + while (copy->next) + { + printf("%s=%s\n", copy->name, copy->value); + copy = copy->next; + } + if (copy) + printf("%s=%s\n", copy->name, copy->value); + exit_free(head, 0); +} + +int checker_env(char **arg) +{ + int i; + + i = 0; + while (arg && arg[i]) + { + if (ft_strcmp(arg[i], "env")) + return (i); + i++; + } + return (0); +} diff --git a/exec/built_exit.c b/exec/built_exit.c new file mode 100644 index 0000000..4d6e0d2 --- /dev/null +++ b/exec/built_exit.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/13 17:28:49 by grebrune #+# #+# */ +/* Updated: 2024/06/21 18:55:20 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void ft_exit(t_head *head) +{ + t_cmd *copy; + + if (!head->cmd->next) + write(2, "exit\n", 5); + if (!head->cmd->arg[1]) + { + exit_free(head, g_error); + return ; + } + copy = head->cmd; + if (ft_strnum(copy->arg[1])) + { + if (is_num(head)) + return ; + exit_free(head, 1); + return ; + } + exit_write(copy->arg[1]); + exit_free(head, 2); +} + +void exit_write(char *arg) +{ + write(2, "bash: exit: ", 12); + write(2, arg, ft_strlen(arg)); + write(2, ": numeric argument required\n", 28); +} + +int ft_strnum(char *str) +{ + int i; + + i = 0; + while (str && str[i]) + { + if (!(str[i] >= 48 && str[i] <= 57)) + return (0); + i++; + } + return (1); +} + +int is_num(t_head *head) +{ + if (head->cmd->arg[2]) + { + write(2, "bash: exit: too many arguments\n", 31); + g_error = 1; + return (1); + } + else + { + g_error = ft_atoi(head->cmd->arg[1]) % 256; + exit_free(head, g_error); + return (0); + } +} diff --git a/exec/built_exit_free.c b/exec/built_exit_free.c new file mode 100644 index 0000000..c6952a6 --- /dev/null +++ b/exec/built_exit_free.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_exit_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 17:13:12 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:21:12 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void exit_free(t_head*head, int status) +{ + free_fnp(head, head->fnp); + ft_free_all(head); + exit (status); +} + +void new_pwd(t_head *head, char *value, char *name) +{ + t_env *new; + t_env *copy; + + copy = head->env; + new = ft_calloc(1, sizeof(t_env)); + if (!new) + return ; + while (copy && copy->next) + copy = copy->next; + new_var_bis(copy, head, new); + new->next = NULL; + new->name = ft_strdup(name); + if (!new->name) + return ; + new->value = ft_strdup(value); + if (!new->value) + return ; + return ; +} + +void cd_rep_or_new(t_head *head, char *n_pwd, char *old_pwd) +{ + if (replace_value(head, n_pwd, "PWD")) + { + new_pwd(head, n_pwd, "PWD"); + free(n_pwd); + } + if (replace_value(head, old_pwd, "OLDPWD")) + { + new_pwd(head, old_pwd, "OLDPWD"); + free(old_pwd); + } +} diff --git a/exec/built_export.c b/exec/built_export.c new file mode 100644 index 0000000..71f1863 --- /dev/null +++ b/exec/built_export.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_export.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 15:51:12 by grebrune #+# #+# */ +/* Updated: 2024/06/28 15:48:54 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int ft_export(t_head *head) +{ + t_env *c_env; + size_t i; + + c_env = head->env; + if (head->cmd->arg[1] == NULL) + return (ex_no_args(head)); + if (head->cmd->next != NULL) + return (3); + i = 1; + while (head->cmd->arg[i]) + { + if (export_bis(head, c_env, i)) + return (2); + i++; + } + return (0); +} + +int export_bis(t_head *head, t_env *c_env, size_t i) +{ + if (check_name(head, head->cmd->arg[i], c_env)) + return (0); + if (no_plus(head->cmd->arg[i])) + { + if (export_search_env(c_env, head->cmd->arg[i]) == 0) + { + if (2 == new_var(head, head->cmd->arg[i])) + return (write(2, "Crash of Malloc\n", 16), 2); + } + else + return (1); + } + return (0); +} + +int ft_strcmp_until(char *s1, const char *s2) +{ + size_t i; + + i = 0; + while (s1[i] != 0 || s2[i] != 0) + { + if (s1[i] == 0 && s2[i] == '+') + return (0); + if (s2[i] == '=' || s1[i] == '=') + return (0); + if (s1[i] != s2[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} + +char *replace_var_until(char *arg, char *result) +{ + size_t z; + size_t i; + char *ret; + + free(arg); + z = ft_strlen_until(result); + i = 0; + if (z == ft_strlen(result) && ft_strlen(arg) == 0) + return (ft_strdup("\0")); + while (result && result[z + 1 + i]) + i++; + ret = malloc(sizeof(char) * (i + 1)); + if (!ret) + return (NULL); + i = 0; + while (result && result[z + 1 + i]) + { + ret[i] = result[z + 1 + i]; + i++; + } + ret[i] = '\0'; + return (ret); +} + +int new_var(t_head *head, char *name) +{ + t_env *new; + t_env *copy; + + copy = head->env; + if (check_equal(name)) + return (g_error = 0, 0); + new = ft_calloc(1, sizeof(t_env)); + if (!new) + return (2); + while (copy && copy->next) + copy = copy->next; + new_var_bis(copy, head, new); + new->next = NULL; + new->name = dup_until(name); + if (!new->name) + return (2); + new->value = dup_if(name); + if (!new->value) + return (2); + return (0); +} diff --git a/exec/built_export_add.c b/exec/built_export_add.c new file mode 100644 index 0000000..45be1ac --- /dev/null +++ b/exec/built_export_add.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_export_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 20:11:01 by grebrune #+# #+# */ +/* Updated: 2024/06/28 15:47:07 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int export_search_env(t_env *c_env, char *name) +{ + while (c_env) + { + if (0 == ft_strcmp_until(c_env->name, name)) + { + c_env->value = replace_var_until(c_env->value, name); + if (!c_env->value) + return (write(2, "Crash of Malloc\n", 16), 2); + return (1); + } + c_env = c_env->next; + } + return (0); +} + +void error_handle(char *str) +{ + write(2, "bash: export: `", 15); + write(2, str, ft_strlen(str)); + write(2, "': not a valid identifier\n", 26); +} + +char *strcat_until(char *val, char *name) +{ + int i; + int x; + char *new_val; + + i = 0; + while (name && name[i]) + i++; + i += ft_strlen(val); + i -= ft_strlen_until(name); + new_val = malloc(sizeof(char) * (i + 1)); + if (!new_val) + return (NULL); + i = -1; + while (val && val[++i]) + new_val[i] = val[i]; + x = ft_strlen_until(name); + x++; + while (name && name[x]) + new_val[i++] = name[x++]; + new_val[i] = 0; + return (free(val), new_val); +} + +int add_var(t_head *head, char *name, t_env *c_env) +{ + while (c_env) + { + if (0 == ft_strcmp_until(c_env->name, name)) + { + c_env->value = strcat_until(c_env->value, name); + if (!c_env->value) + return (write(2, "Crash of Malloc\n", 16), 2); + return (1); + } + c_env = c_env->next; + } + if (new_var(head, name)) + return (write(2, "Crash of Malloc\n", 16), 2); + return (0); +} + +void new_var_bis(t_env *copy, t_head *head, t_env *new) +{ + if (copy) + { + copy->next = new; + new->prev = copy; + } + else + { + head->env = new; + new->prev = NULL; + } +} diff --git a/exec/built_export_checker.c b/exec/built_export_checker.c new file mode 100644 index 0000000..1268025 --- /dev/null +++ b/exec/built_export_checker.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_export_checker.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 16:32:41 by grebrune #+# #+# */ +/* Updated: 2024/06/24 17:17:53 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int check_name_errors(char *name) +{ + if (ft_strcmp("", name) == 0) + return (write(2, "bash: export: `': not a valid identifier\n", 41), 1); + if (ft_strcmp("=", name) == 0) + return (g_error = 1, write(2, "bash: export: " + "`=': not a valid identifier\n", 42), 1); + if (ft_strcmp("+=", name) == 0) + return (g_error = 1, write(2, "bash: export: " + "`+=': not a valid identifier\n", 43), 1); + if (ft_isdigit(name[0])) + return (error_handle(name), 1); + if (name[0] == '=') + { + write(2, "bash: export: '", 15); + write(2, name, ft_strlen(name)); + write(2, "': not a valid identifier\n", 26); + return (g_error = 1, 1); + } + return (0); +} + +int check_name(t_head *head, char *name, t_env *c_env) +{ + int i; + + i = 0; + if (check_name_errors(name)) + return (1); + while (name[i] && name[i] != '=') + { + if (name[i] == '+' && name[i + 1] == '=') + return (add_var(head, name, c_env), 0); + if (ft_isalnum(name[i]) == 0) + return (error_handle(name), 1); + i++; + } + return (0); +} + +int no_plus(char *name) +{ + size_t i; + + i = 0; + while (name && name[i] && name[i] != '=') + { + if (name[i] == '+') + return (0); + i++; + } + return (1); +} diff --git a/exec/built_export_no_arg.c b/exec/built_export_no_arg.c new file mode 100644 index 0000000..3660fd7 --- /dev/null +++ b/exec/built_export_no_arg.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_export_no_arg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/13 15:09:17 by grebrune #+# #+# */ +/* Updated: 2024/06/28 11:38:35 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/builtins.h" + +int ex_no_args(t_head *head) +{ + char **tab; + t_env *copy; + + if (!head->env) + { + free_fnp(head, head->fnp); + ft_free_all(head); + exit(0); + } + copy = head->env; + tab = env_to_tab(copy); + if (!tab) + { + write(2, "Crash of Malloc\n", 16); + ft_exit(head); + } + swap_this_tab(tab); + printf_tab(tab); + free_tab(tab); + close_pipe(head, head->fnp->pipe); + free_fnp(head, head->fnp); + ft_free_all(head); + exit(0); +} + +char **env_to_tab(t_env *copy) +{ + char **tab; + size_t x; + + tab = malloc((sizeof (char *)) * (envlen(copy) + 2)); + if (!tab) + return (NULL); + x = 0; + while (copy) + { + tab[x] = join_equal(copy->name, copy->value); + if (!tab[x]) + return (free_tab(tab), NULL); + x++; + copy = copy->next; + } + tab[x] = NULL; + return (tab); +} + +void printf_tab(char **tab) +{ + size_t x; + + x = 0; + while (tab[x] != NULL) + { + if (ft_strncmp("_=\"/home", tab[x], 8) != 0) + printf("declare -x %s\n", tab[x]); + x++; + } +} + +void swap_this_tab(char **tab) +{ + int x; + char *tmp; + + x = 0; + while (tab[x] && tab[x + 1]) + { + if (0 < ft_strcmp(tab[x], tab[x + 1])) + { + tmp = tab[x]; + tab[x] = tab[x + 1]; + tab[x + 1] = tmp; + x = 0; + } + else + x++; + } +} + +char *join_equal(char *name, char *val) +{ + char *n_w_e; + char *v_w_e; + char *join; + + n_w_e = ft_strjoin(name, "=\""); + v_w_e = ft_strjoin(val, "\""); + join = ft_strjoin(n_w_e, v_w_e); + free(n_w_e); + free(v_w_e); + return (join); +} diff --git a/exec/built_export_utils.c b/exec/built_export_utils.c new file mode 100644 index 0000000..2b38255 --- /dev/null +++ b/exec/built_export_utils.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_export_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/12 13:46:00 by grebrune #+# #+# */ +/* Updated: 2024/06/20 12:29:16 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int check_equal(char *str) +{ + int i; + + i = 0; + while (str && str[i]) + { + if (str[i] == '=') + return (0); + i++; + } + return (1); +} + +char *dup_until(char *str) +{ + size_t size; + size_t i; + char *ptr; + + size = 0; + while (str && str[size] && str[size] != '=' && str[size] != '+') + size++; + ptr = malloc((size + 1) * sizeof(char)); + if (ptr == NULL) + return (ptr); + i = 0; + while (i < size) + { + ptr[i] = str[i]; + i++; + } + ptr[i] = 0; + return (ptr); +} + +char *dup_if(char *name) +{ + size_t i; + + i = 0; + while (name && name[i] && name[i] != '=') + i++; + if (!(name[i] == '=' && name[i + 1] != '\0')) + return (ft_strdup("\0")); + return (ft_strdup(&name[i + 1])); +} + +int ft_strlen_until(char *str) +{ + int i; + + i = 0; + while (str && str[i] && str[i] != '=') + i++; + return (i); +} + +void free_tab(char **tab) +{ + int x; + + x = 0; + while (tab[x]) + ft_free(tab[x++]); + ft_free(tab); +} diff --git a/exec/built_pwd.c b/exec/built_pwd.c new file mode 100644 index 0000000..fb6a1f9 --- /dev/null +++ b/exec/built_pwd.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ +/* Updated: 2024/06/28 16:58:52 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void ft_pwd(t_head *head) +{ + int err; + char *str; + + str = NULL; + err = get_path(&str); + if (err == 2) + { + write(2, "bash: pwd: ", 11); + perror(str); + } + if (str) + printf("%s\n", str); + free(str); + exit_free(head, 0); +} + +int get_path(char **str) +{ + size_t size; + + size = 10; + while (!*(str)) + { + *str = getcwd(*(str), size); + if (!*(str)) + { + free(*(str)); + *(str) = NULL; + if (errno != ERANGE) + return (2); + } + size += 10; + } + return (0); +} + +char *value_of_name(t_env *env, char *name) +{ + t_env *copy; + + copy = env; + while (env) + { + if (0 == ft_strcmp(env->name, name)) + return (env->value); + env = env->next; + } + return (NULL); +} diff --git a/exec/built_unset.c b/exec/built_unset.c new file mode 100644 index 0000000..ec00ce1 --- /dev/null +++ b/exec/built_unset.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* built_unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/14 16:03:36 by grebrune #+# #+# */ +/* Updated: 2024/06/24 16:53:16 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void ft_unset(t_head *head) +{ + int i; + + if (head->cmd->next || !head->cmd->arg[1]) + return ; + i = 1; + while (head->cmd->arg[i]) + { + rem_env(&head->env, head->cmd->arg[i], &ft_strcmp); + i++; + } +} + +void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) +{ + t_env *remove; + t_env *current; + + current = *env; + while (current && current->next) + { + if ((*cmp)(current->next->name, ref) == 0) + { + remove = current->next; + current->next = current->next->next; + free(remove->name); + free(remove->value); + free(remove); + } + current = current->next; + } + current = *env; + if (current && (*cmp)(current->name, ref) == 0) + { + *env = current->next; + free(current->value); + free(current->name); + free(current); + } +} diff --git a/exec/exec_main.c b/exec/exec_main.c new file mode 100644 index 0000000..99da40e --- /dev/null +++ b/exec/exec_main.c @@ -0,0 +1,127 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:09:41 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/builtins.h" + +void there_cmd(char **arg, char *str, char **env) +{ + char **path; + int i; + char *cmd; + + path = ft_split(str, ':'); + if (!(access(arg[0], F_OK)) && !(access(arg[0], X_OK))) + execve(arg[0], arg, env); + i = 0; + while (path[i]) + { + cmd = join_with_char(path[i], arg[0], '/'); + if (!(access(cmd, F_OK)) && !(access(cmd, X_OK))) + execve(cmd, arg, env); + i++; + ft_free(cmd); + } + perror(arg[0]); + free_tab(env); + free_tab(path); + free_tab(arg); + ft_free(str); + exit (127); +} + +int no_fork_cmd(t_head *head, t_cmd *copy, char *str) +{ + if (!str || str[0] == 0) + return (0); + if (ft_strcmp(str, "cd") == 0) + return (ft_cd(head), 0); + if ((ft_strcmp(str, "export") == 0) && copy->arg[1] != NULL) + return (ft_export(head), 0); + if (ft_strcmp(str, "unset") == 0) + return (ft_unset(head), 0); + if (ft_strcmp(str, "exit") == 0 && (!copy->next && !copy->prev)) + return (ft_exit(head), 0); + return (3); +} + +int exec_shell(t_head *head, t_cmd *copy, t_fnp *fnp) +{ + char **env; + char **tab; + char *path; + + env = make_env(head->env); + tab = make_arg(copy); + if (!env || !tab) + { + if (!env) + return (free(tab), ft_free_all(head), 1); + else + return (free(env), ft_free_all(head), 1); + } + path = find_path(head); + if (!path) + no_path_to_hapiness(head, env, tab); + free_fnp(head, fnp); + ft_free_all(head); + there_cmd(tab, path, env); + return (0); +} + +int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x) +{ + int fd[2]; + + if (no_fork_cmd(head, copy, copy->arg[0]) == 0) + return (fnp->pid[x] = 0, 0); + fnp->pid[x] = fork(); + if (fnp->pid[x] < 0) + ft_exit(head); + if (fnp->pid[x] != 0) + return (0); + sig_main(head, 1); + if (redir_with_fd(fd, fnp->pipe, copy, x)) + return (2); + close_pipe(head, fnp->pipe); + if (ft_strcmp(copy->arg[0], "echo") == 0) + return (ft_echo(head, copy), 1); + if (ft_strcmp(copy->arg[0], "env") == 0) + return (ft_env(head), 1); + if (ft_strcmp(copy->arg[0], "pwd") == 0) + return (ft_pwd(head), 1); + if (ft_strcmp(copy->arg[0], "export") == 0) + return (ft_export(head)); + if (ft_strcmp(copy->arg[0], "exit") == 0) + return (ft_exit(head), 0); + return (exec_shell(head, copy, fnp)); +} + +int executable(t_head *head) +{ + t_cmd *copy; + int x; + + x = 0; + malloc_fnp(head); + open_the_pipe(head->fnp->pipe, head); + copy = head->cmd; + while (copy != NULL) + { + find_cmd(head, copy, head->fnp, x); + copy = copy->next; + x++; + } + close_pipe(head, head->fnp->pipe); + wait_for_all(head->fnp->pid, x); + free_fnp(head, head->fnp); + return (0); +} diff --git a/exec/exec_redir.c b/exec/exec_redir.c new file mode 100644 index 0000000..a6c0e5c --- /dev/null +++ b/exec/exec_redir.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 14:44:44 by grebrune #+# #+# */ +/* Updated: 2024/06/28 14:05:23 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/builtins.h" + +void fail_malloc(t_head *head, int **fd, size_t i) +{ + while (i-- > 0) + free(fd[i]); + free(head->fnp->pid); + free(head->fnp->pipe); + free(head->fnp); +} + +int open_the_pipe(int **pipe, t_head *head) +{ + size_t numb; + size_t i; + + numb = cmdlen(head->cmd); + if (numb == 1) + return (0); + i = 0; + while (i < numb) + { + pipe[i] = malloc(sizeof(int) * 2); + if (!pipe[i]) + return (fail_malloc(head, pipe, i), 1); + i++; + } + fill_pipe(numb, pipe, head); + return (0); +} + +int open_files(t_redir *redir) +{ + if (redir->type == 1) + redir->fd = open(redir->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (redir->type == 2) + redir->fd = open(redir->arg, O_WRONLY | O_CREAT | O_APPEND, 0644); + if ((redir->type == 4 && redir->fd == -1) || redir->type == 3) + redir->fd = open(redir->arg, O_RDONLY); + if (redir->fd == -1) + return (perror(redir->arg), 2); + return (0); +} + +int open_redir(t_cmd *copy, int fd[2]) +{ + t_redir *parser; + + fd[1] = 1; + fd[0] = 0; + parser = copy->redir; + while (parser) + { + if (open_files(parser)) + return (2); + if (parser->type == 1 || parser->type == 2) + fd[1] = parser->fd; + if (parser->type == 3 || parser->type == 4) + fd[0] = parser->fd; + parser = parser->next; + } + return (0); +} diff --git a/exec/exec_redir_utils.c b/exec/exec_redir_utils.c new file mode 100644 index 0000000..69a4860 --- /dev/null +++ b/exec/exec_redir_utils.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_redir_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/12 13:57:10 by grebrune #+# #+# */ +/* Updated: 2024/06/14 16:44:09 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int fill_pipe(size_t numb, int **fd, t_head *head) +{ + size_t i; + + i = 0; + while (i < numb - 1) + { + if (pipe(fd[i]) == -1) + { + perror("pipe"); + free_fnp(head, head->fnp); + ft_free_all(head); + exit (1); + } + i++; + } + fd[i][1] = 1; + return (0); +} + +void close_pipe(t_head *head, int **pipe) +{ + int i; + + (void)head; + (void)pipe; + i = 2; + while (++i < 1024) + close(i); +} + +int redir_with_fd(int fd[2], int **pipe, t_cmd *copy, int x) +{ + if (open_redir(copy, fd)) + return (2); + if (copy->prev) + fd[0] = pipe[x - 1][0]; + if (copy->next && !copy->redir) + fd[1] = pipe[x][1]; + if (fd[0] != 0 && (copy->prev || copy->redir)) + { + if (dup2(fd[0], STDIN_FILENO) == -1) + return (perror("dup2"), 2); + } + if (fd[1] != 1 && (copy->next || copy->redir)) + { + if (dup2(fd[1], STDOUT_FILENO) == -1) + return (perror("dup2"), 2); + } + return (0); +} + +void free_fnp(t_head *head, t_fnp *fnp) +{ + size_t nbr_pipe; + size_t i; + + i = 0; + nbr_pipe = cmdlen(head->cmd); + while (nbr_pipe != 1 && i < nbr_pipe) + { + free(fnp->pipe[i]); + i++; + } + free(head->fnp->pid); + free(head->fnp->pipe); + free(head->fnp); +} + +int malloc_fnp(t_head *head) +{ + head->fnp = malloc(sizeof(t_fnp)); + if (!head->fnp) + return (ft_free_all(head), exit(1), 1); + head->fnp->pid = malloc(sizeof(int) * cmdlen(head->cmd)); + if (!head->fnp->pid) + return (ft_free_all(head), exit(1), 1); + head->fnp->pipe = malloc(sizeof(int *) * cmdlen(head->cmd)); + if (!head->fnp->pid) + return (ft_free_all(head), exit(1), 1); + return (0); +} diff --git a/exec/exec_utils.c b/exec/exec_utils.c new file mode 100644 index 0000000..111322a --- /dev/null +++ b/exec/exec_utils.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/22 17:31:24 by grebrune #+# #+# */ +/* Updated: 2024/06/26 21:44:03 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/builtins.h" + +char *find_path(t_head *head) +{ + t_env *copy; + char *path; + + copy = head->env; + while (copy->next != NULL) + { + if (ft_strcmp("PATH", copy->name) == 0) + { + path = strdup(copy->value); + return (path); + } + copy = copy->next; + } + return (NULL); +} + +char *join_with_char(char const *s1, char const *s2, char c) +{ + size_t len; + size_t i; + size_t j; + char *str; + + len = strlen((char *)s1) + strlen((char *)s2); + i = 0; + str = malloc (sizeof(char) * (len + 2)); + if (str == NULL) + return (str); + while (s1[i]) + { + str[i] = s1[i]; + i++; + } + str[i++] = c; + j = 0; + while (s2[j]) + { + str[i + j] = s2[j]; + j++; + } + str [i + j] = 0; + return (str); +} + +char **make_env(t_env *env) +{ + t_env *copy; + char **tab; + size_t size; + size_t i; + + size = envlen(env); + tab = malloc(sizeof (char *) * (size + 1)); + if (tab == NULL) + return (NULL); + copy = env; + i = 0; + while (i < size) + { + tab[i] = join_with_char(copy->name, copy->value, '='); + if (tab[i] == NULL) + return (NULL); + i++; + copy = copy->next; + } + tab[i] = NULL; + return (tab); +} + +void no_path_to_hapiness(t_head *head, char **env, char **tab) +{ + if (!(access(head->cmd->arg[0], F_OK)) + && !(access(head->cmd->arg[0], X_OK))) + execve(head->cmd->arg[0], head->cmd->arg, env); + perror(head->cmd->arg[0]); + free_tab(env); + free_tab(tab); + free_fnp(head, head->fnp); + ft_free_all(head); + exit (127); +} diff --git a/exec/exec_wait.c b/exec/exec_wait.c new file mode 100644 index 0000000..634f5e9 --- /dev/null +++ b/exec/exec_wait.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_wait.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ +/* Updated: 2024/06/28 13:31:36 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/builtins.h" + +void wait_for_all(pid_t *pid, int x) +{ + int i; + int wstatus[2]; + int last; + + wstatus[1] = 0; + wstatus[0] = 0; + i = 0; + while (i < x) + { + last = waitpid(0, &wstatus[0], 0); + if (last == pid[x - 1]) + wstatus[1] = wstatus[0]; + i++; + } + if (WIFEXITED(wstatus[1]) == 0) + g_error = 130; + else + g_error = WEXITSTATUS(wstatus[1]); +} + +size_t envlen(t_env *base) +{ + size_t size; + t_env *copy; + + copy = base; + size = 0; + if (copy != NULL) + size++; + while (copy->next != NULL) + { + size++; + copy = copy->next; + } + return (size); +} + +size_t cmdlen(t_cmd *base) +{ + size_t size; + t_cmd *copy; + + copy = base; + size = 0; + if (copy != NULL) + size++; + while (copy->next != NULL) + { + size++; + copy = copy->next; + } + return (size); +} + +char **make_arg(t_cmd *cmd) +{ + char **arg; + int x; + + x = 0; + while (cmd->arg && cmd->arg[x]) + x++; + arg = malloc(sizeof (char *) * (x + 1)); + if (arg == NULL) + return (NULL); + x = 0; + while (cmd->arg && cmd->arg[x]) + { + arg[x] = ft_strdup(cmd->arg[x]); + x++; + } + arg[x] = NULL; + return (arg); +} diff --git a/exec/heredoc.c b/exec/heredoc.c new file mode 100644 index 0000000..a45e022 --- /dev/null +++ b/exec/heredoc.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ +/* Updated: 2024/06/28 16:38:01 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +char *replace_var_heredoc(char *str, t_head *head) +{ + int start; + int end; + char *value; + char *dup; + + start = find_var(str); + end = find_end_var(str, start); + value = var_value(str, start + 1, head->env); + if (value == NULL) + return (NULL); + dup = str_dup_var(str, start, end, value); + if (dup == NULL) + return (NULL); + return (dup); +} + +int expand_heredoc(char **str, t_head *head) +{ + while (find_var(*str) != -1) + { + *str = replace_var_heredoc(*str, head); + if (*str == NULL) + return (1); + } + return (0); +} + +int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) +{ + char *str; + + while (42) + { + str = readline(">> "); + if (g_error == 130 || !str) + return (0); + if (ft_strcmp(str, eof) == 0) + { + free(str); + break ; + } + if (copy->redir->quote == 0) + { + if (expand_heredoc(&str, head) == 1) + return (-1); + } + write(fd, str, ft_strlen(str)); + write(fd, "\n", 1); + free(str); + } + close(fd); + return (0); +} + +int checker_heredoc(t_head *head, t_redir *c_redir, t_cmd *c_cmd) +{ + int fd[2]; + + if (!c_redir->arg || !c_redir->arg[0]) + return (write(2, "bash: syntax error "\ + "near unexpected token `newline'\n", 51), 0); + if (pipe(fd) == -1) + { + perror("pipe"); + exit_free(head, 1); + } + c_redir->fd = fd[0]; + if (here_read_print(fd[1], c_redir->arg, c_cmd, head)) + return (0); + return (1); +} + +int heredoc(t_head *head) +{ + t_redir *c_redir; + t_cmd *c_cmd; + + c_cmd = head->cmd; + while (c_cmd) + { + c_redir = head->cmd->redir; + while (c_redir) + { + if (c_redir->type == 4) + { + sig_main(head, 2); + if (checker_heredoc(head, c_redir, c_cmd) == 0) + return (0); + sig_main(head, 1); + } + c_redir = c_redir->next; + } + c_cmd = c_cmd->next; + } + return (1); +} diff --git a/exec/signals_main.c b/exec/signals_main.c new file mode 100644 index 0000000..01993b7 --- /dev/null +++ b/exec/signals_main.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* signals_main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:53:49 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:06:17 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void sig(int sig) +{ + if (sig == SIGINT) + { + g_error = 130; + write(2, "\n", 1); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + } +} + +void sig_child(int sig) +{ + if (sig == SIGPIPE) + { + rl_on_new_line(); + rl_replace_line("", 0); + } + if (sig == SIGINT) + { + g_error = 130; + write(1, "\n", 1); + } + if (sig == SIGQUIT) + { + g_error = 131; + write(2, "Quit (core dumped)\n", 19); + rl_on_new_line(); + rl_replace_line("", 0); + } +} + +void sig_heredoc(int sig) +{ + if (sig == SIGINT) + { + g_error = 130; + ioctl(STDIN_FILENO, TIOCSTI, "\n"); + rl_replace_line("", 0); + rl_redisplay(); + } +} + +void sig_main(t_head *head, int sig_val) +{ + if (SIG_ERR == signal(SIGINT, SIG_IGN)) + ft_exit(head); + if (SIG_ERR == signal(SIGQUIT, SIG_IGN)) + ft_exit(head); + if (sig_val == 0) + { + signal(SIGQUIT, SIG_IGN); + signal(SIGINT, &sig); + } + else if (sig_val == 1) + { + signal(SIGQUIT, &sig_child); + signal(SIGPIPE, &sig_child); + signal(SIGINT, &sig_child); + } + else if (sig_val == 2) + { + signal(SIGQUIT, SIG_IGN); + signal(SIGINT, &sig_heredoc); + } +} diff --git a/includes/builtins.h b/includes/builtins.h new file mode 100644 index 0000000..54e3324 --- /dev/null +++ b/includes/builtins.h @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ +/* Updated: 2024/06/28 17:58:23 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTINS_H +# define BUILTINS_H + +# include "minishell.h" +# include "errno.h" +# include +# include +# include +# include + +typedef struct s_env t_env; +typedef struct s_redir t_redir; +typedef struct s_cmd t_cmd; +typedef struct s_fnp t_fnp; +typedef struct s_head t_head; + +int ft_cd(t_head *head); +int ft_cd_bis(t_head *head, char **str); +int replace_value(t_head *head, char *value, char *replace); +void cd_no_arg(t_head *head, char **str); +char *ft_strcat(char *path, char *dir); +//cd_back +void cd_back(t_head *head); +int cd_tild(t_head *head); + +void ft_echo(t_head *head, t_cmd *copy); +void print_tab(t_head *head, char **arg, int i, int n); + +void ft_env(t_head *head); +int checker_env(char **arg); + +void ft_exit(t_head *head); +void exit_write(char *arg); +int ft_strnum(char *str); +int is_num(t_head *head); +//exit_free +void exit_free(t_head *head, int status); +void new_pwd(t_head *head, char *value, char *name); +void cd_rep_or_new(t_head *head, char *n_pwd, char *old_pwd); + +int ft_export(t_head *head); +int export_bis(t_head *head, t_env *c_env, size_t i); +int ft_strcmp_until(char *s1, const char *s2); +char *replace_var_until(char *arg, char *result); +int new_var(t_head *head, char *name); +//export_add +void new_var_bis(t_env *copy, t_head *head, t_env *new); +int export_search_env(t_env *c_env, char *name); +void error_handle(char *str); +int add_var(t_head *head, char *str, t_env *c_env); +//export_checker +int check_name_errors(char *name); +int check_name(t_head *head, char *name, t_env *c_env); +int no_plus(char *name); +//export_utils +int check_equal(char *str); +char *dup_until(char *str); +char *dup_if(char *name); +int ft_strlen_until(char *str); +void free_tab(char **tab); +//built_export_no_arg +char *join_equal(char *name, char *val); +char **env_to_tab(t_env *copy); +void printf_tab(char **tab); +void swap_this_tab(char **tab); +int ex_no_args(t_head *head); + +void ft_pwd(t_head *head); +int get_path(char **str); +int check_dir(t_head *head); +char *value_of_name(t_env *env, char *name); + +void ft_unset(t_head *head); +void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)); +//exec_main +int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x); +void there_cmd(char **arg, char *str, char **env); +int exec_shell(t_head *head, t_cmd *copy, t_fnp *fnp); +int executable(t_head *head); + +//exec_utils +char *find_path(t_head *head); +char *join_with_char(char const *s1, char const *s2, char c); +char **make_env(t_env *env); +void no_path_to_hapiness(t_head *head, char **env, char **tab); + +//exec_wait +void wait_for_all(pid_t *pid, int x); +size_t envlen(t_env *base); +size_t cmdlen(t_cmd *base); +char **redir_arg(t_cmd *cmd, int type); +char **make_arg(t_cmd *cmd); + +//exec_redir +void fail_malloc(t_head *head, int **fd, size_t i); +int open_the_pipe(int **fd, t_head *head); +int open_files(t_redir *redir); +int open_redir(t_cmd *copy, int fd[2]); + +//exec_redir_utils +int fill_pipe(size_t numb, int **fd, t_head *head); +int redir_with_fd(int fd[2], int **pipe, t_cmd *copy, int x); +void close_pipe(t_head *head, int **fd); +void free_fnp(t_head *head, t_fnp *fnp); +int malloc_fnp(t_head *head); +//heredoc +void clear_heredoc_exec(char **arg, char *str, char **env); +int clear_heredoc(t_head *head); +int heredoc(t_head *head); + +#endif diff --git a/includes/minishell.h b/includes/minishell.h new file mode 100644 index 0000000..834c680 --- /dev/null +++ b/includes/minishell.h @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* minishell.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/11 16:24:43 by beroy #+# #+# */ +/* Updated: 2024/06/26 20:16:08 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_H +# define MINISHELL_H + +# include "../super_libft/libft.h" +# include +# include +# include +# include +# include +# include +# include "builtins.h" +# include "parser.h" + +// COLOR CODES + +# define BLACK "\033[0;30m" +# define RED "\033[0;31m" +# define GREEN "\033[0;32m" +# define YELLOW "\033[0;33m" +# define BLUE "\033[0;34m" +# define PURPLE "\033[0;35m" +# define CYAN "\033[0;36m" + +extern int g_error; + +typedef struct s_env +{ + char *name; + char *value; + struct s_env *next; + struct s_env *prev; +} t_env; + +typedef struct s_redir +{ + int type; + int fd; + int quote; + char *arg; + struct s_redir *next; + struct s_redir *prev; +} t_redir; + +typedef struct s_cmd +{ + char *line; + char **arg; + t_redir *redir; + struct s_cmd *next; + struct s_cmd *prev; +} t_cmd; + +typedef struct s_fnp +{ + int *pid; + int **pipe; +} t_fnp; + +typedef struct s_head +{ + t_cmd *cmd; + t_env *env; + t_fnp *fnp; +} t_head; + +//signal +void sig_main(t_head *head, int sig_val); + +#endif diff --git a/includes/parser.h b/includes/parser.h new file mode 100644 index 0000000..98e71e4 --- /dev/null +++ b/includes/parser.h @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 14:44:03 by beroy #+# #+# */ +/* Updated: 2024/06/28 15:59:26 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSER_H +# define PARSER_H + +typedef struct s_env t_env; +typedef struct s_redir t_redir; +typedef struct s_cmd t_cmd; +typedef struct s_head t_head; + +// backslash.c + +void remove_backslash(char *str); +void format_backslash(t_head *head); + +// expand_var.c + +char *str_dup_var(char *str, int s, int e, char *val); +int replace_var_redir(char *str, t_head *head); +int format_var_redir(t_head *head); +int format_var(t_head *head); + +// expand_var_utils.c + +int find_var(char *str); +int str_cmp_var(char *str, int i, char *value); +char *var_value(char *str, int i, t_env *env); +int find_end_var(char *str, int i); +int replace_var_line(char *str, t_head *head); + +// expand_var_utils.c + +int end_of_var(char c); + +// format_redir.c + +void tab_display(char **tab); +int spaces_to_add(char *str); +void ft_dupwithspace2(char *str, char *dup, int i, int j); +char *ft_dupwithspace(char *str); +int space_redir(t_cmd *cmd); + +// free_func.c + +void ft_free_cmd(t_cmd **cmd); +void ft_free_all(t_head *head); + +// header.c + +char *ft_color(int i); +void ft_header(void); + +// init.c + +t_env *ft_env_new(char *env); +t_env *ft_envlast(t_env *lst); +void ft_envadd_back(t_env **lst, t_env *new); +t_env *env_init(char **env_tab); +t_head *head_init(char **env); + +// init_utils.c + +t_env *ft_env_new_bis(char *name, char *value); +int find_equal(char *str); +t_redir *ft_redir_new(char *line, int start, int end, int type); +t_redir *ft_redirlast(t_redir *lst); +void ft_rediradd_back(t_redir **lst, t_redir *new); + +// init_utils2.c + +t_env *env_if_empty(void); + +// parser.c + +int redir_is_empty(t_redir *redir); +int cmd_is_empty(t_cmd *cmd, int full); +int quote_skip(char *input, int *i, char find); +int check_line(char *input); +t_cmd *split_pipe(char *input); +int ft_parse(char *input, t_head *head); + +// parser_utils.c + +int redir_is_empty(t_redir *redir); +t_cmd *ft_cmd_new(char *input); +t_cmd *ft_cmdlast(t_cmd *lst); +void ft_cmdadd_back(t_cmd **lst, t_cmd *new); +void find_pipe(char *input, int *i); + +// pre_check.c + +int first_pipe(char *str); +int check_pipe(char *input, int *i); +int check_redir(char *input, int *i); +int check_redir_pipe(char *input); + +// redir.c + +int format_redir(t_cmd *cmd); + +// redir_utils.c + +int redir_type(char *line, int i); +int find_redir(char *line); +int find_arg(char *line, int start); +char *dup_without_redir(char *line, int end); +int extract_redir(t_cmd *cmd); + +// split.c + +int ft_countwords(char *input); +int ft_wdlen(char *input, int j); +char **split_ws_quote(char *input); +int split_ws(t_cmd *cmd); + +// trim.c + +int nbr_quote(char *str); +void trim_str_2(char *str, char *trim, int len, int nbr); +char *trim_str(char *str); +int trim_tab(char **tab); +int format(t_head *head); + +// trim_utils.c + +int trim_redir(t_head *head); + +// utils.c + +int str_empty(char *str); +int char_is_ws(char c); +void single_index_up(char *input, int *i); +int char_is_num(char c); +void index_up(char *input, int *i); + +#endif \ No newline at end of file diff --git a/srcs/backslash.c b/srcs/backslash.c new file mode 100644 index 0000000..6cba345 --- /dev/null +++ b/srcs/backslash.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* backslash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 16:10:02 by beroy #+# #+# */ +/* Updated: 2024/06/20 17:33:09 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void remove_backslash(char *str) +{ + int i; + int j; + + i = 0; + j = 0; + while (str && str[j]) + { + if (str[j] == '\\' && (str[j + 1] == '$' + || (j != 0 && str[j - 1] == '\\'))) + j++; + str[i] = str[j]; + i++; + j++; + } + str[i] = 0; +} + +void format_backslash(t_head *head) +{ + while (head->cmd) + { + remove_backslash(head->cmd->line); + while (head->cmd->redir) + { + remove_backslash(head->cmd->redir->arg); + if (head->cmd->redir->next == NULL) + break ; + head->cmd->redir = head->cmd->redir->next; + } + while (head->cmd->redir && head->cmd->redir->prev) + head->cmd->redir = head->cmd->redir->prev; + if (head->cmd->next == NULL) + break ; + head->cmd = head->cmd->next; + } + while (head->cmd->prev) + head->cmd = head->cmd->prev; +} diff --git a/srcs/expand_var.c b/srcs/expand_var.c new file mode 100644 index 0000000..615bc12 --- /dev/null +++ b/srcs/expand_var.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 14:03:41 by beroy #+# #+# */ +/* Updated: 2024/06/27 13:08:22 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +char *str_dup_var(char *str, int s, int e, char *val) +{ + int i; + int j; + char *dup; + + i = 0; + j = 0; + dup = ft_calloc(ft_strlen(str) - e + s + ft_strlen(val) + 1, sizeof(char)); + if (dup == NULL) + return (NULL); + while (i < s) + dup[j++] = str[i++]; + i = 0; + while (val[i]) + dup[j++] = val[i++]; + while (str[e]) + dup[j++] = str[e++]; + return (ft_free(str), ft_free(val), dup); +} + +int replace_var_redir(char *str, t_head *head) +{ + int start; + int end; + char *value; + + start = find_var(str); + end = find_end_var(str, start); + value = var_value(str, start + 1, head->env); + if (value == NULL) + return (1); + head->cmd->redir->arg = str_dup_var(str, start, end, value); + if (str == NULL) + return (1); + return (0); +} + +int format_var_redir(t_head *head) +{ + while (head->cmd->redir) + { + while (head->cmd->redir->type != 4 + && find_var(head->cmd->redir->arg) != -1) + { + if (replace_var_redir(head->cmd->redir->arg, head) == 1) + return (1); + } + if (head->cmd->redir->next == NULL) + break ; + head->cmd->redir = head->cmd->redir->next; + } + while (head->cmd->redir && head->cmd->redir->prev) + head->cmd->redir = head->cmd->redir->prev; + return (0); +} + +int format_var(t_head *head) +{ + while (head->cmd) + { + while (find_var(head->cmd->line) != -1) + { + if (replace_var_line(head->cmd->line, head) == 1) + return (1); + } + if (format_var_redir(head) == 1) + return (1); + if (head->cmd->next == NULL) + break ; + head->cmd = head->cmd->next; + } + while (head->cmd->prev) + head->cmd = head->cmd->prev; + return (0); +} diff --git a/srcs/expand_var_utils.c b/srcs/expand_var_utils.c new file mode 100644 index 0000000..e120aff --- /dev/null +++ b/srcs/expand_var_utils.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_var_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 13:55:23 by beroy #+# #+# */ +/* Updated: 2024/06/24 17:10:51 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int find_var(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] == '$') + { + if (i == 0) + return (i); + if (i > 0 && str[i - 1] != '\\') + return (i); + if (i > 1 && str[i - 1] == '\\' && str[i - 2] == '\\') + return (i); + } + single_index_up(str, &i); + } + return (-1); +} + +int str_cmp_var(char *str, int i, char *value) +{ + int j; + + j = 0; + while (str[i] && value[j] && str[i] == value[j]) + { + i++; + j++; + } + if (end_of_var(str[i]) == 1 && value[j] == 0) + return (1); + return (0); +} + +char *var_value(char *str, int i, t_env *env) +{ + char *value; + + value = NULL; + while (env && value == NULL) + { + if (str_cmp_var(str, i, env->name) == 1) + { + value = ft_strdup(env->value); + if (value == NULL) + return (NULL); + } + if (env->next == NULL) + break ; + env = env->next; + } + if (value == NULL && str_cmp_var(str, i, "?")) + value = ft_itoa(g_error); + if (value == NULL) + value = ft_strdup(""); + while (env && env->prev) + env = env->prev; + return (value); +} + +int find_end_var(char *str, int i) +{ + int j; + + i++; + j = i; + while (str && str[i]) + { + if (end_of_var(str[i]) == 1) + break ; + if (j == i && char_is_num(str[i]) == 1) + return (i + 1); + single_index_up(str, &i); + } + return (i); +} + +int replace_var_line(char *str, t_head *head) +{ + int start; + int end; + char *value; + + start = find_var(str); + end = find_end_var(str, start); + value = var_value(str, start + 1, head->env); + if (value == NULL) + return (1); + head->cmd->line = str_dup_var(str, start, end, value); + if (str == NULL) + return (1); + return (0); +} diff --git a/srcs/expand_var_utils2.c b/srcs/expand_var_utils2.c new file mode 100644 index 0000000..07605a0 --- /dev/null +++ b/srcs/expand_var_utils2.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_var_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 18:32:58 by beroy #+# #+# */ +/* Updated: 2024/06/28 18:37:44 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int end_of_var(char c) +{ + if (char_is_num(c) == 1) + return (0); + if (c == '_') + return (0); + if (c >= 65 && c <= 90) + return (0); + if (c >= 97 && c <= 122) + return (0); + return (1); +} diff --git a/srcs/format_redir.c b/srcs/format_redir.c new file mode 100644 index 0000000..9245bdb --- /dev/null +++ b/srcs/format_redir.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_utils_3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/22 17:36:26 by beroy #+# #+# */ +/* Updated: 2024/06/17 18:11:26 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int spaces_to_add(char *str) +{ + int i; + int spaces; + + i = 0; + spaces = 0; + while (str[i]) + { + if (str[i] == '<') + { + if (str[i + 1] == '<') + i++; + spaces += 2; + } + else if (str[i] == '>') + { + if (str[i + 1] == '>') + i++; + spaces += 2; + } + index_up(str, &i); + } + return (spaces); +} + +void ft_dupwithspace2(char *str, char *dup, int i, int j) +{ + char quote; + + quote = 0; + while (str[i]) + { + if (quote == 0 && (str[i] == 34 || str[i] == 39)) + quote = str[i]; + else if (str[i] == quote) + quote = 0; + if (quote == 0 && (str[i] == '<' || str[i] == '>')) + { + dup[j++] = ' '; + dup[j++] = str[i]; + if (str[i + 1] == str[i]) + { + dup[j++] = str[i]; + i++; + } + dup[j++] = ' '; + } + else + dup[j++] = str[i]; + i++; + } +} + +char *ft_dupwithspace(char *str) +{ + int i; + int j; + char *dup; + + i = 0; + j = 0; + dup = ft_calloc(ft_strlen(str) + spaces_to_add(str) + 1, sizeof(char)); + if (dup == NULL) + return (NULL); + ft_dupwithspace2(str, dup, i, j); + return (ft_free(str), dup); +} + +int space_redir(t_cmd *cmd) +{ + while (cmd) + { + cmd->line = ft_dupwithspace(cmd->line); + if (cmd->line == NULL) + return (1); + if (cmd->next == NULL) + break ; + cmd = cmd->next; + } + while (cmd->prev) + cmd = cmd->prev; + return (0); +} diff --git a/srcs/free_func.c b/srcs/free_func.c new file mode 100644 index 0000000..bed64a1 --- /dev/null +++ b/srcs/free_func.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_func.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:57:26 by beroy #+# #+# */ +/* Updated: 2024/06/27 17:40:52 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void ft_free_cmd(t_cmd **cmd) +{ + t_redir *tmp_redir; + t_cmd *tmp_cmd; + + while ((*cmd)) + { + tmp_cmd = (*cmd)->next; + if ((*cmd)->arg) + ft_splitdestroy((*cmd)->arg); + if ((*cmd)->line) + ft_free((*cmd)->line); + while ((*cmd)->redir != NULL) + { + tmp_redir = (*cmd)->redir->next; + ft_free((*cmd)->redir->arg); + ft_free((*cmd)->redir); + (*cmd)->redir = tmp_redir; + } + if ((*cmd) != NULL) + ft_free(*cmd); + (*cmd) = tmp_cmd; + } +} + +void ft_free_all(t_head *head) +{ + t_env *tmp; + + if (head->cmd != NULL) + ft_free_cmd(&(head->cmd)); + while (head->env != NULL) + { + tmp = head->env->next; + ft_free(head->env->name); + ft_free(head->env->value); + ft_free(head->env); + head->env = tmp; + } + if (head != NULL) + ft_free(head); +} diff --git a/srcs/header.c b/srcs/header.c new file mode 100644 index 0000000..c0dbece --- /dev/null +++ b/srcs/header.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* header.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/20 17:55:35 by beroy #+# #+# */ +/* Updated: 2024/06/20 17:55:35 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +char *ft_color(int i) +{ + if (i % 6 == 0) + return (ft_strdup(YELLOW)); + if (i % 6 == 1) + return (ft_strdup(GREEN)); + if (i % 6 == 2) + return (ft_strdup(CYAN)); + if (i % 6 == 3) + return (ft_strdup(BLUE)); + if (i % 6 == 4) + return (ft_strdup(PURPLE)); + else + return (ft_strdup(RED)); +} + +void ft_header(void) +{ + char *str; + char *color; + int fd; + int i; + + fd = open("data/header.txt", O_RDONLY); + str = get_next_line(fd); + i = 0; + while (str) + { + color = ft_color(i); + printf("%s%s", color, str); + ft_free (str); + ft_free (color); + str = get_next_line(fd); + i++; + } + printf("\033[0m\n"); + close (fd); +} diff --git a/srcs/init.c b/srcs/init.c new file mode 100644 index 0000000..1566eb5 --- /dev/null +++ b/srcs/init.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/18 17:14:37 by beroy #+# #+# */ +/* Updated: 2024/06/26 19:38:31 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +t_env *ft_env_new(char *env) +{ + t_env *s_new; + int equal; + + equal = find_equal(env) + 1; + s_new = ft_calloc(1, sizeof(t_env)); + if (s_new == NULL) + return (s_new); + s_new->name = ft_substr(env, 0, equal - 1); + if (s_new->name == NULL) + return (free(s_new), NULL); + s_new->value = ft_substr(env, equal, ft_strlen(env) - equal); + if (s_new->value == NULL) + return (ft_free(s_new->name), ft_free(s_new), NULL); + s_new->next = NULL; + s_new->prev = NULL; + return (s_new); +} + +t_env *ft_envlast(t_env *lst) +{ + if (!lst) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} + +void ft_envadd_back(t_env **lst, t_env *new) +{ + t_env *last; + + if (lst) + { + if (*lst == NULL) + *lst = new; + else + { + last = ft_envlast(*lst); + last->next = new; + new->prev = last; + } + } +} + +t_env *env_init(char **env_tab) +{ + int i; + t_env *new; + t_env *env; + + if (env_tab && !env_tab[0]) + return (env_if_empty()); + i = 0; + env = NULL; + while (env_tab[i]) + { + new = ft_env_new(env_tab[i]); + if (new == NULL) + return (NULL); + ft_envadd_back(&env, new); + i++; + } + return (env); +} + +t_head *head_init(char **env) +{ + t_head *head; + + head = ft_calloc(1, sizeof(t_head)); + if (head == NULL) + return (printf("Malloc failed!\n"), NULL); + head->env = env_init(env); + if (head->env == NULL) + return (printf("Malloc failed!\n"), free(head), NULL); + head->cmd = NULL; + return (head); +} diff --git a/srcs/init_utils.c b/srcs/init_utils.c new file mode 100644 index 0000000..30ce646 --- /dev/null +++ b/srcs/init_utils.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/29 16:59:12 by beroy #+# #+# */ +/* Updated: 2024/06/26 19:54:15 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +t_env *ft_env_new_bis(char *name, char *value) +{ + t_env *s_new; + + s_new = ft_calloc(1, sizeof(t_env)); + if (s_new == NULL) + return (s_new); + s_new->name = ft_strdup(name); + if (s_new->name == NULL) + return (free(s_new), NULL); + s_new->value = ft_strdup(value); + if (s_new->value == NULL) + return (ft_free(s_new->name), ft_free(s_new), NULL); + s_new->next = NULL; + s_new->prev = NULL; + return (s_new); +} + +int find_equal(char *str) +{ + int i; + + i = 0; + while (str[i] && str[i] != '=') + i++; + return (i); +} + +t_redir *ft_redir_new(char *line, int start, int end, int type) +{ + t_redir *s_new; + + s_new = ft_calloc(1, sizeof(t_redir)); + if (s_new == NULL) + return (s_new); + s_new->type = type; + s_new->arg = ft_substr(line, start, end - start + 1); + if (s_new->arg == NULL) + return (ft_free(s_new), NULL); + s_new->next = NULL; + s_new->prev = NULL; + s_new->fd = -1; + s_new->quote = 0; + return (s_new); +} + +t_redir *ft_redirlast(t_redir *lst) +{ + if (!lst) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} + +void ft_rediradd_back(t_redir **lst, t_redir *new) +{ + t_redir *last; + + if (lst) + { + if (*lst == NULL) + *lst = new; + else + { + last = ft_redirlast(*lst); + last->next = new; + new->prev = last; + } + } +} diff --git a/srcs/init_utils2.c b/srcs/init_utils2.c new file mode 100644 index 0000000..bb1dc79 --- /dev/null +++ b/srcs/init_utils2.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 20:10:22 by grebrune #+# #+# */ +/* Updated: 2024/06/26 20:10:22 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +t_env *env_if_empty(void) +{ + t_env *new; + t_env *env; + char *str; + + env = NULL; + get_path(&str); + new = ft_env_new_bis("PWD", str); + ft_free(str); + if (new == NULL) + return (NULL); + ft_envadd_back(&env, new); + new = ft_env_new_bis("SHLVL", "1"); + if (new == NULL) + return (NULL); + ft_envadd_back(&env, new); + return (env); +} diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..0e8f3e1 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ +/* Updated: 2024/06/28 15:55:25 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int g_error = 0; + +int single_exec(t_head *head, char *input) +{ + sig_main(head, 0); + if (input == NULL || str_empty(input) == 1) + return (0); + if (ft_parse(input, head) == 0) + { + heredoc(head); + if (cmd_is_empty(head->cmd, 0) == 0) + executable(head); + } + return (0); +} + +void main_loop(t_head *head) +{ + char *input; + + while (42) + { + sig_main(head, 0); + input = readline("> "); + if (input == NULL) + break ; + add_history(input); + sig_main(head, 3); + if (str_empty(input) == 1) + continue ; + if (ft_parse(input, head) == 0) + { + if (heredoc(head)) + if (cmd_is_empty(head->cmd, 0) == 0) + executable(head); + } + if (head->cmd != NULL) + ft_free_cmd(&(head->cmd)); + } + write(1, "exit\n", 5); +} + +int main(int ac, char **av, char **env) +{ + t_head *head; + + (void)ac; + (void)av; + ft_header(); + head = head_init(env); + if (head == NULL) + return (0); + if (ac == 1) + main_loop(head); + if (ac == 2) + single_exec(head, av[1]); + return (ft_free_all(head), 0); +} diff --git a/srcs/parser.c b/srcs/parser.c new file mode 100644 index 0000000..124141e --- /dev/null +++ b/srcs/parser.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/17 13:19:51 by beroy #+# #+# */ +/* Updated: 2024/06/27 13:29:29 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int cmd_is_empty(t_cmd *cmd, int full) +{ + int i; + int ret; + + ret = 1; + while (cmd) + { + i = 0; + while (cmd->arg[i]) + { + if (str_empty(cmd->arg[i]) == 0) + ret = 0; + i++; + } + if (full == 1 && redir_is_empty(cmd->redir) == 0) + ret = 0; + if (cmd->next == NULL) + break ; + cmd = cmd->next; + } + while (cmd->prev) + cmd = cmd->prev; + return (ret); +} + +int quote_skip(char *input, int *i, char find) +{ + *i += 1; + while (input[*i] != 0 && input[*i] != find) + *i += 1; + if (input[*i] == 0) + return (1); + return (0); +} + +int check_line(char *input) +{ + int i; + + i = 0; + if (first_pipe(input) == 1) + return (1); + if (str_empty(input) == 1) + return (1); + if (check_redir_pipe(input) == 1) + return (1); + while (input[i]) + { + if (input[i] == '|') + { + if (input[i + 1] == '|') + return (printf("Double pipe not handled by minishell!\n"), 1); + } + else if (input[i] == 34 || input[i] == 39) + { + if (quote_skip(input, &i, input[i]) == 1) + return (printf("Quote unterminated!\n"), 1); + } + i++; + } + return (0); +} + +t_cmd *split_pipe(char *input) +{ + t_cmd *cmd; + t_cmd *new; + char *extract; + int offset; + int i; + + offset = 0; + i = 0; + cmd = NULL; + while (input[i]) + { + find_pipe(input, &i); + extract = ft_substr(input, offset, i - offset); + if (extract == NULL) + return (ft_free_cmd(&cmd), NULL); + new = ft_cmd_new(extract); + if (new == NULL) + return (ft_free_cmd(&cmd), NULL); + ft_cmdadd_back(&cmd, new); + if (input[i] != 0) + i++; + offset = i; + } + return (cmd); +} + +int ft_parse(char *input, t_head *head) +{ + if (check_line(input) == 1) + return (1); + head->cmd = split_pipe(input); + if (head->cmd == NULL) + return (1); + if (space_redir(head->cmd) == 1) + return (1); + if (format_redir(head->cmd) == 1) + return (1); + if (format_var(head) == 1) + return (1); + format_backslash(head); + if (split_ws(head->cmd) == 1) + return (1); + if (format(head) == 1) + return (1); + if (cmd_is_empty(head->cmd, 1) == 1) + return (1); + return (0); +} diff --git a/srcs/parser_utils.c b/srcs/parser_utils.c new file mode 100644 index 0000000..c67222e --- /dev/null +++ b/srcs/parser_utils.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/22 13:21:52 by beroy #+# #+# */ +/* Updated: 2024/06/28 15:59:26 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int redir_is_empty(t_redir *redir) +{ + int ret; + + ret = 1; + if (!redir) + return (ret); + while (redir) + { + if (str_empty(redir->arg) == 0) + ret = 0; + if (redir->next == NULL) + break ; + redir = redir->next; + } + while (redir->prev) + redir = redir->prev; + return (ret); +} + +t_cmd *ft_cmd_new(char *input) +{ + t_cmd *s_new; + + s_new = ft_calloc(1, sizeof(t_cmd)); + if (s_new == NULL) + return (s_new); + s_new->line = input; + s_new->arg = NULL; + s_new->redir = NULL; + s_new->next = NULL; + s_new->prev = NULL; + return (s_new); +} + +t_cmd *ft_cmdlast(t_cmd *lst) +{ + if (!lst) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} + +void ft_cmdadd_back(t_cmd **lst, t_cmd *new) +{ + t_cmd *last; + + if (lst) + { + if (*lst == NULL) + *lst = new; + else + { + last = ft_cmdlast(*lst); + last->next = new; + new->prev = last; + } + } +} + +void find_pipe(char *input, int *i) +{ + while (input[*i]) + { + if (input[*i] == '|' || input [*i] == 0) + return ; + index_up(input, i); + } +} diff --git a/srcs/pre_check.c b/srcs/pre_check.c new file mode 100644 index 0000000..e2d4439 --- /dev/null +++ b/srcs/pre_check.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pre_check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/03 14:45:58 by beroy #+# #+# */ +/* Updated: 2024/06/28 16:00:01 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int first_pipe(char *str) +{ + int i; + + i = 0; + while (str && str[i] && str[i] != '|') + { + if (char_is_ws(str[i]) == 0) + return (0); + i++; + } + return (1); +} + +int check_pipe(char *input, int *i) +{ + int nbr; + + nbr = 0; + if (*i != 0 && input[*i - 1] == input[*i]) + nbr++; + if (input[*i] == input[*i + 1]) + nbr++; + if (nbr >= 2) + return (1); + return (0); +} + +int check_redir(char *input, int *i) +{ + int nbr; + + nbr = 0; + if (input[*i] == '<' && input[*i + 1] == '>') + return (1); + if (input[*i] == '>' && input[*i + 1] == '<') + return (1); + if (*i != 0 && input[*i - 1] == input[*i]) + nbr++; + if (input[*i] == input[*i + 1]) + nbr++; + if (nbr >= 2) + return (1); + return (0); +} + +int check_redir_pipe(char *input) +{ + int i; + + i = 0; + while (input && input[i]) + { + if (input[i] == '|') + if (check_pipe(input, &i) == 1) + return (1); + if (input[i] == '<' || input[i] == '>') + if (check_redir(input, &i) == 1) + return (1); + index_up(input, &i); + } + return (0); +} diff --git a/srcs/redir.c b/srcs/redir.c new file mode 100644 index 0000000..40566f5 --- /dev/null +++ b/srcs/redir.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 14:02:57 by beroy #+# #+# */ +/* Updated: 2024/06/19 14:06:05 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int format_redir(t_cmd *cmd) +{ + while (cmd) + { + while (find_redir(cmd->line) == 1) + { + if (extract_redir(cmd) == 1) + return (1); + } + if (cmd->next == NULL) + break ; + cmd = cmd->next; + } + while (cmd->prev) + cmd = cmd->prev; + return (0); +} diff --git a/srcs/redir_utils.c b/srcs/redir_utils.c new file mode 100644 index 0000000..27109cf --- /dev/null +++ b/srcs/redir_utils.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redir_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/13 15:00:16 by beroy #+# #+# */ +/* Updated: 2024/06/26 20:22:45 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int redir_type(char *line, int i) +{ + if (line[i] == '>') + { + if (line[i + 1] == '>') + return (2); + else + return (1); + } + else if (line[i] == '<') + { + if (line[i + 1] == '<') + return (4); + else + return (3); + } + else + return (0); +} + +int find_redir(char *line) +{ + int i; + + i = 0; + while (line[i]) + { + if (redir_type(line, i) != 0) + return (1); + index_up(line, &i); + } + return (0); +} + +int find_arg(char *line, int start) +{ + while (line[start] && char_is_ws(line[start]) == 1) + index_up(line, &start); + while (line[start] && char_is_ws(line[start]) == 0) + index_up(line, &start); + return (start - 1); +} + +char *dup_without_redir(char *line, int end) +{ + int i; + int j; + char *dup; + int start; + + i = 0; + j = 0; + start = 0; + while (redir_type(line, start) == 0) + index_up(line, &start); + dup = ft_calloc(ft_strlen(line) - end + start + 1, sizeof(char)); + if (dup == NULL) + return (ft_free(line), NULL); + while (line[i]) + { + if (i == start) + i = end + 1; + dup[j] = line[i]; + if (line[i] != 0) + i++; + j++; + } + return (ft_free(line), dup); +} + +int extract_redir(t_cmd *cmd) +{ + int start; + int end; + int type; + t_redir *new; + + start = 0; + while (redir_type(cmd->line, start) == 0) + index_up(cmd->line, &start); + type = redir_type(cmd->line, start); + if (type % 2 == 0) + start += 2; + else + start++; + while (char_is_ws(cmd->line[start]) == 1) + start++; + end = find_arg(cmd->line, start); + new = ft_redir_new(cmd->line, start, end, type); + if (new == NULL) + return (1); + ft_rediradd_back(&cmd->redir, new); + cmd->line = dup_without_redir(cmd->line, end); + if (cmd->line == NULL) + return (1); + return (0); +} diff --git a/srcs/split.c b/srcs/split.c new file mode 100644 index 0000000..982ce1c --- /dev/null +++ b/srcs/split.c @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/22 16:18:25 by beroy #+# #+# */ +/* Updated: 2024/06/27 17:38:42 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int ft_countwords(char *input) +{ + int i; + int wds; + + wds = 0; + i = 0; + if (char_is_ws(input[i]) == 0 && input [i]) + wds++; + index_up(input, &i); + while (input[i]) + { + if (char_is_ws(input[i]) == 0 && char_is_ws(input[i - 1]) == 1) + wds++; + index_up(input, &i); + } + return (wds); +} + +int ft_wdlen(char *input, int j) +{ + int i; + + i = j; + while (char_is_ws(input[i]) == 0 && input[i]) + index_up(input, &i); + return (i); +} + +static char *ft_superdup(char *s, int *j) +{ + char *str; + int len; + int i; + + len = ft_wdlen(s, *j); + str = ft_calloc(len - *j + 1, sizeof(char)); + if (str == NULL) + return (str); + i = 0; + while (*j < len) + { + str[i] = s[*j]; + *j += 1; + i++; + } + str[i] = 0; + return (str); +} + +char **split_ws_quote(char *input) +{ + int i; + int j; + int words; + char **split; + + i = 0; + j = 0; + words = ft_countwords(input); + split = ft_calloc(words + 1, sizeof(char *)); + if (split == NULL) + return (NULL); + while (i < words) + { + if (char_is_ws(input[j]) == 0) + { + split[i] = ft_superdup(input, &j); + if (split[i] == NULL) + return (ft_splitdestroy(split)); + i++; + } + else + index_up(input, &j); + } + return (split); +} + +int split_ws(t_cmd *cmd) +{ + while (cmd->next) + { + cmd->arg = split_ws_quote(cmd->line); + if (cmd->arg == NULL) + return (1); + cmd = cmd->next; + } + cmd->arg = split_ws_quote(cmd->line); + if (cmd->arg == NULL) + return (1); + while (cmd->prev) + cmd = cmd->prev; + return (0); +} diff --git a/srcs/trim.c b/srcs/trim.c new file mode 100644 index 0000000..940276b --- /dev/null +++ b/srcs/trim.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* trim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/25 15:00:48 by beroy #+# #+# */ +/* Updated: 2024/06/19 16:59:47 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int nbr_quote(char *str) +{ + int i; + int nbr; + char find; + + i = 0; + nbr = 0; + while (str && str[i]) + { + if (str[i] == 34 || str[i] == 39) + { + find = str[i]; + if (quote_skip(str, &i, find) == 1) + nbr++; + else + nbr += 2; + } + i++; + } + return (nbr); +} + +void trim_str_2(char *str, char *trim, int len, int nbr) +{ + int i; + int j; + int trigger; + char find; + + i = 0; + j = 0; + trigger = 0; + while (i < len - nbr) + { + if (trigger == 0 && (str[j] == 34 || str[j] == 39)) + { + find = str[j]; + trigger = 1; + j++; + } + else if (trigger == 1 && str[j] == find) + { + trigger = 0; + j++; + } + else + trim[i++] = str[j++]; + } +} + +char *trim_str(char *str) +{ + char *trim; + int len; + int nbr; + + nbr = nbr_quote(str); + if (nbr <= 1) + return (str); + len = (int)ft_strlen(str); + trim = ft_calloc(len - nbr + 1, sizeof(char)); + if (trim == NULL) + return (NULL); + trim_str_2(str, trim, len, nbr); + ft_free(str); + return (trim); +} + +int trim_tab(char **tab) +{ + int i; + + i = 0; + while (tab[i]) + { + tab[i] = trim_str(tab[i]); + if (tab[i] == NULL) + return (1); + i++; + } + return (0); +} + +int format(t_head *head) +{ + while (head->cmd) + { + if (trim_tab(head->cmd->arg) == 1) + return (1); + if (head->cmd->redir && trim_redir(head) == 1) + return (1); + if (head->cmd->next == NULL) + break ; + head->cmd = head->cmd->next; + } + while (head->cmd->prev) + head->cmd = head->cmd->prev; + return (0); +} diff --git a/srcs/trim_utils.c b/srcs/trim_utils.c new file mode 100644 index 0000000..4b17bb9 --- /dev/null +++ b/srcs/trim_utils.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* trim_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 13:54:15 by beroy #+# #+# */ +/* Updated: 2024/06/21 14:20:52 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int trim_redir(t_head *head) +{ + while (head->cmd->redir) + { + if (nbr_quote(head->cmd->redir->arg) > 1) + head->cmd->redir->quote = 1; + head->cmd->redir->arg = trim_str(head->cmd->redir->arg); + if (head->cmd->redir->arg == NULL) + return (1); + if (head->cmd->redir->next == NULL) + break ; + head->cmd->redir = head->cmd->redir->next; + } + while (head->cmd->redir->prev) + head->cmd->redir = head->cmd->redir->prev; + return (0); +} diff --git a/srcs/utils.c b/srcs/utils.c new file mode 100644 index 0000000..d147d83 --- /dev/null +++ b/srcs/utils.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 14:16:23 by beroy #+# #+# */ +/* Updated: 2024/06/19 14:17:17 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +int str_empty(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (char_is_ws(str[i]) == 0) + return (0); + i++; + } + return (1); +} + +int char_is_ws(char c) +{ + if ((c > 7 && c < 13) || c == ' ') + return (1); + return (0); +} + +void single_index_up(char *input, int *i) +{ + if (input[*i] == 39) + quote_skip(input, i, 39); + *i += 1; +} + +int char_is_num(char c) +{ + if (c >= 48 && c <= 57) + return (1); + return (0); +} + +void index_up(char *input, int *i) +{ + if (input[*i] == 34) + quote_skip(input, i, 34); + else if (input[*i] == 39) + quote_skip(input, i, 39); + *i += 1; +} diff --git a/super_libft/Makefile b/super_libft/Makefile new file mode 100644 index 0000000..95983c6 --- /dev/null +++ b/super_libft/Makefile @@ -0,0 +1,84 @@ +SRCS = ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_putchar_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstr_fd.c \ + ft_split.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_striteri.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strcmp.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_lstadd_back.c \ + ft_lstadd_front.c \ + ft_lstclear.c \ + ft_lstdelone.c \ + ft_lstiter.c \ + ft_lstlast.c \ + ft_lstmap.c \ + ft_lstnew.c \ + ft_lstsize.c \ + get_next_line.c \ + get_next_line_utils.c \ + ft_printf.c \ + ft_printf_utils.c \ + ft_free.c + +SRCS_D := srcs/ + +OBJS_D := objs/ + +OBJS := $(SRCS:%.c=$(OBJS_D)%.o) + +HEAD_D := . + +CFLAGS := -Wall -Wextra -Werror + +AR := ar -rc + +NAME := libft.a + +all : $(NAME) + +$(NAME) : $(OBJS_D) $(OBJS) Makefile + $(AR) $(NAME) $(OBJS) + +$(OBJS) : $(OBJS_D)%.o: $(SRCS_D)%.c $(HEAD) + $(CC) $(CFLAGS) -c $< -o $@ + +$(OBJS_D) : + @mkdir -p $(OBJS_D) + +clean : + $(RM) -r $(OBJS) $(OBJS_D) $(OBJS_B) + +fclean : clean + $(RM) $(NAME) + +re : fclean all + +.PHONY: all bonus clean fclean re \ No newline at end of file diff --git a/super_libft/libft.h b/super_libft/libft.h new file mode 100644 index 0000000..26c5ca8 --- /dev/null +++ b/super_libft/libft.h @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 13:57:13 by beroy #+# #+# */ +/* Updated: 2024/01/17 16:10:23 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 3 +# endif +# ifndef OPEN_MAX +# define OPEN_MAX 1024 +# endif + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +void ft_free(void *ptr); +void ft_bzero(void *s, size_t n); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *str, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void *ft_splitdestroy(char **split); + +void *ft_memchr(const void *s, int c, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +void *ft_calloc(size_t nmemb, size_t size); + +int ft_memcmp(const void *s1, const void *s2, size_t n); +int ft_strncmp(char *s1, const char *s2, size_t n); +int ft_strcmp(char *s1, const char *s2); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +int ft_tolower(int c); +int ft_toupper(int c); +int ft_atoi(const char *nptr); + +size_t ft_strlen(const char *str); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlcat(char *dest, char *src, unsigned int size); + +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strdup(const char *s); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strnstr(const char *big, const char *little, size_t len); + +char **ft_split(char const *s, char c); + +t_list *ft_lstnew(void *content); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +int ft_lstsize(t_list *lst); + +void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); + +// GNL + +char *get_next_line(int fd); +char *ft_strdup_nl(char *str); +int ft_linecheck(char *str); +char *ft_strjoin_nl(char *s1, char *s2); +void ft_buffclean(char *str); + +// Prinft + +int ft_printf(const char *str, ...); +int ft_printf_fd(int fd, const char *str, ...); +ssize_t ft_putchar(int c); +ssize_t ft_putstr(char *str); +void ft_putnbr_base(ssize_t nbr, char *str, ssize_t *length); +void ft_putnbr_ul(size_t nbr, char *str, ssize_t *length); + +#endif \ No newline at end of file diff --git a/super_libft/srcs/ft_atoi.c b/super_libft/srcs/ft_atoi.c new file mode 100644 index 0000000..6714275 --- /dev/null +++ b/super_libft/srcs/ft_atoi.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 14:21:21 by beroy #+# #+# */ +/* Updated: 2023/11/07 18:41:02 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_atoi(const char *nptr) +{ + size_t i; + int res; + int sign; + + i = 0; + res = 0; + sign = 1; + while ((nptr[i] > 8 && nptr[i] < 14) || nptr[i] == ' ') + i++; + if (nptr[i] == '+') + i++; + else if (nptr[i] == '-') + { + sign *= -1; + i++; + } + while (ft_isdigit(nptr[i]) == 1) + { + res = res * 10 + (nptr[i] - 48); + i++; + } + return (res * sign); +} diff --git a/super_libft/srcs/ft_bzero.c b/super_libft/srcs/ft_bzero.c new file mode 100644 index 0000000..d4a3129 --- /dev/null +++ b/super_libft/srcs/ft_bzero.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:52:19 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:56:54 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_bzero(void *s, size_t n) +{ + size_t i; + char *temp; + + temp = (char *)s; + i = 0; + while (i < n) + { + temp[i] = '\0'; + i++; + } +} diff --git a/super_libft/srcs/ft_calloc.c b/super_libft/srcs/ft_calloc.c new file mode 100644 index 0000000..27b9ad3 --- /dev/null +++ b/super_libft/srcs/ft_calloc.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 10:35:43 by beroy #+# #+# */ +/* Updated: 2023/11/14 11:42:55 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + size_t i; + + if (nmemb == 0 || size == 0) + { + ptr = malloc(nmemb * size); + return (ptr); + } + if (nmemb > SIZE_MAX / size || size > SIZE_MAX / nmemb) + return (NULL); + ptr = malloc(nmemb * size); + if (ptr == NULL) + return (ptr); + i = 0; + while (i < nmemb * size) + { + ((unsigned char *)ptr)[i] = 0; + i++; + } + return (ptr); +} diff --git a/super_libft/srcs/ft_free.c b/super_libft/srcs/ft_free.c new file mode 100644 index 0000000..ad15bb3 --- /dev/null +++ b/super_libft/srcs/ft_free.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 17:41:19 by grebrune #+# #+# */ +/* Updated: 2024/06/26 17:44:02 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_free(void *ptr) +{ + if (!ptr) + return ; + free(ptr); + ptr = NULL; +} diff --git a/super_libft/srcs/ft_isalnum.c b/super_libft/srcs/ft_isalnum.c new file mode 100644 index 0000000..eb36ec8 --- /dev/null +++ b/super_libft/srcs/ft_isalnum.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:04:27 by beroy #+# #+# */ +/* Updated: 2023/11/06 15:34:45 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalnum(int c) +{ + if (c < 48 + || (c > 57 && c < 65) + || (c > 90 && c < 97) + || c > 122) + return (0); + return (1); +} diff --git a/super_libft/srcs/ft_isalpha.c b/super_libft/srcs/ft_isalpha.c new file mode 100644 index 0000000..76f047d --- /dev/null +++ b/super_libft/srcs/ft_isalpha.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:32:56 by beroy #+# #+# */ +/* Updated: 2023/11/06 15:30:56 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c >= 65 && c <= 90) + || (c >= 97 && c <= 122)) + return (1); + return (0); +} diff --git a/super_libft/srcs/ft_isascii.c b/super_libft/srcs/ft_isascii.c new file mode 100644 index 0000000..aa24c27 --- /dev/null +++ b/super_libft/srcs/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:08:35 by beroy #+# #+# */ +/* Updated: 2023/11/06 15:35:46 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c < 0 || c > 127) + return (0); + return (1); +} diff --git a/super_libft/srcs/ft_isdigit.c b/super_libft/srcs/ft_isdigit.c new file mode 100644 index 0000000..35121c4 --- /dev/null +++ b/super_libft/srcs/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:50:41 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:56:47 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c < 48 || c > 57) + return (0); + return (1); +} diff --git a/super_libft/srcs/ft_isprint.c b/super_libft/srcs/ft_isprint.c new file mode 100644 index 0000000..035965e --- /dev/null +++ b/super_libft/srcs/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:18:22 by beroy #+# #+# */ +/* Updated: 2023/11/09 10:48:13 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c < 32 || c >= 127) + return (0); + return (1); +} diff --git a/super_libft/srcs/ft_itoa.c b/super_libft/srcs/ft_itoa.c new file mode 100644 index 0000000..7538f3e --- /dev/null +++ b/super_libft/srcs/ft_itoa.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 15:11:45 by beroy #+# #+# */ +/* Updated: 2023/11/08 15:48:57 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +static void ft_strfill(long int n, char *str, long int *i) +{ + if (n < 0) + { + str[*i] = '-'; + *i += 1; + ft_strfill(n * -1, str, i); + } + else if (n > 9) + { + ft_strfill(n / 10, str, i); + ft_strfill(n % 10, str, i); + } + else + { + str[*i] = n + 48; + *i += 1; + } +} + +static void ft_strsize(long int n, long int *size) +{ + if (n < 0) + { + *size += 1; + ft_strsize(n * -1, size); + } + else if (n > 9) + { + ft_strsize(n / 10, size); + ft_strsize(n % 10, size); + } + else + *size += 1; +} + +char *ft_itoa(int n) +{ + long int size; + long int i; + char *str; + + size = 0; + i = 0; + ft_strsize(n, &size); + str = malloc(sizeof(char) * (size + 1)); + if (str == NULL) + return (str); + ft_strfill(n, str, &i); + str[i] = 0; + return (str); +} diff --git a/super_libft/srcs/ft_lstadd_back.c b/super_libft/srcs/ft_lstadd_back.c new file mode 100644 index 0000000..1cff03d --- /dev/null +++ b/super_libft/srcs/ft_lstadd_back.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 13:00:56 by beroy #+# #+# */ +/* Updated: 2023/11/14 11:20:43 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *last; + + if (lst) + { + if (*lst == NULL) + *lst = new; + else + { + last = ft_lstlast(*lst); + last->next = new; + } + } +} diff --git a/super_libft/srcs/ft_lstadd_front.c b/super_libft/srcs/ft_lstadd_front.c new file mode 100644 index 0000000..0942a37 --- /dev/null +++ b/super_libft/srcs/ft_lstadd_front.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 10:50:44 by sweek #+# #+# */ +/* Updated: 2023/11/14 11:01:48 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (lst && new) + { + new->next = *lst; + *lst = new; + } +} diff --git a/super_libft/srcs/ft_lstclear.c b/super_libft/srcs/ft_lstclear.c new file mode 100644 index 0000000..7ac527a --- /dev/null +++ b/super_libft/srcs/ft_lstclear.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/14 12:07:11 by beroy #+# #+# */ +/* Updated: 2023/11/14 13:40:20 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (!lst || !del) + return ; + tmp = *lst; + while (*lst) + { + tmp = tmp->next; + ft_lstdelone(*lst, del); + *lst = tmp; + } +} diff --git a/super_libft/srcs/ft_lstdelone.c b/super_libft/srcs/ft_lstdelone.c new file mode 100644 index 0000000..c071719 --- /dev/null +++ b/super_libft/srcs/ft_lstdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 13:43:17 by beroy #+# #+# */ +/* Updated: 2023/11/14 11:57:23 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (!lst || !del) + return ; + del(lst->content); + free(lst); +} diff --git a/super_libft/srcs/ft_lstiter.c b/super_libft/srcs/ft_lstiter.c new file mode 100644 index 0000000..f4d807a --- /dev/null +++ b/super_libft/srcs/ft_lstiter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/14 14:03:02 by beroy #+# #+# */ +/* Updated: 2023/11/14 14:12:47 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + t_list *tmp; + + if (!lst || !f) + return ; + tmp = lst; + while (tmp) + { + f(tmp->content); + tmp = tmp->next; + } +} diff --git a/super_libft/srcs/ft_lstlast.c b/super_libft/srcs/ft_lstlast.c new file mode 100644 index 0000000..29225f6 --- /dev/null +++ b/super_libft/srcs/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 11:40:56 by beroy #+# #+# */ +/* Updated: 2023/11/13 16:23:13 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/super_libft/srcs/ft_lstmap.c b/super_libft/srcs/ft_lstmap.c new file mode 100644 index 0000000..f4f7329 --- /dev/null +++ b/super_libft/srcs/ft_lstmap.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/14 14:14:01 by beroy #+# #+# */ +/* Updated: 2023/11/14 15:06:13 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new; + t_list *head; + void *tmp; + + head = NULL; + if (!lst || !f || !del) + return (NULL); + while (lst) + { + tmp = f(lst->content); + new = ft_lstnew(tmp); + if (new == NULL) + { + del(tmp); + ft_lstclear(&head, del); + return (head); + } + ft_lstadd_back(&head, new); + lst = lst->next; + } + return (head); +} diff --git a/super_libft/srcs/ft_lstnew.c b/super_libft/srcs/ft_lstnew.c new file mode 100644 index 0000000..89cbd7b --- /dev/null +++ b/super_libft/srcs/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/10 14:28:15 by sweek #+# #+# */ +/* Updated: 2023/11/13 10:55:18 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *s_new; + + s_new = malloc(sizeof(t_list)); + if (s_new == NULL) + return (s_new); + s_new->content = content; + s_new->next = NULL; + return (s_new); +} diff --git a/super_libft/srcs/ft_lstsize.c b/super_libft/srcs/ft_lstsize.c new file mode 100644 index 0000000..f0ea37d --- /dev/null +++ b/super_libft/srcs/ft_lstsize.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 11:16:51 by beroy #+# #+# */ +/* Updated: 2023/11/13 11:23:54 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + if (!lst) + return (0); + i = 1; + while (lst->next != NULL) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/super_libft/srcs/ft_memchr.c b/super_libft/srcs/ft_memchr.c new file mode 100644 index 0000000..36efe9a --- /dev/null +++ b/super_libft/srcs/ft_memchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:00:25 by beroy #+# #+# */ +/* Updated: 2023/11/09 13:43:15 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (((unsigned char *)s)[i] == (unsigned char)c) + return ((unsigned char *)s + i); + i++; + } + return (NULL); +} diff --git a/super_libft/srcs/ft_memcmp.c b/super_libft/srcs/ft_memcmp.c new file mode 100644 index 0000000..7234ae4 --- /dev/null +++ b/super_libft/srcs/ft_memcmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 17:04:40 by beroy #+# #+# */ +/* Updated: 2023/11/07 18:01:41 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/super_libft/srcs/ft_memcpy.c b/super_libft/srcs/ft_memcpy.c new file mode 100644 index 0000000..59c3c07 --- /dev/null +++ b/super_libft/srcs/ft_memcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:55:10 by beroy #+# #+# */ +/* Updated: 2023/11/07 14:20:12 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + if (!dest && !src) + return (dest); + i = 0; + while (i < n) + { + ((char *) dest)[i] = ((char *)src)[i]; + i++; + } + return (dest); +} diff --git a/super_libft/srcs/ft_memmove.c b/super_libft/srcs/ft_memmove.c new file mode 100644 index 0000000..df9fb00 --- /dev/null +++ b/super_libft/srcs/ft_memmove.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:57:04 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:57:05 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + if (!dest && !src) + return (dest); + if (src < dest) + while (n-- > 0) + ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; + else + ft_memcpy(dest, src, n); + return (dest); +} diff --git a/super_libft/srcs/ft_memset.c b/super_libft/srcs/ft_memset.c new file mode 100644 index 0000000..21fd021 --- /dev/null +++ b/super_libft/srcs/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:44:54 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:57:02 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + size_t i; + char *temp; + + temp = (char *)s; + i = 0; + while (i < n) + { + temp[i] = c; + i++; + } + return (temp); +} diff --git a/super_libft/srcs/ft_printf.c b/super_libft/srcs/ft_printf.c new file mode 100644 index 0000000..0ee48c4 --- /dev/null +++ b/super_libft/srcs/ft_printf.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/20 19:03:15 by beroy #+# #+# */ +/* Updated: 2024/06/12 15:51:10 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_printmemory(size_t arg, ssize_t *length) +{ + if ((char *)arg == NULL) + { + *length = ft_putstr("(nil)"); + return ; + } + *length = ft_putstr("0x"); + if (*length == -1) + return ; + ft_putnbr_ul(arg, "0123456789abcdef", length); +} + +void ft_checkarg(const char c, va_list arg, ssize_t *length) +{ + ssize_t tmp; + + tmp = 0; + if (c == 'c') + tmp = ft_putchar((unsigned char)va_arg(arg, int)); + else if (c == 's') + tmp = ft_putstr(va_arg(arg, char *)); + else if (c == 'p') + ft_printmemory((size_t)va_arg(arg, size_t), &tmp); + else if (c == 'd' || c == 'i') + ft_putnbr_base(va_arg(arg, int), "0123456789", &tmp); + else if (c == 'u') + ft_putnbr_base(va_arg(arg, unsigned int), "0123456789", &tmp); + else if (c == 'x') + ft_putnbr_base(va_arg(arg, unsigned int), "0123456789abcdef", &tmp); + else if (c == 'X') + ft_putnbr_base(va_arg(arg, unsigned int), "0123456789ABCDEF", &tmp); + else if (c == '%') + tmp += ft_putchar('%'); + else if (c == 0) + tmp = -1; + if (tmp == -1) + *length = tmp; + else + *length += tmp; +} + +int ft_printf(const char *str, ...) +{ + size_t i; + va_list args; + ssize_t length; + + if (!str) + return (-1); + i = 0; + length = 0; + va_start(args, str); + while (str[i]) + { + if (str[i] != '%') + { + if (ft_putchar(str[i]) == -1) + return (va_end(args), -1); + length++; + } + else + ft_checkarg(str[++i], args, &length); + if (length == -1) + break ; + i++; + } + return (va_end(args), length); +} diff --git a/super_libft/srcs/ft_printf_utils.c b/super_libft/srcs/ft_printf_utils.c new file mode 100644 index 0000000..75b2da9 --- /dev/null +++ b/super_libft/srcs/ft_printf_utils.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/20 19:18:35 by beroy #+# #+# */ +/* Updated: 2024/01/17 16:20:00 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +ssize_t ft_putchar(int c) +{ + return (write(1, &c, 1)); +} + +ssize_t ft_putstr(char *str) +{ + ssize_t i; + ssize_t j; + + i = 0; + if (!str) + return (ft_putstr("(null)")); + while (str[i]) + { + j = ft_putchar(str[i]); + if (j == -1) + return (-1); + i += j; + } + return (i); +} + +void ft_putnbr_base(ssize_t nbr, char *str, ssize_t *length) +{ + ssize_t i; + + if (nbr < 0) + { + i = ft_putchar('-'); + if (i == -1) + return (*length = -1, (void)0); + *length += i; + ft_putnbr_base(nbr * -1, str, length); + if (*length == -1) + return (*length = -1, (void)0); + } + else + { + if (nbr >= (ssize_t)ft_strlen(str)) + { + ft_putnbr_base(nbr / ft_strlen(str), str, length); + if (*length == -1) + return (*length = -1, (void)0); + } + i = ft_putchar(str[nbr % ft_strlen(str)]); + if (i == -1) + return (*length = -1, (void)0); + *length += i; + } +} + +void ft_putnbr_ul(size_t nbr, char *str, ssize_t *length) +{ + ssize_t i; + + if (nbr >= ft_strlen(str)) + { + ft_putnbr_ul(nbr / ft_strlen(str), str, length); + if (*length == -1) + return (*length = -1, (void)0); + } + i = ft_putchar(str[nbr % ft_strlen(str)]); + if (i == -1) + return (*length = -1, (void)0); + *length += i; +} diff --git a/super_libft/srcs/ft_putchar_fd.c b/super_libft/srcs/ft_putchar_fd.c new file mode 100644 index 0000000..18c7c4d --- /dev/null +++ b/super_libft/srcs/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 16:50:12 by beroy #+# #+# */ +/* Updated: 2023/11/08 16:50:12 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/super_libft/srcs/ft_putendl_fd.c b/super_libft/srcs/ft_putendl_fd.c new file mode 100644 index 0000000..da271e1 --- /dev/null +++ b/super_libft/srcs/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 16:54:34 by beroy #+# #+# */ +/* Updated: 2023/11/08 16:59:54 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/super_libft/srcs/ft_putnbr_fd.c b/super_libft/srcs/ft_putnbr_fd.c new file mode 100644 index 0000000..d73c5be --- /dev/null +++ b/super_libft/srcs/ft_putnbr_fd.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 17:01:09 by beroy #+# #+# */ +/* Updated: 2023/11/08 17:14:53 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + write(fd, "-2147483648", 11); + else + { + if (n < 0) + { + n *= -1; + ft_putchar_fd('-', fd); + } + if (n > 9) + { + ft_putnbr_fd(n / 10, fd); + ft_putnbr_fd(n % 10, fd); + } + else + ft_putchar_fd(n + 48, fd); + } +} diff --git a/super_libft/srcs/ft_putstr_fd.c b/super_libft/srcs/ft_putstr_fd.c new file mode 100644 index 0000000..1ce9095 --- /dev/null +++ b/super_libft/srcs/ft_putstr_fd.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 16:52:10 by beroy #+# #+# */ +/* Updated: 2023/11/08 16:52:10 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_putstr_fd(char *str, int fd) +{ + size_t i; + + i = 0; + while (str[i]) + { + ft_putchar_fd(str[i], fd); + i++; + } +} diff --git a/super_libft/srcs/ft_split.c b/super_libft/srcs/ft_split.c new file mode 100644 index 0000000..1e34fb1 --- /dev/null +++ b/super_libft/srcs/ft_split.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 12:27:55 by beroy #+# #+# */ +/* Updated: 2024/06/27 17:38:42 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +static size_t ft_countwords(char const *s, char c) +{ + size_t i; + size_t words; + + words = 0; + i = 0; + if (s[i] != c && s[i] != 0) + words++; + i++; + while (s[0] && s[i]) + { + if (s[i] != c && s[i - 1] == c) + words++; + i++; + } + return (words); +} + +static size_t ft_wdlen(char const *s, char c, size_t j) +{ + size_t i; + + i = j; + while (s[i] != c && s[i] != 0) + i++; + return (i); +} + +static char *ft_superdup(char const *s, char c, size_t *j) +{ + char *str; + size_t len; + size_t i; + + len = ft_wdlen(s, c, *j); + str = ft_calloc(len - *j + 1, sizeof(char)); + if (str == NULL) + return (str); + i = 0; + while (*j < len) + { + str[i] = s[*j]; + *j += 1; + i++; + } + str[i] = 0; + return (str); +} + +void *ft_splitdestroy(char **split) +{ + size_t i; + + i = 0; + while (split[i] != NULL) + { + ft_free(split[i]); + i++; + } + if (split != NULL) + ft_free(split); + return (NULL); +} + +char **ft_split(char const *s, char c) +{ + size_t i; + size_t j; + size_t words; + char **split; + + i = 0; + j = 0; + words = ft_countwords(s, c); + split = ft_calloc((words + 1), sizeof(char *)); + if (!s[0] || split == NULL) + return (split); + while (i < words) + { + if (s[j] != c) + { + split[i++] = ft_superdup(s, c, &j); + if (split[i - 1] == NULL) + return (ft_splitdestroy(split)); + } + else + j++; + } + return (split); +} diff --git a/super_libft/srcs/ft_strchr.c b/super_libft/srcs/ft_strchr.c new file mode 100644 index 0000000..f61a209 --- /dev/null +++ b/super_libft/srcs/ft_strchr.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 14:43:35 by beroy #+# #+# */ +/* Updated: 2023/11/09 11:29:11 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strchr(const char *s, int c) +{ + int i; + + i = 0; + while (s[i] != (unsigned char)c && s[i]) + i++; + if (s[i] == 0 && (unsigned char)c != 0) + return (NULL); + return (((char *)s) + i); +} diff --git a/super_libft/srcs/ft_strcmp.c b/super_libft/srcs/ft_strcmp.c new file mode 100644 index 0000000..ac395c9 --- /dev/null +++ b/super_libft/srcs/ft_strcmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 15:26:26 by beroy #+# #+# */ +/* Updated: 2023/11/07 15:31:01 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_strcmp(char *s1, const char *s2) +{ + size_t i; + + i = 0; + while (s1[i] != 0 || s2[i] != 0) + { + if (s1[i] != s2[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/super_libft/srcs/ft_strdup.c b/super_libft/srcs/ft_strdup.c new file mode 100644 index 0000000..67d9fbf --- /dev/null +++ b/super_libft/srcs/ft_strdup.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 10:40:49 by beroy #+# #+# */ +/* Updated: 2023/11/08 11:50:04 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strdup(const char *s) +{ + size_t size; + size_t i; + char *ptr; + + size = ft_strlen((char *)s); + ptr = ft_calloc(size + 1, sizeof(char)); + if (ptr == NULL) + return (ptr); + i = 0; + while (i < size) + { + ptr[i] = s[i]; + i++; + } + ptr[i] = 0; + return (ptr); +} diff --git a/super_libft/srcs/ft_striteri.c b/super_libft/srcs/ft_striteri.c new file mode 100644 index 0000000..e517c7d --- /dev/null +++ b/super_libft/srcs/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 16:45:22 by beroy #+# #+# */ +/* Updated: 2023/11/08 16:45:40 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + i = 0; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/super_libft/srcs/ft_strjoin.c b/super_libft/srcs/ft_strjoin.c new file mode 100644 index 0000000..d1f482e --- /dev/null +++ b/super_libft/srcs/ft_strjoin.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sweek +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 11:40:41 by beroy #+# #+# */ +/* Updated: 2023/11/12 14:37:57 by sweek ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t len; + size_t i; + size_t j; + char *str; + + len = ft_strlen((char *)s1) + ft_strlen((char *)s2); + i = 0; + str = malloc (sizeof(char) * (len + 1)); + if (str == NULL) + return (str); + while (s1[i]) + { + str[i] = s1[i]; + i++; + } + j = 0; + while (s2[j]) + { + str[i + j] = s2[j]; + j++; + } + str [i + j] = 0; + return (str); +} diff --git a/super_libft/srcs/ft_strlcat.c b/super_libft/srcs/ft_strlcat.c new file mode 100644 index 0000000..95564d8 --- /dev/null +++ b/super_libft/srcs/ft_strlcat.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 14:30:06 by beroy #+# #+# */ +/* Updated: 2023/11/07 14:30:06 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +size_t ft_strlcat(char *dest, char *src, unsigned int size) +{ + unsigned int isrc; + unsigned int idest; + unsigned int destlen; + unsigned int srclen; + + isrc = 0; + destlen = ft_strlen(dest); + srclen = ft_strlen(src); + idest = destlen; + if (size == 0) + return (srclen); + while (src[isrc] && idest < size - 1) + { + dest[idest] = src[isrc]; + idest++; + isrc++; + } + dest[idest] = 0; + if (size < idest) + return (srclen + size); + return (destlen + srclen); +} diff --git a/super_libft/srcs/ft_strlcpy.c b/super_libft/srcs/ft_strlcpy.c new file mode 100644 index 0000000..56e044e --- /dev/null +++ b/super_libft/srcs/ft_strlcpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 15:32:21 by beroy #+# #+# */ +/* Updated: 2023/11/07 15:42:27 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + if (size != 0) + { + while (src[i] != '\0' && i < size - 1) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + } + i = 0; + while (src[i] != '\0') + i++; + return (i); +} diff --git a/super_libft/srcs/ft_strlen.c b/super_libft/srcs/ft_strlen.c new file mode 100644 index 0000000..e81aa89 --- /dev/null +++ b/super_libft/srcs/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 15:37:08 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:56:56 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +size_t ft_strlen(const char *str) +{ + size_t i; + + i = 0; + while (str && str[i]) + i++; + return (i); +} diff --git a/super_libft/srcs/ft_strmapi.c b/super_libft/srcs/ft_strmapi.c new file mode 100644 index 0000000..dbe6cd0 --- /dev/null +++ b/super_libft/srcs/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 16:14:27 by beroy #+# #+# */ +/* Updated: 2023/11/08 16:32:36 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t len; + size_t i; + char *str; + + len = ft_strlen((char *)s); + i = 0; + str = malloc(sizeof(char) * (len + 1)); + if (str == NULL) + return (str); + while (s[i]) + { + str[i] = f(i, s[i]); + i++; + } + str[i] = 0; + return (str); +} diff --git a/super_libft/srcs/ft_strncmp.c b/super_libft/srcs/ft_strncmp.c new file mode 100644 index 0000000..f23b1b7 --- /dev/null +++ b/super_libft/srcs/ft_strncmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 15:26:26 by beroy #+# #+# */ +/* Updated: 2024/05/13 16:09:49 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_strncmp(char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while ((s1[i] != 0 || s2[i] != 0) && (i < n)) + { + if (s1[i] != s2[i]) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/super_libft/srcs/ft_strnstr.c b/super_libft/srcs/ft_strnstr.c new file mode 100644 index 0000000..000a0ca --- /dev/null +++ b/super_libft/srcs/ft_strnstr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 17:56:43 by beroy #+# #+# */ +/* Updated: 2023/11/09 14:57:18 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t lilen; + + if (!little[0]) + return ((char *)big); + if (len == 0) + return (NULL); + i = 0; + lilen = ft_strlen((char *)little); + while (big[i] && i < len) + { + if (i + lilen > len) + return (NULL); + if (ft_strncmp((char *)big + i, little, lilen) == 0) + return ((char *)big + i); + i++; + } + return (NULL); +} diff --git a/super_libft/srcs/ft_strrchr.c b/super_libft/srcs/ft_strrchr.c new file mode 100644 index 0000000..77ce986 --- /dev/null +++ b/super_libft/srcs/ft_strrchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 15:21:42 by beroy #+# #+# */ +/* Updated: 2023/11/09 12:29:56 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + if ((unsigned char)c == 0) + return ((char *)s + ft_strlen((char *)s)); + i = ft_strlen((char *)s); + if (i == 0) + return (NULL); + i -= 1; + while (s[i] != (unsigned char)c && i) + i--; + if (i == 0 && s[0] != (unsigned char)c) + return (NULL); + return ((char *)s + i); +} diff --git a/super_libft/srcs/ft_strtrim.c b/super_libft/srcs/ft_strtrim.c new file mode 100644 index 0000000..8d55417 --- /dev/null +++ b/super_libft/srcs/ft_strtrim.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 11:50:37 by beroy #+# #+# */ +/* Updated: 2023/11/09 16:42:46 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +static size_t ft_ischarset(char c, char const *set) +{ + size_t i; + size_t chk; + + i = 0; + chk = 0; + while (set[i]) + { + if (set[i] == c) + chk++; + i++; + } + if (chk == 0) + return (0); + return (1); +} + +static char *ft_strldup(char *s1, size_t len) +{ + size_t i; + char *str; + + i = 0; + str = malloc (sizeof(char) * (len + 1)); + if (str == NULL) + return (str); + while (i < len) + { + str[i] = s1[i]; + i++; + } + str[i] = 0; + return (str); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t istart; + size_t iend; + char *str; + + istart = 0; + iend = ft_strlen((char *)s1); + while (s1[istart]) + { + if (ft_ischarset(s1[istart], set) == 0) + break ; + istart++; + } + while (iend > istart) + { + if (ft_ischarset(s1[iend - 1], set) == 0) + break ; + iend--; + } + str = ft_strldup((char *)s1 + istart, iend - istart); + return (str); +} diff --git a/super_libft/srcs/ft_substr.c b/super_libft/srcs/ft_substr.c new file mode 100644 index 0000000..60c600c --- /dev/null +++ b/super_libft/srcs/ft_substr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 11:21:38 by beroy #+# #+# */ +/* Updated: 2023/11/14 13:59:57 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + size_t j; + char *str; + + i = 0; + while (((unsigned char *)s)[i] && i != start) + i++; + if (len > ft_strlen((char *)s) - i) + len = ft_strlen((char *)s) - i; + str = ft_calloc(len + 1, sizeof(char)); + if (str == NULL) + return (str); + j = 0; + while (j < len) + { + str[j] = s[i]; + i++; + j++; + } + str[j] = 0; + return (str); +} diff --git a/super_libft/srcs/ft_tolower.c b/super_libft/srcs/ft_tolower.c new file mode 100644 index 0000000..1526c43 --- /dev/null +++ b/super_libft/srcs/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 13:56:50 by beroy #+# #+# */ +/* Updated: 2023/11/07 15:43:01 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + return (c + 32); + return (c); +} diff --git a/super_libft/srcs/ft_toupper.c b/super_libft/srcs/ft_toupper.c new file mode 100644 index 0000000..5e032cc --- /dev/null +++ b/super_libft/srcs/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 13:56:50 by beroy #+# #+# */ +/* Updated: 2023/11/07 13:56:50 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + return (c - 32); + return (c); +} diff --git a/super_libft/srcs/get_next_line.c b/super_libft/srcs/get_next_line.c new file mode 100644 index 0000000..e4e8a95 --- /dev/null +++ b/super_libft/srcs/get_next_line.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/20 11:34:48 by beroy #+# #+# */ +/* Updated: 2024/01/17 16:18:34 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_bzero_nl(char *str) +{ + size_t i; + + i = 0; + while (str[i] != 0) + { + str[i] = 0; + i++; + } +} + +char *ft_line(int fd, int rd, char *line, char bf[OPEN_MAX][BUFFER_SIZE + 1]) +{ + while (rd && ft_linecheck(line) == 0) + { + rd = read(fd, bf[fd], BUFFER_SIZE); + if (rd < 0) + return (ft_bzero_nl(bf[fd]), free(line), NULL); + bf[fd][rd] = 0; + line = ft_strjoin_nl(line, bf[fd]); + if (line == NULL) + return (NULL); + } + return (line); +} + +char *get_next_line(int fd) +{ + static char buff[OPEN_MAX][BUFFER_SIZE + 1]; + char *line; + int readed; + + readed = 1; + if (fd < 0) + return (NULL); + if (read(fd, NULL, 0) < 0) + return (ft_bzero_nl(buff[fd]), NULL); + line = ft_strdup_nl(buff[fd]); + if (line == NULL) + return (NULL); + line = ft_line(fd, readed, line, buff); + if (line == NULL) + return (NULL); + ft_buffclean(buff[fd]); + if (line[0] == 0) + return (free(line), NULL); + return (line); +} diff --git a/super_libft/srcs/get_next_line_utils.c b/super_libft/srcs/get_next_line_utils.c new file mode 100644 index 0000000..415fc43 --- /dev/null +++ b/super_libft/srcs/get_next_line_utils.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: beroy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/20 12:01:53 by beroy #+# #+# */ +/* Updated: 2024/01/17 16:17:27 by beroy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strdup_nl(char *str) +{ + size_t i; + size_t len; + char *dup; + + len = 0; + while (str[len] != '\n' && str[len] != 0) + len++; + if (str[len] == '\n') + len++; + dup = malloc(sizeof(char) * (len + 1)); + if (dup == NULL) + return (dup); + i = 0; + while (i < len) + { + dup[i] = str[i]; + i++; + } + dup[i] = 0; + return (dup); +} + +int ft_linecheck(char *str) +{ + size_t i; + + i = 0; + if (ft_strlen(str + i) == 0) + return (0); + while (str[i]) + { + if (str[i] == '\n') + return (1); + i++; + } + return (0); +} + +char *ft_strjoin_nl(char *s1, char *s2) +{ + size_t i; + size_t j; + size_t len; + char *join; + + len = 0; + while (s2[len] != '\n' && s2[len] != 0) + len++; + if (s2[len] == '\n') + len++; + join = malloc(sizeof(char) * (ft_strlen(s1) + len + 1)); + if (join == NULL) + return (free(s1), NULL); + i = 0; + j = 0; + while (s1 && s1[i]) + { + join[i] = s1[i]; + i++; + } + while (s2 && j++ < len) + join[i + j - 1] = s2[j - 1]; + join[i + j - 1] = 0; + free (s1); + return (join); +} + +void ft_buffclean(char *str) +{ + size_t i; + size_t j; + + i = 0; + while (str[i] != '\n' && str[i] != 0) + i++; + if (str[i] == '\n') + i++; + j = 0; + while (str[i + j]) + { + str[j] = str[i + j]; + j++; + } + str[j] = 0; +} From 873f255ce5fedffa18559fd4da7e52b090bfac1a Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 1 Jul 2024 17:55:22 +0200 Subject: [PATCH 15/86] value of name copy --- exec/built_pwd.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exec/built_pwd.c b/exec/built_pwd.c index fb6a1f9..a3598b6 100644 --- a/exec/built_pwd.c +++ b/exec/built_pwd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ -/* Updated: 2024/06/28 16:58:52 by grebrune ### ########.fr */ +/* Updated: 2024/07/01 17:53:49 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,11 +55,11 @@ char *value_of_name(t_env *env, char *name) t_env *copy; copy = env; - while (env) + while (copy) { - if (0 == ft_strcmp(env->name, name)) - return (env->value); - env = env->next; + if (0 == ft_strcmp(copy->name, name)) + return (copy->value); + copy = copy->next; } return (NULL); } From c13359001e65e610ccba19773744657a14230f75 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 1 Jul 2024 18:01:03 +0200 Subject: [PATCH 16/86] leaks of readline --- exec/heredoc.c | 2 +- srcs/main.c | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index a45e022..ca1097d 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -49,7 +49,7 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) { str = readline(">> "); if (g_error == 130 || !str) - return (0); + return (free(str), 0); if (ft_strcmp(str, eof) == 0) { free(str); diff --git a/srcs/main.c b/srcs/main.c index 0e8f3e1..b9e061d 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/06/28 15:55:25 by grebrune ### ########.fr */ +/* Updated: 2024/07/01 17:56:19 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,11 +37,17 @@ void main_loop(t_head *head) sig_main(head, 0); input = readline("> "); if (input == NULL) + { + free(input); break ; + } add_history(input); sig_main(head, 3); if (str_empty(input) == 1) + { + free(input); continue ; + } if (ft_parse(input, head) == 0) { if (heredoc(head)) @@ -50,6 +56,7 @@ void main_loop(t_head *head) } if (head->cmd != NULL) ft_free_cmd(&(head->cmd)); + free(input); } write(1, "exit\n", 5); } From 05589596ea38a79ecc77195f75ce6580b355848b Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 14:33:22 +0200 Subject: [PATCH 17/86] ctrl C remake --- exec/heredoc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index ca1097d..fb4d25f 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ -/* Updated: 2024/06/28 16:38:01 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:28:29 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,8 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) while (42) { str = readline(">> "); - if (g_error == 130 || !str) + //replace strstr by ft_strstr + if (!str || strstr("^C", str) != NULL) return (free(str), 0); if (ft_strcmp(str, eof) == 0) { From 1710607d9f892502ccd56851eb65f8e48f3645c6 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 14:33:52 +0200 Subject: [PATCH 18/86] sig heredoc remake --- exec/signals_main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exec/signals_main.c b/exec/signals_main.c index 01993b7..53033b7 100644 --- a/exec/signals_main.c +++ b/exec/signals_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 17:53:49 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:06:17 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:06:28 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ void sig_child(int sig) if (sig == SIGINT) { g_error = 130; - write(1, "\n", 1); + write(2, "\n", 1); } if (sig == SIGQUIT) { @@ -51,8 +51,10 @@ void sig_heredoc(int sig) { g_error = 130; ioctl(STDIN_FILENO, TIOCSTI, "\n"); - rl_replace_line("", 0); - rl_redisplay(); +// write(2, "\n", 1); +// rl_replace_line("", 0); +// rl_on_new_line(); +// rl_redisplay(); } } From 98942390f436fa1c972b58473945c776e5ecb378 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 14:40:48 +0200 Subject: [PATCH 19/86] exit if crash of malloc --- exec/exec_redir.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exec/exec_redir.c b/exec/exec_redir.c index a6c0e5c..8a258cf 100644 --- a/exec/exec_redir.c +++ b/exec/exec_redir.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:44:44 by grebrune #+# #+# */ -/* Updated: 2024/06/28 14:05:23 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:36:20 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,8 @@ void fail_malloc(t_head *head, int **fd, size_t i) free(head->fnp->pid); free(head->fnp->pipe); free(head->fnp); + ft_free_all(head); + exit(0); } int open_the_pipe(int **pipe, t_head *head) From 45927cd7b2c3991579959eb52a271cc35bfb86ba Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 14:43:43 +0200 Subject: [PATCH 20/86] add free_malloc_fnp if crash of malloc --- includes/builtins.h | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/builtins.h b/includes/builtins.h index 54e3324..6cbdc2e 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -95,6 +95,7 @@ char *find_path(t_head *head); char *join_with_char(char const *s1, char const *s2, char c); char **make_env(t_env *env); void no_path_to_hapiness(t_head *head, char **env, char **tab); +void free_malloc_fnp(t_head *head); //exec_wait void wait_for_all(pid_t *pid, int x); From 499069ca7967b2a6ed92820ea2237ce15178b423 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 14:50:10 +0200 Subject: [PATCH 21/86] no leaks exit if crash --- exec/exec_redir_utils.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exec/exec_redir_utils.c b/exec/exec_redir_utils.c index 69a4860..e8ff947 100644 --- a/exec/exec_redir_utils.c +++ b/exec/exec_redir_utils.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/12 13:57:10 by grebrune #+# #+# */ -/* Updated: 2024/06/14 16:44:09 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:48:46 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,12 +85,12 @@ int malloc_fnp(t_head *head) { head->fnp = malloc(sizeof(t_fnp)); if (!head->fnp) - return (ft_free_all(head), exit(1), 1); + return (free_malloc_fnp(head, 0), 1); head->fnp->pid = malloc(sizeof(int) * cmdlen(head->cmd)); if (!head->fnp->pid) - return (ft_free_all(head), exit(1), 1); + return (free_malloc_fnp(head, 1), 1); head->fnp->pipe = malloc(sizeof(int *) * cmdlen(head->cmd)); - if (!head->fnp->pid) - return (ft_free_all(head), exit(1), 1); + if (!head->fnp->pipe) + return (free_malloc_fnp(head, 2), 1); return (0); } From 0836183b570834a36bcd79041d4a5381b4480a86 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:01:32 +0200 Subject: [PATCH 22/86] free_malloc_fnp --- exec/exec_utils.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/exec/exec_utils.c b/exec/exec_utils.c index 111322a..4053fee 100644 --- a/exec/exec_utils.c +++ b/exec/exec_utils.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 17:31:24 by grebrune #+# #+# */ -/* Updated: 2024/06/26 21:44:03 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:43:43 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,3 +95,13 @@ void no_path_to_hapiness(t_head *head, char **env, char **tab) ft_free_all(head); exit (127); } + +void free_malloc_fnp(t_head *head, int to_free) +{ + if (to_free > 1) + free(head->fnp->pid); + if (to_free > 0) + free(head->fnp); + ft_free_all(head); + exit (0); +} From dacf9d398de70b35d18dbac64b8bca05db8a29d9 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:05:42 +0200 Subject: [PATCH 23/86] return g_error commented header for tester --- srcs/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/srcs/main.c b/srcs/main.c index b9e061d..6c0ba59 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/07/01 17:56:19 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 13:02:02 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ int main(int ac, char **av, char **env) (void)ac; (void)av; - ft_header(); +// ft_header(); head = head_init(env); if (head == NULL) return (0); @@ -75,5 +75,5 @@ int main(int ac, char **av, char **env) main_loop(head); if (ac == 2) single_exec(head, av[1]); - return (ft_free_all(head), 0); + return (ft_free_all(head), g_error); } From e76042711108a5f938332c8f62223964336522d0 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:06:05 +0200 Subject: [PATCH 24/86] mod free_malloc_fnp --- includes/builtins.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index 6cbdc2e..3dd2457 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:58:23 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:43:43 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -95,8 +95,7 @@ char *find_path(t_head *head); char *join_with_char(char const *s1, char const *s2, char c); char **make_env(t_env *env); void no_path_to_hapiness(t_head *head, char **env, char **tab); -void free_malloc_fnp(t_head *head); - +void free_malloc_fnp(t_head *head, int to_free); //exec_wait void wait_for_all(pid_t *pid, int x); size_t envlen(t_env *base); From 2f004c23c8ae671151ff4562955735c5bc8196ff Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:06:15 +0200 Subject: [PATCH 25/86] rem comments --- exec/signals_main.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/exec/signals_main.c b/exec/signals_main.c index 53033b7..ccf2144 100644 --- a/exec/signals_main.c +++ b/exec/signals_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 17:53:49 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:06:28 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:06:43 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,10 +51,6 @@ void sig_heredoc(int sig) { g_error = 130; ioctl(STDIN_FILENO, TIOCSTI, "\n"); -// write(2, "\n", 1); -// rl_replace_line("", 0); -// rl_on_new_line(); -// rl_redisplay(); } } From 6e80b148c7e87bb7f604fe0c7a73963bb7f486ff Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:23:01 +0200 Subject: [PATCH 26/86] value of exit of main procs --- exec/exec_wait.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index 634f5e9..cfcc625 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -30,6 +30,8 @@ void wait_for_all(pid_t *pid, int x) } if (WIFEXITED(wstatus[1]) == 0) g_error = 130; + else if (wstatus[1] == 0) + return ; else g_error = WEXITSTATUS(wstatus[1]); } From b15764edf5db02b153c9bd845f4ac0addd3f6b78 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:30:49 +0200 Subject: [PATCH 27/86] cor of exit "" --- exec/built_exit.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/exec/built_exit.c b/exec/built_exit.c index 4d6e0d2..3be1180 100644 --- a/exec/built_exit.c +++ b/exec/built_exit.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 17:28:49 by grebrune #+# #+# */ -/* Updated: 2024/06/21 18:55:20 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 15:24:07 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ void ft_exit(t_head *head) exit_free(head, g_error); return ; } + copy = head->cmd; if (ft_strnum(copy->arg[1])) { @@ -47,6 +48,8 @@ int ft_strnum(char *str) int i; i = 0; + if (str && !str[0]) + return (0); while (str && str[i]) { if (!(str[i] >= 48 && str[i] <= 57)) From 778198842964cb860adddde3d19ef86a5196c3f7 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 15:37:16 +0200 Subject: [PATCH 28/86] exit all heredoc if ctrl C --- exec/heredoc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index fb4d25f..a93320a 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:28:29 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:33:22 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,9 +48,11 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) while (42) { str = readline(">> "); - //replace strstr by ft_strstr - if (!str || strstr("^C", str) != NULL) + if (!str) return (free(str), 0); + //replace strstr by ft_strstr + if (strstr("^C", str) != NULL) + return (free(str), 2); if (ft_strcmp(str, eof) == 0) { free(str); From c00f108b04a49c27244b719ed9bf757e6af81934 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 17:17:33 +0200 Subject: [PATCH 29/86] redir with fd exit proprely --- exec/exec_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_main.c b/exec/exec_main.c index 99da40e..1cf9293 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:09:41 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 17:08:21 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,7 +90,7 @@ int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x) return (0); sig_main(head, 1); if (redir_with_fd(fd, fnp->pipe, copy, x)) - return (2); + return (close_pipe(head, fnp->pipe), exit_free(head, 1), 2); close_pipe(head, fnp->pipe); if (ft_strcmp(copy->arg[0], "echo") == 0) return (ft_echo(head, copy), 1); From 0218768393e1d448df9f8b1d30c783eca139b71f Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 17:40:41 +0200 Subject: [PATCH 30/86] wait for all deals with ctrl \ --- exec/exec_wait.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index cfcc625..f07c3a6 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ -/* Updated: 2024/06/28 13:31:36 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 17:39:14 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,10 @@ void wait_for_all(pid_t *pid, int x) i++; } if (WIFEXITED(wstatus[1]) == 0) - g_error = 130; + { + write(2, "Quit (core dumped)\n", 19); + g_error = 131; + } else if (wstatus[1] == 0) return ; else From 0b351e6d1cdba5b3d0c55279fe8239ff574908ca Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 19:51:30 +0200 Subject: [PATCH 31/86] add exec_error file --- exec/exec_error.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 exec/exec_error.c diff --git a/exec/exec_error.c b/exec/exec_error.c new file mode 100644 index 0000000..a19ebbd --- /dev/null +++ b/exec/exec_error.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/02 19:44:43 by grebrune #+# #+# */ +/* Updated: 2024/07/02 19:46:59 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +void exec_error_exit(char **arg, char *str, char **env, char **path) +{ + perror(arg[0]); + free_tab(env); + free_tab(path); + free_tab(arg); + ft_free(str); + exit (127); +} \ No newline at end of file From 43428d6cfc29924c16072c05bcefa6deed01f6a8 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 20:18:39 +0200 Subject: [PATCH 32/86] add exec_error --- Makefile | 1 + exec/exec_error.c | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 9d1512c..2fe1de8 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ EXEC := built_cd.c \ built_export_checker.c \ built_export_utils.c \ exec_main.c \ + exec_error.c \ exec_utils.c \ built_echo.c \ built_pwd.c \ diff --git a/exec/exec_error.c b/exec/exec_error.c index a19ebbd..05a2cbc 100644 --- a/exec/exec_error.c +++ b/exec/exec_error.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/02 19:44:43 by grebrune #+# #+# */ -/* Updated: 2024/07/02 19:46:59 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 20:12:08 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,5 +19,8 @@ void exec_error_exit(char **arg, char *str, char **env, char **path) free_tab(path); free_tab(arg); ft_free(str); - exit (127); + if (errno == EACCES) + exit (126); + else + exit (127); } \ No newline at end of file From dfbf4cf6067e53563bad796ab1e0f0de6cd1fd75 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 20:18:48 +0200 Subject: [PATCH 33/86] add exec_error --- includes/builtins.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/builtins.h b/includes/builtins.h index 3dd2457..b5c54c8 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:43:43 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 14:48:46 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -84,6 +84,8 @@ char *value_of_name(t_env *env, char *name); void ft_unset(t_head *head); void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)); +//exec_error +void exec_error_exit(char **arg, char *str, char **env, char **path); //exec_main int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x); void there_cmd(char **arg, char *str, char **env); From 94c70c9ecbb77c28b59e90113b2786c41d88052d Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 20:56:42 +0200 Subject: [PATCH 34/86] wait gere les signaux --- exec/exec_wait.c | 13 +++++++------ includes/builtins.h | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index f07c3a6..e47a218 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -6,13 +6,13 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ -/* Updated: 2024/07/02 17:39:14 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 20:52:08 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/builtins.h" -void wait_for_all(pid_t *pid, int x) +int wait_for_all(pid_t *pid, int x) { int i; int wstatus[2]; @@ -28,15 +28,16 @@ void wait_for_all(pid_t *pid, int x) wstatus[1] = wstatus[0]; i++; } - if (WIFEXITED(wstatus[1]) == 0) + if (WIFSIGNALED(wstatus[1]) && WCOREDUMP(wstatus[1])) { write(2, "Quit (core dumped)\n", 19); g_error = 131; } - else if (wstatus[1] == 0) - return ; - else + else if (WIFSIGNALED(wstatus[1])) + return (write(2, "\n", 1), g_error = 130, 0); + if (WIFEXITED(wstatus[1])) g_error = WEXITSTATUS(wstatus[1]); + return (0); } size_t envlen(t_env *base) diff --git a/includes/builtins.h b/includes/builtins.h index b5c54c8..97d607d 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:48:46 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 19:51:30 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -99,7 +99,7 @@ char **make_env(t_env *env); void no_path_to_hapiness(t_head *head, char **env, char **tab); void free_malloc_fnp(t_head *head, int to_free); //exec_wait -void wait_for_all(pid_t *pid, int x); +int wait_for_all(pid_t *pid, int x); size_t envlen(t_env *base); size_t cmdlen(t_cmd *base); char **redir_arg(t_cmd *cmd, int type); From 533cc4ed50c506bea2889abc7c5756c0e15a4487 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:07:41 +0200 Subject: [PATCH 35/86] norme --- exec/heredoc.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index a93320a..1a507ac 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:33:22 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 15:34:40 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,15 +49,12 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) { str = readline(">> "); if (!str) - return (free(str), 0); + return (free(str), close(fd), 0); //replace strstr by ft_strstr if (strstr("^C", str) != NULL) - return (free(str), 2); + return (free(str), close(fd), 2); if (ft_strcmp(str, eof) == 0) - { - free(str); - break ; - } + return (free(str), close(fd), 0); if (copy->redir->quote == 0) { if (expand_heredoc(&str, head) == 1) @@ -67,8 +64,7 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) write(fd, "\n", 1); free(str); } - close(fd); - return (0); + return (close(fd), 0); } int checker_heredoc(t_head *head, t_redir *c_redir, t_cmd *c_cmd) From 74695ebbbe88a7f7147aadd69c27c07c2468fb28 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:08:04 +0200 Subject: [PATCH 36/86] norme --- exec/built_exit.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exec/built_exit.c b/exec/built_exit.c index 3be1180..0736b06 100644 --- a/exec/built_exit.c +++ b/exec/built_exit.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 17:28:49 by grebrune #+# #+# */ -/* Updated: 2024/07/02 15:24:07 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 15:26:09 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,6 @@ void ft_exit(t_head *head) exit_free(head, g_error); return ; } - copy = head->cmd; if (ft_strnum(copy->arg[1])) { From d894c68323ed4ae546f563fa03dfbda47c60ef00 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:08:18 +0200 Subject: [PATCH 37/86] write bash --- exec/exec_error.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/exec/exec_error.c b/exec/exec_error.c index 05a2cbc..ffb2322 100644 --- a/exec/exec_error.c +++ b/exec/exec_error.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/02 19:44:43 by grebrune #+# #+# */ -/* Updated: 2024/07/02 20:12:08 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 20:24:11 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ void exec_error_exit(char **arg, char *str, char **env, char **path) { + write(2, "bash: ", 6); perror(arg[0]); free_tab(env); free_tab(path); @@ -23,4 +24,4 @@ void exec_error_exit(char **arg, char *str, char **env, char **path) exit (126); else exit (127); -} \ No newline at end of file +} From 8618f433d9281ba7487fa6613edf1bc5f74b244d Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:11:31 +0200 Subject: [PATCH 38/86] rename exec shell --- includes/builtins.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index 97d607d..5275638 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/02 19:51:30 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 20:54:45 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,9 +87,9 @@ void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)); //exec_error void exec_error_exit(char **arg, char *str, char **env, char **path); //exec_main +void exec_shell(char **arg, char *str, char **env); int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x); -void there_cmd(char **arg, char *str, char **env); -int exec_shell(t_head *head, t_cmd *copy, t_fnp *fnp); +int exec_shell_malloc(t_head *head, t_cmd *copy, t_fnp *fnp); int executable(t_head *head); //exec_utils From b74249ce6c3001ff18c53affd2c0cd35dfaeccd0 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:12:09 +0200 Subject: [PATCH 39/86] rework name, exit value, exec_shell --- exec/exec_main.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/exec/exec_main.c b/exec/exec_main.c index 1cf9293..e355272 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,21 +6,19 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/07/02 17:08:21 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 20:32:56 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/builtins.h" -void there_cmd(char **arg, char *str, char **env) +void exec_shell(char **arg, char *str, char **env) { char **path; int i; char *cmd; path = ft_split(str, ':'); - if (!(access(arg[0], F_OK)) && !(access(arg[0], X_OK))) - execve(arg[0], arg, env); i = 0; while (path[i]) { @@ -30,12 +28,9 @@ void there_cmd(char **arg, char *str, char **env) i++; ft_free(cmd); } - perror(arg[0]); - free_tab(env); - free_tab(path); - free_tab(arg); - ft_free(str); - exit (127); + if (!(access(arg[0], F_OK)) && !(access(arg[0], X_OK))) + execve(arg[0], arg, env); + exec_error_exit(arg, str, env, path); } int no_fork_cmd(t_head *head, t_cmd *copy, char *str) @@ -53,7 +48,7 @@ int no_fork_cmd(t_head *head, t_cmd *copy, char *str) return (3); } -int exec_shell(t_head *head, t_cmd *copy, t_fnp *fnp) +int exec_shell_malloc(t_head *head, t_cmd *copy, t_fnp *fnp) { char **env; char **tab; @@ -73,7 +68,7 @@ int exec_shell(t_head *head, t_cmd *copy, t_fnp *fnp) no_path_to_hapiness(head, env, tab); free_fnp(head, fnp); ft_free_all(head); - there_cmd(tab, path, env); + exec_shell(tab, path, env); return (0); } @@ -102,7 +97,7 @@ int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x) return (ft_export(head)); if (ft_strcmp(copy->arg[0], "exit") == 0) return (ft_exit(head), 0); - return (exec_shell(head, copy, fnp)); + return (exec_shell_malloc(head, copy, fnp)); } int executable(t_head *head) From ec9872b99b4e79a888f77579449f23b9260586fb Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 21:26:46 +0200 Subject: [PATCH 40/86] sig main value 1 to 0 --- exec/heredoc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index 1a507ac..224fe59 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ -/* Updated: 2024/07/02 15:34:40 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 21:17:45 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,7 +101,7 @@ int heredoc(t_head *head) sig_main(head, 2); if (checker_heredoc(head, c_redir, c_cmd) == 0) return (0); - sig_main(head, 1); + sig_main(head, 0); } c_redir = c_redir->next; } From 4e428cb08a8469931ab93f112201226503b1a4f7 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 22:42:32 +0200 Subject: [PATCH 41/86] parse all redir --- exec/exec_redir.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/exec/exec_redir.c b/exec/exec_redir.c index 8a258cf..8e9f411 100644 --- a/exec/exec_redir.c +++ b/exec/exec_redir.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:44:44 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:36:20 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 22:38:47 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ int open_the_pipe(int **pipe, t_head *head) return (0); } -int open_files(t_redir *redir) +int open_files(t_redir *redir, int err_print) { if (redir->type == 1) redir->fd = open(redir->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -52,26 +52,39 @@ int open_files(t_redir *redir) if ((redir->type == 4 && redir->fd == -1) || redir->type == 3) redir->fd = open(redir->arg, O_RDONLY); if (redir->fd == -1) - return (perror(redir->arg), 2); + { + if (err_print == 0) + perror(redir->arg); + return (2); + } return (0); } int open_redir(t_cmd *copy, int fd[2]) { t_redir *parser; + t_cmd *c_cmd; + int err_print; fd[1] = 1; fd[0] = 0; - parser = copy->redir; - while (parser) + c_cmd = copy; + err_print = 0; + while (c_cmd) { - if (open_files(parser)) - return (2); - if (parser->type == 1 || parser->type == 2) - fd[1] = parser->fd; - if (parser->type == 3 || parser->type == 4) - fd[0] = parser->fd; - parser = parser->next; + parser = c_cmd->redir; + while (parser) + { + if (open_files(parser, err_print)) + return (2); + if (parser->type == 1 || parser->type == 2) + fd[1] = parser->fd; + if (parser->type == 3 || parser->type == 4) + fd[0] = parser->fd; + parser = parser->next; + } + c_cmd = c_cmd->next; + err_print++; } return (0); } From 0e1f610e1f4a14f7e9e80b571b3bb0e1ff82e30d Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 23:10:19 +0200 Subject: [PATCH 42/86] print if first --- exec/exec_redir.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_redir.c b/exec/exec_redir.c index 8e9f411..3b14651 100644 --- a/exec/exec_redir.c +++ b/exec/exec_redir.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:44:44 by grebrune #+# #+# */ -/* Updated: 2024/07/02 22:38:47 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 22:40:24 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,7 @@ int open_files(t_redir *redir, int err_print) redir->fd = open(redir->arg, O_RDONLY); if (redir->fd == -1) { - if (err_print == 0) + if (err_print != 0) perror(redir->arg); return (2); } From 0a46ef5eb8c10624a38319a464543e0a1d773fb5 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 23:14:21 +0200 Subject: [PATCH 43/86] crash of fork updated --- exec/exec_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_main.c b/exec/exec_main.c index e355272..1ca3974 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/07/02 20:32:56 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 23:10:51 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,7 +80,7 @@ int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x) return (fnp->pid[x] = 0, 0); fnp->pid[x] = fork(); if (fnp->pid[x] < 0) - ft_exit(head); + return (write(2, "Crash of Fork\n", 14), exit_free(head, 0), 0); if (fnp->pid[x] != 0) return (0); sig_main(head, 1); From deb8475913f211a49854a8942b1cb182c693f6f0 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 2 Jul 2024 23:14:58 +0200 Subject: [PATCH 44/86] mod open files proto --- includes/builtins.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/builtins.h b/includes/builtins.h index 5275638..8ce3be1 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -108,7 +108,7 @@ char **make_arg(t_cmd *cmd); //exec_redir void fail_malloc(t_head *head, int **fd, size_t i); int open_the_pipe(int **fd, t_head *head); -int open_files(t_redir *redir); +int open_files(t_redir *redir, int err_print); int open_redir(t_cmd *copy, int fd[2]); //exec_redir_utils From f6281e9aa2ca03a01f3e8c5d89f6821d32381a31 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 18:49:26 +0200 Subject: [PATCH 45/86] big rework of cd no leaks found --- exec/built_cd.c | 113 ++++++++++++++++++++++---------------- exec/built_cd_back.c | 127 ++++++++++++++----------------------------- includes/builtins.h | 14 +++-- 3 files changed, 117 insertions(+), 137 deletions(-) diff --git a/exec/built_cd.c b/exec/built_cd.c index 17ad7e3..4e9b5ae 100644 --- a/exec/built_cd.c +++ b/exec/built_cd.c @@ -6,66 +6,80 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:49:11 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:46:19 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:42:01 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -int ft_cd(t_head *head) -{ - int err; - char *str; - char *old_pwd; - DIR *ptr_dir; +//tild +//minus - old_pwd = NULL; - str = NULL; - if (head->cmd->next || !head->cmd->arg[1]) +int cd_error(t_head *head) +{ + if (head->cmd->next || (head->cmd->arg[1] && head->cmd->arg[1][0] == 0)) return (1); - if (ft_cd_bis(head, &str) == 2) - return (2); - get_path(&old_pwd); - err = chdir(str); - if (err != 0) - { - g_error = 1; - ptr_dir = opendir(head->cmd->arg[1]); - return (free(old_pwd), free(str), \ - write(2, "bash: cd: ", 10), perror(head->cmd->arg[1]), 1); - closedir(ptr_dir); - } - cd_rep_or_new(head, str, old_pwd); + if (head->cmd->arg[1] && head->cmd->arg[2]) + return (write(2, "bash: cd: too many arguments\n", 29), g_error = 1, 1); return (0); } -int ft_cd_bis(t_head *head, char **str) +void cd_rep_or_new(t_head *head, char *n_pwd, char *old_pwd) { - if (head->cmd->arg[1] && head->cmd->arg[2]) - return (write(2, "bash: cd: too many arguments\n", 29), 2); - if (head->cmd->arg[1]) + if (replace_value(head, n_pwd, "PWD")) { - if (0 == ft_strcmp(head->cmd->arg[1], "../") - || 0 == ft_strcmp(head->cmd->arg[1], "..")) - return (cd_back(head), 2); - if (0 == ft_strcmp(head->cmd->arg[1], "~/")) - return (cd_tild(head), 2); - get_path(str); - *str = ft_strcat(*str, head->cmd->arg[1]); - if (!*str) - return (ft_free_all(head), exit(1), 1); + new_pwd(head, n_pwd, "PWD"); + free(n_pwd); } - else + if (replace_value(head, old_pwd, "OLDPWD")) { - cd_no_arg(head, str); - if (!*str) - return (write(2, "bash: cd: HOME not set\n", 23), 2); + new_pwd(head, old_pwd, "OLDPWD"); + free(old_pwd); } - if (*str == NULL) - return (write(2, "Crash of Malloc\n", 16), 2); +} + +int ft_cd(t_head *head) +{ + int err; + char *new; + char *old; + + old = NULL; + if (cd_error(head)) + return (1); + new = cd_relative(head); + if (!new) + return (2); + get_path(&old); + err = chdir(new); + if (err != 0) + return (cd_chdir_error(head, old, new), 1); + cd_rep_or_new(head, new, old); return (0); } +char *cd_relative(t_head *head) +{ + char *new; + + new = NULL; + if (!head->cmd->arg[1]) + return (cd_find_var(head, "HOME")); + if (0 == ft_strcmp(head->cmd->arg[1], "../") + || 0 == ft_strcmp(head->cmd->arg[1], "..")) + return (cd_back_trim()); + if (0 == ft_strcmp(head->cmd->arg[1], "~/") + || 0 == ft_strcmp(head->cmd->arg[1], "~")) + return (cd_tild_trim(head)); + if (0 == ft_strcmp(head->cmd->arg[1], "-")) + return (cd_find_var(head, "OLDPWD")); + get_path(&new); + new = ft_strcat(new, head->cmd->arg[1]); + if (!new) + return (write(2, "Crash of Malloc\n", 16), ft_free_all(head), NULL); + return (new); +} + int replace_value(t_head *head, char *value, char *replace) { t_env *copy; @@ -84,20 +98,27 @@ int replace_value(t_head *head, char *value, char *replace) return (1); } -void cd_no_arg(t_head *head, char **str) +char *cd_find_var(t_head *head, char *name) { t_env *env; + char *str; + str = NULL; env = head->env; while (env) { - if (ft_strcmp(env->name, "HOME") == 0) + if (ft_strcmp(env->name, name) == 0) { - *str = env->value; + str = ft_strdup(env->value); + if (!str) + return (write(2, "Crash of Malloc\n", 16), NULL); break ; } env = env->next; } + if (!str) + return (cd_not_found(name), NULL); + return (str); } char *ft_strcat(char *path, char *dir) @@ -106,7 +127,7 @@ char *ft_strcat(char *path, char *dir) size_t i; size_t x; - dest = malloc(sizeof (char) * (ft_strlen(dir) + ft_strlen(path) + 2)); + dest = ft_calloc(sizeof (char), (ft_strlen(dir) + ft_strlen(path) + 2)); if (dest == NULL) return (dest); i = 0; diff --git a/exec/built_cd_back.c b/exec/built_cd_back.c index f12c03b..2e31c26 100644 --- a/exec/built_cd_back.c +++ b/exec/built_cd_back.c @@ -6,32 +6,62 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 14:35:38 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:38:48 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:37:49 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -char *cd_back_trim(char *str) +char *cd_tild_trim(t_head *head) { - char *ret; + char *pwd; + char *new; + + pwd = cd_find_var(head, "USER"); + if (!pwd) + return (NULL); + new = ft_strjoin("/home/", pwd); + if (!new) + return (free(pwd), write(2, "Crash of Malloc\n", 16), NULL); + return (free(pwd), new); +} + +char *cd_back_trim(void) +{ + char *pwd; + char *new; int i; int x; i = 0; - while (str && str[i]) + pwd = NULL; + get_path(&pwd); + printf("--%s--\n", pwd); + while (pwd && pwd[i]) i++; - while (str && str[i] != '/') + while (pwd && pwd[i] != '/') i--; - ret = malloc(sizeof(char) * (i + 1)); - x = -1; - while (++x < i) - ret[x] = str[x]; - ret[x] = 0; - return (ret); + new = malloc(sizeof(char) * (i + 1)); + if (!new) + return (free(pwd), write(2, "Crash of Malloc\n", 16), NULL); + x = 0; + while (x < i) + { + new[x] = pwd[x]; + x++; + } + new[x] = 0; + return (free(pwd), new); } -void cd_back_error(t_head *head, char *old_pwd, char *new_pwd) +void cd_not_found(char *name) +{ + write(2, "bash: cd: ", 10); + write(2, name, ft_strlen(name)); + write(2, " not set\n", 9); +} + +void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd) { DIR *ptr_dir; @@ -42,76 +72,3 @@ void cd_back_error(t_head *head, char *old_pwd, char *new_pwd) free(old_pwd); free(new_pwd); } - -void cd_back(t_head *head) -{ - int err; - char *old_pwd; - char *pwd; - char *n_pwd; - - pwd = value_of_name(head->env, "PWD"); - old_pwd = ft_strdup(pwd); - n_pwd = cd_back_trim(pwd); - err = chdir(n_pwd); - if (err != 0) - { - cd_back_error(head, old_pwd, n_pwd); - return ; - } - cd_rep_or_new(head, n_pwd, old_pwd); -} - -char *cd_tild_trim(t_head *head) -{ - char *path; - char *pwd; - int i; - int x; - - pwd = value_of_name(head->env, "PWD"); - if (!pwd) - return (NULL); - i = 0; - if (ft_strncmp("/home/", pwd, 5) == 0) - i = 6; - while (pwd[i] && pwd[i] != '/') - i++; - if (i < 6 || pwd[i] != '/') - return (NULL); - i++; - path = malloc(sizeof(char) * i + 1); - x = -1; - while (i > ++x) - path[x] = pwd[x]; - return (path[x] = '\0', path); -} - -int cd_tild(t_head *head) -{ - int err; - char *old_pwd; - char *pwd; - char *n_pwd; - - pwd = value_of_name(head->env, "PWD"); - if (!pwd) - return (write(2, "bash: cd: ~/: No such file or directory\n", 40), 2); - old_pwd = ft_strdup(pwd); - if (!old_pwd) - return (2); - n_pwd = cd_tild_trim(head); - if (!n_pwd) - return (free(old_pwd), 2); - err = chdir(n_pwd); - if (err != 0) - { - cd_back_error(head, old_pwd, n_pwd); - return (2); - } - if (replace_value(head, n_pwd, "PWD")) - new_pwd(head, n_pwd, "PWD"); - if (replace_value(head, old_pwd, "OLDPWD")) - new_pwd(head, old_pwd, "OLDPWD"); - return (0); -} diff --git a/includes/builtins.h b/includes/builtins.h index 8ce3be1..605bd7e 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/02 20:54:45 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:14:59 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,13 +27,16 @@ typedef struct s_fnp t_fnp; typedef struct s_head t_head; int ft_cd(t_head *head); -int ft_cd_bis(t_head *head, char **str); +char *cd_relative(t_head *head); int replace_value(t_head *head, char *value, char *replace); -void cd_no_arg(t_head *head, char **str); +char *cd_find_var(t_head *head, char *name); char *ft_strcat(char *path, char *dir); //cd_back -void cd_back(t_head *head); -int cd_tild(t_head *head); +char *cd_tild_trim(t_head *head); +char *cd_back_trim(); +void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd); +void cd_not_found(char *name); +char *cd_minus(t_head *head); void ft_echo(t_head *head, t_cmd *copy); void print_tab(t_head *head, char **arg, int i, int n); @@ -48,7 +51,6 @@ int is_num(t_head *head); //exit_free void exit_free(t_head *head, int status); void new_pwd(t_head *head, char *value, char *name); -void cd_rep_or_new(t_head *head, char *n_pwd, char *old_pwd); int ft_export(t_head *head); int export_bis(t_head *head, t_env *c_env, size_t i); From 5e951349ed84fa64bec9ef5622025c6a1d6c104b Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 18:50:59 +0200 Subject: [PATCH 46/86] last != -1 --- exec/exec_wait.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index e47a218..339211a 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ -/* Updated: 2024/07/02 20:52:08 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 21:16:03 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ int wait_for_all(pid_t *pid, int x) } else if (WIFSIGNALED(wstatus[1])) return (write(2, "\n", 1), g_error = 130, 0); - if (WIFEXITED(wstatus[1])) + if (WIFEXITED(wstatus[1]) && last != -1) g_error = WEXITSTATUS(wstatus[1]); return (0); } From 9bfa73ce85490156f9af8b3b2483a6f5e4edab4b Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 18:51:27 +0200 Subject: [PATCH 47/86] rem of cd_rep_or_new --- exec/built_exit_free.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/exec/built_exit_free.c b/exec/built_exit_free.c index c6952a6..0badf2a 100644 --- a/exec/built_exit_free.c +++ b/exec/built_exit_free.c @@ -40,17 +40,3 @@ void new_pwd(t_head *head, char *value, char *name) return ; return ; } - -void cd_rep_or_new(t_head *head, char *n_pwd, char *old_pwd) -{ - if (replace_value(head, n_pwd, "PWD")) - { - new_pwd(head, n_pwd, "PWD"); - free(n_pwd); - } - if (replace_value(head, old_pwd, "OLDPWD")) - { - new_pwd(head, old_pwd, "OLDPWD"); - free(old_pwd); - } -} From b3a64869bd4ef30ec83d4c717703647b21ad4e71 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 18:56:36 +0200 Subject: [PATCH 48/86] ft_strcat to cd_cat_backslash --- includes/builtins.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index 605bd7e..5adcd9e 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:14:59 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:33:38 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,13 +30,12 @@ int ft_cd(t_head *head); char *cd_relative(t_head *head); int replace_value(t_head *head, char *value, char *replace); char *cd_find_var(t_head *head, char *name); -char *ft_strcat(char *path, char *dir); +char *cd_cat_backslash(char *path, char *dir); //cd_back char *cd_tild_trim(t_head *head); char *cd_back_trim(); void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd); void cd_not_found(char *name); -char *cd_minus(t_head *head); void ft_echo(t_head *head, t_cmd *copy); void print_tab(t_head *head, char **arg, int i, int n); From 77e15aa355c0936450de595d9ecffbf3195140bb Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 18:58:27 +0200 Subject: [PATCH 49/86] move replace value --- exec/built_exit_free.c | 18 ++++++++++++++++++ includes/builtins.h | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/exec/built_exit_free.c b/exec/built_exit_free.c index 0badf2a..7ade7ff 100644 --- a/exec/built_exit_free.c +++ b/exec/built_exit_free.c @@ -40,3 +40,21 @@ void new_pwd(t_head *head, char *value, char *name) return ; return ; } + +int replace_value(t_head *head, char *value, char *replace) +{ + t_env *copy; + + copy = head->env; + while (copy->next != NULL) + { + if (ft_strcmp(copy->name, replace) == 0) + { + free(copy->value); + copy->value = value; + return (0); + } + copy = copy->next; + } + return (1); +} diff --git a/includes/builtins.h b/includes/builtins.h index 5adcd9e..cfcf9a3 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:33:38 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:56:36 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,6 @@ typedef struct s_head t_head; int ft_cd(t_head *head); char *cd_relative(t_head *head); -int replace_value(t_head *head, char *value, char *replace); char *cd_find_var(t_head *head, char *name); char *cd_cat_backslash(char *path, char *dir); //cd_back @@ -50,6 +49,7 @@ int is_num(t_head *head); //exit_free void exit_free(t_head *head, int status); void new_pwd(t_head *head, char *value, char *name); +int replace_value(t_head *head, char *value, char *replace); int ft_export(t_head *head); int export_bis(t_head *head, t_env *c_env, size_t i); From 89a0281b56df7d3e541b65d3810abd5cd05c5aa5 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 19:01:43 +0200 Subject: [PATCH 50/86] normed --- exec/built_cd.c | 55 +++----------------------------------------- exec/built_cd_back.c | 30 +++++++++++++++++++++++- includes/builtins.h | 4 ++-- 3 files changed, 34 insertions(+), 55 deletions(-) diff --git a/exec/built_cd.c b/exec/built_cd.c index 4e9b5ae..5c5f401 100644 --- a/exec/built_cd.c +++ b/exec/built_cd.c @@ -6,15 +6,12 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:49:11 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:42:01 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:56:36 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -//tild -//minus - int cd_error(t_head *head) { if (head->cmd->next || (head->cmd->arg[1] && head->cmd->arg[1][0] == 0)) @@ -74,30 +71,12 @@ char *cd_relative(t_head *head) if (0 == ft_strcmp(head->cmd->arg[1], "-")) return (cd_find_var(head, "OLDPWD")); get_path(&new); - new = ft_strcat(new, head->cmd->arg[1]); + new = cd_cat_backslash(new, head->cmd->arg[1]); if (!new) - return (write(2, "Crash of Malloc\n", 16), ft_free_all(head), NULL); + return (write(2, "Crash of Malloc\n", 16), NULL); return (new); } -int replace_value(t_head *head, char *value, char *replace) -{ - t_env *copy; - - copy = head->env; - while (copy->next != NULL) - { - if (ft_strcmp(copy->name, replace) == 0) - { - free(copy->value); - copy->value = value; - return (0); - } - copy = copy->next; - } - return (1); -} - char *cd_find_var(t_head *head, char *name) { t_env *env; @@ -120,31 +99,3 @@ char *cd_find_var(t_head *head, char *name) return (cd_not_found(name), NULL); return (str); } - -char *ft_strcat(char *path, char *dir) -{ - char *dest; - size_t i; - size_t x; - - dest = ft_calloc(sizeof (char), (ft_strlen(dir) + ft_strlen(path) + 2)); - if (dest == NULL) - return (dest); - i = 0; - while (path && path[i]) - { - dest[i] = path[i]; - i++; - } - x = 0; - dest[i++] = '/'; - while (dir && dir[x]) - { - dest[i] = dir[x]; - i++; - x++; - } - dest[i] = '\0'; - free(path); - return (dest); -} diff --git a/exec/built_cd_back.c b/exec/built_cd_back.c index 2e31c26..ec0c5c9 100644 --- a/exec/built_cd_back.c +++ b/exec/built_cd_back.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 14:35:38 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:37:49 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:41:12 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,3 +72,31 @@ void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd) free(old_pwd); free(new_pwd); } + +char *cd_cat_backslash(char *path, char *dir) +{ + char *dest; + size_t i; + size_t x; + + dest = ft_calloc(sizeof (char), (ft_strlen(dir) + ft_strlen(path) + 2)); + if (!dest) + return (NULL); + i = 0; + while (path && path[i]) + { + dest[i] = path[i]; + i++; + } + x = 0; + dest[i++] = '/'; + while (dir && dir[x]) + { + dest[i] = dir[x]; + i++; + x++; + } + dest[i] = '\0'; + free(path); + return (dest); +} diff --git a/includes/builtins.h b/includes/builtins.h index cfcf9a3..db93ea6 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:56:36 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 18:58:27 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ char *cd_find_var(t_head *head, char *name); char *cd_cat_backslash(char *path, char *dir); //cd_back char *cd_tild_trim(t_head *head); -char *cd_back_trim(); +char *cd_back_trim(void); void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd); void cd_not_found(char *name); From 912cb20a5509e138acf0c2f1743d720e50b99f62 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 19:05:37 +0200 Subject: [PATCH 51/86] TO DO --- to_check | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 to_check diff --git a/to_check b/to_check new file mode 100644 index 0000000..4e6f6f4 --- /dev/null +++ b/to_check @@ -0,0 +1,7 @@ + +valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --read-var-info=yes -s --trace-children=yes ./minishell + + +heredoc strstr replace by ft_strstr + +if malloc fail exit totally ? \ No newline at end of file From ffc39e1ef07443273b7e26da440e978336b4734e Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Wed, 3 Jul 2024 19:06:46 +0200 Subject: [PATCH 52/86] none --- exec/exec_main.c | 2 +- exec/signals_main.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_main.c b/exec/exec_main.c index 1ca3974..36d2b74 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/07/02 23:10:51 by grebrune ### ########.fr */ +/* Updated: 2024/07/03 16:13:25 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/exec/signals_main.c b/exec/signals_main.c index ccf2144..6a36206 100644 --- a/exec/signals_main.c +++ b/exec/signals_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 17:53:49 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:06:43 by grebrune ### ########.fr */ +/* Updated: 2024/07/02 21:24:04 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ From d62e3188f9f1cb1171ab848b9621382aa14c74e4 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 13:52:16 +0200 Subject: [PATCH 53/86] add --- super_libft/srcs/ft_strstr.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 super_libft/srcs/ft_strstr.c diff --git a/super_libft/srcs/ft_strstr.c b/super_libft/srcs/ft_strstr.c new file mode 100644 index 0000000..0f16255 --- /dev/null +++ b/super_libft/srcs/ft_strstr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: grebrune +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/17 22:00:15 by grebrune #+# #+# */ +/* Updated: 2023/09/20 20:43:40 by grebrune ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strstr(char *str, char *to_find) +{ + int i; + int x; + + i = 0; + while (str[i] != '\0') + { + x = 0; + while (str[i + x] == to_find[x] && to_find[x] != '\0') + { + x++; + } + if (to_find[x] == '\0') + return (str + i); + i++; + } + if (str[0] == '\0' && to_find[0] == '\0') + return (str); + return (0); +} From 93a719448180f9834f9783bdbfd8d575c87179c4 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 14:55:00 +0200 Subject: [PATCH 54/86] push for final --- exec/heredoc.c | 3 +-- srcs/expand_var_utils.c | 6 ++++-- srcs/main.c | 31 ++++++++++++++++++++----------- srcs/parser.c | 10 +++++----- srcs/utils.c | 10 ++++++---- super_libft/Makefile | 1 + super_libft/libft.h | 1 + 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/exec/heredoc.c b/exec/heredoc.c index 224fe59..53cb975 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -50,8 +50,7 @@ int here_read_print(int fd, char *eof, t_cmd *copy, t_head *head) str = readline(">> "); if (!str) return (free(str), close(fd), 0); - //replace strstr by ft_strstr - if (strstr("^C", str) != NULL) + if (ft_strstr("^C", str) != NULL) return (free(str), close(fd), 2); if (ft_strcmp(str, eof) == 0) return (free(str), close(fd), 0); diff --git a/srcs/expand_var_utils.c b/srcs/expand_var_utils.c index e120aff..e71cac5 100644 --- a/srcs/expand_var_utils.c +++ b/srcs/expand_var_utils.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 13:55:23 by beroy #+# #+# */ -/* Updated: 2024/06/24 17:10:51 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 13:34:22 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ char *var_value(char *str, int i, t_env *env) break ; env = env->next; } - if (value == NULL && str_cmp_var(str, i, "?")) + if (value == NULL && str[i] == '?') value = ft_itoa(g_error); if (value == NULL) value = ft_strdup(""); @@ -82,6 +82,8 @@ int find_end_var(char *str, int i) j = i; while (str && str[i]) { + if (str[i] == '?') + return (i + 1); if (end_of_var(str[i]) == 1) break ; if (j == i && char_is_num(str[i]) == 1) diff --git a/srcs/main.c b/srcs/main.c index 6c0ba59..5dafb1d 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/07/02 13:02:02 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 14:46:01 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,23 @@ int single_exec(t_head *head, char *input) return (0); } +void here_exec(t_head *head, char *input) +{ + if (ft_parse(input, head) == 0) + { + if (heredoc(head)) + { + if (cmd_is_empty(head->cmd, 0) == 0) + executable(head); + } + else + write(2, "bash: syntax error\n", 19); + } + if (head->cmd != NULL) + ft_free_cmd(&(head->cmd)); + free(input); +} + void main_loop(t_head *head) { char *input; @@ -48,15 +65,7 @@ void main_loop(t_head *head) free(input); continue ; } - if (ft_parse(input, head) == 0) - { - if (heredoc(head)) - if (cmd_is_empty(head->cmd, 0) == 0) - executable(head); - } - if (head->cmd != NULL) - ft_free_cmd(&(head->cmd)); - free(input); + here_exec(head, input); } write(1, "exit\n", 5); } @@ -67,7 +76,7 @@ int main(int ac, char **av, char **env) (void)ac; (void)av; -// ft_header(); + ft_header(); head = head_init(env); if (head == NULL) return (0); diff --git a/srcs/parser.c b/srcs/parser.c index 124141e..0a3180e 100644 --- a/srcs/parser.c +++ b/srcs/parser.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parser.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ +/* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/17 13:19:51 by beroy #+# #+# */ -/* Updated: 2024/06/27 13:29:29 by beroy ### ########.fr */ +/* Updated: 2024/07/08 14:47:24 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,12 +64,12 @@ int check_line(char *input) if (input[i] == '|') { if (input[i + 1] == '|') - return (printf("Double pipe not handled by minishell!\n"), 1); + return (1); } else if (input[i] == 34 || input[i] == 39) { if (quote_skip(input, &i, input[i]) == 1) - return (printf("Quote unterminated!\n"), 1); + return (1); } i++; } @@ -107,7 +107,7 @@ t_cmd *split_pipe(char *input) int ft_parse(char *input, t_head *head) { if (check_line(input) == 1) - return (1); + return (write(2, "bash: syntax error\n", 19), 1); head->cmd = split_pipe(input); if (head->cmd == NULL) return (1); diff --git a/srcs/utils.c b/srcs/utils.c index d147d83..c874556 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: beroy +#+ +:+ +#+ */ +/* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/19 14:16:23 by beroy #+# #+# */ -/* Updated: 2024/06/19 14:17:17 by beroy ### ########.fr */ +/* Updated: 2024/07/08 14:53:09 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,8 @@ void single_index_up(char *input, int *i) { if (input[*i] == 39) quote_skip(input, i, 39); - *i += 1; + if (input[*i] != 0) + *i += 1; } int char_is_num(char c) @@ -53,5 +54,6 @@ void index_up(char *input, int *i) quote_skip(input, i, 34); else if (input[*i] == 39) quote_skip(input, i, 39); - *i += 1; + if (input[*i] != 0) + *i += 1; } diff --git a/super_libft/Makefile b/super_libft/Makefile index 95983c6..6cdfef5 100644 --- a/super_libft/Makefile +++ b/super_libft/Makefile @@ -28,6 +28,7 @@ SRCS = ft_atoi.c \ ft_strcmp.c \ ft_strncmp.c \ ft_strnstr.c \ + ft_strstr.c \ ft_strrchr.c \ ft_strtrim.c \ ft_substr.c \ diff --git a/super_libft/libft.h b/super_libft/libft.h index 26c5ca8..8a5d0d7 100644 --- a/super_libft/libft.h +++ b/super_libft/libft.h @@ -73,6 +73,7 @@ char *ft_strtrim(char const *s1, char const *set); char *ft_itoa(int n); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strstr(char *str, char *to_find); char **ft_split(char const *s, char c); From fad3a3b9c5aa936a1b88797678811fe1fdb09c44 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 18:30:56 +0200 Subject: [PATCH 55/86] corr of cd absolute path --- exec/built_cd.c | 12 ++++-- exec/built_cd_back.c | 8 ++-- exec/built_exit_free.c | 28 ++++++++++++- exec/built_export_no_arg.c | 2 +- exec/built_pwd.c | 9 ++--- exec/exec_main.c | 8 ++-- exec/exec_redir_utils.c | 6 +-- includes/builtins.h | 7 ++-- srcs/main.c | 33 +++++++++++++-- srcs/parser.c | 4 +- to_check | 83 +++++++++++++++++++++++++++++++++++++- 11 files changed, 167 insertions(+), 33 deletions(-) diff --git a/exec/built_cd.c b/exec/built_cd.c index 5c5f401..8a3b371 100644 --- a/exec/built_cd.c +++ b/exec/built_cd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:49:11 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:56:36 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 18:29:32 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,9 @@ int ft_cd(t_head *head) new = cd_relative(head); if (!new) return (2); - get_path(&old); + err = get_path(&old); + if (err == 2) + exit_free(head, 0); err = chdir(new); if (err != 0) return (cd_chdir_error(head, old, new), 1); @@ -64,12 +66,16 @@ char *cd_relative(t_head *head) return (cd_find_var(head, "HOME")); if (0 == ft_strcmp(head->cmd->arg[1], "../") || 0 == ft_strcmp(head->cmd->arg[1], "..")) - return (cd_back_trim()); + return (cd_back_trim(head)); if (0 == ft_strcmp(head->cmd->arg[1], "~/") || 0 == ft_strcmp(head->cmd->arg[1], "~")) return (cd_tild_trim(head)); if (0 == ft_strcmp(head->cmd->arg[1], "-")) return (cd_find_var(head, "OLDPWD")); + if (!ft_strncmp(head->cmd->arg[1], "~/", 2)) + return (cat_of_tild(head, head->cmd->arg[1])); + if (!ft_strncmp(head->cmd->arg[1], "/home", 5)) + return (ft_strdup(head->cmd->arg[1])); get_path(&new); new = cd_cat_backslash(new, head->cmd->arg[1]); if (!new) diff --git a/exec/built_cd_back.c b/exec/built_cd_back.c index ec0c5c9..1400097 100644 --- a/exec/built_cd_back.c +++ b/exec/built_cd_back.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 14:35:38 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:41:12 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 17:39:15 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ char *cd_tild_trim(t_head *head) return (free(pwd), new); } -char *cd_back_trim(void) +char *cd_back_trim(t_head *head) { char *pwd; char *new; @@ -35,8 +35,8 @@ char *cd_back_trim(void) i = 0; pwd = NULL; - get_path(&pwd); - printf("--%s--\n", pwd); + if (get_path(&pwd) == 2) + exit_free(head, 0); while (pwd && pwd[i]) i++; while (pwd && pwd[i] != '/') diff --git a/exec/built_exit_free.c b/exec/built_exit_free.c index 7ade7ff..63e5152 100644 --- a/exec/built_exit_free.c +++ b/exec/built_exit_free.c @@ -46,7 +46,7 @@ int replace_value(t_head *head, char *value, char *replace) t_env *copy; copy = head->env; - while (copy->next != NULL) + while (copy) { if (ft_strcmp(copy->name, replace) == 0) { @@ -58,3 +58,29 @@ int replace_value(t_head *head, char *value, char *replace) } return (1); } + +char *cat_of_tild(t_head *head, char *str) +{ + size_t i; + size_t x; + char *new; + char *home; + + i = 0; + home = cd_find_var(head, "HOME"); + if (!home) + return (NULL); + i = ft_strlen(str) - 1; + x = ft_strlen(home); + new = ft_calloc(sizeof (char), x + i + 1); + if (!new) + return (free(home), NULL); + i = -1; + while (home[++i]) + new[i] = home[i]; + x = 1; + while (str[x]) + new[i++] = str[x++]; + new[i] = '\0'; + return (free(home), new); +} diff --git a/exec/built_export_no_arg.c b/exec/built_export_no_arg.c index 3660fd7..733647b 100644 --- a/exec/built_export_no_arg.c +++ b/exec/built_export_no_arg.c @@ -33,7 +33,7 @@ int ex_no_args(t_head *head) swap_this_tab(tab); printf_tab(tab); free_tab(tab); - close_pipe(head, head->fnp->pipe); + close_pipe(); free_fnp(head, head->fnp); ft_free_all(head); exit(0); diff --git a/exec/built_pwd.c b/exec/built_pwd.c index a3598b6..827cb5e 100644 --- a/exec/built_pwd.c +++ b/exec/built_pwd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ -/* Updated: 2024/07/01 17:53:49 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 16:11:24 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,10 +20,7 @@ void ft_pwd(t_head *head) str = NULL; err = get_path(&str); if (err == 2) - { - write(2, "bash: pwd: ", 11); - perror(str); - } + exit_free(head, 2); if (str) printf("%s\n", str); free(str); @@ -43,7 +40,7 @@ int get_path(char **str) free(*(str)); *(str) = NULL; if (errno != ERANGE) - return (2); + return (perror("bash: getcwd"), 2); } size += 10; } diff --git a/exec/exec_main.c b/exec/exec_main.c index 36d2b74..8d93fbf 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/07/03 16:13:25 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 17:32:16 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,8 +85,8 @@ int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x) return (0); sig_main(head, 1); if (redir_with_fd(fd, fnp->pipe, copy, x)) - return (close_pipe(head, fnp->pipe), exit_free(head, 1), 2); - close_pipe(head, fnp->pipe); + return (close_pipe(), exit_free(head, 1), 2); + close_pipe(); if (ft_strcmp(copy->arg[0], "echo") == 0) return (ft_echo(head, copy), 1); if (ft_strcmp(copy->arg[0], "env") == 0) @@ -115,7 +115,7 @@ int executable(t_head *head) copy = copy->next; x++; } - close_pipe(head, head->fnp->pipe); + close_pipe(); wait_for_all(head->fnp->pid, x); free_fnp(head, head->fnp); return (0); diff --git a/exec/exec_redir_utils.c b/exec/exec_redir_utils.c index e8ff947..06b1085 100644 --- a/exec/exec_redir_utils.c +++ b/exec/exec_redir_utils.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/12 13:57:10 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:48:46 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 17:31:01 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,12 +32,10 @@ int fill_pipe(size_t numb, int **fd, t_head *head) return (0); } -void close_pipe(t_head *head, int **pipe) +void close_pipe(void) { int i; - (void)head; - (void)pipe; i = 2; while (++i < 1024) close(i); diff --git a/includes/builtins.h b/includes/builtins.h index db93ea6..6572aea 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/03 18:58:27 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 17:31:52 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ char *cd_find_var(t_head *head, char *name); char *cd_cat_backslash(char *path, char *dir); //cd_back char *cd_tild_trim(t_head *head); -char *cd_back_trim(void); +char *cd_back_trim(t_head *head); void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd); void cd_not_found(char *name); @@ -50,6 +50,7 @@ int is_num(t_head *head); void exit_free(t_head *head, int status); void new_pwd(t_head *head, char *value, char *name); int replace_value(t_head *head, char *value, char *replace); +char *cat_of_tild(t_head *head, char *str); int ft_export(t_head *head); int export_bis(t_head *head, t_env *c_env, size_t i); @@ -115,7 +116,7 @@ int open_redir(t_cmd *copy, int fd[2]); //exec_redir_utils int fill_pipe(size_t numb, int **fd, t_head *head); int redir_with_fd(int fd[2], int **pipe, t_cmd *copy, int x); -void close_pipe(t_head *head, int **fd); +void close_pipe(void); void free_fnp(t_head *head, t_fnp *fnp); int malloc_fnp(t_head *head); //heredoc diff --git a/srcs/main.c b/srcs/main.c index 5dafb1d..d266909 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/07/08 14:46:01 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 17:31:02 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,30 @@ int single_exec(t_head *head, char *input) return (0); } +int open_on_err(t_cmd *cmd) +{ + t_cmd *c_cmd; + t_redir *c_red; + + c_cmd = cmd; + while (c_cmd) + { + c_red = c_cmd->redir; + while (c_red) + { + if (c_red->type == 1) + c_red->fd = open(c_red->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (c_red->type == 2) + c_red->fd = open(c_red->arg, O_WRONLY | O_CREAT | O_APPEND, 0644); + if ((c_red->type == 2 || c_red->type == 1) && c_red->fd == -1) + return (perror(c_red->arg), 2); + c_red = c_red->next; + } + c_cmd = c_cmd->next; + } + return (0); +} + void here_exec(t_head *head, char *input) { if (ft_parse(input, head) == 0) @@ -36,9 +60,12 @@ void here_exec(t_head *head, char *input) { if (cmd_is_empty(head->cmd, 0) == 0) executable(head); + else + { + open_on_err(head->cmd); + close_pipe(); + } } - else - write(2, "bash: syntax error\n", 19); } if (head->cmd != NULL) ft_free_cmd(&(head->cmd)); diff --git a/srcs/parser.c b/srcs/parser.c index 0a3180e..da470e1 100644 --- a/srcs/parser.c +++ b/srcs/parser.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/17 13:19:51 by beroy #+# #+# */ -/* Updated: 2024/07/08 14:47:24 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 15:09:18 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -123,6 +123,6 @@ int ft_parse(char *input, t_head *head) if (format(head) == 1) return (1); if (cmd_is_empty(head->cmd, 1) == 1) - return (1); + return (write(2, "bash: syntax error\n", 19), 1); return (0); } diff --git a/to_check b/to_check index 4e6f6f4..2f71539 100644 --- a/to_check +++ b/to_check @@ -1,7 +1,86 @@ valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --read-var-info=yes -s --trace-children=yes ./minishell +- Message d'erreur si infile inexistant : + - < Makefile > out < in < Makefile > out1 cat -heredoc strstr replace by ft_strstr +- on unset PATH in bash, then launch minishell ; + - env alone prints env; + - env in pipe (> env | /bin/grep "PATH") says : env: No such file or directory -if malloc fail exit totally ? \ No newline at end of file +- on unset USER, cd ~ : + -> cd ~ + bash: cd: USER not set + > echo $? + ==139176== Invalid read of size 8 + +- cd doesnt accept absolute path from root (?) +> cd + > pwd + /home/grebrune + > cd /home/grebrune/Documents/github/4_Circle/minishell + bash: cd: No such file or directory + > + +- expand segfaults on unset env variable + > echo $OLDPWD + /home/grebrune + > unset OLDPWD + > echo $OLDPWD + [1] 139575 segmentation fault (core dumped) ./minishell + ==139826== Invalid read of size 8 + ==139826== at 0x40181C: var_value (expand_var_utils.c:72) + +- mystery child does not die + ld: No such file or directory + > ls + ls: No such file or directory + > echo $? + 127 + > env + PWD=/home/grebrune/Documents/github/4_Circle/minishell + SHLVL=1 + > unset PWD SHLVL + > env + > echo $USER + + > echo $? + 0 + > export 'a=ehehe' b=super c=quel_fun + > env + a=ehehe + b=super + c=quel_fun + > env | /bin/wc -l + env: No such file or directory + > echo $? + 127 + > env + a=ehehe + b=super + c=quel_fun + > + > mkdir supp + mkdir: No such file or directory + > /^Cdir supp + > exit + > exit + +- on heredoc interrupted with SIGINT + > cat << 0 + ==140494== Warning: noted but unhandled ioctl 0x5412 with no size/direction hints. + ==140494== This could cause spurious value errors to appear. + ==140494== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper. + + > + exit + ==140494== + ==140494== FILE DESCRIPTORS: 4 open (3 std) at exit. + ==140494== Open file descriptor 3: + ==140494== at 0x4AD007D: pipe (pipe.c:29) + ==140494== by 0x408FF1: checker_heredoc (heredoc.c:76) + ==140494== by 0x4090C7: heredoc (heredoc.c:101) + ==140494== by 0x401FDE: here_exec (main.c:59) + ==140494== by 0x4020D6: main_loop (main.c:92) + ==140494== by 0x402155: main (main.c:108) + ==140494== From cc1a251edff98fd850faa68a586c089c0a0db9b7 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 18:51:00 +0200 Subject: [PATCH 56/86] close fd in ctrl c heredoc --- exec/signals_main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/exec/signals_main.c b/exec/signals_main.c index 6a36206..f4d95ce 100644 --- a/exec/signals_main.c +++ b/exec/signals_main.c @@ -51,6 +51,7 @@ void sig_heredoc(int sig) { g_error = 130; ioctl(STDIN_FILENO, TIOCSTI, "\n"); + close_pipe(); } } From ff3b41936db1360fc41fab40ba6217742846ab06 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 18:51:30 +0200 Subject: [PATCH 57/86] up --- to_check | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/to_check b/to_check index 2f71539..6f2d1bf 100644 --- a/to_check +++ b/to_check @@ -65,22 +65,3 @@ valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --r > /^Cdir supp > exit > exit - -- on heredoc interrupted with SIGINT - > cat << 0 - ==140494== Warning: noted but unhandled ioctl 0x5412 with no size/direction hints. - ==140494== This could cause spurious value errors to appear. - ==140494== See README_MISSING_SYSCALL_OR_IOCTL for guidance on writing a proper wrapper. - - > - exit - ==140494== - ==140494== FILE DESCRIPTORS: 4 open (3 std) at exit. - ==140494== Open file descriptor 3: - ==140494== at 0x4AD007D: pipe (pipe.c:29) - ==140494== by 0x408FF1: checker_heredoc (heredoc.c:76) - ==140494== by 0x4090C7: heredoc (heredoc.c:101) - ==140494== by 0x401FDE: here_exec (main.c:59) - ==140494== by 0x4020D6: main_loop (main.c:92) - ==140494== by 0x402155: main (main.c:108) - ==140494== From 21df46ff672aab683f86ced2b52c8fda7bcc3cdb Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 19:57:36 +0200 Subject: [PATCH 58/86] corrected prev value --- exec/built_unset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exec/built_unset.c b/exec/built_unset.c index ec00ce1..69a7dde 100644 --- a/exec/built_unset.c +++ b/exec/built_unset.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 16:03:36 by grebrune #+# #+# */ -/* Updated: 2024/06/24 16:53:16 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 19:55:05 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,7 @@ void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) { remove = current->next; current->next = current->next->next; + current->next->prev = current; free(remove->name); free(remove->value); free(remove); From 2f8e28e1b3c2f5e72eb082bd2f6fbdba2167c3ce Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 20:13:12 +0200 Subject: [PATCH 59/86] unauthorized function --- exec/exec_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/exec_utils.c b/exec/exec_utils.c index 4053fee..c49f789 100644 --- a/exec/exec_utils.c +++ b/exec/exec_utils.c @@ -22,7 +22,7 @@ char *find_path(t_head *head) { if (ft_strcmp("PATH", copy->name) == 0) { - path = strdup(copy->value); + path = ft_strdup(copy->value); return (path); } copy = copy->next; @@ -37,7 +37,7 @@ char *join_with_char(char const *s1, char const *s2, char c) size_t j; char *str; - len = strlen((char *)s1) + strlen((char *)s2); + len = ft_strlen((char *)s1) + ft_strlen((char *)s2); i = 0; str = malloc (sizeof(char) * (len + 2)); if (str == NULL) From 5a9e7753d0d4f56a433b46b111fc906593a08b21 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 20:17:29 +0200 Subject: [PATCH 60/86] correction to do in file to_check --- exec/built_cd.c | 9 +++++---- exec/built_cd_back.c | 6 +++--- exec/built_exit_free.c | 2 +- exec/built_pwd.c | 4 ++-- exec/heredoc.c | 2 +- includes/builtins.h | 2 +- to_check | 23 ----------------------- 7 files changed, 13 insertions(+), 35 deletions(-) diff --git a/exec/built_cd.c b/exec/built_cd.c index 8a3b371..64b7bd1 100644 --- a/exec/built_cd.c +++ b/exec/built_cd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:49:11 by grebrune #+# #+# */ -/* Updated: 2024/07/08 18:29:32 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 20:13:12 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ int ft_cd(t_head *head) return (2); err = get_path(&old); if (err == 2) - exit_free(head, 0); + return (2); err = chdir(new); if (err != 0) return (cd_chdir_error(head, old, new), 1); @@ -66,7 +66,7 @@ char *cd_relative(t_head *head) return (cd_find_var(head, "HOME")); if (0 == ft_strcmp(head->cmd->arg[1], "../") || 0 == ft_strcmp(head->cmd->arg[1], "..")) - return (cd_back_trim(head)); + return (cd_back_trim()); if (0 == ft_strcmp(head->cmd->arg[1], "~/") || 0 == ft_strcmp(head->cmd->arg[1], "~")) return (cd_tild_trim(head)); @@ -76,7 +76,8 @@ char *cd_relative(t_head *head) return (cat_of_tild(head, head->cmd->arg[1])); if (!ft_strncmp(head->cmd->arg[1], "/home", 5)) return (ft_strdup(head->cmd->arg[1])); - get_path(&new); + if (get_path(&new) == 2) + return (NULL); new = cd_cat_backslash(new, head->cmd->arg[1]); if (!new) return (write(2, "Crash of Malloc\n", 16), NULL); diff --git a/exec/built_cd_back.c b/exec/built_cd_back.c index 1400097..009982a 100644 --- a/exec/built_cd_back.c +++ b/exec/built_cd_back.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 14:35:38 by grebrune #+# #+# */ -/* Updated: 2024/07/08 17:39:15 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 20:13:12 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ char *cd_tild_trim(t_head *head) return (free(pwd), new); } -char *cd_back_trim(t_head *head) +char *cd_back_trim(void) { char *pwd; char *new; @@ -36,7 +36,7 @@ char *cd_back_trim(t_head *head) i = 0; pwd = NULL; if (get_path(&pwd) == 2) - exit_free(head, 0); + return (NULL); while (pwd && pwd[i]) i++; while (pwd && pwd[i] != '/') diff --git a/exec/built_exit_free.c b/exec/built_exit_free.c index 63e5152..7c90b29 100644 --- a/exec/built_exit_free.c +++ b/exec/built_exit_free.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 17:13:12 by grebrune #+# #+# */ -/* Updated: 2024/06/28 17:21:12 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 18:40:36 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/exec/built_pwd.c b/exec/built_pwd.c index 827cb5e..b4da395 100644 --- a/exec/built_pwd.c +++ b/exec/built_pwd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ -/* Updated: 2024/07/08 16:11:24 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 16:15:58 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void ft_pwd(t_head *head) str = NULL; err = get_path(&str); if (err == 2) - exit_free(head, 2); + return ; if (str) printf("%s\n", str); free(str); diff --git a/exec/heredoc.c b/exec/heredoc.c index 53cb975..d9f00be 100644 --- a/exec/heredoc.c +++ b/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/17 17:45:32 by grebrune #+# #+# */ -/* Updated: 2024/07/02 21:17:45 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 18:37:36 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/builtins.h b/includes/builtins.h index 6572aea..c43403f 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -32,7 +32,7 @@ char *cd_find_var(t_head *head, char *name); char *cd_cat_backslash(char *path, char *dir); //cd_back char *cd_tild_trim(t_head *head); -char *cd_back_trim(t_head *head); +char *cd_back_trim(void); void cd_chdir_error(t_head *head, char *old_pwd, char *new_pwd); void cd_not_found(char *name); diff --git a/to_check b/to_check index 6f2d1bf..c241e96 100644 --- a/to_check +++ b/to_check @@ -8,29 +8,6 @@ valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --r - env alone prints env; - env in pipe (> env | /bin/grep "PATH") says : env: No such file or directory -- on unset USER, cd ~ : - -> cd ~ - bash: cd: USER not set - > echo $? - ==139176== Invalid read of size 8 - -- cd doesnt accept absolute path from root (?) -> cd - > pwd - /home/grebrune - > cd /home/grebrune/Documents/github/4_Circle/minishell - bash: cd: No such file or directory - > - -- expand segfaults on unset env variable - > echo $OLDPWD - /home/grebrune - > unset OLDPWD - > echo $OLDPWD - [1] 139575 segmentation fault (core dumped) ./minishell - ==139826== Invalid read of size 8 - ==139826== at 0x40181C: var_value (expand_var_utils.c:72) - - mystery child does not die ld: No such file or directory > ls From f0c6fb84606d10e88a00426021da52f8cb99b068 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 20:18:10 +0200 Subject: [PATCH 61/86] supp supp --- supp.supp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 supp.supp diff --git a/supp.supp b/supp.supp new file mode 100644 index 0000000..51566b1 --- /dev/null +++ b/supp.supp @@ -0,0 +1,13 @@ +{ + ignore_libreadline_leaks + Memcheck:Leak + ... + obj:*/libreadline.so.* +} + +{ + leaks bash + Memcheck:Leak + ... + obj:*/usr/bin* +} \ No newline at end of file From b427d8b8e39e50de0ac6836d72c29682539a1459 Mon Sep 17 00:00:00 2001 From: grebrune Date: Mon, 8 Jul 2024 20:19:13 +0200 Subject: [PATCH 62/86] supp supp --- to_check | 3 +++ 1 file changed, 3 insertions(+) diff --git a/to_check b/to_check index c241e96..9f1a6e6 100644 --- a/to_check +++ b/to_check @@ -1,6 +1,9 @@ valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --read-var-info=yes -s --trace-children=yes ./minishell +/bin/valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes --suppressions=supp.supp ./minishell + +------------------------------------------------------------------------------------ - Message d'erreur si infile inexistant : - < Makefile > out < in < Makefile > out1 cat From 4c68218eb983c0a5668362c147fc4f738b6ff708 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 21:13:52 +0200 Subject: [PATCH 63/86] find path env check exist --- exec/exec_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec/exec_utils.c b/exec/exec_utils.c index c49f789..22245b3 100644 --- a/exec/exec_utils.c +++ b/exec/exec_utils.c @@ -18,7 +18,7 @@ char *find_path(t_head *head) char *path; copy = head->env; - while (copy->next != NULL) + while (copy) { if (ft_strcmp("PATH", copy->name) == 0) { From 158f76bc46f8c23a383ddda70302f2c781566e2c Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 21:30:57 +0200 Subject: [PATCH 64/86] return value --- exec/built_exit.c | 4 +++- exec/exec_wait.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/exec/built_exit.c b/exec/built_exit.c index 0736b06..d1becf0 100644 --- a/exec/built_exit.c +++ b/exec/built_exit.c @@ -47,8 +47,10 @@ int ft_strnum(char *str) int i; i = 0; - if (str && !str[0]) + if (!str || (str && !str[0])) return (0); + if (str[i] == '+' || str[i] == '-') + i++; while (str && str[i]) { if (!(str[i] >= 48 && str[i] <= 57)) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index 339211a..13416f8 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -24,7 +24,7 @@ int wait_for_all(pid_t *pid, int x) while (i < x) { last = waitpid(0, &wstatus[0], 0); - if (last == pid[x - 1]) + if (last != -1 && last == pid[x - 1]) wstatus[1] = wstatus[0]; i++; } From 942a9c8c8cbb79aacad5511109f1388e2a73e50c Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 21:31:57 +0200 Subject: [PATCH 65/86] return value --- exec/built_export.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec/built_export.c b/exec/built_export.c index 71f1863..186faab 100644 --- a/exec/built_export.c +++ b/exec/built_export.c @@ -35,7 +35,7 @@ int ft_export(t_head *head) int export_bis(t_head *head, t_env *c_env, size_t i) { if (check_name(head, head->cmd->arg[i], c_env)) - return (0); + return (g_error = 1, 0); if (no_plus(head->cmd->arg[i])) { if (export_search_env(c_env, head->cmd->arg[i]) == 0) From af4151d98f0e9772a65a84c865a3bd8dce9f95f2 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 22:08:32 +0200 Subject: [PATCH 66/86] exit correctly --- exec/built_env.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/exec/built_env.c b/exec/built_env.c index 1f4e42e..ef3adc3 100644 --- a/exec/built_env.c +++ b/exec/built_env.c @@ -18,7 +18,7 @@ void ft_env(t_head *head) int i; if (!head->env) - return ; + exit_free(head, 0); copy = head->env; i = checker_env(head->cmd->arg); if (i != 0) @@ -26,9 +26,7 @@ void ft_env(t_head *head) write(2, "env: '", 6); write(2, head->cmd->arg[i], ft_strlen(head->cmd->arg[i])); write(2, "’: No such file or directory\n", 31); - free_fnp(head, head->fnp); - ft_free_all(head); - exit (0); + exit_free(head, 0); } while (copy->next) { From 48917e2e1c439d122b9b76095d963cc9150bb64d Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 22:08:55 +0200 Subject: [PATCH 67/86] envlen corrected --- exec/exec_wait.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index 13416f8..7852fd0 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ -/* Updated: 2024/07/02 21:16:03 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 21:15:06 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,9 +47,7 @@ size_t envlen(t_env *base) copy = base; size = 0; - if (copy != NULL) - size++; - while (copy->next != NULL) + while (copy) { size++; copy = copy->next; From a1ed83ae9cf5e5b24231ace6e8b571d408aa70ba Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 22:09:41 +0200 Subject: [PATCH 68/86] str initialized --- srcs/init_utils2.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/srcs/init_utils2.c b/srcs/init_utils2.c index bb1dc79..0aff683 100644 --- a/srcs/init_utils2.c +++ b/srcs/init_utils2.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 20:10:22 by grebrune #+# #+# */ -/* Updated: 2024/06/26 20:10:22 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 21:58:20 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,11 @@ t_env *env_if_empty(void) char *str; env = NULL; - get_path(&str); + str = NULL; + if (get_path(&str) == 2) + return (NULL); new = ft_env_new_bis("PWD", str); - ft_free(str); +// ft_free(str); if (new == NULL) return (NULL); ft_envadd_back(&env, new); From e1bbcd0707ad8c7cd94fee7245f9db4c935fd334 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Mon, 8 Jul 2024 22:27:09 +0200 Subject: [PATCH 69/86] up if env empty & prev value --- exec/built_unset.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/exec/built_unset.c b/exec/built_unset.c index 69a7dde..49d4944 100644 --- a/exec/built_unset.c +++ b/exec/built_unset.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 16:03:36 by grebrune #+# #+# */ -/* Updated: 2024/07/08 19:55:05 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 22:26:11 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,6 +38,14 @@ void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) { remove = current->next; current->next = current->next->next; + if (!current->next) + { + current->next = NULL; + free(remove->name); + free(remove->value); + free(remove); + return ; + } current->next->prev = current; free(remove->name); free(remove->value); @@ -49,6 +57,8 @@ void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) if (current && (*cmp)(current->name, ref) == 0) { *env = current->next; + if (current->next) + current->next->prev = NULL; free(current->value); free(current->name); free(current); From e2ceff801de61cc7e57a68ce4384adcc5034d272 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:29:52 +0200 Subject: [PATCH 70/86] cmdlen mod make arg malloc protect --- exec/exec_wait.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exec/exec_wait.c b/exec/exec_wait.c index 7852fd0..d556051 100644 --- a/exec/exec_wait.c +++ b/exec/exec_wait.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/30 18:51:28 by grebrune #+# #+# */ -/* Updated: 2024/07/08 21:15:06 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 01:26:35 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,9 +62,7 @@ size_t cmdlen(t_cmd *base) copy = base; size = 0; - if (copy != NULL) - size++; - while (copy->next != NULL) + while (copy) { size++; copy = copy->next; @@ -87,6 +85,8 @@ char **make_arg(t_cmd *cmd) while (cmd->arg && cmd->arg[x]) { arg[x] = ft_strdup(cmd->arg[x]); + if (!arg[x]) + return (ft_splitdestroy(arg), NULL); x++; } arg[x] = NULL; From a5d3f06cb388a6db8b3e0a2c48c2eb28038224d1 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:30:22 +0200 Subject: [PATCH 71/86] mod exec_error_exit --- includes/builtins.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index c43403f..0878c70 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/08 17:31:52 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 00:58:08 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ char *value_of_name(t_env *env, char *name); void ft_unset(t_head *head); void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)); //exec_error -void exec_error_exit(char **arg, char *str, char **env, char **path); +void exec_error_exit(char **arg, char **env, char **path); //exec_main void exec_shell(char **arg, char *str, char **env); int find_cmd(t_head *head, t_cmd *copy, t_fnp *fnp, int x); @@ -98,7 +98,7 @@ int executable(t_head *head); char *find_path(t_head *head); char *join_with_char(char const *s1, char const *s2, char c); char **make_env(t_env *env); -void no_path_to_hapiness(t_head *head, char **env, char **tab); +void no_path_to_hapiness(t_head *head, char **env, char **tab, t_cmd *copy); void free_malloc_fnp(t_head *head, int to_free); //exec_wait int wait_for_all(pid_t *pid, int x); From 3b76c3cb1b3a6ecc8236daf3dabd37c65ba6ea46 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:30:38 +0200 Subject: [PATCH 72/86] mod exec_error_exit --- exec/exec_error.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/exec/exec_error.c b/exec/exec_error.c index ffb2322..e0fbb70 100644 --- a/exec/exec_error.c +++ b/exec/exec_error.c @@ -12,14 +12,15 @@ #include "../includes/minishell.h" -void exec_error_exit(char **arg, char *str, char **env, char **path) +void exec_error_exit(char **arg, char **env, char **path) { write(2, "bash: ", 6); perror(arg[0]); - free_tab(env); - free_tab(path); + if (env) + free_tab(env); + if (path) + free_tab(path); free_tab(arg); - ft_free(str); if (errno == EACCES) exit (126); else From 3934a471062e9c77e0703aab0f1398a9b9f941db Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:33:39 +0200 Subject: [PATCH 73/86] mod exec_error_exit --- exec/exec_error.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/exec/exec_error.c b/exec/exec_error.c index e0fbb70..59852e3 100644 --- a/exec/exec_error.c +++ b/exec/exec_error.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/02 19:44:43 by grebrune #+# #+# */ -/* Updated: 2024/07/02 20:24:11 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 01:14:30 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ void exec_error_exit(char **arg, char **env, char **path) free_tab(env); if (path) free_tab(path); - free_tab(arg); + if (arg) + free_tab(arg); if (errno == EACCES) exit (126); else From c34025a0cb39ab686e0d4788aeb8dc69c5ddf229 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:34:01 +0200 Subject: [PATCH 74/86] mod exec_error_exit & protect of malloc --- exec/exec_main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/exec/exec_main.c b/exec/exec_main.c index 8d93fbf..79de611 100644 --- a/exec/exec_main.c +++ b/exec/exec_main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 16:34:19 by grebrune #+# #+# */ -/* Updated: 2024/07/08 17:32:16 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 01:23:33 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,9 @@ void exec_shell(char **arg, char *str, char **env) char *cmd; path = ft_split(str, ':'); + ft_free(str); + if (!path) + exec_error_exit(arg, env, path); i = 0; while (path[i]) { @@ -30,7 +33,7 @@ void exec_shell(char **arg, char *str, char **env) } if (!(access(arg[0], F_OK)) && !(access(arg[0], X_OK))) execve(arg[0], arg, env); - exec_error_exit(arg, str, env, path); + exec_error_exit(arg, env, path); } int no_fork_cmd(t_head *head, t_cmd *copy, char *str) @@ -55,17 +58,14 @@ int exec_shell_malloc(t_head *head, t_cmd *copy, t_fnp *fnp) char *path; env = make_env(head->env); + if (!env) + exit_free(head, 1); tab = make_arg(copy); - if (!env || !tab) - { - if (!env) - return (free(tab), ft_free_all(head), 1); - else - return (free(env), ft_free_all(head), 1); - } + if (!tab) + return (free_tab(env), exit_free(head, 1), 0); path = find_path(head); if (!path) - no_path_to_hapiness(head, env, tab); + no_path_to_hapiness(head, env, tab, copy); free_fnp(head, fnp); ft_free_all(head); exec_shell(tab, path, env); From 5fc4c8fd85af41018dc097083b8f814154fe910a Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 01:55:34 +0200 Subject: [PATCH 75/86] rework no_path_to_hapiness --- exec/exec_utils.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/exec/exec_utils.c b/exec/exec_utils.c index 22245b3..e4334db 100644 --- a/exec/exec_utils.c +++ b/exec/exec_utils.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/22 17:31:24 by grebrune #+# #+# */ -/* Updated: 2024/07/02 14:43:43 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 01:14:30 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,6 +66,8 @@ char **make_env(t_env *env) size_t i; size = envlen(env); + if (size == 0) + return (NULL); tab = malloc(sizeof (char *) * (size + 1)); if (tab == NULL) return (NULL); @@ -74,8 +76,8 @@ char **make_env(t_env *env) while (i < size) { tab[i] = join_with_char(copy->name, copy->value, '='); - if (tab[i] == NULL) - return (NULL); + if (!tab[i]) + return (ft_splitdestroy(tab), NULL); i++; copy = copy->next; } @@ -83,17 +85,25 @@ char **make_env(t_env *env) return (tab); } -void no_path_to_hapiness(t_head *head, char **env, char **tab) +void no_path_to_hapiness(t_head *head, char **env, char **tab, t_cmd *copy) { - if (!(access(head->cmd->arg[0], F_OK)) - && !(access(head->cmd->arg[0], X_OK))) - execve(head->cmd->arg[0], head->cmd->arg, env); - perror(head->cmd->arg[0]); - free_tab(env); - free_tab(tab); + char *exec; + + exec = ft_strdup(copy->arg[0]); + if (!exec) + exec_error_exit(tab, env, NULL); free_fnp(head, head->fnp); ft_free_all(head); - exit (127); + if (!(access(exec, F_OK)) && !(access(exec, X_OK))) + execve(exec, tab, env); + perror(exec); + free_tab(env); + free_tab(tab); + ft_free(exec); + if (errno == EACCES) + exit (126); + else + exit (127); } void free_malloc_fnp(t_head *head, int to_free) From 04c272ccb5854aa5d45d3e46e7dc7646be604a71 Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 02:01:04 +0200 Subject: [PATCH 76/86] added --- to_check | 39 +-------------------------------------- 1 file changed, 1 insertion(+), 38 deletions(-) diff --git a/to_check b/to_check index 9f1a6e6..d36aedf 100644 --- a/to_check +++ b/to_check @@ -7,41 +7,4 @@ valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --r - Message d'erreur si infile inexistant : - < Makefile > out < in < Makefile > out1 cat -- on unset PATH in bash, then launch minishell ; - - env alone prints env; - - env in pipe (> env | /bin/grep "PATH") says : env: No such file or directory - -- mystery child does not die - ld: No such file or directory - > ls - ls: No such file or directory - > echo $? - 127 - > env - PWD=/home/grebrune/Documents/github/4_Circle/minishell - SHLVL=1 - > unset PWD SHLVL - > env - > echo $USER - - > echo $? - 0 - > export 'a=ehehe' b=super c=quel_fun - > env - a=ehehe - b=super - c=quel_fun - > env | /bin/wc -l - env: No such file or directory - > echo $? - 127 - > env - a=ehehe - b=super - c=quel_fun - > - > mkdir supp - mkdir: No such file or directory - > /^Cdir supp - > exit - > exit +l'affichage du message d'erreur se fait dans open files. Des testes sont a faire \ No newline at end of file From 850f400db27e83b22a3eb3b314f64b20be6f293e Mon Sep 17 00:00:00 2001 From: Grgoirebrn Date: Tue, 9 Jul 2024 02:01:27 +0200 Subject: [PATCH 77/86] added --- exec/built_pwd.c | 2 +- exec/exec_redir.c | 8 ++++---- includes/builtins.h | 4 ++-- srcs/init.c | 2 +- srcs/main.c | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/exec/built_pwd.c b/exec/built_pwd.c index b4da395..1933654 100644 --- a/exec/built_pwd.c +++ b/exec/built_pwd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ -/* Updated: 2024/07/08 16:15:58 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 21:57:10 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/exec/exec_redir.c b/exec/exec_redir.c index 3b14651..d7486c2 100644 --- a/exec/exec_redir.c +++ b/exec/exec_redir.c @@ -43,7 +43,7 @@ int open_the_pipe(int **pipe, t_head *head) return (0); } -int open_files(t_redir *redir, int err_print) +int open_files(t_redir *redir, int *err_print) { if (redir->type == 1) redir->fd = open(redir->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -53,8 +53,9 @@ int open_files(t_redir *redir, int err_print) redir->fd = open(redir->arg, O_RDONLY); if (redir->fd == -1) { - if (err_print != 0) + if (*err_print == 0) perror(redir->arg); + *err_print = 1; return (2); } return (0); @@ -75,7 +76,7 @@ int open_redir(t_cmd *copy, int fd[2]) parser = c_cmd->redir; while (parser) { - if (open_files(parser, err_print)) + if (open_files(parser, &err_print)) return (2); if (parser->type == 1 || parser->type == 2) fd[1] = parser->fd; @@ -84,7 +85,6 @@ int open_redir(t_cmd *copy, int fd[2]) parser = parser->next; } c_cmd = c_cmd->next; - err_print++; } return (0); } diff --git a/includes/builtins.h b/includes/builtins.h index 0878c70..8bf1c93 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/26 19:52:23 by grebrune #+# #+# */ -/* Updated: 2024/07/09 00:58:08 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 01:14:30 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -110,7 +110,7 @@ char **make_arg(t_cmd *cmd); //exec_redir void fail_malloc(t_head *head, int **fd, size_t i); int open_the_pipe(int **fd, t_head *head); -int open_files(t_redir *redir, int err_print); +int open_files(t_redir *redir, int *err_print); int open_redir(t_cmd *copy, int fd[2]); //exec_redir_utils diff --git a/srcs/init.c b/srcs/init.c index 1566eb5..54f98dc 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/18 17:14:37 by beroy #+# #+# */ -/* Updated: 2024/06/26 19:38:31 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 21:45:22 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/main.c b/srcs/main.c index d266909..f868bce 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/11 16:11:53 by beroy #+# #+# */ -/* Updated: 2024/07/08 17:31:02 by grebrune ### ########.fr */ +/* Updated: 2024/07/08 21:44:00 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ From 60d59c204a7040f361474cf81863015f24dac4a4 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 12:57:25 +0200 Subject: [PATCH 78/86] remove unused code --- exec/built_echo.c | 7 ------- exec/built_pwd.c | 18 ++---------------- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/exec/built_echo.c b/exec/built_echo.c index d9f8417..951270f 100644 --- a/exec/built_echo.c +++ b/exec/built_echo.c @@ -64,13 +64,6 @@ void ft_echo(t_head *head, t_cmd *copy) i++; n++; } - if (ft_strcmp("$?", copy->arg[i]) == 0) - { - printf("%d", g_error); - if (n == 0) - printf("\n"); - exit(0); - } print_tab(head, copy->arg, i, n); } diff --git a/exec/built_pwd.c b/exec/built_pwd.c index 1933654..2a2509d 100644 --- a/exec/built_pwd.c +++ b/exec/built_pwd.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 15:48:06 by grebrune #+# #+# */ -/* Updated: 2024/07/08 21:57:10 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 12:50:59 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void ft_pwd(t_head *head) str = NULL; err = get_path(&str); if (err == 2) - return ; + exit_free(head, 1); if (str) printf("%s\n", str); free(str); @@ -46,17 +46,3 @@ int get_path(char **str) } return (0); } - -char *value_of_name(t_env *env, char *name) -{ - t_env *copy; - - copy = env; - while (copy) - { - if (0 == ft_strcmp(copy->name, name)) - return (copy->value); - copy = copy->next; - } - return (NULL); -} From b5ec4d3a77ee5d38f0c9bf7dece9b4e396565aeb Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 13:00:56 +0200 Subject: [PATCH 79/86] splitdestroy --- exec/built_export_no_arg.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/exec/built_export_no_arg.c b/exec/built_export_no_arg.c index 733647b..ac077c9 100644 --- a/exec/built_export_no_arg.c +++ b/exec/built_export_no_arg.c @@ -18,25 +18,19 @@ int ex_no_args(t_head *head) t_env *copy; if (!head->env) - { - free_fnp(head, head->fnp); - ft_free_all(head); - exit(0); - } + exit_free(head, 0); copy = head->env; tab = env_to_tab(copy); if (!tab) { write(2, "Crash of Malloc\n", 16); - ft_exit(head); + exit_free(head, 0); } swap_this_tab(tab); printf_tab(tab); free_tab(tab); close_pipe(); - free_fnp(head, head->fnp); - ft_free_all(head); - exit(0); + exit_free(head, 0); } char **env_to_tab(t_env *copy) @@ -52,7 +46,7 @@ char **env_to_tab(t_env *copy) { tab[x] = join_equal(copy->name, copy->value); if (!tab[x]) - return (free_tab(tab), NULL); + return (ft_splitdestroy(tab), NULL); x++; copy = copy->next; } From 5f6c0c2032dea1579144fd20f41e2c037baddb11 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 13:02:32 +0200 Subject: [PATCH 80/86] protection --- exec/built_env.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec/built_env.c b/exec/built_env.c index ef3adc3..89cf769 100644 --- a/exec/built_env.c +++ b/exec/built_env.c @@ -28,7 +28,7 @@ void ft_env(t_head *head) write(2, "’: No such file or directory\n", 31); exit_free(head, 0); } - while (copy->next) + while (copy && copy->next) { printf("%s=%s\n", copy->name, copy->value); copy = copy->next; From 9445ecbdcfa80d56486e5e0b16a8036525631695 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 14:05:03 +0200 Subject: [PATCH 81/86] protection --- exec/built_export_no_arg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exec/built_export_no_arg.c b/exec/built_export_no_arg.c index ac077c9..c19d00f 100644 --- a/exec/built_export_no_arg.c +++ b/exec/built_export_no_arg.c @@ -30,7 +30,7 @@ int ex_no_args(t_head *head) printf_tab(tab); free_tab(tab); close_pipe(); - exit_free(head, 0); + return (exit_free(head, 0), 0); } char **env_to_tab(t_env *copy) From 8bc519701b66472bb267a16b13f3a1b239dfac5c Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 14:18:20 +0200 Subject: [PATCH 82/86] unset to do --- exec/built_unset.c | 48 +++++++++++++++++++++++----------------------- srcs/main.c | 20 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/exec/built_unset.c b/exec/built_unset.c index 49d4944..788f7ea 100644 --- a/exec/built_unset.c +++ b/exec/built_unset.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 16:03:36 by grebrune #+# #+# */ -/* Updated: 2024/07/08 22:26:11 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 14:16:20 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,41 +26,41 @@ void ft_unset(t_head *head) } } +void free_rem(t_env *remove) +{ + ft_free(remove->name); + ft_free(remove->value); + ft_free(remove); +} + void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) { t_env *remove; t_env *current; current = *env; - while (current && current->next) + while (current) { - if ((*cmp)(current->next->name, ref) == 0) + if (!(*cmp)(current->name, ref)) { - remove = current->next; - current->next = current->next->next; - if (!current->next) + remove = current; + current->prev = current->prev->prev; + free_rem(remove); + current = current->next; + if (!current) { - current->next = NULL; - free(remove->name); - free(remove->value); - free(remove); + current = NULL; return ; } - current->next->prev = current; - free(remove->name); - free(remove->value); - free(remove); } current = current->next; } - current = *env; - if (current && (*cmp)(current->name, ref) == 0) - { - *env = current->next; - if (current->next) - current->next->prev = NULL; - free(current->value); - free(current->name); - free(current); - } +// current = *env; +// if (current && (*cmp)(current->name, ref) == 0) +// { +// *env = current->next; +// if (current->next) +// current->next->prev = NULL; +// free_rem(current); +// } } diff --git a/srcs/main.c b/srcs/main.c index f868bce..91ed932 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -31,21 +31,21 @@ int single_exec(t_head *head, char *input) int open_on_err(t_cmd *cmd) { t_cmd *c_cmd; - t_redir *c_red; + t_redir *red; c_cmd = cmd; while (c_cmd) { - c_red = c_cmd->redir; - while (c_red) + red = c_cmd->redir; + while (red) { - if (c_red->type == 1) - c_red->fd = open(c_red->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (c_red->type == 2) - c_red->fd = open(c_red->arg, O_WRONLY | O_CREAT | O_APPEND, 0644); - if ((c_red->type == 2 || c_red->type == 1) && c_red->fd == -1) - return (perror(c_red->arg), 2); - c_red = c_red->next; + if (red->type == 1) + red->fd = open(red->arg, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (red->type == 2) + red->fd = open(red->arg, O_WRONLY | O_CREAT | O_APPEND, 0644); + if ((red->type == 2 || red->type == 1) && red->fd == -1) + return (perror(red->arg), 2); + red = red->next; } c_cmd = c_cmd->next; } From 6a2080e94a0bb084d548b0eceeb7a59fd8fae263 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 16:17:30 +0200 Subject: [PATCH 83/86] unset REWORK --- exec/built_unset.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/exec/built_unset.c b/exec/built_unset.c index 788f7ea..409ef35 100644 --- a/exec/built_unset.c +++ b/exec/built_unset.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/14 16:03:36 by grebrune #+# #+# */ -/* Updated: 2024/07/09 14:16:20 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 16:15:07 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,32 +35,27 @@ void free_rem(t_env *remove) void rem_env(t_env **env, void *ref, int (*cmp)(char *, const char *)) { - t_env *remove; - t_env *current; + t_env *tmp; - current = *env; - while (current) + tmp = *env; + if ((*cmp)((*env)->name, ref) == 0) { - if (!(*cmp)(current->name, ref)) + *env = (*env)->next; + (*env)->prev = NULL; + return (free_rem(tmp), (void)0); + } + while (tmp) + { + if ((*cmp)(tmp->name, ref) == 0) { - remove = current; - current->prev = current->prev->prev; - free_rem(remove); - current = current->next; - if (!current) - { - current = NULL; - return ; - } + if (tmp->prev) + tmp->prev->next = tmp->next; + if (tmp->next) + tmp->next->prev = tmp->prev; + return (free_rem(tmp), (void)0); } - current = current->next; + if (tmp->next == NULL) + break ; + tmp = tmp->next; } -// current = *env; -// if (current && (*cmp)(current->name, ref) == 0) -// { -// *env = current->next; -// if (current->next) -// current->next->prev = NULL; -// free_rem(current); -// } } From 1de08f58bcf310afd76310f7b6ade44c12604c93 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 18:40:41 +0200 Subject: [PATCH 84/86] underscore accepted --- exec/built_export_checker.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exec/built_export_checker.c b/exec/built_export_checker.c index 1268025..a19cbc0 100644 --- a/exec/built_export_checker.c +++ b/exec/built_export_checker.c @@ -6,7 +6,7 @@ /* By: grebrune +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/21 16:32:41 by grebrune #+# #+# */ -/* Updated: 2024/06/24 17:17:53 by grebrune ### ########.fr */ +/* Updated: 2024/07/09 18:36:17 by grebrune ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ int check_name(t_head *head, char *name, t_env *c_env) { if (name[i] == '+' && name[i + 1] == '=') return (add_var(head, name, c_env), 0); - if (ft_isalnum(name[i]) == 0) + if (!ft_isalnum(name[i]) && name[i] != '_') return (error_handle(name), 1); i++; } From d95bfe84b3e27f0a93365b97294eaca393bc2b56 Mon Sep 17 00:00:00 2001 From: grebrune Date: Tue, 9 Jul 2024 18:48:47 +0200 Subject: [PATCH 85/86] remove free(str) --- srcs/init_utils2.c | 1 - 1 file changed, 1 deletion(-) diff --git a/srcs/init_utils2.c b/srcs/init_utils2.c index 0aff683..ae53df6 100644 --- a/srcs/init_utils2.c +++ b/srcs/init_utils2.c @@ -23,7 +23,6 @@ t_env *env_if_empty(void) if (get_path(&str) == 2) return (NULL); new = ft_env_new_bis("PWD", str); -// ft_free(str); if (new == NULL) return (NULL); ft_envadd_back(&env, new); From b2efe6f3e5a96c8f185ddc5c9f7d209cfa978e5f Mon Sep 17 00:00:00 2001 From: grebrune Date: Wed, 10 Jul 2024 15:25:20 +0200 Subject: [PATCH 86/86] push for push --- supp.supp | 13 ------------- to_check | 10 ---------- 2 files changed, 23 deletions(-) delete mode 100644 supp.supp delete mode 100644 to_check diff --git a/supp.supp b/supp.supp deleted file mode 100644 index 51566b1..0000000 --- a/supp.supp +++ /dev/null @@ -1,13 +0,0 @@ -{ - ignore_libreadline_leaks - Memcheck:Leak - ... - obj:*/libreadline.so.* -} - -{ - leaks bash - Memcheck:Leak - ... - obj:*/usr/bin* -} \ No newline at end of file diff --git a/to_check b/to_check deleted file mode 100644 index d36aedf..0000000 --- a/to_check +++ /dev/null @@ -1,10 +0,0 @@ - -valgrind --leak-check=full --show-leak-kinds=all --show-mismatched-frees=yes --read-var-info=yes -s --trace-children=yes ./minishell - -/bin/valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes --suppressions=supp.supp ./minishell - ------------------------------------------------------------------------------------- -- Message d'erreur si infile inexistant : - - < Makefile > out < in < Makefile > out1 cat - -l'affichage du message d'erreur se fait dans open files. Des testes sont a faire \ No newline at end of file