-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
88 lines (78 loc) · 1.89 KB
/
ft_putnbr_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlindenm <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/21 17:33:27 by mlindenm #+# #+# */
/* Updated: 2023/05/12 16:59:19 by mlindenm ### ########.fr */
/* */
/* ************************************************************************** */
/*Outputs the integer ’n’ to the given file descriptor.*/
#include "libft.h"
static int check_error(int nbr, int fb);
static int nbr_length(int nbr);
static void print_int_array(int numbers[], int len, int fb);
void ft_putnbr_fd(int n, int fd)
{
int temp[10];
int temp_nbr;
int i;
temp_nbr = 0;
i = 0;
if (check_error(n, fd))
return ;
if (n < 0)
{
write(fd, "-", 1);
n *= -1;
}
temp_nbr = n;
i = nbr_length(n) - 1;
while (i >= 0)
{
temp[i] = temp_nbr % 10;
temp_nbr /= 10;
i--;
}
print_int_array(temp, nbr_length(n), fd);
}
static int check_error(int nbr, int fd)
{
if (nbr == -2147483648)
{
write(fd, "-2147483648", 11);
return (1);
}
if (nbr == 0)
{
write(fd, "0", 1);
return (1);
}
return (0);
}
static int nbr_length(int nbr)
{
int len;
len = 0;
while (nbr != 0)
{
nbr /= 10;
len++;
}
return (len);
}
static void print_int_array(int numbers[], int len, int fd)
{
char c;
int i;
i = 0;
while (len > 0)
{
c = numbers[i] + '0';
write(fd, &c, 1);
len--;
i++;
}
}