-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_memory.c
62 lines (56 loc) · 1.64 KB
/
p_memory.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* p_memory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asoudani <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 17:36:26 by asoudani #+# #+# */
/* Updated: 2024/11/14 21:39:59 by asoudani ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int splitting_f(char *adress, char *hex, size_t n, int *nb)
{
int i;
i = 1;
while (n > 0)
{
adress[i] = hex[n % 16];
n /= 16;
i++;
}
while (--i)
if (ft_putchar_fd(adress[i], 1, nb) == -1)
return (-1);
return (1);
}
static void fireforce(char *adress)
{
free(adress);
}
int p_memory(void *ad, int *nb)
{
size_t n;
char *hex;
char *adress;
adress = malloc(18 * sizeof(char));
if (!adress)
return (-1);
hex = "0123456789abcdef";
n = (size_t)ad;
if (ft_putstr_fd("0x", 1, nb) == -1)
return (fireforce(adress), -1);
if (n == 0)
{
if (ft_putchar_fd('0', 1, nb) == -1)
return (fireforce(adress), -1);
}
else
{
if (splitting_f(adress, hex, n, nb) == -1)
return (fireforce(adress), -1);
}
free(adress);
return (1);
}