-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils.c
89 lines (78 loc) · 2.07 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imunaev- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/17 19:07:18 by imunaev- #+# #+# */
/* Updated: 2024/11/18 21:27:32 by imunaev- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
ssize_t ft_putstr(char *str)
{
ssize_t count;
count = 0;
if (!str)
str = "(null)";
while (*str)
{
count += ft_putchar(*str);
str++;
}
return (count);
}
ssize_t ft_putptr(void *ptr)
{
ssize_t count;
unsigned long address;
address = (unsigned long)ptr;
count = 0;
if (!ptr)
return (ft_putstr("(nil)"));
count += ft_putstr("0x");
count += ft_puthex(address, 'x');
return (count);
}
ssize_t ft_puthex(unsigned long num, char specifier)
{
ssize_t count;
char *hex_digits;
count = 0;
if (specifier == 'x')
hex_digits = "0123456789abcdef";
else if (specifier == 'X')
hex_digits = "0123456789ABCDEF";
else
return (0);
if (num >= 16)
count += ft_puthex(num / 16, specifier);
count += ft_putchar(hex_digits[num % 16]);
return (count);
}
ssize_t ft_putnbr_unsigned(unsigned int n)
{
ssize_t count;
count = 0;
if (n >= 10)
count += ft_putnbr_unsigned(n / 10);
count += ft_putchar((n % 10) + '0');
return (count);
}
ssize_t ft_putnbr(int n)
{
ssize_t count;
long num;
count = 0;
num = n;
if (num < 0)
{
count += ft_putchar('-');
num = -num;
}
if (num >= 10)
count += ft_putnbr(num / 10);
count += ft_putchar((num % 10) + '0');
return (count);
}