-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putptr.c
70 lines (63 loc) · 1.56 KB
/
ft_putptr.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
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <xhelp00@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/09 17:07:39 by phelebra #+# #+# */
/* Updated: 2023/02/10 12:53:25 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_sizehex(uintptr_t u)
{
int i;
i = 0;
while (u)
{
i++;
u /= 16;
}
return (i);
}
static char *ft_itohex(uintptr_t u)
{
char *ptr;
int size;
int i;
size = ft_sizehex(u);
ptr = malloc(sizeof(char) * (size + 1));
if (!ptr)
return (NULL);
ptr[size] = '\0';
while (u)
{
i = u % 16;
if (i < 10)
ptr[size - 1] = i + '0';
else
ptr[size - 1] = i + 87;
u /= 16;
size--;
}
return (ptr);
}
int ft_putptr(uintptr_t u)
{
char *s;
int size;
size = 0;
s = NULL;
if (!u)
{
s = "(nil)";
ft_putstr_fd(s, 1);
return (ft_strlen(s));
}
size += write (1, "0x", 2);
s = ft_itohex(u);
size += ft_putstr(s);
free(s);
return (size);
}