-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_utils.c
92 lines (84 loc) · 1.84 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lkilpela <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 13:35:46 by lkilpela #+# #+# */
/* Updated: 2023/12/07 12:52:58 by lkilpela ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
//character
int ft_putchar(unsigned char c)
{
return (write(1, &c, 1));
}
//string
int ft_putstr(char *s)
{
int i;
i = 0;
if (s == NULL)
{
if (write(1, "(null)", 6) == -1)
return (-1);
return (6);
}
else
{
while (s[i])
{
if (write(1, &s[i], 1) == -1)
return (-1);
i++;
}
}
return (i);
}
//decimal && integer
int ft_putnbr(int n)
{
char c;
int len;
if (n == -2147483648)
return (ft_putstr("-2147483648"));
if (n < 0)
{
len = ft_putchar('-');
if (len == -1)
return (-1);
n *= -1;
}
else
len = 0;
if (n >= 10)
{
len += ft_putnbr(n / 10);
if (len == -1)
return (-1);
}
c = n % 10 + '0';
if (write(1, &c, 1) == -1)
return (-1);
return (len + 1);
}
//unsigned integer
int ft_putunbr(unsigned int n)
{
char c;
int len;
if (n >= 10)
{
len = ft_putunbr(n / 10);
if (len == -1)
return (-1);
}
else
len = 0;
c = n % 10 + '0';
if (write(1, &c, 1) == -1)
return (-1);
return (len + 1);
}