Repository navigation
Expand file tree
/
Copy pathft_printf_utils2.c
More file actions
64 lines (59 loc) · 1.61 KB
/
Copy pathft_printf_utils2.c
File metadata and controls
64 lines (59 loc) · 1.61 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smasatak <smasatak@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/05 01:24:24 by smasatak #+# #+# */
/* Updated: 2026/08/05 12:12:50 by smasatak ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_treat_ptr(void *ptr)
{
uintptr_t addr;
int count;
int er;
count = 0;
er = 0;
if (!ptr)
{
if (write(1, "(nil)", 5) == -1)
return (-1);
else
return (5);
}
addr = (uintptr_t)ptr;
count = write(1, "0x", 2);
if (count == -1)
return (-1);
er = ft_treat_hex(addr, 0);
if (er == -1)
return (-1);
count += er;
return (count);
}
int ft_treat_hex(uintptr_t n, int uppercase)
{
char *base;
int count;
char c;
count = 0;
if (uppercase)
base = "0123456789ABCDEF";
else
base = "0123456789abcdef";
if (n >= 16)
{
count = ft_treat_hex(n / 16, uppercase);
if (count == -1)
return (-1);
}
c = base[n % 16];
if (write(1, &c, 1) == -1)
return (-1);
else
count++;
return (count);
}