shrunk and fixed

This commit is contained in:
LinlyBoi
2023-08-22 19:04:46 +03:00
parent edf0375cf4
commit 72ea1cc4f0

View File

@@ -9,15 +9,17 @@
**/ **/
int _printf(const char *format, ...) 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*/ char *buffer; /*where non formated things are stored*/
va_list args; 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 */ if (!format && !buffer) /* No string. No laundry */
return (0); 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*/ buff_idx = fmt_idx = printed = 0; /*chain assignment*/
while (*(format + fmt_idx)) while (*(format + fmt_idx))
{ {
@@ -26,7 +28,11 @@ int _printf(const char *format, ...)
if (buffer) /* printing and clearing buffer on formatted things */ if (buffer) /* printing and clearing buffer on formatted things */
{ {
printed += _puts(buffer); printed += _puts(buffer);
buff_size -= _strlen(buffer);
_memset(buffer, 0); _memset(buffer, 0);
buffer = (char *) malloc(buff_size);
if (!buffer)
return (-1);
buff_idx = 0; buff_idx = 0;
} }
printed += fmt(*(format + fmt_idx + 1), args); printed += fmt(*(format + fmt_idx + 1), args);