From f3d243b523c3457e597540c904571b81b963729d Mon Sep 17 00:00:00 2001 From: agras Date: Mon, 8 Apr 2024 18:05:15 +0200 Subject: [PATCH] files added --- .gitignore | 52 ++++++++++++ Makefile | 57 ++++++++++++++ includes/client/client.h | 25 ++++++ includes/server/server.h | 29 +++++++ libft/Makefile | 99 +++++++++++++++++++++++ libft/includes/libft.h | 105 +++++++++++++++++++++++++ libft/src/conv/ft_atoi.c | 34 ++++++++ libft/src/conv/ft_itoa.c | 64 +++++++++++++++ libft/src/free/free_2d_tab.c | 31 ++++++++ libft/src/gnl/get_next_line.c | 76 ++++++++++++++++++ libft/src/gnl/get_next_line.h | 31 ++++++++ libft/src/gnl/get_next_line_utils.c | 81 +++++++++++++++++++ libft/src/is/ft_isalnum.c | 20 +++++ libft/src/is/ft_isalpha.c | 18 +++++ libft/src/is/ft_isascii.c | 18 +++++ libft/src/is/ft_isdigit.c | 18 +++++ libft/src/is/ft_isprint.c | 18 +++++ libft/src/lst/ft_lstadd_back.c | 28 +++++++ libft/src/lst/ft_lstadd_front.c | 26 ++++++ libft/src/lst/ft_lstclear.c | 30 +++++++ libft/src/lst/ft_lstdelone.c | 21 +++++ libft/src/lst/ft_lstiter.c | 24 ++++++ libft/src/lst/ft_lstlast.c | 22 ++++++ libft/src/lst/ft_lstmap.c | 48 +++++++++++ libft/src/lst/ft_lstnew.c | 25 ++++++ libft/src/lst/ft_lstsize.c | 26 ++++++ libft/src/mem/ft_bzero.c | 27 +++++++ libft/src/mem/ft_calloc.c | 35 +++++++++ libft/src/mem/ft_memchr.c | 27 +++++++ libft/src/mem/ft_memcmp.c | 31 ++++++++ libft/src/mem/ft_memcpy.c | 30 +++++++ libft/src/mem/ft_memmove.c | 35 +++++++++ libft/src/mem/ft_memset.c | 28 +++++++ libft/src/print/ft_print_2d_tab_char.c | 26 ++++++ libft/src/print/ft_putchar_fd.c | 19 +++++ libft/src/print/ft_putendl_fd.c | 32 ++++++++ libft/src/print/ft_putnbr_fd.c | 33 ++++++++ libft/src/print/ft_putstr_fd.c | 29 +++++++ libft/src/str/ft_cstr.c | 28 +++++++ libft/src/str/ft_split.c | 95 ++++++++++++++++++++++ libft/src/str/ft_strcat_malloc.c | 44 +++++++++++ libft/src/str/ft_strchr.c | 25 ++++++ libft/src/str/ft_strcmp.c | 26 ++++++ libft/src/str/ft_strdup.c | 41 ++++++++++ libft/src/str/ft_striteri.c | 27 +++++++ libft/src/str/ft_strjoin.c | 49 ++++++++++++ libft/src/str/ft_strlcat.c | 46 +++++++++++ libft/src/str/ft_strlcpy.c | 40 ++++++++++ libft/src/str/ft_strlen.c | 25 ++++++ libft/src/str/ft_strmapi.c | 43 ++++++++++ libft/src/str/ft_strncmp.c | 29 +++++++ libft/src/str/ft_strnstr.c | 48 +++++++++++ libft/src/str/ft_strrchr.c | 39 +++++++++ libft/src/str/ft_strtrim.c | 75 ++++++++++++++++++ libft/src/str/ft_substr.c | 57 ++++++++++++++ libft/src/str/ft_tolower.c | 18 +++++ libft/src/str/ft_toupper.c | 18 +++++ libft/src/str/strcmp_occurence.c | 37 +++++++++ src/client/client.c | 56 +++++++++++++ src/server/bit_management.c | 44 +++++++++++ src/server/server.c | 83 +++++++++++++++++++ 61 files changed, 2371 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 includes/client/client.h create mode 100644 includes/server/server.h create mode 100644 libft/Makefile create mode 100644 libft/includes/libft.h create mode 100644 libft/src/conv/ft_atoi.c create mode 100644 libft/src/conv/ft_itoa.c create mode 100644 libft/src/free/free_2d_tab.c create mode 100644 libft/src/gnl/get_next_line.c create mode 100644 libft/src/gnl/get_next_line.h create mode 100644 libft/src/gnl/get_next_line_utils.c create mode 100644 libft/src/is/ft_isalnum.c create mode 100644 libft/src/is/ft_isalpha.c create mode 100644 libft/src/is/ft_isascii.c create mode 100644 libft/src/is/ft_isdigit.c create mode 100644 libft/src/is/ft_isprint.c create mode 100644 libft/src/lst/ft_lstadd_back.c create mode 100644 libft/src/lst/ft_lstadd_front.c create mode 100644 libft/src/lst/ft_lstclear.c create mode 100644 libft/src/lst/ft_lstdelone.c create mode 100644 libft/src/lst/ft_lstiter.c create mode 100644 libft/src/lst/ft_lstlast.c create mode 100644 libft/src/lst/ft_lstmap.c create mode 100644 libft/src/lst/ft_lstnew.c create mode 100644 libft/src/lst/ft_lstsize.c create mode 100644 libft/src/mem/ft_bzero.c create mode 100644 libft/src/mem/ft_calloc.c create mode 100644 libft/src/mem/ft_memchr.c create mode 100644 libft/src/mem/ft_memcmp.c create mode 100644 libft/src/mem/ft_memcpy.c create mode 100644 libft/src/mem/ft_memmove.c create mode 100644 libft/src/mem/ft_memset.c create mode 100644 libft/src/print/ft_print_2d_tab_char.c create mode 100644 libft/src/print/ft_putchar_fd.c create mode 100644 libft/src/print/ft_putendl_fd.c create mode 100644 libft/src/print/ft_putnbr_fd.c create mode 100644 libft/src/print/ft_putstr_fd.c create mode 100644 libft/src/str/ft_cstr.c create mode 100644 libft/src/str/ft_split.c create mode 100644 libft/src/str/ft_strcat_malloc.c create mode 100644 libft/src/str/ft_strchr.c create mode 100644 libft/src/str/ft_strcmp.c create mode 100644 libft/src/str/ft_strdup.c create mode 100644 libft/src/str/ft_striteri.c create mode 100644 libft/src/str/ft_strjoin.c create mode 100644 libft/src/str/ft_strlcat.c create mode 100644 libft/src/str/ft_strlcpy.c create mode 100644 libft/src/str/ft_strlen.c create mode 100644 libft/src/str/ft_strmapi.c create mode 100644 libft/src/str/ft_strncmp.c create mode 100644 libft/src/str/ft_strnstr.c create mode 100644 libft/src/str/ft_strrchr.c create mode 100644 libft/src/str/ft_strtrim.c create mode 100644 libft/src/str/ft_substr.c create mode 100644 libft/src/str/ft_tolower.c create mode 100644 libft/src/str/ft_toupper.c create mode 100644 libft/src/str/strcmp_occurence.c create mode 100644 src/client/client.c create mode 100644 src/server/bit_management.c create mode 100644 src/server/server.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b2b8965 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: agras +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/03/01 10:20:34 by agras #+# #+# # +# Updated: 2022/04/12 21:33:47 by agras ### ########.fr # +# # +# **************************************************************************** # + +INCLUDES_CLIENT = -I includes/server/ -I libft/includes/ + +INCLUDES_SERVER = -I includes/client/ -I libft/includes/ + +SERVER_SRCS = src/server/server.c\ + src/server/bit_management.c + +CLIENT_SRCS = src/client/client.c + +SERVER_OBJS = $(SERVER_SRCS:.c=.o) + +CLIENT_OBJS = $(CLIENT_SRCS:.c=.o) + +CC = gcc +RM = rm -f +CFLAGS = -Wall -Wextra -Werror +#CFLAGS = + + +all: lib server client + +server: $(SERVER_OBJS) + $(CC) $(CFLAGS) $(SERVER_OBJS) ${INCLUDES_SERVER} -L./libft -lft -o server + +client: $(CLIENT_OBJS) + $(CC) $(CFLAGS) $(CLIENT_OBJS) ${INCLUDES_CLIENT} -L./libft -lft -o client + +lib: + @ make --no-print-directory -sC ./libft + +clean: + $(RM) $(SERVER_OBJS) + $(RM) $(CLIENT_OBJS) + make clean -C libft + +fclean: + $(RM) $(SERVER_OBJS) + $(RM) server + $(RM) $(CLIENT_OBJS) + $(RM) client + make fclean -C libft + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/includes/client/client.h b/includes/client/client.h new file mode 100644 index 0000000..04c3ac5 --- /dev/null +++ b/includes/client/client.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/12 22:05:50 by agras #+# #+# */ +/* Updated: 2022/04/12 19:19:24 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CLIENT_H +# define CLIENT_H + +# include +# include +# include +# include +# include +# include + +# include "../../libft/includes/libft.h" + +#endif \ No newline at end of file diff --git a/includes/server/server.h b/includes/server/server.h new file mode 100644 index 0000000..8019267 --- /dev/null +++ b/includes/server/server.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* server.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/12 21:59:44 by agras #+# #+# */ +/* Updated: 2022/04/12 21:32:06 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SERVER_H +# define SERVER_H + +# include +# include +# include +# include +# include +# include + +# include "../../libft/includes/libft.h" + +void set_bit(char *byte, int shift); +void clear_bit(char *byte, int shift); +void print_byte_bin(unsigned char byte); + +#endif \ No newline at end of file diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..28b2633 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,99 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: agras +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2021/11/01 01:48:03 by agras #+# #+# # +# Updated: 2022/02/09 17:01:24 by agras ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a +SRCS = ${IS}ft_isalpha.c\ + ${IS}ft_isdigit.c\ + ${IS}ft_isalnum.c\ + ${IS}ft_isascii.c\ + ${IS}ft_isprint.c\ + ${MEM}ft_memset.c\ + ${MEM}ft_bzero.c\ + ${MEM}ft_memcpy.c\ + ${MEM}ft_memcmp.c\ + ${MEM}ft_memmove.c\ + ${MEM}ft_memchr.c\ + ${MEM}ft_calloc.c\ + ${STR}ft_strlen.c\ + ${STR}ft_substr.c\ + ${STR}ft_strjoin.c\ + ${STR}ft_strlcpy.c\ + ${STR}ft_strlcat.c\ + ${STR}ft_toupper.c\ + ${STR}ft_tolower.c\ + ${STR}ft_strchr.c\ + ${STR}ft_strrchr.c\ + ${STR}ft_strncmp.c\ + ${STR}ft_strnstr.c\ + ${STR}ft_strdup.c\ + ${STR}ft_strtrim.c\ + ${STR}ft_strmapi.c\ + ${STR}ft_striteri.c\ + ${STR}ft_cstr.c\ + ${STR}ft_split.c\ + ${STR}ft_strcmp.c\ + ${STR}strcmp_occurence.c\ + ${STR}ft_strcat_malloc.c\ + ${PRINT}ft_putstr_fd.c\ + ${PRINT}ft_putnbr_fd.c\ + ${PRINT}ft_putendl_fd.c\ + ${PRINT}ft_putchar_fd.c\ + ${PRINT}ft_print_2d_tab_char.c\ + ${CONV}ft_itoa.c\ + ${CONV}ft_atoi.c\ + ${LST}ft_lstsize.c\ + ${LST}ft_lstnew.c\ + ${LST}ft_lstmap.c\ + ${LST}ft_lstlast.c\ + ${LST}ft_lstiter.c\ + ${LST}ft_lstdelone.c\ + ${LST}ft_lstclear.c\ + ${LST}ft_lstadd_front.c\ + ${LST}ft_lstadd_back.c\ + ${GNL}get_next_line_utils.c\ + ${GNL}get_next_line.c\ + ${FREE}free_2d_tab.c + +GNL = src/gnl/ +IS = src/is/ +CONV = src/conv/ +MEM = src/mem/ +PRINT = src/print/ +LST = src/lst/ +STR = src/str/ +FREE = src/free/ + +INCLUDES = -I includes +OBJS = ${SRCS:.c=.o} +CC = gcc +CFLAGS = -Wall -Wextra -Werror +RM = rm -f + +.c.o: + ${CC} ${CFLAGS} ${INCLUDES} -c $< -o ${<:.c=.o} + +${NAME}: ${OBJS} + ar rcs ${NAME} ${OBJS} + +# bonus: ${OBJS} ${OBJSB} +# ar rcs ${NAME} ${OBJSB} + +all: ${NAME} + +clean: + ${RM} ${OBJS} + +fclean: clean + ${RM} ${NAME} + +re: fclean all + diff --git a/libft/includes/libft.h b/libft/includes/libft.h new file mode 100644 index 0000000..5146260 --- /dev/null +++ b/libft/includes/libft.h @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 14:20:16 by agras #+# #+# */ +/* Updated: 2022/02/09 16:44:02 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include +# include +# include + +# include "../src/gnl/get_next_line.h" + +// ----------- STRUCTS + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +// ------------ PROTOTYPES + +// IS + +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); + +// MEM + +void *ft_memset(void *s, int c, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +int ft_memcmp(const void *s1, const void *s2, 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); +void *ft_calloc(size_t nmemb, size_t size); +void ft_bzero(void *s, size_t n); + +// STR + +size_t ft_strlen(const char *s); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +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_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); +int ft_toupper(int c); +int ft_tolower(int c); +char *ft_strdup(const char *s); +char *ft_strtrim(char const *s1, char const *set); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +char **ft_split(char const *s, char c); +char *ft_cstr(char *str, char c); +int ft_strcmp(const char *s1, const char *s2); +int strcmp_occurence(const char *s1, const char *s2); +char *ft_strcat_malloc(const char *dst, const char *src); + +// PRINT + +void ft_putstr_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putchar_fd(char c, int fd); +void ft_print_2d_tab_char(char **tab); + +// CONV + +int ft_atoi(const char *nptr); +char *ft_itoa(int n); + +// LST + +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstclear(t_list **lst, void (*del)(void*)); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lstnew(void *content); +int ft_lstsize(t_list *lst); + +// GNL + +char *get_next_line(int fd, int mod); + +// FREE + +void free_2d_tab(void **tab); + +#endif diff --git a/libft/src/conv/ft_atoi.c b/libft/src/conv/ft_atoi.c new file mode 100644 index 0000000..cd4cfd6 --- /dev/null +++ b/libft/src/conv/ft_atoi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:03:24 by agras #+# #+# */ +/* Updated: 2021/12/01 19:10:52 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_atoi(const char *nptr) +{ + int nb; + int is_neg; + + is_neg = 1; + nb = 0; + while (*nptr && (*nptr == ' ' || (*nptr >= 9 && *nptr <= 13))) + nptr++; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + is_neg = -is_neg; + nptr++; + } + while (*nptr && *nptr >= '0' && *nptr <= '9') + { + nb = nb * 10 + (*nptr - '0'); + nptr++; + } + return (nb * is_neg); +} diff --git a/libft/src/conv/ft_itoa.c b/libft/src/conv/ft_itoa.c new file mode 100644 index 0000000..0c3c979 --- /dev/null +++ b/libft/src/conv/ft_itoa.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 17:39:35 by agras #+# #+# */ +/* Updated: 2021/12/10 16:52:47 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static int get_nb_len(long n) +{ + int len; + + len = 0; + if (n == 0) + return (1); + while (n > 0) + { + n /= 10; + len++; + } + return (len); +} + +static void cpy_nb_in_array(long n, char *arr, int len) +{ + while (len >= 0) + { + arr[len] = (n % 10) + '0'; + n /= 10; + len--; + } +} + +char *ft_itoa(int n) +{ + char *str; + int is_neg; + long nb; + int len; + + nb = n; + is_neg = 0; + if (nb < 0) + { + nb = -nb; + is_neg = 1; + } + len = get_nb_len(nb); + str = malloc(sizeof(char) * (len + is_neg + 1)); + if (!str) + return (NULL); + if (n < 0) + str[0] = '-'; + cpy_nb_in_array(nb, str + is_neg, len - 1); + str[len + is_neg] = '\0'; + return (str); +} diff --git a/libft/src/free/free_2d_tab.c b/libft/src/free/free_2d_tab.c new file mode 100644 index 0000000..ca8c6c3 --- /dev/null +++ b/libft/src/free/free_2d_tab.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_2d_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/29 14:23:26 by agras #+# #+# */ +/* Updated: 2022/02/10 16:24:55 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// !! BE careful, void ** tab sent in params must have his last element set to +// NULL, else it will segfault + +void free_2d_tab(void **tab) +{ + void **tmp; + + if (!tab) + return ; + tmp = tab; + while (*tmp) + { + free(*tmp); + tmp++; + } + free(tab); +} diff --git a/libft/src/gnl/get_next_line.c b/libft/src/gnl/get_next_line.c new file mode 100644 index 0000000..8740fec --- /dev/null +++ b/libft/src/gnl/get_next_line.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/02 15:08:54 by agras #+# #+# */ +/* Updated: 2022/01/29 15:27:50 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" +#include +#include + +char *cut_buff(char **left, int i, int mod) +{ + char *line; + char *tmp; + int cut_pos; + + cut_pos = i; + if (mod == GNL_NORETURN) + cut_pos -= 1; + line = ft_strndup(*left, cut_pos); + tmp = (*left); + (*left) += i; + *left = ft_strndup(*left, BUFFER_SIZE); + free(tmp); + return (line); +} + +char *exit_func(char **left, char *buf) +{ + char *tmp; + + tmp = NULL; + if (*left) + { + if ((*left)[0]) + tmp = ft_strndup(*left, ft_strlen(*left)); + } + free(*left); + free(buf); + *left = NULL; + return (tmp); +} + +char *get_next_line(int fd, int mod) +{ + static char *left = NULL; + char *buf; + int i; + int bytes_read; + + i = 0; + if (left) + { + while (left[i]) + { + if (left[i] == '\n') + return (cut_buff(&left, i + 1, mod)); + i++; + } + } + buf = malloc(sizeof(char) * (BUFFER_SIZE + 1)); + if (!buf) + return (NULL); + bytes_read = read(fd, buf, BUFFER_SIZE); + if (bytes_read <= 0) + return (exit_func(&left, buf)); + buf[bytes_read] = '\0'; + left = ft_strjoin_m(left, buf); + return (get_next_line(fd, mod)); +} diff --git a/libft/src/gnl/get_next_line.h b/libft/src/gnl/get_next_line.h new file mode 100644 index 0000000..756af6c --- /dev/null +++ b/libft/src/gnl/get_next_line.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/02 19:09:34 by agras #+# #+# */ +/* Updated: 2022/01/29 15:28:51 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H + +# include "../../includes/libft.h" + +# include +# include + +# define BUFFER_SIZE 1000000 +// gnl return line with \n +# define GNL_WRETURN 0 +// gnl return line without \n +# define GNL_NORETURN 1 + +char *ft_strjoin_m(char *s1, char *s2); +char *ft_strndup(char *str, int n); +char *get_next_line(int fd, int mod); + +#endif \ No newline at end of file diff --git a/libft/src/gnl/get_next_line_utils.c b/libft/src/gnl/get_next_line_utils.c new file mode 100644 index 0000000..88610c6 --- /dev/null +++ b/libft/src/gnl/get_next_line_utils.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/02 19:08:18 by agras #+# #+# */ +/* Updated: 2022/01/29 15:26:28 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "get_next_line.h" + +// int ft_strlen(char *str) +// { +// int i; + +// i = 0; +// if (!str) +// return (0); +// while (str[i]) +// i++; +// return (i); +// } + +char *ft_strndup(char *str, int n) +{ + char *dup; + int i; + + i = 0; + if (!str) + return (NULL); + dup = malloc(sizeof(char) * (n + 1)); + if (!dup) + return (NULL); + while (str[i] && i < n) + { + dup[i] = str[i]; + i++; + } + dup[i] = '\0'; + return (dup); +} + +char *ft_strcpy_to_end(char *dest, char *src) +{ + int i; + + i = 0; + while (src[i]) + { + dest[i] = src[i]; + i++; + } + return (&(dest[i])); +} + +char *ft_strjoin_m(char *s1, char *s2) +{ + char *dup; + char *dup_start; + + dup = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!dup) + return (NULL); + dup_start = dup; + if (s1) + { + dup = ft_strcpy_to_end(dup, s1); + free(s1); + } + if (s2) + { + dup = ft_strcpy_to_end(dup, s2); + free(s2); + } + *dup = '\0'; + return (dup_start); +} diff --git a/libft/src/is/ft_isalnum.c b/libft/src/is/ft_isalnum.c new file mode 100644 index 0000000..3c73af0 --- /dev/null +++ b/libft/src/is/ft_isalnum.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 15:29:32 by agras #+# #+# */ +/* Updated: 2021/11/24 15:31:32 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalnum(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (1); + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/libft/src/is/ft_isalpha.c b/libft/src/is/ft_isalpha.c new file mode 100644 index 0000000..809c8fd --- /dev/null +++ b/libft/src/is/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 14:10:04 by agras #+# #+# */ +/* Updated: 2021/11/24 14:18:33 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (1); + return (0); +} diff --git a/libft/src/is/ft_isascii.c b/libft/src/is/ft_isascii.c new file mode 100644 index 0000000..a92a7a3 --- /dev/null +++ b/libft/src/is/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 15:43:28 by agras #+# #+# */ +/* Updated: 2021/12/13 16:59:25 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libft/src/is/ft_isdigit.c b/libft/src/is/ft_isdigit.c new file mode 100644 index 0000000..36e7b15 --- /dev/null +++ b/libft/src/is/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 15:05:52 by agras #+# #+# */ +/* Updated: 2021/11/24 15:18:43 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/libft/src/is/ft_isprint.c b/libft/src/is/ft_isprint.c new file mode 100644 index 0000000..7135007 --- /dev/null +++ b/libft/src/is/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 16:14:58 by agras #+# #+# */ +/* Updated: 2021/12/10 14:35:20 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c < 127) + return (1); + return (0); +} diff --git a/libft/src/lst/ft_lstadd_back.c b/libft/src/lst/ft_lstadd_back.c new file mode 100644 index 0000000..b51add2 --- /dev/null +++ b/libft/src/lst/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:32:39 by agras #+# #+# */ +/* Updated: 2021/12/12 21:10:02 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tmp; + + if (!new) + return ; + if (!(*lst)) + { + (*lst) = new; + return ; + } + tmp = ft_lstlast(*lst); + tmp->next = new; +} diff --git a/libft/src/lst/ft_lstadd_front.c b/libft/src/lst/ft_lstadd_front.c new file mode 100644 index 0000000..e143562 --- /dev/null +++ b/libft/src/lst/ft_lstadd_front.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:26:55 by agras #+# #+# */ +/* Updated: 2021/12/12 21:24:03 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (!new) + return ; + if (!lst) + { + (*lst) = new; + return ; + } + new->next = (*lst); + (*lst) = new; +} diff --git a/libft/src/lst/ft_lstclear.c b/libft/src/lst/ft_lstclear.c new file mode 100644 index 0000000..40d8e47 --- /dev/null +++ b/libft/src/lst/ft_lstclear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:38:47 by agras #+# #+# */ +/* Updated: 2021/12/12 21:39:16 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static t_list *delete_elem(t_list *lst, void (*del)(void*)) +{ + t_list *tmp; + + tmp = lst->next; + ft_lstdelone(lst, del); + return (tmp); +} + +void ft_lstclear(t_list **lst, void (*del)(void*)) +{ + if (!*lst) + return ; + while (*lst) + *lst = delete_elem(*lst, del); +} diff --git a/libft/src/lst/ft_lstdelone.c b/libft/src/lst/ft_lstdelone.c new file mode 100644 index 0000000..37e4184 --- /dev/null +++ b/libft/src/lst/ft_lstdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:36:14 by agras #+# #+# */ +/* Updated: 2021/12/12 21:37:29 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void*)) +{ + if (!lst) + return ; + del(lst->content); + free(lst); +} diff --git a/libft/src/lst/ft_lstiter.c b/libft/src/lst/ft_lstiter.c new file mode 100644 index 0000000..4041c10 --- /dev/null +++ b/libft/src/lst/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:45:13 by agras #+# #+# */ +/* Updated: 2021/12/12 21:41:23 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (!lst) + return ; + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/src/lst/ft_lstlast.c b/libft/src/lst/ft_lstlast.c new file mode 100644 index 0000000..a33b9ec --- /dev/null +++ b/libft/src/lst/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:31:05 by agras #+# #+# */ +/* Updated: 2021/12/12 21:06:45 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/libft/src/lst/ft_lstmap.c b/libft/src/lst/ft_lstmap.c new file mode 100644 index 0000000..6c884b9 --- /dev/null +++ b/libft/src/lst/ft_lstmap.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:48:00 by agras #+# #+# */ +/* Updated: 2021/12/12 23:48:42 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static t_list *elem_dup(t_list *lst, t_list *new_lst, void *(*f)(void *)) +{ + t_list *new; + + new = malloc(sizeof(t_list)); + if (!new) + return (NULL); + new->content = f(lst->content); + new->next = NULL; + ft_lstadd_back(&new_lst, new); + return (new_lst); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_lst; + + if (!lst) + return (NULL); + new_lst = ft_lstnew(f(lst->content)); + if (!new_lst) + return (NULL); + lst = lst->next; + while (lst) + { + if (elem_dup(lst, new_lst, f) == NULL) + { + ft_lstclear(&new_lst, del); + return (NULL); + } + lst = lst->next; + } + return (new_lst); +} diff --git a/libft/src/lst/ft_lstnew.c b/libft/src/lst/ft_lstnew.c new file mode 100644 index 0000000..2c135bb --- /dev/null +++ b/libft/src/lst/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:20:10 by agras #+# #+# */ +/* Updated: 2021/12/15 18:09:54 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *new; + + new = malloc(sizeof(t_list)); + if (!new) + return (NULL); + new->content = content; + new->next = NULL; + return (new); +} diff --git a/libft/src/lst/ft_lstsize.c b/libft/src/lst/ft_lstsize.c new file mode 100644 index 0000000..9c05494 --- /dev/null +++ b/libft/src/lst/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 19:29:11 by agras #+# #+# */ +/* Updated: 2021/12/10 18:13:46 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst != NULL) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/libft/src/mem/ft_bzero.c b/libft/src/mem/ft_bzero.c new file mode 100644 index 0000000..23f315d --- /dev/null +++ b/libft/src/mem/ft_bzero.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/25 00:30:44 by agras #+# #+# */ +/* Updated: 2021/12/15 11:55:10 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_bzero(void *s, size_t n) +{ + size_t i; + unsigned char *mem; + + mem = (unsigned char *)s; + i = 0; + while (i < n) + { + mem[i] = 0; + i++; + } +} diff --git a/libft/src/mem/ft_calloc.c b/libft/src/mem/ft_calloc.c new file mode 100644 index 0000000..44cbd66 --- /dev/null +++ b/libft/src/mem/ft_calloc.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/05 20:23:59 by agras #+# #+# */ +/* Updated: 2021/12/13 17:09:15 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + int mem_size; + int i; + + if (nmemb * size > 2147483647) + return (NULL); + mem_size = nmemb * size; + ptr = malloc(mem_size); + if (!ptr) + return (NULL); + i = 0; + while (i < mem_size) + { + ((unsigned char *)ptr)[i] = '\0'; + i++; + } + return (ptr); +} diff --git a/libft/src/mem/ft_memchr.c b/libft/src/mem/ft_memchr.c new file mode 100644 index 0000000..caea04a --- /dev/null +++ b/libft/src/mem/ft_memchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 17:12:39 by agras #+# #+# */ +/* Updated: 2021/12/01 18:24:36 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (((unsigned char *)s)[i] == (unsigned char)c) + return ((void *)s + i); + i++; + } + return (0); +} diff --git a/libft/src/mem/ft_memcmp.c b/libft/src/mem/ft_memcmp.c new file mode 100644 index 0000000..05e2c61 --- /dev/null +++ b/libft/src/mem/ft_memcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 11:16:02 by agras #+# #+# */ +/* Updated: 2021/12/04 21:08:59 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + int balance; + + i = 0; + balance = 0; + while (i < n) + { + balance = ((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]; + if (balance != 0) + return (balance); + i++; + } + return (0); +} diff --git a/libft/src/mem/ft_memcpy.c b/libft/src/mem/ft_memcpy.c new file mode 100644 index 0000000..0c44c49 --- /dev/null +++ b/libft/src/mem/ft_memcpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 10:41:07 by agras #+# #+# */ +/* Updated: 2021/12/15 12:00:31 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + unsigned char *dst; + + i = 0; + if (!dest && !src) + return (NULL); + dst = (unsigned char *)dest; + while (i < n) + { + dst[i] = ((unsigned char *)src)[i]; + i++; + } + return (dest); +} diff --git a/libft/src/mem/ft_memmove.c b/libft/src/mem/ft_memmove.c new file mode 100644 index 0000000..0f703bf --- /dev/null +++ b/libft/src/mem/ft_memmove.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 14:15:40 by agras #+# #+# */ +/* Updated: 2021/12/07 21:15:11 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" +#include + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + if (!src && !dest && n > 0) + return (NULL); + if (n == 0) + return (dest); + if (src < dest) + { + while (n > 0) + { + ((unsigned char *)dest)[n - 1] = ((unsigned char *)src)[n - 1]; + n--; + } + } + else + ft_memcpy(dest, src, n); + return (dest); +} diff --git a/libft/src/mem/ft_memset.c b/libft/src/mem/ft_memset.c new file mode 100644 index 0000000..ffee23a --- /dev/null +++ b/libft/src/mem/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 16:39:15 by agras #+# #+# */ +/* Updated: 2021/11/24 23:29:39 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memset(void *s, int c, size_t n) +{ + size_t i; + unsigned char *mem; + + mem = (unsigned char *)s; + i = 0; + while (i < n) + { + mem[i] = (unsigned char)c; + i++; + } + return (s); +} diff --git a/libft/src/print/ft_print_2d_tab_char.c b/libft/src/print/ft_print_2d_tab_char.c new file mode 100644 index 0000000..a93d54e --- /dev/null +++ b/libft/src/print/ft_print_2d_tab_char.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_2d_tab_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/29 14:36:31 by agras #+# #+# */ +/* Updated: 2022/01/29 14:54:05 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// !! BE careful, void ** tab sent in params must have his last element set to +// NULL, else it will segfault + +void ft_print_2d_tab_char(char **tab) +{ + while (*tab) + { + ft_putstr_fd(*tab, 1); + write(1, "\n", 1); + tab++; + } +} diff --git a/libft/src/print/ft_putchar_fd.c b/libft/src/print/ft_putchar_fd.c new file mode 100644 index 0000000..78650ea --- /dev/null +++ b/libft/src/print/ft_putchar_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 20:57:03 by agras #+# #+# */ +/* Updated: 2021/12/13 17:11:18 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + if (fd != -1) + write(fd, &c, 1); +} diff --git a/libft/src/print/ft_putendl_fd.c b/libft/src/print/ft_putendl_fd.c new file mode 100644 index 0000000..15df61d --- /dev/null +++ b/libft/src/print/ft_putendl_fd.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 21:31:22 by agras #+# #+# */ +/* Updated: 2021/12/15 11:46:48 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +void ft_putendl_fd(char *s, int fd) +{ + if (s && fd != -1) + { + write(fd, s, ft_strlen(s)); + write(fd, "\n", 1); + } +} diff --git a/libft/src/print/ft_putnbr_fd.c b/libft/src/print/ft_putnbr_fd.c new file mode 100644 index 0000000..d33dc8d --- /dev/null +++ b/libft/src/print/ft_putnbr_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 21:56:02 by agras #+# #+# */ +/* Updated: 2021/12/13 18:06:42 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putnbr_fd(int n, int fd) +{ + char c; + + if (n == -2147483648) + { + write(fd, "-2147483648", 11); + return ; + } + if (n < 0) + { + write(fd, "-", 1); + n = -n; + } + if (n > 9) + ft_putnbr_fd(n / 10, fd); + c = (n % 10) + '0'; + write(fd, &c, 1); +} diff --git a/libft/src/print/ft_putstr_fd.c b/libft/src/print/ft_putstr_fd.c new file mode 100644 index 0000000..1ea02bd --- /dev/null +++ b/libft/src/print/ft_putstr_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 21:03:38 by agras #+# #+# */ +/* Updated: 2021/12/15 11:46:29 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static unsigned int ft_strlen(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +void ft_putstr_fd(char *s, int fd) +{ + if (s && fd != -1) + write(fd, s, ft_strlen(s)); +} diff --git a/libft/src/str/ft_cstr.c b/libft/src/str/ft_cstr.c new file mode 100644 index 0000000..0e3bfc9 --- /dev/null +++ b/libft/src/str/ft_cstr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/29 16:02:12 by agras #+# #+# */ +/* Updated: 2022/01/29 16:08:58 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// find a char in a given string + +char *ft_cstr(char *str, char c) +{ + if (!str) + return (NULL); + while (*str) + { + if (*str == c) + return (str); + str++; + } + return (NULL); +} diff --git a/libft/src/str/ft_split.c b/libft/src/str/ft_split.c new file mode 100644 index 0000000..a3d6d97 --- /dev/null +++ b/libft/src/str/ft_split.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/06 17:14:48 by agras #+# #+# */ +/* Updated: 2021/12/10 17:22:59 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +int is_separator(char c, char sep) +{ + if (sep == c) + return (1); + return (0); +} + +int cpy_word(const char *s1, char *s2, char c) +{ + int i; + + i = 0; + while (s1[i] && is_separator(s1[i], c) == 0) + { + s2[i] = s1[i]; + i++; + } + s2[i] = '\0'; + return (i); +} + +int get_word_len(const char *s, char c) +{ + int i; + + i = 0; + while (s[i] && is_separator(s[i], c) == 0) + i++; + return (i); +} + +int count_words(const char *s, char c) +{ + int flick; + int word_count; + + flick = 0; + word_count = 0; + while (*s) + { + if (is_separator(*s, c) == 0 && flick == 0) + { + word_count++; + flick = 1; + } + if (is_separator(*s, c) == 1) + flick = 0; + s++; + } + return (word_count); +} + +char **ft_split(char const *s, char c) +{ + char **tab; + int i; + + tab = NULL; + i = 0; + if (!s) + return (NULL); + tab = malloc(sizeof(char *) * (count_words(s, c) + 1)); + if (tab == NULL) + return (NULL); + while (*s) + { + if (is_separator(*s, c) == 0) + { + tab[i] = malloc(sizeof(char) * (get_word_len(s, c) + 1)); + if (tab[i] == NULL) + return (NULL); + s += cpy_word(s, tab[i], c); + i++; + } + while (*s && is_separator(*s, c) == 1) + s++; + } + tab[i] = NULL; + return (tab); +} diff --git a/libft/src/str/ft_strcat_malloc.c b/libft/src/str/ft_strcat_malloc.c new file mode 100644 index 0000000..86eb26f --- /dev/null +++ b/libft/src/str/ft_strcat_malloc.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat_malloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/09 16:29:41 by agras #+# #+# */ +/* Updated: 2022/02/09 17:01:54 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// Return the concatenated strings as a malloced string in the heap +// instead of modifying the stack based string passed in first param +// as do the real strlcat + +char *ft_strcat_malloc(const char *dst, const char *src) +{ + char *concat; + int len; + int i; + + i = 0; + len = ft_strlen(dst) + ft_strlen(src); + concat = malloc(sizeof(char) * (len + 1)); + if (!concat) + return (NULL); + while (*dst) + { + concat[i] = *dst; + i++; + dst++; + } + while (*src) + { + concat[i] = *src; + i++; + src++; + } + concat[i] = '\0'; + return (concat); +} diff --git a/libft/src/str/ft_strchr.c b/libft/src/str/ft_strchr.c new file mode 100644 index 0000000..eba43f9 --- /dev/null +++ b/libft/src/str/ft_strchr.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 15:42:43 by agras #+# #+# */ +/* Updated: 2021/12/15 12:24:07 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strchr(const char *s, int c) +{ + char c_char; + + c_char = (char)c; + while (*s && *s != c_char) + s++; + if (*s == c_char) + return ((char *)s); + return (0); +} diff --git a/libft/src/str/ft_strcmp.c b/libft/src/str/ft_strcmp.c new file mode 100644 index 0000000..e4aadbc --- /dev/null +++ b/libft/src/str/ft_strcmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/29 17:00:53 by agras #+# #+# */ +/* Updated: 2022/01/29 17:02:11 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strcmp(const char *s1, const char *s2) +{ + int balance; + + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + balance = (unsigned char)*s1 - (unsigned char)*s2; + return (balance); +} diff --git a/libft/src/str/ft_strdup.c b/libft/src/str/ft_strdup.c new file mode 100644 index 0000000..8c8ec06 --- /dev/null +++ b/libft/src/str/ft_strdup.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/06 12:41:48 by agras #+# #+# */ +/* Updated: 2021/12/15 11:47:57 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static unsigned int ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strdup(const char *s) +{ + char *dup; + int i; + + dup = malloc(sizeof(char) * (ft_strlen(s) + 1)); + if (!dup) + return (NULL); + i = 0; + while (s[i]) + { + dup[i] = s[i]; + i++; + } + dup[i] = '\0'; + return (dup); +} diff --git a/libft/src/str/ft_striteri.c b/libft/src/str/ft_striteri.c new file mode 100644 index 0000000..ab74d52 --- /dev/null +++ b/libft/src/str/ft_striteri.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 20:34:33 by agras #+# #+# */ +/* Updated: 2021/12/10 17:32:03 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + int i; + + if (!s) + return ; + i = 0; + while (s[i]) + { + f(i, &s[i]); + i++; + } +} diff --git a/libft/src/str/ft_strjoin.c b/libft/src/str/ft_strjoin.c new file mode 100644 index 0000000..a559478 --- /dev/null +++ b/libft/src/str/ft_strjoin.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/26 17:49:30 by agras #+# #+# */ +/* Updated: 2021/12/13 17:10:56 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strjoin(char const *s1, char const *s2) +{ + int i; + char *cat; + + i = 0; + if (!s1) + return (NULL); + cat = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!cat) + return (NULL); + while (s1[i]) + { + cat[i] = s1[i]; + i++; + } + while (*s2) + { + cat[i] = *s2; + s2++; + i++; + } + cat[i] = '\0'; + return (cat); +} diff --git a/libft/src/str/ft_strlcat.c b/libft/src/str/ft_strlcat.c new file mode 100644 index 0000000..820d6c6 --- /dev/null +++ b/libft/src/str/ft_strlcat.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 15:17:26 by agras #+# #+# */ +/* Updated: 2021/12/17 17:56:10 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static size_t ft_strlen(const char *str) +{ + size_t i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t d_len; + size_t dst_len; + size_t src_len; + size_t s_len; + + d_len = 0; + s_len = 0; + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + while (dst[d_len] != 0 && d_len < dstsize) + d_len++; + while (s_len + 1 < dstsize - d_len && src[s_len]) + { + dst[dst_len + s_len] = src[s_len]; + s_len++; + } + dst[dst_len + s_len] = '\0'; + return (d_len + src_len); +} diff --git a/libft/src/str/ft_strlcpy.c b/libft/src/str/ft_strlcpy.c new file mode 100644 index 0000000..4db8c7a --- /dev/null +++ b/libft/src/str/ft_strlcpy.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 14:58:33 by agras #+# #+# */ +/* Updated: 2021/12/04 17:58:38 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static size_t ft_strlen(const char *str) +{ + size_t i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + if (size > 0) + { + while (src[i] && i < size - 1) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + } + return (ft_strlen(src)); +} diff --git a/libft/src/str/ft_strlen.c b/libft/src/str/ft_strlen.c new file mode 100644 index 0000000..2e4ad93 --- /dev/null +++ b/libft/src/str/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/24 16:20:22 by agras #+# #+# */ +/* Updated: 2022/01/29 15:25:24 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strlen(const char *s) +{ + int i; + + i = 0; + if (!s) + return (0); + while (s[i]) + i++; + return (i); +} diff --git a/libft/src/str/ft_strmapi.c b/libft/src/str/ft_strmapi.c new file mode 100644 index 0000000..168d082 --- /dev/null +++ b/libft/src/str/ft_strmapi.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/29 20:08:50 by agras #+# #+# */ +/* Updated: 2021/12/13 17:10:31 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + int i; + char *new; + + i = 0; + if (!s) + return (NULL); + new = malloc(sizeof(char) * (ft_strlen(s) + 1)); + if (!new) + return (NULL); + while (s[i]) + { + new[i] = f(i, s[i]); + i++; + } + new[i] = '\0'; + return (new); +} diff --git a/libft/src/str/ft_strncmp.c b/libft/src/str/ft_strncmp.c new file mode 100644 index 0000000..b466154 --- /dev/null +++ b/libft/src/str/ft_strncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 17:06:25 by agras #+# #+# */ +/* Updated: 2021/12/13 17:35:20 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + int balance; + + if (n == 0) + return (0); + while (*s1 == *s2 && *s1 && *s2 && n > 1) + { + s1++; + s2++; + n--; + } + balance = (unsigned char)*s1 - (unsigned char)*s2; + return (balance); +} diff --git a/libft/src/str/ft_strnstr.c b/libft/src/str/ft_strnstr.c new file mode 100644 index 0000000..5f99575 --- /dev/null +++ b/libft/src/str/ft_strnstr.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 18:29:43 by agras #+# #+# */ +/* Updated: 2021/12/08 11:41:38 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static size_t ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + size_t max; + + j = 0; + max = ft_strlen(little); + if (*little == 0) + return ((char *)big); + while (big[j] && j < len) + { + i = 0; + while (big[j + i] == little[i] && (j + i) < len && big[j + i]) + { + if (i + 1 >= max) + return ((char *)&(big[j])); + i++; + } + j++; + } + return (NULL); +} diff --git a/libft/src/str/ft_strrchr.c b/libft/src/str/ft_strrchr.c new file mode 100644 index 0000000..906f8ca --- /dev/null +++ b/libft/src/str/ft_strrchr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 16:15:33 by agras #+# #+# */ +/* Updated: 2021/12/15 18:12:58 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +char *ft_strrchr(const char *s, int c) +{ + int i; + char c_char; + + c_char = (char)c; + i = ft_strlen(s); + while (i >= 0) + { + if (s[i] == c_char) + return ((char *)s + i); + i--; + } + return (0); +} diff --git a/libft/src/str/ft_strtrim.c b/libft/src/str/ft_strtrim.c new file mode 100644 index 0000000..3f3488a --- /dev/null +++ b/libft/src/str/ft_strtrim.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/28 18:09:03 by antoine #+# #+# */ +/* Updated: 2021/12/08 16:58:01 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +static char *ft_strndup(const char *str, int n) +{ + char *dup; + int len; + int i; + + i = -1; + len = 0; + while (str[len] && len < n) + len++; + dup = malloc(sizeof(char) * (len + 1)); + if (!dup) + return (NULL); + while (++i < len) + dup[i] = str[i]; + dup[i] = '\0'; + return (dup); +} + +static int is_in_set(char c, char const *set) +{ + while (*set) + { + if (c == *set) + return (1); + set++; + } + return (0); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + int len; + + if (!s1 || !set) + return (NULL); + while (*s1) + { + if (is_in_set(*s1, set) == 0) + break ; + s1++; + } + len = ft_strlen(s1) - 1; + while (len > 0) + { + if (is_in_set(s1[len], set) == 0) + break ; + len--; + } + return (ft_strndup(s1, len + 1)); +} diff --git a/libft/src/str/ft_substr.c b/libft/src/str/ft_substr.c new file mode 100644 index 0000000..5af1118 --- /dev/null +++ b/libft/src/str/ft_substr.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/11/26 15:27:23 by agras #+# #+# */ +/* Updated: 2021/12/12 14:39:44 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +static size_t ft_strlen(const char *str) +{ + size_t i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +static char *ft_strndup(const char *str, size_t n) +{ + size_t i; + char *dup; + + i = 0; + dup = malloc(sizeof(char) * (n + 1)); + if (!dup) + return (NULL); + while (i < n) + { + dup[i] = (char)str[i]; + i++; + } + dup[i] = '\0'; + return (dup); +} + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + size_t s_len; + + i = 0; + if (!s) + return (NULL); + s_len = ft_strlen(s); + while (i < len && start + i < s_len) + i++; + return (ft_strndup(s + start, i)); +} diff --git a/libft/src/str/ft_tolower.c b/libft/src/str/ft_tolower.c new file mode 100644 index 0000000..e068b5d --- /dev/null +++ b/libft/src/str/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 15:36:39 by agras #+# #+# */ +/* Updated: 2021/12/01 15:37:47 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} diff --git a/libft/src/str/ft_toupper.c b/libft/src/str/ft_toupper.c new file mode 100644 index 0000000..672cb8e --- /dev/null +++ b/libft/src/str/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/12/01 15:32:42 by agras #+# #+# */ +/* Updated: 2021/12/13 17:10:06 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} diff --git a/libft/src/str/strcmp_occurence.c b/libft/src/str/strcmp_occurence.c new file mode 100644 index 0000000..ce053ed --- /dev/null +++ b/libft/src/str/strcmp_occurence.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* strcmp_occurence.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/29 17:26:07 by agras #+# #+# */ +/* Updated: 2022/01/29 18:18:27 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +// works like ft_strcmp but without taking in account the order +// of the characters +// return 0 if the 2 strings have the exact same characters but not +// necessarily in the same order +// else return the number of characters they have don't have in common + +int strcmp_occurence(const char *s1, const char *s2) +{ + int count; + int s1_len; + + count = 0; + s1_len = ft_strlen(s1); + while (*s2) + { + if (ft_strchr(s1, *s2) != 0) + count++; + s2++; + } + if (s1_len == count) + return (0); + return (count); +} diff --git a/src/client/client.c b/src/client/client.c new file mode 100644 index 0000000..faa0426 --- /dev/null +++ b/src/client/client.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* client.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/12 22:05:01 by agras #+# #+# */ +/* Updated: 2024/04/08 18:00:28 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/client/client.h" + +void transfer_char(char chara, int pid) +{ + char byte; + int i; + + byte = chara; + i = 0; + while (i < 8) + { + if (((byte << i) & 128) == 128) + kill(pid, SIGUSR2); + else + kill(pid, SIGUSR1); + usleep(100); + i++; + } +} + +int launch_client(int pid, char *msg) +{ + if (pid == -1) + return (-1); + while (*msg) + { + transfer_char(*msg, pid); + msg++; + } + transfer_char(0, pid); + return (0); +} + +int main(int ac, char **av) +{ + if (ac != 3) + { + ft_putstr_fd("Usage: ./client \n", 2); + return (1); + } + if (launch_client(atoi(av[1]), av[2]) == -1) + return (1); + return (0); +} diff --git a/src/server/bit_management.c b/src/server/bit_management.c new file mode 100644 index 0000000..64e7fa8 --- /dev/null +++ b/src/server/bit_management.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bit_management.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/04/12 21:29:28 by agras #+# #+# */ +/* Updated: 2022/04/12 21:32:28 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/server/server.h" + +void set_bit(char *byte, int shift) +{ + *byte |= 1 << shift; +} + +void clear_bit(char *byte, int shift) +{ + *byte &= ~(1 << shift); +} + +void print_bit(unsigned char byte, int shift) +{ + if ((byte >> shift) & (1 == 1)) + write(1, "1", 1); + else + write(1, "0", 1); +} + +void print_byte_bin(unsigned char byte) +{ + int i; + + i = 7; + while (i >= 0) + { + print_bit(byte, i); + i--; + } + write(1, "\n", 1); +} diff --git a/src/server/server.c b/src/server/server.c new file mode 100644 index 0000000..f4ff5b5 --- /dev/null +++ b/src/server/server.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* server.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agras +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/12 21:54:06 by agras #+# #+# */ +/* Updated: 2022/05/02 16:08:06 by agras ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/server/server.h" + +char g_msg[10000]; + +void print_msg(void) +{ + int i; + + i = 0; + while (g_msg[i]) + { + write(1, &g_msg[i], 1); + i++; + } + write(1, "\n", 1); +} + +void reset_msg(void) +{ + int i; + + i = 0; + while (i < 10000) + { + g_msg[i] = '\0'; + i++; + } +} + +void handle_signal(int sig) +{ + static int bit_count = 7; + static int i = 0; + + if (sig == SIGUSR2) + set_bit(&g_msg[i], bit_count); + if (bit_count == 0) + { + bit_count = 8; + if (g_msg[i] == 0) + { + print_msg(); + reset_msg(); + bit_count = 7; + i = 0; + return ; + } + i++; + g_msg[i] = 0; + } + bit_count--; +} + +int wait_for_msg(void) +{ + signal(SIGUSR1, handle_signal); + signal(SIGUSR2, handle_signal); + while (1) + pause(); +} + +int main(void) +{ + pid_t pid; + + reset_msg(); + pid = getpid(); + ft_putnbr_fd(pid, 1); + ft_putchar_fd('\n', 1); + wait_for_msg(); +}