-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_printf.c
More file actions
95 lines (86 loc) · 1.43 KB
/
_printf.c
File metadata and controls
95 lines (86 loc) · 1.43 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
#include "main.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
/**
* print_type - select the correct function depending on
* char after %
* @format: pointer to char in string passed
* @list: pointer to list item
* Return: int
*/
int print_type(const char *format, va_list list)
{
unsigned int j = 0, count = 0, match;
char str;
vars_t v[] = {
{"c", print_c},
{"s", print_s},
{"d", conv_d},
{"i", conv_d},
{"b", print_b},
{NULL, NULL}
};
str = *(format + 1);
if (str == '%')
{
_putchar('%');
count = count + 1;
return (count);
}
j = 0;
while (v[j].t != NULL)
{
if (*(v[j].t) == str)
{
match = v[j].f(list);
return (match);
}
j = j + 1;
}
if (v[j].t == NULL)
{
_putchar('%');
_putchar(str);
count = count + 2;
}
return (count);
}
/**
* _printf - prints anything, followed by a new line
* @format: a list of types of arguments passed to the function
* Return: int
*/
int _printf(const char *format, ...)
{
va_list list;
unsigned int i, len, match;
va_start(list, format);
if (format == NULL)
{
return (-1);
}
len = 0;
i = 0;
while (format != NULL && format[i] != '\0')
{
if (format[i] != '%')
{
_putchar(format[i]);
len = len + 1;
}
else if (format[i] == '%' && format[i + 1] == '\0')
{
return (-1);
}
else
{
match = print_type(format + i, list);
i = i + 1;
len = len + match;
}
i = i + 1;
}
va_end(list);
return (len);
}