aboutsummaryrefslogtreecommitdiff
path: root/ft_printf.c
blob: ce7f15ca2660b375565f5e8de643876f7d09ddcb (plain)
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
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include "header.h"

int ft_printf(const char *format, ...)
{
    va_list ap;
	t_flist	*format_list;
	char	*str;
    int		print_len;
    int		i;

	if ((format_list = parse(format)) == NULL)
		return (-1);
    va_start(ap, format);
    print_len = 0;
	i = -1;
    while (format[++i])
    {
        if (format[i] == '%')
        {
			if ((str = convert_to_str(format_list->content, ap)) == NULL)
			{
				list_destroy(&format_list);
				return (-1);
			}
			ft_putstr(str);
			print_len += ft_strlen(str);
			free(str);
			i += format_list->content->len;
			list_pop_front(&format_list);
        }
        else
            write(STDOUT_FILENO, format + i, 1);
    }
    va_end(ap);
    return (print_len + i);
}