This commit is contained in:
Jack Halford 2017-11-23 15:43:22 +01:00
parent 3efbeb3903
commit cae367e0fc
7 changed files with 51 additions and 63 deletions

View file

@ -105,13 +105,6 @@ test: $(NAME) test.c
@gcc test.c -I $(INC_DIR) -L. -lfts -o test @gcc test.c -I $(INC_DIR) -L. -lfts -o test
@printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n" @printf "\r\033[38;5;117m✓ MAKE test\033[0m\033[K\n"
debug: $(NAME) debug.c
@gcc debug.c -I $(INC_DIR) -L. -lfts -o debug
@printf "\r\033[38;5;117m✓ MAKE debug\033[0m\033[K\n"
run-dbg: debug
lldb ./debug
.PHONY : fclean clean re run-gdb .PHONY : fclean clean re run-gdb
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

View file

@ -1,7 +0,0 @@
#include "libft.h"
int main(void)
{
ft_puts("h2g2");
return (0);
}

View file

@ -1,10 +1,8 @@
global _ft_cat global _ft_cat
global ft_cat global ft_cat
extern ft_putchar
%define STDOUT 1 %define STDOUT 1
%define BUFF_SIZE 10 %define BUFF_SIZE 1024
section .bss section .bss
buf: resb BUFF_SIZE buf: resb BUFF_SIZE

View file

@ -27,6 +27,7 @@ ft_putchar:
; success case then return c ; success case then return c
pop rax pop rax
movzx rax, al ; cast to char
ret ret
err: err:

View file

@ -12,19 +12,24 @@ ft_strdup:
mov rax, 0 mov rax, 0
cmp rdi, 0 cmp rdi, 0
je end je end
push rdi push rdi
call _ft_strlen call _ft_strlen
inc rax inc rax
push rax push rax
mov rdi, rax mov rdi, rax
sub rsp, 8 ; align stack to 16 bytes, x86_64 or mach don't know
call _malloc call _malloc
add rsp, 8
pop rcx
pop rsi
cmp rax, 0 cmp rax, 0
je end je end
mov rdi, rax mov rdi, rax
pop rcx cld ; clear the directon flag
pop rsi
cld
rep movsb rep movsb
end: end:
ret ret

View file

@ -3,14 +3,16 @@ global ft_strlen
_ft_strlen: _ft_strlen:
ft_strlen: ft_strlen:
mov rax, 0 mov rax, 0
cmp rdi, 0 cmp rdi, 0
je end je end
loop: t
cmp byte [rdi], 0 mov rcx, -1
je end cld
inc rax repnz scasb
inc rdi
jmp loop not rcx
lea rax, [rcx - 1]
end: end:
ret ret

View file

@ -6,11 +6,6 @@ int ft_putstr(const char *str)
return (write(1, str, strlen(str))); return (write(1, str, strlen(str)));
} }
/* int ft_putchar(const char c) */
/* { */
/* return (write(1, &c, 1)); */
/* } */
int ft_putnstr(const char *str, size_t n) int ft_putnstr(const char *str, size_t n)
{ {
return (write(1, str, n)); return (write(1, str, n));
@ -400,26 +395,26 @@ int test_strdup()
} }
str = ft_strdup("Coucou"); str = ft_strdup("Coucou");
str_cmp = strdup("Coucou"); str_cmp = strdup("Coucou");
/* len = strlen(str); */ len = strlen(str);
/* len_cmp = strlen(str_cmp); */ len_cmp = strlen(str_cmp);
/* if (len != len_cmp) */ if (len != len_cmp)
/* { */ {
/* printf("FAILED: len is %d vs %d\n", len, len_cmp); */ printf("FAILED: len is %d vs %d\n", len, len_cmp);
/* return (1); */ return (1);
/* } */ }
/* i = -1; */ i = -1;
/* while (++i < len) */ while (++i < len)
/* { */ {
/* if (str[i] != str_cmp[i]) */ if (str[i] != str_cmp[i])
/* { */ {
/* ft_putnstr(str, len); */ ft_putnstr(str, len);
/* ft_putnstr(str_cmp, len); */ ft_putnstr(str_cmp, len);
/* printf("FAILED: %c vs %c\n", str[i], str_cmp[i]); */ printf("FAILED: %c vs %c\n", str[i], str_cmp[i]);
/* return (1); */ return (1);
/* } */ }
/* } */ }
/* free(str); */ free(str);
/* free(str_cmp); */ free(str_cmp);
return (0); return (0);
} }
@ -427,13 +422,15 @@ int test_strdup()
int test_cat(char ** argv) int test_cat(char ** argv)
{ {
ft_putstr(__func__); ft_putstr(":\n"); ft_putstr(__func__); ft_putstr(":\n");
/* ft_putstr("Wait for a user input:\n"); */ /* ft_putstr("Wait for a user input:\n"); */
/* ft_cat(0); */ /* ft_cat(0); */
ft_putstr("\ntest.c:\n");
ft_cat(open(__FILE__, O_RDONLY)); /* ft_putstr("\ntest.c:\n"); */
ft_putstr("\ntest binary:\n"); /* ft_cat(open(__FILE__, O_RDONLY)); */
ft_cat(open(argv[0], O_RDONLY));
ft_cat(-42); /* ft_putstr("\ntest binary:\n"); */
/* ft_cat(open("test", O_RDONLY)); */
return (0); return (0);
} }
@ -510,16 +507,15 @@ int main(int argc, char **argv)
test_memcpy() || test_memcpy() ||
test_strdup()) test_strdup())
return (1); return (1);
exit(1);
ft_putstr("\nPART BONUS:\n_______\n"); ft_putstr("\nPART BONUS:\n_______\n");
if (test_isupper() || if (test_isupper() ||
test_islower() || test_islower() ||
test_isupper()) test_isupper() ||
/* test_putchar()) */ test_putchar())
return (1); return (1);
ft_putstr("\nPART 3:\n_______\n"); ft_putstr("\nPART 3:\n_______\n");
if (test_cat(argv)) if (test_cat(argv))
return (1); return (1);
puts("\033c\n\033[38;5;117mALL PASSED:\n___________\n\033[0m"); puts("\033c\n\033[38;5;117mALL PASSED\n___________\n\033[0m");
return (0); return (0);
} }