├── Makefile ├── README.md ├── ToDoList.txt ├── fr.subject.pdf ├── includes ├── libshell.h ├── struct.h └── termcap.h ├── lib ├── get_next_line │ ├── get_next_line.c │ ├── get_next_line.h │ └── get_next_line_utils.c └── libft │ ├── Makefile │ ├── ft_abs.c │ ├── ft_atoi.c │ ├── ft_atoi_base.c │ ├── ft_bzero.c │ ├── ft_calloc.c │ ├── ft_free_strjoin.c │ ├── ft_isalnum.c │ ├── ft_isalpha.c │ ├── ft_isascii.c │ ├── ft_isdigit.c │ ├── ft_islower.c │ ├── ft_isprecision.c │ ├── ft_isprint.c │ ├── ft_isupper.c │ ├── ft_itoa.c │ ├── ft_lstadd_back_bonus.c │ ├── ft_lstadd_front_bonus.c │ ├── ft_lstclear_bonus.c │ ├── ft_lstdelone_bonus.c │ ├── ft_lstiter_bonus.c │ ├── ft_lstlast_bonus.c │ ├── ft_lstmap_bonus.c │ ├── ft_lstnew_bonus.c │ ├── ft_lstsize_bonus.c │ ├── ft_ltohex.c │ ├── ft_ltohex_spe.c │ ├── ft_max.c │ ├── ft_memccpy.c │ ├── ft_memchr.c │ ├── ft_memcmp.c │ ├── ft_memcpy.c │ ├── ft_memdel.c │ ├── ft_memmove.c │ ├── ft_memset.c │ ├── ft_min.c │ ├── ft_newstr.c │ ├── ft_putchar.c │ ├── ft_putchar_fd.c │ ├── ft_putendl_fd.c │ ├── ft_putnbr.c │ ├── ft_putnbr_fd.c │ ├── ft_putspace.c │ ├── ft_putstr.c │ ├── ft_putstr_fd.c │ ├── ft_putzero.c │ ├── ft_split.c │ ├── ft_stohex.c │ ├── ft_strchr.c │ ├── ft_strcmp.c │ ├── ft_strcpy.c │ ├── ft_strdel.c │ ├── ft_strdup.c │ ├── ft_strjoin.c │ ├── ft_strlcat.c │ ├── ft_strlcpy.c │ ├── ft_strlen.c │ ├── ft_strmapi.c │ ├── ft_strncat.c │ ├── ft_strncmp.c │ ├── ft_strnstr.c │ ├── ft_strrchr.c │ ├── ft_strtoupper.c │ ├── ft_strtrim.c │ ├── ft_substr.c │ ├── ft_tolower.c │ ├── ft_toupper.c │ ├── ft_unsigned.c │ ├── libft.a │ └── libft.h ├── minishell ├── minishell.dSYM └── Contents │ ├── Info.plist │ └── Resources │ └── DWARF │ └── minishell ├── srcs ├── builtins │ ├── export │ │ ├── export_check.c │ │ ├── my_export.c │ │ └── print_export.c │ ├── handler_builtins.c │ ├── my_cd.c │ ├── my_echo.c │ ├── my_env.c │ ├── my_exit.c │ ├── my_pwd.c │ └── my_unset.c ├── cmd │ ├── check │ │ ├── check_block_cmd.c │ │ ├── check_quoteanddollar.c │ │ ├── fill_second_step.c │ │ ├── return_check.c │ │ └── special_case_cmd.c │ ├── exec_cmd.c │ ├── handler_cmd.c │ ├── handler_exec_cmd.c │ ├── pipe_cmd.c │ ├── redir_cmd.c │ └── redir_file_cmd.c ├── data │ ├── handler_data.c │ └── split_data │ │ ├── get_len_split_data.c │ │ ├── split_data.c │ │ └── split_data_utils.c ├── env │ ├── create_list_env.c │ ├── handler_env.c │ └── path_env.c ├── error_and_free │ ├── error_msg.c │ ├── free_error.c │ └── handler_error.c ├── init │ ├── init_shell.c │ └── init_struct.c ├── list │ ├── check_empty.c │ ├── create_list.c │ ├── create_tab_args.c │ ├── delete_list.c │ ├── handler_list.c │ └── set_token_list.c ├── main │ ├── handler_signal.c │ ├── minishell.c │ ├── my_global.c │ ├── my_global_two.c │ └── shell_loop.c └── termcap │ ├── delete_char.c │ └── history │ ├── create_list_history.c │ ├── delete_list_history.c │ └── handler_history.c ├── test.sh └── text.txt /Makefile: -------------------------------------------------------------------------------- 1 | NAME = minishell 2 | 3 | CC = gcc 4 | 5 | RM = rm -f 6 | 7 | CFLAGS = -g 8 | 9 | LFLAGS = -I. -lncurses 10 | 11 | LIBFT = ./lib/libft/libft.a 12 | 13 | SRCS = ./srcs/main/minishell.c \ 14 | ./srcs/main/handler_signal.c \ 15 | ./srcs/main/my_global.c \ 16 | ./srcs/main/my_global_two.c \ 17 | ./srcs/main/shell_loop.c \ 18 | ./srcs/termcap/delete_char.c \ 19 | ./srcs/termcap/history/create_list_history.c \ 20 | ./srcs/termcap/history/delete_list_history.c \ 21 | ./srcs/termcap/history/handler_history.c \ 22 | ./srcs/builtins/handler_builtins.c \ 23 | ./srcs/builtins/my_cd.c \ 24 | ./srcs/builtins/my_env.c \ 25 | ./srcs/builtins/my_pwd.c \ 26 | ./srcs/builtins/export/my_export.c \ 27 | ./srcs/builtins/export/export_check.c \ 28 | ./srcs/builtins/export/print_export.c \ 29 | ./srcs/builtins/my_exit.c \ 30 | ./srcs/builtins/my_echo.c \ 31 | ./srcs/builtins/my_unset.c \ 32 | ./srcs/cmd/exec_cmd.c \ 33 | ./srcs/cmd/handler_exec_cmd.c \ 34 | ./srcs/cmd/handler_cmd.c \ 35 | ./srcs/cmd/pipe_cmd.c \ 36 | ./srcs/cmd/redir_cmd.c \ 37 | ./srcs/cmd/redir_file_cmd.c \ 38 | ./srcs/cmd/check/check_block_cmd.c \ 39 | ./srcs/cmd/check/return_check.c \ 40 | ./srcs/cmd/check/fill_second_step.c \ 41 | ./srcs/cmd/check/check_quoteanddollar.c \ 42 | ./srcs/cmd/check/special_case_cmd.c \ 43 | ./srcs/env/create_list_env.c \ 44 | ./srcs/env/handler_env.c \ 45 | ./srcs/env/path_env.c \ 46 | ./srcs/list/check_empty.c \ 47 | ./srcs/list/create_list.c \ 48 | ./srcs/list/handler_list.c \ 49 | ./srcs/list/delete_list.c \ 50 | ./srcs/list/set_token_list.c \ 51 | ./srcs/list/create_tab_args.c \ 52 | ./srcs/init/init_struct.c \ 53 | ./srcs/init/init_shell.c \ 54 | ./srcs/error_and_free/free_error.c \ 55 | ./srcs/error_and_free/error_msg.c \ 56 | ./srcs/error_and_free/handler_error.c \ 57 | ./srcs/data/handler_data.c \ 58 | ./srcs/data/split_data/split_data.c \ 59 | ./srcs/data/split_data/split_data_utils.c \ 60 | ./srcs/data/split_data/get_len_split_data.c \ 61 | ./lib/get_next_line/get_next_line.c 62 | 63 | OBJS = $(SRCS:.c=.o) 64 | 65 | all: $(NAME) 66 | 67 | $(NAME): $(OBJS) 68 | make -C lib/libft 69 | ${CC} ${CFLAGS} ${LFLAGS} -o ${NAME} ${OBJS} ${LIBFT} 70 | 71 | clean: 72 | $(RM) $(OBJS) 73 | make clean -C lib/libft 74 | 75 | fclean: clean 76 | $(RM) $(NAME) $(LIBFT) 77 | 78 | re: fclean $(NAME) 79 | 80 | f: re 81 | ./minishell 82 | 83 | .PHONY: all clean fclean re f 84 | -------------------------------------------------------------------------------- /ToDoList.txt: -------------------------------------------------------------------------------- 1 | -> echo / unset / redirection (vendredi) 2 | -> signal ctrl-c ???? (vendredi) 3 | -> voir les erreurs (samedi) 4 | -> leaks (samedi) 5 | -> mise a propre (dimanche + push) 6 | 7 | 8 | error: 9 | 10 | [SETUP mkdir d] echo $PWD; echo $OLDPWD 11 | [EXPORTS HOME='/Users/emma'] echo $PWD; echo $OLDPWD -> erreur car handler builtins zebi!!!!!!!!!!!!!!!!! 12 | 13 | 14 | 15 | BROUILLION : 16 | 17 | - 'echo' et "echo" marche aussi comme echo 18 | 19 | 20 | link pour les list doublement chainee 21 | -> https://www.commentcamarche.net/faq/7636-liste-doublement-chainee#:~:text=Les%20listes%20doublement%20cha%C3%AEn%C3%A9es%20sont,semblables%20aux%20listes%20simplement%20cha%C3%AEn%C3%A9es%20.&text=Le%20pointeur%20precedent%20du%20premier,la%20fin%20de%20la%20liste). 22 | 23 | -> http://sdz.tdct.org/sdz/les-listes-doublement-chainees-en-langage-c.html 24 | 25 | link fork : 26 | 27 | -> https://www.commentcamarche.net/faq/10611-que-fait-un-fork 28 | 29 | pays du 42 doc 30 | -> http://paysdu42.fr/?page=minishell.c 31 | 32 | -> https://segfault42.github.io/posts/minishell/ 33 | 34 | link pour getcwd 35 | 36 | -> http://manpages.ubuntu.com/manpages/trusty/fr/man3/getcwd.3.html 37 | 38 | link opendir / readdir etc 39 | -> https://openclassrooms.com/forum/sujet/opendirreaddir-45778 40 | -> https://openclassrooms.com/forum/sujet/coding-minishell-54695 41 | 42 | 43 | 44 | 45 | 46 | export A='fi le' ; echo bonjour > $A 47 | export lol2="\'" 48 | export lol2="\\" 49 | export lol2='$' 50 | export lol2='\t' 51 | export test=$test"coucou" 52 | export A=a B=b C=c D=d E=e F=f G=g H=h I=i J=j K=k L=l M=m N=n O=o P=p Q=q R=r // ERREUR C=> COLORFGBG ???? 53 | // erreru strcmp ??? 54 | 55 | // new 56 | cat /dev/random | head -c 100 | wc -c 57 | echo testing multi ; echo "test 1 ; | and 2" ; cat test.sh | grep echo 58 | 59 | 60 | pas gerer 61 | export A='fi le' ; echo bonjour > $A 62 | error ';;' 63 | -------------------------------------------------------------------------------- /fr.subject.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MickeyMighty/MiniShell/e952835751ea1744df3f3bd1c53ffd3b33b768c6/fr.subject.pdf -------------------------------------------------------------------------------- /includes/termcap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* termcap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/28 12:23:54 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 14:14:24 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef TERMCAPS_H 14 | # define TERMCAPS_H 15 | 16 | # include "libshell.h" 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | # define BACKSPACE 127 24 | # define EOF_KEY 4 25 | # define LEFT_ARROW 4479771 26 | # define RIGHT_ARROW 4414235 27 | # define UP_ARROW 4283163 28 | # define DOWN_ARROW 4348699 29 | 30 | # define HOME 4741915 31 | # define END 4610843 32 | 33 | # define ALT_X 8948194 34 | # define ALT_C 42947 35 | # define ALT_V 10127586 36 | 37 | # define CTRL_LEFT 74995417045787 38 | # define CTRL_RIGHT 73895905418011 39 | # define CTRL_UP 71696882162459 40 | # define CTRL_DOWN 72796393790235 41 | // typedef struct s_termcap 42 | // { 43 | // struct termios term; 44 | // struct termios term_backup; 45 | // // t_hist *history; 46 | // // t_hist *cur_history; 47 | // // char *backup_cmd; 48 | // // char *copy_cmd; 49 | // int start_row; 50 | // int start_col; 51 | // int col; 52 | // int row; 53 | // int plen; 54 | // int cur_pos; 55 | // int currow; 56 | // int curcol; 57 | // int lenlen; 58 | // int rowoffset; 59 | // int mod_offset; 60 | // int endcol; 61 | // int endrow; 62 | // char *cm; 63 | // char *ce; 64 | // char *dl; 65 | // long backspace; 66 | // } t_termcap; 67 | 68 | // t_termcap *g_tc; 69 | 70 | # endif 71 | -------------------------------------------------------------------------------- /lib/get_next_line/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/10 00:37:54 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 13:26:48 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include "../../includes/libshell.h" 15 | 16 | static int ft_free(char **str, int ret) 17 | { 18 | free(*str); 19 | *str = NULL; 20 | g_error(SET, SUCCESS); 21 | return (ret); 22 | } 23 | 24 | static int ft_clean(char **str) 25 | { 26 | if (g_bytes(GET, 0) == 0 && g_error(GET, 0) == ERROR) 27 | return (ft_free(str, 1)); 28 | return (1); 29 | } 30 | 31 | static int ft_check(char **str, char **line) 32 | { 33 | char *tmp; 34 | int i; 35 | 36 | i = -1; 37 | while ((*str)[++i]) 38 | if ((*str)[i] == '\n') 39 | { 40 | if (i == 0) 41 | { 42 | if (!(*line = ft_strdup(""))) 43 | return (-1); 44 | } 45 | else if (!(*line = ft_substr(*str, 0, i))) 46 | return (-1); 47 | tmp = *str; 48 | if (!(*str = ft_substr(*str, (i + 1), ft_strlen(*str)))) 49 | return (-1); 50 | free(tmp); 51 | return (1); 52 | } 53 | return (0); 54 | } 55 | 56 | static int ft_read(int fd, char *buf, char **str, char **line) 57 | { 58 | int bytes; 59 | char *tmp; 60 | 61 | if ((bytes = (ft_check(str, line))) == -1) 62 | return (ft_free(str, -1)); 63 | if (bytes) 64 | return (1); 65 | while ((bytes = read(fd, buf, BUFFER_SIZE)) >= 0 && ft_clean(str) == 1) 66 | { 67 | g_bytes(SET, bytes); 68 | buf[bytes] = '\0'; 69 | if (buf[0] == '\0' && ft_strlen(*str)) 70 | continue; 71 | else if (buf[0] == '\0' && ft_strlen(*str) <= 0) 72 | return (ft_free(str, 0)); 73 | tmp = *str; 74 | *str = ft_strjoin(*str, buf); 75 | free(tmp); 76 | if ((bytes = ft_check(str, line)) == -1) 77 | return (ft_free(str, -1)); 78 | if (bytes) 79 | return (ft_free(str, 1)); 80 | } 81 | if (bytes == -1 || !(*line = ft_substr(*str, 0, ft_strlen(*str)))) 82 | return (ft_free(str, -1)); 83 | return (ft_free(str, 0)); 84 | } 85 | 86 | int get_next_line(int fd, char **line) 87 | { 88 | static char *str; 89 | char buf[BUFFER_SIZE + 1]; 90 | 91 | if (fd < 0 || line == NULL || BUFFER_SIZE <= 0) 92 | return (-1); 93 | if (!str) 94 | if (!(str = ft_calloc(1, sizeof(char)))) 95 | return (-1); 96 | return (ft_read(fd, buf, &str, line)); 97 | } 98 | -------------------------------------------------------------------------------- /lib/get_next_line/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/10 00:38:28 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 11:17:25 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | # ifndef BUFFER_SIZE 16 | # define BUFFER_SIZE 1 17 | # endif 18 | 19 | # include 20 | # include "../libft/libft.h" 21 | # include 22 | # include 23 | # include 24 | # include 25 | 26 | int get_next_line(int fd, char **line); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /lib/get_next_line/get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/23 13:14:59 by loamar #+# #+# */ 9 | /* Updated: 2021/04/26 21:39:18 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | #include "../../includes/libshell.h" 15 | 16 | // void ft_handler_gnl_ctrl_c(char **str, int bytes) 17 | // { 18 | // if (g_status == ) 19 | // } 20 | -------------------------------------------------------------------------------- /lib/libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: loamar +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2019/11/30 19:29:10 by loamar #+# #+# # 9 | # Updated: 2021/05/01 12:52:06 by loamar ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME= libft.a 14 | 15 | CC=gcc 16 | 17 | SRC=ft_putnbr.c \ 18 | ft_islower.c \ 19 | ft_isupper.c \ 20 | ft_abs.c \ 21 | ft_putchar.c \ 22 | ft_putstr.c \ 23 | ft_atoi.c \ 24 | ft_bzero.c \ 25 | ft_calloc.c \ 26 | ft_isalnum.c \ 27 | ft_isalpha.c \ 28 | ft_isascii.c \ 29 | ft_isdigit.c \ 30 | ft_isprint.c \ 31 | ft_itoa.c \ 32 | ft_memccpy.c \ 33 | ft_memchr.c \ 34 | ft_memcmp.c \ 35 | ft_memcpy.c \ 36 | ft_memmove.c \ 37 | ft_memset.c \ 38 | ft_putchar_fd.c \ 39 | ft_putendl_fd.c \ 40 | ft_putnbr_fd.c \ 41 | ft_putstr_fd.c \ 42 | ft_split.c \ 43 | ft_strdup.c \ 44 | ft_strjoin.c \ 45 | ft_strlcat.c \ 46 | ft_strncat.c \ 47 | ft_strlcpy.c \ 48 | ft_strlen.c \ 49 | ft_strmapi.c \ 50 | ft_strncmp.c \ 51 | ft_strnstr.c \ 52 | ft_strchr.c \ 53 | ft_strrchr.c \ 54 | ft_strtrim.c \ 55 | ft_substr.c \ 56 | ft_tolower.c \ 57 | ft_toupper.c \ 58 | ft_stohex.c \ 59 | ft_strtoupper.c \ 60 | ft_putzero.c \ 61 | ft_putspace.c \ 62 | ft_unsigned.c \ 63 | ft_ltohex.c \ 64 | ft_atoi_base.c \ 65 | ft_isprecision.c \ 66 | ft_ltohex_spe.c \ 67 | ft_strdel.c \ 68 | ft_strcpy.c \ 69 | ft_newstr.c \ 70 | ft_min.c \ 71 | ft_max.c \ 72 | ft_memdel.c \ 73 | ft_free_strjoin.c \ 74 | ft_strcmp.c 75 | 76 | 77 | OBJ= $(SRC:.c=.o) 78 | 79 | OBJ_BON= $(BONUS:.c=.o) 80 | 81 | BONUS=ft_lstadd_front_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c ft_lstlast_bonus.c ft_lstnew_bonus.c ft_lstsize_bonus.c 82 | 83 | FLAGS=-Wall -Wextra -Werror 84 | 85 | INC=./INCLUDES 86 | 87 | 88 | all: $(NAME) 89 | 90 | $(NAME): $(OBJ) 91 | @ar rc $(NAME) $^ 92 | @ranlib $(NAME) 93 | 94 | .o: .c 95 | @$(CC) $(FLAGS) -I$(INC) -o $@ -c $< 96 | 97 | bonus: $(OBJ) $(OBJ_BON) 98 | @ar rc $(NAME) $^ 99 | @ranlib $(NAME) 100 | 101 | clean: 102 | @rm -f $(OBJ) 103 | 104 | clean_b: 105 | @rm -f $(OBJ_BON) 106 | 107 | fclean: clean clean_b 108 | @rm -f $(NAME) 109 | 110 | re: fclean all 111 | 112 | .PHONY: all bonus clean clean_b fclean re 113 | 114 | .SILENT: $(OBJ) 115 | -------------------------------------------------------------------------------- /lib/libft/ft_abs.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_abs.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:07:50 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:07:53 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_abs(int i) 16 | { 17 | return ((i >= 0) ? i : -i); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:08:12 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:08:15 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | long i; 18 | long sign; 19 | long res; 20 | 21 | sign = 1; 22 | i = 0; 23 | while (str[i] == ' ' || str[i] == '\t' || str[i] == '\v' || 24 | str[i] == '\n' || str[i] == '\r' || str[i] == '\f') 25 | i++; 26 | if (str[i] == '-' || str[i] == '+') 27 | { 28 | if (str[i] == '-') 29 | sign *= -1; 30 | i++; 31 | } 32 | res = 0; 33 | while (str[i] != '\0' && ft_isdigit(str[i]) != 0) 34 | { 35 | res = res * 10 + (str[i] - '0'); 36 | i++; 37 | } 38 | return (res * sign); 39 | } 40 | -------------------------------------------------------------------------------- /lib/libft/ft_atoi_base.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi_base.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:08:27 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:08:39 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_check_base(char *base) 16 | { 17 | int i; 18 | int j; 19 | int size_b; 20 | 21 | i = 0; 22 | size_b = 0; 23 | while (base[size_b] != '\0') 24 | { 25 | if (base[size_b] == '+' || base[size_b] == '-' || \ 26 | base[size_b] <= 32 || base[size_b] > 126) 27 | return (0); 28 | size_b++; 29 | } 30 | while (i < size_b - 1) 31 | { 32 | j = i + 1; 33 | while (j < size_b) 34 | { 35 | if (base[i] == base[j]) 36 | return (0); 37 | j++; 38 | } 39 | i++; 40 | } 41 | return (size_b); 42 | } 43 | 44 | int ft_check_str(char *str, char *base, int size_base) 45 | { 46 | int i; 47 | int j; 48 | int count; 49 | 50 | i = 0; 51 | count = 0; 52 | while (str[i] != '\0') 53 | { 54 | j = 0; 55 | while (j < size_base) 56 | { 57 | if (str[i] == base[j]) 58 | count++; 59 | j++; 60 | } 61 | if (count != i + 1) 62 | return (count); 63 | i++; 64 | } 65 | return (count); 66 | } 67 | 68 | int ft_power(int nb, int power) 69 | { 70 | if (power < 0) 71 | return (0); 72 | if (power == 0 || nb == 1) 73 | return (1); 74 | if (power == 1) 75 | return (nb); 76 | else 77 | return (nb = nb * ft_power(nb, power - 1)); 78 | } 79 | 80 | long ft_str_to_int(char *str, char *base, int size_str, int size_base) 81 | { 82 | int i; 83 | int j; 84 | long rslt; 85 | 86 | i = 0; 87 | rslt = 0; 88 | while (i < size_str) 89 | { 90 | j = 0; 91 | while (str[i] != base[j]) 92 | j++; 93 | rslt = rslt + j * ft_power(size_base, size_str - 1 - i); 94 | i++; 95 | } 96 | return (rslt); 97 | } 98 | 99 | int ft_atoi_base(char *str, char *base) 100 | { 101 | int size_b; 102 | int size_str; 103 | int sign; 104 | int i; 105 | 106 | sign = 0; 107 | i = 0; 108 | size_b = ft_check_base(base); 109 | while (str[i] != '\0' && ((str[i] >= 7 && str[i] <= 13) || str[i] == 32)) 110 | i++; 111 | while (str[i] != '\0' && (str[i] == '-' || str[i] == '+')) 112 | { 113 | if (str[i] == '-') 114 | sign++; 115 | i++; 116 | } 117 | size_str = ft_check_str(str + i, base, size_b); 118 | if (size_b > 1 && size_str > 0) 119 | { 120 | if (sign % 2) 121 | return (-1 * ft_str_to_int(str + i, base, size_str, size_b)); 122 | else 123 | return (ft_str_to_int(str + i, base, size_str, size_b)); 124 | } 125 | return (0); 126 | } 127 | -------------------------------------------------------------------------------- /lib/libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:08:52 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:08:55 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | char *str; 18 | size_t i; 19 | 20 | if (n != 0) 21 | { 22 | i = 0; 23 | str = (char *)s; 24 | while (i < n) 25 | { 26 | str[i] = '\0'; 27 | i++; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /lib/libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:09:16 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:09:20 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t count, size_t size) 16 | { 17 | void *dest; 18 | 19 | if (count * size == 0) 20 | { 21 | count = 1; 22 | size = 1; 23 | } 24 | if (!(dest = (void *)malloc(size * count))) 25 | return (0); 26 | ft_memset(dest, '\0', size * count); 27 | return (dest); 28 | } 29 | -------------------------------------------------------------------------------- /lib/libft/ft_free_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_free_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/31 23:57:55 by loamar #+# #+# */ 9 | /* Updated: 2021/05/01 12:32:15 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *return_error(char *s1, char *s2) 16 | { 17 | char *tmp; 18 | 19 | if ((!s1 && !s2) || (s1 == NULL && s2 == NULL)) 20 | return (ft_strdup("\0")); 21 | if (!s1 || s1 == NULL) 22 | return (s2); 23 | if (!s2 || s2 == NULL) 24 | return (s1); 25 | return (NULL); 26 | } 27 | 28 | static int check(char *s1, char *s2) 29 | { 30 | if (!s1 && !s2) 31 | return (-1); 32 | if (!s1) 33 | return (-1); 34 | if (!s2) 35 | return (-1); 36 | return (0); 37 | } 38 | 39 | static char *return_dest(char *s1, char *s2, char *dest, int lens1) 40 | { 41 | int i; 42 | 43 | i = 0; 44 | while (s1[i] != '\0') 45 | { 46 | dest[i] = s1[i]; 47 | i++; 48 | } 49 | i = 0; 50 | while (s2[i] != '\0') 51 | { 52 | dest[i + lens1] = s2[i]; 53 | i++; 54 | } 55 | dest[i + lens1] = '\0'; 56 | return (dest); 57 | } 58 | 59 | char *ft_free_strjoin(char *s1, char *s2) 60 | { 61 | size_t lens1; 62 | size_t lens2; 63 | char *dest; 64 | 65 | dest = NULL; 66 | if (check(s1, s2) == -1) 67 | return (return_error(s1, s2)); 68 | lens1 = ft_strlen(s1); 69 | lens2 = ft_strlen(s2); 70 | if (!(dest = (char *)malloc(sizeof(char) * (lens1 + lens2 + 1)))) 71 | return (0); 72 | dest = return_dest(s1, s2, dest, lens1); 73 | free(s1); 74 | free(s2); 75 | return (dest); 76 | } 77 | -------------------------------------------------------------------------------- /lib/libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:09:32 by loamar #+# #+# */ 9 | /* Updated: 2021/04/02 10:35:14 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if (ft_isdigit(c) == 1 || ft_isalpha(c) == 1) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:09:48 by loamar #+# #+# */ 9 | /* Updated: 2020/12/03 16:58:43 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:10:11 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:10:15 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (0 <= c && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:07:36 by loamar #+# #+# */ 9 | /* Updated: 2021/03/20 12:52:51 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= '0' && c <= '9') 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/ft_islower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_islower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:10:46 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:10:49 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_islower(int c) 16 | { 17 | return ('a' <= c && c <= 'z'); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_isprecision.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprecision.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:10:56 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:10:58 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprecision(int c) 16 | { 17 | return (('0' <= c && c <= '9') || c == '-'); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:11:10 by loamar #+# #+# */ 9 | /* Updated: 2021/04/30 00:32:06 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | if (c >= 32 && c <= 126) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/ft_isupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:11:22 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:11:25 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isupper(int c) 16 | { 17 | return ('A' <= c && c <= 'Z'); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:11:37 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:11:41 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | unsigned int ft_get_size(unsigned int n) 16 | { 17 | unsigned int size; 18 | 19 | size = 0; 20 | while (n >= 10) 21 | { 22 | n /= 10; 23 | size++; 24 | } 25 | return (size + 1); 26 | } 27 | 28 | char *ft_itoa(long int n) 29 | { 30 | unsigned long int nb; 31 | unsigned long int size; 32 | long int i; 33 | char *str; 34 | 35 | nb = (n < 0) ? (unsigned int)(n * -1) : (unsigned int)n; 36 | size = (n < 0) ? ft_get_size(nb) + 1 : ft_get_size(nb); 37 | if (!(str = (char *)malloc(sizeof(char) * (size + 1)))) 38 | return (0); 39 | if (n < 0) 40 | str[0] = '-'; 41 | i = size - 1; 42 | str[size] = '\0'; 43 | while (nb >= 10) 44 | { 45 | str[i--] = (nb % 10) + '0'; 46 | nb /= 10; 47 | } 48 | str[i] = (nb % 10) + '0'; 49 | return (str); 50 | } 51 | -------------------------------------------------------------------------------- /lib/libft/ft_lstadd_back_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:11:50 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:58:29 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list_libft **alst, t_list_libft *new) 16 | { 17 | t_list_libft *last; 18 | 19 | last = *alst; 20 | new->next = NULL; 21 | if (!(*alst)) 22 | (*alst) = new; 23 | else 24 | { 25 | while (last->next != NULL) 26 | last = last->next; 27 | last->next = new; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/ft_lstadd_front_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:12:01 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:58:39 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list_libft **alst, t_list_libft *new) 16 | { 17 | new->next = (*alst); 18 | (*alst) = new; 19 | } 20 | -------------------------------------------------------------------------------- /lib/libft/ft_lstclear_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:12:12 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:58:48 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list_libft **lst, void (*del)(void *)) 16 | { 17 | t_list_libft *next_node; 18 | t_list_libft *current; 19 | 20 | if (lst && del) 21 | { 22 | next_node = (*lst); 23 | current = (*lst); 24 | while (next_node != NULL) 25 | { 26 | current = next_node; 27 | next_node = next_node->next; 28 | (*del)(current->content); 29 | free(current); 30 | } 31 | *lst = NULL; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lib/libft/ft_lstdelone_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:12:23 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:58:56 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list_libft *lst, void (*del)(void *)) 16 | { 17 | if (lst && del) 18 | { 19 | (*del)(lst->content); 20 | free(lst); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/ft_lstiter_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:12:37 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:59:08 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list_libft *lst, void (*f)(void *)) 16 | { 17 | if (lst && f) 18 | { 19 | while (lst) 20 | { 21 | (*f)(lst->content); 22 | lst = lst->next; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/ft_lstlast_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:12:53 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:59:26 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list_libft *lst) 16 | { 17 | if (lst == NULL) 18 | return (lst); 19 | while (lst->next != NULL) 20 | lst = lst->next; 21 | return (lst); 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/ft_lstmap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:13:14 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 13:36:53 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list_libft *ft_lstmap(t_list_libft *lst, void *(*f)(void *), 16 | void (*d)(void *)) 17 | { 18 | t_list_libft *head; 19 | t_list_libft *new; 20 | 21 | if (!lst || !f) 22 | return (NULL); 23 | if (!(new = ft_lstnew((*f)(lst->content)))) 24 | return (NULL); 25 | head = new; 26 | lst = lst->next; 27 | while (lst) 28 | { 29 | if (!(new->next = ft_lstnew((*f)(lst->content)))) 30 | { 31 | ft_lstclear(&head, (*d)); 32 | return (NULL); 33 | } 34 | new = new->next; 35 | lst = lst->next; 36 | } 37 | return (head); 38 | } 39 | -------------------------------------------------------------------------------- /lib/libft/ft_lstnew_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:13:29 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:59:40 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list_libft *ft_lstnew(void *content) 16 | { 17 | t_list_libft *new_node; 18 | 19 | if (!(new_node = (t_list_libft *)malloc(sizeof(t_list)))) 20 | return (NULL); 21 | new_node->content = content; 22 | new_node->next = NULL; 23 | return (new_node); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/ft_lstsize_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:13:43 by loamar #+# #+# */ 9 | /* Updated: 2020/12/11 02:59:46 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list_libft *lst) 16 | { 17 | int size; 18 | 19 | size = 0; 20 | while (lst != NULL) 21 | { 22 | size++; 23 | lst = lst->next; 24 | } 25 | return (size); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/ft_ltohex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_ltohex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:13:54 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:14:03 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static long ft_neg(long num, char *base) 16 | { 17 | long max; 18 | 19 | max = 4294967295; 20 | num = max - num + 1; 21 | return (num); 22 | } 23 | 24 | static long ft_len(long num) 25 | { 26 | long len; 27 | 28 | len = 0; 29 | while (num) 30 | { 31 | num /= 16; 32 | len++; 33 | } 34 | return (len); 35 | } 36 | 37 | char *ft_ltohex(long num) 38 | { 39 | long pos; 40 | long len; 41 | char *stock; 42 | char *base; 43 | 44 | base = ft_strdup("0123456789abcdef"); 45 | len = 0; 46 | if (num < 0) 47 | num = ft_neg(-num, base); 48 | if (num == 0) 49 | return ("0"); 50 | len = ft_len(num); 51 | if (!(stock = (char *)malloc(sizeof(char) * len + 1))) 52 | return (NULL); 53 | pos = 1; 54 | while (num != 0) 55 | { 56 | stock[len - pos++] = base[num % 16]; 57 | num /= 16; 58 | } 59 | stock[len] = '\0'; 60 | free(base); 61 | return (stock); 62 | } 63 | -------------------------------------------------------------------------------- /lib/libft/ft_ltohex_spe.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_ltohex_spe.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:14:11 by loamar #+# #+# */ 9 | /* Updated: 2019/11/30 19:36:01 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static long ft_neg(long num, char *base) 16 | { 17 | long max; 18 | 19 | max = 4294967295; 20 | num = max - num + 1; 21 | return (num); 22 | } 23 | 24 | static long ft_len(long num) 25 | { 26 | long len; 27 | 28 | len = 0; 29 | while (num) 30 | { 31 | num /= 16; 32 | len++; 33 | } 34 | return (len); 35 | } 36 | 37 | char *ft_ltohex_spe(long num) 38 | { 39 | char *stock; 40 | char *base; 41 | t_num p; 42 | 43 | p.tmp = 4294967295; 44 | base = ft_strdup("0123456789abcdef"); 45 | p.len = 0; 46 | num = ft_neg(-num, base); 47 | p.len = ft_len(num) * 2; 48 | if (!(stock = (char *)malloc(sizeof(char) * p.len + 1))) 49 | return (NULL); 50 | p.pos = 1; 51 | while (p.tmp != 0) 52 | { 53 | stock[p.len - p.pos++] = base[p.tmp % 16]; 54 | p.tmp /= 16; 55 | } 56 | while (num != 0) 57 | { 58 | stock[p.len - p.pos++] = base[num % 16]; 59 | num /= 16; 60 | } 61 | stock[p.len] = '\0'; 62 | free(base); 63 | return (stock); 64 | } 65 | -------------------------------------------------------------------------------- /lib/libft/ft_max.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_max.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/13 13:34:29 by loamar #+# #+# */ 9 | /* Updated: 2020/06/08 14:09:13 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_max(int a, int b) 16 | { 17 | return (a > b ? a : b); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:14:48 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:14:49 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *d; 19 | unsigned char *s; 20 | 21 | s = (unsigned char *)src; 22 | d = (unsigned char *)dst; 23 | i = 0; 24 | if (n == 0) 25 | return (NULL); 26 | while (i < n) 27 | { 28 | d[i] = s[i]; 29 | if (s[i] == (unsigned char)c) 30 | { 31 | i++; 32 | return (dst + i); 33 | } 34 | i++; 35 | } 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /lib/libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:15:03 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:15:05 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *src; 19 | 20 | if (n == 0) 21 | return (0); 22 | src = (unsigned char *)s; 23 | i = 0; 24 | while (i < n) 25 | { 26 | if (src[i] == (unsigned char)c) 27 | return ((void *)(src + i)); 28 | i++; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:16:23 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:16:24 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *src1; 18 | unsigned char *src2; 19 | size_t i; 20 | 21 | if (n == 0) 22 | return (0); 23 | src1 = (unsigned char *)s1; 24 | src2 = (unsigned char *)s2; 25 | i = 0; 26 | while (i < n) 27 | { 28 | if (src1[i] != src2[i]) 29 | return (src1[i] - src2[i]); 30 | i++; 31 | } 32 | return (0); 33 | } 34 | -------------------------------------------------------------------------------- /lib/libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:16:35 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:16:37 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | unsigned char *d; 18 | unsigned char *s; 19 | size_t i; 20 | 21 | s = (unsigned char *)src; 22 | d = (unsigned char *)dst; 23 | i = 0; 24 | if (d == NULL && s == NULL) 25 | return (NULL); 26 | while (i < n) 27 | { 28 | d[i] = s[i]; 29 | i++; 30 | } 31 | return (dst); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/03/03 18:40:22 by loamar #+# #+# */ 9 | /* Updated: 2020/06/08 19:40:33 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_memdel(void **ap) 16 | { 17 | if (ap && *ap) 18 | { 19 | free(*ap); 20 | *ap = NULL; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:16:46 by loamar #+# #+# */ 9 | /* Updated: 2021/04/08 12:49:22 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | size_t i; 18 | unsigned char *s; 19 | unsigned char *d; 20 | 21 | if (!dst && !src) 22 | return (NULL); 23 | s = (unsigned char *)src; 24 | d = (unsigned char *)dst; 25 | i = 0; 26 | if (d > s) 27 | while (0 < len) 28 | { 29 | d[len - 1] = s[len - 1]; 30 | len--; 31 | } 32 | else 33 | while (i < len) 34 | { 35 | d[i] = s[i]; 36 | i++; 37 | } 38 | return (dst); 39 | } 40 | -------------------------------------------------------------------------------- /lib/libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:17:05 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:17:08 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | unsigned char *str; 18 | size_t i; 19 | 20 | str = (unsigned char *)b; 21 | i = 0; 22 | while (i < len) 23 | { 24 | str[i] = (unsigned char)c; 25 | i++; 26 | } 27 | return (b); 28 | } 29 | -------------------------------------------------------------------------------- /lib/libft/ft_min.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_min.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/13 13:34:29 by loamar #+# #+# */ 9 | /* Updated: 2020/06/08 14:09:34 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_min(int a, int b) 16 | { 17 | return (a < b ? a : b); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_newstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_newstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/14 19:26:50 by loamar #+# #+# */ 9 | /* Updated: 2020/01/14 19:29:05 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_newstr(size_t size) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | i = 0; 21 | if ((ptr = malloc(sizeof(*ptr) * size + 1)) == NULL) 22 | return (NULL); 23 | while (i <= size) 24 | ptr[i++] = '\0'; 25 | return (ptr); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:17:18 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:17:22 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:17:30 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 14:53:22 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:17:41 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:17:43 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | if (s && fd >= 0) 18 | { 19 | while (*s) 20 | ft_putchar_fd(*s++, fd); 21 | ft_putchar_fd('\n', fd); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:17:54 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:17:56 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | long nb; 18 | 19 | nb = n; 20 | if (n < 0) 21 | { 22 | ft_putchar('-'); 23 | nb *= -1; 24 | } 25 | if (nb >= 10) 26 | ft_putnbr(nb / 10); 27 | ft_putchar(nb % 10 + '0'); 28 | } 29 | -------------------------------------------------------------------------------- /lib/libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:18:12 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:18:13 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | long nb; 18 | 19 | nb = n; 20 | if (nb < 0) 21 | { 22 | ft_putchar_fd('-', fd); 23 | nb *= -1; 24 | } 25 | if (nb >= 10) 26 | ft_putnbr_fd(nb / 10, fd); 27 | ft_putchar_fd((nb % 10) + '0', fd); 28 | } 29 | -------------------------------------------------------------------------------- /lib/libft/ft_putspace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putspace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:18:24 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:18:25 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putspace(size_t n) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i++ < n) 21 | ft_putchar(' '); 22 | i--; 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:18:34 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:18:36 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char *s) 16 | { 17 | while (*s) 18 | { 19 | ft_putchar(*s++); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /lib/libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:18:53 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:18:55 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (s && fd >= 0) 18 | while (*s) 19 | ft_putchar_fd(*s++, fd); 20 | } 21 | -------------------------------------------------------------------------------- /lib/libft/ft_putzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:19:32 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:19:35 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putzero(size_t n) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (i++ < n) 21 | ft_putchar('0'); 22 | i--; 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:19:45 by loamar #+# #+# */ 9 | /* Updated: 2021/03/22 16:39:34 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | #include 15 | 16 | static int ft_count_word(char const *s, char c) 17 | { 18 | int nb; 19 | int i; 20 | 21 | i = 0; 22 | nb = 0; 23 | if (!s) 24 | return (0); 25 | while (s[i] != '\0') 26 | { 27 | while (s[i] == c) 28 | { 29 | i++; 30 | } 31 | if (s[i] != '\0' && s[i] != c) 32 | { 33 | nb++; 34 | while (s[i] != '\0' && s[i] != c) 35 | i++; 36 | } 37 | } 38 | return (nb); 39 | } 40 | 41 | static size_t ft_get_len_word(const char *s, int index, char c) 42 | { 43 | size_t len_word; 44 | 45 | len_word = 0; 46 | while (s[index] != '\0' && s[index] != c) 47 | { 48 | len_word++; 49 | index++; 50 | } 51 | return (len_word); 52 | } 53 | 54 | static char **free_tab(char **tab, int j) 55 | { 56 | while (j-- >= 0) 57 | free(tab[j]); 58 | free(tab); 59 | return (NULL); 60 | } 61 | 62 | char **ft_split(char *s, char c) 63 | { 64 | char **res; 65 | int nb_word; 66 | int i; 67 | int j; 68 | 69 | nb_word = ft_count_word(s, c); 70 | if (!s || !(res = (char **)malloc((nb_word + 1) * sizeof(char *)))) 71 | return (NULL); 72 | i = 0; 73 | j = 0; 74 | while (s[i] != '\0' && j < nb_word) 75 | { 76 | while (s[i] == c) 77 | i++; 78 | if (s[i] != '\0' && s[i] != c) 79 | { 80 | if (!(res[j] = ft_substr(s, i, ft_get_len_word(s, i, c)))) 81 | return (free_tab(res, j)); 82 | j++; 83 | while (s[i] != '\0' && s[i] != c) 84 | i++; 85 | } 86 | } 87 | res[j] = 0; 88 | return (res); 89 | } 90 | -------------------------------------------------------------------------------- /lib/libft/ft_stohex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_stohex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:19:55 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:19:58 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static long int ft_neg(long int num, char *base) 16 | { 17 | long int max; 18 | 19 | max = 4294967295; 20 | num = max - num + 1; 21 | return (num); 22 | } 23 | 24 | static long int ft_len(long int num) 25 | { 26 | long int len; 27 | 28 | len = 0; 29 | while (num) 30 | { 31 | num /= 16; 32 | len++; 33 | } 34 | return (len); 35 | } 36 | 37 | char *ft_stohex(char *str) 38 | { 39 | long int num; 40 | long int pos; 41 | long int len; 42 | char *stock; 43 | char *base; 44 | 45 | base = ft_strdup("0123456789abcdef"); 46 | len = 0; 47 | num = ft_atoi(str); 48 | if (num < 0) 49 | num = ft_neg(-num, base); 50 | if (num == 0) 51 | return ("0"); 52 | len = ft_len(num); 53 | if (!(stock = (char *)malloc(sizeof(char) * len + 1))) 54 | return (NULL); 55 | pos = 1; 56 | while (num != 0) 57 | { 58 | stock[len - pos++] = base[num % 16]; 59 | num /= 16; 60 | } 61 | stock[len] = '\0'; 62 | free(base); 63 | return (stock); 64 | } 65 | -------------------------------------------------------------------------------- /lib/libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:20:09 by loamar #+# #+# */ 9 | /* Updated: 2020/06/08 14:13:08 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!s) 21 | return (NULL); 22 | while (s[i] != '\0') 23 | { 24 | if (s[i] == c) 25 | return ((char *)(s + i)); 26 | i++; 27 | } 28 | if (s[i] == c) 29 | return ((char *)(s + i)); 30 | return (NULL); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/05 23:13:42 by loamar #+# #+# */ 9 | /* Updated: 2020/11/05 23:14:07 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(char *s1, char *s2) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') 21 | i++; 22 | return (s1[i] - s2[i]); 23 | } 24 | -------------------------------------------------------------------------------- /lib/libft/ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/14 19:25:24 by loamar #+# #+# */ 9 | /* Updated: 2020/01/14 19:25:32 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcpy(char *dest, const char *src) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (src[++i] && (dest[i] = src[i])) 21 | ; 22 | dest[i] = '\0'; 23 | return (dest); 24 | } 25 | -------------------------------------------------------------------------------- /lib/libft/ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/01/10 15:42:19 by loamar #+# #+# */ 9 | /* Updated: 2020/01/10 15:42:41 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strdel(char **as) 16 | { 17 | if (as) 18 | { 19 | free(*as); 20 | *as = NULL; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /lib/libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:20:22 by loamar #+# #+# */ 9 | /* Updated: 2020/01/17 11:09:15 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | size_t len; 18 | size_t i; 19 | char *dest; 20 | 21 | if (!s1) 22 | return (NULL); 23 | len = ft_strlen(s1); 24 | if (!(dest = (char *)malloc((len + 1) * sizeof(char)))) 25 | return (NULL); 26 | i = 0; 27 | while (i < len) 28 | { 29 | dest[i] = s1[i]; 30 | i++; 31 | } 32 | dest[i] = '\0'; 33 | return (dest); 34 | } 35 | -------------------------------------------------------------------------------- /lib/libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:20:33 by loamar #+# #+# */ 9 | /* Updated: 2021/03/29 01:26:33 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int check(char *s1, char *s2) 16 | { 17 | if (!s1 && !s2) 18 | return (-1); 19 | if (!s1) 20 | return (-1); 21 | if (!s2) 22 | return (-1); 23 | return (0); 24 | } 25 | 26 | static char *return_error(char *s1, char *s2) 27 | { 28 | if (!s1 && !s2) 29 | return (ft_strdup("\0")); 30 | if (!s1) 31 | return (ft_strdup(s2)); 32 | if (!s2) 33 | return (ft_strdup(s1)); 34 | return (NULL); 35 | } 36 | 37 | char *ft_strjoin(char *s1, char *s2) 38 | { 39 | size_t lens1; 40 | size_t lens2; 41 | char *dest; 42 | int i; 43 | 44 | if (check(s1, s2) == -1) 45 | return (return_error(s1, s2)); 46 | lens1 = ft_strlen(s1); 47 | lens2 = ft_strlen(s2); 48 | if (!(dest = (char *)malloc(sizeof(char) * (lens1 + lens2 + 1)))) 49 | return (0); 50 | i = 0; 51 | while (s1[i] != '\0') 52 | { 53 | dest[i] = s1[i]; 54 | i++; 55 | } 56 | i = 0; 57 | while (s2[i] != '\0') 58 | { 59 | dest[i + lens1] = s2[i]; 60 | i++; 61 | } 62 | dest[i + lens1] = '\0'; 63 | return (dest); 64 | } 65 | -------------------------------------------------------------------------------- /lib/libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:20:44 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:20:45 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t len; 18 | size_t len_d; 19 | size_t i; 20 | 21 | len = ft_strlen(src); 22 | if (!dst && dstsize == 0) 23 | return (len); 24 | len_d = ft_strlen(dst); 25 | if (dstsize <= len_d) 26 | return (len + dstsize); 27 | else 28 | len += len_d; 29 | i = 0; 30 | while (src[i] != '\0' && len_d < dstsize - 1) 31 | { 32 | dst[len_d] = src[i]; 33 | i++; 34 | len_d++; 35 | } 36 | dst[len_d] = '\0'; 37 | return (len); 38 | } 39 | -------------------------------------------------------------------------------- /lib/libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:20:54 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:20:55 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t i; 18 | size_t len; 19 | 20 | i = 0; 21 | len = ft_strlen(src); 22 | if (dstsize == 0) 23 | return (len); 24 | while (i < (dstsize - 1) && src[i] != '\0') 25 | { 26 | dst[i] = src[i]; 27 | i++; 28 | } 29 | dst[i] = '\0'; 30 | return (len); 31 | } 32 | -------------------------------------------------------------------------------- /lib/libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:21:04 by loamar #+# #+# */ 9 | /* Updated: 2020/01/16 20:17:40 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *str) 16 | { 17 | size_t len; 18 | 19 | if (!str) 20 | return (0); 21 | len = 0; 22 | while (str[len] != '\0') 23 | len++; 24 | return (len); 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:21:25 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:21:26 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) 16 | { 17 | char *dest; 18 | unsigned int i; 19 | 20 | if (!s || !f) 21 | return (NULL); 22 | if (!(dest = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1)))) 23 | return (NULL); 24 | i = 0; 25 | while (s[i] != '\0') 26 | { 27 | dest[i] = f(i, s[i]); 28 | i++; 29 | } 30 | dest[i] = '\0'; 31 | return (dest); 32 | } 33 | -------------------------------------------------------------------------------- /lib/libft/ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/05/01 12:51:36 by loamar #+# #+# */ 9 | /* Updated: 2021/05/01 12:51:39 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncat(char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | 19 | i = ft_strlen(s1); 20 | while (*s2 && n) 21 | { 22 | s1[i] = *s2; 23 | i++; 24 | s2++; 25 | n--; 26 | } 27 | s1[i] = '\0'; 28 | return (s1); 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:21:37 by loamar #+# #+# */ 9 | /* Updated: 2020/06/29 15:59:56 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | if (!n) 21 | return (0); 22 | while (s1[i] && s2[i] && s1[i] == s2[i] && i < (n - 1)) 23 | i++; 24 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 25 | } 26 | -------------------------------------------------------------------------------- /lib/libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:22:01 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:22:04 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | size_t len_ne; 20 | 21 | i = 0; 22 | len_ne = ft_strlen(needle); 23 | if (needle[0] == '\0') 24 | return ((char *)haystack); 25 | while (haystack[i] != '\0' && i < len) 26 | { 27 | j = 0; 28 | while (haystack[i + j] == needle[j] && (i + j) < len) 29 | { 30 | j++; 31 | if (j == len_ne) 32 | return ((char *)(haystack + i)); 33 | } 34 | i++; 35 | } 36 | return (0); 37 | } 38 | -------------------------------------------------------------------------------- /lib/libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:22:28 by loamar #+# #+# */ 9 | /* Updated: 2021/03/04 10:29:34 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int len; 18 | 19 | len = ft_strlen(s); 20 | if (s[len] == c) 21 | return ((char *)(s + len)); 22 | while (len > 0) 23 | { 24 | if (s[len - 1] == c) 25 | return ((char *)(s + len - 1)); 26 | len--; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/ft_strtoupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtoupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:22:43 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:22:45 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | char *ft_strtoupper(char *str) 14 | { 15 | int i; 16 | 17 | i = 0; 18 | while (str[i]) 19 | { 20 | if (str[i] >= 'a' && str[i] <= 'z') 21 | { 22 | str[i] = str[i] - 32; 23 | i++; 24 | } 25 | else 26 | i++; 27 | } 28 | return (str); 29 | } 30 | -------------------------------------------------------------------------------- /lib/libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:22:57 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:22:58 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_isset(const char c, const char *set) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (set[i] != '\0') 21 | { 22 | if (c == set[i]) 23 | return (1); 24 | i++; 25 | } 26 | return (0); 27 | } 28 | 29 | char *ft_strtrim(char const *s1, char const *set) 30 | { 31 | size_t len; 32 | size_t i; 33 | char *dest; 34 | 35 | if (!s1) 36 | return (NULL); 37 | if (!set) 38 | return (ft_strdup(s1)); 39 | len = ft_strlen(s1); 40 | i = 0; 41 | while (ft_isset(s1[i], set) == 1) 42 | i++; 43 | if (s1[i] == '\0') 44 | return (ft_strdup("")); 45 | while (ft_isset(s1[len - 1], set) == 1) 46 | len--; 47 | if (!(dest = ft_substr(s1, i, len - i))) 48 | return (NULL); 49 | return (dest); 50 | } 51 | -------------------------------------------------------------------------------- /lib/libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:23:16 by loamar #+# #+# */ 9 | /* Updated: 2021/04/02 13:35:46 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | size_t i; 18 | char *str; 19 | 20 | if (!s) 21 | return (NULL); 22 | if (!len || (unsigned int)ft_strlen(s) < start) 23 | { 24 | if (!(str = (char*)malloc(sizeof(char)))) 25 | return (NULL); 26 | str[0] = '\0'; 27 | return (str); 28 | } 29 | if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) 30 | return (NULL); 31 | i = 0; 32 | while (i < len && s[start] != '\0') 33 | str[i++] = s[start++]; 34 | str[i] = '\0'; 35 | return (str); 36 | } 37 | -------------------------------------------------------------------------------- /lib/libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:23:26 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:23:30 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_tolower(int c) 14 | { 15 | if ('A' <= c && c <= 'Z') 16 | c += 32; 17 | return (c); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:23:39 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:23:44 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_toupper(int c) 14 | { 15 | if ('a' <= c && c <= 'z') 16 | c -= 32; 17 | return (c); 18 | } 19 | -------------------------------------------------------------------------------- /lib/libft/ft_unsigned.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_unsigned.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2019/11/29 16:23:53 by loamar #+# #+# */ 9 | /* Updated: 2019/11/29 16:23:55 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | unsigned long ft_unsigned(long int n) 16 | { 17 | long int ret; 18 | 19 | ret = 4294967296; 20 | if (n < 0) 21 | { 22 | n = -n; 23 | n = ret - n; 24 | } 25 | return (n); 26 | } 27 | -------------------------------------------------------------------------------- /lib/libft/libft.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MickeyMighty/MiniShell/e952835751ea1744df3f3bd1c53ffd3b33b768c6/lib/libft/libft.a -------------------------------------------------------------------------------- /minishell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MickeyMighty/MiniShell/e952835751ea1744df3f3bd1c53ffd3b33b768c6/minishell -------------------------------------------------------------------------------- /minishell.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.minishell 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /minishell.dSYM/Contents/Resources/DWARF/minishell: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MickeyMighty/MiniShell/e952835751ea1744df3f3bd1c53ffd3b33b768c6/minishell.dSYM/Contents/Resources/DWARF/minishell -------------------------------------------------------------------------------- /srcs/builtins/export/export_check.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* export_check.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/03 22:39:18 by loamar #+# #+# */ 9 | /* Updated: 2021/04/13 16:22:03 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | static char *fill_first_step(char *str, char *first_step, 16 | int pos) 17 | { 18 | int dollar_case; 19 | char *second_step; 20 | 21 | dollar_case = 0; 22 | second_step = NULL; 23 | if (str[pos] == '\\' && str[pos + 1] != '\\') 24 | second_step = ft_strdup("\\\\"); 25 | else if (str[pos] == '$' && ft_isalpha(str[pos + 1]) == 0) 26 | { 27 | dollar_case = 1; 28 | second_step = ft_strdup("\\"); 29 | } 30 | else 31 | second_step = ft_substr(str, pos, 1); 32 | first_step = ft_free_strjoin(first_step, second_step); 33 | if (dollar_case == 1) 34 | { 35 | second_step = ft_substr(str, pos, 1); 36 | first_step = ft_free_strjoin(first_step, second_step); 37 | } 38 | return (first_step); 39 | } 40 | 41 | char *export_secondcontent(char *str, int start) 42 | { 43 | char *first_step; 44 | int pos; 45 | 46 | first_step = NULL; 47 | pos = start; 48 | while (str[pos]) 49 | { 50 | first_step = fill_first_step(str, first_step, pos); 51 | if (str[pos]) 52 | pos++; 53 | } 54 | return (first_step); 55 | } 56 | 57 | static int return_error_export(t_msh *msh, char *str, int pos) 58 | { 59 | if (str[pos] == '_' && pos == 0) 60 | return (ERROR); 61 | if ((ft_isdigit(str[pos]) == 1 && msh->utils->check == 0)) 62 | { 63 | return (return_error(ERROR_QTE, "export", str, 64 | ": not a valid identifier")); 65 | } 66 | if (msh->utils->check == 0 && (ft_isalpha(str[pos]) != 1) 67 | && ((ft_isdigit(str[pos]) == 1) || (str[pos] != '_'))) 68 | { 69 | return (return_error(ERROR_QTE, "export", str, 70 | ": not a valid identifier")); 71 | } 72 | if (ft_isalpha(str[pos]) == 1) 73 | msh->utils->check = 1; 74 | else if (msh->utils->check == 0) 75 | { 76 | return (return_error(ERROR_QTE, "export", str, 77 | ": not a valid identifier")); 78 | } 79 | return (SUCCESS); 80 | } 81 | 82 | int check_arg(t_msh *msh, char *str) 83 | { 84 | int pos; 85 | 86 | pos = 0; 87 | msh->utils->check = 0; 88 | if (str[pos] == '=') 89 | { 90 | return (return_error(ERROR_QTE, "export", str, 91 | ": not a valid identifier")); 92 | } 93 | if (str[pos] == '\0') 94 | { 95 | return (return_error(ERROR_QTE, "export", str, 96 | ": not a valid identifier")); 97 | } 98 | while (str[pos]) 99 | { 100 | if (str[pos] == '=') 101 | return (SUCCESS); 102 | if (return_error_export(msh, str, pos) == ERROR) 103 | return (ERROR); 104 | if (str[pos]) 105 | pos++; 106 | } 107 | return (SUCCESS); 108 | } 109 | -------------------------------------------------------------------------------- /srcs/builtins/export/my_export.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_export.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/12 18:04:14 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 11:12:55 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int check_first_content(char *content, char *str) 16 | { 17 | size_t pos; 18 | 19 | pos = 0; 20 | if (!content || !str) 21 | return (ERROR); 22 | while (str[pos] != '=' && str[pos] != '\0') 23 | pos++; 24 | if ((str[pos] == '\0') && (ft_strncmp(str, content, pos) == 0) 25 | && (pos == ft_strlen(content))) 26 | return (ERROR_EXPORT); 27 | else if ((str[pos] != '\0') && (ft_strncmp(str, content, pos) == 0) 28 | && (pos == ft_strlen(content))) 29 | return (SUCCESS); 30 | else 31 | return (ERROR); 32 | } 33 | 34 | int add_back(t_msh *msh, t_env_list *element, char *str) 35 | { 36 | if (!element->next) 37 | { 38 | ft_fill_end_env(msh->env_lair, sep_env(str, 0, EXPORT), 39 | sep_env(str, 1, EXPORT)); 40 | return (1); 41 | } 42 | return (0); 43 | } 44 | 45 | static int handler_check_export(t_msh *msh, t_list *element, int pos, 46 | int error) 47 | { 48 | while (element->tab_args[pos]) 49 | { 50 | if (check_arg(msh, element->tab_args[pos]) == ERROR) 51 | { 52 | error = 1; 53 | pos++; 54 | } 55 | else 56 | { 57 | push_to_env(msh, element->tab_args[pos]); 58 | pos++; 59 | } 60 | } 61 | if (error == 1) 62 | return (ERROR); 63 | return (SUCCESS); 64 | } 65 | 66 | int my_export(t_msh *msh, t_list *element) 67 | { 68 | int pos; 69 | int error; 70 | 71 | error = 0; 72 | pos = 1; 73 | if (!element) 74 | return (SUCCESS); 75 | if (ft_strcmp(element->content, "export") == 0 76 | && element->tab_args[1] == NULL) 77 | print_env(msh); 78 | else if (ft_strcmp(element->content, "export") == 0 79 | && element->tab_args[1] != NULL) 80 | { 81 | if (handler_check_export(msh, element, pos, error) == ERROR) 82 | return (ERROR); 83 | } 84 | return (SUCCESS); 85 | } 86 | -------------------------------------------------------------------------------- /srcs/builtins/export/print_export.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_export.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: lorenzoa +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/06 16:37:21 by lorenzoa #+# #+# */ 9 | /* Updated: 2021/04/16 12:31:23 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | static int check_start(t_msh *msh, t_env_list *element, char *str) 16 | { 17 | if (!element) 18 | { 19 | ft_fill_empty_env(msh->env_lair, sep_env(str, 0, EXPORT), 20 | sep_env(str, 1, EXPORT)); 21 | return (-1); 22 | } 23 | return (0); 24 | } 25 | 26 | void push_to_env(t_msh *msh, char *str) 27 | { 28 | t_env_list *element; 29 | int check; 30 | 31 | check = 0; 32 | element = msh->env_lair->start; 33 | if (check_start(msh, element, str) == -1) 34 | return ; 35 | while (element) 36 | { 37 | check = check_first_content(element->first_content, str); 38 | if (check == SUCCESS) 39 | { 40 | if (element->second_content) 41 | free(element->second_content); 42 | element->second_content = ft_strdup(sep_env(str, 1, EXPORT)); 43 | return ; 44 | } 45 | else if ((check == ERROR) && add_back(msh, element, str) == 1) 46 | return ; 47 | else if (check == ERROR_EXPORT) 48 | return ; 49 | else if (element->next) 50 | element = element->next; 51 | } 52 | } 53 | 54 | static void print_content(t_env_list *env, int limit) 55 | { 56 | if (env->first_content && env->first_content[0] == limit) 57 | { 58 | ft_putstr_fd("declare -x ", 1); 59 | ft_putstr_fd(env->first_content, 1); 60 | if (env->second_content) 61 | { 62 | ft_putstr_fd("=", 1); 63 | ft_putstr_fd("\"", 1); 64 | ft_putstr_fd(env->second_content, 1); 65 | ft_putendl_fd("\"", 1); 66 | } 67 | else 68 | ft_putstr_fd("\n", 1); 69 | } 70 | } 71 | 72 | void print_env(t_msh *msh) 73 | { 74 | t_env_list *env; 75 | int limit; 76 | 77 | limit = 0; 78 | if (!msh->env_lair->start) 79 | return ; 80 | while (limit <= 255) 81 | { 82 | env = msh->env_lair->start; 83 | while (env && env->first_content && env->first_content[0] != limit) 84 | env = env->next; 85 | while (env) 86 | { 87 | print_content(env, limit); 88 | env = env->next; 89 | } 90 | limit++; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /srcs/builtins/handler_builtins.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_builtins.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/12 18:04:49 by loamar #+# #+# */ 9 | /* Updated: 2021/04/07 12:13:24 by lorenzoamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int ft_handler_builtins(t_msh *msh, t_list *element) 16 | { 17 | if (ft_strcmp(element->content, "echo") == 0) 18 | return (my_echo(msh, element)); 19 | else if (ft_strcmp(element->content, "cd") == 0) 20 | return (my_cd(msh, element)); 21 | else if (ft_strcmp(element->content, "pwd") == 0) 22 | return (my_pwd()); 23 | else if (ft_strcmp(element->content, "export") == 0) 24 | return (my_export(msh, element)); 25 | else if (ft_strcmp(element->content, "env") == 0) 26 | return (my_env(msh)); 27 | else if (ft_strcmp(element->content, "unset") == 0) 28 | return (my_unset(msh, element)); 29 | else if (ft_strcmp(element->content, "exit") == 0) 30 | return (my_exit(msh, element)); 31 | else 32 | return (ERROR_BUILTINS); 33 | } 34 | -------------------------------------------------------------------------------- /srcs/builtins/my_echo.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_echo.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/12 06:06:53 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 11:12:40 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int check_option(char *str) 16 | { 17 | int pos; 18 | 19 | pos = 0; 20 | if (ft_strncmp("-n", str, 2) == 0) 21 | { 22 | pos = 2; 23 | if (str[pos]) 24 | { 25 | while (str[pos] == 'n') 26 | pos++; 27 | if (str[pos] == '\0') 28 | return (1); 29 | else if (str[pos] != 'n') 30 | return (0); 31 | } 32 | return (1); 33 | } 34 | return (0); 35 | } 36 | 37 | static void print_echo(t_msh *msh, t_list *element) 38 | { 39 | ft_putstr_fd(element->tab_args[msh->utils->pos_echo], 1); 40 | if (element->tab_args[msh->utils->pos_echo + 1] != NULL 41 | && (check_specase(msh, msh->utils->pos_echo) == 0)) 42 | ft_putstr_fd(" ", 1); 43 | } 44 | 45 | int my_echo(t_msh *msh, t_list *element) 46 | { 47 | msh->utils->option_n = 0; 48 | msh->utils->pos_echo = 1; 49 | if (element->tab_args[msh->utils->pos_echo] == NULL) 50 | { 51 | ft_putstr_fd("\n", 1); 52 | return (SUCCESS); 53 | } 54 | else 55 | { 56 | while (element->tab_args[msh->utils->pos_echo] && 57 | check_option(element->tab_args[msh->utils->pos_echo]) == 1) 58 | { 59 | msh->utils->option_n = 1; 60 | msh->utils->pos_echo++; 61 | } 62 | while (element->tab_args[msh->utils->pos_echo]) 63 | { 64 | print_echo(msh, element); 65 | msh->utils->pos_echo++; 66 | } 67 | if (msh->utils->option_n == 0) 68 | ft_putstr_fd("\n", 1); 69 | } 70 | return (SUCCESS); 71 | } 72 | -------------------------------------------------------------------------------- /srcs/builtins/my_env.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_env.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tidminta +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/12 15:41:31 by tidminta #+# #+# */ 9 | /* Updated: 2021/04/16 11:12:34 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int my_env(t_msh *msh) 16 | { 17 | t_env_list *env; 18 | 19 | if (!msh->env_lair->start) 20 | return (SUCCESS); 21 | env = msh->env_lair->start; 22 | while (env) 23 | { 24 | if (env->first_content && env->second_content) 25 | { 26 | ft_putstr_fd(env->first_content, 1); 27 | ft_putstr_fd("=", 1); 28 | ft_putendl_fd(env->second_content, 1); 29 | } 30 | env = env->next; 31 | } 32 | return (SUCCESS); 33 | } 34 | -------------------------------------------------------------------------------- /srcs/builtins/my_exit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_exit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/04 09:12:18 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 19:20:14 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static long long atoi_exit(const char *str, int i, int *pbm) 16 | { 17 | int j; 18 | long neg; 19 | long long sum; 20 | 21 | neg = 1; 22 | sum = 0; 23 | j = 0; 24 | if (str[i] && (str[i] == '-' || str[i] == '+')) 25 | if (str[i++] == '-') 26 | neg *= -1; 27 | while (str[i] && ((str[i] == ' ') || str[i] == '0')) 28 | i++; 29 | while (str[i] >= '0' && str[i] <= '9' && ++j) 30 | { 31 | sum = (sum * 10) + (str[i] - 48); 32 | if (((i == 18 && neg == 1) && (str[i] > '7' && str[i] <= '9')) 33 | || ((i == 19 && neg == -1) && (str[i] == '9'))) 34 | *pbm = 1; 35 | i++; 36 | } 37 | while (str[i++]) 38 | j++; 39 | if ((j > 19 && neg == 1) || (j > 20 && neg == -1)) 40 | *pbm = 1; 41 | return (sum * neg); 42 | } 43 | 44 | static void exit_error_numeric(t_msh *msh, char *arg) 45 | { 46 | return_error(ERROR_CMD, "exit", arg, ": numeric argument required"); 47 | g_status = 2; 48 | exit_cmd(msh); 49 | } 50 | 51 | static void exit_arg(t_msh *msh, char *str) 52 | { 53 | int pos; 54 | 55 | pos = 0; 56 | if (str[pos] == '-' || str[pos] == '+') 57 | pos++; 58 | while (str[pos]) 59 | { 60 | if (str[pos] != '\f' && str[pos] != '\t' && str[pos] != '\r' 61 | && str[pos] != '\v' && str[pos] != ' ') 62 | { 63 | if (str[pos] < 48 || str[pos] > 57) 64 | exit_error_numeric(msh, str); 65 | } 66 | pos++; 67 | } 68 | } 69 | 70 | static void exit_numeric(t_msh *msh, t_list *element, int pbm, 71 | int code2) 72 | { 73 | code2 = atoi_exit(element->tab_args[1], 0, &pbm); 74 | if (pbm == 1) 75 | exit_error_numeric(msh, element->tab_args[1]); 76 | g_status = code2 % 256; 77 | } 78 | 79 | int my_exit(t_msh *msh, t_list *element) 80 | { 81 | size_t i; 82 | int pbm; 83 | long long code2; 84 | 85 | pbm = 0; 86 | code2 = 0; 87 | i = 1; 88 | g_status = 0; 89 | if (element->tab_args[1] == NULL) 90 | exit_cmd(msh); 91 | exit_arg(msh, element->tab_args[1]); 92 | while (element->tab_args[i]) 93 | i++; 94 | if (i > 2) 95 | { 96 | return_error(ERROR_CMD, "exit", NULL, "too many arguments"); 97 | g_status = 1; 98 | } 99 | else 100 | exit_numeric(msh, element, pbm, code2); 101 | exit_cmd(msh); 102 | return (SUCCESS); 103 | } 104 | -------------------------------------------------------------------------------- /srcs/builtins/my_pwd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_pwd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: tidminta +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/12 16:01:14 by tidminta #+# #+# */ 9 | /* Updated: 2021/04/16 11:11:32 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int my_pwd(void) 16 | { 17 | char *pwd; 18 | 19 | if (!(pwd = getcwd(NULL, PATH_MAX))) 20 | return (SUCCESS); 21 | ft_putendl_fd(pwd, 1); 22 | free(pwd); 23 | pwd = NULL; 24 | return (SUCCESS); 25 | } 26 | -------------------------------------------------------------------------------- /srcs/builtins/my_unset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_unset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/25 21:56:45 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 11:12:27 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int pop_env(t_msh *msh, int pos) 16 | { 17 | t_env_list *delete_element; 18 | t_env_list *now_element; 19 | 20 | msh->utils->pos = 1; 21 | if (msh->env_lair->size == 0) 22 | return (-1); 23 | if (msh->env_lair->size == 1) 24 | return (clear_env(msh->env_lair)); 25 | now_element = msh->env_lair->start; 26 | while (msh->utils->pos++ < pos) 27 | now_element = now_element->next; 28 | delete_element = now_element; 29 | if (now_element->next != NULL) 30 | now_element->next->previous = now_element->previous; 31 | else 32 | msh->env_lair->end = now_element->previous; 33 | if (now_element->previous != NULL) 34 | now_element->previous->next = now_element->next; 35 | if (delete_element->first_content) 36 | free(delete_element->first_content); 37 | if (delete_element->second_content) 38 | free(delete_element->second_content); 39 | free(delete_element); 40 | msh->env_lair->size--; 41 | return (0); 42 | } 43 | 44 | static int find_env(t_msh *msh, char *content, int index) 45 | { 46 | t_env_list *element; 47 | 48 | if (!content) 49 | return (-1); 50 | element = msh->env_lair->start; 51 | if (!element || element == NULL) 52 | return (-1); 53 | index = 1; 54 | while (element) 55 | { 56 | if (element && element->first_content) 57 | { 58 | if (ft_strcmp(content, element->first_content) == 0) 59 | return (index); 60 | } 61 | if (element) 62 | element = element->next; 63 | index++; 64 | } 65 | return (-1); 66 | } 67 | 68 | int my_unset(t_msh *msh, t_list *element) 69 | { 70 | size_t pos; 71 | ssize_t index; 72 | 73 | g_status = 0; 74 | if (!element->tab_args[1]) 75 | return (SUCCESS); 76 | pos = 1; 77 | while (element->tab_args[pos]) 78 | { 79 | index = find_env(msh, element->tab_args[pos], pos); 80 | if (index != -1) 81 | { 82 | if (element->tab_args[pos]) 83 | pop_env(msh, index); 84 | } 85 | else 86 | { 87 | return_error(ERROR_QTE, "unset", element->tab_args[pos], 88 | ": not a valid identifier"); 89 | g_status = 1; 90 | } 91 | pos++; 92 | } 93 | return (SUCCESS); 94 | } 95 | -------------------------------------------------------------------------------- /srcs/cmd/check/check_quoteanddollar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_quoteanddollar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: lorenzoa +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/06 18:02:00 by lorenzoa #+# #+# */ 9 | /* Updated: 2021/04/22 22:49:26 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int data_check_qte(t_msh *msh, int count, int pos, int flag) 16 | { 17 | if (flag == 0) 18 | { 19 | while (msh->data->prompt_data[count][pos] != '\'') 20 | { 21 | if (msh->data->prompt_data[count][pos] == '\0') 22 | return (-1); 23 | pos++; 24 | } 25 | } 26 | else if (flag == 1) 27 | { 28 | pos++; 29 | while (msh->data->prompt_data[count][pos] != '\"') 30 | { 31 | if (msh->data->prompt_data[count][pos] == '\0') 32 | return (-1); 33 | pos++; 34 | } 35 | } 36 | return (pos); 37 | } 38 | 39 | char *check_dollar_case(t_msh *msh, char *str) 40 | { 41 | if (str[msh->utils->pos + 1] == '?') 42 | { 43 | msh->utils->pos++; 44 | return (ft_itoa(g_status)); 45 | } 46 | if (str[msh->utils->pos + 1] == '$') 47 | { 48 | msh->utils->pos++; 49 | return (ft_itoa(4714)); 50 | } 51 | return (NULL); 52 | } 53 | 54 | int check_backslash(t_msh *msh, char *str) 55 | { 56 | if (str[msh->utils->pos] == '\\') 57 | { 58 | if (str[msh->utils->pos + 1] == '\0') 59 | return (-1); 60 | msh->utils->pos++; 61 | } 62 | return (1); 63 | } 64 | -------------------------------------------------------------------------------- /srcs/cmd/check/fill_second_step.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fill_second_step.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/27 16:55:15 by loamar #+# #+# */ 9 | /* Updated: 2021/04/13 16:11:24 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | char *get_tmp_value(t_msh *msh, char *str, int len) 16 | { 17 | int lair; 18 | int after_len; 19 | char *tmp; 20 | 21 | after_len = 0; 22 | if (str[len + 1] != '\0') 23 | after_len = 1; 24 | lair = len; 25 | len = (len - (msh->utils->pos - 1)); 26 | tmp = ft_substr(str, msh->utils->pos, len); 27 | if (after_len == 1) 28 | msh->utils->pos = len + 1; 29 | else 30 | msh->utils->pos = len; 31 | tmp = return_element(msh, tmp, msh->utils->key); 32 | if (msh->utils->key == YESQTE && after_len == 1) 33 | msh->utils->pos++; 34 | msh->utils->pos = lair; 35 | return (tmp); 36 | } 37 | 38 | char *fill_step_qte(t_msh *msh, char *str, char *second_step) 39 | { 40 | if (msh->utils->quote == DQUOTE && str[msh->utils->pos] == '\\' 41 | && (str[msh->utils->pos + 1] == DQUOTE 42 | || str[msh->utils->pos + 1] == '\\' 43 | || str[msh->utils->pos + 1] == '$')) 44 | { 45 | msh->utils->backslash_dollar = 1; 46 | msh->utils->pos++; 47 | } 48 | second_step = fill_second_step_quote(msh, str, second_step); 49 | if (second_step == NULL 50 | && ((str[msh->utils->pos] != msh->utils->quote) 51 | || ((str[msh->utils->pos] == msh->utils->quote) 52 | && (str[msh->utils->pos - 1] == '\\')))) 53 | second_step = ft_substr(str, msh->utils->pos, 1); 54 | return (second_step); 55 | } 56 | 57 | char *fill_second_step_quote(t_msh *msh, char *str, char *second_step) 58 | { 59 | if ((str[msh->utils->pos] == '$') && msh->utils->quote != SQUOTE 60 | && str[msh->utils->pos + 1] && msh->utils->backslash_dollar == 0) 61 | second_step = return_dollar(msh, str, YESQTE); 62 | else 63 | return (NULL); 64 | return (second_step); 65 | } 66 | 67 | char *fill_second_step_content(t_msh *msh, char *str, char *second_step) 68 | { 69 | if (str[msh->utils->pos] == DQUOTE || str[msh->utils->pos] == SQUOTE) 70 | { 71 | msh->utils->quote = str[msh->utils->pos]; 72 | if (str[msh->utils->pos + 1] == msh->utils->quote 73 | && (str[msh->utils->pos + 2] == '\0' 74 | || str[msh->utils->pos + 2] == DQUOTE 75 | || str[msh->utils->pos + 2] == SQUOTE)) 76 | { 77 | msh->utils->pos++; 78 | second_step = ft_strdup("\0"); 79 | } 80 | else 81 | second_step = return_quote(msh, str); 82 | } 83 | else if (str[msh->utils->pos] == '$') 84 | second_step = return_dollar(msh, str, NOQTE); 85 | else 86 | return (NULL); 87 | return (second_step); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/cmd/check/special_case_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* special_case_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/02 15:25:23 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 17:15:16 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int check_end(char *str, int pos) 16 | { 17 | if (str[pos + 1] != '\0' && (ft_isalnum(str[pos + 1]) == 0)) 18 | return (1); 19 | else 20 | return (0); 21 | } 22 | 23 | int return_pos(t_msh *msh, char *str, int pos) 24 | { 25 | while (str[pos] == ' ' && str[pos + 1] == ' ') 26 | pos++; 27 | if (str[pos] == ' ' && str[pos + 1] == '\0') 28 | pos = put_pos_check(msh, str, pos, 2); 29 | return (pos); 30 | } 31 | 32 | static int *realloc_int_tab(int *tabs, int size) 33 | { 34 | int *fresh; 35 | int pos; 36 | 37 | pos = 0; 38 | if (size == 1 && tabs == NULL) 39 | { 40 | if (!(fresh = (int *)malloc(sizeof(int) * (1)))) 41 | return (NULL); 42 | } 43 | else 44 | { 45 | if (!(fresh = (int *)malloc(sizeof(int) * (size)))) 46 | return (NULL); 47 | while (pos < (size - 1)) 48 | { 49 | fresh[pos] = tabs[pos]; 50 | pos++; 51 | } 52 | free(tabs); 53 | } 54 | return (fresh); 55 | } 56 | 57 | int *add_empty_dollar(t_msh *msh, int add) 58 | { 59 | if (msh->utils->size_tab == 0) 60 | { 61 | msh->utils->tab_specase = realloc_int_tab(msh->utils->tab_specase, 1); 62 | if (msh->utils->tab_specase == NULL) 63 | return (NULL); 64 | msh->utils->tab_specase[0] = add; 65 | msh->utils->size_tab = 1; 66 | } 67 | else 68 | { 69 | msh->utils->tab_specase = realloc_int_tab(msh->utils->tab_specase, 70 | msh->utils->size_tab + 1); 71 | if (msh->utils->tab_specase == NULL) 72 | return (NULL); 73 | msh->utils->tab_specase[msh->utils->size_tab] = add; 74 | msh->utils->size_tab++; 75 | } 76 | return (msh->utils->tab_specase); 77 | } 78 | 79 | int check_specase(t_msh *msh, int pos) 80 | { 81 | int limite; 82 | 83 | limite = 0; 84 | if (msh->utils->tab_specase == NULL) 85 | return (0); 86 | while (limite <= (msh->utils->size_tab - 1)) 87 | { 88 | if (msh->utils->tab_specase[limite] == pos) 89 | return (1); 90 | limite++; 91 | } 92 | return (0); 93 | } 94 | -------------------------------------------------------------------------------- /srcs/cmd/exec_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* exec_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/11 22:57:42 by loamar #+# #+# */ 9 | /* Updated: 2021/04/22 23:17:02 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void status_child(void) 16 | { 17 | pid_t pid; 18 | 19 | pid = 0; 20 | pid = g_pid(GET, 0); 21 | if (WIFEXITED(pid)) 22 | g_status = WEXITSTATUS(pid); 23 | if (WIFSIGNALED(pid)) 24 | { 25 | g_status = WTERMSIG(pid); 26 | if (g_status != 131) 27 | g_status += 128; 28 | } 29 | g_pid(SET, pid); 30 | } 31 | 32 | static int check_permission_exec(t_msh *msh, t_list *cmd, char **env, 33 | int lock) 34 | { 35 | char *exec_path; 36 | 37 | exec_path = NULL; 38 | if (cmd == NULL) 39 | return (ERROR); 40 | exec_path = get_exec_path(msh, cmd->content); 41 | if (!exec_path) 42 | lock = check_permission(cmd->content); 43 | else 44 | lock = check_permission(exec_path); 45 | if (lock == FALSE) 46 | { 47 | ft_strdel(&exec_path); 48 | return (ERROR); 49 | } 50 | if (msh->utils->pipe == 0) 51 | g_pid(SET, fork()); 52 | if (g_pid(GET, 0) == 0) 53 | child_process(msh, cmd, env, exec_path); 54 | else 55 | parent_process(); 56 | if (exec_path) 57 | free(exec_path); 58 | msh->utils->pipe = 0; 59 | return (SUCCESS); 60 | } 61 | 62 | int exec_cmd(t_msh *msh, t_list *cmd, char **env, int pipe) 63 | { 64 | int lock; 65 | int status; 66 | 67 | if (ft_strcmp(cmd->content, "\0") == 0) 68 | { 69 | if (cmd->next && cmd->next->token != SEPARATOR) 70 | cmd = cmd->next; 71 | else 72 | return (SUCCESS); 73 | } 74 | lock = TRUE; 75 | g_error(SET, SUCCESS); 76 | msh->utils->pipe = pipe; 77 | status = ft_handler_builtins(msh, cmd); 78 | if (status == SUCCESS) 79 | g_status = status; 80 | else if (status == ERROR_BUILTINS) 81 | if (check_permission_exec(msh, cmd, env, lock) == ERROR) 82 | return (ERROR); 83 | if (pipe == 0) 84 | g_pid(SET, 0); 85 | if (g_error(GET, 0) == ERROR) 86 | return (ERROR); 87 | return (SUCCESS); 88 | } 89 | -------------------------------------------------------------------------------- /srcs/cmd/handler_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/11 22:57:33 by loamar #+# #+# */ 9 | /* Updated: 2021/04/22 22:53:04 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int error_cmd(t_list *element) 16 | { 17 | if ((ft_strcmp(element->content, "|") == 0) 18 | || (ft_strcmp(element->content, "&") == 0) 19 | || (ft_strcmp(element->content, ";") == 0)) 20 | { 21 | g_error(SET, ERROR); 22 | g_status = 2; 23 | return (return_error(ERROR_TOKEN, element->content, NULL, 24 | "syntax error near unexpected token")); 25 | } 26 | else if ((ft_strcmp(element->content, ">") == 0) 27 | || (ft_strcmp(element->content, ">>") == 0) 28 | || (ft_strcmp(element->content, "<") == 0) 29 | || (ft_strcmp(element->content, "!") == 0)) 30 | { 31 | g_error(SET, ERROR); 32 | g_status = 2; 33 | return (return_error(ERROR_TOKEN, "newline", NULL, 34 | "syntax error near unexpected token")); 35 | } 36 | return (ERROR); 37 | } 38 | 39 | static t_list *sort_cmd(t_msh *msh, t_list *element, char **env) 40 | { 41 | if (get_value_sep(element->next->content) == PIPE) 42 | element = multi_pipe(msh, element, env); 43 | else if (get_value_sep(element->next->content) == REDIR) 44 | { 45 | if (element->next->next && element->next->next->next 46 | && (get_value_sep(element->next->content) == PIPE)) 47 | { 48 | msh->utils->pipe = 1; 49 | msh->utils->multi_pipe = 0; 50 | element = multi_pipe(msh, element, env); 51 | } 52 | else 53 | element = redirections(msh, element, env); 54 | } 55 | else if (get_value_sep(element->next->content) == SEMICOLON) 56 | { 57 | exec_cmd(msh, element, env, 0); 58 | element = element->next; 59 | } 60 | return (element); 61 | } 62 | 63 | static int check_return_error(void) 64 | { 65 | if (g_error_msg(GET, 0) == ERROR_MULTI) 66 | { 67 | return (return_error(ERROR_MSG, NULL, NULL, 68 | "syntax error multiligne.")); 69 | } 70 | return (SUCCESS); 71 | } 72 | 73 | int handler_cmd(t_msh *msh, char **env) 74 | { 75 | t_list *element; 76 | 77 | g_error_msg(SET, 0); 78 | element = msh->lair_list->start; 79 | while (element != NULL) 80 | { 81 | msh->utils->no_space = 0; 82 | msh->utils->pipe = 0; 83 | if (element->token == CMD) 84 | element = check_block_cmd(msh, element); 85 | msh->utils->pos = 0; 86 | if (check_return_error() == ERROR) 87 | return (ERROR); 88 | if (element->next != NULL && element->next->token == SEPARATOR) 89 | { 90 | element = sort_cmd(msh, element, env); 91 | if (check_return_error() == ERROR) 92 | return (ERROR); 93 | } 94 | else 95 | exec_cmd(msh, element, env, 0); 96 | if (element) 97 | element = element->next; 98 | } 99 | return (SUCCESS); 100 | } 101 | -------------------------------------------------------------------------------- /srcs/cmd/handler_exec_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_exec_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: lorenzoa +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/06 17:23:29 by lorenzoa #+# #+# */ 9 | /* Updated: 2021/04/22 23:17:17 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int is_executable(struct stat permstat, int flag, 16 | char *cmd) 17 | { 18 | int lock; 19 | 20 | lock = TRUE; 21 | flag = permstat.st_mode & S_IFMT; 22 | g_status = 126; 23 | if ((permstat.st_mode & S_IFMT) == S_IFREG) 24 | { 25 | if ((permstat.st_mode & S_IXUSR) == 0) 26 | { 27 | return_error(ERROR_CMD, cmd, NULL, "Permission denied"); 28 | lock = FALSE; 29 | } 30 | } 31 | if (flag == S_IFDIR) 32 | { 33 | return_error(ERROR_CMD, cmd, NULL, "Is a directory"); 34 | lock = FALSE; 35 | } 36 | return (lock); 37 | } 38 | 39 | int check_permission(char *cmd) 40 | { 41 | struct stat permstat; 42 | int lock; 43 | int flag; 44 | 45 | ft_bzero(&permstat, sizeof(struct stat)); 46 | flag = stat(cmd, &permstat); 47 | if (flag == ERROR) 48 | { 49 | g_status = 127; 50 | g_return(SET, ERROR); 51 | if ((ft_strncmp(cmd, "./", 2) == 0) || cmd[0] == '/') 52 | return_error(ERROR_CMD, cmd, NULL, ": No such file or directory"); 53 | else 54 | return_error(ERROR_CMD, cmd, NULL, "command not found"); 55 | return (FALSE); 56 | } 57 | lock = is_executable(permstat, flag, cmd); 58 | return (lock); 59 | } 60 | 61 | void child_process(t_msh *msh, t_list *cmd, char **env, char *exec_path) 62 | { 63 | int ret; 64 | 65 | if (!exec_path) 66 | exec_path = ft_strdup(cmd->content); 67 | cmd->tab_args[0] = ft_strdup(exec_path); 68 | ret = execve(exec_path, cmd->tab_args, env); 69 | free(exec_path); 70 | if (msh->utils->pipe == 1) 71 | free_all(msh, EXIT); 72 | exit(ret); 73 | } 74 | 75 | void parent_process(void) 76 | { 77 | int child_status; 78 | 79 | child_status = 0; 80 | wait(&child_status); 81 | signal(SIGINT, SIG_IGN); 82 | if (WIFSIGNALED(child_status) && (g_status != 130 83 | && g_status != 131)) 84 | { 85 | ft_putstr_fd("\n", 2); 86 | g_status = WTERMSIG(child_status); 87 | } 88 | if (WIFEXITED(child_status)) 89 | g_return(SET, child_status); 90 | if (g_status != 130 && g_status != 131) 91 | g_status = WEXITSTATUS(child_status); 92 | } 93 | -------------------------------------------------------------------------------- /srcs/cmd/pipe_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pipe_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/24 07:42:36 by loamar #+# #+# */ 9 | /* Updated: 2021/04/22 23:21:20 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static t_list *return_multi_pipe(t_msh *msh, t_list *element, char **env) 16 | { 17 | if (element->next && (get_value_sep(element->next->content) == PIPE)) 18 | { 19 | if (!element->next->next) 20 | { 21 | g_error_msg(SET, ERROR); 22 | return (NULL); 23 | } 24 | msh->utils->backup_fd = ft_pipe(msh, element, env, 25 | msh->utils->backup_fd); 26 | element = element->next->next; 27 | if (element->token == CMD) 28 | element = check_block_cmd(msh, element); 29 | msh->utils->backup_fd = ft_pipe(msh, element, env, 30 | msh->utils->backup_fd); 31 | } 32 | else if (element->next && (get_value_sep(element->next->content) == REDIR)) 33 | element = redirections(msh, element, env); 34 | return (element); 35 | } 36 | 37 | t_list *multi_pipe(t_msh *msh, t_list *element, char **env) 38 | { 39 | msh->utils->backup_fd = 0; 40 | msh->utils->backup_fd = dup(0); 41 | while (element) 42 | { 43 | element = return_multi_pipe(msh, element, env); 44 | if (g_error_msg(GET, 0) == ERROR) 45 | return (NULL); 46 | if (element->next 47 | && (get_value_sep(element->next->content) != PIPE)) 48 | { 49 | element = element->next; 50 | return (element); 51 | } 52 | else if (!element->next) 53 | return (element); 54 | } 55 | close(msh->utils->backup_fd); 56 | return (element); 57 | } 58 | 59 | static void pipe_child(t_list *element, int pipefd[2], int backup_fd) 60 | { 61 | close(pipefd[0]); 62 | close(0); 63 | dup(backup_fd); 64 | close(backup_fd); 65 | if (element->next != NULL && element->next->next != NULL 66 | && (get_value_sep(element->next->content) == PIPE)) 67 | { 68 | close(1); 69 | dup(pipefd[1]); 70 | close(pipefd[1]); 71 | } 72 | } 73 | 74 | static int bad_fork(int *pipefd, int backup_fd) 75 | { 76 | close(pipefd[0]); 77 | close(pipefd[1]); 78 | close(backup_fd); 79 | return (-1); 80 | } 81 | 82 | int ft_pipe(t_msh *msh, t_list *element, char **env, int backup_fd) 83 | { 84 | int pipefd[2]; 85 | pid_t pid; 86 | 87 | pid = 0; 88 | pipefd[0] = -1; 89 | pipefd[1] = -1; 90 | if (pipe(pipefd) == -1) 91 | return (-1); 92 | g_pid(SET, fork()); 93 | if (g_pid(GET, 0) < 0) 94 | return (bad_fork(pipefd, backup_fd)); 95 | if (g_pid(GET, 0) == 0) 96 | { 97 | pipe_child(element, pipefd, backup_fd); 98 | exec_cmd(msh, element, env, 1); 99 | free_all(msh, EXIT); 100 | exit(g_status); 101 | } 102 | pid = g_pid(GET, 0); 103 | wait(&pid); 104 | g_pid(SET, pid); 105 | status_child(); 106 | close(backup_fd); 107 | close(pipefd[1]); 108 | return (pipefd[0]); 109 | } 110 | -------------------------------------------------------------------------------- /srcs/cmd/redir_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* redir_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/26 10:16:20 by loamar #+# #+# */ 9 | /* Updated: 2021/04/22 22:58:11 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int get_type_redir(char *content) 16 | { 17 | if (!content) 18 | return (ERROR); 19 | if (ft_strcmp(content, "<") == 0) 20 | return (CHEVRONL); 21 | else if (ft_strcmp(content, ">") == 0) 22 | return (CHEVRONR); 23 | else if (ft_strcmp(content, ">>") == 0) 24 | return (CHEVROND); 25 | else 26 | return (ERROR); 27 | } 28 | 29 | int return_redir_error(t_list *element, int fd, int i) 30 | { 31 | char *join; 32 | 33 | join = ft_strjoin(element->tab_args[i], ": "); 34 | return_error(ERROR_ERRNO, join, NULL, strerror(errno)); 35 | g_error(SET, ERROR); 36 | free(join); 37 | return (fd); 38 | } 39 | 40 | static t_list *exec_redirection(t_msh *msh, t_list *element, int fd, 41 | char **env) 42 | { 43 | int child_status; 44 | 45 | child_status = 0; 46 | if (fork() == 0) 47 | { 48 | dup2(fd, msh->utils->redirection); 49 | close(fd); 50 | exec_cmd(msh, element->previous, env, 0); 51 | element = element->next; 52 | free_all(msh, EXIT); 53 | exit(g_status); 54 | } 55 | else 56 | { 57 | wait(&child_status); 58 | g_status = WEXITSTATUS(child_status); 59 | } 60 | return (element); 61 | } 62 | 63 | static int check_next(t_list *element) 64 | { 65 | if (!element->next->next) 66 | { 67 | error_cmd(element->next); 68 | return (-1); 69 | } 70 | return (0); 71 | } 72 | 73 | t_list *redirections(t_msh *msh, t_list *element, char **env) 74 | { 75 | int fd; 76 | int type; 77 | 78 | type = 0; 79 | msh->utils->redirection = 1; 80 | if (check_next(element) == -1) 81 | return (NULL); 82 | if (element->next && element->next->token == SEPARATOR) 83 | type = get_type_redir(element->next->content); 84 | if (type == ERROR) 85 | return (NULL); 86 | if (!element->next->next && element->next->next->token != CMD) 87 | { 88 | error_cmd(element->next); 89 | return (NULL); 90 | } 91 | if (element->next && type == CHEVRONL) 92 | msh->utils->redirection = 0; 93 | element = element->next; 94 | fd = create_file(element->next, type); 95 | if (fd == ERROR) 96 | return (NULL); 97 | return (exec_redirection(msh, element, fd, env)); 98 | } 99 | -------------------------------------------------------------------------------- /srcs/cmd/redir_file_cmd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* redir_file_cmd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/02/24 07:42:13 by loamar #+# #+# */ 9 | /* Updated: 2021/04/10 10:40:52 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int fd_simple_redirection_right(t_list *element) 16 | { 17 | int i; 18 | int fd; 19 | 20 | i = 1; 21 | if (element->content) 22 | { 23 | fd = open(element->content, O_CREAT | O_RDWR | O_TRUNC, 0644); 24 | if (fd == ERROR) 25 | return (return_redir_error(element, fd, i)); 26 | while (element->tab_args[i]) 27 | { 28 | fd = open(element->tab_args[i], O_CREAT | O_RDWR | O_TRUNC, 0644); 29 | if (fd == ERROR) 30 | return (return_redir_error(element, fd, i)); 31 | i++; 32 | } 33 | return (fd); 34 | } 35 | return (-1); 36 | } 37 | 38 | static int fd_double_redirection_right(t_list *element) 39 | { 40 | int i; 41 | int fd; 42 | 43 | i = 1; 44 | if (element->content) 45 | { 46 | fd = open(element->content, O_CREAT | O_RDWR | O_APPEND, 0644); 47 | if (fd == ERROR) 48 | return (return_redir_error(element, fd, i)); 49 | while (element->tab_args[i]) 50 | { 51 | fd = open(element->tab_args[i], O_CREAT | O_RDWR | O_APPEND, 0644); 52 | if (fd == ERROR) 53 | return (return_redir_error(element, fd, i)); 54 | i++; 55 | } 56 | return (fd); 57 | } 58 | return (-1); 59 | } 60 | 61 | static int fd_simple_redirection_left(t_list *element) 62 | { 63 | int i; 64 | int fd; 65 | 66 | i = 1; 67 | if (element->content) 68 | { 69 | fd = open(element->content, O_RDONLY); 70 | if (fd == ERROR) 71 | return (return_redir_error(element, fd, i)); 72 | while (element->tab_args[i]) 73 | { 74 | fd = open(element->tab_args[i], O_RDONLY); 75 | if (fd == ERROR) 76 | return (return_redir_error(element, fd, i)); 77 | i++; 78 | } 79 | return (fd); 80 | } 81 | return (-1); 82 | } 83 | 84 | int create_file(t_list *element, int type) 85 | { 86 | int fd; 87 | 88 | fd = 0; 89 | if (type == CHEVRONR) 90 | fd = fd_simple_redirection_right(element); 91 | else if (type == CHEVROND) 92 | fd = fd_double_redirection_right(element); 93 | else if (type == CHEVRONL) 94 | fd = fd_simple_redirection_left(element); 95 | return (fd); 96 | } 97 | -------------------------------------------------------------------------------- /srcs/data/split_data/get_len_split_data.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_len_split_data.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: lorenzoa +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/06 19:32:35 by lorenzoa #+# #+# */ 9 | /* Updated: 2021/04/16 13:08:14 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int ft_size_quote(t_split_data *split_data, char *str, int index) 16 | { 17 | char quote; 18 | 19 | quote = str[index]; 20 | index++; 21 | while (str[index]) 22 | { 23 | if (str[index] == quote && quote == SQUOTE) 24 | return (index); 25 | if (str[index] == quote && str[index - 1] != '\\' && quote == DQUOTE) 26 | return (index); 27 | index++; 28 | } 29 | split_data->error = 1; 30 | return (index); 31 | } 32 | 33 | int get_split_pos(char *str, t_split_data *split_data, int index) 34 | { 35 | index++; 36 | while (str[index] && str[index] != DQUOTE) 37 | { 38 | if (str[index] == '\\' && str[index + 1]) 39 | index++; 40 | else if (str[index] == '\\' && str[index + 1] == DQUOTE) 41 | index++; 42 | else if (str[index] == '\\' && !str[index + 1]) 43 | { 44 | split_data->error = 1; 45 | return (-1); 46 | } 47 | index++; 48 | } 49 | return (index); 50 | } 51 | 52 | static int get_pos_word(char *str, t_split_data *split_data, int index) 53 | { 54 | if (str[index] == '\\' && str[index + 1]) 55 | index++; 56 | else if (str[index] == '\\' && str[index + 1] == '\0') 57 | split_data->error = 1; 58 | else if (str[index] == SQUOTE || str[index] == DQUOTE) 59 | index = ft_size_quote(split_data, str, index); 60 | if (str[index]) 61 | index++; 62 | return (index); 63 | } 64 | 65 | int ft_get_len_word(t_split_data *split_data, char *str) 66 | { 67 | int index; 68 | 69 | index = split_data->pos; 70 | if (!str[index]) 71 | return (0); 72 | if (ft_count_separator(split_data, str, index) != 0) 73 | { 74 | split_data->pos_index = index + 75 | ft_count_separator(split_data, str, index); 76 | index = ft_count_separator(split_data, str, index); 77 | return (index); 78 | } 79 | while (str[index] != '\0' && str[index] != ' ' 80 | && ft_count_separator(split_data, str, index) == 0) 81 | index = get_pos_word(str, split_data, index); 82 | split_data->pos_index = index; 83 | return (index - split_data->pos); 84 | } 85 | -------------------------------------------------------------------------------- /srcs/data/split_data/split_data.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* split_data.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/11 19:36:18 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 13:28:04 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | static void to_tab(char *str, 16 | t_split_data *split_data, char **res) 17 | { 18 | split_data->pos_index = 0; 19 | while (str[split_data->pos] != '\0' && str[split_data->pos] == ' ') 20 | split_data->pos++; 21 | res[split_data->nb] = ft_substr(str, split_data->pos, 22 | ft_get_len_word(split_data, str)); 23 | split_data->nb++; 24 | split_data->pos = split_data->pos_index; 25 | } 26 | 27 | char **ft_word_to_tab(char *str, 28 | t_split_data *split_data, char **res) 29 | { 30 | int i; 31 | int loop; 32 | 33 | i = 0; 34 | loop = 0; 35 | split_data->nb = 0; 36 | split_data->error = 0; 37 | while (str[split_data->pos] && (split_data->nb < split_data->nb_word)) 38 | { 39 | loop++; 40 | to_tab(str, split_data, res); 41 | if (res[split_data->nb - 1] == NULL && split_data->error == 1) 42 | { 43 | while (i < split_data->nb_word && i < loop) 44 | { 45 | free(res[i]); 46 | i++; 47 | } 48 | return (NULL); 49 | } 50 | } 51 | return (res); 52 | } 53 | 54 | static int get_pos_after_word(char *s, char c, 55 | t_split_data *split_data) 56 | { 57 | split_data->nb_word++; 58 | if (s[split_data->pos] == '\\' && s[split_data->pos + 1] == c) 59 | split_data->pos += 2; 60 | while (s[split_data->pos] != '\0' && s[split_data->pos] != c 61 | && (ft_count_separator(split_data, s, split_data->pos) == 0)) 62 | { 63 | if ((split_data->pos = check_word_qte(split_data, s)) == ERROR) 64 | return (-1); 65 | else if (s[split_data->pos] == '\\' && s[split_data->pos + 1]) 66 | split_data->pos++; 67 | if (s[split_data->pos]) 68 | split_data->pos++; 69 | } 70 | return (1); 71 | } 72 | 73 | void ft_count_word(t_msh *msh, char *s, char c, 74 | t_split_data *split_data) 75 | { 76 | while (s[split_data->pos]) 77 | { 78 | while (s[split_data->pos] == c) 79 | split_data->pos++; 80 | if (s[split_data->pos] != '\0' && s[split_data->pos] != c 81 | && (ft_count_separator(split_data, s, split_data->pos) == 0)) 82 | if (get_pos_after_word(s, c, split_data) == -1) 83 | return ; 84 | split_data->double_semicolon = 0; 85 | if (s[split_data->pos] && ft_count_separator(split_data, s, 86 | split_data->pos) != 0) 87 | { 88 | split_data->nb_word++; 89 | if (split_data->double_semicolon == 1 && s[split_data->pos + 1]) 90 | if (ft_count_separator(split_data, s, (split_data->pos + 1)) 91 | == 1) 92 | if (split_data->double_semicolon == 2) 93 | split_data->error = 2; 94 | split_data->pos += ft_count_separator(split_data, s, 95 | split_data->pos); 96 | } 97 | else if (s[split_data->pos] != '\0') 98 | split_data->pos++; 99 | } 100 | msh->data->size_data = split_data->nb_word; 101 | } 102 | -------------------------------------------------------------------------------- /srcs/data/split_data/split_data_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* split_data_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/11 19:43:38 by loamar #+# #+# */ 9 | /* Updated: 2021/04/15 13:53:28 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int check_word(char *s, char c) 16 | { 17 | int pos; 18 | 19 | pos = 0; 20 | while (s[pos] == c) 21 | pos++; 22 | if (s[pos] == '\0') 23 | return (1); 24 | else 25 | return (0); 26 | } 27 | 28 | t_split_data *init_split_data(t_split_data *split_data) 29 | { 30 | if (!(split_data = (t_split_data*)malloc(sizeof(t_split_data)))) 31 | return (NULL); 32 | split_data->error = 0; 33 | split_data->size = 0; 34 | split_data->double_semicolon = 0; 35 | split_data->nb_word = 0; 36 | split_data->nb = 0; 37 | split_data->pos_index = 0; 38 | split_data->pos = 0; 39 | return (split_data); 40 | } 41 | 42 | int ft_count_separator(t_split_data *split_data, char *s, int pos) 43 | { 44 | if (s[pos + 1]) 45 | if ((s[pos] == '>' && s[pos + 1] == '>')) 46 | return (2); 47 | if (s[pos] == ';' || s[pos] == '|' || s[pos] == '<' || s[pos] == '>') 48 | { 49 | if (s[pos] == ';') 50 | { 51 | if (split_data->double_semicolon == 0) 52 | split_data->double_semicolon = 1; 53 | else if (split_data->double_semicolon == 1) 54 | split_data->double_semicolon = 2; 55 | } 56 | return (1); 57 | } 58 | return (0); 59 | } 60 | 61 | static int check_word_squote(t_split_data *split_data, char *str, 62 | int index) 63 | { 64 | index++; 65 | while (str[index] && str[index] != SQUOTE) 66 | index++; 67 | if (str[index] == SQUOTE) 68 | return (index); 69 | split_data->error = 1; 70 | return (index); 71 | } 72 | 73 | int check_word_qte(t_split_data *split_data, char *str) 74 | { 75 | int index; 76 | 77 | index = split_data->pos; 78 | if (str[index] == SQUOTE) 79 | return (check_word_squote(split_data, str, index)); 80 | else if (str[index] == DQUOTE) 81 | { 82 | index = get_split_pos(str, split_data, index); 83 | if (index == -1) 84 | return (ERROR); 85 | if (str[index] == DQUOTE) 86 | return (index); 87 | else 88 | { 89 | split_data->error = 1; 90 | return (ERROR); 91 | } 92 | } 93 | else if ((index == (int)ft_strlen(str)) && str[index - 1] == '\\') 94 | { 95 | split_data->error = 1; 96 | return (ERROR); 97 | } 98 | return (index); 99 | } 100 | -------------------------------------------------------------------------------- /srcs/env/create_list_env.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_list_env.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/05 12:26:48 by loamar #+# #+# */ 9 | /* Updated: 2021/04/16 12:48:13 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int ft_fill_empty_env(t_env_lair *env_lair, char *first_content, 16 | char *second_content) 17 | { 18 | t_env_list *new_block; 19 | 20 | if (!(new_block = (t_env_list *)malloc(sizeof(t_env_list)))) 21 | return (-1); 22 | new_block->first_content = first_content; 23 | new_block->second_content = second_content; 24 | new_block->previous = env_lair->start; 25 | new_block->next = env_lair->end; 26 | env_lair->end = new_block; 27 | env_lair->start = new_block; 28 | env_lair->size++; 29 | return (0); 30 | } 31 | 32 | int ft_fill_end_env(t_env_lair *env_lair, char *first_content, 33 | char *second_content) 34 | { 35 | t_env_list *new_block; 36 | 37 | if (!(new_block = (t_env_list *)malloc(sizeof(t_env_list)))) 38 | return (-1); 39 | new_block->first_content = first_content; 40 | new_block->second_content = second_content; 41 | new_block->next = NULL; 42 | new_block->previous = env_lair->end; 43 | env_lair->end->next = new_block; 44 | env_lair->end = new_block; 45 | env_lair->size++; 46 | return (0); 47 | } 48 | 49 | static int is_empty_env(t_env_lair *env_lair) 50 | { 51 | if (env_lair->size == 0) 52 | { 53 | free(env_lair); 54 | return (SUCCESS); 55 | } 56 | return (ERROR); 57 | } 58 | 59 | static void free_popback_env(t_env_list *temp) 60 | { 61 | if (temp->first_content) 62 | free(temp->first_content); 63 | if (temp->second_content) 64 | free(temp->second_content); 65 | free(temp); 66 | } 67 | 68 | int pop_back_env(t_env_lair *env_lair) 69 | { 70 | t_env_list *temp; 71 | 72 | if (is_empty_env(env_lair) == SUCCESS) 73 | return (ERROR); 74 | if (env_lair->size == 1) 75 | { 76 | temp = env_lair->start; 77 | env_lair->start = env_lair->start->next; 78 | if (env_lair->start == NULL) 79 | env_lair->end = NULL; 80 | else 81 | env_lair->start->previous = NULL; 82 | } 83 | else 84 | { 85 | temp = env_lair->end; 86 | env_lair->end->previous->next = NULL; 87 | env_lair->end = env_lair->end->previous; 88 | } 89 | free_popback_env(temp); 90 | env_lair->size--; 91 | return (SUCCESS); 92 | } 93 | -------------------------------------------------------------------------------- /srcs/env/handler_env.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_env.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/14 17:30:11 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 17:12:57 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int clear_env(t_env_lair *env_lair) 16 | { 17 | int loop; 18 | 19 | loop = 0; 20 | while (loop == 0) 21 | { 22 | if (pop_back_env(env_lair) == ERROR) 23 | loop = 1; 24 | } 25 | return (SUCCESS); 26 | } 27 | 28 | void set_env(t_msh *msh, char *first_content, char *second_content) 29 | { 30 | t_env_list *element; 31 | 32 | if (!first_content || !second_content) 33 | return ; 34 | element = msh->env_lair->start; 35 | if (!element || element == NULL) 36 | ft_fill_empty_env(msh->env_lair, first_content, second_content); 37 | else 38 | { 39 | while (element) 40 | { 41 | if (ft_strcmp(first_content, element->first_content) == 0) 42 | { 43 | if (element->second_content) 44 | free(element->second_content); 45 | element->second_content = ft_strdup(second_content); 46 | return ; 47 | } 48 | element = element->next; 49 | } 50 | ft_fill_end_env(msh->env_lair, first_content, second_content); 51 | } 52 | } 53 | 54 | char *get_env(t_msh *msh, char *str) 55 | { 56 | t_env_list *element; 57 | 58 | element = msh->env_lair->start; 59 | if (element == NULL || !element) 60 | return (NULL); 61 | while (element) 62 | { 63 | if (ft_strcmp(str, element->first_content) == 0) 64 | { 65 | if (!element->second_content) 66 | return (NULL); 67 | else 68 | return (ft_strdup(element->second_content)); 69 | } 70 | element = element->next; 71 | } 72 | return (NULL); 73 | } 74 | 75 | char *sep_env(char *str, int prt, int type) 76 | { 77 | int pos; 78 | 79 | pos = 0; 80 | if (!str) 81 | return (NULL); 82 | if (prt == 0) 83 | { 84 | while (str[pos] != '=' && str[pos] != '\0') 85 | pos++; 86 | return (ft_substr(str, 0, pos)); 87 | } 88 | else 89 | { 90 | while (str[pos] != '=' && str[pos] != '\0') 91 | pos++; 92 | if (str[pos] == '\0') 93 | return (NULL); 94 | if (str[pos] == '=') 95 | pos++; 96 | if (type == ENV) 97 | return (ft_substr(str, pos, (ft_strlen(str) - pos))); 98 | else 99 | return (export_secondcontent(str, pos)); 100 | } 101 | } 102 | 103 | int handler_env(t_msh *msh, char **env) 104 | { 105 | int count; 106 | 107 | count = 0; 108 | msh->env_lair = init_env_lair(msh->env_lair); 109 | if (msh->env_lair == NULL || !msh->env_lair || !env) 110 | return (EMPTY_ENV); 111 | msh->env_list = NULL; 112 | ft_fill_empty_env(msh->env_lair, sep_env(env[count], 0, ENV), 113 | sep_env(env[count], 1, ENV)); 114 | while (env[++count] != NULL) 115 | { 116 | ft_fill_end_env(msh->env_lair, sep_env(env[count], 0, ENV), 117 | sep_env(env[count], 1, ENV)); 118 | } 119 | return (SUCCESS); 120 | } 121 | -------------------------------------------------------------------------------- /srcs/env/path_env.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* path_env.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/23 10:31:47 by loamar #+# #+# */ 9 | /* Updated: 2021/04/07 13:38:14 by lorenzoamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static char *get_dir(char *name_dir, char *cmd) 16 | { 17 | DIR *folder; 18 | struct dirent *entry; 19 | 20 | folder = opendir(name_dir); 21 | if (!folder) 22 | return (0); 23 | while ((entry = readdir(folder))) 24 | { 25 | if (!ft_strcmp(entry->d_name, cmd)) 26 | { 27 | closedir(folder); 28 | return (name_dir); 29 | } 30 | } 31 | closedir(folder); 32 | return (0); 33 | } 34 | 35 | char *get_exec_path(t_msh *msh, char *content) 36 | { 37 | char *exec_path; 38 | char *tmp; 39 | int index; 40 | 41 | index = -1; 42 | exec_path = NULL; 43 | if (!content || msh->utils->path == NULL) 44 | return (NULL); 45 | while (msh->utils->path[++index] && exec_path == NULL) 46 | exec_path = get_dir(msh->utils->path[index], content); 47 | if (exec_path) 48 | { 49 | tmp = ft_strjoin(exec_path, "/"); 50 | exec_path = ft_strjoin(tmp, content); 51 | free(tmp); 52 | } 53 | return (exec_path); 54 | } 55 | 56 | int get_path(t_msh *msh) 57 | { 58 | t_env_list *element; 59 | 60 | element = msh->env_lair->start; 61 | while (element != NULL) 62 | { 63 | if ((ft_strlen(element->first_content) >= 4) 64 | && element->first_content[0] == 'P' 65 | && element->first_content[1] == 'A' 66 | && element->first_content[2] == 'T' 67 | && element->first_content[3] == 'H') 68 | { 69 | msh->utils->path = ft_split(element->second_content, ':'); 70 | return (SUCCESS); 71 | } 72 | else 73 | element = element->next; 74 | } 75 | msh->utils->path = NULL; 76 | return (EMPTY_ENV); 77 | } 78 | -------------------------------------------------------------------------------- /srcs/error_and_free/error_msg.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* error_msg.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/04 00:50:24 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 16:56:41 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static void return_error_cmd(char *cmd, char *msg) 16 | { 17 | if (cmd != NULL) 18 | { 19 | ft_putstr_fd(cmd, 2); 20 | ft_putstr_fd(": ", 2); 21 | } 22 | if (msg != NULL) 23 | ft_putstr_fd(msg, 2); 24 | } 25 | 26 | static void return_error_token(char *cmd, char *msg) 27 | { 28 | if (msg) 29 | { 30 | ft_putstr_fd(msg, 2); 31 | ft_putstr_fd(" ", 2); 32 | } 33 | if (cmd) 34 | { 35 | ft_putstr_fd("`", 2); 36 | ft_putstr_fd(cmd, 2); 37 | ft_putstr_fd("\'", 2); 38 | } 39 | } 40 | 41 | static void return_error_args(char *cmd, char *arg, char *msg, int type) 42 | { 43 | if (cmd != NULL) 44 | { 45 | ft_putstr_fd(cmd, 2); 46 | ft_putstr_fd(": ", 2); 47 | } 48 | if (arg != NULL) 49 | { 50 | if (type == ERROR_QTE) 51 | ft_putstr_fd("`", 2); 52 | ft_putstr_fd(arg, 2); 53 | if (type == ERROR_QTE) 54 | ft_putstr_fd("\'", 2); 55 | } 56 | if (msg != NULL) 57 | ft_putstr_fd(msg, 2); 58 | } 59 | 60 | int return_error(int type, char *cmd, char *arg, char *msg) 61 | { 62 | if (type != ERROR_MSG) 63 | ft_putstr_fd("minishell: ", 2); 64 | if (type == ERROR_ARGS || type == ERROR_QTE) 65 | return_error_args(cmd, arg, msg, type); 66 | else if (type == ERROR_MULTI) 67 | ft_putstr_fd(msg, 2); 68 | else if (type == ERROR_TOKEN) 69 | return_error_token(cmd, msg); 70 | else if (type == ERROR_CMD) 71 | return_error_cmd(cmd, msg); 72 | else if (type == ERROR_MSG) 73 | ft_putstr_fd(msg, 2); 74 | else 75 | ft_putstr_fd(strerror(errno), 2); 76 | ft_putstr_fd("\n", 2); 77 | g_error(SET, ERROR); 78 | return (ERROR); 79 | } 80 | -------------------------------------------------------------------------------- /srcs/error_and_free/free_error.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* free_error.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/07 11:56:22 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 15:28:50 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void free_split(char **str) 16 | { 17 | int index; 18 | 19 | index = 0; 20 | while (str[index]) 21 | { 22 | free(str[index]); 23 | index++; 24 | } 25 | free(str[index]); 26 | free(str); 27 | } 28 | 29 | void free_list(t_msh *msh, int key) 30 | { 31 | if ((key == ENDLOOP || key == EXIT) 32 | && msh->lair_list && msh->lair_list->size > 0) 33 | clear_list(msh->lair_list); 34 | if (msh->env_lair && msh->env_lair->size > 0 35 | && (key == CTRLD || key == EXIT)) 36 | clear_env(msh->env_lair); 37 | if (msh->history_lair && msh->history_lair->size > 0 38 | && (key == CTRLD || key == EXIT)) 39 | clear_history(msh->history_lair); 40 | } 41 | 42 | char **ft_free_tab(char **tabs, int j, t_split_data *split_data) 43 | { 44 | while (j-- >= 0) 45 | free(tabs[j]); 46 | free(tabs); 47 | free(split_data); 48 | return (NULL); 49 | } 50 | 51 | void free_tab_args(char **str) 52 | { 53 | int index; 54 | 55 | index = 0; 56 | free(str[index]); 57 | index++; 58 | while (str[index]) 59 | { 60 | free(str[index]); 61 | index++; 62 | } 63 | free(str[index]); 64 | free(str); 65 | } 66 | 67 | int exit_cmd(t_msh *msh) 68 | { 69 | free_all(msh, EXIT); 70 | exit(g_status); 71 | } 72 | -------------------------------------------------------------------------------- /srcs/error_and_free/handler_error.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_error.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/11 03:33:33 by loamar #+# #+# */ 9 | /* Updated: 2021/05/05 17:56:54 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void free_all(t_msh *msh, int free_key) 16 | { 17 | if (msh && (g_loop(GET, 0) != LOOP)) 18 | { 19 | // if (msh->termcap) 20 | // { 21 | // if (msh->termcap->line) 22 | // free(msh->termcap->line); 23 | // } 24 | if (msh->utils && (free_key == ENDLOOP || free_key == EXIT)) 25 | { 26 | if (msh->utils->path) 27 | { 28 | free_split(msh->utils->path); 29 | msh->utils->path = NULL; 30 | } 31 | if (msh->utils->tab_specase) 32 | free(msh->utils->tab_specase); 33 | free(msh->utils); 34 | } 35 | if (msh->data && (free_key == ENDLOOP || free_key == EXIT)) 36 | free(msh->data); 37 | free_list(msh, free_key); 38 | if (free_key == EXIT || free_key == CTRLD) 39 | free(msh); 40 | } 41 | } 42 | 43 | int error_data(t_data *data, int token) 44 | { 45 | ft_putstr(data->prompt_data[token]); 46 | ft_putstr(": "); 47 | ft_putstr(strerror(errno)); 48 | ft_putstr("\n"); 49 | return (1); 50 | } 51 | -------------------------------------------------------------------------------- /srcs/init/init_shell.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init_shell.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/11/05 18:42:01 by loamar #+# #+# */ 9 | /* Updated: 2021/04/27 02:45:02 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void aff_welcome(void) 16 | { 17 | ft_putstr("\n"); 18 | ft_putstr(" +--------------+\n"); 19 | ft_putstr(" |.------------.|\n"); 20 | ft_putstr(" || ||\n"); 21 | ft_putstr(" || WELCOME TO ||\n"); 22 | ft_putstr(" || MINISHELL ||\n"); 23 | ft_putstr(" || ||\n"); 24 | ft_putstr(" |+------------+|\n"); 25 | ft_putstr(" +-..--------..-+\n"); 26 | ft_putstr(" .--------------.\n"); 27 | ft_putstr(" | .============. | \n"); 28 | ft_putstr(" | .==============. | \n"); 29 | ft_putstr("| ___________________| \n"); 30 | ft_putstr("|____________________| \n"); 31 | ft_putstr("*+* BY LOAMAR *+* \n\n"); 32 | } 33 | 34 | void prompt(void) 35 | { 36 | ft_putstr_fd("\e[0;36m", 2); 37 | ft_putstr_fd("minishell$ ", 2); 38 | ft_putstr_fd("\e[0;37m", 2); 39 | } 40 | 41 | t_utils *init_utils(t_utils *utils) 42 | { 43 | utils->check = 0; 44 | utils->size_tab = 0; 45 | utils->pos_args = 1; 46 | utils->export_check = 0; 47 | return (utils); 48 | } 49 | -------------------------------------------------------------------------------- /srcs/init/init_struct.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* init_struct.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/06 07:27:43 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 15:10:21 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static t_msh *init_struct(t_msh *msh) 16 | { 17 | if (g_loop(GET, 0) == LOOP) 18 | { 19 | if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) 20 | return (NULL); 21 | if (!(msh->termcap = (t_termcap*)malloc(sizeof(t_termcap)))) 22 | return (NULL); 23 | } 24 | if (!(msh->utils = (t_utils*)malloc(sizeof(t_utils)))) 25 | return (NULL); 26 | if (!(msh->data = (t_data *)malloc(sizeof(t_data)))) 27 | return (NULL); 28 | return (msh); 29 | } 30 | 31 | t_msh *init_msh(t_msh *msh) 32 | { 33 | msh = init_struct(msh); 34 | if (msh == NULL) 35 | return (NULL); 36 | msh->termcap->start_col = 0; 37 | msh->termcap->start_row = 0; 38 | msh->termcap->end_col = 0; 39 | msh->termcap->end_row = 0; 40 | msh->termcap->col = 0; 41 | msh->termcap->row = 0; 42 | msh->utils->tab_specase = NULL; 43 | msh->utils->size_tab = 0; 44 | msh->utils->loop = 0; 45 | msh->utils->loop2 = 0; 46 | msh->utils->loop3 = 0; 47 | msh->list = NULL; 48 | msh->lair_list = NULL; 49 | msh->utils->path = NULL; 50 | return (msh); 51 | } 52 | 53 | t_history_lair *init_history_lair(t_history_lair *history_lair) 54 | { 55 | if (!(history_lair = (t_history_lair*)malloc(sizeof(t_history_lair)))) 56 | return (NULL); 57 | history_lair->start = NULL; 58 | history_lair->end = NULL; 59 | history_lair->size = 0; 60 | return (history_lair); 61 | } 62 | 63 | t_lair_list *init_lair_list(t_lair_list *lair_list) 64 | { 65 | if (!(lair_list = (t_lair_list*)malloc(sizeof(t_lair_list)))) 66 | return (NULL); 67 | lair_list->start = NULL; 68 | lair_list->end = NULL; 69 | lair_list->size = 0; 70 | return (lair_list); 71 | } 72 | 73 | t_env_lair *init_env_lair(t_env_lair *env_lair) 74 | { 75 | if (!(env_lair = (t_env_lair*)malloc(sizeof(t_env_lair)))) 76 | return (NULL); 77 | env_lair->start = NULL; 78 | env_lair->end = NULL; 79 | env_lair->size = 0; 80 | return (env_lair); 81 | } 82 | -------------------------------------------------------------------------------- /srcs/list/check_empty.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_empty.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/19 12:50:04 by loamar #+# #+# */ 9 | /* Updated: 2021/04/19 12:55:14 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static t_list *return_no_empty(t_msh *msh, t_list *element, char *check) 16 | { 17 | if (element) 18 | element = element->next; 19 | msh->utils->i++; 20 | free(check); 21 | return (element); 22 | } 23 | 24 | static t_list *free_empty(t_msh *msh, t_list *element) 25 | { 26 | char *check; 27 | t_list *index; 28 | int limite; 29 | 30 | limite = 1; 31 | check = NULL; 32 | check = return_all_content(msh, element->content, 1); 33 | if (ft_strcmp(check, "\0") == 0) 34 | { 35 | free(check); 36 | pop_choose_list(msh->lair_list, msh->utils->i); 37 | if (msh->lair_list->size == 0) 38 | return (0); 39 | index = msh->lair_list->start; 40 | while (limite < msh->utils->i) 41 | { 42 | index = index->next; 43 | limite++; 44 | } 45 | set_token_list(msh); 46 | return (index); 47 | } 48 | else 49 | return (return_no_empty(msh, element, check)); 50 | } 51 | 52 | static t_list *if_empty(t_msh *msh, t_list *element) 53 | { 54 | if ((element->next 55 | && (get_value_sep(element->next->content) == PIPE)) 56 | || ((msh->utils->i > 1) 57 | && (get_value_sep(element->previous->content) == PIPE) 58 | && (!element->next || element->next->token == SEPARATOR))) 59 | { 60 | msh->utils->i++; 61 | element = element->next; 62 | } 63 | else 64 | element = free_empty(msh, element); 65 | return (element); 66 | } 67 | 68 | void check_empty(t_msh *msh) 69 | { 70 | t_list *element; 71 | 72 | msh->utils->i = 1; 73 | msh->utils->no_space = 0; 74 | element = msh->lair_list->start; 75 | while (element) 76 | { 77 | if ((element->token == CMD || element->token == ARGS 78 | || element->token == OPTION) && element->content[0] == '$') 79 | element = if_empty(msh, element); 80 | else 81 | { 82 | element = element->next; 83 | msh->utils->i++; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /srcs/list/create_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/07 04:25:27 by loamar #+# #+# */ 9 | /* Updated: 2021/04/19 12:50:20 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int ft_fill_empty_list(t_lair_list *lair_list, char *content) 16 | { 17 | t_list *new_element; 18 | 19 | if (!(new_element = (t_list *)malloc(sizeof(t_list)))) 20 | return (-1); 21 | new_element->content = content; 22 | new_element->token = 0; 23 | new_element->tab_args = NULL; 24 | new_element->previous = lair_list->start; 25 | new_element->next = lair_list->end; 26 | lair_list->end = new_element; 27 | lair_list->start = new_element; 28 | lair_list->size++; 29 | return (0); 30 | } 31 | 32 | int ft_fill_end_list(t_lair_list *lair_list, char *content) 33 | { 34 | t_list *new_element; 35 | 36 | if (!(new_element = (t_list *)malloc(sizeof(t_list)))) 37 | return (-1); 38 | new_element->content = content; 39 | new_element->token = 0; 40 | new_element->tab_args = NULL; 41 | new_element->previous = lair_list->end; 42 | new_element->next = NULL; 43 | lair_list->end->next = new_element; 44 | lair_list->end = new_element; 45 | lair_list->size++; 46 | return (0); 47 | } 48 | 49 | int clear_list(t_lair_list *lair_list) 50 | { 51 | int loop; 52 | 53 | loop = 0; 54 | while (loop == 0) 55 | { 56 | if (pop_back_list(lair_list) == ERROR) 57 | loop = 1; 58 | } 59 | return (SUCCESS); 60 | } 61 | -------------------------------------------------------------------------------- /srcs/list/create_tab_args.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_tab_args.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/11 23:20:47 by loamar #+# #+# */ 9 | /* Updated: 2021/04/19 15:39:49 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int pop_list(t_msh *msh, int pos) 16 | { 17 | t_list *delete_element; 18 | t_list *now_element; 19 | int i; 20 | 21 | i = 1; 22 | if (msh->lair_list->size == 0) 23 | return (-1); 24 | now_element = msh->lair_list->start; 25 | while (i < pos) 26 | { 27 | now_element = now_element->next; 28 | i++; 29 | } 30 | delete_element = now_element; 31 | if (now_element->next != NULL) 32 | now_element->next->previous = now_element->previous; 33 | else 34 | msh->lair_list->end = now_element->previous; 35 | if (now_element->previous != NULL) 36 | now_element->previous->next = now_element->next; 37 | free(delete_element->content); 38 | free(delete_element); 39 | msh->lair_list->size--; 40 | return (0); 41 | } 42 | 43 | static int ft_put_args(t_msh *msh, t_list *cmd) 44 | { 45 | t_list *tmp; 46 | int pos; 47 | 48 | pos = 0; 49 | tmp = cmd; 50 | cmd = cmd->next; 51 | msh->utils->size_opt_arg = 0; 52 | while (cmd != NULL && (cmd->token == OPTION || cmd->token == ARGS)) 53 | { 54 | cmd = cmd->next; 55 | msh->utils->size_opt_arg++; 56 | } 57 | cmd = tmp; 58 | if (!(cmd->tab_args = malloc(sizeof(char *) * 59 | (msh->utils->size_opt_arg + 2)))) 60 | return (ERROR); 61 | while (++pos <= msh->utils->size_opt_arg) 62 | { 63 | tmp = tmp->next; 64 | cmd->tab_args[pos] = ft_strdup(tmp->content); 65 | } 66 | cmd->tab_args[0] = NULL; 67 | cmd->tab_args[msh->utils->size_opt_arg + 1] = NULL; 68 | return (SUCCESS); 69 | } 70 | 71 | static t_list *get_args_to_pop(t_msh *msh, t_list *cmd, t_list *tmp) 72 | { 73 | msh->utils->pos_list++; 74 | while (cmd != NULL && (cmd->token == OPTION || cmd->token == ARGS)) 75 | { 76 | pop_list(msh, msh->utils->pos_list); 77 | cmd = tmp; 78 | cmd = cmd->next; 79 | } 80 | if (cmd != NULL && cmd->token == SEPARATOR) 81 | { 82 | msh->utils->pos_list++; 83 | cmd = cmd->next; 84 | } 85 | return (cmd); 86 | } 87 | 88 | int create_tab_args(t_msh *msh) 89 | { 90 | t_list *cmd; 91 | t_list *tmp; 92 | int ret; 93 | 94 | msh->utils->pos_list = 1; 95 | cmd = msh->lair_list->start; 96 | if (cmd->token == CMD) 97 | { 98 | while (cmd != NULL) 99 | { 100 | ret = ft_put_args(msh, cmd); 101 | if (ret == ERROR) 102 | return (ERROR); 103 | tmp = cmd; 104 | cmd = cmd->next; 105 | if (cmd == NULL) 106 | break ; 107 | cmd = get_args_to_pop(msh, cmd, tmp); 108 | } 109 | } 110 | return (SUCCESS); 111 | } 112 | 113 | int check_handler_list(t_msh *msh) 114 | { 115 | set_token_list(msh); 116 | check_empty(msh); 117 | if (msh->lair_list->size != 0) 118 | { 119 | if (create_tab_args(msh) == ERROR) 120 | return (ERROR); 121 | if (check_pipe_multi(msh) == ERROR) 122 | return (ERROR); 123 | if (check_token_list(msh) == ERROR) 124 | return (ERROR); 125 | } 126 | return (SUCCESS); 127 | } 128 | -------------------------------------------------------------------------------- /srcs/list/delete_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* delete_list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/17 13:38:22 by loamar #+# #+# */ 9 | /* Updated: 2021/04/19 12:41:37 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int is_empty_list(t_lair_list *lair_list) 16 | { 17 | if (lair_list->size == 0) 18 | { 19 | free(lair_list); 20 | return (SUCCESS); 21 | } 22 | return (ERROR); 23 | } 24 | 25 | static void free_popback_list(t_list *temp) 26 | { 27 | if (temp->content) 28 | free(temp->content); 29 | if (temp->tab_args) 30 | free_tab_args(temp->tab_args); 31 | free(temp); 32 | } 33 | 34 | int pop_back_list(t_lair_list *lair_list) 35 | { 36 | t_list *temp; 37 | 38 | if (is_empty_list(lair_list) == SUCCESS) 39 | return (ERROR); 40 | if (lair_list->size == 1) 41 | { 42 | temp = lair_list->start; 43 | lair_list->start = lair_list->start->next; 44 | if (lair_list->start == NULL) 45 | lair_list->end = NULL; 46 | else 47 | lair_list->start->previous = NULL; 48 | } 49 | else 50 | { 51 | temp = lair_list->end; 52 | lair_list->end->previous->next = NULL; 53 | lair_list->end = lair_list->end->previous; 54 | } 55 | free_popback_list(temp); 56 | lair_list->size--; 57 | return (SUCCESS); 58 | } 59 | 60 | static t_list *pop_pos_list(t_lair_list *lair_list, t_list *temp, 61 | int pos) 62 | { 63 | int target; 64 | t_list *courant; 65 | 66 | courant = NULL; 67 | courant = lair_list->start; 68 | target = 1; 69 | while (target < pos) 70 | { 71 | courant = courant->next; 72 | target++; 73 | } 74 | temp = courant; 75 | courant->previous->next = courant->next; 76 | courant->next->previous = courant->previous; 77 | return (temp); 78 | } 79 | 80 | int pop_choose_list(t_lair_list *lair_list, int pos) 81 | { 82 | t_list *temp; 83 | 84 | temp = NULL; 85 | if (is_empty_list(lair_list) == SUCCESS) 86 | return (ERROR); 87 | if (pos == 1) 88 | { 89 | temp = lair_list->start; 90 | lair_list->start = lair_list->start->next; 91 | if (lair_list->start == NULL) 92 | lair_list->end = NULL; 93 | else 94 | lair_list->start->previous = NULL; 95 | } 96 | else if (pos == lair_list->size) 97 | { 98 | temp = lair_list->end; 99 | lair_list->end->previous->next = NULL; 100 | lair_list->end = lair_list->end->previous; 101 | } 102 | else 103 | temp = pop_pos_list(lair_list, temp, pos); 104 | free_popback_list(temp); 105 | lair_list->size--; 106 | return (SUCCESS); 107 | } 108 | -------------------------------------------------------------------------------- /srcs/list/handler_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/11 04:42:10 by loamar #+# #+# */ 9 | /* Updated: 2021/04/19 15:48:41 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static int error_pipe_multi(t_list *element) 16 | { 17 | if (element->next && element->token == SEPARATOR 18 | && element->next->token == SEPARATOR 19 | && get_value_sep(element->content) == PIPE 20 | && get_value_sep(element->next->content) == PIPE 21 | && (element->next->next == NULL || !element->next->next)) 22 | { 23 | return (return_error(ERROR_MULTI, NULL, NULL, 24 | "syntax error multiligne")); 25 | } 26 | if ((!element->next || (element->next == NULL)) 27 | && (element->token == SEPARATOR) 28 | && (get_value_sep(element->content) == PIPE)) 29 | { 30 | return (return_error(ERROR_MULTI, NULL, NULL, 31 | "syntax error multiligne")); 32 | } 33 | return (SUCCESS); 34 | } 35 | 36 | int check_pipe_multi(t_msh *msh) 37 | { 38 | t_list *element; 39 | int ret; 40 | 41 | ret = 0; 42 | element = msh->lair_list->start; 43 | if (!element || (element == NULL)) 44 | return (ERROR); 45 | while (element) 46 | { 47 | if (element->token == CMD && element->next) 48 | { 49 | element = element->next; 50 | ret = error_pipe_multi(element); 51 | if (ret == ERROR) 52 | return (ret); 53 | } 54 | else 55 | element = element->next; 56 | } 57 | return (SUCCESS); 58 | } 59 | 60 | static int check_cmd_list(t_list *element, int size) 61 | { 62 | if (size > 1) 63 | if (element->previous->token == SEPARATOR) 64 | element = element->previous; 65 | return (error_cmd(element)); 66 | } 67 | 68 | int check_token_list(t_msh *msh) 69 | { 70 | t_list *element; 71 | int check_cmd; 72 | int size; 73 | 74 | size = 1; 75 | check_cmd = 0; 76 | element = msh->lair_list->start; 77 | if (!element || (element == NULL)) 78 | return (ERROR); 79 | if (element->token == SEPARATOR) 80 | return (check_cmd_list(element, size)); 81 | while (element) 82 | { 83 | if (element->token == CMD) 84 | check_cmd = 1; 85 | else if (element->token == SEPARATOR) 86 | { 87 | if (check_cmd == 0) 88 | return (check_cmd_list(element, size)); 89 | check_cmd = 0; 90 | } 91 | size++; 92 | element = element->next; 93 | } 94 | return (SUCCESS); 95 | } 96 | 97 | int handler_list(t_msh *msh) 98 | { 99 | int count; 100 | 101 | count = 0; 102 | msh->lair_list = init_lair_list(msh->lair_list); 103 | if (msh->lair_list == NULL) 104 | return (ERROR); 105 | if (msh->data->prompt_data[count] != NULL) 106 | ft_fill_empty_list(msh->lair_list, msh->data->prompt_data[count]); 107 | else 108 | return (SUCCESS); 109 | count++; 110 | while (count < msh->data->size_data) 111 | { 112 | ft_fill_end_list(msh->lair_list, msh->data->prompt_data[count]); 113 | count++; 114 | } 115 | free(msh->data->prompt_data); 116 | if (check_handler_list(msh) == ERROR) 117 | return (ERROR); 118 | return (SUCCESS); 119 | } 120 | -------------------------------------------------------------------------------- /srcs/list/set_token_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* set_token_list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/01/12 18:10:32 by loamar #+# #+# */ 9 | /* Updated: 2021/04/09 14:09:24 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int get_quote(char *str) 16 | { 17 | if (!str) 18 | return (0); 19 | else if (str[0] == '\'') 20 | return (1); 21 | else if (str[0] == '\"') 22 | return (2); 23 | else 24 | return (0); 25 | } 26 | 27 | static int separator_check(char *s) 28 | { 29 | if (!ft_strncmp(s, "|", ft_strlen(s)) 30 | && ft_strlen(s) == 1) 31 | return (1); 32 | else if (!ft_strncmp(s, ";", ft_strlen(s)) 33 | && ft_strlen(s) == 1) 34 | return (1); 35 | else if (!ft_strncmp(s, "<", ft_strlen(s)) 36 | && ft_strlen(s) == 1) 37 | return (1); 38 | else if (!ft_strncmp(s, ">", ft_strlen(s)) 39 | && ft_strlen(s) == 1) 40 | return (1); 41 | else if (!ft_strncmp(s, ">>", ft_strlen(s)) 42 | && ft_strlen(s) == 2) 43 | return (1); 44 | return (0); 45 | } 46 | 47 | static int option_check(t_msh *msh, char *s) 48 | { 49 | (void)msh; 50 | if (!s || !s[0]) 51 | return (0); 52 | if (s[0] == 45) 53 | return (OPTION); 54 | else if (s[1] && ((s[0] == DQUOTE && s[1] == 45) 55 | || (s[0] == SQUOTE && s[1] == 45))) 56 | return (OPTION); 57 | else 58 | return (0); 59 | } 60 | 61 | static int token_recognition(t_msh *msh, char *s, int indice) 62 | { 63 | if (indice == 0) 64 | { 65 | if (separator_check(s) == 1) 66 | return (SEPARATOR); 67 | else 68 | return (CMD); 69 | } 70 | else if (((indice == 1 && option_check(msh, s)) 71 | || (indice > 1 && option_check(msh, s))) 72 | && (msh->utils->check_arg == 0)) 73 | return (OPTION); 74 | else if (indice != 0 && separator_check(s) == 1) 75 | return (SEPARATOR); 76 | else 77 | { 78 | msh->utils->check_arg = 1; 79 | return (ARGS); 80 | } 81 | return (0); 82 | } 83 | 84 | void set_token_list(t_msh *msh) 85 | { 86 | t_list *lst; 87 | int i; 88 | 89 | lst = msh->lair_list->start; 90 | i = 0; 91 | msh->utils->check_arg = 0; 92 | while (lst != NULL) 93 | { 94 | lst->token = 0; 95 | lst->token = token_recognition(msh, lst->content, i); 96 | i++; 97 | i = (lst->token == SEPARATOR) ? 0 : i; 98 | lst = lst->next; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /srcs/main/handler_signal.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_signal.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/03/17 13:17:45 by loamar #+# #+# */ 9 | /* Updated: 2021/04/26 20:43:09 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | static void process(int sign) 16 | { 17 | if (!kill(g_pid(GET, 0), sign)) 18 | { 19 | if (sign == SIGQUIT) 20 | { 21 | ft_putstr_fd("Quit: 3\n", 1); 22 | g_status = 131; 23 | g_error(SET, ERROR); 24 | } 25 | else if (sign == SIGINT) 26 | { 27 | ft_putchar_fd('\n', 1); 28 | g_status = 130; 29 | g_error(SET, ERROR); 30 | } 31 | } 32 | else if (sign == SIGINT) 33 | { 34 | ft_putchar_fd('\n', 1); 35 | g_status = 127; 36 | g_error(SET, ERROR); 37 | prompt(); 38 | } 39 | } 40 | 41 | void handler_signal(int sign) 42 | { 43 | if ((sign == SIGINT || sign == SIGQUIT) && g_pid(GET, 0) != 0) 44 | process(sign); 45 | else if (sign == SIGINT || sign == SIGQUIT) 46 | { 47 | if (sign == SIGINT) 48 | { 49 | ft_putchar_fd('\n', 1); 50 | g_status = 130; 51 | g_error(SET, ERROR); 52 | prompt(); 53 | } 54 | else if (sign == SIGQUIT) 55 | ft_putstr_fd("\b\b \b\b", 1); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /srcs/main/minishell.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minishell.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/12/11 19:54:29 by loamar #+# #+# */ 9 | /* Updated: 2021/04/30 12:41:41 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void end_loop(t_msh *msh, int free) 16 | { 17 | g_return(SET, SUCCESS); 18 | g_error(SET, SUCCESS); 19 | g_error_msg(SET, SUCCESS); 20 | free_all(msh, free); 21 | signal(SIGINT, handler_signal); 22 | } 23 | 24 | void main_handler(t_msh *msh, char *buf, char **env) 25 | { 26 | g_return(SET, SUCCESS); 27 | if (g_return(GET, 0) == SUCCESS && g_loop(GET, 0) == LOOP) 28 | g_return(SET, handler_env(msh, env)); 29 | if (g_return(GET, 0) == SUCCESS || g_return(GET, 0) == EMPTY_ENV) 30 | g_return(SET, get_path(msh)); 31 | if (g_return(GET, 0) == SUCCESS || g_return(GET, 0) == EMPTY_ENV) 32 | g_return(SET, handler_data(msh, buf)); 33 | g_loop(SET, ENDLOOP); 34 | if (g_return(GET, 0) == SUCCESS || g_return(GET, 0) == EMPTY_ENV) 35 | g_return(SET, handler_list(msh)); 36 | if (g_return(GET, 0) == SUCCESS || g_return(GET, 0) == EMPTY_ENV) 37 | g_return(SET, handler_cmd(msh, env)); 38 | } 39 | 40 | static void choose_shell(char **env) 41 | { 42 | t_msh *msh; 43 | // int loop; 44 | // int ret; 45 | 46 | // loop = 1; 47 | // ret = 0; 48 | msh = NULL; 49 | g_loop(SET, LOOP); 50 | g_pid(SET, 0); 51 | signal(SIGQUIT, handler_signal); 52 | signal(SIGINT, handler_signal); 53 | printf("yeyo\n"); 54 | if (isatty(0)) 55 | { 56 | printf("yeyo 1\n"); 57 | // printf("yeyo 1 2\n"); 58 | shell_loop_termcap(msh, env); 59 | } 60 | else 61 | { 62 | printf("yeyo 2\n"); 63 | prompt(); 64 | shell_loop(msh, env); 65 | } 66 | } 67 | 68 | int main(int argc, char **argv, char **env) 69 | { 70 | aff_welcome(); 71 | (void)argc; 72 | (void)argv; 73 | choose_shell(env); 74 | return (0); 75 | } 76 | -------------------------------------------------------------------------------- /srcs/main/my_global.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_global.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/22 22:24:44 by loamar #+# #+# */ 9 | /* Updated: 2021/04/22 23:15:19 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int g_pid(int mode, int value) 16 | { 17 | static pid_t pid; 18 | 19 | if (mode == 1) 20 | pid = value; 21 | return (pid); 22 | } 23 | 24 | int g_loop(int mode, int value) 25 | { 26 | static int loop; 27 | 28 | if (mode == 1) 29 | loop = value; 30 | return (loop); 31 | } 32 | 33 | int g_error(int mode, int value) 34 | { 35 | static int error; 36 | 37 | if (mode == 1) 38 | error = value; 39 | return (error); 40 | } 41 | 42 | int g_error_msg(int mode, int value) 43 | { 44 | static int error_msg; 45 | 46 | if (mode == 1) 47 | error_msg = value; 48 | return (error_msg); 49 | } 50 | 51 | int g_return(int mode, int value) 52 | { 53 | static int return_value; 54 | 55 | if (mode == 1) 56 | return_value = value; 57 | return (return_value); 58 | } 59 | -------------------------------------------------------------------------------- /srcs/main/my_global_two.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* my_global_two.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/23 12:51:26 by loamar #+# #+# */ 9 | /* Updated: 2021/04/26 21:56:46 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | int g_bytes(int mode, int value) 16 | { 17 | static int bytes; 18 | 19 | if (mode == 1) 20 | bytes = value; 21 | return (bytes); 22 | } 23 | -------------------------------------------------------------------------------- /srcs/termcap/delete_char.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* delete_char.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/05/01 12:47:53 by loamar #+# #+# */ 9 | /* Updated: 2021/05/02 00:38:15 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../includes/libshell.h" 14 | 15 | void delete_char(t_msh *msh) 16 | { 17 | char *str; 18 | int len; 19 | 20 | if (!msh->termcap->line || msh->termcap->line == NULL) 21 | return ; 22 | len = ft_strlen(msh->termcap->line); 23 | if (len == 1 && msh->termcap->cur_pos == 1) 24 | { 25 | ft_strdel(&msh->termcap->line); 26 | msh->termcap->cur_pos--; 27 | return ; 28 | } 29 | if (!(str = (char*)ft_calloc(1, sizeof(char) * (len)))) 30 | return ; 31 | ft_strlcpy(str, msh->termcap->line, msh->termcap->cur_pos); 32 | ft_strncat(str, msh->termcap->line + msh->termcap->cur_pos, len - msh->termcap->cur_pos); 33 | ft_strdel(&msh->termcap->line); 34 | msh->termcap->line = str; 35 | msh->termcap->cur_pos--; 36 | } 37 | -------------------------------------------------------------------------------- /srcs/termcap/history/create_list_history.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_list_history.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/27 02:33:35 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 14:50:27 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | int ft_fill_empty_history(t_history_lair *history_lair, char *content) 16 | { 17 | t_history_list *new_element; 18 | 19 | if (!(new_element = (t_history_list *)malloc(sizeof(t_list)))) 20 | return (-1); 21 | new_element->content = content; 22 | new_element->previous = history_lair->start; 23 | new_element->next = history_lair->end; 24 | history_lair->end = new_element; 25 | history_lair->start = new_element; 26 | history_lair->size++; 27 | return (0); 28 | } 29 | 30 | int ft_fill_start_history(t_history_lair *history_lair, char *content) 31 | { 32 | t_history_list *new_element; 33 | 34 | if (!(new_element = (t_history_list *)malloc(sizeof(t_history_list)))) 35 | return (-1); 36 | new_element->content = content; 37 | new_element->previous = NULL; 38 | new_element->next = history_lair->start; 39 | history_lair->start->previous = new_element; 40 | history_lair->start = new_element; 41 | history_lair->size++; 42 | return (0); 43 | } 44 | 45 | int clear_history(t_history_lair *history_lair) 46 | { 47 | int loop; 48 | 49 | loop = 0; 50 | while (loop == 0) 51 | { 52 | if (pop_back_history(history_lair) == ERROR) 53 | loop = 1; 54 | } 55 | return (SUCCESS); 56 | } 57 | -------------------------------------------------------------------------------- /srcs/termcap/history/delete_list_history.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* delete_list_history.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/27 02:34:50 by loamar #+# #+# */ 9 | /* Updated: 2021/04/29 14:50:22 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | static int is_empty_history(t_history_lair *history_lair) 16 | { 17 | if (history_lair->size == 0) 18 | { 19 | free(history_lair); 20 | return (SUCCESS); 21 | } 22 | return (ERROR); 23 | } 24 | 25 | static void free_popback_history(t_history_list *temp) 26 | { 27 | if (temp->content) 28 | free(temp->content); 29 | free(temp); 30 | } 31 | 32 | int pop_back_history(t_history_lair *history_lair) 33 | { 34 | t_history_list *temp; 35 | 36 | if (is_empty_history(history_lair) == SUCCESS) 37 | return (ERROR); 38 | if (history_lair->size == 1) 39 | { 40 | temp = history_lair->start; 41 | history_lair->start = history_lair->start->next; 42 | if (history_lair->start == NULL) 43 | history_lair->end = NULL; 44 | else 45 | history_lair->start->previous = NULL; 46 | } 47 | else 48 | { 49 | temp = history_lair->end; 50 | history_lair->end->previous->next = NULL; 51 | history_lair->end = history_lair->end->previous; 52 | } 53 | free_popback_history(temp); 54 | history_lair->size--; 55 | return (SUCCESS); 56 | } 57 | -------------------------------------------------------------------------------- /srcs/termcap/history/handler_history.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handler_history.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: loamar +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/04/27 02:39:00 by loamar #+# #+# */ 9 | /* Updated: 2021/05/06 01:12:39 by loamar ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../../../includes/libshell.h" 14 | 15 | //isatty, ttyname, ttyslot, ioctl, 16 | //getenv, tcsetattr, tcgetattr, tgetent, tgetflag, 17 | //tgetnum, tgetstr, tgoto, tputs 18 | 19 | static void init_tc(t_msh *msh) 20 | { 21 | g_cm(GET, tgetstr("cm", NULL)); 22 | msh->termcap->ce = tgetstr("ce", NULL); 23 | msh->termcap->dl = tgetstr("DL", NULL); 24 | } 25 | 26 | void init_term(t_msh *msh) 27 | { 28 | char *term_type; 29 | 30 | term_type = NULL; 31 | if (!(term_type = getenv("TERM"))) 32 | term_type = "xterm"; 33 | tgetent(NULL, term_type); 34 | setupterm(NULL, STDOUT_FILENO, NULL); 35 | tcgetattr(0, &msh->termcap->term); 36 | tcgetattr(0, &msh->termcap->save); 37 | msh->termcap->term.c_lflag = msh->termcap->term.c_lflag & ~ICANON; 38 | msh->termcap->term.c_lflag = msh->termcap->term.c_lflag & ~ECHO; 39 | msh->termcap->term.c_cc[VMIN] = 1; 40 | msh->termcap->term.c_cc[VTIME] = 0; 41 | tcsetattr(0, TCSANOW, &msh->termcap->term); 42 | init_tc(msh); 43 | } 44 | 45 | static size_t ft_intlen(intmax_t n) 46 | { 47 | size_t len; 48 | 49 | len = 0; 50 | if (!n) 51 | len++; 52 | while (n) 53 | { 54 | n = n / 10; 55 | len++; 56 | } 57 | return (len); 58 | } 59 | 60 | void get_cursor_pos(t_msh *msh, int *col, int *rows) 61 | { 62 | int a; 63 | int pos; 64 | char buf[255]; 65 | int ret; 66 | 67 | a = 0; 68 | pos = 1; 69 | write(0, "\033[6n", 4); 70 | ret = read(0, buf, 254); 71 | buf[ret] = '\0'; 72 | while (buf[pos]) 73 | { 74 | if (buf[pos] >= 48 && buf[pos] <= 57) 75 | { 76 | if (a == 0) 77 | *rows = ft_atoi(&buf[pos]) - 1; 78 | else 79 | *col = ft_atoi(&buf[pos]) - 1; 80 | 81 | a++; 82 | pos += ft_intlen(msh->termcap->col) - 1; 83 | } 84 | pos++; 85 | } 86 | } 87 | 88 | int handler_history(t_msh *msh, char *line) 89 | { 90 | if (g_loop(GET, 0) == LOOP || !msh->history_lair) 91 | { 92 | msh->history_lair = init_history_lair(msh->history_lair); 93 | if (msh->history_lair == NULL) 94 | return (ERROR); 95 | msh->history_list = NULL; 96 | } 97 | if (msh->history_lair->size == 0) 98 | ft_fill_empty_history(msh->history_lair, line); 99 | else 100 | ft_fill_start_history(msh->history_lair, line); 101 | return (0); 102 | } 103 | -------------------------------------------------------------------------------- /text.txt: -------------------------------------------------------------------------------- 1 | git@vogsphere-v2.42.fr:vogsphere/intra-uuid-3e6da47d-3d0b-49fb-b727-95334e7eb88d-3460984 2 | --------------------------------------------------------------------------------