-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_putnbr_fd.c
60 lines (55 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasuda <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/11 22:30:20 by mmasuda #+# #+# */
/* Updated: 2021/04/20 11:14:50 by mmasuda ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void calc_putnbr_fd(long num, int digit, int minus, int fd)
{
int i;
char s[256];
i = 0;
if (minus)
{
s[i] = '-';
num *= -1;
}
while (num > 0)
{
s[digit - i - 1] = num % 10 + '0';
num /= 10;
i++;
}
s[digit] = '\0';
write(fd, s, sizeof(char) * digit);
}
void ft_putnbr_fd(int n, int fd)
{
long num;
int digit;
int minus;
num = (long)n;
digit = 0;
minus = 0;
if (num == 0)
write(fd, "0", sizeof(char));
if (num < 0)
{
num *= -1;
minus = 1;
digit++;
}
while (num > 0)
{
num /= 10;
digit++;
}
num = (long)n;
calc_putnbr_fd(num, digit, minus, fd);
}