-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
66 lines (59 loc) · 1.8 KB
/
ft_printf.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf.c :+: :+: */
/* +:+ */
/* By: mraasvel <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/09 20:14:16 by mraasvel #+# #+# */
/* Updated: 2020/11/22 17:36:54 by mraasvel ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_conversion.h"
/*
** Prints normal characters from the string.
** If it encounters a '%' sign, it will call the
** conversion function to read it.
** Bytes written are stored in the 'n' variable.
*/
static int parse_string(const char *format, va_list start)
{
size_t i;
int n;
int return_value;
i = 0;
n = 0;
while (format[i] != 0)
{
if (format[i] == '%')
{
i++;
return_value = conversion(format, &i, start, n);
if (return_value == -1)
return (-1);
n += return_value;
continue ;
}
if (write(1, format + i, 1) == -1)
return (-1);
n++;
i++;
}
return (n);
}
/*
** 1. Open the argument list
** 2. Call the main functions
** 3. Return values: bytes written or
** -1 on error.
*/
int ft_printf(const char *format, ...)
{
va_list start;
int bytes_written;
va_start(start, format);
bytes_written = parse_string(format, start);
va_end(start);
return (bytes_written);
}