From 72ea1cc4f0bd0fd3a8aca75faf31db3a519bdf72 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Tue, 22 Aug 2023 19:04:46 +0300 Subject: [PATCH] shrunk and fixed --- printf.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/printf.c b/printf.c index f1106cd..5817695 100644 --- a/printf.c +++ b/printf.c @@ -9,15 +9,17 @@ **/ int _printf(const char *format, ...) { - unsigned int buff_idx, fmt_idx, printed; + unsigned int buff_idx, fmt_idx, buff_size, printed; char *buffer; /*where non formated things are stored*/ va_list args; + va_start(args, format); + buff_size = _strlen(format) - _contains(format , '%'); + buffer = (char *) malloc(_strlen(format) - _contains(format,'%')); /* sized of the non % instances only*/ + if (!format && !buffer) /* No string. No laundry */ return (0); - va_start(args, format); - buffer = (char *) malloc(_strlen(format) - _contains(format,'%')); /* sized of the non % instances only*/ buff_idx = fmt_idx = printed = 0; /*chain assignment*/ while (*(format + fmt_idx)) { @@ -26,7 +28,11 @@ int _printf(const char *format, ...) if (buffer) /* printing and clearing buffer on formatted things */ { printed += _puts(buffer); + buff_size -= _strlen(buffer); _memset(buffer, 0); + buffer = (char *) malloc(buff_size); + if (!buffer) + return (-1); buff_idx = 0; } printed += fmt(*(format + fmt_idx + 1), args);