-
Notifications
You must be signed in to change notification settings - Fork 0
Test debug
d0vak1n edited this page Mar 19, 2024
·
1 revision
#include <unistd.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
size_t ft_strlen(const char *s)
{
size_t count;
count = 0;
while (*s != '\0')
{
count++;
s++;
}
return (count);
}
void ft_bzero(void *s, size_t n)
{
size_t i;
char *reserva;
i = 0;
if (n == 0)
return ;
reserva = (char *)s;
while (i < n)
{
*reserva = '\0';
i++;
reserva++;
}
}
void *ft_calloc(size_t count, size_t size)
{
void *result;
result = malloc(count * size);
if (!result)
return (NULL);
else
{
ft_bzero(result, count * size);
return (result);
}
}
static int _numlen(unsigned long p)
{
int string_lenght;
string_lenght = 1;
while (p >= 16)
{
p /= 16;
string_lenght++;
}
return (string_lenght);
}
static char *_create_str(unsigned long long p)
{
char *str;
int string_lenght;
string_lenght = _numlen(p);
str = ft_calloc((string_lenght + 1), sizeof(char));
if (!str)
return (NULL);
return (str);
}
int ft_print_hex(unsigned long long h)
{
int i;
char *hex;
char *res;
hex = "0123456789abcdef";
res = _create_str(h);
if (!res)
return (0);
i = _numlen(h) - 1;
while (i >= 0)
{
res[i] = hex[h % 16];
h /= 16;
i--;
}
printf("0x");
printf("%s", res);
i = ft_strlen(res) + 2;
free(res);
return (i);
}
int ft_print_pointer(unsigned long int address)
{
int len;
len = 0;
len = ft_print_hex(address);
return (len);
}
static int _format(char const *str, void *arg)
{
int numchars;
numchars = 0;
if (*str == 'p')
numchars += ft_print_pointer((unsigned long int)arg);
return (numchars);
}
int ft_printf(char const *str, ...)
{
va_list args;
int numchars;
int i;
i = -1;
numchars = 0;
va_start(args, str);
while (str[++i])
{
if (str[i] == '%')
{
if (strchr("cspdiuxX", str[i + 1]))
numchars += _format(&str[i + 1], va_arg(args, void *));
else if (str[i + 1] == '%')
numchars += printf("%");
i++;
}
else
numchars += printf("%c", str[i]);
}
va_end(args);
return (numchars);
}
int main() {
char *str;
str = "%p\n\n";
int printedPointer1;
printedPointer1 = ft_printf(str, 156);
printf("%d", printedPointer1);
return 0;
}