Compare commits
10 commits
4a20982777
...
b5172551b8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5172551b8 | ||
|
|
93a750ca6a | ||
|
|
d287180a85 | ||
|
|
ac1ce88633 | ||
|
|
cb24a4301e | ||
|
|
e84d0ea774 | ||
|
|
13be686bcd | ||
|
|
0bfedb14e0 | ||
|
|
b2c1ad07c9 | ||
|
|
c7f7e10e74 |
7 changed files with 87 additions and 5 deletions
3
Makefile
3
Makefile
|
|
@ -6,7 +6,7 @@
|
||||||
# By: rparodi <marvin@42.fr> +#+ +:+ +#+ #
|
# By: rparodi <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
# Updated: 2025/12/13 13:27:57 by rparodi ### ########.fr #
|
# Updated: 2025/12/28 19:40:32 by rparodi ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
|
@ -39,6 +39,7 @@ OBJ_BONUS = $(addprefix $(OBJDIRNAME)/,$(SRC_BONUS:.s=.o))
|
||||||
SRC = src/ft_strlen.s \
|
SRC = src/ft_strlen.s \
|
||||||
src/ft_strcmp.s \
|
src/ft_strcmp.s \
|
||||||
src/ft_strcpy.s \
|
src/ft_strcpy.s \
|
||||||
|
src/ft_strdup.s \
|
||||||
src/ft_write.s \
|
src/ft_write.s \
|
||||||
src/ft_read.s
|
src/ft_read.s
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/12 14:02:08 by rparodi #+# #+# */
|
/* Created: 2025/12/12 14:02:08 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/12/13 13:18:50 by rparodi ### ########.fr */
|
/* Updated: 2025/12/28 19:42:43 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
char *ft_strdup(char *s);
|
||||||
char *ft_strcpy(char *dest, const char *src);
|
char *ft_strcpy(char *dest, const char *src);
|
||||||
int ft_strcmp(const char *s1, const char *s2);
|
int ft_strcmp(const char *s1, const char *s2);
|
||||||
size_t ft_strlen(char *s);
|
size_t ft_strlen(char *s);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
segment .note.GNU-stack
|
segment .note.GNU-stack
|
||||||
|
|
||||||
extern __errno_location
|
extern __errno_location
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
|
|
@ -6,6 +7,7 @@ section .text
|
||||||
|
|
||||||
ft_read:
|
ft_read:
|
||||||
cmp rsi, 0 ; check if the buffer is NULL
|
cmp rsi, 0 ; check if the buffer is NULL
|
||||||
|
mov rax, -1 ; set the return value to -1 (for error) (UB)
|
||||||
je .exit ; if true go to exit
|
je .exit ; if true go to exit
|
||||||
mov rax, 0 ; syscall 0 is for read
|
mov rax, 0 ; syscall 0 is for read
|
||||||
syscall ; call the system call read
|
syscall ; call the system call read
|
||||||
|
|
|
||||||
23
src/ft_strcpy.s
Normal file
23
src/ft_strcpy.s
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
segment .note.GNU-stack
|
||||||
|
|
||||||
|
section .text
|
||||||
|
global ft_strcpy
|
||||||
|
|
||||||
|
ft_strcpy:
|
||||||
|
mov rax, rdi ; dest is the return value
|
||||||
|
cmp rdi, 0 ; check if dest is NULL
|
||||||
|
je .exit ; if true return NULL
|
||||||
|
cmp rsi, 0 ; check if src is NULL
|
||||||
|
je .exit ; if true return NULL
|
||||||
|
xor ecx, ecx ; index set to 0
|
||||||
|
|
||||||
|
.loop:
|
||||||
|
mov dl, BYTE [rsi + rcx] ; take the value of src
|
||||||
|
mov BYTE [rax + rcx], dl ; copy the value of src in dst
|
||||||
|
cmp dl, 0 ; check if the null byte is the last copied
|
||||||
|
je .exit ; return if it was the last byte
|
||||||
|
inc rcx ; increment the index
|
||||||
|
jmp .loop ; restart the loop again
|
||||||
|
|
||||||
|
.exit:
|
||||||
|
ret ; return the rax (dest) value
|
||||||
27
src/ft_strdup.s
Normal file
27
src/ft_strdup.s
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
segment .note.GNU-stack
|
||||||
|
|
||||||
|
extern ft_strcpy
|
||||||
|
extern ft_strlen
|
||||||
|
extern malloc
|
||||||
|
|
||||||
|
section .text
|
||||||
|
global ft_strdup
|
||||||
|
|
||||||
|
ft_strdup:
|
||||||
|
push rbx ; save rbx
|
||||||
|
cmp rdi, 0 ; check s is NULL
|
||||||
|
je .exit ; exit (with restore of rbx) if true
|
||||||
|
mov rbx, rdi ; save rdi value in rbx
|
||||||
|
call ft_strlen ; call ft_strlen on rdi
|
||||||
|
lea rdi, [rax + 1] ; calculation of the malloc size
|
||||||
|
call malloc ; malloc is called return value on rax
|
||||||
|
test rax, rax ; check the return value of malloc
|
||||||
|
je .exit ; exit (with restore of rbx) if true
|
||||||
|
mov rdi, rax ; set the dest string (for ft_strcpy)
|
||||||
|
mov rsi, rbx ; set the source string (for ft_strcpy)
|
||||||
|
pop rbx ; restore rbx
|
||||||
|
jmp ft_strcpy ; call ft_strcpy
|
||||||
|
|
||||||
|
.exit:
|
||||||
|
pop rbx ; restore rbx
|
||||||
|
ret ; return rax (NULL)
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
segment .note.GNU-stack
|
segment .note.GNU-stack
|
||||||
|
|
||||||
extern __errno_location
|
extern __errno_location
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
|
|
|
||||||
33
test/main.c
33
test/main.c
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/12 14:02:17 by rparodi #+# #+# */
|
/* Created: 2025/12/12 14:02:17 by rparodi #+# #+# */
|
||||||
/* Updated: 2025/12/13 13:30:29 by rparodi ### ########.fr */
|
/* Updated: 2026/01/13 12:12:01 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#include "_internal_test.h"
|
#include "_internal_test.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
@ -121,6 +122,30 @@ static void _test_strcpy(void)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _test_strdup(void)
|
||||||
|
{
|
||||||
|
printf("\n%sTesting '%sstrdup%s'%s\n", CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
||||||
|
ft_strdup(NULL);
|
||||||
|
printf("%s✔%s ", CLR_GREEN, RESET);
|
||||||
|
for (int i = 0; strs[i]; i++)
|
||||||
|
{
|
||||||
|
char buf_orig[8192];
|
||||||
|
char buf_home[8192];
|
||||||
|
memset(buf_orig, 0, sizeof(buf_orig));
|
||||||
|
memset(buf_home, 0, sizeof(buf_home));
|
||||||
|
char *ret_orig = strdup(strs[i]);
|
||||||
|
char *ret_home = ft_strdup(strs[i]);
|
||||||
|
if (strcmp(ret_orig, ret_home) == 0 && strcmp(buf_orig, buf_home) == 0)
|
||||||
|
printf("%s✔%s", CLR_GREEN, RESET);
|
||||||
|
else
|
||||||
|
dprintf(2, "\t%s✘%s", CLR_RED, RESET);
|
||||||
|
putchar(' ');
|
||||||
|
free(ret_orig);
|
||||||
|
free(ret_home);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
static void _test_write(void)
|
static void _test_write(void)
|
||||||
{
|
{
|
||||||
int original, homemade;
|
int original, homemade;
|
||||||
|
|
@ -171,8 +196,8 @@ static void _test_read(void)
|
||||||
int original, homemade;
|
int original, homemade;
|
||||||
int err_original, err_homemade;
|
int err_original, err_homemade;
|
||||||
int test_file;
|
int test_file;
|
||||||
char buf_orig[100];
|
char buf_orig[8192];
|
||||||
char buf_home[100];
|
char buf_home[8192];
|
||||||
|
|
||||||
printf("\n%sTesting '%sft_read%s'%s\n",
|
printf("\n%sTesting '%sft_read%s'%s\n",
|
||||||
CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
CLR_YELLOW, CLR_BLUE, CLR_YELLOW, RESET);
|
||||||
|
|
@ -222,6 +247,7 @@ int main(int argc, char *argv[])
|
||||||
_test_strcpy,
|
_test_strcpy,
|
||||||
_test_write,
|
_test_write,
|
||||||
_test_read,
|
_test_read,
|
||||||
|
_test_strdup,
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
char *func_name[] = {
|
char *func_name[] = {
|
||||||
|
|
@ -230,6 +256,7 @@ int main(int argc, char *argv[])
|
||||||
"strcpy",
|
"strcpy",
|
||||||
"write",
|
"write",
|
||||||
"read",
|
"read",
|
||||||
|
"strdup",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue