├── author ├── libft ├── .gitignore ├── ft_putnbr.c ├── ft_putchar.c ├── ft_isascii.c ├── ft_isprint.c ├── ft_putendl.c ├── ft_putstr.c ├── ft_isdigit.c ├── ft_islower.c ├── ft_isupper.c ├── ft_putchar_fd.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_strclr.c ├── ft_putstr_fd.c ├── ft_strdup.c ├── ft_lstadd.c ├── ft_tolower.c ├── ft_toupper.c ├── ft_memdel.c ├── ft_strdel.c ├── ft_strlen.c ├── ft_strequ.c ├── ft_strpop.c ├── ft_strstartswith.c ├── ft_striter.c ├── ft_putendl_fd.c ├── ft_strnequ.c ├── ft_lstdel.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_memalloc.c ├── ft_striteri.c ├── ft_bzero.c ├── ft_strndup.c ├── ft_memset.c ├── ft_strnchr.c ├── ft_strcpy.c ├── ft_strjoinchcl.c ├── ft_memcpy.c ├── ft_strnew.c ├── ft_freestrarr.c ├── ft_strchr.c ├── ft_strrchr.c ├── ft_strendswith.c ├── ft_strcmp.c ├── ft_lstaddback.c ├── ft_strcat.c ├── ft_memchr.c ├── ft_strncat.c ├── ft_strncpy.c ├── ft_strjoincl.c ├── ft_putnstr.c ├── ft_strncmp.c ├── ft_isemptystr.c ├── ft_strmap.c ├── ft_lst_reverse.c ├── ft_strsub.c ├── ft_strmapi.c ├── ft_countwords.c ├── ft_memccpy.c ├── ft_realloc.c ├── ft_countwordsall.c ├── ft_memcmp.c ├── ft_capitalize.c ├── ft_memmove.c ├── ft_putnbr_fd.c ├── ft_strjoinch.c ├── ft_lstmap.c ├── ft_copyuntil.c ├── ft_pathjoin.c ├── ft_strlcat.c ├── ft_lstnew.c ├── ft_strstr.c ├── ft_atoi.c ├── ft_strjoin.c ├── ft_itoa.c ├── ft_intlen.c ├── ft_strnstr.c ├── ft_strreplace.c ├── Makefile ├── ft_strtrim.c ├── ft_strsplit.c ├── ft_strsplitall.c ├── get_next_line.c └── includes │ └── libft.h ├── norme.en.pdf ├── minishell.en.pdf ├── minishell-quick-demo.gif ├── .gitignore ├── LICENSE ├── src ├── signal_handler.c ├── echo_builtin.c ├── display_prompt_msg.c ├── unsetenv_builtin.c ├── cd_builtin.c ├── setenv_builtin.c ├── main.c └── exec_command.c ├── Makefile ├── inc └── minishell.h └── README.md /author: -------------------------------------------------------------------------------- 1 | jrameau 2 | -------------------------------------------------------------------------------- /libft/.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /norme.en.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/minishell/HEAD/norme.en.pdf -------------------------------------------------------------------------------- /minishell.en.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/minishell/HEAD/minishell.en.pdf -------------------------------------------------------------------------------- /minishell-quick-demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nickdotht/minishell/HEAD/minishell-quick-demo.gif -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | *.su 34 | 35 | *DS_Store 36 | minishell 37 | *.nfs 38 | launch.json 39 | *test* -------------------------------------------------------------------------------- /libft/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:10:39 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:10:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | ft_putnbr_fd(n, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:45:18 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 21:44:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | ft_putchar_fd(c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:10:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:02 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | return (c >= 0 && c <= 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 21:21:53 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:40:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | return (c >= 32 && c < 127); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putendl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:04:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:04:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl(char const *s) 16 | { 17 | ft_putendl_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/08/17 17:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/08/17 17:47:34 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char const *s) 16 | { 17 | ft_putstr_fd(s, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 17:00:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 23:43:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | return (c <= '9' && c >= '0'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_islower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_islower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:45:32 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:45:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_islower(int c) 16 | { 17 | return (c <= 'z' && c >= 'a'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:42:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:42:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isupper(int c) 16 | { 17 | return (c <= 'Z' && c >= 'A'); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 17:49:11 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 17:49:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 20:54:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 20:54:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | return (ft_isalpha(c) || ft_isdigit(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 12:39:45 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 12:39:46 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | return (ft_islower(c) || ft_isupper(c)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strclr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strclr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:07:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:07:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strclr(char *s) 16 | { 17 | if (s) 18 | ft_bzero(s, ft_strlen(s)); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:23:42 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/15 18:36:20 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | write(fd, s, ft_strlen(s)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:52:14 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:52:15 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | return (ft_strndup(s1, ft_strlen(s1))); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_lstadd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/19 22:48:33 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:48:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd(t_list **alst, t_list *new) 16 | { 17 | new->next = *alst; 18 | *alst = new; 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 23:44:33 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:24:45 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | if (ft_isupper(c)) 18 | return (c + 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 22:57:01 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | if (ft_islower(c)) 18 | return (c - 32); 19 | return (c); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 20:09:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 20:09:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_memdel(void **ap) 16 | { 17 | if (!ap || !*ap) 18 | return ; 19 | free(*ap); 20 | *ap = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:03:47 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:03:48 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strdel(char **as) 16 | { 17 | if (!as || !*as) 18 | return ; 19 | free(*as); 20 | *as = 0; 21 | } 22 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:43:37 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 12:43:39 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (*(s + ++i)) 21 | ; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:42:03 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:42:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strequ(char const *s1, char const *s2) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strcmp(s1, s2) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_strpop.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strpop.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/12 17:01:31 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:11:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strpop(char *str) 16 | { 17 | char *new; 18 | 19 | new = ft_strndup(str, ft_strlen(str) - 1); 20 | free(str); 21 | return (new); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_strstartswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstartswith.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/14 17:33:27 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:52:24 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int ft_strstartswith(char *s1, char *s2) 14 | { 15 | int i; 16 | 17 | i = -1; 18 | while (s2[++i]) 19 | if (s1[i] != s2[i]) 20 | return (0); 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_striter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:12:58 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:12:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striter(char *s, void (*f)(char *)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s && f) 21 | while (*(s + i)) 22 | f(s + i++); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:26:34 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 22:37:43 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char const *s, int fd) 16 | { 17 | char *tmp; 18 | 19 | tmp = ft_strjoin(s, "\n"); 20 | ft_putstr_fd(tmp, fd); 21 | free(tmp); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_strnequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:47:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:47:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strnequ(char const *s1, char const *s2, size_t n) 16 | { 17 | if (!s1 || !s2) 18 | return (0); 19 | return (ft_strncmp(s1, s2, n) ? 0 : 1); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:58:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:58:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | if ((*alst)->next) 18 | ft_lstdel(&(*alst)->next, del); 19 | ft_lstdelone(&(*alst), del); 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 23:17:34 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 23:17:35 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | del((*alst)->content, (*alst)->content_size); 18 | free(*alst); 19 | *alst = NULL; 20 | } 21 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:16:06 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:16:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) 16 | { 17 | if (!lst) 18 | return ; 19 | if (lst->next) 20 | ft_lstiter(lst->next, f); 21 | f(lst); 22 | } 23 | -------------------------------------------------------------------------------- /libft/ft_memalloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memalloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 03:37:39 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 21:44:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memalloc(size_t size) 16 | { 17 | void *mem; 18 | 19 | if (!(mem = malloc(size))) 20 | return (NULL); 21 | ft_bzero(mem, size); 22 | return (mem); 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Nick Rameau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:22:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:22:15 by jrameau ### ########.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 = -1; 20 | if (s && f) 21 | while (*(s + ++i)) 22 | f(i, s + i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 15:52:51 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 15:52:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | if (!n) 21 | return ; 22 | ptr = s; 23 | i = 0; 24 | while (i < n) 25 | *(ptr + i++) = 0; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strndup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strndup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:20:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:20:23 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strndup(const char *s1, size_t n) 16 | { 17 | char *tmp; 18 | 19 | if (!(tmp = ft_strnew(n))) 20 | return (NULL); 21 | ft_strncpy(tmp, s1, n); 22 | return (tmp); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 13:13:13 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 13:13:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | ptr = b; 21 | i = 0; 22 | while (i < len) 23 | *(ptr + i++) = c; 24 | return (b); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strnchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:38:48 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:39:55 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnchr(char *s, char c, int offset) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s[++i]) 21 | if (s[i] == c) 22 | return (s + i + offset); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 15:25:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 02:49:18 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcpy(char *dst, const char *src) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (*(src + ++i)) 21 | *(dst + i) = *(src + i); 22 | *(dst + i) = '\0'; 23 | return (dst); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strjoinchcl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoinchcl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 19:30:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/19 20:57:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinchcl(char *s1, char c) 16 | { 17 | char *new; 18 | 19 | if (!(new = ft_strjoinch(s1, c))) 20 | return (NULL); 21 | free(s1); 22 | s1 = NULL; 23 | return (new); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 16:54:04 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/08 11:58:56 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < n) 21 | ((unsigned char *)dst)[i] = ((unsigned char *)src)[i]; 22 | return (dst); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_strnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 22:53:09 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 22:53:10 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnew(size_t size) 16 | { 17 | char *str; 18 | 19 | str = (char *)malloc(sizeof(char) * size + 1); 20 | if (!str) 21 | return (NULL); 22 | ft_bzero(str, size + 1); 23 | return (str); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_freestrarr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_freestrarr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 16:00:12 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/18 16:36:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_freestrarr(char **arr) 16 | { 17 | int i; 18 | 19 | if (!arr) 20 | return ; 21 | i = -1; 22 | while (arr[++i]) 23 | { 24 | free(arr[i]); 25 | } 26 | free(arr); 27 | arr = NULL; 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 00:41:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 00:41:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (++i < (int)ft_strlen(s) + 1) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:25:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:25:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | int i; 18 | 19 | i = (int)ft_strlen(s) + 1; 20 | while (i--) 21 | if (*(s + i) == (char)c) 22 | return ((char *)s + i); 23 | return (NULL); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_strendswith.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strendswith.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/01/17 20:56:49 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:54:50 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strendswith(char *s1, char *s2) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | while (s1[++i]) 21 | if (s1[i] == s2[0]) 22 | if (ft_strcmp(s1 + i, s2) == 0) 23 | return (1); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:51:36 by jrameau #+# #+# */ 9 | /* Updated: 2017/02/21 15:53:57 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i)) 21 | i++; 22 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 23 | } 24 | -------------------------------------------------------------------------------- /libft/ft_lstaddback.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstaddback.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:01:01 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:49:40 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstaddback(t_list **alst, t_list *new) 16 | { 17 | t_list *tmp; 18 | t_list *head; 19 | 20 | tmp = *alst; 21 | head = tmp; 22 | while (tmp->next) 23 | tmp = tmp->next; 24 | tmp->next = new; 25 | *alst = head; 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:07:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 18:07:44 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcat(char *s1, const char *s2) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i)) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 13:54:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:55:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | const char *sc; 18 | size_t i; 19 | 20 | sc = (const char *)s; 21 | i = -1; 22 | while (++i < n) 23 | if (*(sc + i) == (char)c) 24 | return ((void *)sc + i); 25 | return (NULL); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 18:58:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:16:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncat(char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | int j; 19 | 20 | i = -1; 21 | j = (int)ft_strlen(s1); 22 | while (*(s2 + ++i) && i < (int)n) 23 | *(s1 + j++) = *(s2 + i); 24 | *(s1 + j) = '\0'; 25 | return (s1); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_strncpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/24 02:48:43 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 03:25:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncpy(char *dst, const char *src, size_t len) 16 | { 17 | size_t i; 18 | 19 | i = -1; 20 | while (++i < len) 21 | if (*(src + i)) 22 | *(dst + i) = *(src + i); 23 | else 24 | while (i < len) 25 | *(dst + i++) = '\0'; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strjoincl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoincl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/18 19:30:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/19 10:59:24 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoincl(char *s1, char *s2, int free_both) 16 | { 17 | char *new; 18 | 19 | if (!(new = ft_strjoin(s1, s2))) 20 | return (NULL); 21 | free(s1); 22 | s1 = NULL; 23 | if (free_both) 24 | { 25 | free(s2); 26 | s2 = NULL; 27 | } 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_putnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/29 13:50:34 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:10:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnstr(char *str, int n) 16 | { 17 | int i; 18 | 19 | i = -1; 20 | if (n < 0) 21 | { 22 | while (str[++i] && i < (int)ft_strlen(str) + n) 23 | ft_putchar(str[i]); 24 | } 25 | else 26 | { 27 | while (str[++i] && i < n) 28 | ft_putchar(str[i]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:22:10 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:22:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*(s1 + i) && *(s1 + i) == *(s2 + i) && i < (int)n - 1) 21 | i++; 22 | if (n) 23 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); 24 | return (0); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_isemptystr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isemptystr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/11 00:03:25 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:09:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isemptystr(char *str, int consider_space) 16 | { 17 | int i; 18 | int min; 19 | int max; 20 | 21 | i = -1; 22 | min = 32 + consider_space; 23 | max = 126; 24 | while (str[++i]) 25 | { 26 | if (str[i] >= min && str[i] <= max) 27 | return (0); 28 | } 29 | return (1); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_strmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:27:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:27:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmap(char const *s, char (*f)(char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(*(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_lst_reverse.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_reverse.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/15 16:09:31 by jrameau #+# #+# */ 9 | /* Updated: 2016/10/15 16:09:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lst_reverse(t_list *alst) 16 | { 17 | t_list *prev; 18 | t_list *cur; 19 | t_list *next; 20 | 21 | prev = NULL; 22 | cur = alst; 23 | while (cur != NULL) 24 | { 25 | next = cur->next; 26 | cur->next = prev; 27 | prev = cur; 28 | cur = next; 29 | } 30 | alst = prev; 31 | return (alst); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:50:07 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:50:08 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strsub(char const *s, unsigned int start, size_t len) 16 | { 17 | char *new_str; 18 | size_t i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(len); 23 | if (!new_str) 24 | return (NULL); 25 | i = 0; 26 | while (i < len) 27 | *(new_str + i++) = *(s + start++); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/26 23:39:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/26 23:39:43 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | char *new_str; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new_str = ft_strnew(ft_strlen(s)); 23 | if (!new_str) 24 | return (NULL); 25 | i = -1; 26 | while (*(s + ++i)) 27 | *(new_str + i) = f(i, *(s + i)); 28 | return (new_str); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_countwords.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_countwords.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 15:17:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 15:17:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_countwords(char const *str, char c) 16 | { 17 | int count; 18 | int i; 19 | 20 | i = 0; 21 | count = 0; 22 | while (str[i]) 23 | { 24 | while (str[i] == c) 25 | i++; 26 | if (str[i] != c && str[i] != '\0') 27 | count++; 28 | while (str[i] != c && str[i] != '\0') 29 | i++; 30 | } 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:34:36 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/21 21:34:37 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | char *ptr; 18 | size_t i; 19 | 20 | i = -1; 21 | ptr = dst; 22 | while (++i < n) 23 | { 24 | *(ptr + i) = *((unsigned char *)src + i); 25 | if (*((unsigned char *)src + i) == (unsigned char)c) 26 | return (dst + i + 1); 27 | } 28 | return (NULL); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_realloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_realloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/10/21 01:11:53 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 21:46:33 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_realloc(void *ptr, size_t prev_size, size_t new_size) 16 | { 17 | void *new; 18 | 19 | if (!ptr) 20 | return (NULL); 21 | if (!(new = ft_memalloc(new_size))) 22 | { 23 | free(ptr); 24 | return (NULL); 25 | } 26 | ft_memcpy(new, ptr, prev_size < new_size ? prev_size : new_size); 27 | free(ptr); 28 | return (new); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_countwordsall.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_countwordsall.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 15:17:40 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/11 00:23:19 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_countwordsall(char const *str) 16 | { 17 | int count; 18 | int i; 19 | 20 | i = 0; 21 | count = 0; 22 | while (str[i]) 23 | { 24 | while (IS_SPACE(str[i])) 25 | i++; 26 | if (!IS_SPACE(str[i]) && str[i] != '\0') 27 | count++; 28 | while (!IS_SPACE(str[i]) && str[i] != '\0') 29 | i++; 30 | } 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 14:18:12 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/22 14:18:14 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *s1c; 18 | unsigned char *s2c; 19 | size_t i; 20 | 21 | i = -1; 22 | s1c = (unsigned char *)s1; 23 | s2c = (unsigned char *)s2; 24 | while (++i < n && *(s1c + i) == *(s2c + i)) 25 | ; 26 | if (i == n) 27 | return (0); 28 | return (*(s1c + i) - *(s2c + i)); 29 | } 30 | -------------------------------------------------------------------------------- /libft/ft_capitalize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_capitalize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 15:51:24 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 15:51:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_capitalize(char *s) 16 | { 17 | char *new; 18 | int i; 19 | 20 | if (!s) 21 | return (NULL); 22 | new = ft_strnew(ft_strlen(s)); 23 | new[0] = ft_toupper(s[0]); 24 | i = 0; 25 | while (*(s + ++i)) 26 | if (!ft_isalnum(s[i - 1]) && ft_isalnum(s[i])) 27 | new[i] = ft_toupper(s[i]); 28 | else 29 | new[i] = s[i]; 30 | return (new); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 12:41:41 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 17:15:13 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | char *srcc; 18 | char *dstc; 19 | size_t i; 20 | 21 | i = -1; 22 | srcc = (char *)src; 23 | dstc = (char *)dst; 24 | if (srcc < dstc) 25 | while ((int)(--len) >= 0) 26 | *(dstc + len) = *(srcc + len); 27 | else 28 | while (++i < len) 29 | *(dstc + i) = *(srcc + i); 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n < 0) 18 | { 19 | ft_putchar_fd('-', fd); 20 | n = -n; 21 | } 22 | if (n == -2147483648) 23 | { 24 | ft_putchar_fd('2', fd); 25 | n %= 1000000000; 26 | n = -n; 27 | } 28 | if (n >= 10) 29 | { 30 | ft_putnbr_fd(n / 10, fd); 31 | ft_putnbr_fd(n % 10, fd); 32 | } 33 | else 34 | ft_putchar_fd(n + '0', fd); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strjoinch.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:08:21 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoinch(char const *s1, char c) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t s1_len; 20 | 21 | if (!s1 || !c) 22 | return (NULL); 23 | s1_len = ft_strlen(s1); 24 | new_str = ft_strnew(s1_len + 1); 25 | if (!new_str) 26 | return (NULL); 27 | i = -1; 28 | while (++i < s1_len) 29 | *(new_str + i) = *(s1 + i); 30 | *(new_str + i) = c; 31 | return (new_str); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 00:20:40 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/29 00:20:41 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) 16 | { 17 | t_list *new; 18 | t_list *list; 19 | 20 | if (!lst) 21 | return (NULL); 22 | list = f(lst); 23 | new = list; 24 | while (lst->next) 25 | { 26 | lst = lst->next; 27 | if (!(list->next = f(lst))) 28 | { 29 | free(list->next); 30 | return (NULL); 31 | } 32 | list = list->next; 33 | } 34 | return (new); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_copyuntil.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* copyuntil.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/11/26 05:49:22 by jrameau #+# #+# */ 9 | /* Updated: 2016/11/26 05:52:26 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_copyuntil(char **dst, char *src, char c) 16 | { 17 | int i; 18 | int count; 19 | int pos; 20 | 21 | i = -1; 22 | count = 0; 23 | while (src[++i]) 24 | if (src[i] == c) 25 | break ; 26 | pos = i; 27 | if (!(*dst = ft_strnew(i))) 28 | return (0); 29 | while (src[count] && count < i) 30 | { 31 | if (!(*dst = ft_strjoinch(*dst, src[count]))) 32 | return (0); 33 | count++; 34 | } 35 | return (pos); 36 | } 37 | -------------------------------------------------------------------------------- /libft/ft_pathjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_pathjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/19 22:50:00 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 00:39:12 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_pathjoin(char *p1, char *p2) 16 | { 17 | char *tmp2; 18 | 19 | if (!p2 || !p1) 20 | return (NULL); 21 | if (!ft_strendswith(p1, "/")) 22 | { 23 | if (p2[0] == '/') 24 | return (ft_strjoin(p1, p2)); 25 | else 26 | { 27 | tmp2 = ft_strjoincl(ft_strjoinch(p1, '/'), p2, 0); 28 | return (tmp2); 29 | } 30 | } 31 | else 32 | { 33 | if (p2[0] == '/') 34 | return (ft_strjoin(p1, p2 + 1)); 35 | else 36 | return (ft_strjoin(p1, p2)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/22 19:27:25 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/24 22:41:08 by jrameau ### ########.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 | int j; 19 | size_t dst_len; 20 | size_t src_len; 21 | 22 | i = ft_strlen(dst); 23 | j = 0; 24 | dst_len = ft_strlen(dst); 25 | src_len = ft_strlen(src); 26 | if (size < dst_len + 1) 27 | return (src_len + size); 28 | if (size > dst_len + 1) 29 | { 30 | while (i < size - 1) 31 | *(dst + i++) = *(src + j++); 32 | *(dst + i) = '\0'; 33 | } 34 | return (dst_len + src_len); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 22:49:15 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 22:49:16 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void const *content, size_t content_size) 16 | { 17 | t_list *list; 18 | 19 | if (!(list = (t_list *)malloc(sizeof(*list)))) 20 | return (NULL); 21 | if (!content) 22 | { 23 | list->content = NULL; 24 | list->content_size = 0; 25 | } 26 | else 27 | { 28 | if (!(list->content = malloc(content_size))) 29 | return (NULL); 30 | ft_memcpy(list->content, content, content_size); 31 | list->content_size = content_size; 32 | } 33 | list->next = NULL; 34 | return (list); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 01:39:09 by jrameau #+# #+# */ 9 | /* Updated: 2017/01/17 22:39:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strstr(const char *big, const char *little) 16 | { 17 | int i; 18 | int j; 19 | int k; 20 | int good; 21 | 22 | if (!ft_strlen(little)) 23 | return ((char *)big); 24 | i = -1; 25 | good = 0; 26 | while (*(big + ++i) && !good) 27 | { 28 | if (*(big + i) == *(little + 0)) 29 | { 30 | j = 0; 31 | k = i; 32 | good = 1; 33 | while (*(little + j)) 34 | if (*(little + j++) != *(big + k++)) 35 | good = 0; 36 | if (good) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 04:34:26 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/23 04:34:27 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | int num; 19 | int sign; 20 | 21 | i = 0; 22 | num = 0; 23 | sign = 1; 24 | while (*(str + i) == '\n' || 25 | *(str + i) == '\t' || 26 | *(str + i) == '\r' || 27 | *(str + i) == '\v' || 28 | *(str + i) == '\f' || 29 | *(str + i) == ' ') 30 | i++; 31 | if (*(str + i) == '-') 32 | sign = -1; 33 | if (*(str + i) == '-' || *(str + i) == '+') 34 | i++; 35 | while (*(str + i) && *(str + i) >= '0' && *(str + i) <= '9') 36 | num = num * 10 + (*(str + i++) - '0'); 37 | return (num * sign); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:08:20 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/18 20:52:57 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strjoin(char const *s1, char const *s2) 16 | { 17 | char *new_str; 18 | size_t i; 19 | size_t j; 20 | size_t s1_len; 21 | size_t s2_len; 22 | 23 | if (!s1) 24 | return (NULL); 25 | if (!s2) 26 | return (ft_strdup(s1)); 27 | s1_len = ft_strlen(s1); 28 | s2_len = ft_strlen(s2); 29 | new_str = ft_strnew(s1_len + s2_len + 1); 30 | if (!new_str) 31 | return (NULL); 32 | i = -1; 33 | j = -1; 34 | while (++i < s1_len) 35 | *(new_str + i) = *(s1 + i); 36 | while (++j < s2_len) 37 | *(new_str + i++) = *(s2 + j); 38 | return (new_str); 39 | } 40 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/28 02:04:57 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/28 02:04:59 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static size_t get_str_len(int n) 16 | { 17 | size_t i; 18 | 19 | i = 1; 20 | while (n /= 10) 21 | i++; 22 | return (i); 23 | } 24 | 25 | char *ft_itoa(int n) 26 | { 27 | char *str; 28 | size_t str_len; 29 | unsigned int n_cpy; 30 | 31 | str_len = get_str_len(n); 32 | n_cpy = n; 33 | if (n < 0) 34 | { 35 | n_cpy = -n; 36 | str_len++; 37 | } 38 | if (!(str = ft_strnew(str_len))) 39 | return (NULL); 40 | str[--str_len] = n_cpy % 10 + '0'; 41 | while (n_cpy /= 10) 42 | str[--str_len] = n_cpy % 10 + '0'; 43 | if (n < 0) 44 | *(str + 0) = '-'; 45 | return (str); 46 | } 47 | -------------------------------------------------------------------------------- /libft/ft_intlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_intlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/12/22 13:09:38 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/19 22:47:25 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int higher_nums(int num) 14 | { 15 | if (num >= 10000000) 16 | { 17 | if (num >= 1000000000) 18 | return (10); 19 | if (num >= 100000000) 20 | return (9); 21 | return (8); 22 | } 23 | if (num >= 1000000) 24 | return (7); 25 | return (6); 26 | } 27 | 28 | int ft_intlen(int num) 29 | { 30 | if (num >= 100000) 31 | return (higher_nums(num)); 32 | else 33 | { 34 | if (num >= 1000) 35 | { 36 | if (num >= 10000) 37 | return (5); 38 | return (4); 39 | } 40 | else 41 | { 42 | if (num >= 100) 43 | return (3); 44 | if (num >= 10) 45 | return (2); 46 | } 47 | return (1); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/23 03:19:21 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/25 02:15:51 by jrameau ### ########.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 i; 18 | size_t j; 19 | size_t k; 20 | int found; 21 | 22 | i = -1; 23 | found = 1; 24 | if (!ft_strlen(little)) 25 | return ((char *)big); 26 | while (*(big + ++i) && i < len) 27 | { 28 | j = 0; 29 | if (*(big + i) == *(little + 0)) 30 | { 31 | k = i; 32 | found = 1; 33 | while (*(big + k) && *(little + j) && j < len && k < len) 34 | if (*(big + k++) != *(little + j++)) 35 | found = 0; 36 | if (found && !*(little + j)) 37 | return ((char *)big + i); 38 | } 39 | } 40 | return (NULL); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_strreplace.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strreplace.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/09 20:14:03 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:15:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strreplace(char *str, char *term, char *replace_by) 16 | { 17 | int i; 18 | char *new_path; 19 | int done; 20 | 21 | if (!ft_strstr(str, term)) 22 | return (NULL); 23 | new_path = ft_strnew(1); 24 | i = -1; 25 | done = 0; 26 | while (str[++i]) 27 | { 28 | if (ft_strstartswith(str + i, term) && !done) 29 | { 30 | new_path = ft_strjoincl(new_path, replace_by, 0); 31 | i += ft_strlen(term); 32 | if (!str[i]) 33 | break ; 34 | new_path = ft_strjoinchcl(new_path, str[i]); 35 | done = 1; 36 | } 37 | else 38 | new_path = ft_strjoinchcl(new_path, str[i]); 39 | } 40 | return (new_path); 41 | } 42 | -------------------------------------------------------------------------------- /src/signal_handler.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* signal_handler.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/11 00:50:10 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 20:38:32 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Handles interruption signals on a running process 17 | ** 18 | ** @param signo The signal number 19 | ** @return N/A 20 | */ 21 | 22 | void proc_signal_handler(int signo) 23 | { 24 | if (signo == SIGINT) 25 | { 26 | ft_putstr("\n"); 27 | signal(SIGINT, proc_signal_handler); 28 | } 29 | } 30 | 31 | /* 32 | ** Handles interruption signals on the program 33 | ** 34 | ** @param signo The signal number 35 | ** @return N/A 36 | */ 37 | 38 | void signal_handler(int signo) 39 | { 40 | if (signo == SIGINT) 41 | { 42 | ft_putstr("\n"); 43 | display_prompt_msg(); 44 | signal(SIGINT, signal_handler); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/09/21 14:58:27 by jrameau #+# #+# # 9 | # Updated: 2017/05/21 14:58:14 by jrameau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | CFLAGS = -Wall -Werror -Wextra -Iincludes -c 15 | FILES = $(shell ls | grep -E ".+\.c") 16 | OBJ = $(FILES:%.c=%.o) 17 | 18 | all: $(NAME) 19 | 20 | # This won't run if the .o files don't exist or are not modified 21 | $(NAME): $(OBJ) 22 | @ar rcs $(NAME) $(OBJ) 23 | 24 | # This won't run if the source files don't exist or are not modified 25 | $(OBJ): $(FILES) 26 | @gcc $(CFLAGS) $(FILES) 27 | 28 | clean: 29 | @rm -f $(OBJ) 30 | 31 | fclean: clean 32 | @rm -f $(NAME) 33 | 34 | re: fclean all 35 | 36 | # I use .PHONY to make sure that gnu make will still run even if files called 37 | # clean / fclean / all and re already exist in the directory 38 | .PHONY: clean fclean all re 39 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 00:50:46 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 00:50:47 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int has_whitespaces(char *str, int *i, size_t *j) 16 | { 17 | while (IS_SPACE(*(str + *i))) 18 | (*i)++; 19 | while (IS_SPACE(*(str + *j))) 20 | (*j)--; 21 | if (*i || *j < ft_strlen(str)) 22 | return (1); 23 | return (0); 24 | } 25 | 26 | char *ft_strtrim(char const *s) 27 | { 28 | int i; 29 | size_t j; 30 | int k; 31 | char *new_str; 32 | size_t new_size; 33 | 34 | if (!s) 35 | return (NULL); 36 | i = 0; 37 | k = 0; 38 | j = ft_strlen(s) - 1; 39 | if (!has_whitespaces((char *)s, &i, &j) || !ft_strlen(s)) 40 | return ((char *)s); 41 | new_size = (i == (int)ft_strlen(s)) ? 0 : ft_strlen(s) - (size_t)i - \ 42 | (ft_strlen(s) - j); 43 | new_str = ft_strnew(new_size + 1); 44 | if (!new_str) 45 | return (NULL); 46 | while (i <= (int)j) 47 | *(new_str + k++) = *(s + i++); 48 | return (new_str); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_strsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 13:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2016/09/27 13:18:36 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int get_word_len(char const *str, char c) 16 | { 17 | int i; 18 | int len; 19 | 20 | i = 0; 21 | len = 0; 22 | while (str[i] == c) 23 | i++; 24 | while (str[i] != c && str[i] != '\0') 25 | { 26 | i++; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char **ft_strsplit(char const *s, char c) 33 | { 34 | int i; 35 | int j; 36 | int k; 37 | char **str2; 38 | 39 | if (!s || !(str2 = (char **)malloc(sizeof(*str2) * 40 | (ft_countwords(s, c) + 1)))) 41 | return (NULL); 42 | i = -1; 43 | j = 0; 44 | while (++i < ft_countwords(s, c)) 45 | { 46 | k = 0; 47 | if (!(str2[i] = ft_strnew(get_word_len(&s[j], c) + 1))) 48 | str2[i] = NULL; 49 | while (s[j] == c) 50 | j++; 51 | while (s[j] != c && s[j]) 52 | str2[i][k++] = s[j++]; 53 | str2[i][k] = '\0'; 54 | } 55 | str2[i] = 0; 56 | return (str2); 57 | } 58 | -------------------------------------------------------------------------------- /libft/ft_strsplitall.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplitall.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/27 13:18:35 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/11 00:24:28 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int get_word_len(char const *str) 16 | { 17 | int i; 18 | int len; 19 | 20 | i = 0; 21 | len = 0; 22 | while (IS_SPACE(str[i])) 23 | i++; 24 | while (!IS_SPACE(str[i]) && str[i] != '\0') 25 | { 26 | i++; 27 | len++; 28 | } 29 | return (len); 30 | } 31 | 32 | char **ft_strsplitall(char const *s) 33 | { 34 | int i; 35 | int j; 36 | int k; 37 | char **str2; 38 | 39 | if (!s || !(str2 = (char **)malloc(sizeof(*str2) * 40 | (ft_countwordsall(s) + 1)))) 41 | return (NULL); 42 | i = -1; 43 | j = 0; 44 | while (++i < ft_countwordsall(s)) 45 | { 46 | k = 0; 47 | if (!(str2[i] = ft_strnew(get_word_len(&s[j]) + 1))) 48 | str2[i] = NULL; 49 | while (IS_SPACE(s[j])) 50 | j++; 51 | while (!IS_SPACE(s[j]) && s[j]) 52 | str2[i][k++] = s[j++]; 53 | str2[i][k] = '\0'; 54 | } 55 | str2[i] = 0; 56 | return (str2); 57 | } 58 | -------------------------------------------------------------------------------- /libft/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/29 22:52:30 by jrameau #+# #+# */ 9 | /* Updated: 2017/04/24 19:07:49 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static t_list *get_correct_file(t_list **file, int fd) 16 | { 17 | t_list *tmp; 18 | 19 | tmp = *file; 20 | while (tmp) 21 | { 22 | if ((int)tmp->content_size == fd) 23 | return (tmp); 24 | tmp = tmp->next; 25 | } 26 | tmp = ft_lstnew("\0", fd); 27 | ft_lstadd(file, tmp); 28 | tmp = *file; 29 | return (tmp); 30 | } 31 | 32 | int get_next_line(const int fd, char **line) 33 | { 34 | char buf[BUFF_SIZE + 1]; 35 | static t_list *file; 36 | int i; 37 | int ret; 38 | t_list *curr; 39 | 40 | if ((fd < 0 || line == NULL || read(fd, buf, 0) < 0)) 41 | return (-1); 42 | curr = get_correct_file(&file, fd); 43 | MALLCHECK((*line = ft_strnew(1))); 44 | while ((ret = read(fd, buf, BUFF_SIZE))) 45 | { 46 | buf[ret] = '\0'; 47 | MALLCHECK((curr->content = ft_strjoin(curr->content, buf))); 48 | if (ft_strchr(buf, '\n')) 49 | break ; 50 | } 51 | if (ret < BUFF_SIZE && !ft_strlen(curr->content)) 52 | return (0); 53 | i = ft_copyuntil(line, curr->content, '\n'); 54 | (i < (int)ft_strlen(curr->content)) 55 | ? curr->content += (i + 1) 56 | : ft_strclr(curr->content); 57 | return (1); 58 | } 59 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: jrameau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2016/12/13 11:43:23 by jrameau #+# #+# # 9 | # Updated: 2017/05/21 14:58:05 by jrameau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | # Project file 14 | NAME = minishell 15 | 16 | # Project builds and dirs 17 | SRCDIR = ./src/ 18 | SRCNAMES = $(shell ls $(SRCDIR) | grep -E ".+\.c") 19 | SRC = $(addprefix $(SRCDIR), $(SRCNAMES)) 20 | INC = ./inc/ 21 | BUILDDIR = ./build/ 22 | BUILDOBJS = $(addprefix $(BUILDDIR), $(SRCNAMES:.c=.o)) 23 | 24 | # Libft builds and dirs 25 | LIBDIR = ./libft/ 26 | LIBFT = ./libft/libft.a 27 | LIBINC = ./libft/includes/ 28 | 29 | # Optimization and Compiler flags and commands 30 | CC = gcc 31 | CFLAGS = -Wall -Werror -Wextra 32 | 33 | # Debugging flags 34 | DEBUG = -g 35 | 36 | # Main rule 37 | all: $(BUILDDIR) $(LIBFT) $(NAME) 38 | 39 | # Object dir rule 40 | $(BUILDDIR): 41 | mkdir -p $(BUILDDIR) 42 | 43 | # Objects rule 44 | $(BUILDDIR)%.o:$(SRCDIR)%.c 45 | $(CC) $(CFLAGS) -I$(LIBINC) -I$(INC) -o $@ -c $< 46 | 47 | # Project file rule 48 | $(NAME): $(BUILDOBJS) 49 | $(CC) $(CFLAGS) -o $(NAME) $(BUILDOBJS) $(LIBFT) 50 | 51 | # Libft rule 52 | $(LIBFT): 53 | make -C $(LIBDIR) 54 | 55 | # Cleaning up the build files 56 | clean: 57 | rm -rf $(BUILDDIR) 58 | make -C $(LIBDIR) clean 59 | 60 | # Getting rid of the project file 61 | fclean: clean 62 | rm -rf $(NAME) 63 | make -C $(LIBDIR) fclean 64 | 65 | # Do both of the above 66 | re: fclean all 67 | 68 | # Just in case those files exist in the root dir 69 | .PHONY: all fclean clean re 70 | -------------------------------------------------------------------------------- /src/echo_builtin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* echo_builtin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/07 15:41:15 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 20:29:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Prints out a string on the screen 17 | ** 18 | ** @param str The string to print 19 | ** @param pos The position at which to start printing 20 | ** @return N/A 21 | */ 22 | 23 | static void echo_out(char **str, int pos) 24 | { 25 | int starts_with; 26 | int ends_with; 27 | int str_len; 28 | 29 | starts_with = IS_QUOTE(str[pos][0]); 30 | str_len = (int)ft_strlen(str[pos]); 31 | ends_with = IS_QUOTE(str[pos][str_len - 1]); 32 | if (ends_with && starts_with) 33 | ft_putnstr(str[pos] + 1, -1); 34 | else if (ends_with) 35 | ft_putnstr(str[pos], -1); 36 | else if (starts_with) 37 | ft_putstr(str[pos] + 1); 38 | else 39 | ft_putstr(str[pos]); 40 | if (str[pos + 1]) 41 | ft_putchar(' '); 42 | } 43 | 44 | /* 45 | ** Executes the builtin echo command 46 | ** 47 | ** @param args The arguments to pass to echo 48 | ** @return 1 on completion 49 | */ 50 | 51 | int echo_builtin(char **args) 52 | { 53 | int i; 54 | int n_flag; 55 | 56 | n_flag = 0; 57 | if (!args[0]) 58 | { 59 | write(1, "\n", 1); 60 | return (1); 61 | } 62 | else if (args[0][0] == '-' && args[0][1] == 'n' && args[0][2] == '\0') 63 | n_flag = 1; 64 | i = -1; 65 | if (n_flag) 66 | ++i; 67 | while (args[++i]) 68 | { 69 | echo_out(args, i); 70 | if (!args[i + 1] && !n_flag) 71 | ft_putchar('\n'); 72 | } 73 | return (1); 74 | } 75 | -------------------------------------------------------------------------------- /inc/minishell.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* minishell.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/24 17:40:04 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 15:04:50 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef MINISHELL_H 14 | # define MINISHELL_H 15 | # include 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | # include "libft.h" 23 | 24 | # define IS_QUOTE(x) (x == '"' || x == '\'') 25 | 26 | char **g_envv; 27 | 28 | /* 29 | ** src/cd_builtin.c 30 | */ 31 | void change_dir(char *path, int print_path); 32 | int cd_builtin(char **command); 33 | 34 | /* 35 | ** src/display_prompt_msg.c 36 | */ 37 | void exit_shell(void); 38 | char *parse_home_path(char *path, int reverse_parse); 39 | void display_prompt_msg(void); 40 | 41 | /* 42 | ** src/echo_builtin.c 43 | */ 44 | int echo_builtin(char **command); 45 | 46 | /* 47 | ** src/exec_command.c 48 | */ 49 | int exec_command(char **command); 50 | 51 | /* 52 | ** src/setenv_builtin.c 53 | */ 54 | int find_env_var(char *var); 55 | char *get_env_var(char *var); 56 | char **realloc_envv(int new_size); 57 | void set_env_var(char *key, char *value); 58 | int setenv_builtin(char **command); 59 | int setenv_builtin(char **args); 60 | 61 | /* 62 | ** src/signal_handler.c 63 | */ 64 | void signal_handler(int signo); 65 | void proc_signal_handler(int signo); 66 | 67 | /* 68 | ** src/unsetenv_builtin.c 69 | */ 70 | void print_env(void); 71 | void init_envv(int ac, char **av, char **envv); 72 | int unsetenv_builtin(char **command); 73 | #endif 74 | -------------------------------------------------------------------------------- /src/display_prompt_msg.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* display_prompt_msg.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/26 04:49:25 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:04:42 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Kills the shell gracefully 17 | ** 18 | ** @param N/A 19 | ** @return N/A 20 | */ 21 | 22 | void exit_shell(void) 23 | { 24 | ft_freestrarr(g_envv); 25 | write(1, "\n", 1); 26 | exit(0); 27 | } 28 | 29 | /* 30 | ** Parses a path string from ~ to the home path or from the home path to ~ 31 | ** depending on reverse_parse and returns the parsed string 32 | ** 33 | ** @param path the string to parse 34 | ** @param reverse_parse 0 to parse from home path to ~, 1 to parse from ~ 35 | ** to home path 36 | ** @return NULL if path doesn't exist or the parsed string (new) or a copy of 37 | ** path if path is not a fit 38 | */ 39 | 40 | char *parse_home_path(char *path, int reverse_parse) 41 | { 42 | char *home_path; 43 | char *new; 44 | 45 | if (!path) 46 | return (NULL); 47 | home_path = get_env_var("HOME"); 48 | if (!ft_strstartswith(path, reverse_parse ? "~" : home_path)) 49 | return (ft_strdup(path)); 50 | if (reverse_parse) 51 | new = ft_pathjoin(home_path, path + 1); 52 | else 53 | { 54 | if (*(path + ft_strlen(home_path)) == '\0') 55 | new = ft_strdup("~"); 56 | else 57 | new = ft_pathjoin("~", path + ft_strlen(home_path)); 58 | } 59 | return (new); 60 | } 61 | 62 | /* 63 | ** Displays a prompt message on the screen 64 | ** 65 | ** @param N/A 66 | ** @return N/A 67 | */ 68 | 69 | void display_prompt_msg(void) 70 | { 71 | char *cwd; 72 | char buff[4096 + 1]; 73 | char *parsed_cwd; 74 | 75 | cwd = getcwd(buff, 4096); 76 | parsed_cwd = parse_home_path(cwd, 0); 77 | ft_putstr(parsed_cwd); 78 | free(parsed_cwd); 79 | ft_putstr(" \033[31m︻\033[0m\033[32m┳\033[0m\033[33mデ"); 80 | ft_putstr("\033[0m\033[34m═\033[0m\033[35m—\033[0m$ "); 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minishell - @42Born2Code 2 | 3 | As beautiful as a shell 4 | 5 | ![Quick Demo][quick-demo] 6 | 7 | ## About 8 | 9 | Minishell is the second project of the Unix branch at 42. 10 | It's a minimum viable version of a real shell. 11 | The main goal is to have a good understanding of process creation and 12 | synchronisation using the C programmming language. 13 | 14 | ## Installation & Usage 15 | 16 | ### Requirements 17 | The only requirements are: 18 | - GNU make (v3.81) 19 | - GCC (v4.2.1) 20 | 21 | Those versions are the ones used during development. 22 | 23 | ### Building the program 24 | 25 | 1. Download/Clone this repo 26 | 27 | git clone https://github.com/r4meau/minishell 28 | 2. `cd` into the root directory and run `make` 29 | 30 | cd minishell 31 | make 32 | 33 | ### Running the program 34 | 35 | After building the source, run `./minishell` from the project root. 36 | 37 | ## Main Project Instructions 38 | 39 | ### Mandatory 40 | 41 | - Can only use C 42 | - Must respect the school imposed coding style ([The Norme][norme-pdf]) 43 | - No memory leaks 44 | - Implement a series of builtins: `echo`, `cd`, `setenv`, `unsetenv`, `env`, `exit` 45 | - Manage the errors without using `errno`, by displaying a message adapted 46 | to the error output 47 | - Can only use these standard library functions: 48 | - malloc, free 49 | - access 50 | - open, close, read, write 51 | - opendir, readdir, closedir 52 | - getcwd, chdir 53 | - stat, lstat, fstat 54 | - fork, execve 55 | - wait, waitpid, wait3, wait4 56 | - signal, kill 57 | - exit 58 | - Must have a Makefile to build the program 59 | - The binary file must be named `minishell` 60 | - Can use [Libft][libft-url] 61 | - Handle program interruption (Ctrl + D) 62 | - [Click here][1] for the rest 63 | 64 | ### Bonuses 65 | 66 | - Signal management (specifically Ctrl + C) 67 | - PATH's right management (error handling) 68 | - Multiple commands (semi colons) 69 | 70 | 71 | ## Notes 72 | 73 | - You can find the project instructions by [clicking here][1] 74 | - I commented the whole codebase so it's easy to navigate and understand it. 75 | - No need to mention the odd spacing in files, I'm using a tab size of 4. 76 | - There are probably better ways to implement it, we are limited by a set of functions at my school (just to make it harder and give us a deeper understanding of what's happening in the back), please, review the [project instructions][1] before you explain how `X` or `Y` would have been a better way to do it ;) 77 | 78 | ## Sponsors 79 | Sponsor 80 | 81 | Enjoy! 82 | 83 | [1]: https://github.com/R4meau/minishell/blob/master/minishell.en.pdf 84 | [quick-demo]: https://raw.githubusercontent.com/R4meau/minishell/master/minishell-quick-demo.gif?token=ADzLiR-sTesle5g6_4CQnHz4RFe69TgDks5ZK6oGwA%3D%3D 85 | [libft-url]: https://github.com/R4meau/libft 86 | [norme-pdf]: https://github.com/R4meau/minishell/blob/master/norme.en.pdf 87 | -------------------------------------------------------------------------------- /src/unsetenv_builtin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* unsetenv_builtin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/08 15:35:14 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:33:22 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Prints the environment variable on the screen 17 | ** 18 | ** @param N/A 19 | ** @returns N/A 20 | */ 21 | 22 | void print_env(void) 23 | { 24 | int i; 25 | 26 | i = -1; 27 | while (g_envv[++i]) 28 | ft_putendl(g_envv[i]); 29 | } 30 | 31 | /* 32 | ** Returns the length of the parent shell environment variable 33 | ** 34 | ** @param envv The parent shell environment variable 35 | ** @return The length of envv 36 | */ 37 | 38 | static int envv_len(char **envv) 39 | { 40 | int i; 41 | int count; 42 | 43 | i = -1; 44 | count = 0; 45 | while (envv[++i]) 46 | count++; 47 | return (count); 48 | } 49 | 50 | /* 51 | ** Makes a copy of the environment variable of the parent shell into the 52 | ** global variable g_envv 53 | ** NOTE: I'm using a global variable so I can be able to free the memory once 54 | ** the program gets killed 55 | ** 56 | ** @param ac argument counts 57 | ** @param av argument variables 58 | ** @param envv The parent shell environment variable 59 | ** @return N/A 60 | */ 61 | 62 | void init_envv(int ac, char **av, char **envv) 63 | { 64 | int i; 65 | 66 | (void)ac; 67 | (void)av; 68 | g_envv = (char **)ft_memalloc(sizeof(char *) * (envv_len(envv) + 1)); 69 | i = -1; 70 | while (envv[++i]) 71 | { 72 | if (!(g_envv[i] = ft_strdup(envv[i]))) 73 | exit_shell(); 74 | } 75 | } 76 | 77 | /* 78 | ** Removes a variable from the environment variable list 79 | ** NOTE: Always make sure to search for the variable before using this 80 | ** function, it expects that you're sure the variable already exists 81 | ** in the environment 82 | ** 83 | ** @param var_pos The position at which the variable was found 84 | ** @return N/A 85 | */ 86 | 87 | static void remove_env_var(int var_pos) 88 | { 89 | int i; 90 | int var_count; 91 | 92 | free(g_envv[var_pos]); 93 | g_envv[var_pos] = NULL; 94 | i = var_pos; 95 | var_count = var_pos + 1; 96 | while (g_envv[i + 1]) 97 | { 98 | g_envv[i] = ft_strdup(g_envv[i + 1]); 99 | free(g_envv[i + 1]); 100 | i++; 101 | var_count++; 102 | } 103 | g_envv = realloc_envv(var_count - 1); 104 | } 105 | 106 | /* 107 | ** Executes the builtin unsetenv command 108 | ** 109 | ** @param args The arguments to pass to unsetenv 110 | ** @return 1 on completion 111 | */ 112 | 113 | int unsetenv_builtin(char **args) 114 | { 115 | int i; 116 | int var_pos; 117 | 118 | if (!args[0]) 119 | { 120 | ft_putendl("unsetenv: Too few arguments."); 121 | return (1); 122 | } 123 | i = -1; 124 | while (args[++i]) 125 | { 126 | var_pos = find_env_var(args[i]); 127 | if (g_envv[var_pos]) 128 | remove_env_var(var_pos); 129 | } 130 | return (1); 131 | } 132 | -------------------------------------------------------------------------------- /src/cd_builtin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* cd_builtin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/07 15:56:23 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 15:10:32 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Prints a path on the screen by making sure it's parsed first 17 | ** 18 | ** @param path The path to print 19 | ** @return N/A 20 | */ 21 | 22 | static void print_pth(char *path) 23 | { 24 | char *parsed_home; 25 | 26 | parsed_home = parse_home_path(path, 0); 27 | ft_putstr(parsed_home); 28 | free(parsed_home); 29 | } 30 | 31 | /* 32 | ** Changes the working directoy and updates the environment variable 33 | ** accordingly while handling errors 34 | ** NOTE: I was not allowed to use errno/strerror to handle errors for this 35 | ** project. 36 | ** TODO: I should be changing the value of PWD too but I was getting 37 | ** some memory leaks for some reason. I will fix that later. 38 | ** 39 | ** @param path The path to change to 40 | ** @param print_path A boolean to know whether a not to print the 41 | ** path 42 | ** @return N/A 43 | */ 44 | 45 | void change_dir(char *path, int print_path) 46 | { 47 | char *cwd; 48 | char buff[4097]; 49 | 50 | cwd = getcwd(buff, 4096); 51 | if (!chdir(path)) 52 | { 53 | if (print_path) 54 | { 55 | print_pth(path); 56 | ft_putchar('\n'); 57 | } 58 | set_env_var("OLDPWD", cwd); 59 | } 60 | else 61 | { 62 | ft_putstr("cd: "); 63 | if (access(path, F_OK) == -1) 64 | ft_putstr("no such file or directory: "); 65 | else if (access(path, R_OK) == -1) 66 | ft_putstr("permission denied: "); 67 | else 68 | ft_putstr("not a directory: "); 69 | ft_putendl(path); 70 | } 71 | } 72 | 73 | /* 74 | ** Checks if the input has two or more arguments and acts accordingly 75 | ** 76 | ** @param args The list of arguments to check 77 | ** @return 0 if there is no second argument, 1 if there is 78 | */ 79 | 80 | static int has_two_args(char **args) 81 | { 82 | char *cwd; 83 | char buff[4096 + 1]; 84 | char *tmp; 85 | 86 | if (args[1]) 87 | { 88 | if (args[2]) 89 | { 90 | ft_putendl("cd: too many arguments"); 91 | return (1); 92 | } 93 | cwd = getcwd(buff, 4096); 94 | if (!(tmp = ft_strreplace(cwd, args[0], args[1]))) 95 | { 96 | ft_putstr("cd: string not in pwd: "); 97 | ft_putendl(args[0]); 98 | free(tmp); 99 | return (1); 100 | } 101 | change_dir(tmp, 1); 102 | free(tmp); 103 | return (1); 104 | } 105 | return (0); 106 | } 107 | 108 | /* 109 | ** Executes the builtin cd command 110 | ** 111 | ** @param args The arguments to pass to cd 112 | ** @return 1 on completion 113 | */ 114 | 115 | int cd_builtin(char **args) 116 | { 117 | char *home_path; 118 | 119 | home_path = get_env_var("HOME"); 120 | if (!args[0]) 121 | { 122 | change_dir(home_path, 0); 123 | return (1); 124 | } 125 | if (has_two_args(args)) 126 | return (1); 127 | else 128 | { 129 | if (ft_strequ(args[0], "--")) 130 | { 131 | change_dir(home_path, 0); 132 | return (1); 133 | } 134 | else if (args[0][0] == '-' && !args[0][2]) 135 | { 136 | change_dir(get_env_var("OLDPWD"), 1); 137 | return (1); 138 | } 139 | change_dir(args[0], 0); 140 | } 141 | return (1); 142 | } 143 | -------------------------------------------------------------------------------- /src/setenv_builtin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* setenv_builtin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/07 17:16:55 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/20 20:38:04 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Searches for a variable in the environment variable and returns its 17 | ** index, if not found, it returns the total length 18 | ** 19 | ** @param var The variable name to find 20 | ** @returns The index position of the variable or the length of the 21 | ** environment variable 22 | */ 23 | 24 | int find_env_var(char *var) 25 | { 26 | int i; 27 | char *tmp; 28 | 29 | i = -1; 30 | while (g_envv[++i]) 31 | { 32 | tmp = ft_strjoinch(var, '='); 33 | if (ft_strstartswith(g_envv[i], tmp)) 34 | { 35 | free(tmp); 36 | return (i); 37 | } 38 | free(tmp); 39 | } 40 | return (i); 41 | } 42 | 43 | /* 44 | ** Returns a pointer to the value of the environment variable to find 45 | ** 46 | ** @param var The variable name to find 47 | ** @return NULL if var wasn't found, or a pointer to 48 | ** the value of var in the environment 49 | */ 50 | 51 | char *get_env_var(char *var) 52 | { 53 | int i; 54 | char *tmp; 55 | 56 | i = -1; 57 | while (g_envv[++i]) 58 | { 59 | tmp = ft_strjoinch(var, '='); 60 | if (ft_strstartswith(g_envv[i], tmp)) 61 | { 62 | free(tmp); 63 | return (ft_strchr(g_envv[i], '=') + 1); 64 | } 65 | free(tmp); 66 | } 67 | return (NULL); 68 | } 69 | 70 | /* 71 | ** Reallocates memory for the environment variable 72 | ** 73 | ** @param new_size The new size to allocate 74 | ** @return A copy of the environment variable with the new size 75 | */ 76 | 77 | char **realloc_envv(int new_size) 78 | { 79 | char **new; 80 | int i; 81 | 82 | new = (char **)ft_memalloc(sizeof(char *) * (new_size + 1)); 83 | i = -1; 84 | while (g_envv[++i] && i < new_size) 85 | { 86 | new[i] = ft_strdup(g_envv[i]); 87 | free(g_envv[i]); 88 | } 89 | free(g_envv); 90 | return (new); 91 | } 92 | 93 | /* 94 | ** Adds a variable to the environment variable using a key value pair 95 | ** 96 | ** @param key The variable name 97 | ** @param value The variable value 98 | ** @return N/A 99 | */ 100 | 101 | void set_env_var(char *key, char *value) 102 | { 103 | int pos; 104 | char *tmp; 105 | 106 | pos = find_env_var(key); 107 | tmp = ft_strjoin("=", value); 108 | if (g_envv[pos]) 109 | { 110 | free(g_envv[pos]); 111 | if (value) 112 | g_envv[pos] = ft_strjoin(key, tmp); 113 | else 114 | g_envv[pos] = ft_strjoin(key, "="); 115 | } 116 | else 117 | { 118 | g_envv = realloc_envv(pos + 1); 119 | if (value) 120 | g_envv[pos] = ft_strjoin(key, tmp); 121 | else 122 | g_envv[pos] = ft_strjoin(key, "="); 123 | } 124 | free(tmp); 125 | } 126 | 127 | /* 128 | ** Executes the setenv builtin command, takes the format 'VAR_NAME VAR_VALUE', 129 | ** not 'VAR_NAME=VAR_VALUE'. When called with no arguments, it prints all the 130 | ** environment variables, just like env, otherwise, it parses the arguments and 131 | ** prints accordingly 132 | ** 133 | ** @param args The arguments to pass to setenv 134 | ** @returns 1 on completion 135 | */ 136 | 137 | int setenv_builtin(char **args) 138 | { 139 | if (!args[0]) 140 | { 141 | print_env(); 142 | return (1); 143 | } 144 | if (args[1]) 145 | { 146 | if (args[2]) 147 | { 148 | ft_putendl("setenv: Too many arguments."); 149 | return (1); 150 | } 151 | } 152 | set_env_var(args[0], args[1]); 153 | return (1); 154 | } 155 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* main.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/24 04:17:47 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:04:34 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Parses a string to convert variables to their value then returns the parsed 17 | ** string 18 | ** 19 | ** @param str The input string to parse 20 | ** @param pos The position from which to start in the string 21 | */ 22 | 23 | static char *parse_env_var(char *str, int pos) 24 | { 25 | char *value; 26 | char *key; 27 | char c; 28 | 29 | key = ft_strnew(1); 30 | while (str[pos] && !IS_SPACE(str[pos])) 31 | { 32 | c = str[pos]; 33 | key = ft_strjoinchcl(key, c); 34 | pos++; 35 | } 36 | if (!(value = get_env_var(key))) 37 | { 38 | free(key); 39 | return (NULL); 40 | } 41 | free(key); 42 | return (value); 43 | } 44 | 45 | /* 46 | ** Parses the input by changing $VAR_NAME to the value of VAR_NAME in the 47 | ** environment variable or by nothing if it doesn't exist and by changing ~ 48 | ** to the value of the user's home path then returns the parsed string 49 | ** 50 | ** @param input The input string 51 | ** @return The parsed string 52 | */ 53 | 54 | static char *parse_input(char *input) 55 | { 56 | int i; 57 | char *new; 58 | 59 | i = -1; 60 | new = ft_strnew(1); 61 | while (input[++i]) 62 | { 63 | if (input[i] == '$' && input[i + 1]) 64 | { 65 | new = ft_strjoincl(new, parse_env_var(input, i + 1), 0); 66 | while (input[i + 1] && !IS_SPACE(input[i + 1]) && 67 | input[i + 1] != '$') 68 | i++; 69 | } 70 | else if (((i != 0 && IS_SPACE(input[i - 1])) || i == 0) && 71 | input[i] == '~') 72 | { 73 | new = ft_strjoincl(new, parse_home_path(input + i, 1), 1); 74 | i += ft_strlen(input + i) - 1; 75 | } 76 | else 77 | new = ft_strjoinchcl(new, input[i]); 78 | } 79 | free(input); 80 | return (new); 81 | } 82 | 83 | /* 84 | ** Displays a prompt on the screen and fills the input character by character 85 | ** then adds it to the referenced variable (input) after parsing the whole 86 | ** input if necessary 87 | ** TODO: Not the most efficient way, will improve it later 88 | ** 89 | ** @param input The address of the variable to fill with the parsed input 90 | ** @return N/A 91 | */ 92 | 93 | static void get_input(char **input) 94 | { 95 | int ret; 96 | char buf; 97 | int i; 98 | int count; 99 | 100 | *input = ft_strnew(1); 101 | count = 1; 102 | i = 0; 103 | while ((ret = read(0, &buf, 1)) && buf != '\n') 104 | { 105 | *(*input + i++) = buf; 106 | *input = ft_realloc(*input, count, count + 1); 107 | count++; 108 | } 109 | *(*input + i) = '\0'; 110 | if (!ret) 111 | { 112 | free(*input); 113 | exit_shell(); 114 | } 115 | if ((ft_strchr(*input, '$') != NULL) || (ft_strchr(*input, '~') != NULL)) 116 | *input = parse_input(*input); 117 | } 118 | 119 | /* 120 | ** Takes care of multiple commands in the input 121 | ** 122 | ** @param commands The list of commands to execute 123 | ** @return -1 if there was an interruption from one of the commands 124 | ** or 0/1 if not 125 | */ 126 | 127 | int exec_commands(char **commands) 128 | { 129 | int i; 130 | int ret; 131 | char **command; 132 | 133 | i = -1; 134 | ret = 0; 135 | while (commands[++i]) 136 | { 137 | command = ft_strsplitall(commands[i]); 138 | ret = exec_command(command); 139 | ft_freestrarr(command); 140 | if (ret == -1) 141 | break ; 142 | } 143 | return (ret); 144 | } 145 | 146 | /* 147 | ** Initializes minishell 148 | ** 149 | ** @param ac argument counts 150 | ** @param av argument variables 151 | ** @param envv environment variables 152 | ** @return 0 on completion 153 | */ 154 | 155 | int main(int ac, char **av, char **envv) 156 | { 157 | char *input; 158 | int ret; 159 | char **commands; 160 | 161 | init_envv(ac, av, envv); 162 | while (1) 163 | { 164 | display_prompt_msg(); 165 | signal(SIGINT, signal_handler); 166 | get_input(&input); 167 | if (ft_isemptystr(input, 1)) 168 | { 169 | free(input); 170 | continue ; 171 | } 172 | commands = ft_strsplit(input, ';'); 173 | free(input); 174 | ret = exec_commands(commands); 175 | ft_freestrarr(commands); 176 | if (ret == -1) 177 | break ; 178 | } 179 | ft_freestrarr(g_envv); 180 | return (0); 181 | } 182 | -------------------------------------------------------------------------------- /libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2016/09/21 21:30:24 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:18:11 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | # include 16 | # include 17 | # include 18 | 19 | # define BUFF_SIZE 100 20 | # define MALLCHECK(x) if (!x) return (-1); 21 | # define IS_SPACE(x) (x == ' ' || x == '\t' || x == '\r' || x == '\f') 22 | 23 | void ft_putchar(char c); 24 | void ft_putstr(char const *str); 25 | void ft_putendl(char const *str); 26 | void ft_putnbr(int nbr); 27 | void ft_putchar_fd(char c, int fd); 28 | void ft_putstr_fd(char const *s, int fd); 29 | void ft_putendl_fd(char const *s, int fd); 30 | void ft_putnbr_fd(int n, int fd); 31 | 32 | char *ft_itoa(int n); 33 | int ft_atoi(const char *str); 34 | 35 | void *ft_memalloc(size_t size); 36 | void ft_memdel(void **ap); 37 | char *ft_strnew(size_t size); 38 | void ft_strdel(char **as); 39 | void ft_strclr(char *s); 40 | void ft_striter(char *s, void (*f)(char *)); 41 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 42 | char *ft_strmap(char const *s, char (*f)(char)); 43 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 44 | 45 | int ft_strequ(char const *s1, char const *s2); 46 | int ft_strnequ(char const *s1, char const *s2, size_t n); 47 | char *ft_strsub(char const *s, unsigned int start, size_t len); 48 | char *ft_strjoin(char const *s1, char const *s2); 49 | char *ft_strtrim(char const *s); 50 | char **ft_strsplit(char const *s, char c); 51 | 52 | void *ft_memset(void *b, int c, size_t len); 53 | void ft_bzero(void *s, size_t n); 54 | void *ft_memcpy(void *dst, const void *src, size_t n); 55 | void *ft_memccpy(void *dst, const void *restrict src, 56 | int c, size_t n); 57 | void *ft_memmove(void *dst, const void *src, size_t len); 58 | void *ft_memchr(const void *s, int c, size_t n); 59 | int ft_memcmp(const void *s1, const void *s2, size_t n); 60 | 61 | size_t ft_strlen(const char *str); 62 | char *ft_strdup(const char *s1); 63 | char *ft_strndup(const char *s1, size_t n); 64 | char *ft_strcpy(char *dst, const char *src); 65 | char *ft_strncpy(char *dst, const char *src, size_t len); 66 | char *ft_strcat(char *s1, const char *s2); 67 | char *ft_strncat(char *s1, const char *s2, size_t n); 68 | size_t ft_strlcat(char *dst, const char *src, size_t size); 69 | char *ft_strchr(const char *s, int c); 70 | char *ft_strrchr(const char *s, int c); 71 | char *ft_strstr(const char *big, const char *little); 72 | char *ft_strnstr(const char *big, 73 | const char *little, size_t len); 74 | int ft_strcmp(const char *s1, const char *s2); 75 | int ft_strncmp(const char *s1, const char *s2, size_t n); 76 | 77 | int ft_isalpha(int c); 78 | int ft_isdigit(int c); 79 | int ft_isalnum(int c); 80 | int ft_isascii(int c); 81 | int ft_isprint(int c); 82 | int ft_toupper(int c); 83 | int ft_tolower(int c); 84 | 85 | # ifndef IS_SPACE 86 | # define IS_SPACE(x) (x==' '||x=='\n'||x=='\t') 87 | # endif 88 | 89 | typedef struct s_list 90 | { 91 | void *content; 92 | size_t content_size; 93 | struct s_list *next; 94 | } t_list; 95 | 96 | t_list *ft_lstnew(const void *content, size_t content_size); 97 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); 98 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); 99 | void ft_lstadd(t_list **alst, t_list *n); 100 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); 101 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); 102 | 103 | /* 104 | ** Extra functions 105 | */ 106 | 107 | int ft_isupper(int c); 108 | int ft_islower(int c); 109 | int ft_countwords(char const *str, char c); 110 | char *ft_strndup(const char *s1, size_t n); 111 | char *ft_capitalize(char *s); 112 | t_list *ft_lst_reverse(t_list *alst); 113 | void *ft_realloc(void *ptr, size_t prev_size, size_t new_size); 114 | char *ft_strjoinch(char const *s1, char c); 115 | char *ft_strnchr(char *s, char c, int offset); 116 | int ft_copyuntil(char **dst, char *src, char c); 117 | int ft_strstartswith(char *s1, char *s2); 118 | int ft_intlen(int num); 119 | int ft_strendswith(char *s1, char *s2); 120 | char *ft_pathjoin(char *p1, char *p2); 121 | void ft_lstaddback(t_list **alst, t_list *new); 122 | int get_next_line(const int fd, char **line); 123 | void ft_putnstr(char *str, int n); 124 | char *ft_strreplace(char *str, char *term, char *replace_by); 125 | int ft_isemptystr(char *str, int consider_space); 126 | char **ft_strsplitall(char const *s); 127 | int ft_countwordsall(char const *str); 128 | void ft_freestrarr(char **arr); 129 | char *ft_strjoincl(char *s1, char *s2, int free_both); 130 | char *ft_strjoinchcl(char *s1, char c); 131 | #endif 132 | -------------------------------------------------------------------------------- /src/exec_command.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* exec_command.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jrameau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/04/26 05:44:00 by jrameau #+# #+# */ 9 | /* Updated: 2017/05/21 01:01:53 by jrameau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "minishell.h" 14 | 15 | /* 16 | ** Runs the binary file by creating a new process and handles signals in case 17 | ** of interruptions then returns whether it executed successfully or not 18 | ** 19 | ** @param path The path to the binary file 20 | ** @param args The arguments to pass to the system command 21 | ** @return -1 on failure, 1 on success 22 | */ 23 | 24 | static int run_cmd(char *path, char **args) 25 | { 26 | pid_t pid; 27 | 28 | pid = fork(); 29 | signal(SIGINT, proc_signal_handler); 30 | if (pid == 0) 31 | execve(path, args, g_envv); 32 | else if (pid < 0) 33 | { 34 | free(path); 35 | ft_putendl("Fork failed to create a new process."); 36 | return (-1); 37 | } 38 | wait(&pid); 39 | if (path) 40 | free(path); 41 | return (1); 42 | } 43 | 44 | /* 45 | ** Checks if the first word in the input is one of the builtin commands, if it 46 | ** is, it executes it and returns -1, 0, or 1 47 | ** 48 | ** @param command The array of strings that contains the command 49 | ** @return -1 on exit, 0 if it's not a builtin, 1 otherwise 50 | */ 51 | 52 | static int check_builtins(char **command) 53 | { 54 | if (ft_strequ(command[0], "exit")) 55 | return (-1); 56 | else if (ft_strequ(command[0], "echo")) 57 | return (echo_builtin(command + 1)); 58 | else if (ft_strequ(command[0], "cd")) 59 | return (cd_builtin(command + 1)); 60 | else if (ft_strequ(command[0], "setenv")) 61 | return (setenv_builtin(command + 1)); 62 | else if (ft_strequ(command[0], "unsetenv")) 63 | return (unsetenv_builtin(command + 1)); 64 | else if (ft_strequ(command[0], "env")) 65 | { 66 | print_env(); 67 | return (1); 68 | } 69 | return (0); 70 | } 71 | 72 | /* 73 | ** Checks if the path is a regular file and is an executable, if it is, 74 | ** it executes it, if not, it prints an error on the screen then it returns 75 | ** whether it executed successfully or not 76 | ** 77 | ** @param bin_path The path to search for 78 | ** @param f The path information from stat/lstat 79 | ** @param command The array of strings containing each word from the 80 | ** input 81 | ** @return 0 if the first path is not an executable or if the command 82 | ** was not executed properly or 1 for the opposite 83 | */ 84 | 85 | static int is_executable(char *bin_path, struct stat f, char **command) 86 | { 87 | if (f.st_mode & S_IFREG) 88 | { 89 | if (f.st_mode & S_IXUSR) 90 | return (run_cmd(bin_path, command)); 91 | else 92 | { 93 | ft_putstr("minishell: permission denied: "); 94 | ft_putendl(bin_path); 95 | } 96 | free(bin_path); 97 | return (1); 98 | } 99 | free(bin_path); 100 | return (0); 101 | } 102 | 103 | /* 104 | ** Uses the first word of the input to search for an existing executable on the 105 | ** system, executes it if it found it and returns whether it found it or not 106 | ** 107 | ** @param command The array of strings containing each word from the 108 | ** input 109 | ** @param path The value of the PATH environment variable 110 | ** @return 0 if the first word is not an executable or if the command was 111 | ** not executed properly or 1 for the opposite 112 | */ 113 | 114 | static int check_bins(char **command) 115 | { 116 | int i; 117 | char *bin_path; 118 | char **path; 119 | struct stat f; 120 | 121 | path = ft_strsplit(get_env_var("PATH"), ':'); 122 | i = -1; 123 | while (path && path[++i]) 124 | { 125 | if (ft_strstartswith(command[0], path[i])) 126 | bin_path = ft_strdup(command[0]); 127 | else 128 | bin_path = ft_pathjoin(path[i], command[0]); 129 | if (lstat(bin_path, &f) == -1) 130 | free(bin_path); 131 | else 132 | { 133 | ft_freestrarr(path); 134 | return (is_executable(bin_path, f, command)); 135 | } 136 | } 137 | ft_freestrarr(path); 138 | return (0); 139 | } 140 | 141 | /* 142 | ** Executes a command and prints the result on the screen then returns whether 143 | ** there was a an exit or not 144 | ** The steps are: 145 | ** 1. Check if it's a builtin command, if not... 146 | ** 2. Check if it's a system command, if not ... 147 | ** 3. Check if it's a folder or an executable, if not... 148 | ** 4. Display an error (not found) message. 149 | ** 150 | ** @param command The command to execute 151 | ** @return -1 if there was an interruption (exit) or 0/1 if not 152 | */ 153 | 154 | int exec_command(char **command) 155 | { 156 | struct stat f; 157 | int is_builtin; 158 | 159 | if ((is_builtin = check_builtins(command)) == 1 || check_bins(command)) 160 | return (0); 161 | if (is_builtin < 0) 162 | return (-1); 163 | if (lstat(command[0], &f) != -1) 164 | { 165 | if (f.st_mode & S_IFDIR) 166 | { 167 | change_dir(command[0], 0); 168 | return (0); 169 | } 170 | else if (f.st_mode & S_IXUSR) 171 | return (run_cmd(ft_strdup(command[0]), command)); 172 | } 173 | ft_putstr("minishell: command not found: "); 174 | ft_putendl(command[0]); 175 | return (0); 176 | } 177 | --------------------------------------------------------------------------------