-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_printf_p.c
51 lines (46 loc) · 1.14 KB
/
my_printf_p.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
** EPITECH PROJECT, 2023
** my_printf_p
** File description:
** Placeholder
*/
#include <stdlib.h>
#include <stdarg.h>
#include "include/my.h"
#include <unistd.h>
int write_pointer(char *str, params_t params)
{
int count = 0;
int padding = params.width - my_strlen(str) - 2;
if ((params.flags & 16) == 0) {
for (; count < padding; count += write(1, " ", 1));
count += write(1, "0x", 2);
my_putstr(str);
} else {
count += write(1, "0x", 2);
my_putstr(str);
for (; count <= padding; count += write(1, " ", 1));
}
return count;
}
int my_printf_p(va_list list, params_t *params)
{
unsigned long int nb = (unsigned long int)va_arg(list, void *);
char base[16] = "0123456789abcdef";
char *str;
int i = 0;
unsigned int nb2 = nb;
for (; nb2 > 0; nb2 /= 16)
i++;
i += (nb == 0) ? 1 : 0;
str = malloc(sizeof(char) * (i + 1));
for (i = 0; nb > 0; nb /= 16) {
str[i] = base[nb % 16];
i++;
}
str[0] = (i == 0) ? '0' : str[0];
i += (nb == 0) ? 1 : 0;
str[i] = '\0';
my_revstr(str);
return write_pointer(str, *params);
}