」 feat(ft_read): added read yayy

This commit is contained in:
y-syo
2025-12-06 17:38:13 +01:00
parent 09649f52d5
commit f6ad491a44
3 changed files with 35 additions and 1 deletions

22
src/sys/ft_read.s Normal file
View File

@@ -0,0 +1,22 @@
extern __errno_location
section .text
global ft_read
; ssize_t ft_read(int fd, void *buf, size_t count);
ft_read:
mov eax, 0
syscall
cmp rax, 0
jle .error
.ret:
ret
.error:
mov r10, rax
call __errno_location wrt ..plt
mov [rax], r10
mov rax, -1
jmp .ret

View File

@@ -3,7 +3,7 @@ extern __errno_location
section .text section .text
global ft_write global ft_write
; ssize_t write(int fd, const void *buf, size_t count); ; ssize_t ft_write(int fd, const void *buf, size_t count);
ft_write: ft_write:
mov eax, 1 mov eax, 1
syscall syscall

View File

@@ -2,11 +2,13 @@ int ft_strlen(char *str);
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);
int ft_write(int fd, const char *buf, unsigned int count); int ft_write(int fd, const char *buf, unsigned int count);
int ft_read(int fd, char *buf, unsigned int count);
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
int main(void) int main(void)
{ {
@@ -38,4 +40,14 @@ int main(void)
printf("| 200 \"xd\" 2 | %d\n", ft_write(200, "xd", 2)); printf("| 200 \"xd\" 2 | %d\n", ft_write(200, "xd", 2));
int fd = open("out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); int fd = open("out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("| %d \"[...]\" 42 | %d\n", fd, ft_write(fd, "wizard money gang, we love casting spells\n", 42)); printf("| %d \"[...]\" 42 | %d\n", fd, ft_write(fd, "wizard money gang, we love casting spells\n", 42));
close(fd);
printf("\n--- ft_read ---\n");
char *xd = calloc(sizeof(char), 43);
printf("| 200 _ 2 | %d : %s\n", ft_read(200, xd, 6), xd);
fd = open("out", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("| %d _ 42 | %d : %s\n", fd, ft_read(fd, xd, 42), xd);
printf("\n--- ft_strdup ---\n");
printf("HELL NAH\n");
} }