this segfaults idk why

This commit is contained in:
LinlyBoi
2023-08-20 23:02:01 +03:00
parent e6d8091daf
commit 0df9b1789c

View File

@@ -1,72 +1,56 @@
#include "main.h" #include "main.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
/**
* _printf - printf but worse
* @format: format string
* @...: ???
* Return: characters written
**/
int _printf(const char *format, ...) int _printf(const char *format, ...)
{ {
/* For any Iterative Operation */ int i, j;
int i, pass_str_len;
unsigned int identifiers, BUFF_SIZE; unsigned int identifiers, BUFF_SIZE;
char *buffer; char *buffer, *next;
va_list args; va_list args;
/* Instantiating Buffer Size */
BUFF_SIZE = _strlen(format) - (identifiers * 2);
/* Shoving args in list */
va_start(args, format); va_start(args, format);
/* Number of Identifiers that aren't "%%" */
identifiers = _contains(format, '%'); identifiers = _contains(format, '%');
BUFF_SIZE = _strlen(format) - (identifiers * 2);
/* If string is nonexist, die */ if (!format) /* No string. No laundry */
if (!format)
return (0); return (0);
buffer = malloc(BUFF_SIZE);
/* While string is alive or smth*/ i = 0; /*TODO:gotta rename those*/
while(format) j = 0;
buffer = "";
while (format)
{ {
/* Detecting identifier in code if ((*format == '%') && (*(format + 1)))
* whilst ensuring next char exists
*/
if ((*format == '%') && (*(format + 1) != '\0'))
{ {
/* Handling depending on char after identifier*/
switch (*(format + 1)) switch (*(format + 1))
{ {
case 's': case 's':
/* Something _strlen() string arg, realloc next = va_arg(args, char*); /*Store string temporarily*/
* Increment i by strlen or smth _strcpy(&buffer[j], next);
*/ j += _strlen(next);
BUFF_SIZE += _strlen(next);
/* Trying to get length of passed string */
pass_str_len = _strlen(va_arg(args, char*));
/* Incrememnting buffersize to fit new string*/
BUFF_SIZE += pass_str_len;
/* Shoving str values into buffer*/
/* Realloc? pls review*/
free(buffer);
buffer = malloc(BUFF_SIZE * sizeof(char));
/* Incrementing iterator i by string length
* So we'd be able to insert at proper
* index in buffer
*/
i += pass_str_len;
break; break;
case 'c': case 'c':
/* Something add 1B to buffer and realloc /*
* You don't add 1Byte because %c becomes a single char so less to reallocate
* Increment i by 1 * Increment i by 1
*/ */
break; break;
case '%': case '%':
/* Something add 1B, shove %, realloc /*
* Something add 1B, shove %, realloc
* increment i by 1 * increment i by 1
*/ */
break; break;
@@ -79,11 +63,11 @@ int _printf(const char *format, ...)
} }
else else
{ {
/* Dereferencing with '*' */ *(buffer + i) = *(format + i);
*(buffer + i) = *(char *)(format + i);
i++; i++;
} }
} }
write(1, buffer, BUFF_SIZE);
return (_strlen(buffer)); return (_strlen(buffer));
} }