diff --git a/printf.c b/printf.c index 13f533c..fc86c4b 100644 --- a/printf.c +++ b/printf.c @@ -11,7 +11,7 @@ **/ int _printf(const char *format, ...) { - int buff_idx; + int buff_idx, fmt_idx; unsigned int identifiers, BUFF_SIZE; char *buffer, *next; va_list args; @@ -25,7 +25,8 @@ int _printf(const char *format, ...) if (!format) /* No string. No laundry */ return (0); buff_idx = 0; - while (format) + fmt_idx = 0; + while (*(format + fmt_idx)) { if ((*format == '%') && (*(format + 1))) /*hello %s*/ { @@ -34,7 +35,9 @@ int _printf(const char *format, ...) case 's': next = va_arg(args, char*); /*Store string temporarily*/ buffer = _strcpy(buffer, next); - BUFF_SIZE += _strlen(buffer); + BUFF_SIZE = _strlen(buffer); + buff_idx += _strlen(next); + fmt_idx += 2; break; case 'c': /* add 1 byte and i++ */ @@ -45,11 +48,12 @@ int _printf(const char *format, ...) } else { - *(buffer + buff_idx) = *(format + buff_idx); + *(buffer + buff_idx) = *(format + fmt_idx); buff_idx++; + fmt_idx++; } } - write(1, &buffer, BUFF_SIZE); + write(1, buffer, BUFF_SIZE); return (_strlen(buffer)); } diff --git a/strcpy.c b/strcpy.c index 3f43ee2..ed0fd48 100644 --- a/strcpy.c +++ b/strcpy.c @@ -23,6 +23,8 @@ char *_strcpy(char *dest, char *src) *(new_me + s_idx++) = *(src + i); *(new_me + _strlen(new_me)) = '\0'; + free(dest); + return (new_me); } diff --git a/tests/hello.c b/tests/hello.c index 368a804..d63f930 100644 --- a/tests/hello.c +++ b/tests/hello.c @@ -8,5 +8,6 @@ int main(void) { int len; - len = _printf("Hello world"); + len = _printf("Hello world\n"); + return (len); } diff --git a/tests/str_print.c b/tests/str_print.c index 0585c23..4f865ee 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -9,10 +9,11 @@ int main(void) { - char test_0[] = "Hej"; + char *test_0 = "Hej"; /* * Brain ded to think bout test cond */ - return (_printf("%s", test_0)); + _printf("%s \n", test_0); + return (_printf("hey, %s", test_0)); } diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..363f67a --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,3 @@ +#!/usr/bin/bash +gcc -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a +./a