├── include ├── banner.png ├── libft.h └── minishell.h ├── Libft ├── GC.c ├── ft_putchar_fd.c ├── ft_tolower.c ├── ft_toupper.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isprint.c ├── ft_lstadd_front_bonus.c ├── ft_strlen.c ├── ft_putstr_fd.c ├── ft_isalpha.c ├── ft_lstiter_bonus.c ├── ft_lstlast_bonus.c ├── ft_lstdelone_bonus.c ├── ft_bzero.c ├── ft_putendl_fd.c ├── ft_lstsize_bonus.c ├── ft_striteri.c ├── ft_strcmp.c ├── ft_isalnum.c ├── ft_memset.c ├── ft_strchr.c ├── ft_strrchr.c ├── ft_lstadd_back_bonus.c ├── ft_lstnew_bonus.c ├── ft_memchr.c ├── ft_strncmp.c ├── ft_strlcpy.c ├── ft_lstclear_bonus.c ├── ft_strdup.c ├── ft_memcmp.c ├── ft_calloc.c ├── ft_memcpy.c ├── ft_putnbr_fd.c ├── ft_strnstr.c ├── ft_atoi.c ├── ft_strmapi.c ├── ft_lstmap_bonus.c ├── ft_strlcat.c ├── ft_strjoin.c ├── ft_substr.c ├── ft_memmove.c ├── ft_itoa.c ├── ft_strtrim.c ├── Makefile ├── ft_split.c └── ft_printf.c ├── setup.sh ├── src ├── errors_and_utils │ ├── wrapper_function.c │ ├── some_printing_functions.c │ ├── env_and_expand_utils.c │ ├── errors_managment_and_heardoc.c │ ├── lists_utils.c │ ├── heredoc_and_lists_utils.c │ ├── errors1.c │ ├── check_pipe_and_quotes_errors.c │ └── expand_utils.c ├── signals │ └── handlingsignals.c ├── parsing │ ├── parsing_change_cmd_to_list.c │ ├── update_command_list.c │ ├── parsing_fill_in_commands_list.c │ ├── parsing.c │ ├── open_heredoc.c │ ├── parsing_expand_vars.c │ ├── expand_variable.c │ ├── parsing_split_with_words.c │ ├── parsing_redirections_errors.c │ ├── parsing_change_list_to_command_list.c │ ├── parsing_init_tokens.c │ └── parsing_split_with_redirections_pipe.c ├── minishell.c ├── builtins │ ├── echo.c │ ├── pwd.c │ ├── env.c │ ├── cd.c │ ├── export.c │ └── unset_exit.c ├── executing │ ├── wait_for_processes_and_print_errors_utils.c │ ├── files_handeling.c │ ├── executing.c │ └── execute_command.c └── lists │ └── linked_list_handling_functions.c ├── Makefile └── README.md /include/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ERROR244/minishell/HEAD/include/banner.png -------------------------------------------------------------------------------- /Libft/GC.c: -------------------------------------------------------------------------------- 1 | #include "libft.h" 2 | 3 | Node* insertNode(Node* node, void* value) 4 | { 5 | Node* newNode = malloc(sizeof(Node)); 6 | Node* head = node; 7 | 8 | newNode->addr = value; 9 | newNode->next = NULL; 10 | if (node == NULL) 11 | return (newNode); 12 | while (node && node->next) 13 | node = node->next; 14 | node->next = newNode; 15 | return (head); 16 | } 17 | 18 | void deleteList(Node* node) 19 | { 20 | Node* curr; 21 | 22 | while (node) 23 | { 24 | curr = node->next; 25 | free(node->addr); 26 | free(node); 27 | node = curr; 28 | } 29 | } 30 | 31 | void *ft_malloc(size_t size, Node *node) 32 | { 33 | void *block = malloc(size); 34 | 35 | if (block == NULL) 36 | { 37 | deleteList(node); 38 | exit(EXIT_FAILURE); 39 | } 40 | 41 | 42 | insertNode(node, block); 43 | return (block); 44 | } -------------------------------------------------------------------------------- /Libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/03 16:27:19 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:39:34 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | if (fd >= 0) 18 | write(fd, &c, 1); 19 | } 20 | -------------------------------------------------------------------------------- /Libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 19:03:37 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 11:16:33 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (c >= 65 && c <= 90) 18 | c += 32; 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /Libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 19:10:50 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 11:16:29 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (c >= 97 && c <= 122) 18 | c -= 32; 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /Libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:40:24 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/05 14:44:27 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii( int c ) 16 | { 17 | if (c >= 0 && c <= 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /Libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:41:46 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 13:42:27 by ksohail- ### ########.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 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /Libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:42:36 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/05 14:46:22 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint( int c ) 16 | { 17 | if (c >= 32 && c < 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Update and upgrade the system 4 | sudo apt update && sudo apt upgrade -y 5 | # Install zsh, git, wget, and curl 6 | sudo apt install -y zsh git wget curl vim 7 | 8 | # Install Visual Studio Code (.deb) 9 | wget -O vscode.deb https://go.microsoft.com/fwlink/?LinkID=760868 10 | sudo dpkg -i vscode.deb 11 | sudo apt install -f -y # Install dependencies if any are missing 12 | rm vscode.deb 13 | 14 | # Install Google Chrome (.deb) 15 | wget -O chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 16 | sudo dpkg -i chrome.deb 17 | sudo apt install -f -y # Install dependencies if any are missing 18 | rm chrome.deb 19 | 20 | # Generate SSH key for GitHub 21 | echo "Generating SSH key for GitHub..." 22 | ssh-keygen -t ed25519 -C "khalilsohail24@gmail.com" -f ~/.ssh/id_ed25519 -N "" 23 | eval "$(ssh-agent -s)" 24 | ssh-add ~/.ssh/id_ed25519 25 | echo "Your SSH public key is:" 26 | cat ~/.ssh/id_ed25519.pub 27 | echo "Add this key to your GitHub account: https://github.com/settings/keys" 28 | 29 | 30 | echo "Installation and setup complete!" 31 | -------------------------------------------------------------------------------- /Libft/ft_lstadd_front_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 15:00:23 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/18 10:04:53 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_front(t_list **lst, t_list *new) 16 | { 17 | if (new && lst) 18 | { 19 | new->next = *lst; 20 | *lst = new; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:33:11 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/04 09:52:15 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!s) 21 | return (0); 22 | while (s[i]) 23 | i++; 24 | return (i); 25 | } 26 | -------------------------------------------------------------------------------- /Libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/06 11:17:54 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:39:07 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char *s, int fd) 16 | { 17 | if (fd >= 0 && s != NULL) 18 | { 19 | while (*s) 20 | { 21 | ft_putchar_fd(*s, fd); 22 | s++; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:34:14 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/07 13:47:42 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 18 | return (1); 19 | if (c == '_' || c == '?') 20 | return (2); 21 | return (0); 22 | } 23 | -------------------------------------------------------------------------------- /Libft/ft_lstiter_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/05 15:33:42 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/17 14:15:50 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(void *)) 16 | { 17 | if (!lst || !f) 18 | return ; 19 | while (lst) 20 | { 21 | f(lst->content); 22 | lst = lst->next; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Libft/ft_lstlast_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 15:51:06 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/17 14:15:47 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstlast(t_list *lst) 16 | { 17 | if (lst == NULL) 18 | return (lst); 19 | while (lst->next != NULL) 20 | { 21 | lst = lst->next; 22 | } 23 | return (lst); 24 | } 25 | -------------------------------------------------------------------------------- /Libft/ft_lstdelone_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 17:10:22 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:36:42 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 16 | { 17 | if (lst && del) 18 | { 19 | if (lst && lst->content) 20 | { 21 | del(lst->content); 22 | } 23 | free(lst); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 11:34:58 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 11:21:29 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | unsigned char *ptr; 18 | size_t i; 19 | 20 | i = 0; 21 | ptr = (unsigned char *)s; 22 | while (i < n) 23 | { 24 | *(ptr + i) = 0; 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 10:51:01 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:39:27 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char *s, int fd) 16 | { 17 | if (fd >= 0 && s != NULL) 18 | { 19 | while (*s) 20 | { 21 | ft_putchar_fd(*s, fd); 22 | s++; 23 | } 24 | ft_putchar_fd('\n', fd); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Libft/ft_lstsize_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 15:29:17 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/17 14:18:24 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lstsize(t_list *lst) 16 | { 17 | t_list *curr; 18 | int i; 19 | 20 | i = 0; 21 | curr = lst; 22 | while (curr != NULL) 23 | { 24 | curr = curr->next; 25 | i++; 26 | } 27 | return (i); 28 | } 29 | -------------------------------------------------------------------------------- /Libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/03 15:28:42 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:37:38 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char*)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (f == NULL || s == NULL) 21 | return ; 22 | while (s[i]) 23 | { 24 | f(i, &s[i]); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Libft/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/04 17:39:39 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/09 08:26:58 by ksohail- ### ########.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 | if (!s1 || !s2) 21 | return (0); 22 | while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') 23 | i++; 24 | return (s1[i] - s2[i]); 25 | } 26 | -------------------------------------------------------------------------------- /Libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:37:59 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/07 13:46:06 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if (c == '?') 18 | return (2); 19 | if ((c >= 'A' && c < 'Z' + 1) || (c >= 'a' && c <= 'z') || c == '_') 20 | return (1); 21 | if (c >= '0' && c <= '9') 22 | return (1); 23 | return (0); 24 | } 25 | -------------------------------------------------------------------------------- /Libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/30 20:21:45 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 14:53:39 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *s, int c, size_t n) 16 | { 17 | unsigned char *ptr; 18 | size_t i; 19 | 20 | i = 0; 21 | ptr = (unsigned char *)s; 22 | while (i < n) 23 | { 24 | ptr[i] = (unsigned char)c; 25 | i++; 26 | } 27 | return (s); 28 | } 29 | -------------------------------------------------------------------------------- /Libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 17:35:05 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/15 13:03:12 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | char k; 18 | 19 | k = (char)c; 20 | while (*s) 21 | { 22 | if (*s == k) 23 | return ((char *)s); 24 | s++; 25 | } 26 | if (k == '\0') 27 | return ((char *)s); 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /Libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/05 18:01:41 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 11:24:48 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | char k; 18 | int i; 19 | 20 | k = (char)c; 21 | i = ft_strlen(s); 22 | while (i >= 0) 23 | { 24 | if (s[i] == k) 25 | return ((char *)&s[i]); 26 | i--; 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /Libft/ft_lstadd_back_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 16:06:10 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/17 13:32:36 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd_back(t_list **lst, t_list *new) 16 | { 17 | t_list *curr; 18 | 19 | if (!lst || !new) 20 | return ; 21 | if (*lst == NULL) 22 | { 23 | *lst = new; 24 | return ; 25 | } 26 | curr = ft_lstlast(*lst); 27 | curr->next = new; 28 | } 29 | -------------------------------------------------------------------------------- /Libft/ft_lstnew_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 14:17:22 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/17 14:15:19 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void *content) 16 | { 17 | t_list *n_node; 18 | 19 | n_node = (t_list *)ft_malloc(sizeof(struct s_list), g_signal.node); 20 | if (n_node == NULL) 21 | return (NULL); 22 | n_node->content = content; 23 | n_node->next = NULL; 24 | return (n_node); 25 | } 26 | -------------------------------------------------------------------------------- /Libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 11:43:02 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 17:45:37 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | unsigned char *ptr; 18 | unsigned char k; 19 | 20 | ptr = (unsigned char *)s; 21 | k = (unsigned char)c; 22 | while (n > 0) 23 | { 24 | if (*ptr == k) 25 | return (ptr); 26 | ptr++; 27 | n--; 28 | } 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /Libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 10:42:46 by ksohail- #+# #+# */ 9 | /* Updated: 2024/06/11 12:02:04 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | while (n > 0) 18 | { 19 | if (*s1 != *s2) 20 | return ((unsigned char)(*s1) - (unsigned char)(*s2)); 21 | if (*s1 == '\0' && *s2 == '\0') 22 | break ; 23 | s1++; 24 | s2++; 25 | n--; 26 | } 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /Libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 15:03:59 by ksohail- #+# #+# */ 9 | /* Updated: 2024/06/01 10:26:15 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcpy(char *dst, const char *src, size_t size) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!dst || !src) 21 | return (0); 22 | if (size == 0) 23 | return (ft_strlen(src)); 24 | while (src[i] && size - 1 > 0) 25 | { 26 | dst[i] = src[i]; 27 | size--; 28 | i++; 29 | } 30 | dst[i] = '\0'; 31 | return (ft_strlen(src)); 32 | } 33 | -------------------------------------------------------------------------------- /Libft/ft_lstclear_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 19:03:51 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/17 13:25:32 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstclear(t_list **lst, void (*del)(void*)) 16 | { 17 | t_list *curr1; 18 | t_list *curr2; 19 | 20 | if (lst == NULL || *lst == NULL || del == NULL) 21 | return ; 22 | curr1 = *lst; 23 | while (curr1->next != NULL) 24 | { 25 | curr2 = curr1->next; 26 | ft_lstdelone(curr1, del); 27 | curr1 = curr2; 28 | } 29 | ft_lstdelone(curr1, del); 30 | *lst = NULL; 31 | } 32 | -------------------------------------------------------------------------------- /Libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 19:47:38 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/09 08:26:40 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s) 16 | { 17 | char *ptr; 18 | int i; 19 | int k; 20 | int len; 21 | 22 | len = ft_strlen(s); 23 | i = 0; 24 | k = 0; 25 | ptr = (char *)ft_malloc((len + 1) * sizeof(char), g_signal.node); 26 | if (ptr == NULL) 27 | return (NULL); 28 | while (i < len) 29 | { 30 | ptr[i] = s[k]; 31 | i++; 32 | k++; 33 | } 34 | ptr[i] = '\0'; 35 | return (ptr); 36 | } 37 | -------------------------------------------------------------------------------- /Libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 11:29:47 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 18:21:49 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | const unsigned char *ptr1; 18 | const unsigned char *ptr2; 19 | 20 | ptr1 = (const unsigned char *)s1; 21 | ptr2 = (const unsigned char *)s2; 22 | while (n > 0) 23 | { 24 | if (*ptr1 != *ptr2) 25 | return ((unsigned char)(*ptr1) - (unsigned char)(*ptr2)); 26 | ptr1++; 27 | ptr2++; 28 | n--; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /Libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 20:45:57 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/12 14:33:21 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_calloc(size_t nmemb, size_t size) 16 | { 17 | size_t i; 18 | void *ptr; 19 | 20 | if (size != 0 && nmemb > ((size_t) -1 / size)) 21 | return (NULL); 22 | ptr = (void *)ft_malloc(nmemb * size, g_signal.node); 23 | if (ptr == NULL) 24 | return (NULL); 25 | else 26 | { 27 | i = 0; 28 | while (i < nmemb * size) 29 | { 30 | *((char *)ptr + i) = 0; 31 | i++; 32 | } 33 | } 34 | return (ptr); 35 | } 36 | -------------------------------------------------------------------------------- /Libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 11:58:10 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 11:24:24 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dest, const void *src, size_t n) 16 | { 17 | unsigned char *ptr1; 18 | const unsigned char *ptr2; 19 | size_t i; 20 | 21 | i = 0; 22 | if (dest == NULL && src == NULL) 23 | return (NULL); 24 | if (dest == src) 25 | return ((void *)src); 26 | ptr1 = (unsigned char *)dest; 27 | ptr2 = (const unsigned char *)src; 28 | while (i < n) 29 | { 30 | ptr1[i] = ptr2[i]; 31 | i++; 32 | } 33 | return (dest); 34 | } 35 | -------------------------------------------------------------------------------- /Libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/04 10:56:29 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/16 18:39:20 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void ft_function(int nb, int fd) 16 | { 17 | long n; 18 | 19 | n = nb; 20 | if (n < 0) 21 | { 22 | n *= -1; 23 | ft_putchar_fd('-', fd); 24 | } 25 | if (n <= 9) 26 | { 27 | ft_putchar_fd(n + '0', fd); 28 | return ; 29 | } 30 | if (n != 0) 31 | { 32 | ft_function(n / 10, fd); 33 | ft_putchar_fd((n % 10) + '0', fd); 34 | } 35 | } 36 | 37 | void ft_putnbr_fd(int n, int fd) 38 | { 39 | if (fd >= 0) 40 | ft_function(n, fd); 41 | } 42 | -------------------------------------------------------------------------------- /Libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/01 12:11:30 by ksohail- #+# #+# */ 9 | /* Updated: 2024/06/10 10:17:12 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *big, const char *little, size_t len) 16 | { 17 | size_t needle_len; 18 | 19 | needle_len = ft_strlen(little); 20 | if (needle_len == 0) 21 | return ((char *)big); 22 | if (*big == '\0' || len < needle_len) 23 | return (NULL); 24 | while (*big != '\0' && len >= needle_len) 25 | { 26 | if (ft_strncmp(big, little, needle_len) == 0) 27 | return ((char *)big); 28 | big++; 29 | len--; 30 | } 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /Libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 19:19:38 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/05 14:26:14 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *nptr) 16 | { 17 | int res; 18 | int i; 19 | int sign; 20 | 21 | i = 0; 22 | res = 0; 23 | sign = 1; 24 | while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32) 25 | i++; 26 | if (nptr[i] == '+' || nptr[i] == '-') 27 | { 28 | if (nptr[i] == '-') 29 | sign *= -1; 30 | i++; 31 | } 32 | while (nptr[i] >= 48 && nptr[i] <= 57) 33 | { 34 | res *= 10; 35 | res += nptr[i] - '0'; 36 | i++; 37 | } 38 | return (sign * res); 39 | } 40 | -------------------------------------------------------------------------------- /Libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/03 11:49:51 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/10 16:17:56 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *ptr; 18 | int len; 19 | 20 | if (s == NULL || f == NULL) 21 | return (NULL); 22 | len = ft_strlen(s); 23 | if (*s == '\0') 24 | return (ft_strdup("")); 25 | ptr = (char *)ft_malloc((len + 1) * sizeof(char), g_signal.node); 26 | if (ptr == NULL) 27 | return (NULL); 28 | ptr[len] = '\0'; 29 | len--; 30 | while (len >= 0) 31 | { 32 | ptr[len] = f(len, s[len]); 33 | len--; 34 | } 35 | return (ptr); 36 | } 37 | -------------------------------------------------------------------------------- /Libft/ft_lstmap_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/17 14:15:37 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/17 14:15:42 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 16 | { 17 | t_list *n_list; 18 | t_list *new; 19 | void *content_res; 20 | 21 | n_list = NULL; 22 | if (lst == NULL || f == NULL || del == NULL) 23 | return (NULL); 24 | while (lst != NULL) 25 | { 26 | content_res = f(lst->content); 27 | new = ft_lstnew(content_res); 28 | if (new == NULL) 29 | { 30 | free(content_res); 31 | free(new); 32 | ft_lstclear(&n_list, del); 33 | return (NULL); 34 | } 35 | ft_lstadd_back(&n_list, new); 36 | lst = lst->next; 37 | } 38 | return (n_list); 39 | } 40 | -------------------------------------------------------------------------------- /Libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 18:20:44 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/17 13:50:39 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t size) 16 | { 17 | size_t i; 18 | size_t res; 19 | size_t dst_length; 20 | size_t src_length; 21 | 22 | i = 0; 23 | src_length = ft_strlen(src); 24 | if (size == 0 && dst == NULL) 25 | return (src_length); 26 | dst_length = ft_strlen(dst); 27 | if (dst_length < size) 28 | res = dst_length + src_length; 29 | else 30 | res = size + src_length; 31 | if (size == 0) 32 | return (res); 33 | while (src[i] && dst_length + i < size - 1) 34 | { 35 | dst[dst_length + i] = src[i]; 36 | i++; 37 | } 38 | dst[dst_length + i] = '\0'; 39 | return (res); 40 | } 41 | -------------------------------------------------------------------------------- /Libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 12:54:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/27 10:22:20 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *ptr; 18 | int i; 19 | int j; 20 | 21 | if (s1 == NULL && s2 != NULL) 22 | return (ft_strdup((char *)s2)); 23 | if (s2 == NULL && s1 != NULL) 24 | return (ft_strdup((char *)s1)); 25 | if (s1 == NULL && s2 == NULL) 26 | return (NULL); 27 | ptr = (char *)ft_malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char), g_signal.node); 28 | if (ptr == 0) 29 | return (0); 30 | i = 0; 31 | j = 0; 32 | while (s1[i]) 33 | { 34 | ptr[i] = s1[i]; 35 | i++; 36 | } 37 | while (s2[j]) 38 | ptr[i++] = s2[j++]; 39 | ptr[i] = '\0'; 40 | return (ptr); 41 | } 42 | -------------------------------------------------------------------------------- /Libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 12:30:12 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/10 16:26:03 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_substr(char const *s, unsigned int start, size_t len) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | i = 0; 21 | if (s == NULL) 22 | return (NULL); 23 | if (start >= ft_strlen(s)) 24 | { 25 | ptr = (char *)ft_malloc(1 * sizeof(char), g_signal.node); 26 | if (ptr == NULL) 27 | return (NULL); 28 | *ptr = '\0'; 29 | return (ptr); 30 | } 31 | if (len > ft_strlen(s) - start) 32 | len = ft_strlen(s) - start; 33 | ptr = (char *)ft_malloc((len + 1) * sizeof(char), g_signal.node); 34 | if (ptr == NULL) 35 | return (NULL); 36 | while (s[start] && i < len) 37 | ptr[i++] = s[start++]; 38 | ptr[i] = '\0'; 39 | return (ptr); 40 | } 41 | -------------------------------------------------------------------------------- /Libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 12:29:48 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/05 16:40:05 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void ben10(size_t n, unsigned char *ptr1, const unsigned char *ptr2) 16 | { 17 | while (n > 0) 18 | { 19 | *ptr1 = *ptr2; 20 | ptr1++; 21 | ptr2++; 22 | n--; 23 | } 24 | } 25 | 26 | void *ft_memmove(void *dest, const void *src, size_t n) 27 | { 28 | unsigned char *ptr1; 29 | const unsigned char *ptr2; 30 | 31 | ptr1 = (unsigned char *)dest; 32 | ptr2 = (const unsigned char *)src; 33 | if (ptr1 == ptr2) 34 | return (dest); 35 | else if (ptr1 < ptr2) 36 | ben10(n, ptr1, ptr2); 37 | else 38 | { 39 | ptr1 += n - 1; 40 | ptr2 += n - 1; 41 | while (n > 0) 42 | { 43 | *ptr1 = *ptr2; 44 | ptr1--; 45 | ptr2--; 46 | n--; 47 | } 48 | } 49 | return (dest); 50 | } 51 | -------------------------------------------------------------------------------- /src/errors_and_utils/wrapper_function.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils7.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/11 19:11:04 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int check_double(char *cmd, int i) 16 | { 17 | while (++i && cmd[i]) 18 | { 19 | if (cmd[i] == 34) 20 | { 21 | i++; 22 | break ; 23 | } 24 | } 25 | return (i); 26 | } 27 | 28 | void ft_close(int fd, char *str) 29 | { 30 | int n; 31 | 32 | n = close(fd); 33 | if (n == -1) 34 | printf("Right here %s\n", str); 35 | } 36 | 37 | int ft_fork(void) 38 | { 39 | int n; 40 | 41 | n = fork(); 42 | if (n == -1) 43 | ft_exit(1); 44 | return (n); 45 | } 46 | 47 | void ft_dup2(int fd, int std) 48 | { 49 | int n; 50 | 51 | n = dup2(fd, std); 52 | if (n == -1) 53 | ft_exit(1); 54 | } 55 | 56 | void ft_clear(t_data *data) 57 | { 58 | lstclear(&data->lst); 59 | free_array(data->cmds); 60 | } 61 | -------------------------------------------------------------------------------- /src/signals/handlingsignals.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handlingsignals.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/09 12:59:14 by ohassani #+# #+# */ 9 | /* Updated: 2024/07/07 10:11:47 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void print_signals_c(int signal) 16 | { 17 | if (signal == SIGINT) 18 | { 19 | { 20 | printf("\n"); 21 | rl_replace_line("", 0); 22 | rl_on_new_line(); 23 | rl_redisplay(); 24 | g_signal.ret = 130; 25 | } 26 | } 27 | } 28 | 29 | void signal_hand2(int s) 30 | { 31 | (void)s; 32 | signal(SIGINT, SIG_DFL); 33 | write(1, "\n", 1); 34 | } 35 | 36 | void ft_handle_sigint(int sig) 37 | { 38 | if (g_signal.ff == 1) 39 | return ; 40 | (void)sig; 41 | printf("\n"); 42 | rl_replace_line("", 1); 43 | rl_on_new_line(); 44 | rl_redisplay(); 45 | g_signal.ret = 130; 46 | } 47 | 48 | void signal_handler(void) 49 | { 50 | signal(SIGQUIT, SIG_IGN); 51 | signal(SIGINT, ft_handle_sigint); 52 | signal(SIGTSTP, SIG_IGN); 53 | } 54 | -------------------------------------------------------------------------------- /src/errors_and_utils/some_printing_functions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* tmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 16:31:09 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:38:23 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | // debug function 16 | void print_array(char **str) 17 | { 18 | if (!str || !*str) 19 | return ; 20 | printf(":"); 21 | printf("%s", *str); 22 | str++; 23 | while (str && *str) 24 | { 25 | printf(" <-> %s", *str); 26 | str++; 27 | } 28 | printf(":\n"); 29 | } 30 | 31 | void ft_puterror_fd(char *str1, char *str2) 32 | { 33 | ft_putstr_fd(str1, 2); 34 | ft_putstr_fd(str2, 2); 35 | } 36 | 37 | void print_value(char *str) 38 | { 39 | while (str && *str && *str != '=') 40 | { 41 | printf("%c", *str); 42 | str++; 43 | } 44 | if (str && *str) 45 | { 46 | printf("=\""); 47 | str++; 48 | while (str && *str) 49 | { 50 | printf("%c", *str); 51 | str++; 52 | } 53 | printf("\""); 54 | } 55 | printf("\n"); 56 | } 57 | -------------------------------------------------------------------------------- /Libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/03 09:40:31 by ksohail- #+# #+# */ 9 | /* Updated: 2023/11/06 11:21:20 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *fill(char *ptr, long n, int x) 16 | { 17 | int i; 18 | 19 | i = n; 20 | if (n < 0) 21 | n *= -1; 22 | if (x >= 0) 23 | { 24 | ptr[x] = (n % 10) + '0'; 25 | n /= 10; 26 | fill(ptr, n, --x); 27 | } 28 | if (i < 0) 29 | ptr[0] = '-'; 30 | return (ptr); 31 | } 32 | 33 | static void nor_line(int *n, int *x) 34 | { 35 | if (*n < 0) 36 | { 37 | *x += 1; 38 | *n *= -1; 39 | } 40 | } 41 | 42 | char *ft_itoa(int n) 43 | { 44 | char *ptr; 45 | long i; 46 | int x; 47 | 48 | x = 0; 49 | i = n; 50 | nor_line(&n, &x); 51 | n /= 10; 52 | x++; 53 | while (n != 0) 54 | { 55 | n /= 10; 56 | x++; 57 | } 58 | ptr = (char *)ft_malloc((x + 1) * sizeof(char), g_signal.node); 59 | if (ptr == NULL) 60 | return (NULL); 61 | ptr[x] = '\0'; 62 | x--; 63 | return (fill(ptr, i, x)); 64 | } 65 | -------------------------------------------------------------------------------- /src/parsing/parsing_change_cmd_to_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils4.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 20:57:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:07:47 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int is_space_in(char *str) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = 0; 21 | j = 0; 22 | while (*str) 23 | { 24 | if (*str != ' ' && *str != ' ') 25 | i++; 26 | if (*str == ' ' || *str == ' ') 27 | j++; 28 | str++; 29 | } 30 | if (i == 0 || j == 0) 31 | return (1); 32 | return (0); 33 | } 34 | 35 | void get_list(char **cmd, int size, t_cmds **lst, t_data *data) 36 | { 37 | t_cmds *node; 38 | t_cmds *curr; 39 | int i; 40 | 41 | i = 0; 42 | if (cmd[0] == NULL) 43 | { 44 | *lst = lstnew("\n", *lst, NULL); 45 | return ; 46 | } 47 | *lst = lstnew(cmd[i++], *lst, NULL); 48 | (*lst)->data = data; 49 | while (i < size) 50 | { 51 | node = lstnew(cmd[i], *lst, NULL); 52 | node->data = data; 53 | curr = lstlast(*lst); 54 | curr->next = node; 55 | i++; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/minishell.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minishell.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 11:50:28 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | t_signal g_signal; 16 | 17 | int data_init(char **av, t_data *data, char **env) 18 | { 19 | (void)av; 20 | g_signal.ret = 0; 21 | data->path_flag = false; 22 | g_signal.ff = 0; 23 | if (!env[0]) 24 | data->path_flag = true; 25 | data->list_env = copieenv(env); 26 | return (1); 27 | } 28 | 29 | int main(int ac, char **av, char **env) 30 | { 31 | t_data data; 32 | 33 | if (ac != 1 || data_init(av, &data, env) == 0) 34 | return (1); 35 | while (1) 36 | { 37 | signal_handler(); 38 | data.env = linked_list_to_array(data.list_env); 39 | data.line = readline("minishell$ "); 40 | if (!data.line) 41 | break ; 42 | else if (check_quotation(data.line) != -1) 43 | { 44 | add_history(data.line); 45 | parsing(&data, NULL, NULL, -1); 46 | } 47 | free(data.line); 48 | free_array(data.env); 49 | g_signal.ff = 0; 50 | } 51 | if (data.env) 52 | free_array(data.env); 53 | lst_env_clear(&data.list_env); 54 | ft_putstr_fd("exit\n", 2); 55 | return (0); 56 | } 57 | -------------------------------------------------------------------------------- /Libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: error01 +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 13:29:10 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/02 17:51:34 by error01 ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int f1(int l, char const *s1, char const *set) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = ft_strlen(s1) - 1; 21 | while (i >= 0) 22 | { 23 | j = 0; 24 | while (set[j]) 25 | { 26 | if (s1[i] == set[j]) 27 | break ; 28 | j++; 29 | } 30 | if (s1[i] != set[j]) 31 | break ; 32 | l++; 33 | i--; 34 | } 35 | return (l); 36 | } 37 | 38 | static int f2(int k, char const *s1, char const *set) 39 | { 40 | int i; 41 | int j; 42 | 43 | i = 0; 44 | while (s1[i]) 45 | { 46 | j = 0; 47 | while (set[j]) 48 | { 49 | if (s1[i] == set[j]) 50 | break ; 51 | j++; 52 | } 53 | if (s1[i] != set[j]) 54 | break ; 55 | k++; 56 | i++; 57 | } 58 | return (k); 59 | } 60 | 61 | char *ft_strtrim(char const *s1, char const *set) 62 | { 63 | char *ptr; 64 | int a; 65 | int k; 66 | int l; 67 | 68 | if (s1 == NULL || set == NULL) 69 | return (NULL); 70 | k = f2(0, s1, set); 71 | l = f1(0, s1, set); 72 | a = ft_strlen(s1) - (k + l); 73 | l = 0; 74 | if (a < 0) 75 | a = 0; 76 | ptr = (char *)ft_malloc((a + 1) * sizeof(char), g_signal.node); 77 | if (ptr == NULL) 78 | return (NULL); 79 | while (l < a) 80 | ptr[l++] = s1[k++]; 81 | ptr[l] = '\0'; 82 | return (ptr); 83 | } 84 | -------------------------------------------------------------------------------- /Libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: error01 +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2023/11/01 14:29:42 by ksohail- #+# #+# # 9 | # Updated: 2024/05/24 17:16:53 by ksohail- ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | SRC = ft_lstadd_back_bonus.c\ 14 | ft_lstadd_front_bonus.c\ 15 | ft_lstclear_bonus.c\ 16 | ft_lstdelone_bonus.c\ 17 | ft_lstiter_bonus.c\ 18 | ft_lstlast_bonus.c\ 19 | ft_lstmap_bonus.c\ 20 | ft_lstnew_bonus.c\ 21 | ft_lstsize_bonus.c\ 22 | ft_atoi.c\ 23 | ft_isascii.c\ 24 | ft_memmove.c\ 25 | ft_putstr_fd.c\ 26 | ft_strjoin.c\ 27 | ft_putchar_fd.c\ 28 | ft_putendl_fd.c\ 29 | ft_putnbr_fd.c\ 30 | ft_strncmp.c\ 31 | ft_tolower.c\ 32 | ft_bzero.c\ 33 | ft_isdigit.c\ 34 | ft_memset.c\ 35 | ft_split.c\ 36 | ft_strlcat.c\ 37 | ft_strnstr.c\ 38 | ft_toupper.c\ 39 | ft_calloc.c\ 40 | ft_isprint.c\ 41 | ft_memchr.c\ 42 | ft_strchr.c\ 43 | ft_strlcpy.c\ 44 | ft_strrchr.c\ 45 | ft_isalnum.c\ 46 | ft_itoa.c\ 47 | ft_memcmp.c\ 48 | ft_strdup.c\ 49 | ft_strlen.c\ 50 | ft_strtrim.c\ 51 | ft_isalpha.c\ 52 | ft_memcpy.c\ 53 | ft_striteri.c\ 54 | ft_strmapi.c\ 55 | ft_substr.c\ 56 | 57 | CC = cc 58 | 59 | CFLAGS = -Wall -Wextra -Werror 60 | 61 | NAME = Libft.A 62 | OBJ = $(SRC:.c=.o) 63 | 64 | 65 | all: $(NAME) 66 | 67 | $(NAME): $(OBJ) 68 | @ar rcs $(NAME) $(OBJ) 69 | 70 | %.o: %.c 71 | $(CC) $(CFLAGS) -c $^ -o $@ 72 | 73 | clean: 74 | @rm -f $(OBJ) 75 | fclean: clean 76 | @rm -f $(NAME) 77 | re: fclean all 78 | 79 | .SECONDARY : ${OBJ} 80 | .PHONY: all clean fclean re 81 | -------------------------------------------------------------------------------- /src/builtins/echo.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* echo.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:36:48 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/08 13:54:59 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void exit_error(void) 16 | { 17 | write(1, "error\n", 7); 18 | return ; 19 | } 20 | 21 | bool check_n_flag(char *str) 22 | { 23 | str++; 24 | while (str && *str) 25 | { 26 | if (*str != 'n') 27 | return (false); 28 | str++; 29 | } 30 | return (true); 31 | } 32 | 33 | void ft_echo(char **com, bool flag, int i) 34 | { 35 | if (!com[0]) 36 | ft_putstr_fd("\n", 1); 37 | if (!com[0]) 38 | return ; 39 | if (com[0][0] == '-' && com[0][1] == 'n') 40 | { 41 | while (com[0] && com[0][0] == '-') 42 | { 43 | if (check_n_flag(com[0]) == true) 44 | { 45 | flag = false; 46 | com++; 47 | } 48 | else 49 | break ; 50 | } 51 | } 52 | while (com[i]) 53 | { 54 | ft_putstr_fd(com[i++], 1); 55 | if (com[i]) 56 | ft_putchar_fd(' ', 1); 57 | } 58 | if (flag == true) 59 | ft_putstr_fd("\n", 1); 60 | } 61 | 62 | char **creat_myenv(void) 63 | { 64 | char **ptr; 65 | char buffer[PATH_MAX]; 66 | 67 | ptr = (char **)ft_malloc((4) * sizeof(char *), g_signal.node); 68 | if (!ptr) 69 | exit_error(); 70 | ptr[0] = ft_strjoin("PWD=", getcwd(buffer, PATH_MAX)); 71 | ptr[1] = ft_strdup("SHLVL=1"); 72 | ptr[2] = ft_strdup("_=/usr/bin/env"); 73 | ptr[3] = NULL; 74 | return (ptr); 75 | } 76 | 77 | t_env *env_last(t_env *lst) 78 | { 79 | if (lst == NULL) 80 | return (lst); 81 | while (lst->next) 82 | { 83 | lst = lst->next; 84 | } 85 | return (lst); 86 | } 87 | -------------------------------------------------------------------------------- /src/executing/wait_for_processes_and_print_errors_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/07 10:09:31 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void ft_put2str_fd(char *s1, char *s2, int fd) 16 | { 17 | if (fd >= 0 && s1 && s2) 18 | { 19 | while (*s1) 20 | { 21 | ft_putchar_fd(*s1, fd); 22 | s1++; 23 | } 24 | while (*s2) 25 | { 26 | ft_putchar_fd(*s2, fd); 27 | s2++; 28 | } 29 | } 30 | } 31 | 32 | int error_msg(char *str) 33 | { 34 | ft_put2str_fd("minishel: syntax error near unexpected token", str, 2); 35 | return (2); 36 | } 37 | 38 | int check_quot(char *str) 39 | { 40 | int i; 41 | 42 | i = 0; 43 | while (*str) 44 | { 45 | if (*str == '\"' || *str == '\'') 46 | i++; 47 | str++; 48 | } 49 | return (i); 50 | } 51 | 52 | int get_command_size(t_command *command) 53 | { 54 | int size; 55 | 56 | size = 0; 57 | while (command) 58 | { 59 | command = command->next; 60 | size++; 61 | } 62 | return (size); 63 | } 64 | 65 | int wait_pid(int *pid, int cmd_num) 66 | { 67 | int i; 68 | int status; 69 | 70 | if (cmd_num == 0) 71 | return (0); 72 | i = cmd_num; 73 | status = 0; 74 | waitpid(pid[--i], &status, 0); 75 | if (WIFSIGNALED(status)) 76 | { 77 | if (WTERMSIG(status) == SIGQUIT) 78 | write(2, "Quit (core dumped)\n", 20); 79 | if (WTERMSIG(status) == SIGINT) 80 | write(2, "\n", 1); 81 | } 82 | if (WIFEXITED(status)) 83 | status = WEXITSTATUS(status); 84 | while (i >= 0) 85 | waitpid(pid[i--], 0, 0); 86 | if (g_signal.ret == 130) 87 | return (130); 88 | return (status); 89 | } 90 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME = minishell 2 | 3 | CC = cc 4 | 5 | CFLAGS = -Wall -Wextra -Werror -I./include 6 | 7 | SRCS = Libft/ft_split.c\ 8 | Libft/ft_putstr_fd.c\ 9 | Libft/ft_strjoin.c\ 10 | Libft/ft_strdup.c\ 11 | Libft/ft_putchar_fd.c\ 12 | Libft/ft_strnstr.c\ 13 | Libft/ft_strncmp.c\ 14 | Libft/ft_isalpha.c\ 15 | Libft/ft_strlen.c\ 16 | Libft/ft_lstsize_bonus.c\ 17 | Libft/ft_strcmp.c\ 18 | Libft/ft_isdigit.c\ 19 | Libft/ft_isalnum.c\ 20 | Libft/ft_strlcpy.c\ 21 | Libft/ft_strchr.c\ 22 | Libft/ft_itoa.c\ 23 | Libft/ft_atoi.c\ 24 | Libft/GC.c\ 25 | src/minishell.c\ 26 | src/parsing/expand_variable.c\ 27 | src/parsing/open_heredoc.c\ 28 | src/parsing/parsing_change_cmd_to_list.c\ 29 | src/parsing/parsing_change_list_to_command_list.c\ 30 | src/parsing/parsing_expand_vars.c\ 31 | src/parsing/parsing_fill_in_commands_list.c\ 32 | src/parsing/parsing_init_tokens.c\ 33 | src/parsing/parsing_redirections_errors.c\ 34 | src/parsing/parsing_split_with_redirections_pipe.c\ 35 | src/parsing/parsing_split_with_words.c\ 36 | src/parsing/parsing.c\ 37 | src/parsing/update_command_list.c\ 38 | src/lists/linked_list_handling_functions.c\ 39 | src/signals/handlingsignals.c\ 40 | src/executing/executing.c\ 41 | src/executing/execute_command.c\ 42 | src/executing/files_handeling.c\ 43 | src/executing/wait_for_processes_and_print_errors_utils.c\ 44 | src/errors_and_utils/env_and_expand_utils.c\ 45 | src/errors_and_utils/errors_managment_and_heardoc.c\ 46 | src/errors_and_utils/expand_utils.c\ 47 | src/errors_and_utils/heredoc_and_lists_utils.c\ 48 | src/errors_and_utils/lists_utils.c\ 49 | src/errors_and_utils/some_printing_functions.c\ 50 | src/errors_and_utils/wrapper_function.c\ 51 | src/errors_and_utils/check_pipe_and_quotes_errors.c\ 52 | src/builtins/cd.c\ 53 | src/builtins/env.c\ 54 | src/builtins/pwd.c\ 55 | src/builtins/echo.c\ 56 | src/builtins/export.c\ 57 | src/builtins/unset_exit.c\ 58 | 59 | OBJ = $(SRCS:.c=.o) 60 | 61 | AR = ar rcs 62 | 63 | RM = rm -rf 64 | 65 | all : $(NAME) 66 | 67 | $(NAME): $(OBJ) 68 | $(CC) $(CFLAGS) $(OBJ) -o minishell -lreadline 69 | 70 | 71 | %.o: %.c 72 | $(CC) $(CFLAGS) -c $< -o $@ 73 | 74 | clean : 75 | @$(RM) $(OBJ) 76 | 77 | fclean : 78 | @$(RM) $(OBJ) $(NAME) 79 | 80 | re : fclean all 81 | 82 | leak: all 83 | valgrind --leak-check=full \ 84 | --show-leak-kinds=all --track-fds=all --trace-children=yes ./$(NAME) 85 | 86 | .SECONDARY : ${OBJ} 87 | -------------------------------------------------------------------------------- /src/parsing/update_command_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils0.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/09 13:56:16 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int count_nodes(t_env *list) 16 | { 17 | int count; 18 | t_env *current; 19 | 20 | count = 0; 21 | current = list; 22 | while (current != NULL) 23 | { 24 | count++; 25 | current = current->next; 26 | } 27 | return (count); 28 | } 29 | 30 | int get_cmd_size(t_cmds *list) 31 | { 32 | int size; 33 | int i; 34 | 35 | size = 0; 36 | while (list) 37 | { 38 | if (list->token == Pipe) 39 | break ; 40 | else if (list->token == Cmd && list->cmd) 41 | { 42 | i = 0; 43 | while (list->cmd[i++]) 44 | size++; 45 | } 46 | else if (list->cmd[0]) 47 | { 48 | i = 1; 49 | while (list->cmd[i++]) 50 | size++; 51 | } 52 | list = list->next; 53 | } 54 | return (size); 55 | } 56 | 57 | t_cmds *find_cmd(t_cmds *list) 58 | { 59 | while (list) 60 | { 61 | if (list->token == Pipe) 62 | break ; 63 | if (list->token == Cmd) 64 | return (list); 65 | list = list->next; 66 | } 67 | return (NULL); 68 | } 69 | 70 | void get_command_done(t_cmds *list, t_cmds *head, char **command, bool flag) 71 | { 72 | int i; 73 | int j; 74 | 75 | list = find_cmd(list); 76 | i = -1; 77 | while (++i >= 0 && list && list->cmd[i]) 78 | command[i] = ft_strdup(list->cmd[i]); 79 | while (head) 80 | { 81 | if (head->token == Pipe) 82 | break ; 83 | else if (head->token == Cmd && flag == true) 84 | flag = false; 85 | else 86 | { 87 | if (head->token == Cmd) 88 | command[i++] = ft_strdup(head->cmd[0]); 89 | j = 1; 90 | while (head->cmd[0] && head->cmd[j]) 91 | command[i++] = ft_strdup(head->cmd[j++]); 92 | } 93 | head = head->next; 94 | } 95 | command[i] = NULL; 96 | } 97 | -------------------------------------------------------------------------------- /Libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/04 17:39:39 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 11:57:07 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int count(char const *s, char c) 16 | { 17 | int count; 18 | int in_word; 19 | 20 | count = 0; 21 | in_word = 0; 22 | while (s && *s) 23 | { 24 | if (*s == c || *s == ' ') 25 | in_word = 0; 26 | else 27 | { 28 | if (in_word == 0 && (*s != c && *s != ' ')) 29 | { 30 | count++; 31 | in_word = 1; 32 | } 33 | } 34 | s++; 35 | } 36 | return (count); 37 | } 38 | 39 | static void ft_free(char **ptr, int i) 40 | { 41 | int j; 42 | 43 | j = 0; 44 | while (j < i) 45 | free(ptr[j++]); 46 | free(ptr); 47 | } 48 | 49 | char const *get_index(char const *s, char c) 50 | { 51 | char tmp; 52 | 53 | while (*s && ((*s != c && *s != ' '))) 54 | { 55 | if ((*s == 39 || *s == 34) && s[1]) 56 | { 57 | tmp = *s; 58 | while (++s && s && s[1] && *s != tmp) 59 | { 60 | } 61 | } 62 | s++; 63 | } 64 | return (s); 65 | } 66 | 67 | static char **split(char const *s, char c, int i, char **ptr) 68 | { 69 | const char *start; 70 | 71 | while (*s) 72 | { 73 | if (*s != c && *s != ' ') 74 | { 75 | start = s; 76 | s = get_index(s, c); 77 | ptr[i] = ndup(start, s - start); 78 | if (ptr[i] == NULL) 79 | { 80 | ft_free(ptr, i); 81 | return (NULL); 82 | } 83 | i++; 84 | } 85 | else 86 | s++; 87 | } 88 | ptr[i] = NULL; 89 | return (ptr); 90 | } 91 | 92 | char **ft_split(char const *s, char c) 93 | { 94 | char **ptr; 95 | int word_count; 96 | 97 | if (s == NULL) 98 | return (NULL); 99 | word_count = count(s, c); 100 | ptr = ft_malloc((word_count + 1) * sizeof(char *), g_signal.node); 101 | if (ptr == NULL) 102 | { 103 | return (NULL); 104 | } 105 | return (split(s, c, 0, ptr)); 106 | } 107 | -------------------------------------------------------------------------------- /src/errors_and_utils/env_and_expand_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/11 09:26:41 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int how_many_dollar_in(char *str) 16 | { 17 | int i; 18 | int k; 19 | 20 | i = 0; 21 | k = 0; 22 | while (str && str[i]) 23 | { 24 | if (str[i] && str[i] == 39 && check_ex(str, i) == true) 25 | { 26 | i++; 27 | while (str[i] && str[i] != 39) 28 | i++; 29 | } 30 | else if (str[i] && str[i + 1] && str[i] == '$' && str[i + 1] != '$') 31 | { 32 | k++; 33 | i++; 34 | } 35 | else if (str[i] && str[i + 1] && str[i] == '$' && str[i + 1] == '$') 36 | i++; 37 | if (str[i]) 38 | i++; 39 | } 40 | return (k + 1); 41 | } 42 | 43 | t_env *find_env_var_index(t_env *list, char *va) 44 | { 45 | int len; 46 | 47 | len = ft_strlen(va); 48 | while (list) 49 | { 50 | if (ft_strncmp(list->var_name, va, len) == 0) 51 | return (list); 52 | list = list->next; 53 | } 54 | return (NULL); 55 | } 56 | 57 | char *ft_strjoin3(char const *s1, char c, char const *s2) 58 | { 59 | char *ptr; 60 | int i; 61 | int j; 62 | 63 | if (s1 == NULL && s2 != NULL) 64 | return (ft_strdup((char *)s2)); 65 | if (s2 == NULL && s1 != NULL) 66 | return (ft_strdup((char *)s1)); 67 | if (s1 == NULL && s2 == NULL) 68 | return (NULL); 69 | ptr = (char *)ft_malloc((ft_strlen(s1) + ft_strlen(s2) + 2) * sizeof(char), g_signal.node); 70 | if (ptr == 0) 71 | return (0); 72 | i = 0; 73 | j = 0; 74 | while (s1[i]) 75 | { 76 | ptr[i] = s1[i]; 77 | i++; 78 | } 79 | ptr[i++] = c; 80 | while (s2[j]) 81 | ptr[i++] = s2[j++]; 82 | ptr[i] = '\0'; 83 | return (ptr); 84 | } 85 | 86 | void insert_var_node_in_list(t_env *index, t_env *node) 87 | { 88 | if (index && node) 89 | { 90 | node->next = index->next; 91 | node->prev = index; 92 | if (index->next) 93 | index->next->prev = node; 94 | index->next = node; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/errors_and_utils/errors_managment_and_heardoc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* errors1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:22:35 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int ft_strcmp_for_heredoc(char *s1, char *s2) 16 | { 17 | char *str; 18 | int k; 19 | int i; 20 | 21 | i = 0; 22 | if (!s1 || !s2) 23 | return (0); 24 | str = get_string(ft_strdup(s2), 0, 0, get_size(s2)); 25 | while (s1[i] == str[i] && s1[i] != '\0' && str[i] != '\0') 26 | i++; 27 | k = s1[i] - str[i]; 28 | free(str); 29 | return (k); 30 | } 31 | 32 | int heredoc(t_cmds *head, t_cmds *curr, int i, int heredoc_num) 33 | { 34 | while (curr) 35 | { 36 | if (heredoc_num > 16) 37 | ft_putstr_fd("minishell: maximum here-doc count exceeded\n", 2); 38 | if (heredoc_num > 16) 39 | ft_exit(2); 40 | if (curr->token == HereDoc) 41 | heredoc_num++; 42 | curr = curr->next; 43 | } 44 | while (head && i != 130) 45 | { 46 | if (head->token == HereDocDel) 47 | { 48 | if (ft_strcmp(head->cmd[0], "\'\'") == 0 49 | || ft_strcmp(head->cmd[0], "\"\"") == 0) 50 | { 51 | free(head->cmd[0]); 52 | head->cmd[0] = ft_strdup(""); 53 | } 54 | i = open_heredoc(head, 0, 0, true); 55 | } 56 | head = head->next; 57 | } 58 | return (i); 59 | } 60 | 61 | int errors_managment(t_data *data, int i) 62 | { 63 | t_cmds *curr; 64 | t_cmds *head; 65 | 66 | curr = data->lst; 67 | head = curr; 68 | i = heredoc(head, head, i, 0); 69 | while (curr && i == 0) 70 | { 71 | if (curr->token == Pipe) 72 | i = check_for_pipe(curr); 73 | else if (curr->token == Output || curr->token == Input) 74 | i = check_for_in_out_put(curr); 75 | else if (curr->token == Append || curr->token == HereDoc) 76 | i = check_for_append_heredoc(curr); 77 | curr = curr->next; 78 | } 79 | return (i); 80 | } 81 | 82 | int cmdcheck(char *str) 83 | { 84 | int i; 85 | 86 | i = 0; 87 | if (!str) 88 | return (0); 89 | while (str[i]) 90 | { 91 | if (str[i] != ' ') 92 | return (0); 93 | i++; 94 | } 95 | return (1); 96 | } 97 | -------------------------------------------------------------------------------- /src/builtins/pwd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pwd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:40:02 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:34:39 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | bool is_it_inside(char *str) 16 | { 17 | while (str && *str) 18 | { 19 | if (*str == '=') 20 | return (true); 21 | str++; 22 | } 23 | return (false); 24 | } 25 | 26 | void my_pwd(t_env *env) 27 | { 28 | char buffer[PATH_MAX]; 29 | char *cur; 30 | char *str; 31 | 32 | cur = getcwd(buffer, PATH_MAX); 33 | if (cur) 34 | { 35 | ft_putstr_fd(cur, 1); 36 | ft_putchar_fd('\n', 1); 37 | } 38 | else 39 | { 40 | str = find_env_var(env, env, "PWD", false); 41 | ft_putstr_fd(str, 1); 42 | ft_putchar_fd('\n', 1); 43 | } 44 | } 45 | 46 | t_env *remove_node(t_env *head, t_env *node_to_remove) 47 | { 48 | if (!head || !node_to_remove) 49 | return (head); 50 | if (node_to_remove == head) 51 | { 52 | head = node_to_remove->next; 53 | if (head) 54 | head->prev = NULL; 55 | free(node_to_remove->var_name); 56 | free(node_to_remove); 57 | return (head); 58 | } 59 | if (node_to_remove->prev) 60 | node_to_remove->prev->next = node_to_remove->next; 61 | if (node_to_remove->next) 62 | node_to_remove->next->prev = node_to_remove->prev; 63 | free(node_to_remove->var_name); 64 | free(node_to_remove); 65 | return (head); 66 | } 67 | 68 | t_env *unset_env(t_env *list, char **com, t_data *data) 69 | { 70 | t_env *index; 71 | int i; 72 | 73 | i = 0; 74 | index = NULL; 75 | while (com[i]) 76 | { 77 | if (is_it_inside(com[i]) == false) 78 | { 79 | if (data->path_flag == true && ft_strcmp(com[i], "PATH") == 0) 80 | data->path_flag = false; 81 | index = find_env_var_index(list, com[i]); 82 | if (index) 83 | list = remove_node(list, index); 84 | } 85 | i++; 86 | } 87 | return (list); 88 | } 89 | 90 | int is_numeric(char *str) 91 | { 92 | while (*str) 93 | { 94 | if (ft_isdigit(*str) == 0) 95 | return (1); 96 | str++; 97 | } 98 | return (0); 99 | } 100 | -------------------------------------------------------------------------------- /src/errors_and_utils/lists_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils9.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 11:20:28 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | t_cmds *copy_node(char **cmd, t_token token, bool flag) 16 | { 17 | t_cmds *new_node; 18 | 19 | new_node = (t_cmds *)ft_malloc(sizeof(t_cmds), g_signal.node); 20 | if (flag == true) 21 | new_node->cmd = cmd; 22 | else 23 | new_node->cmd = get_name(cmd[0]); 24 | new_node->token = token; 25 | new_node->next = NULL; 26 | new_node->prev = NULL; 27 | return (new_node); 28 | } 29 | 30 | t_cmds *copy_single_node(t_cmds *curr, int *i) 31 | { 32 | if (curr->token == Cmd && *i == 0) 33 | { 34 | *i = 1; 35 | return (NULL); 36 | } 37 | return (copy_node(curr->cmd, curr->token, false)); 38 | } 39 | 40 | t_cmds *add_head_to_new_list(t_cmds *head, t_cmds *new_head) 41 | { 42 | if (new_head) 43 | { 44 | head->next = new_head; 45 | new_head->prev = head; 46 | } 47 | return (head); 48 | } 49 | 50 | t_cmds *copy_list(t_cmds *curr, char **command, int i) 51 | { 52 | t_cmds *new_node; 53 | t_cmds *head; 54 | t_cmds *new_head; 55 | t_cmds *new_tail; 56 | 57 | new_tail = NULL; 58 | new_head = NULL; 59 | head = copy_node(command, Cmd, true); 60 | while (curr && curr->token != Pipe) 61 | { 62 | if (curr->token != Cmd || i == 0) 63 | { 64 | new_node = copy_single_node(curr, &i); 65 | if (!new_head) 66 | new_head = new_node; 67 | else 68 | { 69 | new_tail->next = new_node; 70 | new_node->prev = new_tail; 71 | } 72 | new_tail = new_node; 73 | } 74 | curr = curr->next; 75 | } 76 | return (add_head_to_new_list(head, new_head)); 77 | } 78 | 79 | t_cmds *merge_lists(t_cmds *list1, t_cmds *list2) 80 | { 81 | t_cmds *current; 82 | t_cmds *pipe; 83 | 84 | if (!list1) 85 | return (list2); 86 | if (!list2) 87 | return (list1); 88 | pipe = copy_node(get_name("|"), Pipe, true); 89 | current = list1; 90 | while (current->next) 91 | current = current->next; 92 | current->next = pipe; 93 | pipe->prev = current; 94 | pipe->next = list2; 95 | list2->prev = pipe; 96 | return (list1); 97 | } 98 | -------------------------------------------------------------------------------- /src/parsing/parsing_fill_in_commands_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils8.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/25 16:26:19 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 10:25:18 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | t_slist *node_new(t_slist *lst, char *str, t_token token) 16 | { 17 | t_slist *n_node; 18 | t_slist *last_node; 19 | 20 | n_node = (t_slist *)ft_malloc(sizeof(struct s_slist), g_signal.node); 21 | if (n_node == NULL) 22 | return (NULL); 23 | n_node->cmd = str; 24 | n_node->token = token; 25 | n_node->next = NULL; 26 | if (lst == NULL) 27 | { 28 | n_node->prev = NULL; 29 | } 30 | else 31 | { 32 | last_node = nodes_last(lst); 33 | n_node->prev = last_node; 34 | } 35 | return (n_node); 36 | } 37 | 38 | t_slist *get_head(t_slist *list) 39 | { 40 | while (list) 41 | { 42 | if (!list->prev) 43 | break ; 44 | list = list->prev; 45 | } 46 | return (list); 47 | } 48 | 49 | char **array_copy(char **str) 50 | { 51 | char **ptr; 52 | int i; 53 | 54 | i = 0; 55 | if (!str) 56 | return (NULL); 57 | while (str[i]) 58 | i++; 59 | ptr = ft_malloc(sizeof(char *) * (i + 1), g_signal.node); 60 | i = 0; 61 | while (str[i]) 62 | { 63 | ptr[i] = ft_strdup(str[i]); 64 | i++; 65 | } 66 | ptr[i] = NULL; 67 | return (ptr); 68 | } 69 | 70 | void fill_in_commands(t_cmds **cl, t_command **command, t_slist **infile, 71 | t_slist **outfile) 72 | { 73 | if ((*cl)->cmd && !(*command)->cmd && ((*cl)->token == Cmd 74 | || (*cl)->token == Non)) 75 | (*command)->cmd = array_copy((*cl)->cmd); 76 | else if ((*cl)->cmd && (*cl)->token == Infile) 77 | { 78 | if (!(*infile)) 79 | (*infile) = node_new(*infile, (*cl)->cmd[0], (*cl)->token); 80 | else 81 | { 82 | (*infile)->next = node_new(*infile, (*cl)->cmd[0], (*cl)->token); 83 | *infile = (*infile)->next; 84 | } 85 | } 86 | else if ((*cl)->cmd[0] && ((*cl)->token == OutFile 87 | || (*cl)->token == AppendFile)) 88 | { 89 | if (!(*outfile)) 90 | (*outfile) = node_new(*outfile, (*cl)->cmd[0], (*cl)->token); 91 | else 92 | { 93 | (*outfile)->next = node_new(*outfile, (*cl)->cmd[0], (*cl)->token); 94 | *outfile = (*outfile)->next; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/errors_and_utils/heredoc_and_lists_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils8.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 10:02:48 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | bool check_next_for_both(char *str) 16 | { 17 | int i; 18 | int flag1; 19 | int flag2; 20 | 21 | i = 0; 22 | flag1 = 0; 23 | flag2 = 0; 24 | while (str[i]) 25 | { 26 | if (str[i] == '\"') 27 | flag1++; 28 | if (str[i] == '\'') 29 | flag2++; 30 | i++; 31 | } 32 | if ((flag1 != 0 && flag1 % 2 == 0) || (flag2 != 0 && flag2 % 2 == 0)) 33 | return (true); 34 | if (flag1 == 0 && flag2 == 0) 35 | return (true); 36 | return (false); 37 | } 38 | 39 | int get_2d_size(char **vars, char **lines) 40 | { 41 | int i; 42 | int size; 43 | 44 | i = 0; 45 | size = 0; 46 | while (vars[i]) 47 | size += ft_strlen(vars[i++]); 48 | i = 0; 49 | while (lines[i]) 50 | size += ft_strlen(lines[i++]); 51 | return (size); 52 | } 53 | 54 | int array_size(char **var) 55 | { 56 | int i; 57 | 58 | i = 0; 59 | while (var[i]) 60 | i++; 61 | return (i); 62 | } 63 | 64 | char **linked_list_to_array(t_env *list) 65 | { 66 | char **array; 67 | t_env *current; 68 | int count; 69 | int i; 70 | 71 | i = 0; 72 | count = count_nodes(list); 73 | array = (char **)ft_malloc((count + 1) * sizeof(char *), g_signal.node); 74 | if (array == NULL) 75 | ft_exit(EXIT_FAILURE); 76 | current = list; 77 | while (i < count) 78 | { 79 | array[i] = ft_strdup(current->var_name); 80 | if (array[i] == NULL) 81 | { 82 | perror("strdup"); 83 | free_array(array); 84 | return (NULL); 85 | } 86 | current = current->next; 87 | i++; 88 | } 89 | array[i] = NULL; 90 | return (array); 91 | } 92 | 93 | bool check_back_for_heredoc(char *str, int index) 94 | { 95 | if (index < 0 || !str[index]) 96 | return (false); 97 | index--; 98 | while (index >= 0 && str[index] == ' ') 99 | index--; 100 | if (index >= 1 && str[index] == '<') 101 | { 102 | index--; 103 | if (index >= 0 && str[index] == '<') 104 | return (true); 105 | } 106 | return (false); 107 | } 108 | -------------------------------------------------------------------------------- /src/parsing/parsing.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:12:11 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | char **get_name(char *str) 16 | { 17 | char **name; 18 | 19 | name = ft_malloc(sizeof(char *) * 2, g_signal.node); 20 | name[0] = ft_strdup(str); 21 | name[1] = NULL; 22 | return (name); 23 | } 24 | 25 | t_cmds *last_update_in_the_list(t_cmds *head, t_cmds *list, 26 | t_cmds *new_head_tmp, char **command) 27 | { 28 | t_cmds *new_head; 29 | 30 | while (head) 31 | { 32 | list = head; 33 | if (get_cmd_size(list) == 0) 34 | command = get_name(""); 35 | else 36 | { 37 | command = ft_malloc(sizeof(char *) * (get_cmd_size(list) + 1), g_signal.node); 38 | get_command_done(list, list, command, true); 39 | } 40 | new_head = copy_list(list, command, 0); 41 | if (new_head_tmp) 42 | new_head = merge_lists(new_head_tmp, new_head); 43 | while (list && list->token != Pipe) 44 | list = list->next; 45 | new_head_tmp = new_head; 46 | if (!list) 47 | break ; 48 | head = list->next; 49 | } 50 | while (new_head->prev) 51 | new_head = new_head->prev; 52 | return (new_head); 53 | } 54 | 55 | char **get_cmds_done(t_data *data, char **cmds) 56 | { 57 | data->line = check_expand(data->line, data); 58 | cmds = ft_split_msh(data->line); 59 | return (cmds); 60 | } 61 | 62 | void parsing(t_data *data, t_cmds *lst, t_command *commands, int i) 63 | { 64 | data->cmds = get_cmds_done(data, data->cmds); 65 | while (data->cmds[++i]) 66 | { 67 | if (cmdcheck(data->cmds[i]) == 0) 68 | data->cmds[i] = rm_spaces(data->cmds[i]); 69 | } 70 | get_list(data->cmds, i, &lst, data); 71 | init_tokens(lst, 0, lst); 72 | data->lst = lst; 73 | g_signal.ret = errors_managment(data, 0); 74 | remove_quotes(lst); 75 | if (g_signal.ret == 0 && g_signal.sig != -1) 76 | { 77 | lst = last_update_in_the_list(lst, lst, NULL, NULL); 78 | lstclear(&data->lst); 79 | data->lst = lst; 80 | commands = get_commands(lst); 81 | data->lst = lst; 82 | data->list = commands; 83 | g_signal.ret = executing(data); 84 | commands_clear(&commands); 85 | ft_clear(data); 86 | } 87 | else 88 | ft_clear(data); 89 | } 90 | -------------------------------------------------------------------------------- /Libft/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/17 20:47:13 by ksohail- #+# #+# */ 9 | /* Updated: 2024/02/01 01:39:21 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void check_and_print(int *return_len, char *str, va_list lst) 16 | { 17 | if (ft_strncmp("%c", str, 2) == 0) 18 | *return_len = print_ch(va_arg(lst, int)); 19 | else if (ft_strncmp("%d", str, 2) == 0) 20 | *return_len = print_i_d(va_arg(lst, int)); 21 | else if (ft_strncmp("%i", str, 2) == 0) 22 | *return_len = print_i_d(va_arg(lst, int)); 23 | else if (ft_strncmp("%p", str, 2) == 0) 24 | *return_len = print_p(va_arg(lst, void *)); 25 | else if (ft_strncmp("%s", str, 2) == 0) 26 | *return_len = print_str(va_arg(lst, char *)); 27 | else if (ft_strncmp("%u", str, 2) == 0) 28 | *return_len = print_u(va_arg(lst, unsigned int)); 29 | else if (ft_strncmp("%x", str, 2) == 0) 30 | *return_len = print_x(va_arg(lst, unsigned long), 'x'); 31 | else if (ft_strncmp("%X", str, 2) == 0) 32 | *return_len = print_x(va_arg(lst, unsigned long), 'X'); 33 | else if (ft_strncmp("%%", str, 2) == 0) 34 | *return_len = print_ch('%'); 35 | else if (str[1] != '\0') 36 | *return_len = print_ch(str[1]); 37 | else 38 | *return_len = 0; 39 | } 40 | 41 | static int to_check_ptr(const char *str, int i, va_list lst) 42 | { 43 | char ptr[3]; 44 | int return_len; 45 | 46 | return_len = 0; 47 | ptr[0] = str[i]; 48 | ptr[1] = str[i + 1]; 49 | ptr[2] = '\0'; 50 | check_and_print(&return_len, ptr, lst); 51 | return (return_len); 52 | } 53 | 54 | static int function(int *k, int i, const char *str, va_list lst) 55 | { 56 | if (str[i] == '%' && str[i + 1] != '\0') 57 | { 58 | *k = to_check_ptr(str, i, lst); 59 | i += 2; 60 | } 61 | else if (str[i] != '%') 62 | *k = print_ch(str[i++]); 63 | else 64 | i++; 65 | return (i); 66 | } 67 | 68 | int ft_printf(const char *str, ...) 69 | { 70 | int return_len; 71 | int i; 72 | int k; 73 | va_list lst; 74 | 75 | va_start(lst, str); 76 | i = 0; 77 | return_len = 0; 78 | while (str[i] != '\0') 79 | { 80 | k = 0; 81 | i = function(&k, i, str, lst); 82 | if (k < 0) 83 | { 84 | va_end(lst); 85 | return (k); 86 | } 87 | return_len += k; 88 | } 89 | va_end(lst); 90 | return (return_len); 91 | } 92 | -------------------------------------------------------------------------------- /src/errors_and_utils/errors1.c: -------------------------------------------------------------------------------- 1 | // /* ************************************************************************** */ 2 | // /* */ 3 | // /* ::: :::::::: */ 4 | // /* errors1.c :+: :+: :+: */ 5 | // /* +:+ +:+ +:+ */ 6 | // /* By: ksohail- +#+ +:+ +#+ */ 7 | // /* +#+#+#+#+#+ +#+ */ 8 | // /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | // /* Updated: 2024/07/13 09:22:35 by ksohail- ### ########.fr */ 10 | // /* */ 11 | // /* ************************************************************************** */ 12 | 13 | // #include "minishell.h" 14 | 15 | // int ft_strcmp_for_heredoc(char *s1, char *s2) 16 | // { 17 | // char *str; 18 | // int k; 19 | // int i; 20 | 21 | // i = 0; 22 | // if (!s1 || !s2) 23 | // return (0); 24 | // str = get_string(ft_strdup(s2), 0, 0, get_size(s2)); 25 | // while (s1[i] == str[i] && s1[i] != '\0' && str[i] != '\0') 26 | // i++; 27 | // k = s1[i] - str[i]; 28 | // free(str); 29 | // return (k); 30 | // } 31 | 32 | // int heredoc(t_cmds *head, t_cmds *curr, int i, int heredoc_num) 33 | // { 34 | // while (curr) 35 | // { 36 | // if (heredoc_num > 16) 37 | // ft_putstr_fd("minishell: maximum here-doc count exceeded\n", 2); 38 | // if (heredoc_num > 16) 39 | // exit(2); 40 | // if (curr->token == HereDoc) 41 | // heredoc_num++; 42 | // curr = curr->next; 43 | // } 44 | // while (head && i != 130) 45 | // { 46 | // if (head->token == HereDocDel) 47 | // { 48 | // if (ft_strcmp(head->cmd[0], "\'\'") == 0 49 | // || ft_strcmp(head->cmd[0], "\"\"") == 0) 50 | // { 51 | // free(head->cmd[0]); 52 | // head->cmd[0] = ft_strdup(""); 53 | // } 54 | // i = open_heredoc(head, 0, 0, true); 55 | // } 56 | // head = head->next; 57 | // } 58 | // return (i); 59 | // } 60 | 61 | // int errors_managment(t_data *data, int i) 62 | // { 63 | // t_cmds *curr; 64 | // t_cmds *head; 65 | 66 | // curr = data->lst; 67 | // head = curr; 68 | // i = heredoc(head, head, i, 0); 69 | // while (curr && i == 0) 70 | // { 71 | // if (curr->token == Pipe) 72 | // i = check_for_pipe(curr); 73 | // else if (curr->token == Output || curr->token == Input) 74 | // i = check_for_in_out_put(curr); 75 | // else if (curr->token == Append || curr->token == HereDoc) 76 | // i = check_for_append_heredoc(curr); 77 | // curr = curr->next; 78 | // } 79 | // return (i); 80 | // } 81 | 82 | // int cmdcheck(char *str) 83 | // { 84 | // int i; 85 | 86 | // i = 0; 87 | // if (!str) 88 | // return (0); 89 | // while (str[i]) 90 | // { 91 | // if (str[i] != ' ') 92 | // return (0); 93 | // i++; 94 | // } 95 | // return (1); 96 | // } 97 | -------------------------------------------------------------------------------- /src/builtins/env.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* env.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:37:53 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/07 12:00:24 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | t_env *env_new(t_env *lst, char *str) 16 | { 17 | t_env *n_node; 18 | t_env *last_node; 19 | 20 | n_node = (t_env *)ft_malloc(sizeof(struct s_env), g_signal.node); 21 | if (n_node == NULL) 22 | return (NULL); 23 | n_node->var_name = str; 24 | n_node->next = NULL; 25 | if (lst == NULL) 26 | { 27 | n_node->prev = NULL; 28 | } 29 | else 30 | { 31 | last_node = env_last(lst); 32 | n_node->prev = last_node; 33 | } 34 | return (n_node); 35 | } 36 | 37 | void creat_env_list(t_env **list, char **env, int subshell, int i) 38 | { 39 | char *str; 40 | char **ptr; 41 | t_env *node; 42 | t_env *curr; 43 | 44 | *list = env_new(*list, ft_strdup(env[i++])); 45 | while (env[i]) 46 | { 47 | if (ft_strncmp("SHLVL", env[i], 4) == 0) 48 | { 49 | ptr = ft_split(env[i++], '='); 50 | subshell = ft_atoi(ptr[1]); 51 | str = ft_itoa(++subshell); 52 | free_array(ptr); 53 | node = env_new(*list, ft_strjoin3("SHLVL", '=', str)); 54 | free(str); 55 | } 56 | else 57 | node = env_new(*list, ft_strdup(env[i++])); 58 | curr = env_last(*list); 59 | curr->next = node; 60 | } 61 | } 62 | 63 | t_env *copieenv(char **env) 64 | { 65 | t_env *list; 66 | 67 | list = NULL; 68 | if (!env[0]) 69 | { 70 | env = creat_myenv(); 71 | creat_env_list(&list, env, 0, 0); 72 | free_array(env); 73 | return (list); 74 | } 75 | creat_env_list(&list, env, 0, 0); 76 | return (list); 77 | } 78 | 79 | void ft_putendle(char *str, int fd) 80 | { 81 | if (!str) 82 | return ; 83 | ft_putstr_fd(str, fd); 84 | ft_putstr_fd("\n", fd); 85 | } 86 | 87 | void printmyenv(t_env *list) 88 | { 89 | int i; 90 | bool flag; 91 | 92 | while (list) 93 | { 94 | i = 0; 95 | flag = false; 96 | while (list->var_name[i]) 97 | { 98 | if (list->var_name[i] == '=') 99 | { 100 | flag = true; 101 | break ; 102 | } 103 | i++; 104 | } 105 | if (flag == true) 106 | { 107 | ft_putstr_fd(list->var_name, 1); 108 | ft_putchar_fd('\n', 1); 109 | } 110 | list = list->next; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/errors_and_utils/check_pipe_and_quotes_errors.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* errors0.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/04/27 14:11:49 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 14:56:12 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int error_msg_v1(char *str) 16 | { 17 | ft_putstr_fd("minishel: syntax error near unexpected token '", 2); 18 | ft_putstr_fd(str, 2); 19 | ft_putstr_fd("'\n", 2); 20 | return (2); 21 | } 22 | 23 | int error_msg_v2(char *str) 24 | { 25 | ft_putstr_fd("minishel: syntax error near unexpected token '", 2); 26 | ft_putstr_fd(str, 2); 27 | ft_putstr_fd(str, 2); 28 | ft_putstr_fd("'\n", 2); 29 | return (2); 30 | } 31 | 32 | int check_for_pipe(t_cmds *cmds) 33 | { 34 | if (!cmds->prev || cmds->prev->token == Non) 35 | { 36 | if (cmds->next && cmds->next->token == Pipe) 37 | return (error_msg(" `||'\n")); 38 | return (error_msg(" `|'\n")); 39 | } 40 | else if (cmds->prev && cmds->prev->token != Pipe) 41 | { 42 | if (cmds->next && cmds->next->token == Pipe) 43 | { 44 | if (cmds->next->token == Pipe) 45 | return (error_msg(" `||'\n")); 46 | else if (cmds->next->next && cmds->next->next->token == Pipe) 47 | { 48 | if (cmds->next->next->next 49 | && cmds->next->next->next->token == Pipe) 50 | return (error_msg(" `||'\n")); 51 | return (error_msg(" `|'\n")); 52 | } 53 | } 54 | else if (!cmds->next || (cmds->next && cmds->next->token == Non 55 | && !cmds->next->next)) 56 | return (error_msg(" 'newline'\n")); 57 | } 58 | return (0); 59 | } 60 | 61 | int check_quotation(char *str) 62 | { 63 | char c; 64 | int i; 65 | 66 | i = -1; 67 | if (!str) 68 | return (-1); 69 | while (str[++i]) 70 | { 71 | if (str[i] == 39 || str[i] == 34) 72 | { 73 | c = str[i]; 74 | while (str[++i]) 75 | { 76 | if (str[i] == c) 77 | break ; 78 | } 79 | } 80 | if (str[i] == '\0') 81 | { 82 | error_msg(" 'newline'\n"); 83 | return (-1); 84 | } 85 | } 86 | return (0); 87 | } 88 | 89 | bool check_eq(char *str) 90 | { 91 | int i; 92 | int j; 93 | 94 | i = 0; 95 | j = 0; 96 | while (str[i]) 97 | { 98 | if (str[i] == '=') 99 | j++; 100 | i++; 101 | } 102 | if (j == 0) 103 | return (true); 104 | return (false); 105 | } 106 | -------------------------------------------------------------------------------- /src/parsing/open_heredoc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* open_heredoc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 09:12:05 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void signal_herd(int pid) 16 | { 17 | (void)pid; 18 | printf("\n"); 19 | ft_exit(130); 20 | } 21 | 22 | int print_error(int k, char *str) 23 | { 24 | char *num; 25 | 26 | num = ft_itoa(k); 27 | ft_putstr_fd("minishell: warning: here-document at line ", 2); 28 | ft_putstr_fd(num, 2); 29 | free(num); 30 | ft_putstr_fd(" delimited by end-of-file (wanted `", 2); 31 | ft_putstr_fd(str, 2); 32 | ft_putstr_fd("')\n", 2); 33 | return (0); 34 | } 35 | 36 | void check_quot_and_filename(bool *flag, char **filename, char *str) 37 | { 38 | static int i; 39 | char *tmp1; 40 | 41 | if (check_quot(str) != 0) 42 | *flag = false; 43 | tmp1 = ft_itoa(i); 44 | *filename = ft_strjoin("/tmp/HereDoc", tmp1); 45 | free(tmp1); 46 | } 47 | 48 | void child(char *line, t_cmds *cmds, bool flag) 49 | { 50 | int k; 51 | int fd; 52 | 53 | fd = open(line, O_WRONLY | O_CREAT | O_TRUNC, 0600); 54 | free(line); 55 | signal(SIGINT, signal_herd); 56 | line = readline(">"); 57 | k = 1; 58 | while (1) 59 | { 60 | if (!line && print_error(k, cmds->cmd[0]) == 0) 61 | break ; 62 | else if (ft_strcmp_for_heredoc(line, cmds->cmd[0]) == 0) 63 | break ; 64 | k++; 65 | if (flag == true) 66 | line = expand_variable(line, cmds->data); 67 | ft_putstr_fd(line, fd); 68 | ft_putchar_fd('\n', fd); 69 | free(line); 70 | line = readline(">"); 71 | } 72 | close(fd); 73 | ft_exit(0); 74 | } 75 | 76 | int open_heredoc(t_cmds *cmds, int pid, int status, bool flag) 77 | { 78 | char *line; 79 | int fd0; 80 | 81 | fd0 = dup(0); 82 | check_quot_and_filename(&flag, &line, cmds->cmd[0]); 83 | g_signal.ff = 1; 84 | pid = ft_fork(); 85 | if (pid == 0) 86 | child(line, cmds, flag); 87 | else if (pid < 0) 88 | ft_putstr_fd("minishell: fork fail while creating the HereDoc\n", 2); 89 | waitpid(pid, &status, 0); 90 | g_signal.ff = 0; 91 | dup2(fd0, 0); 92 | close(fd0); 93 | free(cmds->cmd[0]); 94 | cmds->cmd[0] = line; 95 | cmds->prev->token = Input; 96 | cmds->token = Infile; 97 | if (status != 0) 98 | return (130); 99 | return (0); 100 | } 101 | -------------------------------------------------------------------------------- /src/executing/files_handeling.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils4.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/09 09:45:43 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int get_files_num(t_slist *list) 16 | { 17 | int size; 18 | 19 | size = 0; 20 | while (list) 21 | { 22 | size++; 23 | list = list->next; 24 | } 25 | return (size); 26 | } 27 | 28 | void ft_ft_close(int *fd) 29 | { 30 | int i; 31 | 32 | i = 0; 33 | while (fd && fd[i] != -11) 34 | { 35 | if (fd[i] == -1) 36 | break ; 37 | ft_close(fd[i], "fd[i]"); 38 | i++; 39 | } 40 | if (fd) 41 | free(fd); 42 | } 43 | 44 | int *print_open_error(int *fd, char *str) 45 | { 46 | ft_ft_close(fd); 47 | ft_putstr_fd("minishell: ", 2); 48 | ft_putstr_fd(str, 2); 49 | ft_putstr_fd(": ", 2); 50 | ft_putstr_fd(strerror(errno), 2); 51 | ft_putchar_fd('\n', 2); 52 | return (NULL); 53 | } 54 | 55 | int *ft_open(t_slist *list, int size, int j) 56 | { 57 | int *fd; 58 | 59 | size = get_files_num(list); 60 | fd = ft_malloc((size + 1) * (sizeof(int)), g_signal.node); 61 | fd[size] = -11; 62 | while (list) 63 | { 64 | if (list->token == Infile) 65 | fd[j] = open(list->cmd, O_RDONLY); 66 | else if (list->token == OutFile) 67 | fd[j] = open(list->cmd, O_WRONLY | O_CREAT | O_TRUNC, 0666); 68 | else if (list->token == AppendFile) 69 | fd[j] = open(list->cmd, O_WRONLY | O_CREAT | O_APPEND, 0666); 70 | if (fd[j] == -1) 71 | return (print_open_error(fd, list->cmd)); 72 | if (list->token == Infile) 73 | dup2(fd[j], STDIN_FILENO); 74 | else if (list->token == OutFile || list->token == AppendFile) 75 | dup2(fd[j], STDOUT_FILENO); 76 | j++; 77 | list = list->next; 78 | } 79 | return (fd); 80 | } 81 | 82 | int hand_the_redirectionin(t_command *lst) 83 | { 84 | int *filein; 85 | int *fileout; 86 | 87 | filein = NULL; 88 | fileout = NULL; 89 | if (lst->infile != NULL) 90 | { 91 | filein = ft_open(lst->infile, 0, 0); 92 | if (!filein) 93 | return (1); 94 | } 95 | if (lst->outfile) 96 | { 97 | if (lst->outfile) 98 | fileout = ft_open(lst->outfile, 0, 0); 99 | if (!fileout) 100 | return (1); 101 | } 102 | if (filein) 103 | ft_ft_close(filein); 104 | if (fileout) 105 | ft_ft_close(fileout); 106 | return (0); 107 | } 108 | -------------------------------------------------------------------------------- /src/parsing/parsing_expand_vars.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils6.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/22 14:47:27 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/11 09:14:31 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | char *grep_variable_name(char *line, int i, int j, int k) 16 | { 17 | int l; 18 | char *str; 19 | 20 | l = 0; 21 | if (!line) 22 | return (NULL); 23 | j = ++i; 24 | if (line[i] && line[i] == '?') 25 | return (ft_strdup("?")); 26 | while (line[i] && ft_isalnum(line[i]) == 1) 27 | i++; 28 | k = i - j; 29 | str = ft_malloc(sizeof(char) * (k + 1), g_signal.node); 30 | if (!str) 31 | return (NULL); 32 | while (l < k) 33 | str[l++] = line[j++]; 34 | str[l] = '\0'; 35 | return (str); 36 | } 37 | 38 | char *in_var(char *s1, int *in_word) 39 | { 40 | s1++; 41 | if (*s1 == '?') 42 | { 43 | s1++; 44 | *in_word = 0; 45 | } 46 | else if (*s1 != '$') 47 | { 48 | while (*s1 && ft_isalpha(*s1) != 0 && *s1 != '?') 49 | s1++; 50 | *in_word = 0; 51 | } 52 | return (s1); 53 | } 54 | 55 | int count_vars(char *s1) 56 | { 57 | int count; 58 | int in_word; 59 | 60 | count = 0; 61 | in_word = 0; 62 | while (s1 && *s1) 63 | { 64 | if (*s1 == '$' && ft_isalpha(s1[1]) != 0) 65 | s1 = in_var(s1, &in_word); 66 | else if (in_word == 0) 67 | { 68 | count++; 69 | while (*s1 && (*s1 != '$' || ft_isalnum(s1[0]) == 1 70 | || *s1 == '?' || !s1[1])) 71 | { 72 | if (s1[0] == '$' && s1[1] == '$') 73 | s1++; 74 | s1++; 75 | } 76 | in_word = 1; 77 | } 78 | else 79 | s1++; 80 | } 81 | return (count); 82 | } 83 | 84 | int dollar_is_in(char *str) 85 | { 86 | int i; 87 | int k; 88 | 89 | i = 0; 90 | k = 0; 91 | while (str[i]) 92 | { 93 | if (str[i] == '<') 94 | { 95 | if (str[i + 1] == '<') 96 | { 97 | i += 2; 98 | while (str[i] == ' ') 99 | i++; 100 | while (str[i] && ft_isalpha(str[i]) == 0) 101 | i++; 102 | } 103 | } 104 | if (!str[i]) 105 | break ; 106 | if (str[i] == '$') 107 | k++; 108 | i++; 109 | } 110 | return (k); 111 | } 112 | 113 | bool check_next(char *str) 114 | { 115 | int i; 116 | int flag; 117 | 118 | i = 0; 119 | flag = 0; 120 | while (str[i]) 121 | { 122 | if (str[i] == 34) 123 | flag++; 124 | i++; 125 | } 126 | if (flag % 2 == 0) 127 | return (true); 128 | return (false); 129 | } 130 | -------------------------------------------------------------------------------- /src/parsing/expand_variable.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* expand_variable.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/07 14:21:16 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 15:42:43 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | bool check_ex(char *str, int size) 16 | { 17 | int i; 18 | int flag; 19 | 20 | flag = 0; 21 | i = 0; 22 | if (size == 0) 23 | return (true); 24 | while (str[i] && i < size) 25 | { 26 | if (str[i] == 34) 27 | flag++; 28 | i++; 29 | } 30 | if (flag % 2 == 0) 31 | return (true); 32 | return (false); 33 | } 34 | 35 | char **get_vars_content(char **var, char **env, char *str, int i) 36 | { 37 | char **vars; 38 | int k; 39 | 40 | k = 0; 41 | vars = ft_malloc(sizeof(char *) * (array_size(var) + 1), g_signal.node); 42 | while (var[i]) 43 | { 44 | while (str[k] && str[k] != '$') 45 | { 46 | k++; 47 | if (str[k] == '$' && str[k + 1] == '$') 48 | k += 2; 49 | } 50 | if (var[i][0] == '?' && var[i][1] == '\0') 51 | vars[i] = ft_itoa(g_signal.ret); 52 | else if (check_back_for_heredoc(str, k) == true) 53 | vars[i] = ft_strjoin("$", var[i]); 54 | else 55 | vars[i] = get_content(env, var[i]); 56 | i++; 57 | k++; 58 | } 59 | vars[i] = NULL; 60 | free_array(var); 61 | return (vars); 62 | } 63 | 64 | char *join_vars(char **vars) 65 | { 66 | char *line; 67 | int i; 68 | int j; 69 | int size; 70 | 71 | i = 0; 72 | size = 0; 73 | j = 0; 74 | while (vars[i]) 75 | size += ft_strlen(vars[i++]); 76 | line = ft_malloc(sizeof(char) * (size + 1), g_signal.node); 77 | i = 0; 78 | size = 0; 79 | while (vars[i]) 80 | { 81 | j = 0; 82 | while (vars[i][j]) 83 | line[size++] = vars[i][j++]; 84 | i++; 85 | } 86 | line[size] = '\0'; 87 | return (line); 88 | } 89 | 90 | char *expand_variable(char *str, t_data *data) 91 | { 92 | char **var; 93 | char **spleted_line; 94 | char *line; 95 | t_line line_data; 96 | 97 | line = NULL; 98 | if (dollar_is_in(str)) 99 | { 100 | var = get_vars(str); 101 | var = get_vars_content(var, data->env, str, 0); 102 | spleted_line = ft_split_str(str); 103 | if (spleted_line == NULL) 104 | line = join_vars(var); 105 | else 106 | { 107 | line = get_final_line(spleted_line, var, str, &line_data); 108 | free_array(spleted_line); 109 | } 110 | free_array(var); 111 | } 112 | else 113 | return (str); 114 | free(str); 115 | return (line); 116 | } 117 | 118 | char *check_expand(char *str, t_data *data) 119 | { 120 | char *new_str; 121 | 122 | new_str = expand_variable(str, data); 123 | return (new_str); 124 | } 125 | -------------------------------------------------------------------------------- /src/parsing/parsing_split_with_words.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils7.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 20:57:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 16:33:12 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | static char *dup_size(char *s, size_t n) 16 | { 17 | char *dup; 18 | size_t i; 19 | 20 | if (!s) 21 | return (NULL); 22 | dup = (char *)ft_malloc((n + 1) * sizeof(char), g_signal.node); 23 | if (dup != NULL) 24 | { 25 | i = 0; 26 | while (i < n) 27 | { 28 | dup[i] = s[i]; 29 | i++; 30 | } 31 | dup[n] = '\0'; 32 | } 33 | return (dup); 34 | } 35 | 36 | static char **ft_free(char **ptr, int i) 37 | { 38 | int j; 39 | 40 | j = 0; 41 | while (j < i) 42 | free(ptr[j++]); 43 | free(ptr); 44 | return (NULL); 45 | } 46 | 47 | char *increment_s1(char *s1) 48 | { 49 | if (*s1 == '\'') 50 | { 51 | s1++; 52 | while (*s1 && *s1 != '\'') 53 | s1++; 54 | } 55 | else 56 | while (*s1 == '$' && s1[1]) 57 | s1++; 58 | return (s1); 59 | } 60 | 61 | char *get_word(char *s1) 62 | { 63 | while (*s1) 64 | { 65 | if (s1[0] == '$' && s1[1] && ft_isalnum(s1[1]) == 0) 66 | s1++; 67 | if (*s1 == 34) 68 | { 69 | s1++; 70 | while (*s1 && *s1 != 34) 71 | { 72 | if (*s1 == '$' && s1[1] != '$') 73 | return (s1); 74 | else 75 | while (*s1 == '$' && s1[1]) 76 | s1++; 77 | s1++; 78 | } 79 | } 80 | else if (*s1 == '$' && s1[1] && s1[1] != '$') 81 | break ; 82 | else 83 | s1 = increment_s1(s1); 84 | if (*s1) 85 | s1++; 86 | } 87 | return (s1); 88 | } 89 | 90 | static char **split(char *s1, int i, char **ptr, char *start) 91 | { 92 | while (s1 && *s1) 93 | { 94 | if (*s1 != '$' || ft_isalnum(s1[1]) == 0) 95 | { 96 | start = s1; 97 | s1 = get_word(s1); 98 | ptr[i++] = dup_size(start, s1 - start); 99 | if (ptr[i - 1] == NULL) 100 | return (ft_free(ptr, i - 1)); 101 | } 102 | else 103 | { 104 | s1++; 105 | while (*s1 && ft_isalpha(*s1) == 1) 106 | s1++; 107 | if ((*s1 >= '0' && *s1 <= '9') || (s1[-1] == '$' && *s1 == '?')) 108 | s1++; 109 | if (*s1 == '_') 110 | while (ft_isalnum(*s1) == 1) 111 | s1++; 112 | } 113 | } 114 | ptr[i] = NULL; 115 | return (ptr); 116 | } 117 | 118 | char **ft_split_str(char *s1) 119 | { 120 | char **ptr; 121 | int word_count; 122 | 123 | if (s1 == NULL) 124 | return (NULL); 125 | word_count = count_vars(s1); 126 | if (word_count == 0) 127 | return (NULL); 128 | ptr = ft_malloc((word_count + 2) * sizeof(char *), g_signal.node); 129 | if (ptr == NULL) 130 | { 131 | return (NULL); 132 | } 133 | return (split(s1, 0, ptr, NULL)); 134 | } 135 | -------------------------------------------------------------------------------- /src/errors_and_utils/expand_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils5.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/11 09:46:29 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | char *get_content(char **env, char *str) 16 | { 17 | int i; 18 | int size; 19 | char *ptr; 20 | 21 | i = 0; 22 | ptr = NULL; 23 | if (str == NULL) 24 | return (NULL); 25 | while (env[i]) 26 | { 27 | size = ft_strlen(str); 28 | ptr = ft_strnstr(env[i], str, size); 29 | if (ptr != NULL) 30 | { 31 | if (*(ptr + size) == '=') 32 | return (ft_strdup(ptr + size + 1)); 33 | else 34 | return (ft_strdup("")); 35 | } 36 | i++; 37 | } 38 | return (ft_strdup("")); 39 | } 40 | 41 | int fill_in(char *line, char *ptr, int pos) 42 | { 43 | int i; 44 | int tpos; 45 | 46 | i = 0; 47 | tpos = pos; 48 | if (ptr) 49 | { 50 | while (ptr[i]) 51 | line[tpos++] = ptr[i++]; 52 | } 53 | return (tpos); 54 | } 55 | 56 | void cmd_check(char *cmd) 57 | { 58 | int i; 59 | 60 | i = 0; 61 | while (cmd[i]) 62 | { 63 | if (cmd[i] == 34) 64 | i = check_double(cmd, i); 65 | else if (cmd[i] == 39) 66 | { 67 | i++; 68 | while (cmd[i]) 69 | { 70 | if (cmd[i] == 39) 71 | { 72 | i++; 73 | break ; 74 | } 75 | if (cmd[i] == '$') 76 | cmd[i] = '1'; 77 | i++; 78 | } 79 | } 80 | else 81 | i++; 82 | } 83 | } 84 | 85 | void init_line_data(t_line *line_data, char **lines, char **vars, char *cmd) 86 | { 87 | line_data->i = 0; 88 | line_data->k = 0; 89 | line_data->size = 0; 90 | line_data->pos = 0; 91 | line_data->line = ft_malloc(sizeof(char) * (get_2d_size(vars, lines) + 1), g_signal.node); 92 | cmd_check(cmd); 93 | } 94 | 95 | char *get_final_line(char **lines, char **vars, char *cmd, t_line *data) 96 | { 97 | init_line_data(data, lines, vars, cmd); 98 | while (cmd[data->size]) 99 | { 100 | if ((cmd[data->size] != '$' || cmd[data->size + 1] != '$') 101 | && lines[data->k]) 102 | { 103 | data->pos = fill_in(data->line, lines[data->k++], data->pos); 104 | while (cmd[data->size] && cmd[data->size] != '$') 105 | data->size++; 106 | } 107 | if (cmd[data->size] == '$' && vars[data->i] && vars[data->i][0] != '\0') 108 | { 109 | data->pos = fill_in(data->line, vars[data->i++], data->pos); 110 | data->size++; 111 | if (cmd[data->size] == '?') 112 | data->size++; 113 | else 114 | while (cmd[data->size] && ft_isalnum(cmd[data->size]) == 1) 115 | data->size++; 116 | } 117 | else if (cmd[data->size]) 118 | data->size++; 119 | } 120 | data->line[data->pos] = '\0'; 121 | return (data->line); 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minishell 2 | 3 | ![minishell](./include//banner.png) 4 | 5 | ## Table of Contents 6 | 7 | - [Introduction](#introduction) 8 | - [Features](#features) 9 | - [Built-ins](#built-ins) 10 | - [Installation](#installation) 11 | - [Usage](#usage) 12 | - [Examples](#examples) 13 | - [Project Structure](#project-structure) 14 | - [Contact](#Contact) 15 | 16 | ## Introduction 17 | 18 | `minishell` is a simple UNIX shell developed as part of the 42 School curriculum. It mimics the behavior of bash with a subset of its features, providing a command-line interface for interacting with the operating system. 19 | 20 | ## Features 21 | 22 | - Display a prompt when waiting for a new command. 23 | - Working command history. 24 | - Search and launch the right executable based on the PATH variable or using a relative or an absolute path. 25 | - Support for single and double quotes, preventing the shell from interpreting metacharacters within quotes. 26 | - Redirections: 27 | - `<` for input redirection. 28 | - `>` for output redirection. 29 | - `<<` for heredoc. 30 | - `>>` for appending output redirection. 31 | - Piping: Use `|` to connect the output of one command to the input of another. 32 | - Environment variable handling, including: 33 | - `$?` for the last executed command's exit status. 34 | - `$_` for the last argument of the previous command executed. 35 | - Signal handling for `ctrl-C`, `ctrl-D`, and `ctrl-\`. 36 | - `ctrl-C` displays a new prompt on a new line. 37 | - `ctrl-D` exits the shell. 38 | - `ctrl-\` does nothing. 39 | - Implementation of the following built-in commands: 40 | - `echo` with `-n` option. 41 | - `cd` with relative or absolute paths. 42 | - `pwd` without options. 43 | - `export` without options. 44 | - `unset` without options. 45 | - `env` without options or arguments. 46 | - `exit` without options. 47 | 48 | ## Built-ins 49 | 50 | - `echo [-n] [string ...]`: Prints the string to the standard output. 51 | - `cd [path]`: Changes the current directory to the specified path. 52 | - `pwd`: Prints the current working directory. 53 | - `export [name[=value] ...]`: Sets environment variables. 54 | - `unset [name ...]`: Unsets environment variables. 55 | - `env`: Prints the environment variables. 56 | - `exit`: Exits the shell. 57 | 58 | ## Installation 59 | 60 | 1. Clone the repository: 61 | ```sh 62 | git clone https://github.com/yourusername/minishell.git 63 | cd minishell 64 | ``` 65 | 66 | ## Usage 67 | 68 | 1. Build the project: 69 | ```sh 70 | make 71 | make clean 72 | ``` 73 | 2. run it: 74 | ```sh 75 | ./minishell 76 | ``` 77 | - `ctrl-D` to exit. 78 | 79 | ## Examples 80 | 81 | ```sh 82 | minishell$ echo "Hello, World!" 83 | Hello, World! 84 | minishell$ pwd 85 | /home/yourusername/minishell 86 | minishell$ export MYVAR="Hello" 87 | minishell$ echo $MYVAR 88 | Hello 89 | minishell$ ls | grep minishell 90 | minishell 91 | minishell$ cat < input.txt > output.txt 92 | minishell$ echo $_ 93 | output.txt 94 | ``` 95 | 96 | ## Project Structure 97 | 98 | ```sh 99 | minishell/ 100 | ├── include/ 101 | │ ├── minishell.h 102 | │ ├── banner.png 103 | ├── src/ 104 | │ ├── *.c 105 | ├── Libft/ 106 | │ ├── *.c 107 | │ ├── *.h 108 | ├── Makefile 109 | ├── README.md 110 | ``` 111 | 112 | ## Contact Information 113 | 114 | - **Email:** [khalilsohail24@gmail.com](mailto:khalilsohail24@gmail.com) 115 | - **Discord:** khalil_sohail 116 | - **LinkedIn:** [LinkedIn Profile](https://www.linkedin.com/in/khalil-sohail-5260892b8/) 117 | -------------------------------------------------------------------------------- /src/parsing/parsing_redirections_errors.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/11 23:48:23 by error01 #+# #+# */ 9 | /* Updated: 2024/07/12 15:36:48 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void free_array(char **array) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!array) 21 | return ; 22 | while (array[i]) 23 | { 24 | free(array[i]); 25 | i++; 26 | } 27 | free(array); 28 | } 29 | 30 | int check_for_in_out_put(t_cmds *cmds) 31 | { 32 | if (!cmds->next || (cmds->next->token == Non && !cmds->next->next)) 33 | return (error_msg(" 'newline'\n")); 34 | else if (cmds->next && cmds->next->next && cmds->next->token == Pipe 35 | && cmds->next->next->token == Pipe) 36 | return (error_msg_v2(cmds->next->cmd[0])); 37 | else if (cmds->next && cmds->next->operation == Operation) 38 | return (error_msg_v1(cmds->next->cmd[0])); 39 | else if (cmds->prev && cmds->prev->token == Non && cmds->prev->prev 40 | && cmds->prev->prev->token != Pipe) 41 | return (error_msg_v1(cmds->cmd[0])); 42 | return (0); 43 | } 44 | 45 | int check_for_append_heredoc(t_cmds *cmds) 46 | { 47 | if (!cmds->next || (cmds->next->token == Non && !cmds->next->next)) 48 | return (error_msg(" 'newline'\n")); 49 | else if (cmds->prev && cmds->prev->token == Non && cmds->prev->prev 50 | && cmds->prev->prev->token != Pipe) 51 | return (error_msg_v1(cmds->cmd[0])); 52 | else if (cmds->next && cmds->next->next && cmds->next->token == Pipe 53 | && cmds->next->next->token == Pipe) 54 | return (error_msg_v2(cmds->next->cmd[0])); 55 | else if (cmds->next && cmds->next->operation == Operation) 56 | return (error_msg_v1(cmds->next->cmd[0])); 57 | return (0); 58 | } 59 | 60 | char **get_file_name(char *str) 61 | { 62 | char **ptr; 63 | int size; 64 | int i; 65 | 66 | size = 0; 67 | i = 0; 68 | ptr = ft_malloc(sizeof(char *) * (3), g_signal.node); 69 | while (str[size] && str[size] != ' ' && str[size] != ' ') 70 | size++; 71 | ptr[0] = ft_malloc(sizeof(char) * (size + 1), g_signal.node); 72 | ft_strlcpy(ptr[0], str, size + 1); 73 | if (!str[size]) 74 | { 75 | ptr[1] = NULL; 76 | return (ptr); 77 | } 78 | i = 0; 79 | while (str[i]) 80 | i++; 81 | ptr[1] = ft_malloc(sizeof(char) * (i - size + 1), g_signal.node); 82 | ft_strlcpy(ptr[1], str + size, ft_strlen(str + size) + 1); 83 | ptr[2] = NULL; 84 | return (ptr); 85 | } 86 | 87 | char *rm_spaces(char *str) 88 | { 89 | int i; 90 | int k; 91 | int l; 92 | char *ptr; 93 | 94 | i = 0; 95 | while (str[i] && (str[i] == ' ' || str[i] == ' ')) 96 | i++; 97 | l = i; 98 | k = i; 99 | while (str[i++]) 100 | l++; 101 | i--; 102 | while (str[i] == ' ' || str[i] == ' ') 103 | { 104 | i--; 105 | l--; 106 | } 107 | ptr = ft_malloc(sizeof(char) * (l - k + 1), g_signal.node); 108 | i = 0; 109 | while (k < l) 110 | ptr[i++] = str[k++]; 111 | ptr[i] = '\0'; 112 | free(str); 113 | return (ptr); 114 | } 115 | -------------------------------------------------------------------------------- /include/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/10/31 11:39:24 by ksohail- #+# #+# */ 9 | /* Updated: 2024/05/21 14:35:16 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | 24 | typedef struct s_list 25 | { 26 | void *content; 27 | struct s_list *next; 28 | } t_list; 29 | 30 | typedef struct s_node { 31 | void* addr; 32 | struct s_node* next; 33 | } Node; 34 | 35 | Node* insertNode(Node* node, void* value); 36 | void deleteList(Node* node); 37 | void* ft_malloc(size_t size, Node *node); 38 | void ft_exit(int e); 39 | 40 | #include "minishell.h" 41 | 42 | int ft_atoi(const char *nptr); 43 | void ft_bzero(void *s, size_t n); 44 | void *ft_calloc(size_t nmemb, size_t size); 45 | int ft_isalnum(int c); 46 | int ft_isalpha(int c); 47 | int ft_isascii( int c ); 48 | int ft_isdigit(int c); 49 | int ft_isprint( int c ); 50 | char *ft_itoa(int n); 51 | void ft_lstadd_back(t_list **lst, t_list *new); 52 | void ft_lstadd_front(t_list **lst, t_list *new); 53 | void ft_lstclear(t_list **lst, void (*del)(void*)); 54 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 55 | void ft_lstiter(t_list *lst, void (*f)(void *)); 56 | t_list *ft_lstlast(t_list *lst); 57 | t_list *ft_lstnew(void *content); 58 | int ft_lstsize(t_list *lst); 59 | void *ft_memchr(const void *s, int c, size_t n); 60 | int ft_memcmp(const void *s1, const void *s2, size_t n); 61 | void *ft_memcpy(void *dest, const void *src, size_t n); 62 | void *ft_memmove(void *dest, const void *src, size_t n); 63 | void *ft_memset(void *s, int c, size_t n); 64 | void ft_putchar_fd(char c, int fd); 65 | void ft_putendl_fd(char *s, int fd); 66 | void ft_putnbr_fd(int n, int fd); 67 | void ft_putstr_fd(char *s, int fd); 68 | char **ft_split_msh(char const *s); 69 | char **ft_split(char const *s, char c); 70 | char *ft_strchr(const char *s, int c); 71 | char *ft_strdup(const char *s); 72 | void ft_striteri(char *s, void (*f)(unsigned int, char*)); 73 | char *ft_strjoin(char const *s1, char const *s2); 74 | size_t ft_strlcat(char *dst, const char *src, size_t size); 75 | size_t ft_strlcpy(char *dst, const char *src, size_t size); 76 | size_t ft_strlen(const char *s); 77 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 78 | int ft_strncmp(const char *s1, const char *s2, size_t n); 79 | int ft_strcmp(char *s1, char *s2); 80 | char *ft_strnstr(const char *big, const char *little, size_t len); 81 | char *ft_strrchr(const char *s, int c); 82 | char *ft_strtrim(char const *s1, char const *set); 83 | char *ft_substr(char const *s, unsigned int start, size_t len); 84 | int ft_tolower(int c); 85 | int ft_toupper(int c); 86 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /src/parsing/parsing_change_list_to_command_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils5.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 20:57:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 12:05:44 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | size_t get_size(char *str) 16 | { 17 | size_t k; 18 | size_t i; 19 | char tmp; 20 | 21 | i = 0; 22 | k = 0; 23 | while (str[i]) 24 | { 25 | if ((str[i] == '\'' && str[i + 1] && str[i + 1] != '\'') 26 | || (str[i] == '\"' && str[i + 1] && str[i + 1] != '\"')) 27 | { 28 | tmp = str[i]; 29 | i++; 30 | while (str[i] && str[i + 1] && str[i] != tmp) 31 | i++; 32 | k += 2; 33 | } 34 | else if ((str[i] == '\'' && str[i + 1] == '\'') 35 | || (str[i] == '\"' && str[i + 1] == '\"')) 36 | i++; 37 | i++; 38 | } 39 | return (i - k); 40 | } 41 | 42 | char *get_string(char *s, size_t i, size_t k, size_t size) 43 | { 44 | char *ptr; 45 | char tmp; 46 | 47 | ptr = ft_malloc(sizeof(char) * (size + 1), g_signal.node); 48 | while (k < size) 49 | { 50 | while ((s[i] == '\'' && s[i + 1] && s[i + 1] != '\'') 51 | || (s[i] == '\"' && s[i + 1] && s[i + 1] != '\"')) 52 | { 53 | tmp = s[i++]; 54 | while (s[i] && s[i + 1] && s[i] != tmp) 55 | ptr[k++] = s[i++]; 56 | i++; 57 | } 58 | if (k == size) 59 | break ; 60 | if ((s[i] == '\'' && s[i + 1] == '\'') 61 | || (s[i] == '\"' && s[i + 1] == '\"')) 62 | ptr[k++] = s[i++]; 63 | ptr[k++] = s[i++]; 64 | } 65 | ptr[k] = '\0'; 66 | free(s); 67 | return (ptr); 68 | } 69 | 70 | void remove_quotes(t_cmds *lst) 71 | { 72 | int i; 73 | 74 | while (lst) 75 | { 76 | i = 0; 77 | while (lst->cmd[i]) 78 | { 79 | if (lst->cmd[i] != NULL 80 | && get_size(lst->cmd[i]) != ft_strlen(lst->cmd[i])) 81 | { 82 | lst->cmd[i] = get_string(lst->cmd[i], 0, 0, 83 | get_size(lst->cmd[i])); 84 | } 85 | i++; 86 | } 87 | lst = lst->next; 88 | } 89 | } 90 | 91 | t_command *get_command(t_cmds *lst) 92 | { 93 | t_command *command; 94 | t_command *node; 95 | t_command *curr; 96 | 97 | command = NULL; 98 | command = command_new(command); 99 | while (lst) 100 | { 101 | if (lst->token == Pipe) 102 | { 103 | node = command_new(command); 104 | curr = command_last(command); 105 | curr->next = node; 106 | } 107 | lst = lst->next; 108 | } 109 | return (command); 110 | } 111 | 112 | t_command *get_commands(t_cmds *lst) 113 | { 114 | t_command *command; 115 | t_command *head; 116 | t_slist *infile; 117 | t_slist *outfile; 118 | 119 | command = get_command(lst); 120 | head = command; 121 | while (command && lst) 122 | { 123 | infile = NULL; 124 | outfile = NULL; 125 | while (lst && lst->token != Pipe) 126 | { 127 | fill_in_commands(&lst, &command, &infile, &outfile); 128 | lst = lst->next; 129 | } 130 | command->infile = get_head(infile); 131 | command->outfile = get_head(outfile); 132 | if (lst) 133 | lst = lst->next; 134 | command = command->next; 135 | } 136 | command = head; 137 | return (command); 138 | } 139 | -------------------------------------------------------------------------------- /src/builtins/cd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:37:35 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 14:49:16 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void set_env_if_plus(t_env *index, char *export) 16 | { 17 | char *tmp; 18 | 19 | tmp = index->var_name; 20 | if (tmp[ft_strlen(tmp) - 1] != '=' && check_eq(tmp) == true) 21 | index->var_name = ft_strjoin3(tmp, '=', export); 22 | else 23 | index->var_name = ft_strjoin(tmp, export); 24 | free(tmp); 25 | } 26 | 27 | void set_env_after_cd(t_env *list, char *key, char *value) 28 | { 29 | t_env *index; 30 | t_env *node; 31 | char *tmp; 32 | 33 | index = find_env_var_index(list, key); 34 | if (index && (ft_strcmp(key, "OLDPWD") == 0 || ft_strcmp(key, "PWD") == 0)) 35 | { 36 | free(index->var_name); 37 | index->var_name = ft_strjoin(key, "="); 38 | if (value) 39 | { 40 | tmp = index->var_name; 41 | index->var_name = ft_strjoin(index->var_name, value); 42 | free(tmp); 43 | } 44 | } 45 | else if (!index && (ft_strcmp(key, "PWD") == 0 || ft_strcmp(key, 46 | "OLDPWD") == 0)) 47 | { 48 | node = env_new(list, ft_strjoin3(key, '=', value)); 49 | list = env_last(list); 50 | list->next = node; 51 | } 52 | } 53 | 54 | void change_my_dir(t_env *list, char *path) 55 | { 56 | char *cur; 57 | char buffer[PATH_MAX]; 58 | 59 | cur = find_env_var(list, list, "PWD", false); 60 | if (path[0] == '\0') 61 | return ; 62 | if (chdir(path) != 0) 63 | perror("cd"); 64 | else 65 | { 66 | set_env_after_cd(list, "OLDPWD", cur); 67 | set_env_after_cd(list, "PWD", getcwd(buffer, PATH_MAX)); 68 | } 69 | } 70 | 71 | char *find_env_var(t_env *list, t_env *head, char *va, bool flag) 72 | { 73 | char **vale; 74 | 75 | while (list) 76 | { 77 | vale = ft_split(list->var_name, '='); 78 | if (ft_strcmp(vale[0], va) == 0) 79 | { 80 | free_array(vale); 81 | return (ft_strchr(list->var_name, '=') + 1); 82 | } 83 | free_array(vale); 84 | if (!list->next) 85 | break ; 86 | list = list->next; 87 | } 88 | while (head) 89 | { 90 | if (ft_strncmp(head->var_name, "path", 3) == 0) 91 | return (NULL); 92 | head = head->next; 93 | } 94 | if (ft_strncmp(va, "PATH", 3) == 0 && flag == true) 95 | return ("/usr/bin"); 96 | return (NULL); 97 | } 98 | 99 | int more_then_two_arg(char **com) 100 | { 101 | int i; 102 | int counter; 103 | 104 | i = 0; 105 | counter = 0; 106 | while (com[i]) 107 | { 108 | i++; 109 | counter++; 110 | } 111 | return (counter); 112 | } 113 | 114 | void my_cd(t_env *list, char **com) 115 | { 116 | char *myhome; 117 | int counter; 118 | 119 | counter = more_then_two_arg(com); 120 | if (counter > 2) 121 | { 122 | ft_putstr_fd("cd: too many arguments\n", 2); 123 | return ; 124 | } 125 | myhome = find_env_var(list, list, "HOME", false); 126 | if (com[1] == NULL || com[1][0] == '~') 127 | { 128 | if (!myhome) 129 | ft_putstr_fd("minishell: cd: HOME not set\n", 2); 130 | else 131 | change_my_dir(list, myhome); 132 | } 133 | else 134 | change_my_dir(list, com[1]); 135 | } 136 | -------------------------------------------------------------------------------- /src/parsing/parsing_init_tokens.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils1.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/04 19:12:43 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/07 10:12:08 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | static void token1(t_cmds *cmds, char c) 16 | { 17 | if (c == '<') 18 | { 19 | if (cmds->prev && cmds->prev->token == Non) 20 | cmds->prev->token = Cmd; 21 | if (cmds->next && cmds->next->token == Non) 22 | cmds->next->token = Infile; 23 | cmds->token = Input; 24 | } 25 | else if (c == '>') 26 | { 27 | if (cmds->prev && cmds->prev->token == Non) 28 | cmds->prev->token = Cmd; 29 | if (cmds->next && cmds->next->token == Non) 30 | cmds->next->token = OutFile; 31 | cmds->token = Output; 32 | } 33 | else if (c == '|') 34 | { 35 | if (cmds->prev && cmds->prev->token == Non) 36 | cmds->prev->token = Cmd; 37 | if (cmds->next && cmds->next->token == Non) 38 | cmds->next->token = Cmd; 39 | cmds->token = Pipe; 40 | } 41 | } 42 | 43 | static void token2(t_cmds *cmds, int i) 44 | { 45 | if (i == 1) 46 | { 47 | if (cmds->prev && cmds->prev->token == Non) 48 | cmds->prev->token = Cmd; 49 | if (cmds->next && cmds->next->token == Non) 50 | cmds->next->token = AppendFile; 51 | cmds->token = Append; 52 | } 53 | else if (i == 2) 54 | { 55 | if (cmds->prev && cmds->prev->token == Non) 56 | cmds->prev->token = Cmd; 57 | if (cmds->next && cmds->next->token == Non) 58 | cmds->next->token = HereDocDel; 59 | cmds->token = HereDoc; 60 | } 61 | } 62 | 63 | void init_tokens(t_cmds *cmds, int size, t_cmds *lst) 64 | { 65 | while (cmds) 66 | { 67 | size = ft_strlen(cmds->cmd[0]); 68 | if (size == 1 && cmds->cmd[0][0] == '<') 69 | token1(cmds, '<'); 70 | else if (size == 1 && cmds->cmd[0][0] == '>') 71 | token1(cmds, '>'); 72 | else if (size == 1 && cmds->cmd[0][0] == '|') 73 | token1(cmds, '|'); 74 | else if (size == 2 && cmds->cmd[0][0] == '>' && cmds->cmd[0][1] == '>') 75 | token2(cmds, 1); 76 | else if (size == 2 && cmds->cmd[0][0] == '<' && cmds->cmd[0][1] == '<') 77 | token2(cmds, 2); 78 | else if (!cmds->prev && !cmds->next) 79 | cmds->token = Cmd; 80 | else if (cmds->token == Non && cmds->prev 81 | && (cmds->prev->token == OutFile || cmds->prev->token == Infile 82 | || cmds->prev->token == Append 83 | || cmds->prev->token == HereDocDel)) 84 | cmds->token = Cmd; 85 | if (cmds) 86 | cmds = cmds->next; 87 | } 88 | non_token(lst); 89 | } 90 | 91 | void non_token(t_cmds *lst) 92 | { 93 | while (lst) 94 | { 95 | if (!lst->cmd[0]) 96 | lst->token = Non; 97 | if (lst->token == Non && lst->prev && lst->prev->token == OutFile) 98 | lst->token = Cmd; 99 | if (lst->token == Input || lst->token == Output || lst->token == Append 100 | || lst->token == HereDoc || lst->token == Pipe) 101 | lst->operation = Operation; 102 | else 103 | lst->operation = NonOperation; 104 | lst = lst->next; 105 | } 106 | } 107 | 108 | int is_spaces(char *str) 109 | { 110 | while (*str) 111 | { 112 | if (*str != ' ') 113 | return (1); 114 | str++; 115 | } 116 | return (0); 117 | } 118 | -------------------------------------------------------------------------------- /src/parsing/parsing_split_with_redirections_pipe.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parsing_utils3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2023/11/02 20:57:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 11:56:33 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void ft_free(char **ptr, int i) 16 | { 17 | int j; 18 | 19 | j = 0; 20 | if (ptr[i] != NULL) 21 | return ; 22 | while (j < i) 23 | free(ptr[j++]); 24 | free(ptr); 25 | } 26 | 27 | int inside(char const *s) 28 | { 29 | char c; 30 | int i; 31 | 32 | i = 0; 33 | while (*s) 34 | { 35 | if ((*s == 39 || *s == 34) && s[1]) 36 | { 37 | c = *s; 38 | while (++s && s && s[1] && *s) 39 | { 40 | if (*s == '|' || *s == '<' || *s == '>') 41 | { 42 | if ((*s == '<' && *(s + 1) == '<') || (*s == '>' && *(s 43 | + 1) == '>')) 44 | s++; 45 | i++; 46 | } 47 | if (*s == c) 48 | break ; 49 | } 50 | } 51 | s++; 52 | } 53 | return (i); 54 | } 55 | 56 | char const *get_position(char const *s) 57 | { 58 | char c; 59 | 60 | while (*s && (*s != '|' && *s != '<' && *s != '>')) 61 | { 62 | if ((*s == 39 || *s == 34) && s[1]) 63 | { 64 | c = *s; 65 | while (++s && s && s[1] && *s != c) 66 | { 67 | } 68 | } 69 | s++; 70 | } 71 | return (s); 72 | } 73 | 74 | static char **split(char const *s, int i, char **ptr, int k) 75 | { 76 | char const *start; 77 | 78 | while (*s) 79 | { 80 | k = 1; 81 | if ((*s != '|' && *s != '<' && *s != '>')) 82 | { 83 | start = s; 84 | s = get_position(s); 85 | ptr[i] = ndup(start, s - start); 86 | ft_free(ptr, i); 87 | } 88 | else 89 | { 90 | if ((*s == '<' && *(s + 1) == '<') || (*s == '>' && *(s 91 | + 1) == '>')) 92 | k++; 93 | ptr[i] = ndup(s, k); 94 | ft_free(ptr, i); 95 | s += k; 96 | } 97 | i++; 98 | } 99 | ptr[i] = NULL; 100 | return (ptr); 101 | } 102 | 103 | int count_words(char const *s, int count, int in_word) 104 | { 105 | if (s[0] == '\0') 106 | return (1); 107 | while (s && *s) 108 | { 109 | if (*s == '|' || *s == '<' || *s == '>') 110 | { 111 | if ((*s == '<' && *(s + 1) == '<') || (*s == '>' && *(s 112 | + 1) == '>')) 113 | s++; 114 | in_word = 0; 115 | count++; 116 | } 117 | else 118 | { 119 | if (in_word == 0 && (*s != '|' && *s != '<' && *s != '>')) 120 | { 121 | count++; 122 | in_word = 1; 123 | } 124 | } 125 | s++; 126 | } 127 | return (count); 128 | } 129 | 130 | char *ndup(const char *s, size_t n) 131 | { 132 | char *dup; 133 | size_t i; 134 | 135 | if (!s) 136 | return (NULL); 137 | dup = (char *)ft_malloc((n + 1) * sizeof(char), g_signal.node); 138 | if (dup != NULL) 139 | { 140 | i = 0; 141 | while (i < n) 142 | { 143 | dup[i] = s[i]; 144 | i++; 145 | } 146 | dup[n] = '\0'; 147 | } 148 | return (dup); 149 | } 150 | 151 | char **ft_split_msh(char const *s) 152 | { 153 | char **ptr; 154 | int word_count; 155 | 156 | if (s == NULL) 157 | return (NULL); 158 | word_count = count_words(s, 0, 0) - inside(s); 159 | ptr = ft_malloc((word_count + 1) * sizeof(char *), g_signal.node); 160 | if (ptr == NULL) 161 | { 162 | return (NULL); 163 | } 164 | ptr = split(s, 0, ptr, 1); 165 | return (ptr); 166 | } 167 | -------------------------------------------------------------------------------- /src/builtins/export.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* export.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 15:41:01 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void swap(char **s1, char **s2) 16 | { 17 | char *tmp; 18 | 19 | tmp = *s1; 20 | *s1 = *s2; 21 | *s2 = tmp; 22 | } 23 | 24 | char **sortexport(char **arr, int n) 25 | { 26 | int i; 27 | int j; 28 | 29 | i = 0; 30 | while (i < n - 1) 31 | { 32 | j = 0; 33 | while (j < n - i - 1) 34 | { 35 | if (ft_strcmp(arr[j], arr[j + 1]) > 0) 36 | swap(&arr[j], &arr[j + 1]); 37 | j++; 38 | } 39 | i++; 40 | } 41 | return (arr); 42 | } 43 | 44 | int printmyexport(t_env *list) 45 | { 46 | int i; 47 | char **ptr; 48 | 49 | i = 0; 50 | ptr = linked_list_to_array(list); 51 | while (ptr[i]) 52 | i++; 53 | sortexport(ptr, i); 54 | i = 0; 55 | while (ptr[i]) 56 | { 57 | if (ptr[i][0] == '_' && ptr[i][1] == '=') 58 | i++; 59 | else 60 | { 61 | printf("declare -x "); 62 | print_value(ptr[i]); 63 | i++; 64 | } 65 | } 66 | free_array(ptr); 67 | return (0); 68 | } 69 | 70 | int ft_all_isalpha(char *str) 71 | { 72 | int i; 73 | 74 | i = 0; 75 | while (str[i] && (ft_isalpha(str[i]) == 1 || str[i] == '_' 76 | || str[i] == '+' || str[i] == ' ')) 77 | { 78 | if (str[i] == ' ') 79 | str[i] = ' '; 80 | i++; 81 | } 82 | if ((str[i] == '=' && i != 0) || str[i] == '\0') 83 | return (0); 84 | return (1); 85 | } 86 | 87 | char *fill_var(char *var, char *c) 88 | { 89 | char *str; 90 | int j; 91 | 92 | *c = '+'; 93 | str = ft_malloc(sizeof(char) * ft_strlen(var), g_signal.node); 94 | j = -1; 95 | while (++j >= 0 && var[j] != '+') 96 | str[j] = var[j]; 97 | str[j] = '\0'; 98 | free(var); 99 | return (str); 100 | } 101 | 102 | void set_env_after_export(t_env *list, char **export, char c, 103 | bool export_flag) 104 | { 105 | t_env *index; 106 | 107 | index = find_env_var_index(list, export[0]); 108 | if (index) 109 | { 110 | if (c == '+') 111 | set_env_if_plus(index, export[1]); 112 | else 113 | { 114 | if (export[1] != NULL || export_flag == true) 115 | free(index->var_name); 116 | if (export[1] != NULL) 117 | index->var_name = ft_strjoin3(export[0], '=', export[1]); 118 | else if (export_flag == true) 119 | index->var_name = ft_strjoin3(export[0], '=', ""); 120 | } 121 | return ; 122 | } 123 | list = env_last(list); 124 | list->next = env_new(list, ft_strjoin3(export[0], '=', export[1])); 125 | } 126 | 127 | void export(t_env *list, char **com, char c, int i) 128 | { 129 | char **export; 130 | bool export_flag; 131 | 132 | if (com[1] == NULL && printmyexport(list) == 0) 133 | return ; 134 | while (com[i]) 135 | { 136 | c = '-'; 137 | export_flag = false; 138 | if (ft_all_isalpha(com[i]) == 1) 139 | printf("export: '%s' :not a valid identifier\n", com[i]); 140 | else 141 | { 142 | if (com[i][ft_strlen(com[i]) - 1] == '=') 143 | export_flag = true; 144 | export = get_key_and_value(com[i], NULL, 0, 0); 145 | if (export[0][ft_strlen(export[0]) - 1] == '+') 146 | export[0] = fill_var(export[0], &c); 147 | set_env_after_export(list, export, c, export_flag); 148 | free_array(export); 149 | } 150 | i++; 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/executing/executing.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* executing.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/05/20 11:03:16 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 11:50:43 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | bool change_underscore(t_data *data, t_command *command, char *str, int i) 16 | { 17 | t_env *index; 18 | 19 | if (!command->cmd || !command->cmd[0]) 20 | return (false); 21 | while (command->cmd[i]) 22 | i++; 23 | str = command->cmd[--i]; 24 | if (!str || *str == '\n') 25 | return (false); 26 | index = find_env_var_index(data->list_env, "_"); 27 | if (index) 28 | { 29 | free(index->var_name); 30 | index->var_name = ft_strjoin("_=", str); 31 | } 32 | else 33 | { 34 | index = env_new(data->list_env, ft_strjoin("_=", str)); 35 | data->list_env = env_last(data->list_env); 36 | if (data->list_env) 37 | data->list_env->next = index; 38 | else 39 | data->list_env = index; 40 | } 41 | return (true); 42 | } 43 | 44 | int run_builtins(int c, t_command *command, t_data *data, int flag) 45 | { 46 | t_env *list; 47 | 48 | list = data->list_env; 49 | if (c == 1) 50 | my_cd(list, command->cmd); 51 | else if (c == 2) 52 | my_pwd(list); 53 | else if (c == 3) 54 | printmyenv(list); 55 | else if (c == 4) 56 | export(list, command->cmd, '-', 1); 57 | else if (c == 5) 58 | data->list_env = unset_env(list, command->cmd, data); 59 | else if (c == 6) 60 | exit_myminishell(command->cmd, flag); 61 | else if (c == 7) 62 | ft_echo(command->cmd + 1, true, 0); 63 | return (0); 64 | } 65 | 66 | void only_builtins(t_data *data, t_command *list, int builtins) 67 | { 68 | int in; 69 | int out; 70 | 71 | in = dup(STDIN_FILENO); 72 | out = dup(STDOUT_FILENO); 73 | if (list->infile || list->outfile) 74 | g_signal.ret = hand_the_redirectionin(list); 75 | if (g_signal.ret != 1) 76 | run_builtins(builtins, list, data, 0); 77 | if ((g_signal.ret != 1) && list->infile) 78 | dup2(in, STDIN_FILENO); 79 | else 80 | ft_close(in, "in"); 81 | if ((g_signal.ret != 1) && list->outfile) 82 | dup2(out, STDOUT_FILENO); 83 | else 84 | ft_close(out, "out"); 85 | } 86 | 87 | void with_pipe(t_data *data, t_command *list) 88 | { 89 | data->k = 0; 90 | data->pid = ft_malloc(sizeof(int) * (get_command_size(list)), g_signal.node); 91 | data->fd_in = STDIN_FILENO; 92 | while (list) 93 | { 94 | if (list->next) 95 | { 96 | if (pipe(data->fd) == -1) 97 | break ; 98 | } 99 | g_signal.ret = execute_command(data->list_env, list, data, 100 | data->k++); 101 | if (list->next) 102 | ft_close(data->fd[1], "data->fd[0]"); 103 | if (list->prev) 104 | ft_close(data->fd_in, "data->fd[1]"); 105 | if (list->next) 106 | data->fd_in = data->fd[0]; 107 | list = list->next; 108 | } 109 | if (g_signal.ret == 0 && data->k != 0) 110 | g_signal.ret = wait_pid(data->pid, data->k); 111 | free(data->pid); 112 | data->pid = NULL; 113 | } 114 | 115 | int executing(t_data *data) 116 | { 117 | t_command *list; 118 | int builtins; 119 | bool flag; 120 | 121 | list = data->list; 122 | flag = false; 123 | builtins = get_command_in_one_char(list->cmd); 124 | if (!list || (list->cmd && list->cmd[0][0] == '\n')) 125 | return (2); 126 | if (!list->next) 127 | flag = change_underscore(data, list, NULL, 0); 128 | if (builtins != 0 && !list->next) 129 | only_builtins(data, list, builtins); 130 | else 131 | with_pipe(data, list); 132 | if (flag == true) 133 | change_underscore(data, list, NULL, 0); 134 | g_signal.ret_exit = g_signal.ret; 135 | return (g_signal.ret); 136 | } 137 | -------------------------------------------------------------------------------- /src/lists/linked_list_handling_functions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* lst.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/01/06 11:09:54 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 09:54:14 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void commands_clear(t_command **lst) 16 | { 17 | t_command *curr1; 18 | t_command *curr2; 19 | 20 | if (lst == NULL || *lst == NULL) 21 | return ; 22 | curr1 = *lst; 23 | while (curr1->next != NULL) 24 | { 25 | curr2 = curr1->next; 26 | free_array(curr1->cmd); 27 | slist_clear(&curr1->infile); 28 | slist_clear(&curr1->outfile); 29 | free(curr1); 30 | curr1 = curr2; 31 | } 32 | free_array(curr1->cmd); 33 | slist_clear(&curr1->infile); 34 | slist_clear(&curr1->outfile); 35 | free(curr1); 36 | *lst = NULL; 37 | } 38 | 39 | void lstclear(t_cmds **lst) 40 | { 41 | t_cmds *curr1; 42 | t_cmds *curr2; 43 | 44 | if (lst == NULL || *lst == NULL) 45 | return ; 46 | curr1 = *lst; 47 | while (curr1->next != NULL) 48 | { 49 | curr2 = curr1->next; 50 | free_array(curr1->cmd); 51 | free(curr1); 52 | curr1 = curr2; 53 | } 54 | free_array(curr1->cmd); 55 | free(curr1); 56 | *lst = NULL; 57 | } 58 | 59 | t_cmds *lstlast(t_cmds *lst) 60 | { 61 | if (lst == NULL) 62 | return (lst); 63 | while (lst->next) 64 | { 65 | lst = lst->next; 66 | } 67 | return (lst); 68 | } 69 | 70 | t_command *command_last(t_command *lst) 71 | { 72 | if (lst == NULL) 73 | return (lst); 74 | while (lst->next) 75 | { 76 | lst = lst->next; 77 | } 78 | return (lst); 79 | } 80 | 81 | t_slist *nodes_last(t_slist *lst) 82 | { 83 | if (lst == NULL) 84 | return (lst); 85 | while (lst->next) 86 | { 87 | lst = lst->next; 88 | } 89 | return (lst); 90 | } 91 | 92 | t_cmds *lstnew(char *cmd, t_cmds *lst, char **str) 93 | { 94 | t_cmds *n_node; 95 | t_cmds *last_node; 96 | 97 | n_node = (t_cmds *)ft_malloc(sizeof(struct s_cmds), g_signal.node); 98 | if (n_node == NULL) 99 | return (NULL); 100 | if (cmd) 101 | n_node->cmd = ft_split(cmd, ' '); 102 | else 103 | n_node->cmd = str; 104 | n_node->token = Non; 105 | n_node->next = NULL; 106 | if (lst == NULL) 107 | { 108 | n_node->prev = NULL; 109 | } 110 | else 111 | { 112 | last_node = lstlast(lst); 113 | n_node->prev = last_node; 114 | } 115 | return (n_node); 116 | } 117 | 118 | void slist_clear(t_slist **lst) 119 | { 120 | t_slist *curr1; 121 | t_slist *curr2; 122 | 123 | if (lst == NULL || *lst == NULL) 124 | return ; 125 | curr1 = *lst; 126 | while (curr1->next != NULL) 127 | { 128 | curr2 = curr1->next; 129 | free(curr1); 130 | curr1 = curr2; 131 | } 132 | free(curr1); 133 | *lst = NULL; 134 | } 135 | 136 | void lst_env_clear(t_env **lst) 137 | { 138 | t_env *curr1; 139 | t_env *curr2; 140 | 141 | if (lst == NULL || *lst == NULL) 142 | return ; 143 | curr1 = *lst; 144 | while (curr1->next != NULL) 145 | { 146 | curr2 = curr1->next; 147 | free(curr1->var_name); 148 | free(curr1); 149 | curr1 = curr2; 150 | } 151 | free(curr1->var_name); 152 | free(curr1); 153 | *lst = NULL; 154 | } 155 | 156 | t_command *command_new(t_command *lst) 157 | { 158 | t_command *n_node; 159 | t_command *last_node; 160 | 161 | n_node = (t_command *)ft_malloc(sizeof(struct s_command), g_signal.node); 162 | if (n_node == NULL) 163 | return (NULL); 164 | n_node->cmd = NULL; 165 | n_node->infile = NULL; 166 | n_node->outfile = NULL; 167 | n_node->next = NULL; 168 | if (lst == NULL) 169 | { 170 | n_node->prev = NULL; 171 | } 172 | else 173 | { 174 | last_node = command_last(lst); 175 | n_node->prev = last_node; 176 | } 177 | return (n_node); 178 | } 179 | -------------------------------------------------------------------------------- /src/executing/execute_command.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* utils3.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:38:01 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 14:34:11 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | int get_command_in_one_char(char **str) 16 | { 17 | int c; 18 | 19 | c = 0; 20 | if (!str || !*str) 21 | return (c); 22 | if (ft_strcmp(str[0], "cd") == 0) 23 | c = 1; 24 | else if (ft_strcmp(str[0], "pwd") == 0) 25 | c = 2; 26 | else if (ft_strcmp(str[0], "env") == 0 && str[1] == NULL) 27 | c = 3; 28 | else if (ft_strcmp(str[0], "export") == 0) 29 | c = 4; 30 | else if (ft_strcmp(str[0], "unset") == 0) 31 | c = 5; 32 | else if (ft_strcmp(str[0], "exit") == 0) 33 | c = 6; 34 | else if (ft_strcmp(str[0], "echo") == 0) 35 | c = 7; 36 | return (c); 37 | } 38 | 39 | void execute_command_part_one(char **com, t_command *command, t_data *data, 40 | char *path) 41 | { 42 | int red; 43 | 44 | red = 0; 45 | signal(SIGINT, SIG_DFL); 46 | free(data->line); 47 | if (command->infile || command->outfile) 48 | red = hand_the_redirectionin(command); 49 | if (red == 1) 50 | { 51 | lst_env_clear(&data->list_env); 52 | free_array(data->env); 53 | ft_exit(1); 54 | } 55 | else if (path == NULL && get_command_in_one_char(com) == 0) 56 | { 57 | ft_putstr_fd("minishell: ", 2); 58 | if (((com[0][0] == 39 && com[0][1] == 39) || (com[0][0] == 34 59 | && com[0][1] == 34)) && com[0][2] == '\0') 60 | ft_putstr_fd("'': command not found\n", 2); 61 | else 62 | ft_puterror_fd(com[0], ": command not found\n"); 63 | lst_env_clear(&data->list_env); 64 | free_array(data->env); 65 | ft_exit(127); 66 | } 67 | } 68 | 69 | void execute_command_part_two(t_command *command, t_data *data) 70 | { 71 | if (command->prev && !command->infile) 72 | { 73 | dup2(data->fd_in, STDIN_FILENO); 74 | ft_close(data->fd_in, "fd_in\n"); 75 | } 76 | if (command->next && !command->outfile) 77 | dup2(data->fd[1], STDOUT_FILENO); 78 | if (command->next) 79 | { 80 | ft_close(data->fd[1], "fd[0]\n"); 81 | ft_close(data->fd[0], "fd[1]\n"); 82 | } 83 | } 84 | 85 | void execute_command_part_three(char **com, t_command *command, t_data *data, 86 | char *path) 87 | { 88 | struct stat stats; 89 | int cmd; 90 | 91 | cmd = get_command_in_one_char(com); 92 | if (cmd == 0) 93 | { 94 | if (signal(SIGQUIT, print_signals_c) != SIG_ERR) 95 | g_signal.ret = 131; 96 | execve(path, com, data->env); 97 | ft_putstr_fd("minishell: ", 2); 98 | ft_putstr_fd(com[0], 2); 99 | ft_putstr_fd(": ", 2); 100 | stat(path, &stats); 101 | if (S_ISDIR(stats.st_mode)) 102 | ft_putendle("Is a directory", 2); 103 | else 104 | ft_putendle(strerror(errno), 2); 105 | lst_env_clear(&data->list_env); 106 | free_array(data->env); 107 | ft_exit(-1); 108 | } 109 | else 110 | run_builtins(cmd, command, data, 1); 111 | } 112 | 113 | int execute_command(t_env *list, t_command *command, t_data *data, int index) 114 | { 115 | char *path; 116 | 117 | if (!command->cmd) 118 | return (-1); 119 | if (command->cmd[0][0] == '\0' && !command->infile && !command->outfile) 120 | return (0); 121 | path = get_my_path(list, command->cmd, data->path_flag, 0); 122 | signal(SIGINT, SIG_IGN); 123 | data->pid[index] = ft_fork(); 124 | if (data->pid[index] == 0) 125 | { 126 | execute_command_part_one(command->cmd, command, data, path); 127 | execute_command_part_two(command, data); 128 | if (command->cmd[0][0] != '\0') 129 | execute_command_part_three(command->cmd, command, data, path); 130 | lst_env_clear(&data->list_env); 131 | free_array(data->env); 132 | ft_exit(0); 133 | } 134 | free(path); 135 | if (data->pid[index] < 0) 136 | return (-1); 137 | return (0); 138 | } 139 | -------------------------------------------------------------------------------- /src/builtins/unset_exit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* unset_exit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/06/02 18:41:13 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/12 15:51:48 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | void ft_exit(int e) 16 | { 17 | deleteList(g_signal.node); 18 | exit(e); 19 | } 20 | 21 | int is_exitnumeric(char *str) 22 | { 23 | while (*str) 24 | { 25 | if (ft_isdigit(*str) == 0 && *str != '-' && *str != '+') 26 | return (1); 27 | str++; 28 | } 29 | return (0); 30 | } 31 | 32 | void exit_from_child(char **com) 33 | { 34 | int i; 35 | int size; 36 | 37 | size = ft_strlen(com[1]); 38 | if (com[1] == NULL) 39 | ft_exit(g_signal.ret_exit); 40 | else if (is_exitnumeric(com[1]) != 0 || size > 19 || (size == 19 && com[1][18] > '7')) 41 | { 42 | printf("minishell: "); 43 | printf(com[1], 2); 44 | printf(": numeric argument required\n"); 45 | ft_exit(2); 46 | } 47 | else if (com[1] && com[2]) 48 | ft_putstr_fd("minishell: exit: too many arguments\n", 2); 49 | else 50 | { 51 | i = ft_atoi(com[1]); 52 | g_signal.ret = i; 53 | ft_exit((int)i); 54 | } 55 | } 56 | 57 | void exit_myminishell(char **com, int flag) 58 | { 59 | int i; 60 | int size; 61 | 62 | size = ft_strlen(com[1]); 63 | if (flag != 0) 64 | exit_from_child(com); 65 | else if (com[1] == NULL) 66 | { 67 | printf("exit\n"); 68 | ft_exit(g_signal.ret_exit); 69 | } 70 | else if (is_exitnumeric(com[1]) != 0 || size > 19 || (size == 19 && com[1][18] > '7')) 71 | { 72 | printf("minishell: "); 73 | printf(com[1], 2); 74 | printf(": numeric argument required\n"); 75 | ft_exit(2); 76 | } 77 | else if (com[1] && com[2]) 78 | printf("minishell: exit: too many arguments\n"); 79 | else 80 | { 81 | i = ft_atoi(com[1]); 82 | printf("exit\n"); 83 | g_signal.ret = i; 84 | ft_exit((int)i); 85 | } 86 | } 87 | 88 | char *get_my_path(t_env *list, char **com, bool flag, int i) 89 | { 90 | char **str; 91 | char *path1; 92 | char *mypath; 93 | char *command_path; 94 | 95 | mypath = NULL; 96 | if (com[0][0] == '/' || com[0][0] == '.') 97 | return (ft_strdup(com[0])); 98 | path1 = find_env_var(list, list, "PATH", flag); 99 | if (!path1) 100 | return (NULL); 101 | str = ft_split(path1, ':'); 102 | while (str[i]) 103 | { 104 | command_path = ft_strjoin3(str[i], '/', com[0]); 105 | if (access(command_path, X_OK) == 0) 106 | { 107 | mypath = command_path; 108 | break ; 109 | } 110 | free(command_path); 111 | i++; 112 | } 113 | free_array(str); 114 | return (mypath); 115 | } 116 | 117 | char **get_vars(char *cmd) 118 | { 119 | char **var; 120 | int k; 121 | int j; 122 | 123 | var = malloc(sizeof(char *) * how_many_dollar_in(cmd)); 124 | k = 0; 125 | j = 0; 126 | while (cmd[j]) 127 | { 128 | if (cmd[j] == 39 && check_ex(cmd, j) == true) 129 | { 130 | j++; 131 | while (cmd[j] && cmd[j] != 39) 132 | j++; 133 | } 134 | if (cmd[j] == '$' && cmd[j + 1] && cmd[j + 1] != '$') 135 | var[k++] = grep_variable_name(cmd + j, 0, 0, 0); 136 | else if (cmd[j] == '$' && cmd[j + 1] == '$') 137 | j++; 138 | if (cmd[j]) 139 | j++; 140 | } 141 | var[k] = NULL; 142 | return (var); 143 | } 144 | 145 | char **get_key_and_value(char *str, char **ptr, int i, int j) 146 | { 147 | while (str[i]) 148 | { 149 | if (str[i] == '=') 150 | j++; 151 | i++; 152 | } 153 | if (j == 0) 154 | return (get_name(str)); 155 | else 156 | ptr = malloc(sizeof(char *) * 3); 157 | i = 0; 158 | while (str[i] && str[i] != '=') 159 | i++; 160 | ptr[0] = malloc(sizeof(char ) * (i + 1)); 161 | j = -1; 162 | while (++j < i) 163 | ptr[0][j] = str[j]; 164 | ptr[0][j] = '\0'; 165 | i++; 166 | ptr[1] = ft_strdup(str + i); 167 | ptr[2] = NULL; 168 | return (ptr); 169 | } 170 | -------------------------------------------------------------------------------- /include/minishell.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minishell.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: ksohail- +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2024/01/31 16:38:08 by ksohail- #+# #+# */ 9 | /* Updated: 2024/07/13 11:50:09 by ksohail- ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINISHELL_H 14 | # define MINISHELL_H 15 | 16 | # include "libft.h" 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | # include 26 | # include 27 | # include 28 | # include 29 | # include 30 | # include 31 | 32 | typedef struct s_data t_data; 33 | 34 | typedef struct s_signal 35 | { 36 | int ret; 37 | int ret_exit; 38 | int sig; 39 | int ff; 40 | Node *node; 41 | } t_signal; 42 | 43 | extern t_signal g_signal; 44 | 45 | typedef struct s_env 46 | { 47 | char *var_name; 48 | 49 | struct s_env *next; 50 | struct s_env *prev; 51 | } t_env; 52 | 53 | typedef enum s_token 54 | { 55 | Cmd, 56 | AppendFile, 57 | HereDocDel, 58 | Infile, 59 | OutFile, 60 | Operation, 61 | NonOperation, 62 | Input, 63 | Output, 64 | Append, 65 | HereDoc, 66 | Pipe, 67 | Non 68 | } t_token; 69 | 70 | typedef struct s_cmds 71 | { 72 | char **cmd; 73 | 74 | t_token token; 75 | t_token operation; 76 | t_data *data; 77 | 78 | struct s_cmds *next; 79 | struct s_cmds *prev; 80 | } t_cmds; 81 | 82 | typedef struct s_slist 83 | { 84 | char *cmd; 85 | 86 | t_token token; 87 | 88 | struct s_slist *next; 89 | struct s_slist *prev; 90 | } t_slist; 91 | 92 | typedef struct s_command 93 | { 94 | char **cmd; 95 | 96 | t_slist *infile; 97 | t_slist *outfile; 98 | 99 | struct s_command *next; 100 | struct s_command *prev; 101 | } t_command; 102 | 103 | struct s_data 104 | { 105 | int fd[2]; 106 | int *pid; 107 | int k; 108 | int fd_in; 109 | char **env; 110 | char **cmds; 111 | char *line; 112 | 113 | bool path_flag; 114 | bool flag; 115 | 116 | t_env *list_env; 117 | t_cmds *lst; 118 | t_command *list; 119 | }; 120 | 121 | typedef struct s_line 122 | { 123 | char *line; 124 | int i; 125 | int k; 126 | int pos; 127 | int size; 128 | } t_line; 129 | 130 | 131 | t_cmds *lstlast(t_cmds *lst); 132 | t_slist *get_head(t_slist *list); 133 | void fill_in_commands(t_cmds **cl, t_command **command, t_slist **infile, t_slist **outfile); 134 | void lstclear(t_cmds **lst); 135 | t_cmds *lstnew(char *cmd, t_cmds *stack, char **str); 136 | void signal_handler(void); 137 | char *ft_strjoin3(char const *s1, char c, char const *s2); 138 | void free_array(char **array); 139 | char *rm_spaces(char *str); 140 | void get_list(char **cmd, int size, t_cmds **lst, 141 | t_data *data); 142 | void init_tokens(t_cmds *cmds, int size, t_cmds *lst); 143 | void parsing(t_data *data, t_cmds *lst, t_command *commands, 144 | int i); 145 | int errors_managment(t_data *data, int flag); 146 | int check_for_pipe(t_cmds *cmds); 147 | int cmdcheck(char *str); 148 | int error_msg_v1(char *str); 149 | void print_signals_c(int signal); 150 | int error_msg_v2(char *str); 151 | int is_spaces(char *str); 152 | void non_token(t_cmds *lst); 153 | int check_for_in_out_put(t_cmds *cmds); 154 | int check_for_append_heredoc(t_cmds *cmds); 155 | int error_msg(char *str); 156 | int check_quotation(char *str); 157 | int count_words(char const *s, int count, int in_word); 158 | char *ndup(const char *s, size_t n); 159 | void remove_quotes(t_cmds *lst); 160 | int dollar_is_in(char *str); 161 | char *grep_variable_name(char *line, int i, int j, int k); 162 | char **ft_split_str(char *s1); 163 | char *expand_variable(char *str, t_data *data); 164 | char **get_vars(char *cmd); 165 | char *get_final_line(char **lines, char **vars, char *cmd, 166 | t_line *line_data); 167 | int dollar_is_in(char *str); 168 | int count_vars(char *s1); 169 | bool check_ex(char *str, int size); 170 | char *check_expand(char *str, t_data *data); 171 | char const *get_position(char const *s); 172 | size_t get_size(char *str); 173 | char *get_string(char *str, size_t i, size_t k, size_t size); 174 | int run_builtins(int c, t_command *command, t_data *data, 175 | int flag); 176 | int hand_the_redirectionin(t_command *lst); 177 | void ft_handle_sigint(int sig); 178 | t_slist *nodes_last(t_slist *lst); 179 | t_command *command_last(t_command *lst); 180 | t_command *command_new(t_command *lst); 181 | t_command *get_command(t_cmds *lst); 182 | t_command *get_commands(t_cmds *lst); 183 | void commands_clear(t_command **lst); 184 | void ft_clear(t_data *data); 185 | void print_array(char **str); 186 | int ft_fork(void); 187 | void ft_close(int fd, char *str); 188 | int executing(t_data *data); 189 | void my_cd(t_env *list, char **com); 190 | void ft_echo(char **com, bool flag, int i); 191 | void exit_error(void); 192 | bool check_n_flag(char *str); 193 | t_env *env_new(t_env *lst, char *str); 194 | t_env *env_last(t_env *lst); 195 | char **linked_list_to_array(t_env *list); 196 | char **sort_export(char **arr, int n); 197 | void lst_env_clear(t_env **lst); 198 | char **linked_list_to_array(t_env *list); 199 | t_env *unset_env(t_env *list, char **com, t_data *data); 200 | void exit_myminishell(char **com, int flag); 201 | char *get_my_path(t_env *list, char **com, bool flag, int i); 202 | void export(t_env *list, char **com, char c, int i); 203 | int printmyexport(t_env *list); 204 | void exit_error(void); 205 | t_env *copieenv(char **env); 206 | void ft_putendle(char *str, int fd); 207 | void printmyenv(t_env *list); 208 | t_env *find_env_var_index(t_env *list, char *va); 209 | void change_my_dir(t_env *list, char *path); 210 | int more_then_two_arg(char **com); 211 | void my_pwd(t_env *env); 212 | int array_size(char **var); 213 | void swap(char **s1, char **s2); 214 | int count_nodes(t_env *list); 215 | int get_cmd_size(t_cmds *list); 216 | void get_command_done(t_cmds *list, t_cmds *head, 217 | char **command, bool flag); 218 | char **creat_myenv(void); 219 | int check_quot(char *str); 220 | int open_heredoc(t_cmds *cmds, int pid, int status, 221 | bool flag); 222 | int ft_strcmp_for_heredoc(char *s1, char *s2); 223 | int get_command_size(t_command *command); 224 | int wait_pid(int *pid, int cmd_num); 225 | int execute_command(t_env *list, t_command *command, 226 | t_data *data, int index); 227 | int get_command_in_one_char(char **str); 228 | char *get_content(char **env, char *str); 229 | bool check_back_for_heredoc(char *str, int index); 230 | void slist_clear(t_slist **lst); 231 | char **get_file_name(char *str); 232 | bool check_next(char *str); 233 | char **get_name(char *str); 234 | int is_numeric(char *str); 235 | int get_2d_size(char **vars, char **lines); 236 | bool check_eq(char *str); 237 | void set_env_if_plus(t_env *index, char *export); 238 | int how_many_dollar_in(char *str); 239 | bool check_next_for_both(char *str); 240 | int check_double(char *cmd, int i); 241 | void set_env_after_export(t_env *list, char **export, char c, 242 | bool export_flag); 243 | void set_env_after_cd(t_env *list, char *key, char *value); 244 | char *find_env_var(t_env *list, t_env *head, char *va, 245 | bool flag); 246 | char **array_copy(char **str); 247 | t_cmds *copy_node(char **cmd, t_token token, bool flag); 248 | t_cmds *copy_single_node(t_cmds *curr, int *i); 249 | t_cmds *add_head_to_new_list(t_cmds *head, t_cmds *new_head); 250 | t_cmds *copy_list(t_cmds *curr, char **command, int i); 251 | t_cmds *merge_lists(t_cmds *list1, t_cmds *list2); 252 | char **get_key_and_value(char *str, char **ptr, int i, 253 | int j); 254 | void ft_puterror_fd(char *str1, char *str2); 255 | void print_value(char *str); 256 | char *increment_s1(char *s1); 257 | bool is_it_inside(char *str); 258 | 259 | #endif --------------------------------------------------------------------------------