From e6177d94293e31fd8e54d568455abb1108ad1f11 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 13 Mar 2024 15:25:43 +0100 Subject: [PATCH] feat(ft_strdup|ft_strlen) --- .gitignore | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 52 ++++++++++++++++++++++++++++++++++++++++++++ inc/libft.h | 10 +++++++++ src/ft_strdup.c | 14 ++++++++++++ src/ft_strlen.c | 9 ++++++++ 5 files changed, 142 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 inc/libft.h create mode 100644 src/ft_strdup.c create mode 100644 src/ft_strlen.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40f8930 --- /dev/null +++ b/.gitignore @@ -0,0 +1,57 @@ +# vim +*.swp + +# ---> C +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f7fe466 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +NAME = libft.a + +SRC_FILES = ft_strdup.c \ + ft_strlen.c \ + +INC_FILES = libft.h \ + +OBJ_FILES = $(SRC_FILES:.c=.o) + +SRC_DIR = src/ +INC_DIR = inc/ +OBJ_DIR = obj/ + +SRC = $(addprefix $(SRC_DIR), $(SRC_FILES)) +INC = $(addprefix $(INC_DIR), $(INC_FILES)) +OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES)) + +CC = gcc + +CFLAGS = -Wall -Werror -Wextra + +RED = \033[31m +GREEN = \033[32m +YELLOW = \033[33m +BLUE = \033[34m +CYAN = \033[36m +WHITE = \033[0m + +all: $(NAME) + +$(NAME): $(OBJ) $(INC) + @$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC) + @mv $(OBJ_FILES) $(OBJ_DIR) + @ar rc $(NAME) $(OBJ) + @echo "$(GREEN)[OK]$(WHITE) $(NAME)" + +$(OBJ_DIR)%.o: $(SRC_DIR)%.c + @if [ ! -d ./obj ]; then \ + mkdir -p ./obj; \ + fi; + @$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $< + @echo "$(CYAN)[CC]$(WHITE) $<" + +clean: + @rm -rf $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/inc/libft.h b/inc/libft.h new file mode 100644 index 0000000..ceb21cb --- /dev/null +++ b/inc/libft.h @@ -0,0 +1,10 @@ +#ifndef _LIBFT_H +# define _LIBFT_H + +# include +# include + +char *ft_strdup(const char *s); +size_t ft_strlen(const char *s); + +#endif diff --git a/src/ft_strdup.c b/src/ft_strdup.c new file mode 100644 index 0000000..3e83343 --- /dev/null +++ b/src/ft_strdup.c @@ -0,0 +1,14 @@ +#include "libft.h" + +char *ft_strdup(const char *s) { + size_t len = ft_strlen(s); + char *dest = malloc(len + 1); + if (!dest) { + return NULL; + } + dest[len] = '\0'; + for (size_t i = 0; i < len; i++) { + dest[i] = s[i]; + } + return dest; +} diff --git a/src/ft_strlen.c b/src/ft_strlen.c new file mode 100644 index 0000000..414e29c --- /dev/null +++ b/src/ft_strlen.c @@ -0,0 +1,9 @@ +#include "libft.h" + +size_t ft_strlen(const char *s) { + size_t len = 0; + while (s[len]) { + len = len + 1; + } + return len; +}