From 2a2f5bd4d3b8ad06925fdb14eecdc070f3f44316 Mon Sep 17 00:00:00 2001 From: tvdu29 Date: Fri, 31 Jan 2020 15:06:17 +0100 Subject: [PATCH] Dev (#2) * Restart from scratch branched out on dev removed all previous source files started test on new env2lst function * norm: splitted functions in main into appropriate file Moved lstdelenvelem & env2lst to ms_env.c Normed out files * Feat(WIP): Added possibility to cmd_env to execute commands with altered env. Added dummy function for cd, echo, setenv & unsetenv Started work on env copy feature * feature: switched env from ft_list to specific list * feature: added execution of commands from path * feature: added env, setenv & unsetenv builtins added -i option & command execution to env added setenv & unsetenv builtins * Clean-up: normed out files * Changed comportment on error for check_path functions * feature: added completion for ~ and ${} WIP leaks detected on ${} completion * fix leak on ext need test * feature: added echo cmd changed printf ref to ft_printf * feature: add cd built-in * Simple norm-out * moved env functions to libft * fixed out-of-memory access in extension * Resolved infinite loop in extension revolving WIP norming in cd * Normedout cmd_cd.c * Normed out functions in cmd_cd * Normed out funtions sorting out needed * removed -fsanitize from Makefile * corrected env -i crash & SHLVL * Delete norm.txt * added put_error_cd2 in put_error_cd.c * added inline environment --- .ccls | 4 + .gitmodules | 3 - Makefile | 118 +++++-- includes/minishell.h | 45 +-- libft | 1 - libft/.gitignore | 52 +++ libft/Makefile | 175 ++++++++++ libft/auteur | 1 + libft/includes/libft.h | 384 ++++++++++++++++++++++ libft/srcs/ft_abs.c | 20 ++ libft/srcs/ft_atoi.c | 40 +++ libft/srcs/ft_atois.c | 41 +++ libft/srcs/ft_bzero.c | 18 + libft/srcs/ft_close.c | 22 ++ libft/srcs/ft_del_words_tables.c | 24 ++ libft/srcs/ft_envaddend.c | 31 ++ libft/srcs/ft_envclean.c | 32 ++ libft/srcs/ft_envdel.c | 25 ++ libft/srcs/ft_envdelelem.c | 23 ++ libft/srcs/ft_envgetelem.c | 23 ++ libft/srcs/ft_envnew.c | 29 ++ libft/srcs/ft_envtochar.c | 28 ++ libft/srcs/ft_envtotab.c | 41 +++ libft/srcs/ft_envupdate.c | 28 ++ libft/srcs/ft_getline.c | 87 +++++ libft/srcs/ft_gets.c | 13 + libft/srcs/ft_hasdigit.c | 23 ++ libft/srcs/ft_isalnum.c | 18 + libft/srcs/ft_isalpha.c | 18 + libft/srcs/ft_isascii.c | 18 + libft/srcs/ft_isdigit.c | 18 + libft/srcs/ft_islower.c | 18 + libft/srcs/ft_isprint.c | 18 + libft/srcs/ft_issign.c | 18 + libft/srcs/ft_isupper.c | 18 + libft/srcs/ft_iswhitespace.c | 19 ++ libft/srcs/ft_itoa.c | 41 +++ libft/srcs/ft_lstadd.c | 19 ++ libft/srcs/ft_lstaddend.c | 29 ++ libft/srcs/ft_lstaddsort.c | 31 ++ libft/srcs/ft_lstdel.c | 23 ++ libft/srcs/ft_lstdelone.c | 20 ++ libft/srcs/ft_lstgetat.c | 28 ++ libft/srcs/ft_lstgetlast.c | 18 + libft/srcs/ft_lstiter.c | 25 ++ libft/srcs/ft_lstmap.c | 37 +++ libft/srcs/ft_lstnew.c | 39 +++ libft/srcs/ft_lstsize.c | 21 ++ libft/srcs/ft_lstsort.c | 30 ++ libft/srcs/ft_memalloc.c | 22 ++ libft/srcs/ft_memccpy.c | 27 ++ libft/srcs/ft_memchr.c | 26 ++ libft/srcs/ft_memcmp.c | 31 ++ libft/srcs/ft_memcpy.c | 23 ++ libft/srcs/ft_memdel.c | 22 ++ libft/srcs/ft_memmove.c | 30 ++ libft/srcs/ft_memset.c | 23 ++ libft/srcs/ft_nbrlen.c | 30 ++ libft/srcs/ft_open.c | 28 ++ libft/srcs/ft_print_words_tables.c | 25 ++ libft/srcs/ft_printf.c | 91 +++++ libft/srcs/ft_printf_check_fields.c | 142 ++++++++ libft/srcs/ft_printf_check_type.c | 69 ++++ libft/srcs/ft_printf_tools.c | 49 +++ libft/srcs/ft_printf_tools_char.c | 30 ++ libft/srcs/ft_printf_tools_float.c | 122 +++++++ libft/srcs/ft_printf_tools_hexa.c | 120 +++++++ libft/srcs/ft_printf_tools_int.c | 113 +++++++ libft/srcs/ft_printf_tools_lenght.c | 50 +++ libft/srcs/ft_printf_tools_malloc_size.c | 79 +++++ libft/srcs/ft_printf_tools_oct.c | 110 +++++++ libft/srcs/ft_printf_tools_prec_size.c | 97 ++++++ libft/srcs/ft_printf_tools_str.c | 64 ++++ libft/srcs/ft_printf_tools_unsigned_int.c | 99 ++++++ libft/srcs/ft_printf_tools_wchar_t.c | 109 ++++++ libft/srcs/ft_printf_tools_wstr.c | 89 +++++ libft/srcs/ft_putchar.c | 18 + libft/srcs/ft_putchar_fd.c | 18 + libft/srcs/ft_putendl.c | 20 ++ libft/srcs/ft_putendl2.c | 28 ++ libft/srcs/ft_putendl_fd.c | 22 ++ libft/srcs/ft_putendl_fd2.c | 28 ++ libft/srcs/ft_putnbr.c | 18 + libft/srcs/ft_putnbr_fd.c | 33 ++ libft/srcs/ft_putnbrpad.c | 24 ++ libft/srcs/ft_putstr.c | 19 ++ libft/srcs/ft_putstr_fd.c | 19 ++ libft/srcs/ft_putstrn.c | 24 ++ libft/srcs/ft_putstrpad.c | 24 ++ libft/srcs/ft_realpath.c | 77 +++++ libft/srcs/ft_round.c | 20 ++ libft/srcs/ft_sort_params.c | 32 ++ libft/srcs/ft_str_is_alpha.c | 24 ++ libft/srcs/ft_str_is_lowercase.c | 24 ++ libft/srcs/ft_str_is_numeric.c | 24 ++ libft/srcs/ft_str_is_printable.c | 24 ++ libft/srcs/ft_str_is_uppercase.c | 24 ++ libft/srcs/ft_strcapitalize.c | 29 ++ libft/srcs/ft_strcat.c | 22 ++ libft/srcs/ft_strchr.c | 29 ++ libft/srcs/ft_strclr.c | 23 ++ libft/srcs/ft_strcmp.c | 28 ++ libft/srcs/ft_strcpy.c | 23 ++ libft/srcs/ft_strdel.c | 18 + libft/srcs/ft_strdup.c | 22 ++ libft/srcs/ft_strequ.c | 18 + libft/srcs/ft_striter.c | 23 ++ libft/srcs/ft_striteri.c | 23 ++ libft/srcs/ft_strjoin.c | 27 ++ libft/srcs/ft_strlcat.c | 27 ++ libft/srcs/ft_strlcpy.c | 26 ++ libft/srcs/ft_strlen.c | 23 ++ libft/srcs/ft_strlowcase.c | 24 ++ libft/srcs/ft_strmap.c | 32 ++ libft/srcs/ft_strmapi.c | 32 ++ libft/srcs/ft_strncat.c | 29 ++ libft/srcs/ft_strnchr.c | 26 ++ libft/srcs/ft_strncmp.c | 31 ++ libft/srcs/ft_strncpy.c | 25 ++ libft/srcs/ft_strndup.c | 22 ++ libft/srcs/ft_strnequ.c | 18 + libft/srcs/ft_strnew.c | 18 + libft/srcs/ft_strnstr.c | 35 ++ libft/srcs/ft_strrchr.c | 28 ++ libft/srcs/ft_strrnchr.c | 28 ++ libft/srcs/ft_strsplit.c | 80 +++++ libft/srcs/ft_strsplitwhitespace.c | 80 +++++ libft/srcs/ft_strstr.c | 34 ++ libft/srcs/ft_strsub.c | 23 ++ libft/srcs/ft_strtrim.c | 42 +++ libft/srcs/ft_strupcase.c | 24 ++ libft/srcs/ft_tolower.c | 18 + libft/srcs/ft_toupper.c | 18 + libft/srcs/get_next_line.c | 98 ++++++ srcs/cmd_cd.c | 140 ++++---- srcs/cmd_echo.c | 17 +- srcs/cmd_env.c | 167 ++++------ srcs/cmd_setenv.c | 33 +- srcs/cmd_unsetenv.c | 32 +- srcs/exec.c | 222 ------------- srcs/main.c | 203 +++++++----- srcs/ms_env.c | 99 ------ srcs/ms_env2.c | 85 ----- srcs/ms_exec.c | 183 +++++++++++ srcs/ms_ext.c | 117 +++++++ srcs/put_error_cd.c | 31 ++ 146 files changed, 5802 insertions(+), 772 deletions(-) create mode 100644 .ccls delete mode 100644 .gitmodules delete mode 160000 libft create mode 100644 libft/.gitignore create mode 100644 libft/Makefile create mode 100644 libft/auteur create mode 100644 libft/includes/libft.h create mode 100644 libft/srcs/ft_abs.c create mode 100644 libft/srcs/ft_atoi.c create mode 100644 libft/srcs/ft_atois.c create mode 100644 libft/srcs/ft_bzero.c create mode 100644 libft/srcs/ft_close.c create mode 100644 libft/srcs/ft_del_words_tables.c create mode 100644 libft/srcs/ft_envaddend.c create mode 100644 libft/srcs/ft_envclean.c create mode 100644 libft/srcs/ft_envdel.c create mode 100644 libft/srcs/ft_envdelelem.c create mode 100644 libft/srcs/ft_envgetelem.c create mode 100644 libft/srcs/ft_envnew.c create mode 100644 libft/srcs/ft_envtochar.c create mode 100644 libft/srcs/ft_envtotab.c create mode 100644 libft/srcs/ft_envupdate.c create mode 100644 libft/srcs/ft_getline.c create mode 100644 libft/srcs/ft_gets.c create mode 100644 libft/srcs/ft_hasdigit.c create mode 100644 libft/srcs/ft_isalnum.c create mode 100644 libft/srcs/ft_isalpha.c create mode 100644 libft/srcs/ft_isascii.c create mode 100644 libft/srcs/ft_isdigit.c create mode 100644 libft/srcs/ft_islower.c create mode 100644 libft/srcs/ft_isprint.c create mode 100644 libft/srcs/ft_issign.c create mode 100644 libft/srcs/ft_isupper.c create mode 100644 libft/srcs/ft_iswhitespace.c create mode 100644 libft/srcs/ft_itoa.c create mode 100644 libft/srcs/ft_lstadd.c create mode 100644 libft/srcs/ft_lstaddend.c create mode 100644 libft/srcs/ft_lstaddsort.c create mode 100644 libft/srcs/ft_lstdel.c create mode 100644 libft/srcs/ft_lstdelone.c create mode 100644 libft/srcs/ft_lstgetat.c create mode 100644 libft/srcs/ft_lstgetlast.c create mode 100644 libft/srcs/ft_lstiter.c create mode 100644 libft/srcs/ft_lstmap.c create mode 100644 libft/srcs/ft_lstnew.c create mode 100644 libft/srcs/ft_lstsize.c create mode 100644 libft/srcs/ft_lstsort.c create mode 100644 libft/srcs/ft_memalloc.c create mode 100644 libft/srcs/ft_memccpy.c create mode 100644 libft/srcs/ft_memchr.c create mode 100644 libft/srcs/ft_memcmp.c create mode 100644 libft/srcs/ft_memcpy.c create mode 100644 libft/srcs/ft_memdel.c create mode 100644 libft/srcs/ft_memmove.c create mode 100644 libft/srcs/ft_memset.c create mode 100644 libft/srcs/ft_nbrlen.c create mode 100644 libft/srcs/ft_open.c create mode 100644 libft/srcs/ft_print_words_tables.c create mode 100644 libft/srcs/ft_printf.c create mode 100644 libft/srcs/ft_printf_check_fields.c create mode 100644 libft/srcs/ft_printf_check_type.c create mode 100644 libft/srcs/ft_printf_tools.c create mode 100644 libft/srcs/ft_printf_tools_char.c create mode 100644 libft/srcs/ft_printf_tools_float.c create mode 100644 libft/srcs/ft_printf_tools_hexa.c create mode 100644 libft/srcs/ft_printf_tools_int.c create mode 100644 libft/srcs/ft_printf_tools_lenght.c create mode 100644 libft/srcs/ft_printf_tools_malloc_size.c create mode 100644 libft/srcs/ft_printf_tools_oct.c create mode 100644 libft/srcs/ft_printf_tools_prec_size.c create mode 100644 libft/srcs/ft_printf_tools_str.c create mode 100644 libft/srcs/ft_printf_tools_unsigned_int.c create mode 100644 libft/srcs/ft_printf_tools_wchar_t.c create mode 100644 libft/srcs/ft_printf_tools_wstr.c create mode 100644 libft/srcs/ft_putchar.c create mode 100644 libft/srcs/ft_putchar_fd.c create mode 100644 libft/srcs/ft_putendl.c create mode 100644 libft/srcs/ft_putendl2.c create mode 100644 libft/srcs/ft_putendl_fd.c create mode 100644 libft/srcs/ft_putendl_fd2.c create mode 100644 libft/srcs/ft_putnbr.c create mode 100644 libft/srcs/ft_putnbr_fd.c create mode 100644 libft/srcs/ft_putnbrpad.c create mode 100644 libft/srcs/ft_putstr.c create mode 100644 libft/srcs/ft_putstr_fd.c create mode 100644 libft/srcs/ft_putstrn.c create mode 100644 libft/srcs/ft_putstrpad.c create mode 100644 libft/srcs/ft_realpath.c create mode 100644 libft/srcs/ft_round.c create mode 100644 libft/srcs/ft_sort_params.c create mode 100644 libft/srcs/ft_str_is_alpha.c create mode 100644 libft/srcs/ft_str_is_lowercase.c create mode 100644 libft/srcs/ft_str_is_numeric.c create mode 100644 libft/srcs/ft_str_is_printable.c create mode 100644 libft/srcs/ft_str_is_uppercase.c create mode 100644 libft/srcs/ft_strcapitalize.c create mode 100644 libft/srcs/ft_strcat.c create mode 100644 libft/srcs/ft_strchr.c create mode 100644 libft/srcs/ft_strclr.c create mode 100644 libft/srcs/ft_strcmp.c create mode 100644 libft/srcs/ft_strcpy.c create mode 100644 libft/srcs/ft_strdel.c create mode 100644 libft/srcs/ft_strdup.c create mode 100644 libft/srcs/ft_strequ.c create mode 100644 libft/srcs/ft_striter.c create mode 100644 libft/srcs/ft_striteri.c create mode 100644 libft/srcs/ft_strjoin.c create mode 100644 libft/srcs/ft_strlcat.c create mode 100644 libft/srcs/ft_strlcpy.c create mode 100644 libft/srcs/ft_strlen.c create mode 100644 libft/srcs/ft_strlowcase.c create mode 100644 libft/srcs/ft_strmap.c create mode 100644 libft/srcs/ft_strmapi.c create mode 100644 libft/srcs/ft_strncat.c create mode 100644 libft/srcs/ft_strnchr.c create mode 100644 libft/srcs/ft_strncmp.c create mode 100644 libft/srcs/ft_strncpy.c create mode 100644 libft/srcs/ft_strndup.c create mode 100644 libft/srcs/ft_strnequ.c create mode 100644 libft/srcs/ft_strnew.c create mode 100644 libft/srcs/ft_strnstr.c create mode 100644 libft/srcs/ft_strrchr.c create mode 100644 libft/srcs/ft_strrnchr.c create mode 100644 libft/srcs/ft_strsplit.c create mode 100644 libft/srcs/ft_strsplitwhitespace.c create mode 100644 libft/srcs/ft_strstr.c create mode 100644 libft/srcs/ft_strsub.c create mode 100644 libft/srcs/ft_strtrim.c create mode 100644 libft/srcs/ft_strupcase.c create mode 100644 libft/srcs/ft_tolower.c create mode 100644 libft/srcs/ft_toupper.c create mode 100644 libft/srcs/get_next_line.c delete mode 100644 srcs/exec.c delete mode 100644 srcs/ms_env.c delete mode 100644 srcs/ms_env2.c create mode 100644 srcs/ms_exec.c create mode 100644 srcs/ms_ext.c create mode 100644 srcs/put_error_cd.c diff --git a/.ccls b/.ccls new file mode 100644 index 0000000..fd46537 --- /dev/null +++ b/.ccls @@ -0,0 +1,4 @@ +clang +-Iincludes +-Ilibft/includes +%c %h -Wall -Wextra -Werror -g-Wall -Wextra -Werror -g \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index b03f5d3..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "libft"] - path = libft - url = https://github.com/tvdu29/libft diff --git a/Makefile b/Makefile index c915c5a..e24b2d0 100644 --- a/Makefile +++ b/Makefile @@ -5,45 +5,107 @@ # +:+ +:+ +:+ # # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # -# Created: 2018/11/22 14:43:33 by tmaze #+# #+# # -# Updated: 2019/02/01 10:20:18 by tmaze ### ########.fr # +# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # +# Updated: 2020/01/30 12:00:42 by tmaze ### ########.fr # # # #******************************************************************************# -CC := gcc -CCFLAGS := -Wall -Werror -Wextra -g -CCSTD := +NAME = minishell -NAME := minishell +# Make options +MAKEFLAGS += --no-print-directory -SRCS := main.c exec.c cmd_echo.c cmd_cd.c cmd_env.c cmd_setenv.c cmd_unsetenv.c ms_env.c ms_env2.c -OBJS_DIR := objs -OBJS := $(SRCS:.c=.o) -INCLS := -Iincludes -Ilibft -LIBS := -Llibft -lft +# Output +UNAME := $(shell uname) +ifeq ($(UNAME), Darwin) +ECHO = @echo +endif -.PHONY = all clean fclean re +ifeq ($(UNAME), Linux) +ECHO = @echo -e +endif -all: $(NAME) +# Compilator +CC = gcc +FLAGS = -Wall -Wextra -Werror -$(NAME): $(OBJS) libft/libft.a - $(CC) $(CCFLAGS) $(CCSTD) $(INCLS) $(OBJS) -o $(NAME) $(LIBS) +# Folders +LIBDIR = libft +SRCDIR = srcs +OBJDIR = objs +INCDIR = includes libft/includes -%.o: srcs/%.c includes/minishell.h - $(CC) $(CCFLAGS) $(CCSTD) $(INCLS) -c $< -o $@ +# Source files +SRC = main.c \ + cmd_env.c \ + cmd_setenv.c \ + cmd_unsetenv.c \ + cmd_cd.c \ + cmd_echo.c \ + ms_exec.c \ + ms_ext.c \ + put_error_cd.c -$(OBJS_DIR): - mkdir $(OBJS_DIR) +OBJ = $(SRC:.c=.o) -libft/libft.a: - $(MAKE) -Clibft all +LIB = ft +LIBFILE = libft.a -clean: - rm -rf $(OBJS) - $(MAKE) -Clibft clean +# Prefixes +OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) +INCP = $(foreach dir, $(INCDIR), -I$(dir)) -fclean: clean - rm -f $(NAME) - $(MAKE) -Clibft fclean +# Default Rule +DRULE = all -re: fclean all +# Main rules +default : + @$(ECHO) -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) START RULE : $(DRULE) <===$(DEF)" + @make $(DRULE) + @$(ECHO) -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) END RULE : $(DRULE) <===$(DEF)" + +all : $(NAME) + +# Compilation rules + +$(OBJDIR)/%.o : $(SRCDIR)/%.c includes/minishell.h + @mkdir -p $(OBJDIR) + $(CC) $(FLAGS) -c -o $@ $< $(INCP) + +$(LIBDIR)/$(LIBFILE) : + @$(ECHO) "$(YEL)===> $(GRE)$(NAME) : $(YEL) Librairy Compilation <===$(DEF)" + $(MAKE) -C $(LIBDIR) all + +$(NAME) : $(OBJP) $(LIBDIR)/$(LIBFILE) + @$(ECHO) "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)" + $(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB) + +# Cleaner rules +clean : + @$(ECHO) "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Object Files <===$(DEF)" + @$(RM) -rf $(OBJDIR) + @$(MAKE) -C $(LIBDIR) clean + +fclean : clean + @$(ECHO) "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)" + @$(RM) -f $(NAME) $(NAME2) + @$(RM) -rf *.dSYM + @$(MAKE) -C $(LIBDIR) fclean + +re : fclean default + +# Phony +.PHONY = default all re clean fclean +# Color +DEF = \033[0m +BLA = \033[30m +BLI = \033[5m +BLU = \033[34m +CYA = \033[36m +GRA = \033[1m +GRE = \033[32m +PUR = \033[35m +RED = \033[31m +SOU = \033[4m +WHI = \033[37m +YEL = \033[33m diff --git a/includes/minishell.h b/includes/minishell.h index a22817d..57addc9 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */ -/* Updated: 2019/02/01 10:19:53 by tmaze ### ########.fr */ +/* Updated: 2020/01/26 22:19:25 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,37 +17,26 @@ # include # include "libft.h" -typedef struct s_builtin +# define S_BIN 5 + +typedef struct s_builtin { - char *cmd; - int (*f)(char **argv, t_list **env); -} t_builtin; + char *cmd; + int (*f)(char **argv, t_env **env); +} t_builtin; -typedef struct s_envelem -{ - char *key; - char *val; -} t_envelem; +int cmd_env(char **argv, t_env **env); +int cmd_setenv(char **argv, t_env **env); +int cmd_unsetenv(char **argv, t_env **env); +int cmd_cd(char **argv, t_env **env); +int cmd_echo(char **argv, t_env **env); -t_envelem *elem_new(char *key, char *val); -t_envelem *env_getelemfromkey(char *key, t_list *env); -t_envelem *env_addupdate(char *key, char *val, t_list **env); -void env_delelem(char *key, t_list **env); +char **res_ext(char **argv, t_env *env); -t_envelem *ft_envtoenvelem(char *env); -void ft_lstdelenvelem(void *content, size_t size); -void ft_envelemdel(t_envelem **env); -t_list *ft_envtolst(char **env); +int exec_cmd(char **argv, t_env **env); +void *ft_strdel_null(char **s); -int exec_cmd(char **argv, t_list **env); -int cmd_echo(char **argv, t_list **env); -int cmd_cd(char **argv, t_list **env); -int cmd_env(char **argv, t_list **env); -int cmd_setenv(char **argv, t_list **env); -int cmd_unsetenv(char **argv, t_list **env); - -int ft_isin(char *str, char c); -char *check_path_slash(char *exec); -char *check_path_dot(char *exec, t_list *env); +void *put_error_cd(char *file, char *msg); +int put_error_cd2(char *file, char *msg); #endif diff --git a/libft b/libft deleted file mode 160000 index 4b44c49..0000000 --- a/libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4b44c49fc6caa15379cd821f967e53462d4dc837 diff --git a/libft/.gitignore b/libft/.gitignore new file mode 100644 index 0000000..6d5206b --- /dev/null +++ b/libft/.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 \ No newline at end of file diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..551776b --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,175 @@ +#******************************************************************************# +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tmaze +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/04/07 12:47:06 by tmaze #+# #+# # +# Updated: 2020/01/25 16:24:08 by tmaze ### ########.fr # +# # +#******************************************************************************# + +CC = gcc +CCFLAGS = -Wall -Werror -Wextra +CCSTD = + +NAME = libft.a + +SRCS = ft_memalloc.c \ + ft_memdel.c \ + ft_memset.c \ + ft_bzero.c \ + ft_memcpy.c \ + ft_memccpy.c \ + ft_memmove.c \ + ft_memchr.c \ + ft_memcmp.c \ + \ + ft_isalpha.c \ + ft_isdigit.c \ + ft_isalnum.c \ + ft_isascii.c \ + ft_isprint.c \ + ft_toupper.c \ + ft_tolower.c \ + ft_isupper.c \ + ft_islower.c \ + ft_iswhitespace.c \ + ft_issign.c \ + \ + ft_strnew.c \ + ft_strdel.c \ + ft_strclr.c \ + ft_striter.c \ + ft_striteri.c \ + ft_strmap.c \ + ft_strmapi.c \ + ft_strequ.c \ + ft_strnequ.c \ + ft_strsub.c \ + ft_strjoin.c \ + ft_strtrim.c \ + ft_strsplit.c \ + ft_strlen.c \ + ft_strdup.c \ + ft_strcpy.c \ + ft_strncpy.c \ + ft_strcat.c \ + ft_strncat.c \ + ft_strlcat.c \ + ft_strchr.c \ + ft_strrchr.c \ + ft_strstr.c \ + ft_strnstr.c \ + ft_strcmp.c \ + ft_strncmp.c \ + ft_str_is_alpha.c \ + ft_str_is_lowercase.c \ + ft_str_is_numeric.c \ + ft_str_is_printable.c \ + ft_str_is_uppercase.c \ + ft_strcapitalize.c \ + ft_strlcpy.c \ + ft_strlowcase.c \ + ft_strupcase.c \ + ft_strndup.c \ + ft_strnchr.c \ + ft_strrnchr.c \ + ft_strsplitwhitespace.c \ + ft_hasdigit.c \ + ft_del_words_tables.c \ + \ + ft_putchar.c \ + ft_putstr.c \ + ft_putendl.c \ + ft_putnbr.c \ + ft_putchar_fd.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstrpad.c \ + ft_putnbrpad.c \ + ft_putstrn.c \ + ft_print_words_tables.c \ + ft_putendl2.c \ + ft_putendl_fd2.c \ + \ + ft_lstnew.c \ + ft_lstdelone.c \ + ft_lstdel.c \ + ft_lstadd.c \ + ft_lstiter.c \ + ft_lstmap.c \ + ft_lstaddend.c \ + ft_lstsize.c \ + ft_lstgetat.c \ + ft_lstgetlast.c \ + ft_lstsort.c \ + ft_lstaddsort.c \ + \ + ft_abs.c \ + ft_atoi.c \ + ft_atois.c \ + ft_itoa.c \ + ft_nbrlen.c \ + ft_round.c \ + \ + ft_printf.c \ + ft_printf_check_fields.c \ + ft_printf_check_type.c \ + ft_printf_tools.c \ + ft_printf_tools_char.c \ + ft_printf_tools_float.c \ + ft_printf_tools_hexa.c \ + ft_printf_tools_int.c \ + ft_printf_tools_lenght.c \ + ft_printf_tools_malloc_size.c \ + ft_printf_tools_oct.c \ + ft_printf_tools_prec_size.c \ + ft_printf_tools_str.c \ + ft_printf_tools_unsigned_int.c \ + ft_printf_tools_wchar_t.c \ + ft_printf_tools_wstr.c \ + \ + get_next_line.c \ + ft_getline.c \ + \ + ft_sort_params.c \ + ft_realpath.c \ + \ + ft_envaddend.c \ + ft_envclean.c \ + ft_envdel.c \ + ft_envdelelem.c \ + ft_envgetelem.c \ + ft_envnew.c \ + ft_envtochar.c \ + ft_envtotab.c \ + ft_envupdate.c + +SRCDIR = srcs +OBJDIR = objs +OBJS = $(SRCS:.c=.o) +OBJP = $(addprefix $(OBJDIR)/, $(OBJS)) +INCLS = -Iincludes + +.PHONY = all clean fclean re + +all: $(NAME) + +$(NAME): $(OBJP) + ar rcs $(NAME) $(OBJP) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c includes/libft.h + @mkdir -p $(OBJDIR) + $(CC) $(CCFLAGS) $(CCSTD) $(INCLS) -c $< -o $@ + +clean: + rm -f $(OBJP) + +fclean: clean + rm -f $(NAME) + rm -rf $(OBJDIR) + +re: fclean all diff --git a/libft/auteur b/libft/auteur new file mode 100644 index 0000000..3d3f281 --- /dev/null +++ b/libft/auteur @@ -0,0 +1 @@ +tmaze diff --git a/libft/includes/libft.h b/libft/includes/libft.h new file mode 100644 index 0000000..120dc20 --- /dev/null +++ b/libft/includes/libft.h @@ -0,0 +1,384 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */ +/* Updated: 2019/12/06 09:57:17 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include +# include +# include +# include + +# define FT_RESET "\x1b[0m" +# define FT_BOLD "\x1b[1m" +# define FT_CLEAR "\x1b[2J" + +# define FT_COLOR_BLACK "\x1b[30m" +# define FT_COLOR_RED "\x1b[31m" +# define FT_COLOR_GREEN "\x1b[32m" +# define FT_COLOR_YELLOW "\x1b[33m" +# define FT_COLOR_BLUE "\x1b[34m" +# define FT_COLOR_MAGENTA "\x1b[35m" +# define FT_COLOR_CYAN "\x1b[36m" +# define FT_COLOR_WHITE "\x1b[37m" + +# define FT_COLOR_BG_BLACK "\x1b[40m" +# define FT_COLOR_BG_RED "\x1b[41m" +# define FT_COLOR_BG_GREEN "\x1b[42m" +# define FT_COLOR_BG_YELLOW "\x1b[43m" +# define FT_COLOR_BG_BLUE "\x1b[44m" +# define FT_COLOR_BG_MAGENTA "\x1b[45m" +# define FT_COLOR_BG_CYAN "\x1b[46m" +# define FT_COLOR_BG_WHITE "\x1b[47m" + +# define FT_INT_MAX 2147483647 +# define FT_INT_MIN -2147483648 + +/* +** définition type t_list +*/ + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +/* +** definition macro BUFF_SIZE +*/ + +# define BUFF_SIZE 30 + +/* +** definition type t_file +*/ + +typedef struct s_file +{ + int fd; + char buff[BUFF_SIZE + 1]; +} t_file; + +enum e_size +{ + h, + hh, + l, + ll, + j, + z, + L +}; + +enum e_type +{ + p, + d, + i, + D, + o, + O, + u, + x, + X, + U, + s, + S, + c, + C, + percent, + f +}; + +typedef struct s_conv +{ + int fl_minus; + int fl_plus; + int fl_zero; + int fl_hashtag; + int fl_space; + int fl_witdth; + int fl_prec; + enum e_size fl_size; + enum e_type fl_type; + char *str; + int str_size; +} t_conv; + +typedef struct s_env +{ + char *key; + char *val; + struct s_env *next; +} t_env; + +/* +** Print functions +*/ + +int ft_printf(const char *format, ...); +void ft_putchar(char c); +void ft_putnbr(int n); +void ft_putstr(char const *s); + +/* +** ft_printf tools +*/ + +void init_struct_conv(t_conv *field); +void ft_align_str_zero(char *str, t_conv *field); +void ft_align_str(char *str, t_conv *field); +void set_malloc_sizes(uintmax_t nb, t_conv *field); +void set_malloc_sizeu(uintmax_t nb, t_conv *field); +void set_malloc_sizeo(uintmax_t nb, t_conv *field); +void set_malloc_sizeu(uintmax_t nb, t_conv *field); +int set_malloc_sizef(long double nb, char *nbrstr + , t_conv *field); +void init_handler_tab(char *(*f[14])(t_conv *, va_list)); +int ft_wstrlen(const wint_t *str); +uint64_t dmod(long double nb, long double mod); + +/* +** Check Flags +*/ + +void check_flags(const char **str, t_conv *field); +void check_width(const char **str, t_conv *field, va_list ap); +ssize_t check_precision(const char **str, t_conv *field + , va_list ap); +void check_size(const char **str, t_conv *field); +ssize_t check_type_one(const char **str, t_conv *field); +ssize_t check_type_two(const char **str, t_conv *field); +ssize_t check_fields(const char **str, va_list ap, t_conv *field); + +/* +** Handle type i and d +*/ + +char *handler_int(uintmax_t nb, t_conv *field); +char *select_int_handler(t_conv *field, va_list ap); +char *handle_output_i_d(t_conv *field, va_list ap); + +/* +** Handle type u +*/ + +char *select_uns_int_handler(t_conv *field, va_list ap); +char *handle_output_u(t_conv *field, va_list ap); +char *handler_uns(uintmax_t nb, t_conv *field); + +/* +** Handle type x and X +*/ + +char *handler_hexa(uintmax_t nb, t_conv *field); +char *select_hexa_handler(t_conv *field, va_list ap); +char *handle_output_hexa(t_conv *field, va_list ap); +void ft_align_hex_zero(char *str, t_conv *field); +void ft_align_hex(char *str, t_conv *field); +int set_precision_sizex(uintmax_t nb, t_conv *field); +void set_malloc_sizeh(uintmax_t nb, t_conv *field); + +/* +** Handle type o +*/ + +char *handler_oct(uintmax_t nb, t_conv *field); +char *select_oct_handler(t_conv *field, va_list ap); +char *handle_output_oct(t_conv *field, va_list ap); +void ft_align_oct_zero(char *str, t_conv *field); +void ft_align_oct(char *str, t_conv *field); +int set_precision_sizeo(uintmax_t nb, t_conv *field); + +/* +** Handle type c +*/ + +char *handle_output_char(t_conv *field, va_list ap); + +/* +** Handle type C +*/ + +unsigned int ft_bin_size(unsigned int nb); +void ft_align_wchar(char *str, t_conv *field); +char *handler_2oct_char(wint_t c, t_conv *field); +char *handler_3oct_char(wint_t c, t_conv *field); +char *handler_4oct_char(wint_t c, t_conv *field); +char *handle_output_wchar(t_conv *field, va_list ap); + +/* +** Handle type s +*/ + +char *handle_output_str(t_conv *field, va_list ap); +void ft_align_wstr(char *str, t_conv *field); +int set_precision_sizes(uintmax_t nb, t_conv *field); + +/* +** Handle type S +*/ + +char *handle_output_wstr(t_conv *field, va_list ap); +int set_prec_size_wstr(const wint_t *str, t_conv *field); + +/* +** Handle type f +*/ + +char *handle_output_float(t_conv *field, va_list ap); + +/* +** fonctions obligatoires +*/ + +void *ft_memset(void *b, int c, size_t len); +void ft_bzero(void *s, size_t n); +void *ft_memcpy(void *dest, const void *src,\ + size_t n); +void *ft_memccpy(void *dest, const void *src,\ + int c, size_t n); +void *ft_memmove(void *dest, const void *src, size_t len); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +size_t ft_strlen(const char *s); +char *ft_strdup(const char *s); +char *ft_strcpy(char *dest, const char *src); +char *ft_strncpy(char *dest, const char *src, size_t n); +char *ft_strcat(char *dest, const char *src); +char *ft_strncat(char *dest, const char *src,\ + size_t n); +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); +char *ft_strstr(const char *haystack, const char *needle); +char *ft_strnstr(const char *haystack, const char *needle,\ + size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_atoi(const char *str); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +int ft_toupper(int c); +int ft_tolower(int c); + +/* +** fonctions supplémentaires +*/ + +void *ft_memalloc(size_t size); +void ft_memdel(void **ap); +char *ft_strnew(size_t size); +void ft_strdel(char **as); +void ft_strclr(char *s); +void ft_striter(char *s, void (*f)(char *)); +void ft_striteri(char *s, void (*f)(unsigned int, char *)); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strequ(char const *s1, char const *s2); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s); +char **ft_strsplit(char const *s, char c); +char *ft_itoa(int n); +void ft_putchar(char c); +void ft_putstr(char const *s); +void ft_putendl(char const *s); +void ft_putnbr(int n); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char const *s, int fd); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr_fd(int n, int fd); + +/* +** fonctions bonus +*/ + +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); + +/* +** fonctions additionneles +*/ + +int ft_isupper(int c); +int ft_islower(int c); +int ft_str_is_alpha(char *str); +int ft_str_is_lowercase(char *str); +int ft_str_is_numeric(char *str); +int ft_str_is_printable(char *str); +int ft_str_is_uppercase(char *str); +char *ft_strlowcase(char *s); +char *ft_strupcase(char *s); +char *ft_strcapitalize(char *str); +size_t ft_strlcpy(char *dst, const char *src,\ + size_t size); +size_t ft_lstsize(t_list *lst); +t_list *ft_lstgetat(t_list *lst, size_t ind); +t_list *ft_lstgetlast(t_list *lst); +void ft_sort_params(int ac, char **av); +void ft_print_words_tables(char **tab); +t_list *ft_lstaddend(t_list **alst, t_list *new); +char *ft_strndup(const char *s1, size_t n); +unsigned int ft_abs(int nb); +char *ft_strnchr(const char *s, int c, int n); +char *ft_strrnchr(const char *s, int c, int n); + +int get_next_line(const int fd, char **line); + +void ft_del_words_tables(char ***word_table); +void ft_putstrn(char const *s, size_t nb); +size_t ft_nbrlen(int nb); +void ft_putstrpad(char *str, size_t size, char pad, char align); +void ft_putnbrpad(int nbr, size_t size, char pad, char align); + +void ft_lstsort(t_list **lst, int (*f)(void *c1, void *c2), + int rev); +void ft_lstaddsort(t_list **lst, t_list *new, + int (*f)(void *c1, void *c2), int rev); + +int ft_round(float x, float ind); + +char *ft_realpath(char *r_path, char **a_path); +int ft_issign(char c); +int ft_iswhitespace(char c); +int ft_hasdigit(char *s); +char **ft_strsplitwhitespace(char *s); +int ft_atois(const char *str, int *nb); +int ft_getline(char **line); +int ft_putendl2(char const *s); +int ft_putendl_fd2(char const *s, int fd); + +void ft_envdelelem(t_env **elem); +void ft_envdel(t_env **env); +t_env *ft_envnew(char *env); +t_env *ft_envaddend(t_env **alst, t_env *new); +t_env *ft_envgetelem(char *key, t_env *env); +char *ft_envtochar(t_env *env); +char **ft_envtotab(t_env *env); +void ft_envclean(t_env **env); +char *ft_envupdate(char *key, t_env *env, char *val);; + +#endif diff --git a/libft/srcs/ft_abs.c b/libft/srcs/ft_abs.c new file mode 100644 index 0000000..891e8bb --- /dev/null +++ b/libft/srcs/ft_abs.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/05 16:44:52 by tmaze #+# #+# */ +/* Updated: 2018/05/05 16:49:04 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +unsigned int ft_abs(int nb) +{ + if (nb < 0) + return (nb * -1); + return (nb); +} diff --git a/libft/srcs/ft_atoi.c b/libft/srcs/ft_atoi.c new file mode 100644 index 0000000..7614ed7 --- /dev/null +++ b/libft/srcs/ft_atoi.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 13:50:22 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:37:42 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *str) +{ + int nbnum; + int mult; + int ret; + + nbnum = 0; + while (str[nbnum] == ' ' || str[nbnum] == '\t' || str[nbnum] == '\n' + || str[nbnum] == '\v' || str[nbnum] == '\f' || str[nbnum] == '\r') + nbnum++; + if (str[nbnum] == '+' || str[nbnum] == '-') + nbnum++; + while (str[nbnum] != '\0' && ft_isdigit(str[nbnum])) + nbnum++; + nbnum--; + ret = 0; + mult = 1; + while (nbnum >= 0 && ft_isdigit(str[nbnum])) + { + ret += (str[nbnum--] - '0') * mult; + mult *= 10; + } + if (nbnum >= 0 && str[nbnum] == '-') + ret *= -1; + return (ret); +} diff --git a/libft/srcs/ft_atois.c b/libft/srcs/ft_atois.c new file mode 100644 index 0000000..f0c20a0 --- /dev/null +++ b/libft/srcs/ft_atois.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atois.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/27 11:58:44 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:47:50 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atois(const char *str, int *nb) +{ + int num; + int mult; + int max; + + num = 0; + while (ft_iswhitespace(str[num])) + num++; + max = (str[num] == '-') ? FT_INT_MIN : FT_INT_MAX; + if (str[num] == '+' || str[num] == '-') + num++; + while (str[num] != '\0' && ft_isdigit(str[num])) + num++; + num--; + *nb = 0; + mult = 1; + while (num >= 0 && ft_isdigit(str[num]) + && (ft_abs(max - *nb) >= 2000000000)) + { + *nb += (str[num--] - '0') * mult; + mult *= 10; + } + if (num >= 0 && str[num] == '-') + *nb *= -1; + return (!ft_isdigit(str[num]) || ft_abs(max - *nb) >= 2000000000); +} diff --git a/libft/srcs/ft_bzero.c b/libft/srcs/ft_bzero.c new file mode 100644 index 0000000..1cc6791 --- /dev/null +++ b/libft/srcs/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 16:43:09 by tmaze #+# #+# */ +/* Updated: 2018/04/06 10:49:12 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/srcs/ft_close.c b/libft/srcs/ft_close.c new file mode 100644 index 0000000..e6a9c7d --- /dev/null +++ b/libft/srcs/ft_close.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_close.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:39:05 by tmaze #+# #+# */ +/* Updated: 2019/03/08 13:44:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_close(t_file *file) +{ + int filedes; + + filedes = file->fd; + ft_memdel(file); + return (close(filedes) == 0); +} diff --git a/libft/srcs/ft_del_words_tables.c b/libft/srcs/ft_del_words_tables.c new file mode 100644 index 0000000..86cee0f --- /dev/null +++ b/libft/srcs/ft_del_words_tables.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_del_words_tables.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/07/25 15:13:09 by tmaze #+# #+# */ +/* Updated: 2018/07/25 16:03:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_del_words_tables(char ***word_table) +{ + int i; + + i = -1; + while ((*word_table)[++i]) + ft_strdel(&(*word_table)[i]); + ft_memdel((void**)word_table); + *word_table = NULL; +} diff --git a/libft/srcs/ft_envaddend.c b/libft/srcs/ft_envaddend.c new file mode 100644 index 0000000..bb91f12 --- /dev/null +++ b/libft/srcs/ft_envaddend.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envaddend.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 15:02:33 by tmaze #+# #+# */ +/* Updated: 2019/12/09 13:00:42 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_env *ft_envaddend(t_env **alst, t_env *new) +{ + t_env *tmp; + + if (new == NULL) + return (NULL); + if (*alst == NULL) + *alst = new; + else + { + tmp = *alst; + while (tmp->next) + tmp = tmp->next; + tmp->next = new; + } + return (*alst); +} diff --git a/libft/srcs/ft_envclean.c b/libft/srcs/ft_envclean.c new file mode 100644 index 0000000..008094e --- /dev/null +++ b/libft/srcs/ft_envclean.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envclean.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/06 09:53:25 by tmaze #+# #+# */ +/* Updated: 2019/12/06 09:53:47 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_envclean(t_env **env) +{ + t_env **it; + t_env *tmp; + + it = env; + while (*it) + { + if (ft_strequ((*it)->val, "")) + { + tmp = *it; + *it = tmp->next; + ft_envdelelem(&tmp); + } + else + it = &((*it)->next); + } +} diff --git a/libft/srcs/ft_envdel.c b/libft/srcs/ft_envdel.c new file mode 100644 index 0000000..47677c0 --- /dev/null +++ b/libft/srcs/ft_envdel.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 14:51:20 by tmaze #+# #+# */ +/* Updated: 2019/12/09 13:00:46 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_envdel(t_env **env) +{ + t_env *tmp; + + while (*env) + { + tmp = *env; + *env = (*env)->next; + ft_envdelelem(&tmp); + } +} diff --git a/libft/srcs/ft_envdelelem.c b/libft/srcs/ft_envdelelem.c new file mode 100644 index 0000000..059032f --- /dev/null +++ b/libft/srcs/ft_envdelelem.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envdelelem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 14:43:45 by tmaze #+# #+# */ +/* Updated: 2019/12/09 13:00:45 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_envdelelem(t_env **elem) +{ + if (elem) + { + ft_strdel(&((*elem)->key)); + ft_strdel(&((*elem)->val)); + ft_memdel((void**)elem); + } +} diff --git a/libft/srcs/ft_envgetelem.c b/libft/srcs/ft_envgetelem.c new file mode 100644 index 0000000..ebe461c --- /dev/null +++ b/libft/srcs/ft_envgetelem.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envgetelem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 16:46:17 by tmaze #+# #+# */ +/* Updated: 2019/12/05 16:47:00 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_env *ft_envgetelem(char *key, t_env *env) +{ + t_env *it; + + it = env; + while (it && !ft_strequ(it->key, key)) + it = it->next; + return (it); +} diff --git a/libft/srcs/ft_envnew.c b/libft/srcs/ft_envnew.c new file mode 100644 index 0000000..9994459 --- /dev/null +++ b/libft/srcs/ft_envnew.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 14:54:55 by tmaze #+# #+# */ +/* Updated: 2019/12/05 17:12:59 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_env *ft_envnew(char *env) +{ + t_env *ret; + int i; + + i = 0; + ret = NULL; + while (env[i] && env[i] != '=') + i++; + if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL) + if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL + || (ret->key = ft_strsub(env, 0, i)) == NULL) + ft_envdelelem(&ret); + return (ret); +} diff --git a/libft/srcs/ft_envtochar.c b/libft/srcs/ft_envtochar.c new file mode 100644 index 0000000..1aa3cef --- /dev/null +++ b/libft/srcs/ft_envtochar.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envtochar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 17:28:25 by tmaze #+# #+# */ +/* Updated: 2019/12/09 13:00:44 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_envtochar(t_env *env) +{ + int size; + char *ret; + + size = ft_strlen(env->key) + ft_strlen(env->val) + 2; + if ((ret = ft_strnew(size)) != NULL) + { + ft_strlcpy(ret, env->key, size); + ft_strlcat(ret, "=", size); + ft_strlcat(ret, env->val, size); + } + return (ret); +} diff --git a/libft/srcs/ft_envtotab.c b/libft/srcs/ft_envtotab.c new file mode 100644 index 0000000..f8966bb --- /dev/null +++ b/libft/srcs/ft_envtotab.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envtotab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 17:34:22 by tmaze #+# #+# */ +/* Updated: 2019/12/05 17:34:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_envtotab(t_env *env) +{ + t_env *it; + char *new; + char **ret; + int i; + + i = 0; + ret = NULL; + it = env; + while (it && (++i)) + it = it->next; + if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL) + return (NULL); + it = env; + i = 0; + while (it) + { + if ((new = ft_envtochar(it)) == NULL) + ft_del_words_tables(&ret); + if (new == NULL) + break ; + ret[i++] = new; + it = it->next; + } + return (ret); +} diff --git a/libft/srcs/ft_envupdate.c b/libft/srcs/ft_envupdate.c new file mode 100644 index 0000000..54d6871 --- /dev/null +++ b/libft/srcs/ft_envupdate.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_envupdate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/12/05 19:10:38 by tmaze #+# #+# */ +/* Updated: 2019/12/05 19:10:52 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_envupdate(char *key, t_env *env, char *val) +{ + t_env *elem; + char *new; + + new = NULL; + if ((elem = ft_envgetelem(key, env)) != NULL) + if ((new = ft_strdup(val)) != NULL) + { + ft_strdel(&elem->val); + elem->val = new; + } + return (new); +} diff --git a/libft/srcs/ft_getline.c b/libft/srcs/ft_getline.c new file mode 100644 index 0000000..4801d93 --- /dev/null +++ b/libft/srcs/ft_getline.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */ +/* Updated: 2019/03/19 17:30:24 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int get_last_ind(char *buff) +{ + int i; + + i = 0; + while (buff[i] && buff[i] != '\n') + i++; + return (i); +} + +static int flush_buff(char **line, char *buff) +{ + char *tmp; + int i; + + i = get_last_ind(buff); + if (*line == NULL) + { + if ((*line = ft_strndup(buff, i)) == NULL) + return (-1); + } + else if (*line != NULL) + { + if ((tmp = ft_strnew(ft_strlen(*line) + i)) != NULL) + { + ft_strcpy(tmp, *line); + ft_strncat(tmp, buff, i); + ft_strdel(line); + *line = tmp; + } + else + { + ft_strdel(line); + return (-1); + } + } + return (i); +} + +static void init_getline(char **line, int *check, int *ret) +{ + *line = NULL; + *check = BUFF_SIZE; + *ret = -2; +} + +int ft_getline(char **line) +{ + static char buff[BUFF_SIZE] = "\0"; + int check; + int ret; + int i; + + if (line == NULL || BUFF_SIZE < 1) + return (-1); + init_getline(line, &check, &ret); + while (check == BUFF_SIZE) + { + if (buff[0] == '\0') + { + if ((ret = read(0, buff, BUFF_SIZE)) == -1) + return (-1); + if (ret == 0 && *line == NULL) + return (0); + } + if ((i = flush_buff(line, buff)) == -1) + return (-1); + check = (buff[i] != '\n' && ret != 0) ? BUFF_SIZE : i; + ft_memmove(buff, buff + i + 1, BUFF_SIZE - i); + ft_memset(buff + BUFF_SIZE - i, '\0', i); + } + return (1); +} diff --git a/libft/srcs/ft_gets.c b/libft/srcs/ft_gets.c new file mode 100644 index 0000000..cbc28b9 --- /dev/null +++ b/libft/srcs/ft_gets.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_gets.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:44:31 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:33:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" diff --git a/libft/srcs/ft_hasdigit.c b/libft/srcs/ft_hasdigit.c new file mode 100644 index 0000000..9c57d0d --- /dev/null +++ b/libft/srcs/ft_hasdigit.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hasdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/25 16:16:40 by tmaze #+# #+# */ +/* Updated: 2019/02/28 16:18:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_hasdigit(char *s) +{ + int i; + + i = 0; + while (s[i] && !ft_isdigit(s[i])) + i++; + return (ft_isdigit(s[i])); +} diff --git a/libft/srcs/ft_isalnum.c b/libft/srcs/ft_isalnum.c new file mode 100644 index 0000000..2d1d9c5 --- /dev/null +++ b/libft/srcs/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 11:51:53 by tmaze #+# #+# */ +/* Updated: 2018/04/04 12:00:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft/srcs/ft_isalpha.c b/libft/srcs/ft_isalpha.c new file mode 100644 index 0000000..2e24e50 --- /dev/null +++ b/libft/srcs/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 10:56:47 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:21:34 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (ft_islower(c) || ft_isupper(c)); +} diff --git a/libft/srcs/ft_isascii.c b/libft/srcs/ft_isascii.c new file mode 100644 index 0000000..9e83a3b --- /dev/null +++ b/libft/srcs/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 12:23:59 by tmaze #+# #+# */ +/* Updated: 2018/04/04 12:25:37 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/libft/srcs/ft_isdigit.c b/libft/srcs/ft_isdigit.c new file mode 100644 index 0000000..aa76237 --- /dev/null +++ b/libft/srcs/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 11:33:35 by tmaze #+# #+# */ +/* Updated: 2018/04/04 11:34:10 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft/srcs/ft_islower.c b/libft/srcs/ft_islower.c new file mode 100644 index 0000000..a6a6147 --- /dev/null +++ b/libft/srcs/ft_islower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:20:43 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:21:23 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_islower(int c) +{ + return (c >= 'a' && c <= 'z'); +} diff --git a/libft/srcs/ft_isprint.c b/libft/srcs/ft_isprint.c new file mode 100644 index 0000000..d57ba58 --- /dev/null +++ b/libft/srcs/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 12:29:36 by tmaze #+# #+# */ +/* Updated: 2018/04/04 12:30:28 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= 32 && c <= 126); +} diff --git a/libft/srcs/ft_issign.c b/libft/srcs/ft_issign.c new file mode 100644 index 0000000..5b18417 --- /dev/null +++ b/libft/srcs/ft_issign.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_issign.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/25 16:18:54 by tmaze #+# #+# */ +/* Updated: 2019/02/25 16:19:13 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_issign(char c) +{ + return (c == '+' || c == '-'); +} diff --git a/libft/srcs/ft_isupper.c b/libft/srcs/ft_isupper.c new file mode 100644 index 0000000..b86d1e4 --- /dev/null +++ b/libft/srcs/ft_isupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:10:12 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:10:31 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isupper(int c) +{ + return (c >= 'A' && c <= 'Z'); +} diff --git a/libft/srcs/ft_iswhitespace.c b/libft/srcs/ft_iswhitespace.c new file mode 100644 index 0000000..48c7fc1 --- /dev/null +++ b/libft/srcs/ft_iswhitespace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_iswhitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/25 16:11:19 by tmaze #+# #+# */ +/* Updated: 2019/02/25 16:13:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_iswhitespace(char c) +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' + || c == '\r'); +} diff --git a/libft/srcs/ft_itoa.c b/libft/srcs/ft_itoa.c new file mode 100644 index 0000000..5b2f97b --- /dev/null +++ b/libft/srcs/ft_itoa.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 11:20:12 by tmaze #+# #+# */ +/* Updated: 2018/05/05 16:48:49 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + unsigned long mult; + int nb_nbr; + int sign; + unsigned long nbr; + char *ret; + + mult = 10; + nb_nbr = 1; + nbr = ft_abs(n); + sign = (n < 0); + while (mult <= nbr && (nb_nbr++)) + mult *= 10; + nb_nbr += sign; + if ((ret = ft_strnew(nb_nbr)) == NULL) + return (NULL); + nb_nbr = 0; + if (sign && (++nb_nbr)) + ret[0] = '-'; + while (mult > 1) + { + ret[nb_nbr++] = '0' + ((nbr % mult) / (mult / 10)); + mult /= 10; + } + return (ret); +} diff --git a/libft/srcs/ft_lstadd.c b/libft/srcs/ft_lstadd.c new file mode 100644 index 0000000..1cb3221 --- /dev/null +++ b/libft/srcs/ft_lstadd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 19:26:05 by tmaze #+# #+# */ +/* Updated: 2018/04/08 19:27:35 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + new->next = *alst; + *alst = new; +} diff --git a/libft/srcs/ft_lstaddend.c b/libft/srcs/ft_lstaddend.c new file mode 100644 index 0000000..abb02e5 --- /dev/null +++ b/libft/srcs/ft_lstaddend.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstaddend.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/17 20:55:26 by tmaze #+# #+# */ +/* Updated: 2018/11/26 12:30:30 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstaddend(t_list **alst, t_list *new) +{ + t_list *tmp; + + if (new == NULL) + return (NULL); + if (ft_lstsize(*alst) == 0) + ft_lstadd(alst, new); + else if (ft_lstsize(*alst) > 0) + { + tmp = ft_lstgetlast(*alst); + ft_lstadd(&(tmp->next), new); + } + return (*alst); +} diff --git a/libft/srcs/ft_lstaddsort.c b/libft/srcs/ft_lstaddsort.c new file mode 100644 index 0000000..c8aef4a --- /dev/null +++ b/libft/srcs/ft_lstaddsort.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstaddsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/01 11:57:25 by tmaze #+# #+# */ +/* Updated: 2018/10/02 16:43:09 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstaddsort(t_list **lst, t_list *new, int (*f)(void *c1, void *c2), + int rev) +{ + t_list **sort; + + sort = lst; + if (*lst == NULL || (!rev && (*f)((*lst)->content, new->content) > 0) + || (rev && (*f)((*lst)->content, new->content) < 0)) + ft_lstadd(lst, new); + else + { + while ((*sort) && ((rev && (*f)((*sort)->content, new->content) >= 0) + || (!rev && (*f)((*sort)->content, new->content) <= 0))) + sort = &(*sort)->next; + ft_lstadd(&(*sort), new); + } +} diff --git a/libft/srcs/ft_lstdel.c b/libft/srcs/ft_lstdel.c new file mode 100644 index 0000000..c5e6cc9 --- /dev/null +++ b/libft/srcs/ft_lstdel.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 17:19:32 by tmaze #+# #+# */ +/* Updated: 2018/06/12 00:33:45 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void*, size_t)) +{ + if (*alst) + { + if ((*alst)->next != NULL) + ft_lstdel(&((*alst)->next), del); + ft_lstdelone(alst, del); + } +} diff --git a/libft/srcs/ft_lstdelone.c b/libft/srcs/ft_lstdelone.c new file mode 100644 index 0000000..94c5e15 --- /dev/null +++ b/libft/srcs/ft_lstdelone.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 17:00:17 by tmaze #+# #+# */ +/* Updated: 2018/04/08 17:22:15 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)) +{ + (*del)((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; +} diff --git a/libft/srcs/ft_lstgetat.c b/libft/srcs/ft_lstgetat.c new file mode 100644 index 0000000..9092b65 --- /dev/null +++ b/libft/srcs/ft_lstgetat.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstgetat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 16:35:51 by tmaze #+# #+# */ +/* Updated: 2018/04/09 16:45:36 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstgetat(t_list *lst, size_t ind) +{ + size_t i; + t_list *tmp; + + i = 0; + tmp = lst; + while (i != ind && tmp) + { + i++; + tmp = tmp->next; + } + return (tmp); +} diff --git a/libft/srcs/ft_lstgetlast.c b/libft/srcs/ft_lstgetlast.c new file mode 100644 index 0000000..ebd2bde --- /dev/null +++ b/libft/srcs/ft_lstgetlast.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstgetlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 17:34:10 by tmaze #+# #+# */ +/* Updated: 2018/04/12 12:23:02 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstgetlast(t_list *lst) +{ + return (ft_lstgetat(lst, ft_lstsize(lst) - 1)); +} diff --git a/libft/srcs/ft_lstiter.c b/libft/srcs/ft_lstiter.c new file mode 100644 index 0000000..bb6882e --- /dev/null +++ b/libft/srcs/ft_lstiter.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 19:48:44 by tmaze #+# #+# */ +/* Updated: 2018/04/08 19:51:33 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + t_list *tmp; + + tmp = lst; + while (tmp) + { + (*f)(tmp); + tmp = tmp->next; + } +} diff --git a/libft/srcs/ft_lstmap.c b/libft/srcs/ft_lstmap.c new file mode 100644 index 0000000..86be818 --- /dev/null +++ b/libft/srcs/ft_lstmap.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 19:54:31 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:31:03 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_lstdelmem(void *content, size_t content_size) +{ + (void)content_size; + free(content); +} + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *ret; + t_list *new; + + ret = NULL; + if (lst != NULL && f != NULL) + { + if (lst->next != NULL) + ret = ft_lstmap(lst->next, f); + if ((new = (*f)(lst)) == NULL) + ft_lstdel(&ret, &ft_lstdelmem); + else + ft_lstadd(&ret, new); + } + return (ret); +} diff --git a/libft/srcs/ft_lstnew.c b/libft/srcs/ft_lstnew.c new file mode 100644 index 0000000..d6bbaeb --- /dev/null +++ b/libft/srcs/ft_lstnew.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 00:35:42 by tmaze #+# #+# */ +/* Updated: 2018/04/08 17:15:34 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *ret; + + if ((ret = (t_list*)malloc(sizeof(t_list))) != NULL) + { + if (!content) + { + ret->content = NULL; + ret->content_size = 0; + } + else + { + if ((ret->content = malloc(content_size)) == NULL) + { + free(ret); + return (0); + } + ft_memmove(ret->content, content, content_size); + ret->content_size = content_size; + } + ret->next = NULL; + } + return (ret); +} diff --git a/libft/srcs/ft_lstsize.c b/libft/srcs/ft_lstsize.c new file mode 100644 index 0000000..937736b --- /dev/null +++ b/libft/srcs/ft_lstsize.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 13:55:29 by tmaze #+# #+# */ +/* Updated: 2018/11/28 14:46:12 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_lstsize(t_list *lst) +{ + if (lst == NULL) + return (0); + else + return (ft_lstsize(lst->next) + 1); +} diff --git a/libft/srcs/ft_lstsort.c b/libft/srcs/ft_lstsort.c new file mode 100644 index 0000000..08233f7 --- /dev/null +++ b/libft/srcs/ft_lstsort.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/01 11:54:22 by tmaze #+# #+# */ +/* Updated: 2018/10/01 12:42:27 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstsort(t_list **lst, int (*f)(void *c1, void *c2), int rev) +{ + t_list *sorted; + t_list *ind; + t_list *next; + + ind = *lst; + sorted = NULL; + while (ind) + { + next = ind->next; + ft_lstaddsort(&sorted, ind, (*f), rev); + ind = next; + } + *lst = sorted; +} diff --git a/libft/srcs/ft_memalloc.c b/libft/srcs/ft_memalloc.c new file mode 100644 index 0000000..d3dd14e --- /dev/null +++ b/libft/srcs/ft_memalloc.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 17:38:56 by tmaze #+# #+# */ +/* Updated: 2018/04/06 18:07:20 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + void *ret; + + if ((ret = malloc(size)) != NULL) + ft_bzero(ret, size); + return (ret); +} diff --git a/libft/srcs/ft_memccpy.c b/libft/srcs/ft_memccpy.c new file mode 100644 index 0000000..fe6cafc --- /dev/null +++ b/libft/srcs/ft_memccpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 11:29:28 by tmaze #+# #+# */ +/* Updated: 2018/04/06 11:30:21 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + size_t i; + + i = 0; + while (++i <= n) + { + ((char*)dest)[i - 1] = ((char*)src)[i - 1]; + if (((unsigned char*)src)[i - 1] == (unsigned char)c) + return (dest + i); + } + return (NULL); +} diff --git a/libft/srcs/ft_memchr.c b/libft/srcs/ft_memchr.c new file mode 100644 index 0000000..17937b1 --- /dev/null +++ b/libft/srcs/ft_memchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 16:07:52 by tmaze #+# #+# */ +/* Updated: 2018/04/06 16:27:54 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + int i; + char *tmp; + + i = -1; + tmp = (char*)s; + while ((unsigned int)++i < n) + if ((char)tmp[i] == (char)c) + return ((void*)&tmp[i]); + return (NULL); +} diff --git a/libft/srcs/ft_memcmp.c b/libft/srcs/ft_memcmp.c new file mode 100644 index 0000000..8e8b9d6 --- /dev/null +++ b/libft/srcs/ft_memcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 16:55:25 by tmaze #+# #+# */ +/* Updated: 2018/04/08 13:41:54 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + int diff; + void *str1; + void *str2; + + i = 0; + diff = 0; + str1 = (void*)s1; + str2 = (void*)s2; + if (n > 0) + diff = ((unsigned char*)str1)[0] - ((unsigned char*)str2)[0]; + while (diff == 0 && ++i < n) + diff = ((unsigned char*)str1)[i] - ((unsigned char*)str2)[i]; + return (diff); +} diff --git a/libft/srcs/ft_memcpy.c b/libft/srcs/ft_memcpy.c new file mode 100644 index 0000000..97d9faa --- /dev/null +++ b/libft/srcs/ft_memcpy.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 11:05:36 by tmaze #+# #+# */ +/* Updated: 2018/04/06 11:09:57 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + i = 0; + while (++i <= n) + ((char*)dest)[i - 1] = ((char*)src)[i - 1]; + return (dest); +} diff --git a/libft/srcs/ft_memdel.c b/libft/srcs/ft_memdel.c new file mode 100644 index 0000000..1e80242 --- /dev/null +++ b/libft/srcs/ft_memdel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 18:21:08 by tmaze #+# #+# */ +/* Updated: 2018/04/07 15:27:28 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_memdel(void **ap) +{ + if (ap && *ap) + { + free(*ap); + *ap = NULL; + } +} diff --git a/libft/srcs/ft_memmove.c b/libft/srcs/ft_memmove.c new file mode 100644 index 0000000..c5883c4 --- /dev/null +++ b/libft/srcs/ft_memmove.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 15:06:49 by tmaze #+# #+# */ +/* Updated: 2018/04/08 13:28:55 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + size_t i; + + i = 0; + if (src < dst) + { + i = len + 1; + while (--i > 0) + ((char*)dst)[i - 1] = ((char*)src)[i - 1]; + } + else if (src > dst) + while (++i <= len) + ((char*)dst)[i - 1] = ((char*)src)[i - 1]; + return (dst); +} diff --git a/libft/srcs/ft_memset.c b/libft/srcs/ft_memset.c new file mode 100644 index 0000000..4fd4fa1 --- /dev/null +++ b/libft/srcs/ft_memset.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 15:22:55 by tmaze #+# #+# */ +/* Updated: 2018/04/06 18:20:06 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *b, int c, size_t len) +{ + size_t i; + + i = 0; + while (i < len) + ((char*)b)[i++] = c; + return (b); +} diff --git a/libft/srcs/ft_nbrlen.c b/libft/srcs/ft_nbrlen.c new file mode 100644 index 0000000..9351379 --- /dev/null +++ b/libft/srcs/ft_nbrlen.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nbrlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/07/25 17:45:29 by tmaze #+# #+# */ +/* Updated: 2018/07/25 18:20:22 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_nbrlen(int nb) +{ + size_t nb_len; + + nb_len = 0; + if (nb <= 0) + nb_len++; + if (nb < 0) + nb *= -1; + while (nb > 0) + { + nb /= 10; + nb_len++; + } + return (nb_len); +} diff --git a/libft/srcs/ft_open.c b/libft/srcs/ft_open.c new file mode 100644 index 0000000..2706216 --- /dev/null +++ b/libft/srcs/ft_open.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_open.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/08 13:27:26 by tmaze #+# #+# */ +/* Updated: 2019/03/08 13:38:29 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_file *ft_open(char *filename, oint oflag) +{ + t_file *ret; + + if ((ret = ft_memalloc(sizeof(t_file))) == NULL) + return (NULL); + if ((ret->fd = open(filename, oflag)) == -1) + { + ft_memdel(&ret); + return (NULL); + } + ft_bzero(ret->buff, BUFF_SIZE + 1); + return (ret); +} diff --git a/libft/srcs/ft_print_words_tables.c b/libft/srcs/ft_print_words_tables.c new file mode 100644 index 0000000..efa8efc --- /dev/null +++ b/libft/srcs/ft_print_words_tables.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_words_tables.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/15 15:48:16 by tmaze #+# #+# */ +/* Updated: 2018/04/21 15:47:57 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_print_words_tables(char **tab) +{ + unsigned int i; + + i = 0; + while (tab[i]) + { + ft_putendl(tab[i]); + i++; + } +} diff --git a/libft/srcs/ft_printf.c b/libft/srcs/ft_printf.c new file mode 100644 index 0000000..6da2349 --- /dev/null +++ b/libft/srcs/ft_printf.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/28 22:27:58 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:16:43 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void init_handler_tab(char *(*t[16])(t_conv *, va_list)) +{ + t[d] = &handle_output_i_d; + t[D] = &handle_output_i_d; + t[i] = &handle_output_i_d; + t[x] = &handle_output_hexa; + t[X] = &handle_output_hexa; + t[p] = &handle_output_hexa; + t[o] = &handle_output_oct; + t[O] = &handle_output_oct; + t[u] = handle_output_u; + t[U] = handle_output_u; + t[c] = &handle_output_char; + t[C] = &handle_output_wchar; + t[s] = &handle_output_str; + t[S] = &handle_output_wstr; + t[percent] = &handle_output_char; + t[f] = &handle_output_float; +} + +void clean_mem(t_conv *field, va_list ap) +{ + free(field); + va_end(ap); +} + +int handle_conv(const char **str, t_conv *field, va_list ap, int *nb_print) +{ + char *(*handler[16])(t_conv *, va_list); + + init_handler_tab(handler); + (*str)++; + if (check_fields(str, ap, field)) + { + if (!(field->str = handler[field->fl_type](field, ap))) + { + clean_mem(field, ap); + return (0); + } + write(1, field->str, field->str_size); + *nb_print += field->str_size; + free(field->str); + } + else + { + clean_mem(field, ap); + return (0); + } + return (1); +} + +int ft_printf(const char *format, ...) +{ + va_list ap; + const char *str; + int nb_print_chars; + t_conv *field; + + if (!(field = (t_conv *)malloc(sizeof(t_conv)))) + return (-1); + va_start(ap, format); + str = format; + nb_print_chars = 0; + while (str && *str) + { + if (*str != '%') + { + write(1, str, 1); + ++nb_print_chars; + } + else if (!(handle_conv(&str, field, ap, &nb_print_chars))) + return (-1); + ++str; + } + clean_mem(field, ap); + return (nb_print_chars); +} diff --git a/libft/srcs/ft_printf_check_fields.c b/libft/srcs/ft_printf_check_fields.c new file mode 100644 index 0000000..934dfae --- /dev/null +++ b/libft/srcs/ft_printf_check_fields.c @@ -0,0 +1,142 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_fields.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/29 12:33:40 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:16:54 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void check_flags(const char **str, t_conv *field) +{ + while (**str == '-' || **str == '+' || **str == '0' || **str == '#' + || **str == ' ') + { + if (**str == '-') + field->fl_minus = 1; + else if (**str == '+') + field->fl_plus = 1; + else if (**str == '0') + field->fl_zero = 1; + else if (**str == '#') + field->fl_hashtag = 1; + else if (**str == ' ') + field->fl_space = 1; + (*str)++; + } +} + +void check_width(const char **str, t_conv *field, va_list ap) +{ + int tmp; + + if (**str == '*') + { + tmp = va_arg(ap, int); + field->fl_witdth = (tmp < 0) ? -tmp : tmp; + if (tmp < 0) + field->fl_minus = 1; + (*str)++; + } + else + { + field->fl_witdth = 0; + while (**str >= '0' && **str <= '9') + { + field->fl_witdth *= 10; + field->fl_witdth += **str - '0'; + (*str)++; + } + } +} + +ssize_t check_precision(const char **str, t_conv *field, va_list ap) +{ + int tmp; + + if (**str == '.') + { + (*str)++; + if (**str == '*') + { + if ((tmp = va_arg(ap, int)) >= 0) + field->fl_prec = tmp; + (*str)++; + } + else if (**str >= '0' && **str <= '9') + { + field->fl_prec = 0; + while (**str >= '0' && **str <= '9') + { + field->fl_prec *= 10; + field->fl_prec += **str - '0'; + (*str)++; + } + } + else + field->fl_prec = 0; + } + return (1); +} + +void check_size(const char **str, t_conv *field) +{ + if (**str == 'h' || **str == 'l' || **str == 'j' || **str == 'z' + || **str == 'L') + { + if (**str == 'h' && *(*str + 1) == 'h') + { + field->fl_size = hh; + (*str)++; + } + else if (**str == 'h') + field->fl_size = h; + else if (**str == 'l' && *(*str + 1) == 'l') + { + field->fl_size = ll; + (*str)++; + } + else if (**str == 'l') + field->fl_size = l; + else if (**str == 'j') + field->fl_size = l; + else if (**str == 'z') + field->fl_size = l; + else if (**str == 'L') + field->fl_size = L; + (*str)++; + } +} + +ssize_t check_fields(const char **str, va_list ap, t_conv *field) +{ + init_struct_conv(field); + check_flags(str, field); + check_width(str, field, ap); + if (!(check_precision(str, field, ap))) + return (0); + check_size(str, field); + if (!(check_type_one(str, field)) || !(check_type_two(str, field))) + return (0); + if ((field->fl_type != d && field->fl_type != D && field->fl_type != i + && field->fl_type != f) || field->fl_plus == 1) + field->fl_space = 0; + if (field->fl_type < s && field->fl_prec != -1) + field->fl_zero = 0; + else if (field->fl_type < s && field->fl_prec == -1) + field->fl_prec = 1; + if (field->fl_type == C && field->fl_size == h) + field->fl_type = c; + if (field->fl_type == c && field->fl_size == l) + field->fl_type = C; + if (field->fl_type == S && field->fl_size == h) + field->fl_type = s; + if (field->fl_type == s && field->fl_size == l) + field->fl_type = S; + return (1); +} diff --git a/libft/srcs/ft_printf_check_type.c b/libft/srcs/ft_printf_check_type.c new file mode 100644 index 0000000..8b53e2f --- /dev/null +++ b/libft/srcs/ft_printf_check_type.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_type.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/02 21:09:18 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:18:05 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +ssize_t check_type_one(const char **str, t_conv *field) +{ + if (**str == 'd' || **str == 'D' || **str == 'i' || **str == 'o' + || **str == 'u' || **str == 'x' || **str == 'X' || **str == 's' + || **str == 'S' || **str == 'c' || **str == 'C' || **str == 'U' + || **str == 'p' || **str == '%' || **str == 'O' || **str == 'f') + { + if (**str == 'd') + field->fl_type = d; + else if (**str == 'D') + field->fl_type = D; + else if (**str == 'i') + field->fl_type = i; + else if (**str == 'o') + field->fl_type = o; + else if (**str == 'u') + field->fl_type = u; + else if (**str == 'U') + field->fl_type = U; + else if (**str == 'x') + field->fl_type = x; + else if (**str == 'X') + field->fl_type = X; + return (1); + } + return (0); +} + +ssize_t check_type_two(const char **str, t_conv *field) +{ + if (**str == 'd' || **str == 'D' || **str == 'i' || **str == 'o' + || **str == 'u' || **str == 'x' || **str == 'X' || **str == 's' + || **str == 'S' || **str == 'c' || **str == 'C' || **str == 'U' + || **str == 'p' || **str == '%' || **str == 'O' || **str == 'f') + { + if (**str == 's') + field->fl_type = s; + else if (**str == 'S') + field->fl_type = S; + else if (**str == 'c') + field->fl_type = c; + else if (**str == 'C') + field->fl_type = C; + else if (**str == 'p') + field->fl_type = p; + else if (**str == '%') + field->fl_type = percent; + else if (**str == 'O') + field->fl_type = O; + else if (**str == 'f') + field->fl_type = f; + return (1); + } + return (0); +} diff --git a/libft/srcs/ft_printf_tools.c b/libft/srcs/ft_printf_tools.c new file mode 100644 index 0000000..07cbc68 --- /dev/null +++ b/libft/srcs/ft_printf_tools.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/02 21:49:45 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:18:16 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void init_struct_conv(t_conv *field) +{ + field->fl_minus = 0; + field->fl_plus = 0; + field->fl_zero = 0; + field->fl_hashtag = 0; + field->fl_space = 0; + field->fl_witdth = 0; + field->fl_prec = -1; + field->fl_size = -1; + field->fl_type = -1; + field->str = NULL; + field->str_size = 0; +} + +char *ft_strnewb(size_t size) +{ + size_t i; + char *str; + + i = -1; + if (!(str = (char *)malloc(sizeof(char) * (size + 1)))) + return (NULL); + while (++i < (size + 1)) + str[i] = '\0'; + return (str); +} + +uint64_t dmod(long double nb, long double mod) +{ + uint64_t res; + + res = (uint64_t)(nb - mod * (uint64_t)(nb / mod)); + return (res); +} diff --git a/libft/srcs/ft_printf_tools_char.c b/libft/srcs/ft_printf_tools_char.c new file mode 100644 index 0000000..f6d93e2 --- /dev/null +++ b/libft/srcs/ft_printf_tools_char.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/19 08:51:48 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:22:07 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *handle_output_char(t_conv *field, va_list ap) +{ + char *output; + unsigned char c; + + if (field->fl_type == percent) + c = '%'; + else + c = (unsigned char)va_arg(ap, int); + field->str_size = (field->fl_witdth > 1) ? field->fl_witdth : 1; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + output[0] = c; + ft_align_wchar(output, field); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_float.c b/libft/srcs/ft_printf_tools_float.c new file mode 100644 index 0000000..d943957 --- /dev/null +++ b/libft/srcs/ft_printf_tools_float.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_tools_float.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/13 12:52:34 by tmaze #+# #+# */ +/* Updated: 2019/03/07 22:23:31 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void roundup(char *str, long double n, int i, int prec) +{ + int ret; + int len; + + ret = 1; + len = ft_strlen(str); + if ((int)n >= 5 && (n - (int)n) > 0.0) + { + while (i-- != 0 && ret == 1) + { + if (str[i] != '.' && str[i] + 1 > '9' && (ret = 1)) + str[i] = '0'; + else if (str[i] != '.' && !(ret = 0)) + str[i]++; + } + if (ret == 1 && (i = ft_strlen(str)) < 4980) + while (--i > 0) + str[i + 1] = str[i]; + str[0] = (ret == 1 && i < 4980) ? '1' : str[0]; + } + i = 0; + while (str[i] && str[i] != '.') + i++; + i += prec + 1; + while (i < len) + str[i++] = '\0'; +} + +void getint(char *str, long double n, int *i) +{ + uint64_t nb; + long double div; + + nb = n; + div = 1; + while ((nb / div) >= 10) + div *= 10; + nb /= div; + while (*i < 4935 && div != 0) + { + str[*i] = (int)nb + '0'; + div = (div == 1) ? 0 : (div / 10); + nb = dmod((n / div), 10); + (*i)++; + } +} + +char *getnbrstr(long double nb, t_conv *field) +{ + char str[4980]; + int i; + int j; + + ft_bzero(str, 4980); + nb = nb * ((nb < 0.0) ? -1 : 1); + i = 0; + getint(str, nb, &i); + j = 0; + if (field->fl_prec > 0 || field->fl_hashtag) + str[i++] = '.'; + nb = (nb - (uint64_t)nb) * ((nb < 0.0) ? -1 : 1) * 10; + while (j < field->fl_prec && i + j < 4980) + { + str[i + j] = (uint64_t)nb + '0'; + nb = (nb - (uint64_t)nb) * 10; + j++; + } + i += j; + roundup(str, nb, i, field->fl_prec); + return (ft_strdup(str)); +} + +void set_strf(t_conv *f, char *nbrstr, long double nb, int pad) +{ + if (!f->fl_minus && !f->fl_zero) + ft_memset(f->str, ' ', (pad > 0) ? pad : 0); + if (nb < 0.0) + f->str[ft_strlen(f->str)] = '-'; + else if (f->fl_plus || f->fl_space) + f->str[ft_strlen(f->str)] = (f->fl_plus) ? '+' : ' '; + if (!f->fl_minus && f->fl_zero) + ft_memset(&f->str[ft_strlen(f->str)], '0', (pad > 0) ? pad : 0); + ft_strcat(f->str, nbrstr); + if (f->fl_minus) + ft_memset(&f->str[ft_strlen(f->str)], ' ', (pad > 0) ? pad : 0); +} + +char *handle_output_float(t_conv *field, va_list ap) +{ + long double nb; + int pad; + int size; + char *nbrstr; + + if (field->fl_size == L) + nb = (long double)va_arg(ap, long double); + else + nb = (long double)va_arg(ap, double); + field->fl_prec = (field->fl_prec == -1) ? 6 : field->fl_prec; + nbrstr = getnbrstr(nb, field); + size = set_malloc_sizef(nb, nbrstr, field); + pad = field->str_size - size; + if ((field->str = ft_strnew(field->str_size)) != NULL) + set_strf(field, nbrstr, nb, pad); + ft_strdel(&nbrstr); + return (field->str); +} diff --git a/libft/srcs/ft_printf_tools_hexa.c b/libft/srcs/ft_printf_tools_hexa.c new file mode 100644 index 0000000..a2e6857 --- /dev/null +++ b/libft/srcs/ft_printf_tools_hexa.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_hexa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/13 19:58:30 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:23:44 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *handler_hexa(uintmax_t nb, t_conv *field) +{ + int size; + uintmax_t n; + char *str; + + size = set_precision_sizex(nb, field); + set_malloc_sizeh(nb, field); + if (!(str = ft_strnew(field->str_size))) + return (NULL); + n = nb; + while (n) + { + str[--size] = (field->fl_type == X) ? "0123456789ABCDEF"[n % 16] + : "0123456789abcdef"[n % 16]; + n /= 16; + } + while (--size >= 0 && field->fl_prec) + str[size] = '0'; + if (field->fl_hashtag) + str[1] = (field->fl_type == X) ? 'X' : 'x'; + if (field->fl_hashtag) + str[0] = '0'; + ft_align_hex(str, field); + return (str); +} + +void ft_align_hex_zero(char *str, t_conv *field) +{ + int i; + int len; + + i = 0; + len = (int)ft_strlen(str); + if (field->fl_hashtag == 0 || (field->fl_hashtag == 1 + && field->fl_zero == 0)) + { + ft_memmove(str + (field->str_size - len), str, len); + while (i < field->str_size - len) + str[i++] = (field->fl_zero) ? '0' : ' '; + } + else + { + i = 1; + ft_memmove(str + (field->str_size - len + 2), str + 2, len - 2); + while (++i < field->str_size - len + 2) + str[i] = '0'; + } +} + +void ft_align_hex(char *str, t_conv *field) +{ + int i; + + i = 0; + if ((int)(ft_strlen(str)) < field->str_size) + { + if (field->fl_minus == 1) + { + while (str[i]) + i++; + while (i < field->str_size) + str[i++] = ' '; + } + else + { + ft_align_hex_zero(str, field); + } + } +} + +char *select_hexa_handler(t_conv *field, va_list ap) +{ + uintmax_t tmp; + + if (field->fl_size == h) + tmp = (uintmax_t)(short unsigned int)va_arg(ap, unsigned int); + else if (field->fl_size == hh) + tmp = (uintmax_t)(unsigned char)va_arg(ap, unsigned int); + else if (field->fl_size == l) + tmp = (uintmax_t)va_arg(ap, long unsigned int); + else if (field->fl_size == ll) + tmp = (uintmax_t)va_arg(ap, unsigned long long int); + else if (field->fl_size == j) + tmp = va_arg(ap, uintmax_t); + else if (field->fl_size == z) + tmp = (uintmax_t)va_arg(ap, size_t); + else + tmp = (uintmax_t)va_arg(ap, unsigned int); + if (tmp == 0 && field->fl_type != p) + field->fl_hashtag = 0; + return (handler_hexa(tmp, field)); +} + +char *handle_output_hexa(t_conv *field, va_list ap) +{ + char *output; + + if (field->fl_type == p) + { + field->fl_hashtag = 1; + field->fl_size = j; + } + output = select_hexa_handler(field, ap); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_int.c b/libft/srcs/ft_printf_tools_int.c new file mode 100644 index 0000000..bdaa061 --- /dev/null +++ b/libft/srcs/ft_printf_tools_int.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/10 14:02:42 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:23:55 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *handler_int(uintmax_t nb, t_conv *field) +{ + int size; + uintmax_t n; + char *str; + + size = set_precision_sizes(nb, field); + set_malloc_sizes(nb, field); + if (!(str = ft_strnew(field->str_size))) + return (NULL); + n = ((intmax_t)nb >= 0) ? (uintmax_t)nb : -(uintmax_t)nb; + while (n) + { + str[--size] = n % 10 + '0'; + n /= 10; + } + while (--size >= 0 && field->fl_prec) + str[size] = '0'; + if ((intmax_t)nb < 0) + str[0] = '-'; + else if (field->fl_plus || field->fl_space) + str[0] = (field->fl_plus) ? '+' : ' '; + ft_align_str(str, field); + return (str); +} + +void ft_align_str_zero(char *str, t_conv *field) +{ + int i; + int len; + + i = 0; + len = (int)ft_strlen(str); + if (field->fl_zero == 0 + || (str[0] != '-' && str[0] != '+' && str[0] != ' ')) + { + ft_memmove(str + (field->str_size - len), str, len); + while (i < field->str_size - len) + str[i++] = (field->fl_zero && field->fl_prec == 1) ? '0' : ' '; + } + else + { + ft_memmove(str + (field->str_size - len + 1), str + 1, len - 1); + while (++i < field->str_size - len + 1) + str[i] = '0'; + } +} + +void ft_align_str(char *str, t_conv *field) +{ + int i; + + i = 0; + if ((int)(ft_strlen(str)) < field->str_size) + { + if (field->fl_minus == 1) + { + while (str[i]) + i++; + while (i < field->str_size) + str[i++] = ' '; + } + else + { + ft_align_str_zero(str, field); + } + } +} + +char *select_int_handler(t_conv *field, va_list ap) +{ + uintmax_t tmp; + + if (field->fl_size == h) + tmp = (uintmax_t)(short int)va_arg(ap, int); + else if (field->fl_size == hh) + tmp = (uintmax_t)(char)va_arg(ap, int); + else if (field->fl_size == l) + tmp = (uintmax_t)va_arg(ap, long int); + else if (field->fl_size == ll) + tmp = (uintmax_t)va_arg(ap, long long int); + else if (field->fl_size == j) + tmp = (uintmax_t)va_arg(ap, intmax_t); + else if (field->fl_size == z) + tmp = (uintmax_t)va_arg(ap, ssize_t); + else + tmp = (uintmax_t)va_arg(ap, int); + return (handler_int(tmp, field)); +} + +char *handle_output_i_d(t_conv *field, va_list ap) +{ + char *output; + + if (field->fl_type == D) + field->fl_size = l; + output = select_int_handler(field, ap); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_lenght.c b/libft/srcs/ft_printf_tools_lenght.c new file mode 100644 index 0000000..d1c5a1d --- /dev/null +++ b/libft/srcs/ft_printf_tools_lenght.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_lenght.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/01 15:40:34 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:19:06 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +unsigned int ft_bin_size(unsigned int nb) +{ + unsigned int n; + unsigned int size; + + n = nb; + size = 1; + while (n /= 2) + size++; + return (size); +} + +int ft_wstrlen(const wint_t *str) +{ + int i; + int len; + + i = 0; + len = 0; + while (str[i]) + { + if (str[i] < 0 || (MB_CUR_MAX == 1 && str[i] > 0xFF) + || (str[i] >= 0xD800 && str[i] <= 0xDFFF) || str[i] > 0x10FFFF) + return (-1); + else if (str[i] <= 0x7F || (MB_CUR_MAX == 1 && str[i] <= 0xFF)) + len += 1; + else if (str[i] <= 0x7FF) + len += 2; + else if (str[i] <= 0xFFFF) + len += 3; + else if (str[i] <= 0x10FFFF) + len += 4; + ++i; + } + return (len); +} diff --git a/libft/srcs/ft_printf_tools_malloc_size.c b/libft/srcs/ft_printf_tools_malloc_size.c new file mode 100644 index 0000000..17256fc --- /dev/null +++ b/libft/srcs/ft_printf_tools_malloc_size.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_malloc_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/01 13:43:37 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:20:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void set_malloc_sizes(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + + if (nb == 0 && field->fl_prec == 0) + field->str_size = 0; + else + field->str_size = 1; + tmp = ((intmax_t)nb >= 0) ? (uintmax_t)nb : -(uintmax_t)nb; + while (tmp /= 10) + ++field->str_size; + field->str_size = (field->str_size < field->fl_prec) + ? field->fl_prec : field->str_size; + field->str_size += ((intmax_t)nb < 0 || field->fl_plus + || field->fl_space) ? 1 : 0; + field->str_size = (field->str_size < field->fl_witdth) + ? field->fl_witdth : field->str_size; +} + +void set_malloc_sizeh(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + + if (nb == 0 && field->fl_prec == 0) + field->str_size = 0; + else + field->str_size = 1; + tmp = nb; + while (tmp /= 16) + field->str_size++; + field->str_size = (field->str_size < field->fl_prec) + ? field->fl_prec : field->str_size; + field->str_size += (field->fl_hashtag) ? 2 : 0; + field->str_size = (field->str_size < field->fl_witdth) + ? field->fl_witdth : field->str_size; +} + +void set_malloc_sizeo(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + + if (nb == 0) + field->str_size = 0; + else + field->str_size = 1; + tmp = nb; + while (tmp /= 8) + ++field->str_size; + field->str_size += (field->fl_hashtag) ? 1 : 0; + field->str_size = (field->str_size < field->fl_prec) + ? field->fl_prec : field->str_size; + field->str_size = (field->str_size < field->fl_witdth) + ? field->fl_witdth : field->str_size; +} + +int set_malloc_sizef(long double nb, char *nbrstr, t_conv *field) +{ + int size; + + size = ft_strlen(nbrstr); + if (nb < 0.0 || field->fl_plus || field->fl_space) + size++; + field->str_size = (field->fl_witdth > size) ? field->fl_witdth : size; + return (size); +} diff --git a/libft/srcs/ft_printf_tools_oct.c b/libft/srcs/ft_printf_tools_oct.c new file mode 100644 index 0000000..c3e6215 --- /dev/null +++ b/libft/srcs/ft_printf_tools_oct.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_oct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/13 20:13:28 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:24:23 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *handler_oct(uintmax_t nb, t_conv *field) +{ + int size; + uintmax_t n; + char *str; + + size = set_precision_sizeo(nb, field); + set_malloc_sizeo(nb, field); + if (!(str = ft_strnew(field->str_size))) + return (NULL); + n = nb; + while (n) + { + str[--size] = n % 8 + '0'; + n /= 8; + } + while (--size >= 0 && field->fl_prec) + str[size] = '0'; + if (field->fl_hashtag) + str[0] = '0'; + ft_align_oct(str, field); + return (str); +} + +void ft_align_oct_zero(char *str, t_conv *field) +{ + int i; + int len; + + i = 0; + len = (int)ft_strlen(str); + if (field->fl_zero == 0 || field->fl_hashtag == 0) + { + ft_memmove(str + (field->str_size - len), str, len); + while (i < field->str_size - len) + str[i++] = (field->fl_zero) ? '0' : ' '; + } + else + { + ft_memmove(str + (field->str_size - len + 1), str + 1, len - 1); + while (++i < field->str_size - len + 1) + str[i] = '0'; + } +} + +void ft_align_oct(char *str, t_conv *field) +{ + int i; + + i = 0; + if ((int)(ft_strlen(str)) < field->str_size) + { + if (field->fl_minus == 1) + { + while (str[i]) + i++; + while (i < field->str_size) + str[i++] = ' '; + } + else + { + ft_align_oct_zero(str, field); + } + } +} + +char *select_oct_handler(t_conv *field, va_list ap) +{ + uintmax_t tmp; + + if (field->fl_size == h) + tmp = (uintmax_t)(short unsigned int)va_arg(ap, unsigned int); + else if (field->fl_size == hh) + tmp = (uintmax_t)(unsigned char)va_arg(ap, unsigned int); + else if (field->fl_size == l) + tmp = (uintmax_t)va_arg(ap, long unsigned int); + else if (field->fl_size == ll) + tmp = (uintmax_t)va_arg(ap, unsigned long long int); + else if (field->fl_size == j) + tmp = va_arg(ap, uintmax_t); + else if (field->fl_size == z) + tmp = (uintmax_t)va_arg(ap, size_t); + else + tmp = (uintmax_t)va_arg(ap, unsigned int); + return (handler_oct(tmp, field)); +} + +char *handle_output_oct(t_conv *field, va_list ap) +{ + char *output; + + if (field->fl_type == O) + field->fl_size = l; + output = select_oct_handler(field, ap); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_prec_size.c b/libft/srcs/ft_printf_tools_prec_size.c new file mode 100644 index 0000000..b9d2cc0 --- /dev/null +++ b/libft/srcs/ft_printf_tools_prec_size.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_prec_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/01 15:36:42 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:20:49 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int set_precision_sizex(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + int size; + + if (nb == 0 && field->fl_prec == 0) + size = 0; + else + size = 1; + tmp = nb; + while (tmp /= 16) + ++size; + size = (size < field->fl_prec) ? field->fl_prec : size; + size += (field->fl_hashtag) ? 2 : 0; + return (size); +} + +int set_precision_sizes(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + int size; + + if (nb == 0 && field->fl_prec == 0) + size = 0; + else + size = 1; + tmp = ((intmax_t)nb >= 0) ? (uintmax_t)nb : -(uintmax_t)nb; + while (tmp /= 10) + ++size; + size = (size < field->fl_prec) ? field->fl_prec : size; + size += ((intmax_t)nb < 0 || field->fl_plus || field->fl_space) ? 1 : 0; + return (size); +} + +int set_precision_sizeo(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + int size; + + if (nb == 0 && field->fl_prec == 0) + size = 0; + else if (nb == 0) + { + size = 0; + field->fl_hashtag = 0; + } + else + size = 1; + tmp = nb; + while (tmp /= 8) + ++size; + size += (field->fl_hashtag) ? 1 : 0; + size = (size < field->fl_prec) ? field->fl_prec : size; + return (size); +} + +int set_prec_size_wstr(const wint_t *str, t_conv *field) +{ + int i; + int tmp; + + i = 0; + field->str_size = 0; + while (str[i] && (field->str_size < field->fl_prec)) + { + tmp = field->str_size; + if (str[i] < 0 || (MB_CUR_MAX == 1 && str[i] > 0xFF) + || (str[i] >= 0xD800 && str[i] <= 0xDFFF) || str[i] > 0x10FFFF) + return (0); + else if (str[i] <= 0x7F || (MB_CUR_MAX == 1 && str[i] <= 0xFF)) + field->str_size += (field->str_size + 1 <= field->fl_prec) ? 1 : 0; + else if (str[i] <= 0x7FF) + field->str_size += (field->str_size + 2 <= field->fl_prec) ? 2 : 0; + else if (str[i] <= 0xFFFF) + field->str_size += (field->str_size + 3 <= field->fl_prec) ? 3 : 0; + else if (str[i] <= 0x10FFFF) + field->str_size += (field->str_size + 4 <= field->fl_prec) ? 4 : 0; + if (tmp == field->str_size) + return (1); + ++i; + } + return (1); +} diff --git a/libft/srcs/ft_printf_tools_str.c b/libft/srcs/ft_printf_tools_str.c new file mode 100644 index 0000000..8b39deb --- /dev/null +++ b/libft/srcs/ft_printf_tools_str.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_tools_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/19 09:05:22 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:27:38 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_align_wstr(char *str, t_conv *field) +{ + int i; + int len; + + i = 0; + if (field->fl_witdth >= 1) + { + if (field->fl_minus) + { + while (str[i]) + i++; + while (i < field->str_size) + str[i++] = ' '; + } + else + { + len = (int)ft_strlen(str); + ft_memmove(str + (field->str_size - len), str, len); + while (i < (field->str_size - len)) + { + str[i] = ' '; + i++; + } + } + } +} + +char *handle_output_str(t_conv *field, va_list ap) +{ + char *output; + int len; + const char *str; + + str = va_arg(ap, const char *); + if (str == NULL) + str = "(null)"; + len = (int)ft_strlen(str); + if (field->fl_prec != -1) + len = (len > field->fl_prec) ? field->fl_prec : len; + field->str_size = (field->fl_witdth > len) ? field->fl_witdth : len; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + if (field->fl_prec == -1) + ft_strcpy(output, str); + else + ft_strncpy(output, str, len); + ft_align_wstr(output, field); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_unsigned_int.c b/libft/srcs/ft_printf_tools_unsigned_int.c new file mode 100644 index 0000000..e46bcae --- /dev/null +++ b/libft/srcs/ft_printf_tools_unsigned_int.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_unsigned_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/13 19:43:31 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:25:00 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void set_malloc_sizeu(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + + if (nb == 0 && field->fl_prec == 0) + field->str_size = 0; + else + field->str_size = 1; + tmp = nb; + while (tmp /= 10) + ++field->str_size; + field->str_size = (field->str_size < field->fl_prec) + ? field->fl_prec : field->str_size; + field->str_size = (field->str_size < field->fl_witdth) + ? field->fl_witdth : field->str_size; +} + +int set_precision_sizeu(uintmax_t nb, t_conv *field) +{ + uintmax_t tmp; + int size; + + if (nb == 0 && field->fl_prec == 0) + size = 0; + else + size = 1; + tmp = nb; + while (tmp /= 10) + ++size; + size = (size < field->fl_prec) ? field->fl_prec : size; + return (size); +} + +char *handler_uns(uintmax_t nb, t_conv *field) +{ + int size; + uintmax_t n; + char *str; + + size = set_precision_sizeu(nb, field); + set_malloc_sizeu(nb, field); + if (!(str = ft_strnew(field->str_size))) + return (NULL); + n = nb; + while (n) + { + str[--size] = n % 10 + '0'; + n /= 10; + } + while (--size >= 0 && field->fl_prec) + str[size] = '0'; + ft_align_str(str, field); + return (str); +} + +char *select_uns_int_handler(t_conv *field, va_list ap) +{ + uintmax_t tmp; + + if (field->fl_size == h) + tmp = (uintmax_t)(short unsigned int)va_arg(ap, unsigned int); + else if (field->fl_size == hh) + tmp = (uintmax_t)(unsigned char)va_arg(ap, unsigned int); + else if (field->fl_size == l) + tmp = (uintmax_t)va_arg(ap, long unsigned int); + else if (field->fl_size == ll) + tmp = (uintmax_t)va_arg(ap, unsigned long long int); + else if (field->fl_size == j) + tmp = va_arg(ap, uintmax_t); + else if (field->fl_size == z) + tmp = (uintmax_t)va_arg(ap, size_t); + else + tmp = (uintmax_t)va_arg(ap, unsigned int); + return (handler_uns(tmp, field)); +} + +char *handle_output_u(t_conv *field, va_list ap) +{ + char *output; + + if (field->fl_type == U) + field->fl_size = l; + output = select_uns_int_handler(field, ap); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_wchar_t.c b/libft/srcs/ft_printf_tools_wchar_t.c new file mode 100644 index 0000000..d48cfc7 --- /dev/null +++ b/libft/srcs/ft_printf_tools_wchar_t.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_wchar_t.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/18 18:10:11 by klebon #+# #+# */ +/* Updated: 2019/03/16 15:53:50 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_align_wchar(char *str, t_conv *field) +{ + int i; + int len; + + len = (int)ft_strlen(str); + len = (len) ? len : 1; + if (len < field->str_size) + { + if (field->fl_minus == 1) + { + i = 1; + while (str[i]) + ++i; + while (i < field->str_size) + str[i++] = ' '; + } + else + { + i = 0; + ft_memmove(str + (field->str_size - len), str, len); + while (i < field->str_size - len) + str[i++] = (field->fl_zero) ? '0' : ' '; + } + } +} + +char *handler_2oct_char(wint_t c, t_conv *field) +{ + char *output; + + field->str_size = (field->fl_witdth > 2) ? field->fl_witdth : 2; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + output[0] = (c >> 6) + 0xC0; + output[1] = (c & 0x3F) + 0x80; + ft_align_wchar(output, field); + return (output); +} + +char *handler_3oct_char(wint_t c, t_conv *field) +{ + char *output; + + field->str_size = (field->fl_witdth > 3) ? field->fl_witdth : 3; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + output[0] = (c >> 12) + 0xE0; + output[1] = ((c >> 6) & 0x3F) + 0x80; + output[2] = (c & 0x3F) + 0x80; + ft_align_wchar(output, field); + return (output); +} + +char *handler_4oct_char(wint_t c, t_conv *field) +{ + char *output; + + field->str_size = (field->fl_witdth > 4) ? field->fl_witdth : 4; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + output[0] = (c >> 18) + 0xF0; + output[1] = ((c >> 12) & 0x3F) + 0x80; + output[2] = ((c >> 6) & 0x3F) + 0x80; + output[3] = (c & 0x3F) + 0x80; + ft_align_wchar(output, field); + return (output); +} + +char *handle_output_wchar(t_conv *field, va_list ap) +{ + char *output; + wint_t c; + + c = (wint_t)va_arg(ap, wint_t); + if (c < 0 || (MB_CUR_MAX == 1 && c > 0xFF) || (c >= 0xD800 && c <= 0xDFFF)) + return (NULL); + else if (c <= 0x7F || (MB_CUR_MAX == 1 && c <= 0xFF)) + { + field->str_size = (field->fl_witdth > 1) ? field->fl_witdth : 1; + if (!(output = ft_strnew(field->str_size))) + return (NULL); + output[0] = (char)c; + ft_align_wchar(output, field); + } + else if (c <= 0x7FF) + output = handler_2oct_char(c, field); + else if (c <= 0xFFFF) + output = handler_3oct_char(c, field); + else if (c <= 0x10FFFF) + output = handler_4oct_char(c, field); + else + return (NULL); + return (output); +} diff --git a/libft/srcs/ft_printf_tools_wstr.c b/libft/srcs/ft_printf_tools_wstr.c new file mode 100644 index 0000000..762dc88 --- /dev/null +++ b/libft/srcs/ft_printf_tools_wstr.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tools_wstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: klebon +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/23 19:48:06 by klebon #+# #+# */ +/* Updated: 2019/03/07 22:25:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void handler_2oct_wstr(wint_t c, char *output, int *i) +{ + output[(*i)++] = (c >> 6) + 0xC0; + output[(*i)++] = (c & 0x3F) + 0x80; +} + +void handler_3oct_wstr(wint_t c, char *output, int *i) +{ + output[(*i)++] = (c >> 12) + 0xE0; + output[(*i)++] = ((c >> 6) & 0x3F) + 0x80; + output[(*i)++] = (c & 0x3F) + 0x80; +} + +void handler_4oct_wstr(wint_t c, char *output, int *i) +{ + output[(*i)++] = (c >> 18) + 0xF0; + output[(*i)++] = ((c >> 12) & 0x3F) + 0x80; + output[(*i)++] = ((c >> 6) & 0x3F) + 0x80; + output[(*i)++] = (c & 0x3F) + 0x80; +} + +void convert_wstr_to_str(const wint_t *str, char *output, t_conv *field) +{ + int i; + int j; + int tmp; + + i = 0; + j = 0; + while (str[i] && j <= field->fl_prec) + { + tmp = j; + if (j + 1 <= field->fl_prec + && (str[i] <= 0x7F || (MB_CUR_MAX == 1 && str[i] <= 0xFF))) + output[j++] = (char)str[i]; + else if (j + 2 <= field->fl_prec && str[i] <= 0x7FF) + handler_2oct_wstr(str[i], output, &j); + else if (j + 3 <= field->fl_prec && str[i] <= 0xFFFF) + handler_3oct_wstr(str[i], output, &j); + else if (j + 4 <= field->fl_prec) + handler_4oct_wstr(str[i], output, &j); + ++i; + if (tmp == j) + break ; + } +} + +char *handle_output_wstr(t_conv *field, va_list ap) +{ + char *output; + const wint_t *str; + + str = (wint_t *)va_arg(ap, const wint_t *); + if (str == NULL) + str = L"(null)"; + if (field->fl_prec != -1) + { + if (set_prec_size_wstr(str, field) == 0) + return (NULL); + } + else + { + if ((field->str_size = ft_wstrlen(str)) == -1) + return (NULL); + } + if (field->fl_prec == -1) + field->fl_prec = field->str_size; + if (field->fl_witdth > field->str_size) + field->str_size += (field->fl_witdth - field->str_size); + if (!(output = ft_strnew(field->str_size))) + return (NULL); + convert_wstr_to_str(str, output, field); + ft_align_wstr(output, field); + return (output); +} diff --git a/libft/srcs/ft_putchar.c b/libft/srcs/ft_putchar.c new file mode 100644 index 0000000..0290a79 --- /dev/null +++ b/libft/srcs/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 19:03:57 by tmaze #+# #+# */ +/* Updated: 2018/04/07 23:02:29 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/libft/srcs/ft_putchar_fd.c b/libft/srcs/ft_putchar_fd.c new file mode 100644 index 0000000..8bceeb9 --- /dev/null +++ b/libft/srcs/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 22:43:23 by tmaze #+# #+# */ +/* Updated: 2018/05/01 13:37:42 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/srcs/ft_putendl.c b/libft/srcs/ft_putendl.c new file mode 100644 index 0000000..4d0be4c --- /dev/null +++ b/libft/srcs/ft_putendl.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 22:37:44 by tmaze #+# #+# */ +/* Updated: 2019/03/19 14:50:20 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + if (s != NULL) + ft_putstr(s); + ft_putchar('\n'); +} diff --git a/libft/srcs/ft_putendl2.c b/libft/srcs/ft_putendl2.c new file mode 100644 index 0000000..af66e22 --- /dev/null +++ b/libft/srcs/ft_putendl2.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/19 15:14:49 by tmaze #+# #+# */ +/* Updated: 2019/03/20 17:00:52 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_putendl2(char const *s) +{ + char *str; + + if (s != NULL && (str = ft_strnew(ft_strlen(s) + 1)) != NULL) + { + ft_strcpy(str, s); + ft_strcat(str, "\n"); + ft_putstr(str); + ft_strdel(&str); + return (0); + } + return (1); +} diff --git a/libft/srcs/ft_putendl_fd.c b/libft/srcs/ft_putendl_fd.c new file mode 100644 index 0000000..365dcbd --- /dev/null +++ b/libft/srcs/ft_putendl_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 13:02:13 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:27:49 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + if (s != NULL && fd >= 0) + { + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); + } +} diff --git a/libft/srcs/ft_putendl_fd2.c b/libft/srcs/ft_putendl_fd2.c new file mode 100644 index 0000000..b9f4635 --- /dev/null +++ b/libft/srcs/ft_putendl_fd2.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/20 16:44:51 by tmaze #+# #+# */ +/* Updated: 2019/03/20 17:00:16 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_putendl_fd2(char const *s, int fd) +{ + char *str; + + if (s != NULL && (str = ft_strnew(ft_strlen(s) + 1)) != NULL) + { + ft_strcpy(str, s); + ft_strcat(str, "\n"); + ft_putstr_fd(str, fd); + ft_strdel(&str); + return (0); + } + return (1); +} diff --git a/libft/srcs/ft_putnbr.c b/libft/srcs/ft_putnbr.c new file mode 100644 index 0000000..8233431 --- /dev/null +++ b/libft/srcs/ft_putnbr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 12:52:53 by tmaze #+# #+# */ +/* Updated: 2018/04/08 15:33:50 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + ft_putnbr_fd(n, 1); +} diff --git a/libft/srcs/ft_putnbr_fd.c b/libft/srcs/ft_putnbr_fd.c new file mode 100644 index 0000000..3a4e8d1 --- /dev/null +++ b/libft/srcs/ft_putnbr_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 13:05:24 by tmaze #+# #+# */ +/* Updated: 2018/05/05 16:50:14 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + unsigned long mult; + int nb_nbr; + unsigned long nbr; + + mult = 10; + nbr = ft_abs(n); + while (mult <= nbr) + mult *= 10; + if (n < 0) + write(fd, "-", 1); + while (mult > 1) + { + nb_nbr = '0' + ((nbr % mult) / (mult / 10)); + write(fd, &nb_nbr, 1); + mult /= 10; + } +} diff --git a/libft/srcs/ft_putnbrpad.c b/libft/srcs/ft_putnbrpad.c new file mode 100644 index 0000000..efe5348 --- /dev/null +++ b/libft/srcs/ft_putnbrpad.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrpad.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/07/31 13:51:17 by tmaze #+# #+# */ +/* Updated: 2018/07/31 14:04:43 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbrpad(int nbr, size_t size, char pad, char align) +{ + if (align == 'd') + while (size-- != 0) + ft_putchar(pad); + ft_putnbr(nbr); + if (align == 'g') + while (size-- != 0) + ft_putchar(pad); +} diff --git a/libft/srcs/ft_putstr.c b/libft/srcs/ft_putstr.c new file mode 100644 index 0000000..3d7077e --- /dev/null +++ b/libft/srcs/ft_putstr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 19:13:17 by tmaze #+# #+# */ +/* Updated: 2018/04/12 11:23:29 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + if (s != NULL) + write(1, s, ft_strlen(s)); +} diff --git a/libft/srcs/ft_putstr_fd.c b/libft/srcs/ft_putstr_fd.c new file mode 100644 index 0000000..89d46e0 --- /dev/null +++ b/libft/srcs/ft_putstr_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 23:06:30 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:26:43 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + if (s != NULL && fd >= 0) + write(fd, s, ft_strlen(s)); +} diff --git a/libft/srcs/ft_putstrn.c b/libft/srcs/ft_putstrn.c new file mode 100644 index 0000000..8cdab05 --- /dev/null +++ b/libft/srcs/ft_putstrn.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstrn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/07/25 16:20:52 by tmaze #+# #+# */ +/* Updated: 2018/07/25 16:24:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstrn(char const *s, size_t nb) +{ + size_t strlen; + + if (s != NULL) + { + strlen = ft_strlen(s); + write(1, s, (nb < strlen) ? nb : strlen); + } +} diff --git a/libft/srcs/ft_putstrpad.c b/libft/srcs/ft_putstrpad.c new file mode 100644 index 0000000..1f82896 --- /dev/null +++ b/libft/srcs/ft_putstrpad.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstrpad.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/07/31 13:36:56 by tmaze #+# #+# */ +/* Updated: 2018/07/31 14:05:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstrpad(char *str, size_t size, char pad, char align) +{ + if (align == 'd') + while (size-- != 0) + ft_putchar(pad); + ft_putstr(str); + if (align == 'g') + while (size-- != 0) + ft_putchar(pad); +} diff --git a/libft/srcs/ft_realpath.c b/libft/srcs/ft_realpath.c new file mode 100644 index 0000000..8411ca3 --- /dev/null +++ b/libft/srcs/ft_realpath.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realpath.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/27 14:42:35 by tmaze #+# #+# */ +/* Updated: 2019/01/29 18:16:22 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_tabtopath(char **tab_path) +{ + size_t total_size; + size_t i; + char *ret; + + i = 0; + total_size = 0; + while (tab_path[i]) + total_size += ft_strlen(tab_path[i++]); + total_size = (total_size == 0) ? 1 : total_size; + if ((ret = ft_strnew(total_size + i)) == NULL) + return (NULL); + i = 0; + ft_strcat(ret, "/"); + while (tab_path[i]) + { + if (i > 0) + ft_strcat(ret, "/"); + ft_strcat(ret, tab_path[i++]); + } + return (ret); +} + +void ft_reducepath(char ***tab) +{ + size_t i; + size_t j; + int strcmp; + + i = 0; + while ((*tab)[i]) + { + if ((strcmp = ft_strcmp((*tab)[i], "..")) == 0 + || ft_strcmp((*tab)[i], ".") == 0) + { + ft_strdel(&((*tab)[i])); + if ((j = i) == i && strcmp == 0 && i != 0) + ft_strdel(&((*tab)[i - 1])); + while ((*tab)[++j]) + (*tab)[j - ((strcmp == 0 && i != 0) ? 2 : 1)] = (*tab)[j]; + if (strcmp == 0 && i != 0) + (*tab)[j - 2] = NULL; + (*tab)[j - 1] = NULL; + if (strcmp == 0 && i != 0) + i--; + } + else + i++; + } +} + +char *ft_realpath(char *r_path, char **a_path) +{ + char **tab_path; + + if ((tab_path = ft_strsplit(r_path, '/')) == NULL) + return (NULL); + ft_reducepath(&tab_path); + *a_path = ft_tabtopath(tab_path); + ft_del_words_tables(&tab_path); + return (*a_path); +} diff --git a/libft/srcs/ft_round.c b/libft/srcs/ft_round.c new file mode 100644 index 0000000..4d9fcf1 --- /dev/null +++ b/libft/srcs/ft_round.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_round.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/10/24 13:37:41 by tmaze #+# #+# */ +/* Updated: 2018/10/24 13:37:52 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_round(float x, float ind) +{ + if ((x - (int)x) > ind) + return ((int)x + 1); + return ((int)x); +} diff --git a/libft/srcs/ft_sort_params.c b/libft/srcs/ft_sort_params.c new file mode 100644 index 0000000..7231eed --- /dev/null +++ b/libft/srcs/ft_sort_params.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_params.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 18:28:43 by tmaze #+# #+# */ +/* Updated: 2018/04/09 19:04:57 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sort_params(int ac, char **av) +{ + int i; + int j; + char *tmp; + + i = 1; + while (++i < ac && (j = i)) + { + tmp = av[i]; + while (j > 1 && ft_strcmp(av[j - 1], tmp) > 0) + { + av[j] = av[j - 1]; + j--; + } + av[j] = tmp; + } +} diff --git a/libft/srcs/ft_str_is_alpha.c b/libft/srcs/ft_str_is_alpha.c new file mode 100644 index 0000000..324a1fd --- /dev/null +++ b/libft/srcs/ft_str_is_alpha.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_is_alpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:29:18 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:33:32 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_str_is_alpha(char *str) +{ + int i; + + i = 0; + while (str[i]) + if (!ft_isalpha(str[i++])) + return (0); + return (1); +} diff --git a/libft/srcs/ft_str_is_lowercase.c b/libft/srcs/ft_str_is_lowercase.c new file mode 100644 index 0000000..1ca9116 --- /dev/null +++ b/libft/srcs/ft_str_is_lowercase.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_is_lowercase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:29:18 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:47:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_str_is_lowercase(char *str) +{ + int i; + + i = 0; + while (str[i]) + if (!ft_islower(str[i++])) + return (0); + return (1); +} diff --git a/libft/srcs/ft_str_is_numeric.c b/libft/srcs/ft_str_is_numeric.c new file mode 100644 index 0000000..28e4f62 --- /dev/null +++ b/libft/srcs/ft_str_is_numeric.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_is_numeric.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:29:18 by tmaze #+# #+# */ +/* Updated: 2018/04/09 10:59:32 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_str_is_numeric(char *str) +{ + int i; + + i = 0; + while (str[i]) + if (!ft_isdigit(str[i++])) + return (0); + return (1); +} diff --git a/libft/srcs/ft_str_is_printable.c b/libft/srcs/ft_str_is_printable.c new file mode 100644 index 0000000..f19b0a5 --- /dev/null +++ b/libft/srcs/ft_str_is_printable.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_is_printable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:29:18 by tmaze #+# #+# */ +/* Updated: 2018/04/09 11:11:56 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_str_is_printable(char *str) +{ + int i; + + i = 0; + while (str[i]) + if (!ft_isprint(str[i++])) + return (0); + return (1); +} diff --git a/libft/srcs/ft_str_is_uppercase.c b/libft/srcs/ft_str_is_uppercase.c new file mode 100644 index 0000000..8180ef5 --- /dev/null +++ b/libft/srcs/ft_str_is_uppercase.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_is_uppercase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 10:29:18 by tmaze #+# #+# */ +/* Updated: 2018/04/09 11:01:45 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_str_is_uppercase(char *str) +{ + int i; + + i = 0; + while (str[i]) + if (!ft_isupper(str[i++])) + return (0); + return (1); +} diff --git a/libft/srcs/ft_strcapitalize.c b/libft/srcs/ft_strcapitalize.c new file mode 100644 index 0000000..765077e --- /dev/null +++ b/libft/srcs/ft_strcapitalize.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcapitalize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 11:16:16 by tmaze #+# #+# */ +/* Updated: 2018/04/09 11:31:18 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcapitalize(char *str) +{ + unsigned int i; + + i = 0; + while (str[i]) + { + if (i == 0 || !ft_isalnum(str[i - 1])) + str[i] = ft_toupper(str[i]); + else if (ft_isalnum(str[i - 1])) + str[i] = ft_tolower(str[i]); + i++; + } + return (str); +} diff --git a/libft/srcs/ft_strcat.c b/libft/srcs/ft_strcat.c new file mode 100644 index 0000000..a4f85c9 --- /dev/null +++ b/libft/srcs/ft_strcat.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 17:56:56 by tmaze #+# #+# */ +/* Updated: 2018/04/04 18:21:46 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *dest, const char *src) +{ + int dest_len; + + dest_len = ft_strlen(dest); + ft_strcpy(&dest[dest_len], src); + return (dest); +} diff --git a/libft/srcs/ft_strchr.c b/libft/srcs/ft_strchr.c new file mode 100644 index 0000000..0eae992 --- /dev/null +++ b/libft/srcs/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 18:54:17 by tmaze #+# #+# */ +/* Updated: 2018/05/16 15:09:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + int i; + char *tmp; + + if (s != NULL) + { + i = -1; + tmp = (char*)s; + while (++i == 0 || tmp[i - 1]) + if (tmp[i] == c) + return (&tmp[i]); + } + return (NULL); +} diff --git a/libft/srcs/ft_strclr.c b/libft/srcs/ft_strclr.c new file mode 100644 index 0000000..61297fa --- /dev/null +++ b/libft/srcs/ft_strclr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 23:27:33 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:04:24 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + int i; + + i = 0; + if (s != NULL) + while (s[i]) + s[i++] = '\0'; +} diff --git a/libft/srcs/ft_strcmp.c b/libft/srcs/ft_strcmp.c new file mode 100644 index 0000000..76cbb5a --- /dev/null +++ b/libft/srcs/ft_strcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/03 15:38:27 by tmaze #+# #+# */ +/* Updated: 2018/04/08 15:30:24 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + int i; + int diff; + + i = 1; + diff = (unsigned char)s1[0] - (unsigned char)s2[0]; + while (diff == 0 && s1[i - 1] && s2[i - 1]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + i++; + } + return (diff); +} diff --git a/libft/srcs/ft_strcpy.c b/libft/srcs/ft_strcpy.c new file mode 100644 index 0000000..6ffa032 --- /dev/null +++ b/libft/srcs/ft_strcpy.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 16:15:42 by tmaze #+# #+# */ +/* Updated: 2018/04/04 19:19:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dest, const char *src) +{ + int i; + + i = -1; + while (++i == 0 || src[i - 1]) + dest[i] = src[i]; + return (dest); +} diff --git a/libft/srcs/ft_strdel.c b/libft/srcs/ft_strdel.c new file mode 100644 index 0000000..5cac214 --- /dev/null +++ b/libft/srcs/ft_strdel.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 18:59:40 by tmaze #+# #+# */ +/* Updated: 2018/04/06 19:14:01 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strdel(char **as) +{ + ft_memdel((void**)as); +} diff --git a/libft/srcs/ft_strdup.c b/libft/srcs/ft_strdup.c new file mode 100644 index 0000000..11a2c48 --- /dev/null +++ b/libft/srcs/ft_strdup.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/03 16:33:35 by tmaze #+# #+# */ +/* Updated: 2018/04/09 09:52:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *ret; + + if ((ret = ft_strnew(ft_strlen(s1))) != NULL) + ft_strcpy(ret, s1); + return (ret); +} diff --git a/libft/srcs/ft_strequ.c b/libft/srcs/ft_strequ.c new file mode 100644 index 0000000..bb42a96 --- /dev/null +++ b/libft/srcs/ft_strequ.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 15:10:56 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:09:20 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + return ((s1 != NULL && s2 != NULL) ? ft_strcmp(s1, s2) == 0 : 0); +} diff --git a/libft/srcs/ft_striter.c b/libft/srcs/ft_striter.c new file mode 100644 index 0000000..74e24a2 --- /dev/null +++ b/libft/srcs/ft_striter.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 11:17:26 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:05:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striter(char *s, void (*f)(char*)) +{ + unsigned int i; + + i = 0; + if (s != NULL && f != NULL) + while (s[i]) + (*f)(&s[i++]); +} diff --git a/libft/srcs/ft_striteri.c b/libft/srcs/ft_striteri.c new file mode 100644 index 0000000..b3b6db3 --- /dev/null +++ b/libft/srcs/ft_striteri.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 11:36:12 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:06:17 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + i = -1; + if (s != NULL && f != NULL) + while (s[++i]) + (*f)(i, &s[i]); +} diff --git a/libft/srcs/ft_strjoin.c b/libft/srcs/ft_strjoin.c new file mode 100644 index 0000000..c497930 --- /dev/null +++ b/libft/srcs/ft_strjoin.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 16:30:47 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:39:59 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *ret; + + ret = NULL; + if (s1 != NULL && s2 != NULL + && (ret = ft_strnew(ft_strlen(s1) + ft_strlen(s2))) != NULL) + { + ft_strcpy(ret, s1); + ft_strcat(ret, s2); + } + return (ret); +} diff --git a/libft/srcs/ft_strlcat.c b/libft/srcs/ft_strlcat.c new file mode 100644 index 0000000..d83c5fc --- /dev/null +++ b/libft/srcs/ft_strlcat.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/08 14:32:30 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:45:42 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + int dest_len; + + dest_len = ft_strlen(dest); + if ((size_t)dest_len < size) + { + ft_strncpy(&dest[dest_len], src, size - dest_len); + dest[size - 1] = '\0'; + } + return (((size_t)dest_len < size) ? (size_t)(dest_len + ft_strlen(src)) + : size + (size_t)ft_strlen(src)); +} diff --git a/libft/srcs/ft_strlcpy.c b/libft/srcs/ft_strlcpy.c new file mode 100644 index 0000000..e6edb7a --- /dev/null +++ b/libft/srcs/ft_strlcpy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 11:55:11 by tmaze #+# #+# */ +/* Updated: 2018/04/27 18:04:14 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t src_len; + + if (dst != NULL && src != NULL) + { + src_len = (size_t)ft_strlen(src); + dst = ft_strncpy(dst, src, (src_len > size) ? size : src_len); + dst[(src_len > size) ? size - 1 : src_len] = '\0'; + } + return ((src != NULL) ? ft_strlen(src) : 0); +} diff --git a/libft/srcs/ft_strlen.c b/libft/srcs/ft_strlen.c new file mode 100644 index 0000000..3b397c3 --- /dev/null +++ b/libft/srcs/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/03 15:33:37 by tmaze #+# #+# */ +/* Updated: 2018/05/16 15:33:46 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + int i; + + i = 0; + while (s != NULL && s[i]) + i++; + return (i); +} diff --git a/libft/srcs/ft_strlowcase.c b/libft/srcs/ft_strlowcase.c new file mode 100644 index 0000000..080d2bf --- /dev/null +++ b/libft/srcs/ft_strlowcase.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlowcase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 17:58:10 by tmaze #+# #+# */ +/* Updated: 2018/04/09 18:06:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_lowcase(char *c) +{ + *c = ft_tolower(*c); +} + +char *ft_strlowcase(char *s) +{ + ft_striter(s, &ft_lowcase); + return (s); +} diff --git a/libft/srcs/ft_strmap.c b/libft/srcs/ft_strmap.c new file mode 100644 index 0000000..8b17088 --- /dev/null +++ b/libft/srcs/ft_strmap.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 14:57:22 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:18:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *ret; + size_t i; + size_t s_len; + + i = -1; + s_len = 0; + ret = NULL; + if (s != NULL && f != NULL) + { + s_len = ft_strlen(s); + if ((ret = ft_strnew(s_len)) != NULL) + while (++i < s_len) + ret[i] = (*f)(s[i]); + } + return (ret); +} diff --git a/libft/srcs/ft_strmapi.c b/libft/srcs/ft_strmapi.c new file mode 100644 index 0000000..69056ca --- /dev/null +++ b/libft/srcs/ft_strmapi.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 15:00:23 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:19:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *ret; + size_t i; + size_t s_len; + + i = -1; + s_len = 0; + ret = NULL; + if (s != NULL && f != NULL) + { + s_len = ft_strlen(s); + if ((ret = ft_strnew(s_len)) != NULL) + while (++i < s_len) + ret[i] = (*f)(i, s[i]); + } + return (ret); +} diff --git a/libft/srcs/ft_strncat.c b/libft/srcs/ft_strncat.c new file mode 100644 index 0000000..fd23bc4 --- /dev/null +++ b/libft/srcs/ft_strncat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 18:29:06 by tmaze #+# #+# */ +/* Updated: 2018/04/10 19:13:54 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncat(char *dest, const char *src, size_t n) +{ + int dest_len; + size_t i; + + dest_len = ft_strlen(dest); + i = 0; + while (i < n && src[i]) + { + dest[dest_len + i] = src[i]; + i++; + } + dest[dest_len + i] = '\0'; + return (dest); +} diff --git a/libft/srcs/ft_strnchr.c b/libft/srcs/ft_strnchr.c new file mode 100644 index 0000000..6fc56c5 --- /dev/null +++ b/libft/srcs/ft_strnchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/16 11:41:02 by tmaze #+# #+# */ +/* Updated: 2018/10/08 15:21:15 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnchr(const char *s, int c, int n) +{ + int i; + char *tmp; + + i = -1; + tmp = (char*)s; + while ((++i == 0 || tmp[i - 1]) && n > 0 && i < n) + if (tmp[i] == c) + return (&tmp[i]); + return (NULL); +} diff --git a/libft/srcs/ft_strncmp.c b/libft/srcs/ft_strncmp.c new file mode 100644 index 0000000..9417e2a --- /dev/null +++ b/libft/srcs/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 13:04:21 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:50:07 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + int i; + int diff; + + i = 1; + diff = 0; + if (n > 0) + diff = (unsigned char)s1[0] - (unsigned char)s2[0]; + while ((unsigned int)i < n && diff == 0 && (unsigned char)s1[i - 1] + && (unsigned char)s2[i - 1]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + i++; + } + return (diff); +} diff --git a/libft/srcs/ft_strncpy.c b/libft/srcs/ft_strncpy.c new file mode 100644 index 0000000..41d050d --- /dev/null +++ b/libft/srcs/ft_strncpy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 16:55:29 by tmaze #+# #+# */ +/* Updated: 2019/01/10 17:37:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strncpy(char *dest, const char *src, size_t n) +{ + unsigned int i; + + i = -1; + while (++i < n && (i == 0 || src[i - 1])) + dest[i] = src[i]; + while (i < n) + dest[i++] = '\0'; + return (dest); +} diff --git a/libft/srcs/ft_strndup.c b/libft/srcs/ft_strndup.c new file mode 100644 index 0000000..f79591f --- /dev/null +++ b/libft/srcs/ft_strndup.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/23 15:44:36 by tmaze #+# #+# */ +/* Updated: 2018/04/23 15:45:12 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strndup(const char *s1, size_t n) +{ + char *ret; + + if ((ret = ft_strnew(n)) != NULL) + ft_strncpy(ret, s1, n); + return (ret); +} diff --git a/libft/srcs/ft_strnequ.c b/libft/srcs/ft_strnequ.c new file mode 100644 index 0000000..d00b8b6 --- /dev/null +++ b/libft/srcs/ft_strnequ.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 15:24:01 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:09:47 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + return ((s1 != NULL && s2 != NULL) ? ft_strncmp(s1, s2, n) == 0 : 0); +} diff --git a/libft/srcs/ft_strnew.c b/libft/srcs/ft_strnew.c new file mode 100644 index 0000000..fe6de11 --- /dev/null +++ b/libft/srcs/ft_strnew.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/06 18:33:28 by tmaze #+# #+# */ +/* Updated: 2018/04/06 18:43:32 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnew(size_t size) +{ + return ((char*)ft_memalloc(sizeof(char) * (size + 1))); +} diff --git a/libft/srcs/ft_strnstr.c b/libft/srcs/ft_strnstr.c new file mode 100644 index 0000000..4f2ab5f --- /dev/null +++ b/libft/srcs/ft_strnstr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 11:35:03 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:52:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + int i; + int j; + char *tmp; + + i = -1; + tmp = (char*)haystack; + if (ft_strequ(needle, "")) + return (tmp); + while ((++i == 0 || tmp[i - 1]) && i < (int)len) + if (tmp[i] == needle[0] && (j = 1)) + { + while (tmp[i + j] == needle[j] && needle[j] && tmp[i + j] + && (i + j) < (int)len) + j++; + if (needle[j] == '\0') + return (&tmp[i]); + } + return (NULL); +} diff --git a/libft/srcs/ft_strrchr.c b/libft/srcs/ft_strrchr.c new file mode 100644 index 0000000..8d357ae --- /dev/null +++ b/libft/srcs/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 10:40:13 by tmaze #+# #+# */ +/* Updated: 2018/04/05 11:05:31 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + char *tmp; + char *ret; + + i = -1; + ret = NULL; + tmp = (char*)s; + while (++i == 0 || tmp[i - 1]) + if (tmp[i] == c) + ret = &tmp[i]; + return (ret); +} diff --git a/libft/srcs/ft_strrnchr.c b/libft/srcs/ft_strrnchr.c new file mode 100644 index 0000000..78bb4b6 --- /dev/null +++ b/libft/srcs/ft_strrnchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrnchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/05/16 12:05:03 by tmaze #+# #+# */ +/* Updated: 2018/05/16 12:32:26 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrnchr(const char *s, int c, int n) +{ + int s_len; + char *tmp; + + s_len = ft_strlen(s); + tmp = (char*)s; + n = (s_len < n) ? s_len : n; + s_len = s_len - n; + while (n >= 0) + if (tmp[s_len + n--] == c) + return (&tmp[s_len + n + 1]); + return (NULL); +} diff --git a/libft/srcs/ft_strsplit.c b/libft/srcs/ft_strsplit.c new file mode 100644 index 0000000..c4a9ce2 --- /dev/null +++ b/libft/srcs/ft_strsplit.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 17:54:29 by tmaze #+# #+# */ +/* Updated: 2018/04/15 15:55:03 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int count_words(char const *s, char c) +{ + int i; + int nb_words; + int in_word; + + in_word = 0; + nb_words = 0; + i = -1; + while (++i == 0 || s[i - 1]) + if (!in_word && s[i] != c && s[i] != '\0') + { + in_word = 1; + nb_words++; + } + else if (in_word && (s[i] == c || s[i] == '\0')) + in_word = 0; + return (nb_words); +} + +static char **get_table(char const *s, char c, char **tab) +{ + int i; + int nb_words; + int in_word; + size_t start; + + nb_words = 0; + in_word = 0; + i = -1; + while (++i == 0 || s[i - 1]) + if (!in_word && s[i] != c && (in_word = 1)) + start = i; + else if (in_word && (s[i] == c || s[i] == '\0')) + { + if ((tab[nb_words] = ft_strsub(s, start, i - start)) == NULL) + { + while (--nb_words >= 0) + ft_strdel(&tab[nb_words]); + free(tab); + return (NULL); + } + nb_words++; + in_word = 0; + } + return (tab); +} + +char **ft_strsplit(char const *s, char c) +{ + int nb_words; + char **tab; + + nb_words = 0; + tab = NULL; + if (s != NULL) + { + nb_words = count_words(s, c); + if ((tab = (char**)malloc(sizeof(char**) * (nb_words + 1))) == NULL) + return (NULL); + tab[nb_words] = NULL; + return (get_table(s, c, tab)); + } + else + return (NULL); +} diff --git a/libft/srcs/ft_strsplitwhitespace.c b/libft/srcs/ft_strsplitwhitespace.c new file mode 100644 index 0000000..6c6b4e3 --- /dev/null +++ b/libft/srcs/ft_strsplitwhitespace.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplitwhitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/25 16:25:18 by tmaze #+# #+# */ +/* Updated: 2019/03/09 15:13:49 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int count_words_ws(char const *s) +{ + int i; + int nb_words; + int in_word; + + in_word = 0; + nb_words = 0; + i = -1; + while (++i == 0 || s[i - 1]) + if (!in_word && !ft_iswhitespace(s[i]) && s[i] != '\0') + { + in_word = 1; + nb_words++; + } + else if (in_word && (ft_iswhitespace(s[i]) || s[i] == '\0')) + in_word = 0; + return (nb_words); +} + +static char **get_table_ws(char const *s, char **tab) +{ + int i; + int nb_words; + int in_word; + size_t start; + + nb_words = 0; + in_word = 0; + i = -1; + while (++i == 0 || s[i - 1]) + if (!in_word && !ft_iswhitespace(s[i]) && (in_word = 1)) + start = i; + else if (in_word && (ft_iswhitespace(s[i]) || s[i] == '\0')) + { + if ((tab[nb_words] = ft_strsub(s, start, i - start)) == NULL) + { + while (--nb_words >= 0) + ft_strdel(&tab[nb_words]); + free(tab); + return (NULL); + } + nb_words++; + in_word = 0; + } + return (tab); +} + +char **ft_strsplitwhitespace(char *s) +{ + int nb_words; + char **tab; + + nb_words = 0; + tab = NULL; + if (s != NULL) + { + nb_words = count_words_ws(s); + if ((tab = (char**)malloc(sizeof(char*) * (nb_words + 1))) == NULL) + return (NULL); + tab[nb_words] = NULL; + return (get_table_ws(s, tab)); + } + else + return (NULL); +} diff --git a/libft/srcs/ft_strstr.c b/libft/srcs/ft_strstr.c new file mode 100644 index 0000000..a8ffd60 --- /dev/null +++ b/libft/srcs/ft_strstr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 10:54:41 by tmaze #+# #+# */ +/* Updated: 2018/04/08 15:25:55 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strstr(const char *haystack, const char *needle) +{ + int i; + int j; + char *tmp; + + i = -1; + tmp = (char*)haystack; + if (ft_strequ(needle, "")) + return (tmp); + while (++i == 0 || tmp[i - 1]) + if (tmp[i] == needle[0] && (j = 1)) + { + while (tmp[i + j] == needle[j] && needle[j] && tmp[i + j]) + j++; + if (needle[j] == '\0') + return (&tmp[i]); + } + return (NULL); +} diff --git a/libft/srcs/ft_strsub.c b/libft/srcs/ft_strsub.c new file mode 100644 index 0000000..3014509 --- /dev/null +++ b/libft/srcs/ft_strsub.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 15:35:49 by tmaze #+# #+# */ +/* Updated: 2018/04/10 14:16:44 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *ret; + + ret = NULL; + if (s != NULL && (ret = ft_strnew(len)) != NULL) + ret = ft_strncpy(ret, &s[start], len); + return (ret); +} diff --git a/libft/srcs/ft_strtrim.c b/libft/srcs/ft_strtrim.c new file mode 100644 index 0000000..b8cd8b0 --- /dev/null +++ b/libft/srcs/ft_strtrim.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */ +/* Updated: 2020/01/22 17:51:11 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + int in_word; + size_t i; + size_t start; + size_t len; + + i = 0; + if (s != NULL) + { + while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') + i++; + start = i; + in_word = 1; + while (i == 0 || s[i - 1]) + { + if (in_word && (!ft_isprint(s[i]) || s[i] == ' ')) + { + len = i - start; + in_word = 0; + } + else if (!in_word && ft_isprint(s[i]) && s[i] != ' ') + in_word = 1; + i++; + } + } + return ((s != NULL) ? ft_strsub(s, start, len) : NULL); +} diff --git a/libft/srcs/ft_strupcase.c b/libft/srcs/ft_strupcase.c new file mode 100644 index 0000000..71bcd4e --- /dev/null +++ b/libft/srcs/ft_strupcase.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strupcase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/09 18:08:19 by tmaze #+# #+# */ +/* Updated: 2018/04/09 18:09:55 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_upcase(char *c) +{ + *c = ft_toupper(*c); +} + +char *ft_strupcase(char *s) +{ + ft_striter(s, &ft_upcase); + return (s); +} diff --git a/libft/srcs/ft_tolower.c b/libft/srcs/ft_tolower.c new file mode 100644 index 0000000..312d28b --- /dev/null +++ b/libft/srcs/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 14:24:13 by tmaze #+# #+# */ +/* Updated: 2018/04/04 14:24:34 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + return ((c >= 'A' && c <= 'Z') ? c + 32 : c); +} diff --git a/libft/srcs/ft_toupper.c b/libft/srcs/ft_toupper.c new file mode 100644 index 0000000..f0d4494 --- /dev/null +++ b/libft/srcs/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/04 14:06:21 by tmaze #+# #+# */ +/* Updated: 2018/04/04 14:20:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + return ((c >= 'a' && c <= 'z') ? c - 32 : c); +} diff --git a/libft/srcs/get_next_line.c b/libft/srcs/get_next_line.c new file mode 100644 index 0000000..7a6c6b5 --- /dev/null +++ b/libft/srcs/get_next_line.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/24 18:08:15 by tmaze #+# #+# */ +/* Updated: 2019/03/16 15:53:27 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char **get_buff(const int fd, t_list **lst) +{ + t_list *tmp; + + tmp = *lst; + while (tmp) + { + if (tmp->content_size == (size_t)fd) + return ((char**)&tmp->content); + tmp = tmp->next; + } + if ((tmp = ft_lstnew("\0", fd)) == NULL) + return (NULL); + ft_lstadd(lst, tmp); + return (get_buff(fd, lst)); +} + +static char *supercat(char **s1, char **s2) +{ + char *tmp; + + if ((tmp = ft_strjoin(*s1, *s2)) == NULL) + return (NULL); + ft_strdel(s1); + ft_strclr(*s2); + *s1 = tmp; + return (*s1); +} + +static int check_buff(char **buff, char **line) +{ + int ret; + + ret = 1; + if (buff != NULL && (*buff == NULL || ft_strlen(*buff) == 0)) + ret = 0; + else if ((*line = ft_strdup(*buff)) == NULL) + ret = -1; + ft_strclr(*buff); + return (ret); +} + +static int read_gnl(const int fd, char **buff, char **line) +{ + char *tmp; + int ret; + + if ((tmp = ft_strnew(BUFF_SIZE)) == NULL) + return (-1); + while (ft_strrnchr(*buff, '\n', BUFF_SIZE) == NULL + && (ret = read(fd, tmp, BUFF_SIZE)) > 0) + if (supercat(buff, &tmp) == NULL) + { + ft_strdel(&tmp); + return (-1); + } + ft_strdel(&tmp); + if (buff != NULL && *buff != NULL + && ft_strrnchr(*buff, '\n', BUFF_SIZE) != NULL) + return (get_next_line(fd, line)); + else + return (check_buff(buff, line)); +} + +int get_next_line(const int fd, char **line) +{ + static t_list *lst = NULL; + char **buff; + char *tmp; + + tmp = NULL; + if (fd < 0 || read(fd, tmp, 0) == -1 || line == NULL) + return (-1); + if ((buff = get_buff(fd, &lst)) == NULL) + return (-1); + tmp = ft_strchr(*buff, '\n'); + if (tmp != NULL) + { + *line = ft_strndup(*buff, tmp - *buff); + ft_memmove(*buff, &tmp[1], ft_strlen(&tmp[1]) + 1); + return (1); + } + return (read_gnl(fd, buff, line)); +} diff --git a/srcs/cmd_cd.c b/srcs/cmd_cd.c index 01269e7..326927b 100644 --- a/srcs/cmd_cd.c +++ b/srcs/cmd_cd.c @@ -6,101 +6,68 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */ -/* Updated: 2019/01/30 17:02:05 by tmaze ### ########.fr */ +/* Updated: 2020/01/26 22:17:48 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void put_error_cd(char *file, char *msg) -{ - ft_putstr("cd: "); - ft_putstr(file); - ft_putstr(": "); - ft_putendl(msg); -} - char *check_path_slash_cd(char *exec) { int i; struct stat info; + struct stat info2; if (exec[0] == '/') { if ((i = access(exec, F_OK)) != 0) - put_error_cd(exec, "no such file or directory"); - if (i != 0) - return (NULL); - if ((i = stat(exec, &info)) != 0) - put_error_cd(exec, "can't determine info"); - if (i != 0) - return (NULL); - if (!S_ISDIR(info.st_mode)) - { - put_error_cd(exec, "not a directory"); - return (NULL); - } + return (put_error_cd(exec, "no such file or directory")); + if ((i = lstat(exec, &info)) != 0) + return (put_error_cd(exec, "can't determine info")); + if ((S_ISLNK(info.st_mode) + && ((i = stat(exec, &info2)) != 0 || !S_ISDIR(info2.st_mode))) + && !S_ISDIR(info.st_mode)) + return (put_error_cd(exec, "not a directory")); if ((i = access(exec, X_OK)) != 0) - put_error_cd(exec, "permission denied"); - if (i != 0) - return (NULL); + return (put_error_cd(exec, "permission denied")); return (exec); } return (NULL); } -int cmd_cd_update_env(char *path, t_list **env, char opt) +int cmd_cd_update_env(char *path, t_env **env, char opt) { - t_envelem *pwd; - char *oldpwd; - char *c_path; - char p_path[4096]; + t_env *pwd; + char *oldpwd; + char *c_path; + char p_path[4096]; ft_bzero(p_path, 4096); - if (getcwd(p_path, 4096) == NULL) + if (!(pwd = ft_envgetelem("PWD", *env))) + return (put_error_cd2(path, "env var PWD undefined")); + if (getcwd(p_path, 4096) == NULL || (oldpwd = ft_strdup(pwd->val)) == NULL + || ft_realpath(path, &c_path) == NULL) + return (put_error_cd2(path, "error")); + if (ft_envupdate("PWD", *env, ((opt == 'P') ? p_path : c_path)) == NULL + || ft_envupdate("OLDPWD", *env, oldpwd) == NULL) { - put_error_cd(path, "error"); - return (1); - } - if ((pwd = env_getelemfromkey("PWD", *env)) == NULL) - { - put_error_cd(path, "error"); - return (1); - } - if ((oldpwd = ft_strdup(pwd->val)) == NULL) - { - put_error_cd(path, "error"); - return (1); - } - if (ft_realpath(path, &c_path) == NULL) - { - put_error_cd(path, "error"); - return (1); - } - if (env_addupdate("PWD", ((opt == 'P') ? p_path : c_path), env) == NULL - || env_addupdate("OLDPWD", oldpwd, env) == NULL) - { - put_error_cd(path, "error"); ft_strdel(&oldpwd); ft_strdel(&c_path); - return (1); + return (put_error_cd2(path, "error")); } ft_strdel(&oldpwd); ft_strdel(&c_path); return (0); } -int cmd_cd_core(char *path, t_list **env, char opt) +int cmd_cd_core(char *path, t_env **env, char opt) { - t_envelem *pwd; + t_env *pwd; if (check_path_slash_cd(path) == NULL) - { - put_error_cd(path, "invalid path"); return (1); - } if (chdir(path) == -1 - || (pwd = env_getelemfromkey("PWD", *env)) == NULL) + || (pwd = ft_envgetelem("PWD", *env)) == NULL) { put_error_cd(path, "error"); return (1); @@ -133,35 +100,42 @@ char cd_getparams(char **argv, size_t *i) return (ret[0]); } -int cmd_cd(char **argv, t_list **env) +int cmd_cd_switchboard(char **av, t_env **env, char opt, int i) { - t_envelem *elem; - size_t i; - char *path; - char opt; - int ret; + t_env *e; + char *p; + int ret; - i = 0; - if ((opt = cd_getparams(argv, &i)) == '\0') - return (1); - if (!argv[i] && (elem = env_getelemfromkey("HOME", *env)) != NULL && elem->val != NULL) - return (cmd_cd_core(elem->val, env, opt)); - else if (argv[i] && argv[i][0] == '/') - return (cmd_cd_core(argv[i], env, opt)); - else if (argv[i] && argv[i][0] == '-' && (elem = env_getelemfromkey("OLDPWD", *env)) != NULL) - return (cmd_cd_core(elem->val, env, opt)); - else if (argv[i] && argv[i][0] != '/' && (elem = env_getelemfromkey("PWD", *env)) != NULL && elem->val != NULL) + if ((!av[i] && (e = ft_envgetelem("HOME", *env)) != NULL && e->val + != NULL) || (av[i] && av[i][0] == '-' + && (e = ft_envgetelem("OLDPWD", *env)) != NULL)) + return (cmd_cd_core(e->val, env, opt)); + else if (av[i] && av[i][0] == '/') + return (cmd_cd_core(av[i], env, opt)); + else if (av[i] && av[i][0] != '/' + && (e = ft_envgetelem("PWD", *env)) != NULL && e->val != NULL) { - if ((path = ft_strnew(ft_strlen(elem->val) + ft_strlen(argv[i]) + 1)) == NULL) - put_error_cd(argv[i], "memory error"); - if (path == NULL) + if ((p = ft_strnew(ft_strlen(e->val) + ft_strlen(av[i]) + 1)) == NULL) + put_error_cd(av[i], "memory error"); + if (p == NULL) return (1); - ft_strcpy(path, elem->val); - path[ft_strlen(path)] = '/'; - ft_strcat(path, argv[i]); - ret = cmd_cd_core(path, env, opt); - ft_strdel(&path); + ft_strcpy(p, e->val); + p[ft_strlen(p)] = '/'; + ft_strcat(p, av[i]); + ret = cmd_cd_core(p, env, opt); + ft_strdel(&p); return (ret); } return (1); } + +int cmd_cd(char **argv, t_env **env) +{ + size_t i; + char opt; + + i = 0; + if ((opt = cd_getparams(argv, &i)) == '\0') + return (1); + return (cmd_cd_switchboard(argv, env, opt, i)); +} diff --git a/srcs/cmd_echo.c b/srcs/cmd_echo.c index 0fb155d..fb8477c 100644 --- a/srcs/cmd_echo.c +++ b/srcs/cmd_echo.c @@ -5,24 +5,21 @@ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2018/11/27 16:14:07 by tmaze #+# #+# */ -/* Updated: 2019/01/13 16:58:14 by tmaze ### ########.fr */ +/* Created: 2019/09/20 14:42:30 by tmaze #+# #+# */ +/* Updated: 2019/11/02 23:04:23 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int cmd_echo(char **argv, t_list **env) +int cmd_echo(char **argv, t_env **env) { - size_t i; + int i; (void)env; - i = 0; - while (argv[0] && argv[++i]) - { - ft_putstr(argv[i]); - ft_putchar(' '); - } + i = 1; + while (argv[i]) + ft_printf("%s ", argv[i++]); ft_putchar('\n'); return (0); } diff --git a/srcs/cmd_env.c b/srcs/cmd_env.c index 2917c76..22fa936 100644 --- a/srcs/cmd_env.c +++ b/srcs/cmd_env.c @@ -5,144 +5,91 @@ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2018/12/14 15:54:45 by tmaze #+# #+# */ -/* Updated: 2019/02/01 16:26:42 by tmaze ### ########.fr */ +/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */ +/* Updated: 2020/01/30 15:41:08 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void put_error(char *exec, char *file, char *msg) +static void put_env(t_env *env) { - ft_putstr(exec); - ft_putstr(": "); - ft_putstr(file); - ft_putstr(": "); - ft_putendl(msg); -} + t_env *it; -void put_env(t_list *env) -{ - t_list *tmp; - - tmp = env; - while (tmp) + it = env; + while (it) { - ft_putstr(((t_envelem*)(tmp->content))->key); - ft_putchar('='); - ft_putendl(((t_envelem*)(tmp->content))->val); - tmp = tmp->next; + ft_printf("%s=", it->key); + if (it->val) + ft_printf("%s", it->val); + ft_putchar('\n'); + it = it->next; } } -t_envelem *envelemdup(t_envelem *elem) +t_env *envcpy(t_env *env) { - t_envelem *ret; + t_env *cpy; - if ((ret = (t_envelem*)ft_memalloc(sizeof(t_envelem))) == NULL) - return (NULL); - if ((ret->key = ft_strdup(elem->key)) == NULL) - ft_envelemdel(&ret); - if (ret == NULL) - return (0); - if ((ret->val = ft_strdup(elem->val)) == NULL) - { - ft_strdel(&(ret->key)); - ft_envelemdel(&ret); - return (NULL); - } - return (ret); + cpy = NULL; + if (env != NULL && (cpy = (t_env*)ft_memalloc(sizeof(t_env))) != NULL) + if ((cpy->key = ft_strdup(env->key)) == NULL + || (cpy->val = ft_strdup(env->val)) == NULL) + ft_envdelelem(&cpy); + return (cpy); } -t_list *env_cpy(t_list *env) +int env_cleanup(t_env *env_cp, int code) { - t_list *ret; - t_list *ind; - t_list *tmp; - t_envelem *envelem; + ft_envdel(&env_cp); + if (code == 2) + ft_putstr_fd("minishell: env: Env copy error\n", 2); + return (code); +} - ret = NULL; - ind = env; - while (ind) +t_env *inline_env(char **argv, int *i, t_env **env_cp) +{ + t_env *new; + + while (ft_strchr(argv[*i], '=')) { - if ((envelem = envelemdup((t_envelem*)(ind->content))) == NULL) - ft_lstdel(&ret, &ft_lstdelenvelem); - if (envelem == NULL) - break ; - if ((tmp = ft_lstnew((void*)envelem, sizeof(t_envelem))) == NULL - || ft_lstaddend(&ret, tmp) == NULL) + if ((new = ft_envnew(argv[*i])) == NULL) { - ft_envelemdel(&envelem); - ft_lstdel(&ret, &ft_lstdelenvelem); + ft_printf("env: memory error\n"); + ft_envdel(env_cp); break ; } - ft_envelemdel(&envelem); - ind = ind->next; + ft_envaddend(env_cp, new); + (*i)++; } - return (ret); + return (*env_cp); } -int exec_env(char **argv, t_list **env) +int cmd_env(char **argv, t_env **env) { - int ret; - char **env_tab; - char *path; + int i; + t_env *it; + t_env *new; + t_env *env_cp; - if ((env_tab = envlsttotab(*env)) == NULL) - return (1); - if ((path = check_path(argv[0], *env)) == NULL) - ft_del_words_tables(&env_tab); - if (path == NULL) - return (1); - if ((ret = fork()) == 0) + i = 1; + if (!(env_cp = NULL) && !ft_strequ(argv[i], "-i") && (env != NULL)) { - execve(path, argv, env_tab); - if (ft_strcmp(path, argv[0]) != 0) - ft_strdel(&path); - ft_del_words_tables(&env_tab); - exit(1); - } - else if (ret == -1) - return (1); - waitpid(ret, NULL, 0); - ft_del_words_tables(&env_tab); - if (ft_strcmp(path, argv[0]) != 0) - ft_strdel(&path); - return (0); -} - -int cmd_env(char **argv, t_list **env) -{ - t_envelem *elem; - t_list *env_cp; - t_list *new; - char *tmp; - size_t i; - - if (argv[1] == NULL) - put_env(*env); - i = 0; - if ((env_cp = env_cpy(*env)) == NULL) - return (2); - while (argv[++i] && (tmp = ft_strchr(argv[i], '='))) - { - if ((elem = ft_envtoenvelem(argv[i])) == NULL) - ft_lstdel(&env_cp, &ft_lstdelenvelem); - if (elem == NULL) - return (1); - if ((new = ft_lstnew((void*)elem, sizeof(t_envelem))) == NULL) + it = *env; + while (it) { - ft_envelemdel(&elem); - ft_lstdel(&env_cp, &ft_lstdelenvelem); - return (1); - } - ft_memdel((void**)&elem); - if (ft_lstaddend(&env_cp, new) == NULL) - { - ft_lstdel(&env_cp, &ft_lstdelenvelem); - return (1); + if ((new = envcpy(it)) == NULL) + return (env_cleanup(env_cp, 2)); + ft_envaddend(&env_cp, new); + it = it->next; } } - ft_lstdel(&env_cp, &ft_lstdelenvelem); - return (0); + else if (ft_strequ(argv[i], "-i")) + i++; + inline_env(argv, &i, &env_cp); + if (argv[i]) + exec_cmd(argv + i, &env_cp); + else + put_env(env_cp); + return (env_cleanup(env_cp, 0)); } diff --git a/srcs/cmd_setenv.c b/srcs/cmd_setenv.c index c75c703..65295d0 100644 --- a/srcs/cmd_setenv.c +++ b/srcs/cmd_setenv.c @@ -5,18 +5,35 @@ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/01/13 16:20:20 by tmaze #+# #+# */ -/* Updated: 2019/01/13 17:05:05 by tmaze ### ########.fr */ +/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */ +/* Updated: 2019/11/10 21:38:30 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int cmd_setenv(char **argv, t_list **env) +int cmd_setenv(char **argv, t_env **env) { - if (argv[1] == NULL) - return (-1); - if (env_addupdate(argv[1], argv[2], env) == NULL) - return (-1); - return (0); + t_env *new; + t_env *tmp; + + if (argv[1] && (new = ft_envnew(argv[1])) != NULL) + { + if (argv[1] && (tmp = ft_envgetelem(new->key, *env)) != NULL) + { + ft_strdel(&(tmp->val)); + tmp->val = new->val; + ft_strdel(&(new->key)); + ft_memdel((void**)&new); + } + else + ft_envaddend(env, new); + ft_envclean(env); + return (0); + } + else if (argv[1] && argv[1][0] != '=' && !ft_strchr(argv[1], '=')) + ft_printf("minishell: setenv: memory error\n"); + else + ft_printf("usage: setenv [KEY]=[value]\n"); + return (1); } diff --git a/srcs/cmd_unsetenv.c b/srcs/cmd_unsetenv.c index 1679125..ad757f7 100644 --- a/srcs/cmd_unsetenv.c +++ b/srcs/cmd_unsetenv.c @@ -5,17 +5,35 @@ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/01/15 14:58:05 by tmaze #+# #+# */ -/* Updated: 2019/01/15 15:04:05 by tmaze ### ########.fr */ +/* Created: 2019/09/20 14:42:01 by tmaze #+# #+# */ +/* Updated: 2019/11/10 15:52:09 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int cmd_unsetenv(char **argv, t_list **env) +int cmd_unsetenv(char **argv, t_env **env) { - if (argv[1] == NULL) - return (-1); - env_delelem(argv[1], env); - return (0); + t_env **it; + t_env *tmp; + + it = env; + if (!argv[1]) + { + ft_printf("usage: unsetenv [KEY]\n"); + return (1); + } + while (*it) + { + if (ft_strequ((*it)->key, argv[1])) + { + tmp = *it; + *it = tmp->next; + ft_envdelelem(&tmp); + return (0); + } + it = &((*it)->next); + } + ft_printf("Entry %s not found\n", argv[1]); + return (1); } diff --git a/srcs/exec.c b/srcs/exec.c deleted file mode 100644 index 0fae5a5..0000000 --- a/srcs/exec.c +++ /dev/null @@ -1,222 +0,0 @@ -'/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tmaze +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2018/11/27 15:32:29 by tmaze #+# #+# */ -/* Updated: 2019/02/01 16:49:31 by tmaze ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -#define S_BIN 5 - -char **envlsttotab(t_list *env) -{ - size_t i; - size_t env_size; - t_list *tmp; - char **ret; - - i = 0; - tmp = env; - env_size = ft_lstsize(env); - if ((ret = (char**)ft_memalloc(sizeof(char*) * (env_size + 1))) == NULL) - return (NULL); - while (i <= env_size) - ret[i++] = NULL; - i = 0; - while (tmp) - { - if ((ret[i] = ft_strnew(ft_strlen(((t_envelem*)(tmp->content))->key) - + ft_strlen(((t_envelem*)(tmp->content))->val) + 1)) == NULL) - { - ft_del_words_tables(&ret); - return (NULL); - } - i++; - tmp = tmp->next; - } - return (ret); -} - -/* -** , {"setenv", &cmd_senv}, {"unsetenv", &cmd_senv}, {"env", &cmd_env}}; -*/ - -int ft_isin(char *str, char c) -{ - int i; - - i = 0; - while (str[i]) - if (str[i++] == c) - return (i - 1); - return (-1); -} - -void put_error(char *file, char *msg) -{ - ft_putstr("minishell: "); - ft_putstr(file); - ft_putstr(": "); - ft_putendl(msg); -} - -char *check_path_slash(char *exec) -{ - int i; - struct stat info; - - if (ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1) - { - if ((i = access(exec, F_OK)) != 0) - put_error(exec, "no such file or directory"); - if (i != 0) - return (NULL); - if ((i = stat(exec, &info)) != 0) - put_error(exec, "can't determine info"); - if (i != 0) - return (NULL); - if (!S_ISREG(info.st_mode)) - { - put_error(exec, "not an executable file"); - return (NULL); - } - if ((i = access(exec, X_OK)) != 0) - put_error(exec, "permission denied"); - if (i != 0) - return (NULL); - return (exec); - } - return (NULL); -} - -char *check_path_dot(char *exec, t_list *env) -{ - t_envelem *path; - char *tmp; - char *ret; - - if (exec[0] == '.') - { - if ((path = env_getelemfromkey("PWD", env)) == NULL - || path->val == NULL) - { - put_error(exec, "memory error"); - return (NULL); - } - if ((tmp = ft_strjoin(path->val, "/")) == NULL) - put_error(exec, "memory error"); - if (tmp == NULL) - return (NULL); - if ((ret = ft_strjoin(tmp, exec)) == NULL) - { - put_error(exec, "memory error"); - ft_strdel(&tmp); - return (NULL); - } - ft_strdel(&tmp); - if (access(ret, F_OK) == 0) - { - if (access(ret, X_OK) == 0) - return (ret); - put_error(exec, "permission denied"); - ft_strdel(&ret); - return (NULL); - } - put_error(exec, "no such file or directory"); - ft_strdel(&ret); - } - return (NULL); -} - -char *check_path(char *exec, t_list *env) -{ - size_t i; - t_envelem *path; - char **path_elems; - char *tmp; - char *ret; - - i = 0; - ret = NULL; - if ((ret = check_path_slash(exec)) != NULL) - return (ret); - if ((ret = check_path_dot(exec, env)) != NULL) - return (ret); - if ((path = env_getelemfromkey("PATH", env)) == NULL - || path->val == NULL) - return (NULL); - if ((path_elems = ft_strsplit(path->val, ':')) == NULL) - return (NULL); - while (path_elems[i]) - { - if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL) - put_error(exec, "memory error"); - if (tmp == NULL) - break ; - if ((ret = ft_strjoin(tmp, exec)) == NULL) - { - put_error(exec, "memory error"); - ft_strdel(&tmp); - break ; - } - ft_strdel(&tmp); - if (access(ret, F_OK) == 0) - { - if (access(ret, X_OK) == 0) - break ; - put_error(exec, "permission denied"); - } - ft_strdel(&ret); - i++; - } - if (path_elems[i] == NULL) - put_error(exec, "command not found"); - ft_del_words_tables(&path_elems); - return (ret); -} - -int exec_cmd(char **argv, t_list **env) -{ - int ret; - char **env_tab; - char *path; - size_t i; - static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo}, - {"cd", &cmd_cd}, {"setenv", &cmd_setenv}, - {"env", &cmd_env}, {"unsetenv", &cmd_unsetenv}}; - - i = 0; - while (i < S_BIN) - if (ft_strcmp(argv[0], builtins[i++].cmd) == 0) - return ((*(builtins[i - 1].f))(argv, env)); - if ((env_tab = envlsttotab(*env)) == NULL) - return (-1); - if ((path = check_path(argv[0], *env)) == NULL) - ft_del_words_tables(&env_tab); - if (path == NULL) - return (-1); - if ((ret = fork()) == 0) - { - execve(path, argv, env_tab); - ft_putendl_fd("minishell: error", 1); - if (ft_strcmp(path, argv[0]) != 0) - ft_strdel(&path); - ft_del_words_tables(&env_tab); - exit(-1); - } - else if (ret == -1) - ft_putendl_fd("minishell: error", 1); - if (ret == -1) - return (-1); - waitpid(ret, NULL, 0); - ft_del_words_tables(&env_tab); - if (ft_strcmp(path, argv[0]) != 0) - ft_strdel(&path); - return (0); -} diff --git a/srcs/main.c b/srcs/main.c index 3756f71..4c03b2a 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -5,103 +5,130 @@ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2018/11/18 13:09:55 by tmaze #+# #+# */ -/* Updated: 2019/02/01 10:18:41 by tmaze ### ########.fr */ +/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */ +/* Updated: 2020/01/30 13:47:35 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int main(void) +t_env *def_env(void) { - int ret; - char *cmd; - char **tab_cmd; - t_list *lst_env; - extern char **environ; + t_env *ret; + t_env *n; + char p[PATH_MAX]; - if ((lst_env = ft_envtolst(environ)) == NULL) - ft_putendl("Error envtolst"); - if (lst_env == NULL) - return (2); - while (1) + n = ft_memalloc(sizeof(t_env)); + if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX))) + ft_envdelelem(&n); + if (n != NULL) + ft_strcpy(n->key, "PWD"); + if (n != NULL && (n->val = ft_strdup(p)) == NULL) + ft_envdelelem(&n); + if (n != NULL) + ft_envaddend(&ret, n); + n = ft_memalloc(sizeof(t_env)); + if (n != NULL && (n->key = ft_strnew(5)) == NULL) + ft_envdelelem(&n); + if (n != NULL) + ft_strcpy(n->key, "SHLVL"); + if (n != NULL && (n->val = ft_strdup("1")) == NULL) + ft_envdelelem(&n); + if (n != NULL) + ft_envaddend(&ret, n); + return (ret); +} + +char *inc_shlvl(char **shlvl) +{ + char *ret; + + if ((ret = ft_itoa(ft_atoi(*shlvl) + 1)) != NULL) { - ret = 0; - ft_putstr(FT_COLOR_GREEN); - ft_putstr("$> "); - ft_putstr(FT_RESET); - ret = get_next_line(1, &cmd); - if (cmd != NULL && ft_strlen(cmd) != 0) - { - if (ret == -1) - { - ft_lstdel(&lst_env, &ft_lstdelenvelem); - ft_strdel(&cmd); - return (2); - } - if (ft_strcmp("exit", cmd) == 0) - { - ft_lstdel(&lst_env, &ft_lstdelenvelem); - ft_strdel(&cmd); - return (0); - } - else if ((tab_cmd = ft_strsplit(cmd, ' ')) == NULL) - { - ft_lstdel(&lst_env, &ft_lstdelenvelem); - ft_strdel(&cmd); - ft_putendl("error split"); - return (2); - } - ft_strdel(&cmd); - ft_putnbr(exec_cmd(tab_cmd, &lst_env)); - ft_putchar('\n'); - ft_del_words_tables(&tab_cmd); - } - ft_strdel(&cmd); + ft_strdel(shlvl); + *shlvl = ret; } - ft_lstdel(&lst_env, &ft_lstdelenvelem); + return (ret); +} + +t_env *env2lst(char **env) +{ + t_env *ret; + t_env *new; + int i; + + i = 0; + ret = NULL; + while (env[i]) + { + if ((new = ft_envnew(env[i])) != NULL) + { + if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val)) + inc_shlvl(&new->val); + ft_envaddend(&ret, new); + } + else + { + ft_envdelelem(&new); + ft_envdel(&ret); + return (NULL); + } + i++; + } + if (i == 0 && ret == NULL && (ret = def_env()) == NULL) + ft_printf("Env: Memory Error\n"); + return (ret); +} + +void check_exec(char **argv, t_env *env) +{ + int i; + static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd} + , {"setenv", &cmd_setenv}, {"echo", &cmd_echo} + , {"unsetenv", &cmd_unsetenv}}; + + i = -1; + while (++i < S_BIN) + if (ft_strequ(argv[0], built[i].cmd)) + { + built[i].f(argv, &env); + break ; + } + if (i == S_BIN) + exec_cmd(argv, &env); +} + +int cleanup(char **argv, t_env *env) +{ + ft_del_words_tables(&argv); + ft_envdel(&env); return (0); } -/* -** char *cmd; -** char **tab_cmd; -** t_list *lst_env; -** int ret; -** extern char **environ; -** -** if ((lst_env = ft_envtolst(environ)) == NULL) -** ft_putendl("Error envtolst"); -** if (lst_env == NULL) -** return (2); -** while (1) -** { -** ret = 0; -** ft_putstr(FT_COLOR_GREEN); -** ft_putstr("$> "); -** ft_putstr(FT_RESET); -** ret = get_next_line(1, &cmd); -** if (ret == -1) -** { -** ft_lstdel(&lst_env, &ft_lstdelenvelem); -** ft_strdel(&cmd); -** return (2); -** } -** if (ft_strcmp("exit", cmd) == 0) -** { -** ft_lstdel(&lst_env, &ft_lstdelenvelem); -** ft_strdel(&cmd); -** return (0); -** } -** else if ((tab_cmd = ft_strsplit(cmd, ' ')) == NULL) -** { -** ft_lstdel(&lst_env, &ft_lstdelenvelem); -** ft_strdel(&cmd); -** ft_putendl("error split"); -** return (2); -** } -** exec_cmd(tab_cmd, lst_env); -** } -** ft_lstdel(&lst_env, &ft_lstdelenvelem); -** return (0); -*/ +int main(void) +{ + extern char **environ; + char **argv; + t_env *env; + char *cmd; + + if ((env = env2lst(environ)) == NULL) + return (2); + while (1) + { + ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET); + if (ft_getline(&cmd) >= 0 && cmd != NULL + && (argv = ft_strsplit(cmd, ' ')) != NULL && !ft_strdel_null(&cmd)) + { + if ((res_ext(argv, env)) != NULL) + { + if (ft_strequ(argv[0], "exit")) + return (cleanup(argv, env)); + check_exec(argv, env); + } + } + ft_strdel(&cmd); + ft_del_words_tables(&argv); + } + return (0); +} diff --git a/srcs/ms_env.c b/srcs/ms_env.c deleted file mode 100644 index eec3261..0000000 --- a/srcs/ms_env.c +++ /dev/null @@ -1,99 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ms_env.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tmaze +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/01/13 14:44:20 by tmaze #+# #+# */ -/* Updated: 2019/01/25 16:05:29 by tmaze ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_envelem *elem_new(char *key, char *val) -{ - t_envelem *elem; - - if ((elem = (t_envelem*)ft_memalloc(sizeof(t_envelem))) == NULL) - return (NULL); - if ((elem->key = ft_strdup(key)) == NULL) - { - ft_memdel((void**)&elem); - return (NULL); - } - if (val != NULL && (elem->val = ft_strdup(val)) == NULL) - { - ft_strdel(&(elem->key)); - ft_memdel((void**)&elem); - return (NULL); - } - elem->val = (val == NULL) ? NULL : elem->val; - return (elem); -} - -t_envelem *env_getelemfromkey(char *key, t_list *env) -{ - t_list *tmp; - - tmp = env; - while (tmp) - { - if (ft_strcmp(((t_envelem*)(tmp->content))->key, key) == 0) - return ((t_envelem*)(tmp->content)); - tmp = tmp->next; - } - return (NULL); -} - -t_envelem *env_addupdate(char *key, char *val, t_list **env) -{ - t_list *new; - t_envelem *elem; - char *tmp; - - if ((elem = env_getelemfromkey(key, *env)) != NULL) - { - if (val == NULL) - tmp = val; - else if ((tmp = ft_strdup(val)) == NULL) - return (NULL); - ft_strdel(&(elem->val)); - elem->val = tmp; - return (elem); - } - if ((elem = elem_new(key, val)) == NULL - || (new = ft_lstnew(elem, sizeof(t_envelem))) == NULL) - return (NULL); - ft_lstaddend(env, new); - ft_memdel((void**)&elem); - return ((t_envelem*)(new->content)); -} - -void env_delelem(char *key, t_list **env) -{ - t_list *tmp; - t_list *prec; - - if (ft_strcmp(((t_envelem*)((*env)->content))->key, key) == 0) - { - tmp = *env; - *env = (*env)->next; - ft_lstdelone(&tmp, &ft_lstdelenvelem); - return ; - } - tmp = (*env)->next; - prec = (*env); - while (tmp) - { - if (ft_strcmp(((t_envelem*)(tmp->content))->key, key) == 0) - { - prec->next = tmp->next; - ft_lstdelone(&tmp, &ft_lstdelenvelem); - return ; - } - prec = tmp; - tmp = tmp->next; - } -} diff --git a/srcs/ms_env2.c b/srcs/ms_env2.c deleted file mode 100644 index 3eb9289..0000000 --- a/srcs/ms_env2.c +++ /dev/null @@ -1,85 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ms_env2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: tmaze +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/02/01 10:18:01 by tmaze #+# #+# */ -/* Updated: 2019/02/01 11:16:28 by tmaze ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_envelem *ft_envtoenvelem(char *env) -{ - size_t i; - t_envelem *ret; - - i = 0; - if ((ret = (t_envelem*)ft_memalloc(sizeof(t_envelem))) == NULL) - return (NULL); - while (env[i] && env[i] != '=') - i++; - if ((ret->key = ft_strnew(i)) == NULL) - { - ft_memdel((void**)&ret); - return (NULL); - } - if ((ret->val = ft_strnew(ft_strlen(env) - i - 1)) == NULL) - { - ft_memdel((void**)&ret); - return (NULL); - } - ft_strlcpy(ret->key, env, i + 1); - ft_strlcpy(ret->val, &env[i + 1], ft_strlen(env) - i); - return (ret); -} - -void ft_lstdelenvelem(void *content, size_t size) -{ - size = 0; - ft_strdel(&(((t_envelem*)(content))->key)); - ft_strdel(&(((t_envelem*)(content))->val)); - ft_memdel(&content); -} - -void ft_envelemdel(t_envelem **env) -{ - ft_strdel(&((*env)->key)); - ft_strdel(&((*env)->val)); - ft_memdel((void**)env); -} - -t_list *ft_envtolst(char **env) -{ - size_t i; - t_list *ret; - t_list *new; - t_envelem *elem; - - ret = NULL; - i = 0; - while (env[i]) - { - if ((elem = ft_envtoenvelem(env[i])) == NULL) - ft_lstdel(&ret, &ft_lstdelenvelem); - if (elem == NULL) - return (NULL); - if ((new = ft_lstnew((void*)elem, sizeof(t_envelem))) == NULL) - { - ft_envelemdel(&elem); - ft_lstdel(&ret, &ft_lstdelenvelem); - return (NULL); - } - ft_envelemdel(&elem); - if (ft_lstaddend(&ret, new) == NULL) - { - ft_lstdel(&ret, &ft_lstdelenvelem); - return (NULL); - } - i++; - } - return (ret); -} diff --git a/srcs/ms_exec.c b/srcs/ms_exec.c new file mode 100644 index 0000000..81b43f2 --- /dev/null +++ b/srcs/ms_exec.c @@ -0,0 +1,183 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ms_exec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */ +/* Updated: 2020/01/30 11:48:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int ft_isin(char *str, char c) +{ + int i; + + i = 0; + while (str && str[i]) + if (str[i++] == c) + return (i - 1); + return (-1); +} + +void *put_error(char *file, char *msg) +{ + ft_putstr("minishell: "); + ft_putstr(file); + ft_putstr(": "); + ft_putendl(msg); + return (NULL); +} + +char *check_path_slash(char *exec) +{ + int i; + struct stat info; + + if ((i = access(exec, F_OK)) != 0) + put_error(exec, "no such file or directory"); + if (i != 0) + return (NULL); + if ((i = stat(exec, &info)) != 0) + put_error(exec, "can't determine info"); + if (i != 0) + return (NULL); + if (!S_ISREG(info.st_mode)) + { + put_error(exec, "not an executable file"); + return (NULL); + } + if ((i = access(exec, X_OK)) != 0) + put_error(exec, "permission denied"); + if (i != 0) + return (NULL); + return (exec); +} + +char *check_path_dot(char *exec, t_env *env) +{ + t_env *path; + char *tmp; + char *ret; + + if ((path = ft_envgetelem("PWD", env)) == NULL + || path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL) + return (put_error(exec, "memory error")); + if ((ret = ft_strjoin(tmp, exec)) == NULL) + put_error(exec, "memory error"); + ft_strdel(&tmp); + if (ret == NULL) + return (NULL); + if (access(ret, F_OK) == 0) + { + if (access(ret, X_OK) == 0) + return (exec); + ft_strdel(&ret); + return (put_error(exec, "permission denied")); + } + ft_strdel(&ret); + return (put_error(exec, "no such file or directory")); +} + +void *ft_strdel_null(char **s) +{ + ft_strdel(s); + return (NULL); +} + +char *check_path_elems(char **path_elems, char *exec) +{ + int i; + char *tmp; + char *ret; + + i = -1; + ret = NULL; + while (path_elems[++i]) + { + if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL + && !put_error(exec, "memory error")) + break ; + if ((ret = ft_strjoin(tmp, exec)) == NULL + && !put_error(exec, "memory error") && !ft_strdel_null(&tmp)) + break ; + if (!ft_strdel_null(&tmp) && access(ret, F_OK) == 0) + { + if (access(ret, X_OK) == 0) + break ; + ft_strdel(&ret); + ft_del_words_tables(&path_elems); + return (put_error(exec, "permission denied")); + } + ft_strdel(&ret); + } + return (ret); +} + +char *check_path(char *exec, t_env *env) +{ + size_t i; + t_env *path; + char **path_elems; + char *ret; + + ret = NULL; + if (exec) + { + if (ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1) + return (check_path_slash(exec)); + if (exec[0] == '.') + return (check_path_dot(exec, env)); + if ((path = ft_envgetelem("PATH", env)) == NULL + || path->val == NULL + || (path_elems = ft_strsplit(path->val, ':')) == NULL) + return (put_error(exec, "could not resolve path")); + i = 0; + if ((ret = check_path_elems(path_elems, exec)) == NULL) + { + ft_del_words_tables(&path_elems); + return (put_error(exec, "command not found")); + } + ft_del_words_tables(&path_elems); + } + return (ret); +} + +void exec_cmd_child(char *path, char **argv, char **env_tab, t_env **env) +{ + execve(path, argv, env_tab); + ft_putendl_fd("minishell: error", 1); + if (ft_strcmp(path, argv[0]) != 0) + ft_strdel(&path); + ft_del_words_tables(&env_tab); + ft_envdel(env); + exit(-1); +} + +int exec_cmd(char **argv, t_env **env) +{ + int ret; + char **env_tab; + char *path; + + if ((env_tab = ft_envtotab(*env)) == NULL) + return (-1); + if ((path = check_path(argv[0], *env)) == NULL) + ft_del_words_tables(&env_tab); + if (path == NULL) + return (-1); + if ((ret = fork()) == 0) + exec_cmd_child(path, argv, env_tab, env); + else if (ret == -1) + ft_putendl_fd("minishell: error", 1); + if (ret == -1) + return (-1); + waitpid(ret, NULL, 0); + ft_del_words_tables(&env_tab); + if (ft_strcmp(path, argv[0]) != 0) + ft_strdel(&path); + return (0); +} diff --git a/srcs/ms_ext.c b/srcs/ms_ext.c new file mode 100644 index 0000000..ca0c0bc --- /dev/null +++ b/srcs/ms_ext.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ms_ext.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */ +/* Updated: 2020/01/30 15:42:17 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void *return_null(char *msg) +{ + ft_printf("%s\n", msg); + return (NULL); +} + +void ft_strreplace(char **dst, char *src, int *index, t_env *elem) +{ + if (*dst != NULL) + ft_strdel(dst); + *dst = src; + *index = ft_strlen(elem->val); +} + +t_env *search_key(t_env *env, char *cmd, int index) +{ + t_env *it; + t_env *ret; + int i; + int j; + + it = env; + ret = NULL; + while (it && cmd[index + 1]) + { + i = 0; + j = index; + while (it->key[i] && cmd[j] && it->key[i] == cmd[j]) + { + i++; + j++; + } + if (!it->key[i] && (!ret || ft_strlen(it->key) > ft_strlen(ret->key))) + ret = it; + it = it->next; + } + return (ret); +} + +char *replace_env(char *src, t_env *elem, int key_size, int i) +{ + char *ret; + int res_len; + + res_len = ft_strlen(src) - key_size + ft_strlen(elem->val); + ft_printf("size replace: %d\n", res_len); + if ((ret = ft_strnew(res_len)) != NULL) + { + ft_strncpy(ret, src, i); + ft_strlcat(ret, elem->val, res_len + 1); + ft_strlcat(ret, src + key_size, res_len + 1); + } + else + ft_printf("minishell: memory error\n"); + return (ret); +} + +char *resolve_complete_key(char **src, int *index, t_env *env) +{ + t_env *tmp; + char *ret; + + if ((tmp = search_key(env, *src, *index + 1)) != NULL) + { + if ((ret = replace_env(*src, tmp, ft_strlen(tmp->key) + + 1, *index)) != NULL) + { + ft_strreplace(src, ret, index, tmp); + return (*src); + } + else + return (return_null("minishell: memory error")); + } + else + return (return_null("minishell: variable not found")); +} + +char **res_ext(char **av, t_env *env) +{ + int i[2]; + t_env *tmp; + char *ret; + + i[0] = -1; + while (av && av[++i[0]] && !(i[1] = 0)) + { + while (av[i[0]][i[1]]) + { + if (av[i[0]][i[1]] != '$' && av[i[0]][i[1]] != '~') + i[1]++; + else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env)) + != NULL && (ret = replace_env(av[i[0]], tmp, 1, i[1])) != NULL) + ft_strreplace(&av[i[0]], ret, &i[1], tmp); + else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env)) + == NULL) + return (return_null("Entry HOME not found\n")); + else if (av[i[0]][i[1]] == '$' + && resolve_complete_key(&av[i[0]], &i[1], env) == NULL) + return (NULL); + } + } + return (av); +} diff --git a/srcs/put_error_cd.c b/srcs/put_error_cd.c new file mode 100644 index 0000000..095e4d9 --- /dev/null +++ b/srcs/put_error_cd.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* put_error_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/26 22:18:09 by tmaze #+# #+# */ +/* Updated: 2020/01/26 22:18:24 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void *put_error_cd(char *file, char *msg) +{ + ft_putstr("cd: "); + ft_putstr(file); + ft_putstr(": "); + ft_putendl(msg); + return (NULL); +} + +int put_error_cd2(char *file, char *msg) +{ + ft_putstr("cd: "); + ft_putstr(file); + ft_putstr(": "); + ft_putendl(msg); + return (1); +}