From 16b3a15963b99720cd64aa11334d83d87b272759 Mon Sep 17 00:00:00 2001 From: y-syo Date: Tue, 6 Jan 2026 11:24:05 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(strdup):=20y?= =?UTF-8?q?ayy=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 1 + src/mem/ft_strdup.s | 31 +++++++++++++++++++++++++++++++ src/tests/main.c | 5 ++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/mem/ft_strdup.s diff --git a/Makefile b/Makefile index 42fc31b..5e45569 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ re: fclean $(MAKE) -s all test: $(OBJ) + rm -f out $(t_CC) $(OBJ) $(t_SRC) $(t_CFLAGS) -o $(t_NAME) .PHONY: clean fclean all re diff --git a/src/mem/ft_strdup.s b/src/mem/ft_strdup.s new file mode 100644 index 0000000..a22449e --- /dev/null +++ b/src/mem/ft_strdup.s @@ -0,0 +1,31 @@ +section .text +global ft_strdup + +extern malloc +extern ft_strlen +extern ft_strcpy + +; char *ft_strdup(const char *str) +ft_strdup: + cmp rdi, 0 + je .endz + + push rdi + call ft_strlen + dec rax + + mov rdi, rax + call malloc + + mov rdx, rdi + mov rdi, rax + pop rsi + call ft_strcpy + + jmp .end + +.endz: + mov rax, 0 + +.end: + ret diff --git a/src/tests/main.c b/src/tests/main.c index f738a82..06df683 100644 --- a/src/tests/main.c +++ b/src/tests/main.c @@ -3,6 +3,7 @@ char *ft_strcpy(char *dest, const char *src); int ft_strcmp(const char *s1, const char *s2); int ft_write(int fd, const char *buf, unsigned int count); int ft_read(int fd, char *buf, unsigned int count); +char *ft_strdup(const char *str); #include #include @@ -49,5 +50,7 @@ int main(void) printf("| %d _ 42 | %d : %s\n", fd, ft_read(fd, xd, 42), xd); printf("\n--- ft_strdup ---\n"); - printf("HELL NAH\n"); + printf("%s\n", ft_strdup("this is a very cool test yayy")); + printf("%s\n", ft_strdup("hi")); + printf("%s\n", ft_strdup(NULL)); }