Repository navigation
Expand file tree
/
Copy path_printf.c
More file actions
106 lines (96 loc) · 2.07 KB
/
Copy path_printf.c
File metadata and controls
106 lines (96 loc) · 2.07 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "main.h"
/**
* adresse - adr
* @argp : list arg
* Return: count
*/
int adresse(va_list argp)
{
int count;
unsigned long int adr;
count = 0;
adr = va_arg(argp, unsigned long int);
if (adr)
{
count += ft_putstr("0x", 0) + ft_adresse(adr, "0123456789abcdef");
}
else
count += ft_putstr("(nil)", 0);
return (count);
}
/**
* ft_format - function format
* @argp : list arguments
* @format : format
* Return: count
*/
static int ft_format(va_list argp, char format)
{
int count;
count = 0;
if (format == 'c')
count += ft_putchar(va_arg(argp, int));
else if (format == 's')
count += ft_putstr(va_arg(argp, char *), 0);
else if (format == 'd' || format == 'i')
count += ft_putnbr(va_arg(argp, int));
else if (format == 'o')
count += ft_octal(va_arg(argp, unsigned int));
else if (format == 'b')
count += ft_binary(va_arg(argp, unsigned int));
else if (format == 'u')
count += ft_unsignedint(va_arg(argp, int));
else if (format == 'x')
count += ft_hexadecimal(va_arg(argp, int), "0123456789abcdef");
else if (format == 'X')
count += ft_hexadecimal(va_arg(argp, int), "0123456789ABCDEF");
else if (format == 'S')
count += ft_putstr(va_arg(argp, char *), 1);
else if (format == 'p')
count += adresse(argp);
else if (format == '%')
count += ft_putchar('%');
else if (format == 'r')
count += rev_str(va_arg(argp, char *));
else if (format == 'R')
count += rot13(va_arg(argp, char *));
else
{
count += ft_putchar('%');
count += ft_putchar(format);
}
return (count);
}
/**
* _printf - printf
* @format : last argument in varidic function
* Return: count
*/
int _printf(const char *format, ...)
{
va_list argp;
int i;
int count;
i = 0;
count = 0;
va_start(argp, format);
if (!format)
return (-1);
if (!format || (format[0] == '%' && format[1] == '\0'))
return (-1);
if (format[0] == '%' && format[1] == ' ' && format[2] == '\0')
return (-1);
while (format[i])
{
if (format[i] == '%')
{
i++;
count += ft_format(argp, format[i]);
}
else
count += ft_putchar(format[i]);
i++;
}
va_end(argp);
return (count);
}