-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_miniprintf.c
67 lines (58 loc) · 1.71 KB
/
ft_miniprintf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_miniprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: malexand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/08 11:19:47 by malexand #+# #+# */
/* Updated: 2017/02/16 17:34:25 by malexand ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_nbrlen(int nbr)
{
int count;
count = 0;
while (nbr > 0)
{
nbr /= 10;
count++;
}
return (count);
}
static int ft_printf_string(va_list pa, int fd)
{
char *str;
str = va_arg(pa, char *);
ft_putstr_fd(str, fd);
return (ft_strlen(str));
}
static int ft_printf_nb(va_list pa, int fd)
{
int nbr;
nbr = va_arg(pa, int);
ft_putnbr_fd(nbr, fd);
return (ft_nbrlen(nbr));
}
int mprintf(int fd, char *str, ...)
{
va_list pa;
int ret;
int count;
ret = 0;
count = 0;
va_start(pa, str);
while (str[count] != '\0')
{
if (str[count] == '%' && str[count + 1] == 's' && ++count)
ret += ft_printf_string(pa, fd);
else if (str[count] == '%' && str[count + 1] == 'd' && ++count)
ret += ft_printf_nb(pa, fd);
else if (++ret)
ft_putchar_fd(str[count], fd);
count++;
}
va_end(pa);
return (ret);
}