From 09c2cb5b3e0989a302c2bdaf1dd931c82ac2294e Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Mon, 13 Jan 2025 13:21:53 +0100 Subject: [PATCH] base struct --- Makefile | 39 +++++++++ lib/libft/.gitignore | 6 ++ lib/libft/Makefile | 56 ++++++++++++ lib/libft/colors.h | 56 ++++++++++++ lib/libft/general.h | 23 +++++ lib/libft/libft.h | 84 ++++++++++++++++++ lib/libft/srcs/aloc/ft_calloc.c | 27 ++++++ lib/libft/srcs/aloc/ft_free.c | 31 +++++++ lib/libft/srcs/check/ft_isalnum.c | 21 +++++ lib/libft/srcs/check/ft_isalpha.c | 21 +++++ lib/libft/srcs/check/ft_isascii.c | 21 +++++ lib/libft/srcs/check/ft_isdigit.c | 21 +++++ lib/libft/srcs/check/ft_isprint.c | 21 +++++ lib/libft/srcs/format/ft_atoi.c | 72 ++++++++++++++++ lib/libft/srcs/format/ft_itoa.c | 67 +++++++++++++++ lib/libft/srcs/format/ft_split.c | 88 +++++++++++++++++++ lib/libft/srcs/gnl/gnl.c | 123 +++++++++++++++++++++++++++ lib/libft/srcs/gnl/gnl.h | 21 +++++ lib/libft/srcs/gnl/gnl_utils.c | 63 ++++++++++++++ lib/libft/srcs/mem/ft_bzero.c | 26 ++++++ lib/libft/srcs/mem/ft_memchr.c | 26 ++++++ lib/libft/srcs/mem/ft_memcmp.c | 28 ++++++ lib/libft/srcs/mem/ft_memcpy.c | 29 +++++++ lib/libft/srcs/mem/ft_memmove.c | 37 ++++++++ lib/libft/srcs/mem/ft_memset.c | 28 ++++++ lib/libft/srcs/print/ft_printf.c | 100 ++++++++++++++++++++++ lib/libft/srcs/print/ft_put.c | 74 ++++++++++++++++ lib/libft/srcs/print/ft_put_fd.c | 54 ++++++++++++ lib/libft/srcs/print/ft_put_u_base.c | 54 ++++++++++++ lib/libft/srcs/print/ft_putendl_fd.c | 22 +++++ lib/libft/srcs/str/ft_strchr.c | 31 +++++++ lib/libft/srcs/str/ft_strdup.c | 30 +++++++ lib/libft/srcs/str/ft_striteri.c | 24 ++++++ lib/libft/srcs/str/ft_strjoin.c | 38 +++++++++ lib/libft/srcs/str/ft_strlcat.c | 32 +++++++ lib/libft/srcs/str/ft_strlcpy.c | 28 ++++++ lib/libft/srcs/str/ft_strlen.c | 25 ++++++ lib/libft/srcs/str/ft_strmapi.c | 32 +++++++ lib/libft/srcs/str/ft_strncmp.c | 33 +++++++ lib/libft/srcs/str/ft_strnstr.c | 37 ++++++++ lib/libft/srcs/str/ft_strrchr.c | 26 ++++++ lib/libft/srcs/str/ft_strtrim.c | 77 +++++++++++++++++ lib/libft/srcs/str/ft_substr.c | 37 ++++++++ lib/libft/srcs/str/ft_tolower.c | 21 +++++ lib/libft/srcs/str/ft_toupper.c | 21 +++++ minishell.h | 0 srcs/main.c | 4 + 47 files changed, 1835 insertions(+) create mode 100644 Makefile create mode 100644 lib/libft/.gitignore create mode 100644 lib/libft/Makefile create mode 100644 lib/libft/colors.h create mode 100644 lib/libft/general.h create mode 100644 lib/libft/libft.h create mode 100644 lib/libft/srcs/aloc/ft_calloc.c create mode 100644 lib/libft/srcs/aloc/ft_free.c create mode 100644 lib/libft/srcs/check/ft_isalnum.c create mode 100644 lib/libft/srcs/check/ft_isalpha.c create mode 100644 lib/libft/srcs/check/ft_isascii.c create mode 100644 lib/libft/srcs/check/ft_isdigit.c create mode 100644 lib/libft/srcs/check/ft_isprint.c create mode 100644 lib/libft/srcs/format/ft_atoi.c create mode 100644 lib/libft/srcs/format/ft_itoa.c create mode 100644 lib/libft/srcs/format/ft_split.c create mode 100644 lib/libft/srcs/gnl/gnl.c create mode 100644 lib/libft/srcs/gnl/gnl.h create mode 100644 lib/libft/srcs/gnl/gnl_utils.c create mode 100644 lib/libft/srcs/mem/ft_bzero.c create mode 100644 lib/libft/srcs/mem/ft_memchr.c create mode 100644 lib/libft/srcs/mem/ft_memcmp.c create mode 100644 lib/libft/srcs/mem/ft_memcpy.c create mode 100644 lib/libft/srcs/mem/ft_memmove.c create mode 100644 lib/libft/srcs/mem/ft_memset.c create mode 100644 lib/libft/srcs/print/ft_printf.c create mode 100644 lib/libft/srcs/print/ft_put.c create mode 100644 lib/libft/srcs/print/ft_put_fd.c create mode 100644 lib/libft/srcs/print/ft_put_u_base.c create mode 100644 lib/libft/srcs/print/ft_putendl_fd.c create mode 100644 lib/libft/srcs/str/ft_strchr.c create mode 100644 lib/libft/srcs/str/ft_strdup.c create mode 100644 lib/libft/srcs/str/ft_striteri.c create mode 100644 lib/libft/srcs/str/ft_strjoin.c create mode 100644 lib/libft/srcs/str/ft_strlcat.c create mode 100644 lib/libft/srcs/str/ft_strlcpy.c create mode 100644 lib/libft/srcs/str/ft_strlen.c create mode 100644 lib/libft/srcs/str/ft_strmapi.c create mode 100644 lib/libft/srcs/str/ft_strncmp.c create mode 100644 lib/libft/srcs/str/ft_strnstr.c create mode 100644 lib/libft/srcs/str/ft_strrchr.c create mode 100644 lib/libft/srcs/str/ft_strtrim.c create mode 100644 lib/libft/srcs/str/ft_substr.c create mode 100644 lib/libft/srcs/str/ft_tolower.c create mode 100644 lib/libft/srcs/str/ft_toupper.c create mode 100644 minishell.h create mode 100644 srcs/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e1e3658 --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +CC = clang +WFLAGS = -Wall -Werror -Wextra -g -g3 -ggdb + +SRCDIR = srcs +OBJDIR = .objs +INCDIR = . +LIBFT_DIR = lib/libft + +NAME = minishell + +SRCS = $(shell find $(SRCDIR) -name "*.c") +OBJS = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS)) +DEPS = $(OBJS:.o=.d) + +CYAN = \033[36m +RESET = \033[0m + +.PHONY: all clean fclean re + +all: $(NAME) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(dir $@) + @$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -g3 -ggdb -o $@ + +$(NAME): $(OBJS) + @$(CC) $(WFLAGS) $(OBJS) -o $(NAME) + @echo "$(CYAN)Build completed: $(NAME)$(RESET)" + +clean: + @rm -rf $(OBJDIR) + @echo "$(CYAN)Project cleaned$(RESET)" + +fclean: clean + @echo "$(CYAN)Executable removed$(RESET)" + +re: fclean all + +-include $(DEPS) diff --git a/lib/libft/.gitignore b/lib/libft/.gitignore new file mode 100644 index 0000000..05e34f3 --- /dev/null +++ b/lib/libft/.gitignore @@ -0,0 +1,6 @@ +obj +**.out +**.a +**.o +.DS_Store +**gch.h \ No newline at end of file diff --git a/lib/libft/Makefile b/lib/libft/Makefile new file mode 100644 index 0000000..e2780ae --- /dev/null +++ b/lib/libft/Makefile @@ -0,0 +1,56 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: nalebrun +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/09 14:22:51 by nalebrun #+# #+# # +# Updated: 2024/11/27 12:57:42 by nalebrun ### ########.fr # +# # +# **************************************************************************** # + +# Compiler and flags +CC = cc +CFLAGS = -Wall -Werror -Wextra + +# Directories +SRCDIR = srcs +OBJDIR = obj +INCDIR = . + +# Library Name +NAME = libft.a + +# Source and object files +SRCS = $(shell find $(SRCDIR) -name "*.c") +OBJS = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS)) + +YELLOW = \033[33m +RESET = \033[0m + +.PHONY: all clean fclean re + +# Default target +all: $(NAME) + +# Create the library +$(NAME): $(OBJS) + @ar rcs $@ $^ + @echo "$(YELLOW)[LIBFT] Library $(NAME) created$(RESET)" + +# obj +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(dir $@) + @$(CC) $(CFLAGS) -c $< -o $@ + +#clean +clean: + @rm -rf $(OBJDIR) + @echo "$(YELLOW)[LIBFT] Object files removed$(RESET)" + +fclean: clean + @rm -f $(NAME) + @echo "$(YELLOW)[LIBFT] Lib removed$(RESET)" + +re: fclean all diff --git a/lib/libft/colors.h b/lib/libft/colors.h new file mode 100644 index 0000000..cb540f0 --- /dev/null +++ b/lib/libft/colors.h @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* colors.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 16:36:18 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:38:11 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLORS_H +# define COLORS_H + +# define RESET "\033[0m" + +# define BLACK "\033[0;30m" +# define RED "\033[0;31m" +# define GREEN "\033[0;32m" +# define YELLOW "\033[0;33m" +# define BLUE "\033[0;34m" +# define MAGENTA "\033[0;35m" +# define CYAN "\033[0;36m" +# define WHITE "\033[0;37m" + +# define BOLD_BLACK "\033[1;30m" +# define BOLD_RED "\033[1;31m" +# define BOLD_GREEN "\033[1;32m" +# define BOLD_YELLOW "\033[1;33m" +# define BOLD_BLUE "\033[1;34m" +# define BOLD_MAGENTA "\033[1;35m" +# define BOLD_CYAN "\033[1;36m" +# define BOLD_WHITE "\033[1;37m" + +# define UNDERLINE_BLACK "\033[4;30m" +# define UNDERLINE_RED "\033[4;31m" +# define UNDERLINE_GREEN "\033[4;32m" +# define UNDERLINE_YELLOW "\033[4;33m" +# define UNDERLINE_BLUE "\033[4;34m" +# define UNDERLINE_YELLOW "\033[4;33m" +# define UNDERLINE_BLUE "\033[4;34m" +# define UNDERLINE_MAGENTA "\033[4;35m" +# define UNDERLINE_CYAN "\033[4;36m" +# define UNDERLINE_WHITE "\033[4;37m" + +# define BG_BLACK "\033[40m" +# define BG_RED "\033[41m" +# define BG_GREEN "\033[42m" +# define BG_YELLOW "\033[43m" +# define BG_BLUE "\033[44m" +# define BG_MAGENTA "\033[45m" +# define BG_CYAN "\033[46m" +# define BG_WHITE "\033[47m" + +#endif \ No newline at end of file diff --git a/lib/libft/general.h b/lib/libft/general.h new file mode 100644 index 0000000..4613325 --- /dev/null +++ b/lib/libft/general.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* general.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 16:32:23 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:40:15 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GENERAL_H +# define GENERAL_H + +# define BASE10 "0123456789" +# define BASE16 "0123456789abcdef" +# define BASE16UP "0123456789ABCDEF" + +# define TRUE 1 +# define FALSE 0 + +#endif \ No newline at end of file diff --git a/lib/libft/libft.h b/lib/libft/libft.h new file mode 100644 index 0000000..56a9638 --- /dev/null +++ b/lib/libft/libft.h @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 10:33:28 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:20:56 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# define BUFFER_SIZE 1 + +# include +# include +# include +# include + +# include "colors.h" +# include "general.h" + +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); + +size_t ft_strlen(const char *s); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +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 *haystack, const char *needle, size_t len); +char *ft_substr(const char *s, unsigned int start, size_t len); +char *ft_strdup(const char *s1); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strtrim(const char *s1, const char *set); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); + +void *ft_memset(void *b, int c, size_t len); +void *ft_memcpy(void *dst, const void *src, size_t n); +char *ft_memchr(const void *s, int c, 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); + +int ft_toupper(int c); +int ft_tolower(int c); + +int ft_atoi(const char *str); +char *ft_itoa(int n); + +void *ft_calloc(size_t count, size_t size); +void ft_bzero(void *s, size_t n); + +char **ft_split(const char *s, char c); +void free_tab(char **tab); + +void ft_put_c_fd(char c, int fd); +void ft_put_s_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_put_i_fd(int n, int fd); +int ft_put_ui_base(unsigned int nbr, char *base); +int ft_put_uli_base(unsigned long int nbr, char *base); +int ft_put_c(char c); +int ft_put_s(char *s); +int ft_put_i(int n); +int ft_put_ui(unsigned int n); +void ft_error(char *e); + +int ft_printf(const char *fstr, ...); +int ft_debug(const char *fstr, ...); + +char *get_next_line(int fd); + +void ft_free(char **p); +void ft_free_v(void **p); + +#endif diff --git a/lib/libft/srcs/aloc/ft_calloc.c b/lib/libft/srcs/aloc/ft_calloc.c new file mode 100644 index 0000000..7b0dbf8 --- /dev/null +++ b/lib/libft/srcs/aloc/ft_calloc.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 15:18:45 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:20 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + void *ptr; + + if ((size != 0 && ((count * size) / size != count)) || (size >= 2147483647 + && count >= 2147483647)) + return (NULL); + ptr = malloc(count * size); + if (!ptr) + return (NULL); + ft_bzero(ptr, count * size); + return (ptr); +} diff --git a/lib/libft/srcs/aloc/ft_free.c b/lib/libft/srcs/aloc/ft_free.c new file mode 100644 index 0000000..f7c1625 --- /dev/null +++ b/lib/libft/srcs/aloc/ft_free.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 15:43:37 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 13:34:36 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void ft_free(char **p) +{ + if (p && *p) + { + free(*p); + *p = NULL; + } +} + +void ft_free_v(void **p) +{ + if (p && *p) + { + free(*p); + *p = NULL; + } +} diff --git a/lib/libft/srcs/check/ft_isalnum.c b/lib/libft/srcs/check/ft_isalnum.c new file mode 100644 index 0000000..e2d9c35 --- /dev/null +++ b/lib/libft/srcs/check/ft_isalnum.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 10:12:06 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:32 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (1); + else + return (0); +} diff --git a/lib/libft/srcs/check/ft_isalpha.c b/lib/libft/srcs/check/ft_isalpha.c new file mode 100644 index 0000000..e15b0f4 --- /dev/null +++ b/lib/libft/srcs/check/ft_isalpha.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/09 14:23:31 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:36 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_isalpha(int a) +{ + if ((a >= 65 && a <= 90) || (a >= 97 && a <= 122)) + return (1); + else + return (0); +} diff --git a/lib/libft/srcs/check/ft_isascii.c b/lib/libft/srcs/check/ft_isascii.c new file mode 100644 index 0000000..5790d40 --- /dev/null +++ b/lib/libft/srcs/check/ft_isascii.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 11:11:09 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:40 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + else + return (0); +} diff --git a/lib/libft/srcs/check/ft_isdigit.c b/lib/libft/srcs/check/ft_isdigit.c new file mode 100644 index 0000000..573a227 --- /dev/null +++ b/lib/libft/srcs/check/ft_isdigit.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 10:02:18 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:43 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_isdigit(int c) +{ + if (c >= 48 && c <= 57) + return (1); + else + return (0); +} diff --git a/lib/libft/srcs/check/ft_isprint.c b/lib/libft/srcs/check/ft_isprint.c new file mode 100644 index 0000000..5696917 --- /dev/null +++ b/lib/libft/srcs/check/ft_isprint.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 11:23:47 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:48 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + else + return (0); +} diff --git a/lib/libft/srcs/format/ft_atoi.c b/lib/libft/srcs/format/ft_atoi.c new file mode 100644 index 0000000..cc37998 --- /dev/null +++ b/lib/libft/srcs/format/ft_atoi.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 14:00:41 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:59:01 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static int ft_isspace(char c) +{ + if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' + || c == '\r') + return (1); + else + return (0); +} + +static int ft_signer(char c, int *i) +{ + int sign; + + sign = 1; + if (c == '-') + { + sign *= -1; + (*i)++; + } + else if (c == '+') + (*i)++; + return (sign); +} + +static int ft_signed(int sign) +{ + if (sign == -1) + return (0); + else + return (-1); +} + +int ft_atoi(const char *str) +{ + int i; + int sign; + long res; + int current_digit; + + if (!str) + return (-1); + res = 0; + i = 0; + while (ft_isspace(str[i])) + i++; + sign = ft_signer(str[i], &i); + while (str[i] == '0') + i++; + while (ft_isdigit(str[i])) + { + current_digit = str[i] - '0'; + if (res > (LONG_MAX - current_digit) / 10) + return (ft_signed(sign)); + res = (res * 10) + current_digit; + i++; + } + return ((int)(res *= sign)); +} diff --git a/lib/libft/srcs/format/ft_itoa.c b/lib/libft/srcs/format/ft_itoa.c new file mode 100644 index 0000000..e38c353 --- /dev/null +++ b/lib/libft/srcs/format/ft_itoa.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/15 14:36:46 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:59:05 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static char *ft_itoamem(long int n) +{ + int i; + char *res; + int sign; + + i = 0; + sign = 0; + if (n < 0) + { + sign = 1; + n *= -1; + } + while (n > 9) + { + n /= 10; + i++; + } + res = malloc(((i + 1) + sign + 1) * sizeof(char)); + if (!res) + return (NULL); + res[i + 1 + sign] = '\0'; + ft_memset(res, 48, ((i + 1) + sign) * sizeof(char)); + return (res); +} + +char *ft_itoa(int n) +{ + char *res; + int i; + long int num; + + num = n; + res = ft_itoamem(num); + if (!res) + return (NULL); + i = 0; + if (num < 0) + { + res[i] = '-'; + num *= -1; + } + i = ft_strlen(res); + res[i] = '\0'; + i--; + while (num > 9) + { + res[i--] = (num % 10) + '0'; + num /= 10; + } + res[i] = num + '0'; + return (res); +} diff --git a/lib/libft/srcs/format/ft_split.c b/lib/libft/srcs/format/ft_split.c new file mode 100644 index 0000000..6bb27c6 --- /dev/null +++ b/lib/libft/srcs/format/ft_split.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/11 16:15:47 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:58:41 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static int ft_word_count(const char *str, char c) +{ + int count; + + if (!str) + return (0); + count = 0; + while (*str) + { + while (*str && *str == c) + str++; + if (*str && *str != c) + count++; + while (*str && *str != c) + str++; + } + return (count); +} + +static char *malloccpy(const char *str, char c) +{ + char *result; + int i; + + i = 0; + while (str[i] && str[i] != c) + i++; + result = (char *)malloc(sizeof(char) * (i + 1)); + if (!result) + return (NULL); + ft_strlcpy(result, str, i + 1); + return (result); +} + +void free_tab(char **tab) +{ + char **pos; + + if (!tab) + return ; + pos = tab; + while (*pos != NULL) + free(*(pos++)); + free(tab); +} + +char **ft_split(const char *s, char c) +{ + char **result; + int i; + int strs_len; + + if (!s) + return (NULL); + strs_len = ft_word_count(s, c); + result = (char **)malloc(sizeof(char *) * (strs_len + 1)); + if (!result) + return (NULL); + i = -1; + while (++i < strs_len) + { + while (*s && *s == c) + s++; + result[i] = malloccpy(s, c); + if (!result[i]) + { + free_tab(result); + return (NULL); + } + s += ft_strlen(result[i]); + } + result[i] = 0; + return (result); +} diff --git a/lib/libft/srcs/gnl/gnl.c b/lib/libft/srcs/gnl/gnl.c new file mode 100644 index 0000000..5aa6450 --- /dev/null +++ b/lib/libft/srcs/gnl/gnl.c @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gnl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/27 11:45:42 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:50:04 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" +#include "gnl.h" + +void free_buf(char **tab) +{ + if (tab && *tab) + { + free(*tab); + *tab = NULL; + } +} + +char *build_tab(char *tab, char *buffer, int i) +{ + char *ret; + size_t t_len; + size_t b_len; + + b_len = ft_gstrlen(buffer, -2); + t_len = ft_gstrlen(tab, i); + ret = malloc(b_len + t_len + 1); + if (!ret) + { + free(tab); + return (NULL); + } + if (tab) + { + ft_strlcpy(ret, tab, t_len + 1); + ft_strlcat(&ret[t_len], buffer, b_len + t_len + 1); + free(tab); + } + else + ft_strlcpy(ret, buffer, b_len + 1); + return (ret); +} + +char *get_the_line(char **tab, int i) +{ + char *line; + char *new_tab; + size_t len; + char *nl_pos; + + nl_pos = ft_strichr(*tab, '\n', i); + if (!nl_pos) + return (NULL); + len = nl_pos - *tab + 1; + line = malloc(len + 1); + if (!line) + return (NULL); + ft_strlcpy(line, *tab, len + 1); + line[len] = '\0'; + new_tab = ft_strdup(*tab + len); + if (!new_tab) + { + free(line); + return (NULL); + } + free(*tab); + *tab = new_tab; + return (line); +} + +int read_file(int fd, char **tab, int i) +{ + char buffer[BUFFER_SIZE + 1]; + ssize_t bytes_read; + + bytes_read = read(fd, buffer, BUFFER_SIZE); + if (bytes_read <= 0) + { + if (bytes_read == -1) + free_buf(&tab[fd]); + return (bytes_read); + } + buffer[bytes_read] = '\0'; + tab[fd] = build_tab(tab[fd], buffer, i); + if (!tab[fd]) + return (-1); + return (bytes_read); +} + +char *get_next_line(int fd) +{ + static char *tab[256]; + char *next_line; + int i; + + i = -1; + next_line = NULL; + if (fd < 0 || fd >= 256 || BUFFER_SIZE == 0) + return (NULL); + while (++i || 1) + { + next_line = get_the_line(&tab[fd], i); + if (next_line) + return (next_line); + if (read_file(fd, tab, i) <= 0) + break ; + } + if (tab[fd] && *tab[fd]) + { + next_line = tab[fd]; + tab[fd] = NULL; + return (next_line); + } + free(tab[fd]); + tab[fd] = NULL; + return (NULL); +} diff --git a/lib/libft/srcs/gnl/gnl.h b/lib/libft/srcs/gnl/gnl.h new file mode 100644 index 0000000..be0dfde --- /dev/null +++ b/lib/libft/srcs/gnl/gnl.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gnl.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/27 12:46:20 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:53:23 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GNL_H +# define GNL_H + +# include "../../libft.h" + +size_t ft_gstrlen(const char *str, int j); +char *ft_strichr(const char *s, int c, int i); + +#endif \ No newline at end of file diff --git a/lib/libft/srcs/gnl/gnl_utils.c b/lib/libft/srcs/gnl/gnl_utils.c new file mode 100644 index 0000000..c472ade --- /dev/null +++ b/lib/libft/srcs/gnl/gnl_utils.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* gnl_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/27 12:48:02 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:53:32 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" +#include "gnl.h" + +size_t ft_gstrlen(const char *str, int j) +{ + size_t i; + + i = 0; + if (!str) + return (0); + if (j == 0 || j == -2) + { + while (str[i]) + i++; + return (i); + } + else + { + if (j > 0) + j--; + if (j != 0) + i += (BUFFER_SIZE * j) - 1; + while (str[i]) + i++; + return (i); + } +} + +char *ft_strichr(const char *s, int c, int i) +{ + char *str; + char ch; + + if (!s) + return (0); + if (i > 0) + i--; + ch = c; + str = (char *)s; + if (i != 0) + str += (BUFFER_SIZE * i) - 1; + while (*str) + { + if (*str == ch) + return (str); + str++; + } + if ((ch == 0) && (*str == 0)) + return (str); + return (0); +} diff --git a/lib/libft/srcs/mem/ft_bzero.c b/lib/libft/srcs/mem/ft_bzero.c new file mode 100644 index 0000000..5b73882 --- /dev/null +++ b/lib/libft/srcs/mem/ft_bzero.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 16:07:05 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:55 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void ft_bzero(void *s, size_t n) +{ + char *ps; + size_t i; + + if (n == 0 || !s) + return ; + ps = (char *)s; + i = 0; + while (i < n) + ps[i++] = 0; +} diff --git a/lib/libft/srcs/mem/ft_memchr.c b/lib/libft/srcs/mem/ft_memchr.c new file mode 100644 index 0000000..6c1741f --- /dev/null +++ b/lib/libft/srcs/mem/ft_memchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 11:38:35 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:14:59 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + const unsigned char *ps; + + ps = (const unsigned char *)s; + i = -1; + while (++i < n) + if (ps[i] == (unsigned char)c) + return ((char *)&ps[i]); + return (NULL); +} diff --git a/lib/libft/srcs/mem/ft_memcmp.c b/lib/libft/srcs/mem/ft_memcmp.c new file mode 100644 index 0000000..734cb7f --- /dev/null +++ b/lib/libft/srcs/mem/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 11:54:32 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:04 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + const unsigned char *ps1; + const unsigned char *ps2; + + ps1 = (const unsigned char *)s1; + ps2 = (const unsigned char *)s2; + i = -1; + while (++i < n) + if (ps1[i] != ps2[i]) + return (ps1[i] - ps2[i]); + return (0); +} diff --git a/lib/libft/srcs/mem/ft_memcpy.c b/lib/libft/srcs/mem/ft_memcpy.c new file mode 100644 index 0000000..61cb2f8 --- /dev/null +++ b/lib/libft/srcs/mem/ft_memcpy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/11 09:21:39 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:09 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + unsigned char *pdst; + const unsigned char *psrc; + size_t i; + + if (!dst && !src) + return (0); + pdst = (unsigned char *)dst; + psrc = (const unsigned char *)src; + i = -1; + while (++i < n) + pdst[i] = psrc[i]; + return (dst); +} diff --git a/lib/libft/srcs/mem/ft_memmove.c b/lib/libft/srcs/mem/ft_memmove.c new file mode 100644 index 0000000..7960c9a --- /dev/null +++ b/lib/libft/srcs/mem/ft_memmove.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/12 09:37:46 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:14 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *pdst; + const unsigned char *psrc; + + if (!dest && !src) + return (0); + pdst = (unsigned char *)dest; + psrc = (const unsigned char *)src; + if (pdst < psrc) + { + while (n--) + *pdst++ = *psrc++; + } + else + { + pdst += n; + psrc += n; + while (n--) + *(--pdst) = *(--psrc); + } + return (dest); +} diff --git a/lib/libft/srcs/mem/ft_memset.c b/lib/libft/srcs/mem/ft_memset.c new file mode 100644 index 0000000..00fc321 --- /dev/null +++ b/lib/libft/srcs/mem/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 13:06:54 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:21 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + unsigned char *ptr; + size_t i; + + ptr = (unsigned char *)b; + i = 0; + while (i < len) + { + ptr[i] = c; + i++; + } + return (b); +} diff --git a/lib/libft/srcs/print/ft_printf.c b/lib/libft/srcs/print/ft_printf.c new file mode 100644 index 0000000..4287548 --- /dev/null +++ b/lib/libft/srcs/print/ft_printf.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/30 11:15:11 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:33 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static int is_format(char c) +{ + if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u' \ + || c == 'x' || c == 'X' || c == '%') + return (1); + return (0); +} + +static int ft_put_p(va_list ap) +{ + int count; + + if (ft_put_s("0x") == -1) + return (-1); + count = ft_put_uli_base((unsigned long)va_arg(ap, void *), BASE16); + if (count == -1) + return (-1); + return (count + 2); +} + +static int formater(va_list ap, const char *fstr) +{ + if (*fstr == 'c') + return (ft_put_c((char)va_arg(ap, int))); + if (*fstr == 's') + return (ft_put_s((char *)va_arg(ap, char *))); + if (*fstr == 'p') + return (ft_put_p(ap)); + if (*fstr == 'd' || *fstr == 'i') + return (ft_put_i((int)va_arg(ap, int))); + if (*fstr == 'u') + return (ft_put_ui((unsigned int)va_arg(ap, unsigned int))); + if (*fstr == 'x') + return (ft_put_ui_base((unsigned int)va_arg(ap, int), BASE16)); + if (*fstr == 'X') + return (ft_put_ui_base((unsigned int)va_arg(ap, int), BASE16UP)); + else + return (ft_put_s("%")); +} + +int ft_printf(const char *fstr, ...) +{ + va_list ap; + int count; + int tmp; + + count = 0; + va_start(ap, fstr); + while (*fstr != 0) + { + if (*fstr == '%' && is_format(*(fstr + 1))) + tmp = formater(ap, ++fstr); + else + tmp = ft_put_c(*fstr); + if (tmp == -1) + return (-1); + count += tmp; + fstr++; + } + va_end(ap); + return (count); +} + +int ft_debug(const char *fstr, ...) +{ + va_list ap; + int count; + int tmp; + + count = 0; + va_start(ap, fstr); + ft_put_s("\033[33m[DEBUG]\033[0m"); + while (*fstr != 0) + { + if (*fstr == '%' && is_format(*(fstr + 1))) + tmp = formater(ap, ++fstr); + else + tmp = ft_put_c(*fstr); + if (tmp == -1) + return (-1); + count += tmp; + fstr++; + } + va_end(ap); + return (count); +} diff --git a/lib/libft/srcs/print/ft_put.c b/lib/libft/srcs/print/ft_put.c new file mode 100644 index 0000000..478f280 --- /dev/null +++ b/lib/libft/srcs/print/ft_put.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 09:09:26 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:47 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_put_c(char c) +{ + return (write(1, &c, 1)); +} + +int ft_put_s(char *s) +{ + if (!s) + return (ft_put_s("(null)")); + return (write(1, s, ft_strlen(s))); +} + +int ft_put_i(int n) +{ + int count; + int tmp; + long nb; + + nb = n; + count = 0; + if (nb < 0) + { + if (write(1, "-", 1) == -1) + return (-1); + count ++; + nb = -nb; + } + if (nb >= 10) + { + tmp = ft_put_i(nb / 10); + if (tmp == -1) + return (-1); + count += tmp; + } + tmp = ft_put_c((nb % 10) + '0'); + if (tmp == -1) + return (-1); + return (tmp + count); +} + +int ft_put_ui(unsigned int n) +{ + char c; + int count; + int tmp; + + count = 0; + if (n >= 10) + { + tmp = ft_put_ui(n / 10); + if (tmp == -1) + return (-1); + count += tmp; + } + c = (n % 10) + '0'; + tmp = write(1, &c, 1); + if (tmp == -1) + return (-1); + return (tmp + count); +} diff --git a/lib/libft/srcs/print/ft_put_fd.c b/lib/libft/srcs/print/ft_put_fd.c new file mode 100644 index 0000000..af0c551 --- /dev/null +++ b/lib/libft/srcs/print/ft_put_fd.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 16:07:21 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:40:00 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void ft_put_i_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 >= 10) + { + ft_put_i_fd(n / 10, fd); + } + c = (n % 10) + '0'; + write(fd, &c, 1); +} + +void ft_put_s_fd(char *s, int fd) +{ + if (!s) + return ; + write(fd, s, ft_strlen(s)); +} + +void ft_put_c_fd(char c, int fd) +{ + write(fd, &c, 1); +} + +void ft_error(char *e) +{ + ft_put_s_fd(RED, 2); + ft_put_s_fd(e, 2); + ft_put_s_fd(RESET, 2); +} diff --git a/lib/libft/srcs/print/ft_put_u_base.c b/lib/libft/srcs/print/ft_put_u_base.c new file mode 100644 index 0000000..1bfde84 --- /dev/null +++ b/lib/libft/srcs/print/ft_put_u_base.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put_u_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/25 09:01:21 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:43 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_put_ui_base(unsigned int nbr, char *base) +{ + unsigned int base_len; + int count; + int tmp; + + count = 0; + base_len = ft_strlen(base); + if (base_len < 2) + return (0); + if (nbr >= base_len) + { + tmp = ft_put_ui_base(nbr / base_len, base); + if (tmp == -1) + return (-1); + count += tmp; + } + tmp = write(1, &base[nbr % base_len], 1); + if (tmp == -1) + return (-1); + return (tmp + count); +} + +int ft_put_uli_base(unsigned long int nbr, char *base) +{ + unsigned long base_len; + int count; + int tmp; + + count = 0; + base_len = ft_strlen(base); + if (base_len < 2) + return (0); + if (nbr >= base_len) + count += ft_put_uli_base(nbr / base_len, base); + tmp = write(1, &base[nbr % base_len], 1); + if (tmp == -1) + return (-1); + return (tmp + count); +} diff --git a/lib/libft/srcs/print/ft_putendl_fd.c b/lib/libft/srcs/print/ft_putendl_fd.c new file mode 100644 index 0000000..bc46855 --- /dev/null +++ b/lib/libft/srcs/print/ft_putendl_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/15 18:01:01 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:50 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + int d; + + d = 10; + write(fd, s, ft_strlen(s)); + write(fd, &d, 1); +} diff --git a/lib/libft/srcs/str/ft_strchr.c b/lib/libft/srcs/str/ft_strchr.c new file mode 100644 index 0000000..868c13b --- /dev/null +++ b/lib/libft/srcs/str/ft_strchr.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 09:06:01 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:15:57 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strchr(const char *s, int c) +{ + int i; + char ch; + + ch = c; + i = 0; + if (ch == '\0') + return ((char *)&s[ft_strlen(s)]); + while (s[i] != 0) + { + if (s[i] == ch) + return ((char *)&s[i]); + i++; + } + return (NULL); +} diff --git a/lib/libft/srcs/str/ft_strdup.c b/lib/libft/srcs/str/ft_strdup.c new file mode 100644 index 0000000..31ea065 --- /dev/null +++ b/lib/libft/srcs/str/ft_strdup.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 15:38:09 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:02 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strdup(const char *s1) +{ + char *out; + unsigned long int i; + + out = ft_calloc(ft_strlen(s1) + 1, sizeof(char)); + if (!out || !s1) + return (NULL); + i = 0; + while (s1[i]) + { + out[i] = s1[i]; + i++; + } + return (out); +} diff --git a/lib/libft/srcs/str/ft_striteri.c b/lib/libft/srcs/str/ft_striteri.c new file mode 100644 index 0000000..2d5eedd --- /dev/null +++ b/lib/libft/srcs/str/ft_striteri.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/15 17:17:33 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:05 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + size_t i; + + if (!s || !f) + return ; + i = -1; + while (s[++i]) + f(i, &s[i]); +} diff --git a/lib/libft/srcs/str/ft_strjoin.c b/lib/libft/srcs/str/ft_strjoin.c new file mode 100644 index 0000000..2cde769 --- /dev/null +++ b/lib/libft/srcs/str/ft_strjoin.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 17:14:32 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:11 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strjoin(const char *s1, const char *s2) +{ + char *out; + unsigned int i; + unsigned int j; + + if (!s1 && !s2) + return (NULL); + if (!s1) + return (ft_strdup(s2)); + if (!s2) + return (ft_strdup(s1)); + out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char)); + if (!out) + return (NULL); + i = 0; + j = 0; + while (s1[j]) + out[i++] = s1[j++]; + j = 0; + while (s2[j]) + out[i++] = s2[j++]; + return (out); +} diff --git a/lib/libft/srcs/str/ft_strlcat.c b/lib/libft/srcs/str/ft_strlcat.c new file mode 100644 index 0000000..4f079da --- /dev/null +++ b/lib/libft/srcs/str/ft_strlcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/12 10:07:49 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:15 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t dst_len; + size_t src_len; + size_t i; + + if (!dst || !src) + return (0); + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + if (dstsize <= dst_len) + return (dstsize + src_len); + i = -1; + while (src[++i] && (dst_len + i) < (dstsize - 1)) + dst[dst_len + i] = src[i]; + dst[dst_len + i] = '\0'; + return (dst_len + src_len); +} diff --git a/lib/libft/srcs/str/ft_strlcpy.c b/lib/libft/srcs/str/ft_strlcpy.c new file mode 100644 index 0000000..8a7a50e --- /dev/null +++ b/lib/libft/srcs/str/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/12 10:07:49 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:20 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) +{ + size_t i; + + if (!dst || !src) + return (0); + if (dstsize == 0) + return (ft_strlen(src)); + i = -1; + while (++i < dstsize - 1 && src[i]) + dst[i] = src[i]; + dst[i] = 0; + return (ft_strlen(src)); +} diff --git a/lib/libft/srcs/str/ft_strlen.c b/lib/libft/srcs/str/ft_strlen.c new file mode 100644 index 0000000..dac85cc --- /dev/null +++ b/lib/libft/srcs/str/ft_strlen.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/10 12:01:43 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:24 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +size_t ft_strlen(const char *s) +{ + size_t i; + + if (!s) + return (0); + i = 0; + while (s[i] != 0) + i++; + return (i); +} diff --git a/lib/libft/srcs/str/ft_strmapi.c b/lib/libft/srcs/str/ft_strmapi.c new file mode 100644 index 0000000..ef7dad5 --- /dev/null +++ b/lib/libft/srcs/str/ft_strmapi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/15 16:32:03 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:28 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t len; + unsigned int i; + char *out; + + if (!s || !f) + return (NULL); + len = ft_strlen(s); + out = (char *)ft_calloc(len + 1, sizeof(char)); + if (!out) + return (NULL); + i = -1; + while (++i < len) + out[i] = f(i, s[i]); + out[len] = '\0'; + return (out); +} diff --git a/lib/libft/srcs/str/ft_strncmp.c b/lib/libft/srcs/str/ft_strncmp.c new file mode 100644 index 0000000..bb67384 --- /dev/null +++ b/lib/libft/srcs/str/ft_strncmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 10:58:07 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:32 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + unsigned char *ss1; + unsigned char *ss2; + + if (!s1 || !s2) + return (0); + i = 0; + ss1 = (unsigned char *)s1; + ss2 = (unsigned char *)s2; + if (n == 0) + return (0); + while (i < n && ss1[i] && ss2[i] && ss1[i] == ss2[i]) + i++; + if (i == n) + return (0); + return (ss1[i] - ss2[i]); +} diff --git a/lib/libft/srcs/str/ft_strnstr.c b/lib/libft/srcs/str/ft_strnstr.c new file mode 100644 index 0000000..aaa3f2c --- /dev/null +++ b/lib/libft/srcs/str/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 12:45:20 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:35 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t i; + size_t j; + + i = 0; + if (!haystack || !needle) + return (NULL); + if (needle[0] == 0) + return ((char *)haystack); + while (haystack[i] && i < len) + { + j = 0; + while (haystack[i + j] == needle[j] && i + j < len) + { + j++; + if (needle[j] == 0) + return ((char *)(haystack + i)); + } + i++; + } + return (NULL); +} diff --git a/lib/libft/srcs/str/ft_strrchr.c b/lib/libft/srcs/str/ft_strrchr.c new file mode 100644 index 0000000..4bfb843 --- /dev/null +++ b/lib/libft/srcs/str/ft_strrchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 09:06:01 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:38 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + if (!s) + return (NULL); + i = ft_strlen(s) + 1; + while (--i >= 0) + if (s[i] == (char)c) + return ((char *)&s[i]); + return (NULL); +} diff --git a/lib/libft/srcs/str/ft_strtrim.c b/lib/libft/srcs/str/ft_strtrim.c new file mode 100644 index 0000000..a2e7582 --- /dev/null +++ b/lib/libft/srcs/str/ft_strtrim.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 17:42:56 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:44 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static int ft_instr(int c, char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (str[i] == c) + return (1); + return (0); +} + +static int ft_gettrimstart(const char *str, const char *set) +{ + int i; + + i = 0; + while (ft_instr(str[i], (char *)set) && str[i]) + i++; + return (i); +} + +static int ft_gettrimend(const char *str, const char *set) +{ + int i; + + i = 0; + while (ft_instr(str[ft_strlen(str) - i - 1], (char *)set)) + i++; + return (i); +} + +static void trloop(char *cpy, const char *s1, size_t trimstart, size_t trimend) +{ + size_t i; + + i = 0; + while (i < ft_strlen(s1) - (trimstart + trimend)) + { + cpy[i] = s1[trimstart + i]; + i++; + } + cpy[i] = 0; +} + +char *ft_strtrim(const char *s1, const char *set) +{ + char *cpy; + size_t trimstart; + size_t trimend; + + if (!s1 || !set) + return (NULL); + cpy = (char *)s1; + trimstart = ft_gettrimstart(s1, set); + if (trimstart == ft_strlen(s1)) + return (ft_strdup("")); + trimend = ft_gettrimend(s1, set); + cpy = ft_calloc(ft_strlen(s1) - (trimstart + trimend) + 1, sizeof(char)); + if (!cpy) + return (NULL); + trloop(cpy, s1, trimstart, trimend); + return (cpy); +} diff --git a/lib/libft/srcs/str/ft_substr.c b/lib/libft/srcs/str/ft_substr.c new file mode 100644 index 0000000..b82bf3a --- /dev/null +++ b/lib/libft/srcs/str/ft_substr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/14 16:17:16 by nalebrun #+# #+# */ +/* Updated: 2024/11/25 16:16:49 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + char *sub; + size_t i; + + if (!s) + return (NULL); + if (start >= ft_strlen(s)) + return (ft_strdup("")); + if ((size_t)start + len > ft_strlen(s)) + sub = ft_calloc(ft_strlen(s) - (size_t)start + 1, sizeof(char)); + else + sub = ft_calloc(len + 1, sizeof(char)); + if (!sub) + return (NULL); + i = 0; + while (sub && i < len && s[start + i] != 0) + { + sub[i] = s[start + i]; + i++; + } + return (sub); +} diff --git a/lib/libft/srcs/str/ft_tolower.c b/lib/libft/srcs/str/ft_tolower.c new file mode 100644 index 0000000..56c978f --- /dev/null +++ b/lib/libft/srcs/str/ft_tolower.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/12 12:07:21 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:58:25 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_tolower(int c) +{ + if (c >= 65 && c <= 90) + return (c + 32); + else + return (c); +} diff --git a/lib/libft/srcs/str/ft_toupper.c b/lib/libft/srcs/str/ft_toupper.c new file mode 100644 index 0000000..c8a56ea --- /dev/null +++ b/lib/libft/srcs/str/ft_toupper.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/12 12:01:30 by nalebrun #+# #+# */ +/* Updated: 2024/11/27 12:58:24 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +int ft_toupper(int c) +{ + if (c >= 97 && c <= 122) + return (c - 32); + else + return (c); +} diff --git a/minishell.h b/minishell.h new file mode 100644 index 0000000..e69de29 diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..ffd8381 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,4 @@ +int main (int ac, char **av, char **envp) +{ + +}