Fixed a lot of segfaults and bad practices
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,3 @@
|
|||||||
*.o
|
*.o
|
||||||
*.c
|
|
||||||
*.out
|
*.out
|
||||||
*.a
|
*.a
|
||||||
1
Makefile
1
Makefile
@@ -17,6 +17,7 @@ SRCS = ft_strlen.s \
|
|||||||
ft_read.s \
|
ft_read.s \
|
||||||
ft_strcpy.s \
|
ft_strcpy.s \
|
||||||
ft_strcmp.s \
|
ft_strcmp.s \
|
||||||
|
ft_strdup.s \
|
||||||
|
|
||||||
OBJS = ${SRCS:.s=.o}
|
OBJS = ${SRCS:.s=.o}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
global _read
|
global ft_read
|
||||||
extern __errno_location
|
extern __errno_location
|
||||||
|
|
||||||
_read:
|
ft_read:
|
||||||
mov eax, 0x11
|
mov eax, 0x11
|
||||||
syscall
|
syscall
|
||||||
test rax, rax
|
test rax, rax
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
extern _strcmp
|
global ft_strcmp
|
||||||
|
extern _malloc
|
||||||
|
|
||||||
|
ft_strcmp:
|
||||||
_strcmp:
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
xor rbx, rbx
|
xor rbx, rbx
|
||||||
|
|
||||||
.count:
|
.count:
|
||||||
mov rax, [rdi + rbx]
|
mov rax, [rdi + rbx]
|
||||||
sub rax, [rsi + rbx]
|
sub rax, [rsi + rbx]
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
extern _strcpy
|
global ft_strcpy
|
||||||
|
|
||||||
_strcpy:
|
ft_strcpy:
|
||||||
xor rbx, rbx
|
xor rbx, rbx
|
||||||
|
|
||||||
.cpy:
|
.cpy:
|
||||||
cmp dword [rsi + rbx], 0
|
cmp byte [rsi + rbx], 0
|
||||||
je .end
|
je .end
|
||||||
mov rax, [rsi + rbx]
|
mov rax, [rsi + rbx]
|
||||||
mov [rdi + rbx], rax
|
mov [rdi + rbx], rax
|
||||||
@@ -12,5 +12,6 @@ _strcpy:
|
|||||||
jmp .cpy
|
jmp .cpy
|
||||||
|
|
||||||
.end:
|
.end:
|
||||||
|
mov byte [rdi + rbx], 0
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
ret
|
ret
|
||||||
|
|||||||
25
ft_strdup.s
Normal file
25
ft_strdup.s
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
global ft_strdup
|
||||||
|
|
||||||
|
extern ft_strlen
|
||||||
|
extern ft_strcpy
|
||||||
|
extern malloc
|
||||||
|
extern __errno_location
|
||||||
|
|
||||||
|
ft_strdup:
|
||||||
|
cmp rdi, 0
|
||||||
|
je .end
|
||||||
|
mov r12, rdi
|
||||||
|
call ft_strlen
|
||||||
|
inc rax
|
||||||
|
mov rdi, rax
|
||||||
|
call malloc wrt ..plt
|
||||||
|
test rax, rax
|
||||||
|
je .end
|
||||||
|
mov rdi, rax
|
||||||
|
mov rsi, r12
|
||||||
|
call ft_strcpy
|
||||||
|
ret
|
||||||
|
|
||||||
|
.end:
|
||||||
|
xor rax, rax
|
||||||
|
ret
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
global _strlen
|
global ft_strlen
|
||||||
|
|
||||||
_strlen:
|
ft_strlen:
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
|
|
||||||
.cmp_char:
|
.cmp_char:
|
||||||
cmp byte [rdi + rax], 0
|
cmp byte [rdi + rax], 0
|
||||||
je .end
|
je .end
|
||||||
inc rax
|
inc rax
|
||||||
jmp .cmp_char
|
jmp .cmp_char
|
||||||
|
|
||||||
.end:
|
.end:
|
||||||
ret
|
ret
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
global _write
|
global ft_write
|
||||||
extern __errno_location
|
extern __errno_location
|
||||||
|
|
||||||
_write:
|
ft_write:
|
||||||
mov eax, 0x1
|
mov eax, 0x1
|
||||||
syscall
|
syscall
|
||||||
test rax, rax
|
test rax, rax
|
||||||
|
|||||||
102
test.c
Normal file
102
test.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
extern ssize_t ft_write(int fd, const void *buf, size_t count);
|
||||||
|
|
||||||
|
extern int ft_strcmp(const char *s1, const char *s2);
|
||||||
|
|
||||||
|
extern char *ft_strcpy(char *dest, const char *src);
|
||||||
|
|
||||||
|
extern size_t ft_strlen(const char *s);
|
||||||
|
|
||||||
|
extern ssize_t ft_read(int fd, void *buf, size_t count);
|
||||||
|
|
||||||
|
extern char *ft_strdup(const char *s);
|
||||||
|
|
||||||
|
void Test_read(char *filename, int len)
|
||||||
|
{
|
||||||
|
char buffer[100] = {0};
|
||||||
|
|
||||||
|
int fd = open(filename, O_RDONLY);
|
||||||
|
printf("READ : %ld | ", read(fd, buffer, len));
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
close(fd);
|
||||||
|
bzero(buffer, len);
|
||||||
|
|
||||||
|
fd = open(filename, O_RDONLY);
|
||||||
|
printf("READ : %ld | ", ft_read(fd, buffer, len));
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_write(char *buffer, int len)
|
||||||
|
{
|
||||||
|
printf(" | WRITE : %ld\n", write(1, buffer, len));
|
||||||
|
printf(" | WRITE : %ld\n", ft_write(1, buffer, len));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_strdup(char *buffer)
|
||||||
|
{
|
||||||
|
char *newbuffer;
|
||||||
|
|
||||||
|
newbuffer = strdup(buffer);
|
||||||
|
printf("STRDUP : %s\n", newbuffer);
|
||||||
|
free(newbuffer);
|
||||||
|
|
||||||
|
newbuffer = ft_strdup(buffer);
|
||||||
|
printf("FT_STRDUP : %s\n", newbuffer);
|
||||||
|
free(newbuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_strlen(char *str)
|
||||||
|
{
|
||||||
|
int len = strlen(str);
|
||||||
|
int ft_len = ft_strlen(str);
|
||||||
|
|
||||||
|
printf("STRLEN : %s\n", (len == ft_len) ? "OK" : "KO");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_strcpy(char *buffer)
|
||||||
|
{
|
||||||
|
char cpybuffer[strlen(buffer) + 1];
|
||||||
|
|
||||||
|
bzero(cpybuffer, strlen(buffer) + 1);
|
||||||
|
strcpy(cpybuffer, buffer);
|
||||||
|
printf("STRCPY : %s\n", cpybuffer);
|
||||||
|
|
||||||
|
bzero(cpybuffer, strlen(buffer) + 1);
|
||||||
|
ft_strcpy(cpybuffer, buffer);
|
||||||
|
printf("FT_STRCPY : %s\n", cpybuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Test_strcmp(char *str1, char *str2)
|
||||||
|
{
|
||||||
|
int res1 = strcmp(str1, str2);
|
||||||
|
int res2 = ft_strcmp(str1, str2);
|
||||||
|
|
||||||
|
printf("STRCMP : %d FT_STRCMP : %d | %s\n", res1, res2, (res1 == res2) ? "OK" : "KO");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int returnval[2];
|
||||||
|
int fd = open("mauvaisdavid.txt", O_RDONLY);
|
||||||
|
char buffer[100] = {0};
|
||||||
|
|
||||||
|
Test_strlen("clafete");
|
||||||
|
|
||||||
|
Test_read("mauvaisdavid.txt", 15);
|
||||||
|
|
||||||
|
Test_write("youpipouic", 10);
|
||||||
|
|
||||||
|
Test_strcpy("jesuislevilainbebe");
|
||||||
|
|
||||||
|
Test_strdup("youpilolololol");
|
||||||
|
|
||||||
|
Test_strcmp("vilain", "vilain");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user