diff --git a/Makefile b/Makefile index 4d4b05d..0699362 100644 --- a/Makefile +++ b/Makefile @@ -1,39 +1,35 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: apommier +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2022/03/06 12:50:24 by apommier #+# #+# # -# Updated: 2022/03/06 13:53:49 by apommier ### ########.fr # -# # -# **************************************************************************** # +NAME = Minishell +CC = clang -NAME = minishell -SRCS = main.c -OBJS = ${SRCS:.c=.o} -CC = gcc -CFLAGS = -Wall -Wextra -#CFLAGS = -Wall -Wextra -Werror -RM = rm -rf -LIBFT = ./libft +FLAGS = -Wall -Wextra -Werror -${NAME}: ${OBJS} - @make bonus -C ${LIBFT} - @${CC} ${CFLAGS} ${OBJS} ${LIBFT}/libft.a -o ${NAME} - -all: ${NAME} bonus +DEL = /bin/rm -f + +SRCS = ./srcs/main.c\ + ./srcs/parser.c\ + ./srcs/init.c\ + ./srcs/exit.c\ + ./srcs/export_utils.c\ + +SRCS_O = ${SRCS:.c=.o} +all: $(NAME) + +LIBC = ar -rcs + +%.o: %.c + ${CC} ${FLAGS} -c $< -o ${<:.c=.o} + +$(NAME): ${SRCS_O} + make bonus -C ./libft/ + $(CC) $(FLAGS) $(SRCS_O) -o $(NAME) -L ./libft/ -lft -lreadline + +fclean: clean + $(DEL) $(NAME) + make fclean -C ./libft/ clean: - @${RM} ${OBJS} - @make clean -C ${LIBFT} + $(DEL) $(SRCS_O) + make clean -C ./libft/ -fclean: clean - @${RM} ${NAME} - @make fclean -C ${LIBFT} - -re: fclean all - -.PHONY: all clean fclean re bonus \ No newline at end of file +re: fclean all diff --git a/includes/mini.h b/includes/mini.h new file mode 100644 index 0000000..68bd24a --- /dev/null +++ b/includes/mini.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mini.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/26 16:00:38 by sadjigui #+# #+# */ +/* Updated: 2022/03/01 17:12:12 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINI_H +# define MINI_H + +# include +# include +# include +# include +# include +# include "../libft/libft.h" + +typedef struct s_struct { + char **envy; +} t_struct; + +int find_len(char *input, int i); +int check_variable(char *variable); + +void normal(); +void red(); +void lexer(char *input, t_struct *data); +void print_env(t_struct *data); +void init_struct(t_struct *data, char **env); +void ft_env(t_struct *data, char **env); +void ft_exit(t_struct *data); +void ft_export(char *input, t_struct *data); +void ft_export_variable(t_struct *data, char *variable); +void register_env(t_struct *data, char **variable); +void free_char_tab(char **libre); +void join_variable(t_struct *data, char **v_v, int size); + +char *check_value(char *value); +char *define_value(char *value); + +#endif diff --git a/libft/Makefile b/libft/Makefile index a77d2ed..ac0ce7a 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -1,68 +1,78 @@ -NAME = libft.a -SRCS = ft_memset.c \ - ft_bzero.c \ - ft_memcpy.c \ - ft_memccpy.c \ - ft_memmove.c \ - ft_memchr.c \ - ft_memcmp.c \ - ft_strlen.c \ - ft_isalpha.c \ - ft_isdigit.c \ - ft_isalnum.c \ - ft_isascii.c \ - ft_isprint.c \ - ft_toupper.c \ - ft_tolower.c \ - ft_strchr.c \ - ft_strrchr.c \ - ft_strncmp.c \ - ft_strlcpy.c \ - ft_strlcat.c \ - ft_strnstr.c \ - ft_atoi.c \ - ft_calloc.c \ - ft_strdup.c \ - ft_substr.c \ - ft_strtrim.c \ - ft_split.c \ - ft_strjoin.c \ - ft_itoa.c \ - ft_strmapi.c \ - ft_putchar_fd.c \ - ft_putstr_fd.c \ - ft_putendl_fd.c \ - ft_putnbr_fd.c \ - get_next_line.c \ - get_next_line_utils.c -OBJS = ${SRCS:.c=.o} -BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \ - ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c -BONUS_O = ${BONUS_C:.c=.o} -CC = gcc -CFLAGS = -Wall -Wextra -RM = rm -rf +NAME = libft.a -.c.o: - @${CC} ${CFLAGS} -c $< -o ${<:.c=.o} +CC = gcc -${NAME}:${OBJS} - @ar -rcs ${NAME} ${OBJS} +FLAGS = -Wall -Wextra -Werror -all: ${NAME} +DEL = /bin/rm -f -bonus: all - @${CC} ${CFLAGS} -c ${BONUS_C} - @ar -rcs ${NAME} ${BONUS_O} +SRCS = ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_memccpy.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_split.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_putchar_fd.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_tolower.c \ + ft_toupper.c \ + +BONUS = ft_lstnew.c \ + ft_lstsize.c \ + ft_lstlast.c \ + ft_lstadd_front.c \ + ft_lstadd_back.c \ + ft_lstdelone.c \ + ft_lstiter.c \ + ft_lstmap.c \ + ft_lstclear.c \ + ft_putchar.c \ + ft_putstr.c \ + ft_strcmp.c \ + +SRCS_O = ${SRCS:.c=.o} +BONUS_O = ${BONUS:.c=.o} +all: $(NAME) + +LIBC = ar -rcs + +%.o: %.c + ${CC} ${FLAGS} -c $< -o ${<:.c=.o} + +$(NAME): ${SRCS_O} + ${LIBC} $(NAME) $(SRCS_O) + +bonus: $(SRCS_O) $(BONUS_O) + $(LIBC) $(NAME) $(SRCS_O) $(BONUS_O) + +fclean: clean + $(DEL) $(NAME) clean: - @${RM} ${OBJS} - @${RM} ${BONUS_O} - -fclean: clean - @${RM} ${NAME} - -re: fclean all - -.PHONY: all clean fclean re bonus + $(DEL) $(SRCS_O) $(BONUS_O) +re: fclean all diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c index 0073b7e..762e397 100644 --- a/libft/ft_atoi.c +++ b/libft/ft_atoi.c @@ -3,37 +3,47 @@ /* ::: :::::::: */ /* ft_atoi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */ -/* Updated: 2022/01/18 06:50:22 by apommier ### ########.fr */ +/* Created: 2021/05/22 12:44:41 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:13:51 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -long ft_atoi(const char *nptr) +static int ft_space(const char *str) +{ + int i; + + i = 0; + while ((str[i] == 32) || (str[i] >= 9 && str[i] <= 13)) + i++; + return (i); +} + +long ft_atoi(const char *str) { int i; - long nbr; - long minus; + int sign; + long result; - minus = 1; - nbr = 0; - i = 0; - while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32) - i++; - if (nptr[i] == '+') - i++; - else if (nptr[i] == '-') + result = 0; + i = ft_space(str); + sign = 0; + if (str[i] == '-' || str[i] == '+') { - i++; - minus = -1; - } - while (nptr[i] >= '0' && nptr[i] <= '9') - { - nbr = nbr * 10 + nptr[i] - '0'; + if (str[i] == '-') + sign = 1; i++; } - return (minus * nbr); + while (str[i] >= '0' && str[i] <= '9') + { + result = result * 10 + str[i] - 48; + i++; + } + if (sign == 1) + return (-result); + else + return (result); } diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c index 5baa4fe..0a47f6d 100644 --- a/libft/ft_bzero.c +++ b/libft/ft_bzero.c @@ -3,26 +3,23 @@ /* ::: :::::::: */ /* ft_bzero.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:09:48 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:26:30 by apommier ### ########.fr */ +/* Created: 2021/05/22 12:54:03 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 16:52:41 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_bzero(void *s, size_t n) +void ft_bzero(void *str, size_t n) { - char *p; - int i; + size_t i; - p = (char *)s; i = 0; - while (n != 0) + while (i < n) { - p[i] = 0; + *(char *)(str + i) = 0; i++; - n--; } } diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c index e4cea33..ee91671 100644 --- a/libft/ft_calloc.c +++ b/libft/ft_calloc.c @@ -3,29 +3,23 @@ /* ::: :::::::: */ /* ft_calloc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:09:57 by apommier #+# #+# */ -/* Updated: 2022/01/17 21:18:04 by apommier ### ########.fr */ +/* Created: 2021/05/22 12:55:08 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 12:55:45 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_calloc(size_t nmemb, size_t size) +void *ft_calloc(size_t count, size_t size) { - char *new; - int i; + void *trace; - i = 0; - new = malloc(size * nmemb); - if (new) - { - while (size * nmemb - i) - { - new[i] = 0; - i++; - } - } - return ((void *)new); + trace = malloc(count * size); + if (!trace) + return (NULL); + if (trace) + ft_bzero(trace, count * size); + return (trace); } diff --git a/libft/ft_isacii.c b/libft/ft_isacii.c new file mode 100644 index 0000000..0c151b8 --- /dev/null +++ b/libft/ft_isacii.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isacii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/05/20 12:08:41 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 12:49:44 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + else + return (0); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c index 7f628b5..73a2498 100644 --- a/libft/ft_isalnum.c +++ b/libft/ft_isalnum.c @@ -3,20 +3,19 @@ /* ::: :::::::: */ /* ft_isalnum.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:10:08 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:26:43 by apommier ### ########.fr */ +/* Created: 2021/05/22 12:56:29 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 13:06:39 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_isalnum(int c) +int ft_isalnum(int i) { - if (c <= '9' && c >= '0') - return (1); - else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') + || (i >= '0' && i <= '9')) return (1); else return (0); diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c index 51292d2..5938f1e 100644 --- a/libft/ft_isalpha.c +++ b/libft/ft_isalpha.c @@ -3,18 +3,18 @@ /* ::: :::::::: */ /* ft_isalpha.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:10:17 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:28:12 by apommier ### ########.fr */ +/* Created: 2021/05/22 13:07:14 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 13:08:24 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_isalpha(int c) +int ft_isalpha(int i) { - if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')) return (1); else return (0); diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c index be6ac40..0ce203b 100644 --- a/libft/ft_isascii.c +++ b/libft/ft_isascii.c @@ -3,18 +3,18 @@ /* ::: :::::::: */ /* ft_isascii.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:10:30 by apommier #+# #+# */ -/* Updated: 2020/12/11 18:11:18 by apommier ### ########.fr */ +/* Created: 2021/05/22 13:08:54 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 13:10:00 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_isascii(int c) +int ft_isascii(int i) { - if (c >= 0 && c <= 127) + if (i >= 0 && i <= 127) return (1); else return (0); diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c index 81ca1f0..f38d511 100644 --- a/libft/ft_isdigit.c +++ b/libft/ft_isdigit.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_isdigit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:10:39 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:27:06 by apommier ### ########.fr */ +/* Created: 2021/05/22 13:10:25 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:18:53 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,5 @@ int ft_isdigit(int c) { - if (c <= '9' && c >= '0') - return (1); - else - return (0); + return (c >= '0' && c <= '9'); } diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c index 1915c16..ad9ce5e 100644 --- a/libft/ft_isprint.c +++ b/libft/ft_isprint.c @@ -3,18 +3,18 @@ /* ::: :::::::: */ /* ft_isprint.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:10:55 by apommier #+# #+# */ -/* Updated: 2020/12/11 18:11:43 by apommier ### ########.fr */ +/* Created: 2021/05/22 13:11:25 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 13:12:26 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_isprint(int c) +int ft_isprint(int i) { - if (c > 31 && c < 127) + if (i >= 32 && i <= 126) return (1); else return (0); diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c index 70f2fd0..97f9351 100644 --- a/libft/ft_itoa.c +++ b/libft/ft_itoa.c @@ -3,57 +3,67 @@ /* ::: :::::::: */ /* ft_itoa.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 18:20:19 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:28:01 by apommier ### ########.fr */ +/* Created: 2021/05/22 13:13:20 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:09:12 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include -static char *fill(long n, int j, int minus) +int base_len(long nb) { - char *dest; + int len; - j += minus; - dest = (char *)malloc(sizeof(char) * (j + 1)); - if (dest == 0) - return (0); - dest[j] = 0; - if (n == 0) - dest[j - 1] = '0'; - while (n) + len = 1; + if (nb < 0) { - dest[j - 1] = n % 10 + '0'; - j--; + nb = -nb; + len++; + } + while (nb >= 10) + { + nb /= 10; + len++; + } + return (len); +} + +static void filler(char *str, int i, long n) +{ + if (n < 0) + { + str[0] = '-'; + n = -n; + } + while (n > 0) + { + str[i] = 48 + (n % 10); n /= 10; + i--; } - if (minus) - dest[j - 1] = '-'; - return (dest); } -char *ft_itoa(int n) +char *ft_itoa(int nb) { - long i; - long k; - int j; - int minus; + long n; + int i; + char *str; - k = n; - minus = 0; - j = 1; - if (k < 0) + n = nb; + i = base_len(n); + str = (char *)malloc(sizeof(char) * i + 1); + if (!str) + return (NULL); + str[i] = '\0'; + i--; + if (n == 0) { - minus = 1; - k = k * -1; + str[0] = 48; + return (str); } - i = k; - while (k >= 10) - { - k /= 10; - j++; - } - return (fill(i, j, minus)); + filler(str, i, n); + return (str); } diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c index 87c0a15..6e42271 100644 --- a/libft/ft_lstadd_back.c +++ b/libft/ft_lstadd_back.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstadd_back.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 19:30:14 by apommier #+# #+# */ -/* Updated: 2020/12/11 15:32:53 by apommier ### ########.fr */ +/* Created: 2021/06/03 15:05:04 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 11:37:08 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,16 @@ void ft_lstadd_back(t_list **alst, t_list *new) { - if (*alst == 0) - *alst = new; - else - ft_lstlast(*alst)->next = new; + t_list *yup; + + if (alst) + { + if (*alst) + { + yup = ft_lstlast(*alst); + yup->next = new; + } + else + *alst = new; + } } diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c index 8dfa145..118b833 100644 --- a/libft/ft_lstadd_front.c +++ b/libft/ft_lstadd_front.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstadd_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 01:31:45 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:34:24 by apommier ### ########.fr */ +/* Created: 2021/06/03 14:47:57 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:18:16 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ void ft_lstadd_front(t_list **alst, t_list *new) { + if (!alst) + return ; new->next = *alst; *alst = new; } diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c index 152fb31..a12b53c 100644 --- a/libft/ft_lstclear.c +++ b/libft/ft_lstclear.c @@ -3,26 +3,25 @@ /* ::: :::::::: */ /* ft_lstclear.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 19:58:04 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:28:39 by apommier ### ########.fr */ +/* Created: 2021/06/03 16:03:18 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 12:13:31 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void ft_lstclear(t_list **lst, void (*del)(void*)) +void ft_lstclear(t_list **lst, void (*del)(void *)) { - t_list *chr; + t_list *nap; - chr = *lst; - while (*lst) + if (!lst || !del || !*lst) + return ; + while (lst && *lst) { - chr = (*lst)->next; - del((*lst)->nbr); - free(*lst); - *lst = chr; + nap = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = nap; } - lst = 0; } diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c index f0dea04..c08716f 100644 --- a/libft/ft_lstdelone.c +++ b/libft/ft_lstdelone.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstdelone.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 19:54:40 by apommier #+# #+# */ -/* Updated: 2020/12/12 09:10:11 by apommier ### ########.fr */ +/* Created: 2021/06/03 15:56:45 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 16:02:20 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,8 @@ void ft_lstdelone(t_list *lst, void (*del)(void*)) { - if (!lst) + if (!del || !lst) return ; - del(lst->nbr); - free(lst); + (del)(lst->content); + free (lst); } diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c index 0e9ea7a..ab2511f 100644 --- a/libft/ft_lstiter.c +++ b/libft/ft_lstiter.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstiter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 20:14:47 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:47:39 by apommier ### ########.fr */ +/* Created: 2021/06/07 11:34:11 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 11:48:46 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,11 @@ void ft_lstiter(t_list *lst, void (*f)(void *)) { + if (!f || !lst) + return ; while (lst) { - f(lst->nbr); + (*f)(lst->content); lst = lst->next; } } diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c index 2bf317b..e310ef5 100644 --- a/libft/ft_lstlast.c +++ b/libft/ft_lstlast.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstlast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 19:16:55 by apommier #+# #+# */ -/* Updated: 2021/11/26 01:21:39 by apommier ### ########.fr */ +/* Created: 2021/06/03 15:00:53 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 16:09:33 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,11 @@ t_list *ft_lstlast(t_list *lst) { if (!lst) - return (0); - while (lst->next) - lst = lst->next; + return (NULL); + if (lst) + { + while (lst->next) + lst = lst->next; + } return (lst); } diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c index 252c0c6..309a7f8 100644 --- a/libft/ft_lstmap.c +++ b/libft/ft_lstmap.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstmap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/11 00:10:12 by apommier #+# #+# */ -/* Updated: 2020/12/13 22:37:30 by apommier ### ########.fr */ +/* Created: 2021/06/07 12:31:29 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 12:37:22 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,20 +14,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) { - t_list *new; - t_list *begin; - - begin = 0; - while (lst) - { - new = ft_lstnew(f(lst->nbr)); - if (!new) - { - ft_lstclear(&begin, *del); - return (0); - } - ft_lstadd_back(&begin, new); - lst = lst->next; - } - return (begin); + (void)*f; + (void)*del; + return (lst); } diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c index e108f42..594218d 100644 --- a/libft/ft_lstnew.c +++ b/libft/ft_lstnew.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 01:06:20 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:29:09 by apommier ### ########.fr */ +/* Created: 2021/06/03 12:53:17 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 11:49:54 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,10 @@ t_list *ft_lstnew(void *content) { t_list *new; - new = (t_list *)malloc(sizeof(t_list)); + new = malloc(sizeof(t_list)); if (!new) - return (0); - new->swap = 0; - new->nbr = content; - new->next = 0; + return (NULL); + new->content = content; + new->next = NULL; return (new); } diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c index 2f6aebe..364c228 100644 --- a/libft/ft_lstsize.c +++ b/libft/ft_lstsize.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_lstsize.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/09 03:24:50 by apommier #+# #+# */ -/* Updated: 2020/12/11 15:33:49 by apommier ### ########.fr */ +/* Created: 2021/06/03 14:58:00 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 16:10:11 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,13 @@ int ft_lstsize(t_list *lst) int i; i = 0; - while (lst) + if (lst) { - lst = lst->next; - i++; + while (lst) + { + lst = lst->next; + i++; + } } return (i); } diff --git a/libft/ft_memccpy.c b/libft/ft_memccpy.c index 0b1644c..9bb337a 100644 --- a/libft/ft_memccpy.c +++ b/libft/ft_memccpy.c @@ -3,30 +3,26 @@ /* ::: :::::::: */ /* ft_memccpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:11:04 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:29:38 by apommier ### ########.fr */ +/* Created: 2021/05/31 17:10:05 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:10:57 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memccpy(void *dest, const void *src, int c, size_t n) +void *ft_memccpy(void *dest, const void *src, int ch, size_t maxSize) { - char *p; - const char *p1; - size_t i; + size_t i; i = 0; - p = (char *)dest; - p1 = (const char *)src; - while (i < n) + while (i < maxSize) { - p[i] = p1[i]; - if ((unsigned char)p[i] == (unsigned char)c) + *(unsigned char *)(dest + i) = *(unsigned char *)(src + i); + if (*(unsigned char *)(src + i) == (unsigned char)ch) return (dest + i + 1); i++; } - return (0); + return (NULL); } diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c index ce8f294..b66d229 100644 --- a/libft/ft_memchr.c +++ b/libft/ft_memchr.c @@ -3,26 +3,25 @@ /* ::: :::::::: */ /* ft_memchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:11:39 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:29:51 by apommier ### ########.fr */ +/* Created: 2021/05/25 14:48:34 by sadjigui #+# #+# */ +/* Updated: 2021/05/25 15:57:53 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memchr(const void *s, int c, size_t n) +void *ft_memchr(const void *mem, int ch, size_t size) { - unsigned char *str; + size_t i; - str = (unsigned char *)s; - while (n) + i = 0; + while (i < size) { - if ((unsigned char)c == *str) - return (str); - n--; - str++; + if (*(unsigned char *)(mem + i) == (unsigned char)ch) + return ((unsigned char *)(mem + i)); + i++; } - return (0); + return (NULL); } diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c index f9af3ee..5ac65e8 100644 --- a/libft/ft_memcmp.c +++ b/libft/ft_memcmp.c @@ -3,25 +3,24 @@ /* ::: :::::::: */ /* ft_memcmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:11:51 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:30:32 by apommier ### ########.fr */ +/* Created: 2021/05/31 17:11:51 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:11:55 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_memcmp(const void *s1, const void *s2, size_t n) +int ft_memcmp(const void *p1, const void *p2, size_t size) { - int i; + size_t i; i = 0; - while (n) + while (i < size) { - if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i]) - return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); - n--; + if (!(*(unsigned char *)(p1 + i) == *(unsigned char *)(p2 + i))) + return (*(unsigned char *)(p1 + i) - *(unsigned char *)(p2 + i)); i++; } return (0); diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c index 92c0480..e06d105 100644 --- a/libft/ft_memcpy.c +++ b/libft/ft_memcpy.c @@ -3,29 +3,26 @@ /* ::: :::::::: */ /* ft_memcpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:12:03 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:30:43 by apommier ### ########.fr */ +/* Created: 2021/05/31 17:46:43 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:47:17 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memcpy(void *dest, const void *src, size_t n) +void *ft_memcpy(void *dest, const void *src, size_t size) { - size_t i; - char *p; - const char *p1; + size_t i; - if (!dest && !src) - return (dest); - p = (char *)dest; - p1 = (const char *)src; i = 0; - while (i < n) + if (!dest && !src) + return (NULL); + while (size > 0) { - p[i] = p1[i]; + *(unsigned char *)(dest + i) = *(unsigned char *)(src + i); + size--; i++; } return (dest); diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c index 0a8a6db..065b69f 100644 --- a/libft/ft_memmove.c +++ b/libft/ft_memmove.c @@ -3,37 +3,35 @@ /* ::: :::::::: */ /* ft_memmove.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:12:14 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:31:05 by apommier ### ########.fr */ +/* Created: 2021/05/31 17:14:39 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:22:11 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memmove(void *dest, const void *src, size_t n) +void *ft_memmove(void *dest, const void *src, size_t size) { size_t i; i = 0; - if (!dest && !src) + if (src == dest) return (dest); if (dest > src) { - while (n) + while (size > 0) { - ((unsigned char *)dest)[n - 1] = ((unsigned char *)src)[n - 1]; - n--; + *(char *)(dest + (size - 1)) = *(char *)(src + (size - 1)); + size--; } + return (dest); } - else + while (i < size) { - while (i < n) - { - ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; - i++; - } + *(char *)(dest + i) = *(char *)(src + i); + i++; } return (dest); } diff --git a/libft/ft_memset.c b/libft/ft_memset.c index 58dacd9..d91526f 100644 --- a/libft/ft_memset.c +++ b/libft/ft_memset.c @@ -3,27 +3,24 @@ /* ::: :::::::: */ /* ft_memset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:12:24 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:31:28 by apommier ### ########.fr */ +/* Created: 2021/05/25 15:58:25 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:17:00 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_memset(void *s, int c, size_t n) +void *ft_memset(void *pointer, int value, size_t count) { - int i; - unsigned char *p; + size_t i; i = 0; - p = (unsigned char *)s; - while (n > 0) + while (i < count) { - p[i] = (unsigned char)c; + *(char *)(pointer + i) = (char)value; i++; - n--; } - return (s); + return (pointer); } diff --git a/libft/get_next_line.h b/libft/ft_putchar.c similarity index 62% rename from libft/get_next_line.h rename to libft/ft_putchar.c index 35d74d6..c76d59d 100644 --- a/libft/get_next_line.h +++ b/libft/ft_putchar.c @@ -1,19 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line.h :+: :+: :+: */ +/* ft_putchar.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/14 05:58:49 by apommier #+# #+# */ -/* Updated: 2022/01/17 21:45:15 by apommier ### ########.fr */ +/* Created: 2021/12/08 19:22:52 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:22:56 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef GET_NEXT_LINE_H -# define GET_NEXT_LINE_H +#include "libft.h" -char *get_next_line(int fd); -char *ft_strjoin(char *save, char *s2); - -#endif +void ft_putchar(int c) +{ + write(1, &c, 1); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c index 96abcb5..bbf3665 100644 --- a/libft/ft_putchar_fd.c +++ b/libft/ft_putchar_fd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_putchar_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 23:11:29 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:02:13 by apommier ### ########.fr */ +/* Created: 2021/05/22 14:44:48 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:18:07 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,6 @@ void ft_putchar_fd(char c, int fd) { - write(fd, &c, 1); + if (fd >= 0) + write(fd, &c, 1); } diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c index a663173..30d621f 100644 --- a/libft/ft_putendl_fd.c +++ b/libft/ft_putendl_fd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_putendl_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 23:22:10 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:14:52 by apommier ### ########.fr */ +/* Created: 2021/05/22 14:46:41 by sadjigui #+# #+# */ +/* Updated: 2021/05/31 17:19:23 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,15 @@ void ft_putendl_fd(char *s, int fd) { - if (s == 0) + int i; + + i = 0; + if (!s) return ; - write(fd, s, ft_strlen(s)); - ft_putchar_fd('\n', fd); + while (s[i]) + { + write(fd, &s[i], 1); + i++; + } + write(fd, "\n", 1); } diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c index eb6aebd..2af610e 100644 --- a/libft/ft_putnbr_fd.c +++ b/libft/ft_putnbr_fd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_putnbr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 23:25:57 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:31:56 by apommier ### ########.fr */ +/* Created: 2021/05/22 14:48:10 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:11:58 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,18 @@ void ft_putnbr_fd(int n, int fd) { - long nbr; - - nbr = n; - if (nbr < 0) + if (n < 0) { ft_putchar_fd('-', fd); - nbr *= -1; + if (n == -2147483648) + write(fd, "2147483648", 10); + n = -n; } - if (nbr >= 10) - ft_putnbr_fd(nbr / 10, fd); - ft_putchar_fd(nbr % 10 + '0', fd); + if (n >= 10) + { + ft_putnbr_fd(n / 10, fd); + ft_putnbr_fd(n % 10, fd); + } + else if (n >= 0 && n <= 9) + ft_putchar_fd(n + '0', fd); } diff --git a/libft/ft_lstbeforelast.c b/libft/ft_putstr.c similarity index 61% rename from libft/ft_lstbeforelast.c rename to libft/ft_putstr.c index 535dd53..2e8afe8 100644 --- a/libft/ft_lstbeforelast.c +++ b/libft/ft_putstr.c @@ -1,28 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstbeforelast.c :+: :+: :+: */ +/* ft_putstr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2021/12/09 19:16:55 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:28:22 by apommier ### ########.fr */ +/* Created: 2021/12/08 19:23:23 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:23:28 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -t_list *ft_lstbeforelast(t_list *lst) +void ft_putstr(char const *str) { - t_list *save; + int i; - save = 0; - if (!lst) - return (0); - while (lst->next) - { - save = lst; - lst = lst->next; - } - return (save); + if (!str) + return ; + i = 0; + while (str[i]) + write(1, &str[i++], 1); } diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c index 61fb28a..5aedcc2 100644 --- a/libft/ft_putstr_fd.c +++ b/libft/ft_putstr_fd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_putstr_fd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 23:15:25 by apommier #+# #+# */ -/* Updated: 2020/12/11 17:15:10 by apommier ### ########.fr */ +/* Created: 2021/05/22 14:51:56 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:41:49 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,14 @@ void ft_putstr_fd(char *s, int fd) { - if (s == 0) + int i; + + i = 0; + if (!s || fd <= 0) return ; - write(fd, s, ft_strlen(s)); + while (s[i]) + { + write(fd, &s[i], 1); + i++; + } } diff --git a/libft/ft_split.c b/libft/ft_split.c index 8302b45..937e29c 100644 --- a/libft/ft_split.c +++ b/libft/ft_split.c @@ -3,78 +3,89 @@ /* ::: :::::::: */ /* ft_split.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/07 00:54:12 by apommier #+# #+# */ -/* Updated: 2022/01/21 08:09:38 by apommier ### ########.fr */ +/* Created: 2021/05/22 14:53:23 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:10:50 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int fill_tab(char *s, char c, char **dest, size_t index) +static size_t ft_wordcount(char const *s, char c) { - int i; + size_t word; + size_t state; - i = 0; - while (s[i] != c && s[i]) - i++; - dest[index] = (char *)ft_calloc(i + 1, sizeof(char)); - if (dest[index] == 0) - return (0); - i = 0; - while (s[i] != c && s[i]) + state = 1; + word = 0; + while (*s) { - dest[index][i] = s[i]; - i++; + if (*s == c) + state = 1; + else + { + if (state == 1) + word++; + state = 0; + } + s++; } - return (1); + return (word); } -static void call(char *s, char c, char **dest, int j) +static size_t ft_word_length(char const *s, char c) { - int index; - int k; + size_t i; - k = 0; - index = 0; - while (j > index) + i = 0; + while (*s && *s != c) { - while (s[k] == c && s[k]) - k++; - if (!s[k]) - return ; - fill_tab(s + k, c, dest, index); - index++; - while (s[k] != c && s[k]) - k++; + i++; + s++; } + return (i); +} + +static char **ft_free(char **str) +{ + size_t i; + + i = 0; + while (str[i]) + { + free(str[i]); + i++; + } + free(str); + return (NULL); } char **ft_split(char const *s, char c) { - int i; - int j; - char **dest; + size_t i; + size_t j; + char **splited; - j = 0; i = 0; - if (!s) - return (0); - while (s[i] == c && s[i]) - i++; - while (s[i]) + if (s == 0) + return (NULL); + splited = (char **)malloc(sizeof(char *) * (ft_wordcount(s, c) + 1)); + if (!splited) + return (NULL); + while (ft_wordcount(s, c)) { - while (s[i] != c && s[i]) - i++; - j++; - while (s[i] == c && s[i]) - i++; + while (*s && *s == c) + s++; + splited[i] = (char *)malloc(sizeof(char) * (ft_word_length(s, c) + 1)); + if (!splited[i]) + return (ft_free(splited)); + j = 0; + while (*s != c && *s) + splited[i][j++] = *s++; + splited[i][j] = 0; + i++; } - dest = (char **)ft_calloc(sizeof(char *), (i + j)); - if (!dest) - return (0); - dest[j] = 0; - call((char *)s, c, dest, j); - return (dest); + splited[i] = 0; + return (splited); } diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c index d3699c2..6b52465 100644 --- a/libft/ft_strchr.c +++ b/libft/ft_strchr.c @@ -3,26 +3,27 @@ /* ::: :::::::: */ /* ft_strchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/12 13:57:59 by apommier #+# #+# */ -/* Updated: 2022/01/17 21:18:59 by apommier ### ########.fr */ +/* Created: 2021/05/20 13:33:40 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:40:48 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strchr(const char *s, int c) +char *ft_strchr(const char *str, int c) { - char *str; + unsigned int i; - if (!s) - return ("no s"); - str = (char *)s; - while ((*str != c) && (*str != 0)) - str++; - if (*str == c) - return ((char *)str); - else - return (0); + i = 0; + while (str[i]) + { + if (str[i] == (char)c) + return ((char *)str + i); + i++; + } + if (str[i] == (char)c) + return ((char *)str + i); + return (NULL); } diff --git a/libft/get_next_line_utils.c b/libft/ft_strcmp.c similarity index 52% rename from libft/get_next_line_utils.c rename to libft/ft_strcmp.c index a8fde1b..86a8e95 100644 --- a/libft/get_next_line_utils.c +++ b/libft/ft_strcmp.c @@ -1,41 +1,25 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line_utils.c :+: :+: :+: */ +/* ft_strcmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/14 06:05:54 by apommier #+# #+# */ -/* Updated: 2022/01/20 21:58:39 by apommier ### ########.fr */ +/* Created: 2021/12/08 19:23:07 by sadjigui #+# #+# */ +/* Updated: 2021/12/08 19:23:11 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" - -char *ft_strjoin(char *save, char *s2) +int ft_strcmp(const char *s1, const char *s2) { - char *dest; - int i; - int j; + int i; i = 0; - j = 0; - if (!save && !s2) - return (0); - dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1); - while (save && save[i]) + while (s1[i] || s2[i]) { - dest[j] = save[i]; - j++; + if (s1[i] != s2[i]) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); i++; } - i = 0; - while (s2 && s2[i]) - { - dest[j] = s2[i]; - j++; - i++; - } - dest[j] = 0; - return (dest); + return (0); } diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c index 58ead0f..faaef01 100644 --- a/libft/ft_strdup.c +++ b/libft/ft_strdup.c @@ -3,32 +3,39 @@ /* ::: :::::::: */ /* ft_strdup.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:12:43 by apommier #+# #+# */ -/* Updated: 2020/12/11 18:35:10 by apommier ### ########.fr */ +/* Created: 2021/05/22 15:05:42 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:38:05 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strdup(const char *s) +static char *ft_strcpy(char *dest, const char *src) { - int i; - char *sdup; + int i; i = 0; - while (s[i]) - i++; - sdup = malloc(sizeof(char) * (i + 1)); - if (sdup == 0) - return (0); - i = 0; - while (s[i]) + while (src[i]) { - sdup[i] = s[i]; + dest[i] = src[i]; i++; } - sdup[i] = 0; - return (sdup); + dest[i] = '\0'; + return (dest); +} + +char *ft_strdup(const char *src) +{ + size_t i; + char *copy; + + copy = NULL; + i = ft_strlen(src); + copy = malloc(sizeof(char) * i + 1); + if (!copy) + return (NULL); + copy = ft_strcpy(copy, src); + return (copy); } diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c index da55361..8b44e67 100644 --- a/libft/ft_strjoin.c +++ b/libft/ft_strjoin.c @@ -3,39 +3,51 @@ /* ::: :::::::: */ /* ft_strjoin.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */ -/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */ +/* Created: 2021/05/22 15:26:12 by sadjigui #+# #+# */ +/* Updated: 2021/05/22 15:40:20 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strjoin(char *save, char *s2) +static char *ft_strcat_j(char const *s1, char const *s2, char *dest) { - char *dest; int i; int j; i = 0; j = 0; - if (!save && !s2) - return (0); - dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1); - while (save && save[i]) + while (s1[i]) { - dest[j] = save[i]; - j++; + dest[j] = s1[i]; i++; + j++; } i = 0; - while (s2 && s2[i]) + while (s2[i]) { dest[j] = s2[i]; - j++; i++; + j++; } - dest[j] = 0; + dest[j] = '\0'; + return (dest); +} + +char *ft_strjoin(char const *s1, char const *s2) +{ + int i; + char *dest; + + dest = NULL; + if (!(s1 && s2)) + return (0); + i = ft_strlen(s1) + ft_strlen(s2); + dest = malloc(sizeof(char) * i + 1); + if (!dest) + return (NULL); + dest = ft_strcat_j(s1, s2, dest); return (dest); } diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c index 374d9d1..c2285cd 100644 --- a/libft/ft_strlcat.c +++ b/libft/ft_strlcat.c @@ -3,32 +3,35 @@ /* ::: :::::::: */ /* ft_strlcat.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:12:57 by apommier #+# #+# */ -/* Updated: 2020/12/12 15:40:58 by apommier ### ########.fr */ +/* Created: 2021/05/25 15:07:11 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 11:46:21 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -size_t ft_strlcat(char *dst, const char *src, size_t size) +size_t ft_strlcat(char *dest, const char *src, size_t size) { size_t i; - size_t k; + size_t j; + size_t res; - if (!size) - return (ft_strlen(src)); - i = 0; - k = ft_strlen(dst); - if (k >= size) - return (ft_strlen(src) + size); - while (size - k - 1 && src[i]) + j = 0; + i = ft_strlen(dest); + res = ft_strlen(src); + if (size <= i) + res = size + res; + else + res = i + res; + while (src[j] && i + 1 < size) { - dst[k + i] = src[i]; + dest[i] = src[j]; i++; - size--; + j++; } - dst[k + i] = 0; - return (k + ft_strlen(src)); + if (i < size) + dest[i] = '\0'; + return (res); } diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c index 05af28a..9d3a5e2 100644 --- a/libft/ft_strlcpy.c +++ b/libft/ft_strlcpy.c @@ -3,34 +3,32 @@ /* ::: :::::::: */ /* ft_strlcpy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:13:07 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:38:48 by apommier ### ########.fr */ +/* Created: 2021/05/22 15:43:29 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:34:12 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -size_t ft_strlcpy(char *dst, const char *src, size_t size) +size_t ft_strlcpy(char *dest, const char *src, size_t size) { - int i; - int j; + size_t i; - j = 0; - i = 0; - if (!src || !dst) - return (0); - while (src[i]) - i++; if (!size) - return (i); - while (size - 1 && src[j] != 0) + return (ft_strlen(src)); + i = 0; + if (!dest && !src) + return (0); + if (size == 0) + return (0); + while (src[i] && i < size - 1) { - dst[j] = src[j]; - j++; - size--; + dest[i] = src[i]; + i++; } - dst[j] = 0; - return (i); + if (size > 0) + dest[i] = '\0'; + return (ft_strlen(src)); } diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c index 5ac9d2b..3c65fee 100644 --- a/libft/ft_strlen.c +++ b/libft/ft_strlen.c @@ -3,23 +3,21 @@ /* ::: :::::::: */ /* ft_strlen.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:13:19 by apommier #+# #+# */ -/* Updated: 2022/01/17 21:12:05 by apommier ### ########.fr */ +/* Created: 2021/06/03 12:31:43 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 12:32:28 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -size_t ft_strlen(const char *s) +size_t ft_strlen(const char *str) { - size_t i; + int i; i = 0; - if (!s) - return (0); - while (s[i] != 0) + while (str[i]) i++; return (i); } diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c index a983137..e824e7b 100644 --- a/libft/ft_strmapi.c +++ b/libft/ft_strmapi.c @@ -3,30 +3,36 @@ /* ::: :::::::: */ /* ft_strmapi.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/08 19:03:09 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:39:05 by apommier ### ########.fr */ +/* Created: 2021/05/25 16:09:10 by sadjigui #+# #+# */ +/* Updated: 2021/06/03 16:14:11 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) { - char *dest; - int i; + unsigned int i; + char *dest; + int a; if (!s) return (0); + a = ft_strlen(s); + dest = NULL; i = 0; - dest = (char *)ft_calloc(ft_strlen(s) + 1, sizeof(char)); + if (!s || !f) + return (NULL); + dest = malloc(sizeof(char) * a + 1); if (!dest) - return (0); + return (NULL); while (s[i]) { - dest[i] = f(i, s[i]); + dest[i] = (*f)(i, (char)s[i]); i++; } + dest[i] = 0; return (dest); } diff --git a/libft/ft_strncmp.c b/libft/ft_strncmp.c index 0951207..e92aca5 100644 --- a/libft/ft_strncmp.c +++ b/libft/ft_strncmp.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_strncmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:13:31 by apommier #+# #+# */ -/* Updated: 2022/02/14 00:27:01 by apommier ### ########.fr */ +/* Created: 2021/06/03 12:29:18 by sadjigui #+# #+# */ +/* Updated: 2021/06/09 12:28:28 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,30 +14,13 @@ int ft_strncmp(const char *s1, const char *s2, size_t n) { - int i; + size_t i; i = 0; - while (n && (s1[i] || s2[i])) + while ((s1[i] || s2[i]) && i < n) { - if (s1[i] != s2[i]) - return ((unsigned char)s1[i] - (unsigned char)s2[i]); - n--; - i++; - } - return (0); -} - -int ft_strcmp(const char *s1, const char *s2) -{ - int i; - - i = 0; - if (!s1 || !s2) - return (1); - while (s1[i] || s2[i]) - { - if (s1[i] != s2[i]) - return ((unsigned char)s1[i] - (unsigned char)s2[i]); + if (!(s1[i] == s2[i])) + return ((unsigned char)s1[i] - s2[i]); i++; } return (0); diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c index e993e0d..c9754ad 100644 --- a/libft/ft_strnstr.c +++ b/libft/ft_strnstr.c @@ -3,38 +3,36 @@ /* ::: :::::::: */ /* ft_strnstr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:13:42 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:39:28 by apommier ### ########.fr */ +/* Created: 2021/05/25 14:58:49 by sadjigui #+# #+# */ +/* Updated: 2021/05/25 16:30:00 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strnstr(const char *big, const char *little, size_t len) +char *ft_strnstr(const char *str, const char *to_find, size_t size) { - int i; - int j; + size_t i; + size_t j; i = 0; - if (!little[i]) - return ((char *)big); - if (!little[i]) - return ((char *)big); - while (big[i] && len - i) + j = 0; + if (*to_find == '\0') + return ((char *)str); + while (i < size && str[i]) { - j = 0; - if (little[j] != big[i]) - i++; + if ((i + j) > size) + return (NULL); + if (to_find[j] == '\0') + return ((char *)&str[i]); + if (str[i + j] == to_find[j]) + j++; else { - while (little[j] == big[i + j] && little[j] && len - i - j) - j++; - if (little[j] == 0) - return ((char *)&big[i]); - else - i++; + j = 0; + i++; } } return (0); diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c index 0d11e30..49d6740 100644 --- a/libft/ft_strrchr.c +++ b/libft/ft_strrchr.c @@ -3,33 +3,27 @@ /* ::: :::::::: */ /* ft_strrchr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:13:52 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:39:37 by apommier ### ########.fr */ +/* Created: 2021/05/20 13:38:42 by sadjigui #+# #+# */ +/* Updated: 2021/06/09 12:59:30 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strrchr(const char *s, int c) +char *ft_strrchr(const char *str, int c) { - char *str; - char *last; + int i; - str = (char *)s; - if (c == 0) + i = ft_strlen(str) - 1; + if (c == '\0') + return ((char *)&(str[i + 1])); + while (i >= 0) { - while (*str) - str++; - return (str); + if (str[i] == (char)c) + return ((char *)&(str[i])); + i--; } - last = 0; - while (*str) - { - if (*str == c) - last = str; - str++; - } - return (last); + return (0); } diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c index 3930252..cdac659 100644 --- a/libft/ft_strtrim.c +++ b/libft/ft_strtrim.c @@ -3,70 +3,28 @@ /* ::: :::::::: */ /* ft_strtrim.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 23:52:05 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:40:17 by apommier ### ########.fr */ +/* Created: 2021/06/03 12:17:01 by sadjigui #+# #+# */ +/* Updated: 2021/06/09 13:45:13 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int is_set(char const *set, char c) -{ - int i; - - i = 0; - while (set[i]) - { - if (set[i] == c) - return (1); - i++; - } - return (0); -} - -static int len_dest(const char *s1, char const *set) -{ - int len; - int j; - int i; - - i = 0; - j = 0; - len = ft_strlen(s1); - while (is_set(set, s1[i])) - i++; - while (is_set(set, s1[len - j - 1])) - j++; - len = len - j - i; - if (len < 0) - len = 0; - return (len); -} - char *ft_strtrim(char const *s1, char const *set) { - int j; - int i; - int len; - char *dest; + char *str; + size_t j; - i = 0; - j = 0; - if (!s1) - return (0); - len = len_dest(s1, set); - dest = ft_calloc(len + 1, 1); - if (!dest) - return (0); - while (is_set(set, s1[i])) - i++; - while (s1[i] && len - j && len > 0) - { - dest[j] = s1[i]; - i++; - j++; - } - return (dest); + if (!s1 || !set) + return (NULL); + while (*s1 && ft_strchr(set, *s1)) + s1++; + j = ft_strlen((char *)s1); + while (j && ft_strchr(set, s1[j])) + j--; + ++j; + str = ft_substr(s1, 0, j); + return (str); } diff --git a/libft/ft_substr.c b/libft/ft_substr.c index 7e4d95b..e6f2384 100644 --- a/libft/ft_substr.c +++ b/libft/ft_substr.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_substr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 22:54:40 by apommier #+# #+# */ -/* Updated: 2020/12/16 16:45:34 by apommier ### ########.fr */ +/* Created: 2021/06/03 12:11:04 by sadjigui #+# #+# */ +/* Updated: 2021/06/07 11:33:45 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,26 +14,25 @@ char *ft_substr(char const *s, unsigned int start, size_t len) { - char *dest; - unsigned int i; + char *str; + size_t i; + size_t j; + i = -1; if (!s) - return (0); - i = 0; - dest = malloc(1 * len + 1); - if (dest == 0) - return (0); - if (start > ft_strlen(s) || len == 0) + return (NULL); + j = ft_strlen(s); + if (!len || j <= start) + return (ft_strdup("")); + str = (char *)malloc(sizeof(char) * len + 1); + if (!str) + return (NULL); + j = 0; + while (s[++i] && j < len) { - dest[i] = 0; - return (dest); + if (i >= start) + str[j++] = s[i]; } - while (len && s[i + start]) - { - dest[i] = s[i + start]; - len--; - i++; - } - dest[i] = 0; - return (dest); + str[j] = '\0'; + return (str); } diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c index d375233..3c49c67 100644 --- a/libft/ft_tolower.c +++ b/libft/ft_tolower.c @@ -3,18 +3,18 @@ /* ::: :::::::: */ /* ft_tolower.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:14:05 by apommier #+# #+# */ -/* Updated: 2020/11/29 17:20:51 by apommier ### ########.fr */ +/* Created: 2021/05/25 16:34:58 by sadjigui #+# #+# */ +/* Updated: 2021/05/25 16:35:45 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_tolower(int c) +int ft_tolower(int i) { - if (c >= 65 && c <= 90) - c = c + 32; - return (c); + if (i >= 'A' && i <= 'Z') + i += 32; + return (i); } diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c index e8c8d89..d44bf55 100644 --- a/libft/ft_toupper.c +++ b/libft/ft_toupper.c @@ -3,18 +3,18 @@ /* ::: :::::::: */ /* ft_toupper.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/11/29 00:14:15 by apommier #+# #+# */ -/* Updated: 2020/11/29 17:21:12 by apommier ### ########.fr */ +/* Created: 2021/05/25 16:36:03 by sadjigui #+# #+# */ +/* Updated: 2021/05/25 16:36:32 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int ft_toupper(int c) +int ft_toupper(int i) { - if (c >= 'a' && c <= 'z') - c = c - 32; - return (c); + if (i >= 'a' && i <= 'z') + i -= 32; + return (i); } diff --git a/libft/get_next_line.c b/libft/get_next_line.c deleted file mode 100644 index 24ca95c..0000000 --- a/libft/get_next_line.c +++ /dev/null @@ -1,100 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: kinou +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/23 07:55:17 by kinou #+# #+# */ -/* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_free(char *save, int *end) -{ - if (!*end) - { - free(save); - free(end); - return (0); - } - free(end); - return (save); -} - -char *set_line(char *line, char *save) -{ - int i; - - i = 0; - line = ft_strjoin(save, 0); - while (line[i] && line[i] != '\n') - i++; - if (line[i] == '\n') - { - while (line[i++]) - line[i] = '\0'; - } - return (line); -} - -char *set_save(char *save) -{ - char *delete; - int i; - - i = 0; - delete = save; - while (save[i] && save[i] != '\n') - i++; - if (save[i] != '\n') - i = 0; - save = ft_strjoin((save + i + 1), 0); - free(delete); - return (save); -} - -char *next_line(char *save, int *end, int fd) -{ - char *delete; - char *dest; - - while (!ft_strchr(save, '\n') && *end > 0) - { - dest = ft_calloc(1, 1 + 1); - *end = read(fd, dest, 1); - delete = save; - save = ft_strjoin(save, dest); - free(delete); - free(dest); - } - return (save); -} - -char *get_next_line(int fd) -{ - static char *save = NULL; - int *end; - char *line; - - line = 0; - if (fd < 0) - return (0); - end = malloc(sizeof(int *)); - *end = 1; - if (save == NULL) - save = ft_calloc(1, 1); - save = next_line(save, end, fd); - line = set_line(line, save); - if (ft_strlen(line) > 0) - { - save = set_save(save); - save = ft_free(save, end); - return (line); - } - free(line); - save = ft_free(save, end); - return (0); -} diff --git a/libft/libft.h b/libft/libft.h index 2aa8847..e7555ae 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -3,75 +3,72 @@ /* ::: :::::::: */ /* libft.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: apommier +#+ +:+ +#+ */ +/* By: sadjigui +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ -/* Updated: 2022/02/14 00:27:42 by apommier ### ########.fr */ +/* Created: 2021/06/07 12:34:41 by sadjigui #+# #+# */ +/* Updated: 2022/02/26 16:46:45 by sadjigui ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef LIBFT_H # define LIBFT_H -# include # include -# include "get_next_line.h" +# include -typedef struct t_slist +typedef struct s_list { - void *nbr; - int index; - int swap; - struct t_slist *next; -} t_list; + void *content; + struct s_list *next; +} t_list; -void *ft_memset(void *s, int c, size_t n); -void ft_bzero(void *s, size_t n); -void *ft_memcpy(void *dest, const void *src, size_t n); -void *ft_memccpy(void *dest, const void *src, int c, size_t n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memchr(const void *s, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -size_t ft_strlen(const char *s); -int ft_isalpha(int c); -int ft_isdigit(int c); -int ft_isalnum(int c); -int ft_isascii(int c); -int ft_isprint(int c); -int ft_toupper(int c); -int ft_tolower(int c); -char *ft_strchr(const char *s, int c); -char *ft_strrchr(const char *s, int c); -int ft_strcmp(const char *s1, const char *s2); -int ft_strncmp(const char *s1, const char *s2, size_t n); -size_t ft_strlcpy(char *dst, const char *src, size_t size); -size_t ft_strlcat(char *dst, const char *src, size_t size); -char *ft_strjoin(char *save, char *s2); -char *ft_strnstr(const char *big, const char *little, size_t len); -long ft_atoi(const char *nptr); -void *ft_calloc(size_t nmenb, size_t size); -char *ft_strdup(const char *s); +void ft_putchar(int c); +void ft_putstr(char const *str); +int ft_strcmp(const char *s1, const char *s2); +long ft_atoi(const char *str); +void ft_bzero(void *str, size_t n); +void *ft_calloc(size_t count, size_t size); +int ft_isalnum(int i); +int ft_isalpha(int i); +int ft_isascii(int i); +int ft_isdigit(int i); +int ft_isprint(int i); +char *ft_itoa(int n); +void *ft_memccpy(void *dest, const void *src, int ch, size_t maxSize); +void *ft_memcpy(void *destination, const void *source, size_t size); +void *ft_memmove(void *dest, const void *src, size_t size); +void *ft_memchr(const void *mem, int ch, size_t size); +void *ft_memset(void *pointer, int value, size_t count); +int ft_memcmp(const void *p1, const void *p2, size_t size); +void ft_putchar_fd(char c, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putstr_fd(char *s, int fd); +char **ft_split(char const *s, char c); +char *ft_strdup(const char *src); +char *ft_strjoin(char const *s1, char const *s2); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlen(const char *str); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strtrim(char const *s1, char const *set); +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_tolower(int i); +int ft_toupper(int i); +char *ft_strchr(const char *str, int c); +char *ft_strrchr(const char *str, int c); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlcat(char *dest, const char *src, size_t size); +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); +char *ft_strnstr(const char *str, const char *to_find, size_t size); -char *ft_substr(char const *s, unsigned int start, size_t len); -char *ft_strtrim(char const *s1, char const *set); -char **ft_split(char const *s, char c); -char *ft_itoa(int n); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); -void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); - -t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **alst, t_list *new); -int ft_lstsize(t_list *lst); -t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **alst, t_list *new); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), - void (*del)(void *)); -t_list *ft_lstbeforelast(t_list *lst); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **alst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); #endif diff --git a/srcs/exit.c b/srcs/exit.c new file mode 100644 index 0000000..8872c18 --- /dev/null +++ b/srcs/exit.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/28 18:00:05 by sadjigui #+# #+# */ +/* Updated: 2022/03/01 18:55:10 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/mini.h" + +int find_len(char *input, int i) +{ + int j; + + j = i; + while (input[j]) + j++; + return (j); +} + +void ft_export(char *input, t_struct *data) +{ + (void)data; + int i; + int j; + char *variable; + + i = 6; + j = find_len(input, i); + variable = ft_substr(input, i, j); + if (j == i) + printf("check\n"); + else if (j > i) + ft_export_variable(data, variable); +} + +void free_char_tab(char **libre) +{ + int i; + + i = 0; + while (libre[i]) + { + free(libre[i]); + i++; + } + free(libre); +} + +void ft_exit(t_struct *data) +{ + int i; + + i = 0; + while (data->envy[i]) + { + free(data->envy[i]); + i++; + } + free(data->envy); + exit(0); +} diff --git a/srcs/export_utils.c b/srcs/export_utils.c new file mode 100644 index 0000000..2a6206b --- /dev/null +++ b/srcs/export_utils.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* export_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/28 23:26:59 by sadjigui #+# #+# */ +/* Updated: 2022/03/01 18:33:45 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/mini.h" + +int check_variable(char *variable) +{ + int i; + + i = 0; + if (!ft_isalpha(variable[i])) + return(0); + i++; + while(variable[i]) + { + printf("%c\n", variable[i]); + if(!ft_isalnum(variable[i])) + return(0); + i++; + } + return(1); +} + +char *define_value(char *value) +{ + int i; + char *new_value; + + i = 0; + if (value[i] == ' ') + { + new_value = ft_strdup(""); + } + else + { + printf("%s\n", value); + while (value[i]) + { + printf("%d\n", i); + i++; + } + new_value = ft_substr(value, 0, i); + } + return (new_value); +} + +char *check_value(char *value) +{ + int i; + char *new_value; + + i = 0; + if (value[0] == '"') + { + new_value = NULL; + printf("jojo\n"); + } + else + new_value = define_value(value); + return(new_value); +} + +void ft_export_variable(t_struct *data, char *variable) +{ + char *v_v[2]; + int i; + char *tmp; + + i = 0; + while (variable[i] != '=') + i++; + v_v[0] = ft_substr(variable, 1, i - 1); + i++; + v_v[1] = ft_substr(variable, i, find_len(variable, i)); + if (check_variable(v_v[0]) == 1) + { + tmp = ft_strdup(v_v[1]); + free(v_v[1]); + v_v[1] = check_value(tmp); + register_env(data, v_v); + } +} diff --git a/srcs/init.c b/srcs/init.c new file mode 100644 index 0000000..bcf630f --- /dev/null +++ b/srcs/init.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */ +/* Updated: 2022/03/01 18:39:20 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/mini.h" + +void join_variable(t_struct *data, char **v_v, int size) +{ + char *str; + char *tmp; + + str = ft_strjoin(v_v[0], "="); + tmp = ft_strdup(str); + free(str); + str = ft_strjoin(tmp,v_v[1]); + data->envy[size] = ft_strdup(str); + free(str); + free(tmp); +} + +void register_env(t_struct *data, char **variable) +{ + int size; + char **tmp = NULL; + + // tmp = NULL; + size = 0; + while (data->envy[size]) + size++; + tmp = malloc(sizeof(char *) * size); + if(!tmp) + ft_exit(data); + size = 0; + while (data->envy[size]) + { + tmp[size] = ft_strdup(data->envy[size]); + printf("ici\n"); + size++; + } + free_char_tab(data->envy); + data->envy = malloc(sizeof(char *) * size + 1); + if (!data->envy) + ft_exit(data); + size = 0; + while (tmp[size]) + { + data->envy[size] = ft_strdup(tmp[size]); + size++; + } + join_variable(data, variable, size); +} + +void ft_env(t_struct *data, char **env) +{ + int i; + + i = 0; + while (env[i]) + i++; + data->envy = malloc(sizeof(char *) * i); + i = 0; + while (env[i]) + { + data->envy[i] = ft_strdup(env[i]); + i++; + } + +} + +void init_struct(t_struct *data, char **env) +{ + ft_env(data, env); +} diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..e574289 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/26 16:00:41 by sadjigui #+# #+# */ +/* Updated: 2022/03/06 15:31:18 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/mini.h" + +void red() +{ + printf("\033[1;31m"); +} + +void normal() +{ + printf("\033[0m"); +} + +void boot_shell(t_struct *data) +{ + char *input; + while(1) + { + red(); + input = readline("Minishell> "); + lexer(input, data); + add_history(input); + normal(); + } +} + +int main(int ac, char **av, char **env) +{ + (void)ac; + (void)av; + t_struct data; + + init_struct(&data, env); + boot_shell(&data); +} diff --git a/srcs/parser.c b/srcs/parser.c new file mode 100644 index 0000000..ff9158e --- /dev/null +++ b/srcs/parser.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: sadjigui +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/28 17:12:52 by sadjigui #+# #+# */ +/* Updated: 2022/03/06 15:33:06 by sadjigui ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/mini.h" + +void print_env(t_struct *data) +{ + int i; + + i = 0; + while (data->envy[i]) + { + printf("%s\n", data->envy[i]); + i++; + } + // printf("ici\n"); +} + +void lexer(char *input, t_struct *data) +{ + if (ft_strcmp(input, "exit") == 0) + ft_exit(data); + if (ft_strcmp(input, "env") == 0) + print_env(data); + if (ft_strncmp(input, "export", ft_strlen("export")) == 0) + ft_export(input, data); +}