-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_pointer.c
57 lines (52 loc) · 1.57 KB
/
ft_printf_pointer.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
52
53
54
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_pointer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mokhan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/05 20:31:36 by mokhan #+# #+# */
/* Updated: 2023/10/06 13:38:39 by mokhan ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int first_nibble(unsigned long long addr)
{
int i;
int hex;
int count;
count = -1;
i = sizeof(addr) * 2 - 1;
hex = 0;
while (!hex)
{
hex = (addr >> (i * 4)) & 0xf;
i--;
count++;
}
return (count);
}
int print_pointer(unsigned long long addr, int *flags)
{
int nibble;
int size;
int i;
char *hex;
if (!addr)
return (print_str("(nil)", flags));
size = sizeof(addr) * 2 - first_nibble(addr) + 2;
hex = malloc(size + 1);
if (!hex)
return (0);
hex[size] = '\0';
i = size - 2;
while (i--)
{
nibble = (addr >> (i * 4)) & 0xf;
hex[size - 1 - i] = HEXBASE[nibble];
}
ft_memcpy(hex, "0x", 2);
i = print_str(hex, flags);
free (hex);
return (i);
}