-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils.c
111 lines (101 loc) · 2.15 KB
/
ft_printf_utils.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: issierra <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 09:31:26 by issierra #+# #+# */
/* Updated: 2023/11/16 11:04:03 by issierra ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
return (write(1, &c, 1));
}
int ft_putnbr(long nb)
{
long num;
int len;
int aux;
len = 0;
if (nb < 0)
{
if (write(1, "-", 1) == -1)
return (-1);
len += 1;
nb *= -1;
}
num = nb % 10 + '0';
if (nb > 9)
{
aux = ft_putnbr(nb / 10);
if (aux == -1)
return (-1);
len += aux;
}
if (write(1, &num, 1) == -1)
return (-1);
len += 1;
return (len);
}
int ft_putnbr_hexa(unsigned long nbr, char up)
{
int len;
int mod;
char *base;
int aux;
mod = 0;
len = 0;
if (up == 'X')
base = "0123456789ABCDEF";
else
base = "0123456789abcdef";
if (nbr > 15)
{
aux = ft_putnbr_hexa((nbr / 16), up);
if (aux == -1)
return (-1);
len += aux;
}
mod = nbr % 16;
if (write(1, &base[mod], 1) == -1)
return (-1);
len += 1;
return (len);
}
int ft_putptr(void *ptr)
{
int len;
int aux;
len = 0;
aux = 0;
if (ft_putstr("0x") == -1)
return (-1);
len += 2;
aux = ft_putnbr_hexa((unsigned long)ptr, 'x');
if (aux == -1)
return (-1);
len += aux;
return (len);
}
int ft_putstr(char *str1)
{
int len;
if (!str1)
{
if (write (1, "(null)", 6) == -1)
return (-1);
return (6);
}
len = 0;
while (*str1 != '\0')
{
if (write(1, str1, 1) == -1)
return (-1);
len++;
str1++;
}
return (len);
}